tun.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. /*
  2. * TUN - Universal TUN/TAP device driver.
  3. * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
  4. * Copyright (c) 2015 Samsung Electronics Co., Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
  17. */
  18. /*
  19. * Changes:
  20. *
  21. * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
  22. * Add TUNSETLINK ioctl to set the link encapsulation
  23. *
  24. * Mark Smith <markzzzsmith@yahoo.com.au>
  25. * Use random_ether_addr() for tap MAC address.
  26. *
  27. * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
  28. * Fixes in packet dropping, queue length setting and queue wakeup.
  29. * Increased default tx queue length.
  30. * Added ethtool API.
  31. * Minor cleanups
  32. *
  33. * Daniel Podlejski <underley@underley.eu.org>
  34. * Modifications for 2.3.99-pre5 kernel.
  35. */
  36. /*
  37. * Changes:
  38. * KwnagHyun Kim <kh0304.kim@samsung.com> 2015/07/08
  39. * Baesung Park <baesung.park@samsung.com> 2015/07/08
  40. * Vignesh Saravanaperumal <vignesh1.s@samsung.com> 2015/07/08
  41. * Add codes to share UID/PID information
  42. *
  43. */
  44. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  45. #define DRV_NAME "tun"
  46. #define DRV_VERSION "1.6"
  47. #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
  48. #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
  49. #include <linux/module.h>
  50. #include <linux/errno.h>
  51. #include <linux/kernel.h>
  52. #include <linux/major.h>
  53. #include <linux/slab.h>
  54. #include <linux/poll.h>
  55. #include <linux/fcntl.h>
  56. #include <linux/init.h>
  57. #include <linux/skbuff.h>
  58. #include <linux/netdevice.h>
  59. #include <linux/etherdevice.h>
  60. #include <linux/miscdevice.h>
  61. #include <linux/ethtool.h>
  62. #include <linux/rtnetlink.h>
  63. #include <linux/compat.h>
  64. #include <linux/if.h>
  65. #include <linux/if_arp.h>
  66. #include <linux/if_ether.h>
  67. #include <linux/if_tun.h>
  68. #include <linux/crc32.h>
  69. #include <linux/nsproxy.h>
  70. #include <linux/virtio_net.h>
  71. #include <linux/rcupdate.h>
  72. #include <net/ipv6.h>
  73. #include <net/net_namespace.h>
  74. #include <net/netns/generic.h>
  75. #include <net/rtnetlink.h>
  76. #include <net/sock.h>
  77. #include <net/cls_cgroup.h>
  78. #include <asm/uaccess.h>
  79. /* Uncomment to enable debugging */
  80. #define TUN_DEBUG 1
  81. #ifdef TUN_DEBUG
  82. static int debug;
  83. #define tun_debug(level, tun, fmt, args...) \
  84. do { \
  85. if (tun->debug) \
  86. netdev_printk(level, tun->dev, fmt, ##args); \
  87. } while (0)
  88. #define DBG1(level, fmt, args...) \
  89. do { \
  90. if (debug == 2) \
  91. printk(level fmt, ##args); \
  92. } while (0)
  93. #else
  94. #define tun_debug(level, tun, fmt, args...) \
  95. do { \
  96. if (0) \
  97. netdev_printk(level, tun->dev, fmt, ##args); \
  98. } while (0)
  99. #define DBG1(level, fmt, args...) \
  100. do { \
  101. if (0) \
  102. printk(level fmt, ##args); \
  103. } while (0)
  104. #endif
  105. #define GOODCOPY_LEN 128
  106. #define FLT_EXACT_COUNT 8
  107. struct tap_filter {
  108. unsigned int count; /* Number of addrs. Zero means disabled */
  109. u32 mask[2]; /* Mask of the hashed addrs */
  110. unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
  111. };
  112. struct tun_file {
  113. atomic_t count;
  114. struct tun_struct *tun;
  115. struct net *net;
  116. };
  117. struct tun_sock;
  118. struct tun_struct {
  119. struct tun_file *tfile;
  120. unsigned int flags;
  121. kuid_t owner;
  122. kgid_t group;
  123. struct net_device *dev;
  124. netdev_features_t set_features;
  125. #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
  126. NETIF_F_TSO6|NETIF_F_UFO)
  127. struct fasync_struct *fasync;
  128. struct tap_filter txflt;
  129. struct socket socket;
  130. struct socket_wq wq;
  131. int vnet_hdr_sz;
  132. #ifdef TUN_DEBUG
  133. int debug;
  134. #endif
  135. };
  136. struct tun_sock {
  137. struct sock sk;
  138. struct tun_struct *tun;
  139. };
  140. static inline struct tun_sock *tun_sk(struct sock *sk)
  141. {
  142. return container_of(sk, struct tun_sock, sk);
  143. }
  144. static int tun_attach(struct tun_struct *tun, struct file *file)
  145. {
  146. struct tun_file *tfile = file->private_data;
  147. int err;
  148. ASSERT_RTNL();
  149. netif_tx_lock_bh(tun->dev);
  150. err = -EINVAL;
  151. if (tfile->tun)
  152. goto out;
  153. err = -EBUSY;
  154. if (tun->tfile)
  155. goto out;
  156. err = 0;
  157. tfile->tun = tun;
  158. tun->tfile = tfile;
  159. tun->socket.file = file;
  160. netif_carrier_on(tun->dev);
  161. dev_hold(tun->dev);
  162. sock_hold(tun->socket.sk);
  163. atomic_inc(&tfile->count);
  164. out:
  165. netif_tx_unlock_bh(tun->dev);
  166. return err;
  167. }
  168. static void __tun_detach(struct tun_struct *tun)
  169. {
  170. /* Detach from net device */
  171. netif_tx_lock_bh(tun->dev);
  172. netif_carrier_off(tun->dev);
  173. tun->tfile = NULL;
  174. netif_tx_unlock_bh(tun->dev);
  175. /* Drop read queue */
  176. skb_queue_purge(&tun->socket.sk->sk_receive_queue);
  177. /* Drop the extra count on the net device */
  178. dev_put(tun->dev);
  179. }
  180. static void tun_detach(struct tun_struct *tun)
  181. {
  182. rtnl_lock();
  183. __tun_detach(tun);
  184. rtnl_unlock();
  185. }
  186. static struct tun_struct *__tun_get(struct tun_file *tfile)
  187. {
  188. struct tun_struct *tun = NULL;
  189. if (atomic_inc_not_zero(&tfile->count))
  190. tun = tfile->tun;
  191. return tun;
  192. }
  193. static struct tun_struct *tun_get(struct file *file)
  194. {
  195. return __tun_get(file->private_data);
  196. }
  197. static void tun_put(struct tun_struct *tun)
  198. {
  199. struct tun_file *tfile = tun->tfile;
  200. if (atomic_dec_and_test(&tfile->count))
  201. tun_detach(tfile->tun);
  202. }
  203. /* TAP filtering */
  204. static void addr_hash_set(u32 *mask, const u8 *addr)
  205. {
  206. int n = ether_crc(ETH_ALEN, addr) >> 26;
  207. mask[n >> 5] |= (1 << (n & 31));
  208. }
  209. static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
  210. {
  211. int n = ether_crc(ETH_ALEN, addr) >> 26;
  212. return mask[n >> 5] & (1 << (n & 31));
  213. }
  214. static int update_filter(struct tap_filter *filter, void __user *arg)
  215. {
  216. struct { u8 u[ETH_ALEN]; } *addr;
  217. struct tun_filter uf;
  218. int err, alen, n, nexact;
  219. if (copy_from_user(&uf, arg, sizeof(uf)))
  220. return -EFAULT;
  221. if (!uf.count) {
  222. /* Disabled */
  223. filter->count = 0;
  224. return 0;
  225. }
  226. alen = ETH_ALEN * uf.count;
  227. addr = kmalloc(alen, GFP_KERNEL);
  228. if (!addr)
  229. return -ENOMEM;
  230. if (copy_from_user(addr, arg + sizeof(uf), alen)) {
  231. err = -EFAULT;
  232. goto done;
  233. }
  234. /* The filter is updated without holding any locks. Which is
  235. * perfectly safe. We disable it first and in the worst
  236. * case we'll accept a few undesired packets. */
  237. filter->count = 0;
  238. wmb();
  239. /* Use first set of addresses as an exact filter */
  240. for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
  241. memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
  242. nexact = n;
  243. /* Remaining multicast addresses are hashed,
  244. * unicast will leave the filter disabled. */
  245. memset(filter->mask, 0, sizeof(filter->mask));
  246. for (; n < uf.count; n++) {
  247. if (!is_multicast_ether_addr(addr[n].u)) {
  248. err = 0; /* no filter */
  249. goto done;
  250. }
  251. addr_hash_set(filter->mask, addr[n].u);
  252. }
  253. /* For ALLMULTI just set the mask to all ones.
  254. * This overrides the mask populated above. */
  255. if ((uf.flags & TUN_FLT_ALLMULTI))
  256. memset(filter->mask, ~0, sizeof(filter->mask));
  257. /* Now enable the filter */
  258. wmb();
  259. filter->count = nexact;
  260. /* Return the number of exact filters */
  261. err = nexact;
  262. done:
  263. kfree(addr);
  264. return err;
  265. }
  266. /* Returns: 0 - drop, !=0 - accept */
  267. static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
  268. {
  269. /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
  270. * at this point. */
  271. struct ethhdr *eh = (struct ethhdr *) skb->data;
  272. int i;
  273. /* Exact match */
  274. for (i = 0; i < filter->count; i++)
  275. if (!compare_ether_addr(eh->h_dest, filter->addr[i]))
  276. return 1;
  277. /* Inexact match (multicast only) */
  278. if (is_multicast_ether_addr(eh->h_dest))
  279. return addr_hash_test(filter->mask, eh->h_dest);
  280. return 0;
  281. }
  282. /*
  283. * Checks whether the packet is accepted or not.
  284. * Returns: 0 - drop, !=0 - accept
  285. */
  286. static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
  287. {
  288. if (!filter->count)
  289. return 1;
  290. return run_filter(filter, skb);
  291. }
  292. /* Network device part of the driver */
  293. static const struct ethtool_ops tun_ethtool_ops;
  294. /* Net device detach from fd. */
  295. static void tun_net_uninit(struct net_device *dev)
  296. {
  297. struct tun_struct *tun = netdev_priv(dev);
  298. struct tun_file *tfile = tun->tfile;
  299. /* Inform the methods they need to stop using the dev.
  300. */
  301. if (tfile) {
  302. wake_up_all(&tun->wq.wait);
  303. if (atomic_dec_and_test(&tfile->count))
  304. __tun_detach(tun);
  305. }
  306. }
  307. static void tun_free_netdev(struct net_device *dev)
  308. {
  309. struct tun_struct *tun = netdev_priv(dev);
  310. BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED, &tun->socket.flags));
  311. sk_release_kernel(tun->socket.sk);
  312. }
  313. /* Net device open. */
  314. static int tun_net_open(struct net_device *dev)
  315. {
  316. netif_start_queue(dev);
  317. return 0;
  318. }
  319. /* Net device close. */
  320. static int tun_net_close(struct net_device *dev)
  321. {
  322. netif_stop_queue(dev);
  323. return 0;
  324. }
  325. /* Net device start xmit */
  326. static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
  327. {
  328. struct tun_struct *tun = netdev_priv(dev);
  329. tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
  330. /* Drop packet if interface is not attached */
  331. if (!tun->tfile)
  332. goto drop;
  333. /* Drop if the filter does not like it.
  334. * This is a noop if the filter is disabled.
  335. * Filter can be enabled only for the TAP devices. */
  336. if (!check_filter(&tun->txflt, skb))
  337. goto drop;
  338. if (tun->socket.sk->sk_filter &&
  339. sk_filter(tun->socket.sk, skb))
  340. goto drop;
  341. if (skb_queue_len(&tun->socket.sk->sk_receive_queue) >= dev->tx_queue_len) {
  342. if (!(tun->flags & TUN_ONE_QUEUE)) {
  343. /* Normal queueing mode. */
  344. /* Packet scheduler handles dropping of further packets. */
  345. netif_stop_queue(dev);
  346. /* We won't see all dropped packets individually, so overrun
  347. * error is more appropriate. */
  348. dev->stats.tx_fifo_errors++;
  349. } else {
  350. /* Single queue mode.
  351. * Driver handles dropping of all packets itself. */
  352. goto drop;
  353. }
  354. }
  355. /* Orphan the skb - required as we might hang on to it
  356. * for indefinite time. */
  357. skb_orphan(skb);
  358. nf_reset(skb);
  359. /* Enqueue packet */
  360. skb_queue_tail(&tun->socket.sk->sk_receive_queue, skb);
  361. /* Notify and wake up reader process */
  362. if (tun->flags & TUN_FASYNC)
  363. kill_fasync(&tun->fasync, SIGIO, POLL_IN);
  364. wake_up_interruptible_poll(&tun->wq.wait, POLLIN |
  365. POLLRDNORM | POLLRDBAND);
  366. return NETDEV_TX_OK;
  367. drop:
  368. dev->stats.tx_dropped++;
  369. kfree_skb(skb);
  370. return NETDEV_TX_OK;
  371. }
  372. static void tun_net_mclist(struct net_device *dev)
  373. {
  374. /*
  375. * This callback is supposed to deal with mc filter in
  376. * _rx_ path and has nothing to do with the _tx_ path.
  377. * In rx path we always accept everything userspace gives us.
  378. */
  379. }
  380. #define MIN_MTU 68
  381. #define MAX_MTU 65535
  382. static int
  383. tun_net_change_mtu(struct net_device *dev, int new_mtu)
  384. {
  385. if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
  386. return -EINVAL;
  387. dev->mtu = new_mtu;
  388. return 0;
  389. }
  390. static netdev_features_t tun_net_fix_features(struct net_device *dev,
  391. netdev_features_t features)
  392. {
  393. struct tun_struct *tun = netdev_priv(dev);
  394. return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
  395. }
  396. #ifdef CONFIG_NET_POLL_CONTROLLER
  397. static void tun_poll_controller(struct net_device *dev)
  398. {
  399. /*
  400. * Tun only receives frames when:
  401. * 1) the char device endpoint gets data from user space
  402. * 2) the tun socket gets a sendmsg call from user space
  403. * Since both of those are syncronous operations, we are guaranteed
  404. * never to have pending data when we poll for it
  405. * so theres nothing to do here but return.
  406. * We need this though so netpoll recognizes us as an interface that
  407. * supports polling, which enables bridge devices in virt setups to
  408. * still use netconsole
  409. */
  410. return;
  411. }
  412. #endif
  413. static const struct net_device_ops tun_netdev_ops = {
  414. .ndo_uninit = tun_net_uninit,
  415. .ndo_open = tun_net_open,
  416. .ndo_stop = tun_net_close,
  417. .ndo_start_xmit = tun_net_xmit,
  418. .ndo_change_mtu = tun_net_change_mtu,
  419. .ndo_fix_features = tun_net_fix_features,
  420. #ifdef CONFIG_NET_POLL_CONTROLLER
  421. .ndo_poll_controller = tun_poll_controller,
  422. #endif
  423. };
  424. static const struct net_device_ops tap_netdev_ops = {
  425. .ndo_uninit = tun_net_uninit,
  426. .ndo_open = tun_net_open,
  427. .ndo_stop = tun_net_close,
  428. .ndo_start_xmit = tun_net_xmit,
  429. .ndo_change_mtu = tun_net_change_mtu,
  430. .ndo_fix_features = tun_net_fix_features,
  431. .ndo_set_rx_mode = tun_net_mclist,
  432. .ndo_set_mac_address = eth_mac_addr,
  433. .ndo_validate_addr = eth_validate_addr,
  434. #ifdef CONFIG_NET_POLL_CONTROLLER
  435. .ndo_poll_controller = tun_poll_controller,
  436. #endif
  437. };
  438. /* Initialize net device. */
  439. static void tun_net_init(struct net_device *dev)
  440. {
  441. struct tun_struct *tun = netdev_priv(dev);
  442. switch (tun->flags & TUN_TYPE_MASK) {
  443. case TUN_TUN_DEV:
  444. dev->netdev_ops = &tun_netdev_ops;
  445. /* Point-to-Point TUN Device */
  446. dev->hard_header_len = 0;
  447. dev->addr_len = 0;
  448. dev->mtu = 1500;
  449. /* Zero header length */
  450. dev->type = ARPHRD_NONE;
  451. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  452. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  453. break;
  454. case TUN_TAP_DEV:
  455. dev->netdev_ops = &tap_netdev_ops;
  456. /* Ethernet TAP Device */
  457. ether_setup(dev);
  458. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  459. eth_hw_addr_random(dev);
  460. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  461. break;
  462. }
  463. }
  464. /* Character device part */
  465. /* Poll */
  466. static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
  467. {
  468. struct tun_file *tfile = file->private_data;
  469. struct tun_struct *tun = __tun_get(tfile);
  470. struct sock *sk;
  471. unsigned int mask = 0;
  472. if (!tun)
  473. return POLLERR;
  474. sk = tun->socket.sk;
  475. tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
  476. poll_wait(file, &tun->wq.wait, wait);
  477. if (!skb_queue_empty(&sk->sk_receive_queue))
  478. mask |= POLLIN | POLLRDNORM;
  479. if (sock_writeable(sk) ||
  480. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
  481. sock_writeable(sk)))
  482. mask |= POLLOUT | POLLWRNORM;
  483. if (tun->dev->reg_state != NETREG_REGISTERED)
  484. mask = POLLERR;
  485. tun_put(tun);
  486. return mask;
  487. }
  488. /* prepad is the amount to reserve at front. len is length after that.
  489. * linear is a hint as to how much to copy (usually headers). */
  490. static struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
  491. size_t prepad, size_t len,
  492. size_t linear, int noblock)
  493. {
  494. struct sock *sk = tun->socket.sk;
  495. struct sk_buff *skb;
  496. int err;
  497. sock_update_classid(sk, current);
  498. /* Under a page? Don't bother with paged skb. */
  499. if (prepad + len < PAGE_SIZE || !linear)
  500. linear = len;
  501. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  502. &err);
  503. if (!skb)
  504. return ERR_PTR(err);
  505. skb_reserve(skb, prepad);
  506. skb_put(skb, linear);
  507. skb->data_len = len - linear;
  508. skb->len += len - linear;
  509. return skb;
  510. }
  511. /* set skb frags from iovec, this can move to core network code for reuse */
  512. static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
  513. int offset, size_t count)
  514. {
  515. int len = iov_length(from, count) - offset;
  516. int copy = skb_headlen(skb);
  517. int size, offset1 = 0;
  518. int i = 0;
  519. /* Skip over from offset */
  520. while (count && (offset >= from->iov_len)) {
  521. offset -= from->iov_len;
  522. ++from;
  523. --count;
  524. }
  525. /* copy up to skb headlen */
  526. while (count && (copy > 0)) {
  527. size = min_t(unsigned int, copy, from->iov_len - offset);
  528. if (copy_from_user(skb->data + offset1, from->iov_base + offset,
  529. size))
  530. return -EFAULT;
  531. if (copy > size) {
  532. ++from;
  533. --count;
  534. offset = 0;
  535. } else
  536. offset += size;
  537. copy -= size;
  538. offset1 += size;
  539. }
  540. if (len == offset1)
  541. return 0;
  542. while (count--) {
  543. struct page *page[MAX_SKB_FRAGS];
  544. int num_pages;
  545. unsigned long base;
  546. unsigned long truesize;
  547. len = from->iov_len - offset;
  548. if (!len) {
  549. offset = 0;
  550. ++from;
  551. continue;
  552. }
  553. base = (unsigned long)from->iov_base + offset;
  554. size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
  555. if (i + size > MAX_SKB_FRAGS)
  556. return -EMSGSIZE;
  557. num_pages = get_user_pages_fast(base, size, 0, &page[i]);
  558. if (num_pages != size) {
  559. for (i = 0; i < num_pages; i++)
  560. put_page(page[i]);
  561. return -EFAULT;
  562. }
  563. truesize = size * PAGE_SIZE;
  564. skb->data_len += len;
  565. skb->len += len;
  566. skb->truesize += truesize;
  567. atomic_add(truesize, &skb->sk->sk_wmem_alloc);
  568. while (len) {
  569. int off = base & ~PAGE_MASK;
  570. int size = min_t(int, len, PAGE_SIZE - off);
  571. __skb_fill_page_desc(skb, i, page[i], off, size);
  572. skb_shinfo(skb)->nr_frags++;
  573. /* increase sk_wmem_alloc */
  574. base += size;
  575. len -= size;
  576. i++;
  577. }
  578. offset = 0;
  579. ++from;
  580. }
  581. return 0;
  582. }
  583. /* Get packet from user space buffer */
  584. static ssize_t tun_get_user(struct tun_struct *tun, void *msg_control,
  585. const struct iovec *iv, size_t total_len,
  586. size_t count, int noblock)
  587. {
  588. struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
  589. struct sk_buff *skb;
  590. size_t len = total_len, align = NET_SKB_PAD;
  591. struct virtio_net_hdr gso = { 0 };
  592. int offset = 0;
  593. int copylen;
  594. bool zerocopy = false;
  595. int err;
  596. if (!(tun->flags & TUN_NO_PI)) {
  597. if ((len -= sizeof(pi)) > total_len)
  598. return -EINVAL;
  599. len -= sizeof(pi);
  600. if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
  601. return -EFAULT;
  602. offset += sizeof(pi);
  603. }
  604. if (tun->flags & TUN_VNET_HDR) {
  605. if ((len -= tun->vnet_hdr_sz) > total_len)
  606. return -EINVAL;
  607. len -= tun->vnet_hdr_sz;
  608. if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
  609. return -EFAULT;
  610. if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  611. gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
  612. gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
  613. if (gso.hdr_len > len)
  614. return -EINVAL;
  615. offset += tun->vnet_hdr_sz;
  616. }
  617. if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
  618. align += NET_IP_ALIGN;
  619. if (unlikely(len < ETH_HLEN ||
  620. (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
  621. return -EINVAL;
  622. }
  623. if (msg_control)
  624. zerocopy = true;
  625. if (zerocopy) {
  626. /* Userspace may produce vectors with count greater than
  627. * MAX_SKB_FRAGS, so we need to linearize parts of the skb
  628. * to let the rest of data to be fit in the frags.
  629. */
  630. if (count > MAX_SKB_FRAGS) {
  631. copylen = iov_length(iv, count - MAX_SKB_FRAGS);
  632. if (copylen < offset)
  633. copylen = 0;
  634. else
  635. copylen -= offset;
  636. } else
  637. copylen = 0;
  638. /* There are 256 bytes to be copied in skb, so there is enough
  639. * room for skb expand head in case it is used.
  640. * The rest of the buffer is mapped from userspace.
  641. */
  642. if (copylen < gso.hdr_len)
  643. copylen = gso.hdr_len;
  644. if (!copylen)
  645. copylen = GOODCOPY_LEN;
  646. } else
  647. copylen = len;
  648. skb = tun_alloc_skb(tun, align, copylen, gso.hdr_len, noblock);
  649. if (IS_ERR(skb)) {
  650. if (PTR_ERR(skb) != -EAGAIN)
  651. tun->dev->stats.rx_dropped++;
  652. return PTR_ERR(skb);
  653. }
  654. if (zerocopy)
  655. err = zerocopy_sg_from_iovec(skb, iv, offset, count);
  656. else
  657. err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
  658. if (err) {
  659. tun->dev->stats.rx_dropped++;
  660. kfree_skb(skb);
  661. return -EFAULT;
  662. }
  663. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  664. if (!skb_partial_csum_set(skb, gso.csum_start,
  665. gso.csum_offset)) {
  666. tun->dev->stats.rx_frame_errors++;
  667. kfree_skb(skb);
  668. return -EINVAL;
  669. }
  670. }
  671. switch (tun->flags & TUN_TYPE_MASK) {
  672. case TUN_TUN_DEV:
  673. if (tun->flags & TUN_NO_PI) {
  674. switch (skb->data[0] & 0xf0) {
  675. case 0x40:
  676. pi.proto = htons(ETH_P_IP);
  677. break;
  678. case 0x60:
  679. pi.proto = htons(ETH_P_IPV6);
  680. break;
  681. default:
  682. tun->dev->stats.rx_dropped++;
  683. kfree_skb(skb);
  684. return -EINVAL;
  685. }
  686. }
  687. skb_reset_mac_header(skb);
  688. skb->protocol = pi.proto;
  689. skb->dev = tun->dev;
  690. break;
  691. case TUN_TAP_DEV:
  692. skb->protocol = eth_type_trans(skb, tun->dev);
  693. break;
  694. }
  695. skb_reset_network_header(skb);
  696. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  697. pr_debug("GSO!\n");
  698. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  699. case VIRTIO_NET_HDR_GSO_TCPV4:
  700. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  701. break;
  702. case VIRTIO_NET_HDR_GSO_TCPV6:
  703. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  704. break;
  705. case VIRTIO_NET_HDR_GSO_UDP:
  706. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  707. if (skb->protocol == htons(ETH_P_IPV6))
  708. ipv6_proxy_select_ident(dev_net(skb->dev), skb);
  709. break;
  710. default:
  711. tun->dev->stats.rx_frame_errors++;
  712. kfree_skb(skb);
  713. return -EINVAL;
  714. }
  715. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  716. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  717. skb_shinfo(skb)->gso_size = gso.gso_size;
  718. if (skb_shinfo(skb)->gso_size == 0) {
  719. tun->dev->stats.rx_frame_errors++;
  720. kfree_skb(skb);
  721. return -EINVAL;
  722. }
  723. /* Header must be checked, and gso_segs computed. */
  724. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  725. skb_shinfo(skb)->gso_segs = 0;
  726. }
  727. /* copy skb_ubuf_info for callback when skb has no error */
  728. if (zerocopy) {
  729. skb_shinfo(skb)->destructor_arg = msg_control;
  730. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  731. }
  732. netif_rx_ni(skb);
  733. tun->dev->stats.rx_packets++;
  734. tun->dev->stats.rx_bytes += len;
  735. return total_len;
  736. }
  737. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  738. unsigned long count, loff_t pos)
  739. {
  740. struct file *file = iocb->ki_filp;
  741. struct tun_struct *tun = tun_get(file);
  742. ssize_t result;
  743. if (!tun)
  744. return -EBADFD;
  745. tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
  746. result = tun_get_user(tun, NULL, iv, iov_length(iv, count), count,
  747. file->f_flags & O_NONBLOCK);
  748. tun_put(tun);
  749. return result;
  750. }
  751. /* Put packet to the user space buffer */
  752. static ssize_t tun_put_user(struct tun_struct *tun,
  753. struct sk_buff *skb,
  754. const struct iovec *iv, int len)
  755. {
  756. struct tun_pi pi = { 0, skb->protocol };
  757. ssize_t total = 0;
  758. if (!(tun->flags & TUN_NO_PI)) {
  759. if ((len -= sizeof(pi)) < 0)
  760. return -EINVAL;
  761. if (len < skb->len) {
  762. /* Packet will be striped */
  763. pi.flags |= TUN_PKT_STRIP;
  764. }
  765. if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
  766. return -EFAULT;
  767. total += sizeof(pi);
  768. }
  769. if (tun->flags & TUN_VNET_HDR) {
  770. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  771. if ((len -= tun->vnet_hdr_sz) < 0)
  772. return -EINVAL;
  773. if (skb_is_gso(skb)) {
  774. struct skb_shared_info *sinfo = skb_shinfo(skb);
  775. /* This is a hint as to how much should be linear. */
  776. gso.hdr_len = skb_headlen(skb);
  777. gso.gso_size = sinfo->gso_size;
  778. if (sinfo->gso_type & SKB_GSO_TCPV4)
  779. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  780. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  781. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  782. else if (sinfo->gso_type & SKB_GSO_UDP)
  783. gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  784. else {
  785. pr_err("unexpected GSO type: "
  786. "0x%x, gso_size %d, hdr_len %d\n",
  787. sinfo->gso_type, gso.gso_size,
  788. gso.hdr_len);
  789. print_hex_dump(KERN_ERR, "tun: ",
  790. DUMP_PREFIX_NONE,
  791. 16, 1, skb->head,
  792. min((int)gso.hdr_len, 64), true);
  793. WARN_ON_ONCE(1);
  794. return -EINVAL;
  795. }
  796. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  797. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  798. } else
  799. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  800. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  801. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  802. gso.csum_start = skb_checksum_start_offset(skb);
  803. gso.csum_offset = skb->csum_offset;
  804. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  805. gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
  806. } /* else everything is zero */
  807. if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
  808. sizeof(gso))))
  809. return -EFAULT;
  810. total += tun->vnet_hdr_sz;
  811. }
  812. len = min_t(int, skb->len, len);
  813. skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
  814. total += skb->len;
  815. tun->dev->stats.tx_packets++;
  816. tun->dev->stats.tx_bytes += len;
  817. return total;
  818. }
  819. static ssize_t tun_do_read(struct tun_struct *tun,
  820. struct kiocb *iocb, const struct iovec *iv,
  821. ssize_t len, int noblock)
  822. {
  823. DECLARE_WAITQUEUE(wait, current);
  824. struct sk_buff *skb;
  825. ssize_t ret = 0;
  826. tun_debug(KERN_INFO, tun, "tun_chr_read\n");
  827. if (unlikely(!noblock))
  828. add_wait_queue(&tun->wq.wait, &wait);
  829. while (len) {
  830. current->state = TASK_INTERRUPTIBLE;
  831. /* Read frames from the queue */
  832. if (!(skb=skb_dequeue(&tun->socket.sk->sk_receive_queue))) {
  833. if (noblock) {
  834. ret = -EAGAIN;
  835. break;
  836. }
  837. if (signal_pending(current)) {
  838. ret = -ERESTARTSYS;
  839. break;
  840. }
  841. if (tun->dev->reg_state != NETREG_REGISTERED) {
  842. ret = -EIO;
  843. break;
  844. }
  845. /* Nothing to read, let's sleep */
  846. schedule();
  847. continue;
  848. }
  849. netif_wake_queue(tun->dev);
  850. ret = tun_put_user(tun, skb, iv, len);
  851. kfree_skb(skb);
  852. break;
  853. }
  854. current->state = TASK_RUNNING;
  855. if (unlikely(!noblock))
  856. remove_wait_queue(&tun->wq.wait, &wait);
  857. return ret;
  858. }
  859. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  860. unsigned long count, loff_t pos)
  861. {
  862. struct file *file = iocb->ki_filp;
  863. struct tun_file *tfile = file->private_data;
  864. struct tun_struct *tun = __tun_get(tfile);
  865. ssize_t len, ret;
  866. if (!tun)
  867. return -EBADFD;
  868. len = iov_length(iv, count);
  869. if (len < 0) {
  870. ret = -EINVAL;
  871. goto out;
  872. }
  873. ret = tun_do_read(tun, iocb, iv, len, file->f_flags & O_NONBLOCK);
  874. ret = min_t(ssize_t, ret, len);
  875. if (ret > 0)
  876. iocb->ki_pos = ret;
  877. out:
  878. tun_put(tun);
  879. return ret;
  880. }
  881. static void tun_setup(struct net_device *dev)
  882. {
  883. struct tun_struct *tun = netdev_priv(dev);
  884. tun->owner = INVALID_UID;
  885. tun->group = INVALID_GID;
  886. dev->ethtool_ops = &tun_ethtool_ops;
  887. dev->destructor = tun_free_netdev;
  888. }
  889. /* Trivial set of netlink ops to allow deleting tun or tap
  890. * device with netlink.
  891. */
  892. static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
  893. {
  894. return -EINVAL;
  895. }
  896. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  897. .kind = DRV_NAME,
  898. .priv_size = sizeof(struct tun_struct),
  899. .setup = tun_setup,
  900. .validate = tun_validate,
  901. };
  902. static void tun_sock_write_space(struct sock *sk)
  903. {
  904. struct tun_struct *tun;
  905. wait_queue_head_t *wqueue;
  906. if (!sock_writeable(sk))
  907. return;
  908. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  909. return;
  910. wqueue = sk_sleep(sk);
  911. if (wqueue && waitqueue_active(wqueue))
  912. wake_up_interruptible_sync_poll(wqueue, POLLOUT |
  913. POLLWRNORM | POLLWRBAND);
  914. tun = tun_sk(sk)->tun;
  915. kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
  916. }
  917. static void tun_sock_destruct(struct sock *sk)
  918. {
  919. free_netdev(tun_sk(sk)->tun->dev);
  920. }
  921. static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
  922. struct msghdr *m, size_t total_len)
  923. {
  924. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  925. return tun_get_user(tun, m->msg_control, m->msg_iov, total_len,
  926. m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
  927. }
  928. static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
  929. struct msghdr *m, size_t total_len,
  930. int flags)
  931. {
  932. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  933. int ret;
  934. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  935. return -EINVAL;
  936. ret = tun_do_read(tun, iocb, m->msg_iov, total_len,
  937. flags & MSG_DONTWAIT);
  938. if (ret > total_len) {
  939. m->msg_flags |= MSG_TRUNC;
  940. ret = flags & MSG_TRUNC ? ret : total_len;
  941. }
  942. return ret;
  943. }
  944. static int tun_release(struct socket *sock)
  945. {
  946. if (sock->sk)
  947. sock_put(sock->sk);
  948. return 0;
  949. }
  950. /* Ops structure to mimic raw sockets with tun */
  951. static const struct proto_ops tun_socket_ops = {
  952. .sendmsg = tun_sendmsg,
  953. .recvmsg = tun_recvmsg,
  954. .release = tun_release,
  955. };
  956. static struct proto tun_proto = {
  957. .name = "tun",
  958. .owner = THIS_MODULE,
  959. .obj_size = sizeof(struct tun_sock),
  960. };
  961. static int tun_flags(struct tun_struct *tun)
  962. {
  963. int flags = 0;
  964. if (tun->flags & TUN_TUN_DEV)
  965. flags |= IFF_TUN;
  966. else
  967. flags |= IFF_TAP;
  968. if (tun->flags & TUN_NO_PI)
  969. flags |= IFF_NO_PI;
  970. if (tun->flags & TUN_ONE_QUEUE)
  971. flags |= IFF_ONE_QUEUE;
  972. if (tun->flags & TUN_VNET_HDR)
  973. flags |= IFF_VNET_HDR;
  974. return flags;
  975. }
  976. static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
  977. char *buf)
  978. {
  979. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  980. return sprintf(buf, "0x%x\n", tun_flags(tun));
  981. }
  982. static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
  983. char *buf)
  984. {
  985. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  986. return uid_valid(tun->owner)?
  987. sprintf(buf, "%u\n",
  988. from_kuid_munged(current_user_ns(), tun->owner)):
  989. sprintf(buf, "-1\n");
  990. }
  991. static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
  992. char *buf)
  993. {
  994. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  995. return gid_valid(tun->group) ?
  996. sprintf(buf, "%u\n",
  997. from_kgid_munged(current_user_ns(), tun->group)):
  998. sprintf(buf, "-1\n");
  999. }
  1000. static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
  1001. static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
  1002. static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
  1003. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  1004. {
  1005. struct sock *sk;
  1006. struct tun_struct *tun;
  1007. struct net_device *dev;
  1008. int err;
  1009. dev = __dev_get_by_name(net, ifr->ifr_name);
  1010. if (dev) {
  1011. const struct cred *cred = current_cred();
  1012. if (ifr->ifr_flags & IFF_TUN_EXCL)
  1013. return -EBUSY;
  1014. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  1015. tun = netdev_priv(dev);
  1016. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  1017. tun = netdev_priv(dev);
  1018. else
  1019. return -EINVAL;
  1020. if (((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
  1021. (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
  1022. !capable(CAP_NET_ADMIN))
  1023. return -EPERM;
  1024. err = security_tun_dev_attach(tun->socket.sk);
  1025. if (err < 0)
  1026. return err;
  1027. err = tun_attach(tun, file);
  1028. if (err < 0)
  1029. return err;
  1030. }
  1031. else {
  1032. char *name;
  1033. unsigned long flags = 0;
  1034. if (!capable(CAP_NET_ADMIN))
  1035. return -EPERM;
  1036. err = security_tun_dev_create();
  1037. if (err < 0)
  1038. return err;
  1039. /* Set dev type */
  1040. if (ifr->ifr_flags & IFF_TUN) {
  1041. /* TUN device */
  1042. flags |= TUN_TUN_DEV;
  1043. name = "tun%d";
  1044. } else if (ifr->ifr_flags & IFF_TAP) {
  1045. /* TAP device */
  1046. flags |= TUN_TAP_DEV;
  1047. name = "tap%d";
  1048. } else
  1049. return -EINVAL;
  1050. if (*ifr->ifr_name)
  1051. name = ifr->ifr_name;
  1052. dev = alloc_netdev(sizeof(struct tun_struct), name,
  1053. tun_setup);
  1054. if (!dev)
  1055. return -ENOMEM;
  1056. dev_net_set(dev, net);
  1057. dev->rtnl_link_ops = &tun_link_ops;
  1058. tun = netdev_priv(dev);
  1059. tun->dev = dev;
  1060. tun->flags = flags;
  1061. tun->txflt.count = 0;
  1062. tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  1063. set_bit(SOCK_EXTERNALLY_ALLOCATED, &tun->socket.flags);
  1064. err = -ENOMEM;
  1065. sk = sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
  1066. if (!sk)
  1067. goto err_free_dev;
  1068. sk_change_net(sk, net);
  1069. tun->socket.wq = &tun->wq;
  1070. init_waitqueue_head(&tun->wq.wait);
  1071. tun->socket.ops = &tun_socket_ops;
  1072. sock_init_data(&tun->socket, sk);
  1073. sk->sk_write_space = tun_sock_write_space;
  1074. sk->sk_sndbuf = INT_MAX;
  1075. sock_set_flag(sk, SOCK_ZEROCOPY);
  1076. tun_sk(sk)->tun = tun;
  1077. security_tun_dev_post_create(sk);
  1078. tun_net_init(dev);
  1079. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
  1080. TUN_USER_FEATURES;
  1081. dev->features = dev->hw_features;
  1082. err = register_netdevice(tun->dev);
  1083. if (err < 0)
  1084. goto err_free_sk;
  1085. if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
  1086. device_create_file(&tun->dev->dev, &dev_attr_owner) ||
  1087. device_create_file(&tun->dev->dev, &dev_attr_group))
  1088. pr_err("Failed to create tun sysfs files\n");
  1089. sk->sk_destruct = tun_sock_destruct;
  1090. err = tun_attach(tun, file);
  1091. if (err < 0)
  1092. goto failed;
  1093. }
  1094. tun_debug(KERN_INFO, tun, "tun_set_iff\n");
  1095. if (ifr->ifr_flags & IFF_NO_PI)
  1096. tun->flags |= TUN_NO_PI;
  1097. else
  1098. tun->flags &= ~TUN_NO_PI;
  1099. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  1100. tun->flags |= TUN_ONE_QUEUE;
  1101. else
  1102. tun->flags &= ~TUN_ONE_QUEUE;
  1103. if (ifr->ifr_flags & IFF_VNET_HDR)
  1104. tun->flags |= TUN_VNET_HDR;
  1105. else
  1106. tun->flags &= ~TUN_VNET_HDR;
  1107. /* Make sure persistent devices do not get stuck in
  1108. * xoff state.
  1109. */
  1110. if (netif_running(tun->dev))
  1111. netif_wake_queue(tun->dev);
  1112. strcpy(ifr->ifr_name, tun->dev->name);
  1113. return 0;
  1114. err_free_sk:
  1115. tun_free_netdev(dev);
  1116. err_free_dev:
  1117. free_netdev(dev);
  1118. failed:
  1119. return err;
  1120. }
  1121. static int tun_get_iff(struct net *net, struct tun_struct *tun,
  1122. struct ifreq *ifr)
  1123. {
  1124. tun_debug(KERN_INFO, tun, "tun_get_iff\n");
  1125. strcpy(ifr->ifr_name, tun->dev->name);
  1126. ifr->ifr_flags = tun_flags(tun);
  1127. return 0;
  1128. }
  1129. /* This is like a cut-down ethtool ops, except done via tun fd so no
  1130. * privs required. */
  1131. static int set_offload(struct tun_struct *tun, unsigned long arg)
  1132. {
  1133. netdev_features_t features = 0;
  1134. if (arg & TUN_F_CSUM) {
  1135. features |= NETIF_F_HW_CSUM;
  1136. arg &= ~TUN_F_CSUM;
  1137. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  1138. if (arg & TUN_F_TSO_ECN) {
  1139. features |= NETIF_F_TSO_ECN;
  1140. arg &= ~TUN_F_TSO_ECN;
  1141. }
  1142. if (arg & TUN_F_TSO4)
  1143. features |= NETIF_F_TSO;
  1144. if (arg & TUN_F_TSO6)
  1145. features |= NETIF_F_TSO6;
  1146. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  1147. }
  1148. if (arg & TUN_F_UFO) {
  1149. features |= NETIF_F_UFO;
  1150. arg &= ~TUN_F_UFO;
  1151. }
  1152. }
  1153. /* This gives the user a way to test for new features in future by
  1154. * trying to set them. */
  1155. if (arg)
  1156. return -EINVAL;
  1157. tun->set_features = features;
  1158. netdev_update_features(tun->dev);
  1159. return 0;
  1160. }
  1161. static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  1162. unsigned long arg, int ifreq_len)
  1163. {
  1164. struct tun_file *tfile = file->private_data;
  1165. struct tun_struct *tun;
  1166. void __user* argp = (void __user*)arg;
  1167. struct sock_fprog fprog;
  1168. struct ifreq ifr;
  1169. kuid_t owner;
  1170. kgid_t group;
  1171. int sndbuf;
  1172. int vnet_hdr_sz;
  1173. int ret;
  1174. #ifdef CONFIG_ANDROID_PARANOID_NETWORK
  1175. if (cmd != TUNGETIFF && !capable(CAP_NET_ADMIN)) {
  1176. return -EPERM;
  1177. }
  1178. #endif
  1179. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) {
  1180. if (copy_from_user(&ifr, argp, ifreq_len))
  1181. return -EFAULT;
  1182. } else {
  1183. memset(&ifr, 0, sizeof(ifr));
  1184. }
  1185. if (cmd == TUNGETFEATURES) {
  1186. /* Currently this just means: "what IFF flags are valid?".
  1187. * This is needed because we never checked for invalid flags on
  1188. * TUNSETIFF. */
  1189. }
  1190. rtnl_lock();
  1191. tun = __tun_get(tfile);
  1192. if (cmd == TUNSETIFF && !tun) {
  1193. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  1194. ret = tun_set_iff(tfile->net, file, &ifr);
  1195. if (ret)
  1196. goto unlock;
  1197. if (copy_to_user(argp, &ifr, ifreq_len))
  1198. ret = -EFAULT;
  1199. goto unlock;
  1200. }
  1201. ret = -EBADFD;
  1202. if (!tun)
  1203. goto unlock;
  1204. tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %d\n", cmd);
  1205. ret = 0;
  1206. switch (cmd) {
  1207. case TUNGETIFF:
  1208. ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
  1209. if (ret)
  1210. break;
  1211. if (copy_to_user(argp, &ifr, ifreq_len))
  1212. ret = -EFAULT;
  1213. break;
  1214. case TUNSETNOCSUM:
  1215. /* Disable/Enable checksum */
  1216. /* [unimplemented] */
  1217. tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
  1218. arg ? "disabled" : "enabled");
  1219. break;
  1220. case TUNSETPERSIST:
  1221. /* Disable/Enable persist mode */
  1222. if (arg)
  1223. tun->flags |= TUN_PERSIST;
  1224. else
  1225. tun->flags &= ~TUN_PERSIST;
  1226. tun_debug(KERN_INFO, tun, "persist %s\n",
  1227. arg ? "enabled" : "disabled");
  1228. break;
  1229. case TUNSETOWNER:
  1230. /* Set owner of the device */
  1231. owner = make_kuid(current_user_ns(), arg);
  1232. if (!uid_valid(owner)) {
  1233. ret = -EINVAL;
  1234. break;
  1235. }
  1236. tun->owner = owner;
  1237. tun_debug(KERN_INFO, tun, "owner set to %d\n",
  1238. from_kuid(&init_user_ns, tun->owner));
  1239. break;
  1240. case TUNSETGROUP:
  1241. /* Set group of the device */
  1242. group = make_kgid(current_user_ns(), arg);
  1243. if (!gid_valid(group)) {
  1244. ret = -EINVAL;
  1245. break;
  1246. }
  1247. tun->group = group;
  1248. tun_debug(KERN_INFO, tun, "group set to %d\n",
  1249. from_kgid(&init_user_ns, tun->group));
  1250. break;
  1251. case TUNSETLINK:
  1252. /* Only allow setting the type when the interface is down */
  1253. if (tun->dev->flags & IFF_UP) {
  1254. tun_debug(KERN_INFO, tun,
  1255. "Linktype set failed because interface is up\n");
  1256. ret = -EBUSY;
  1257. } else {
  1258. tun->dev->type = (int) arg;
  1259. tun_debug(KERN_INFO, tun, "linktype set to %d\n",
  1260. tun->dev->type);
  1261. ret = 0;
  1262. }
  1263. break;
  1264. #ifdef TUN_DEBUG
  1265. case TUNSETDEBUG:
  1266. tun->debug = arg;
  1267. break;
  1268. #endif
  1269. case TUNSETOFFLOAD:
  1270. ret = set_offload(tun, arg);
  1271. break;
  1272. case TUNSETTXFILTER:
  1273. /* Can be set only for TAPs */
  1274. ret = -EINVAL;
  1275. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1276. break;
  1277. ret = update_filter(&tun->txflt, (void __user *)arg);
  1278. break;
  1279. case SIOCGIFHWADDR:
  1280. /* Get hw address */
  1281. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  1282. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  1283. if (copy_to_user(argp, &ifr, ifreq_len))
  1284. ret = -EFAULT;
  1285. break;
  1286. case SIOCSIFHWADDR:
  1287. /* Set hw address */
  1288. tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
  1289. ifr.ifr_hwaddr.sa_data);
  1290. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  1291. break;
  1292. case TUNGETSNDBUF:
  1293. sndbuf = tun->socket.sk->sk_sndbuf;
  1294. if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
  1295. ret = -EFAULT;
  1296. break;
  1297. case TUNSETSNDBUF:
  1298. if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
  1299. ret = -EFAULT;
  1300. break;
  1301. }
  1302. tun->socket.sk->sk_sndbuf = sndbuf;
  1303. break;
  1304. case TUNGETVNETHDRSZ:
  1305. vnet_hdr_sz = tun->vnet_hdr_sz;
  1306. if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
  1307. ret = -EFAULT;
  1308. break;
  1309. case TUNSETVNETHDRSZ:
  1310. if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
  1311. ret = -EFAULT;
  1312. break;
  1313. }
  1314. if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
  1315. ret = -EINVAL;
  1316. break;
  1317. }
  1318. tun->vnet_hdr_sz = vnet_hdr_sz;
  1319. break;
  1320. case TUNATTACHFILTER:
  1321. /* Can be set only for TAPs */
  1322. ret = -EINVAL;
  1323. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1324. break;
  1325. ret = -EFAULT;
  1326. if (copy_from_user(&fprog, argp, sizeof(fprog)))
  1327. break;
  1328. ret = sk_attach_filter(&fprog, tun->socket.sk);
  1329. break;
  1330. case TUNDETACHFILTER:
  1331. /* Can be set only for TAPs */
  1332. ret = -EINVAL;
  1333. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1334. break;
  1335. ret = sk_detach_filter(tun->socket.sk);
  1336. break;
  1337. default:
  1338. ret = -EINVAL;
  1339. break;
  1340. }
  1341. unlock:
  1342. rtnl_unlock();
  1343. if (tun)
  1344. tun_put(tun);
  1345. return ret;
  1346. }
  1347. static long tun_chr_ioctl(struct file *file,
  1348. unsigned int cmd, unsigned long arg)
  1349. {
  1350. return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
  1351. }
  1352. #ifdef CONFIG_COMPAT
  1353. static long tun_chr_compat_ioctl(struct file *file,
  1354. unsigned int cmd, unsigned long arg)
  1355. {
  1356. switch (cmd) {
  1357. case TUNSETIFF:
  1358. case TUNGETIFF:
  1359. case TUNSETTXFILTER:
  1360. case TUNGETSNDBUF:
  1361. case TUNSETSNDBUF:
  1362. case SIOCGIFHWADDR:
  1363. case SIOCSIFHWADDR:
  1364. arg = (unsigned long)compat_ptr(arg);
  1365. break;
  1366. default:
  1367. arg = (compat_ulong_t)arg;
  1368. break;
  1369. }
  1370. /*
  1371. * compat_ifreq is shorter than ifreq, so we must not access beyond
  1372. * the end of that structure. All fields that are used in this
  1373. * driver are compatible though, we don't need to convert the
  1374. * contents.
  1375. */
  1376. return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
  1377. }
  1378. #endif /* CONFIG_COMPAT */
  1379. static int tun_chr_fasync(int fd, struct file *file, int on)
  1380. {
  1381. struct tun_struct *tun = tun_get(file);
  1382. int ret;
  1383. if (!tun)
  1384. return -EBADFD;
  1385. tun_debug(KERN_INFO, tun, "tun_chr_fasync %d\n", on);
  1386. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  1387. goto out;
  1388. if (on) {
  1389. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  1390. if (ret)
  1391. goto out;
  1392. tun->flags |= TUN_FASYNC;
  1393. } else
  1394. tun->flags &= ~TUN_FASYNC;
  1395. ret = 0;
  1396. out:
  1397. tun_put(tun);
  1398. return ret;
  1399. }
  1400. static int tun_chr_open(struct inode *inode, struct file * file)
  1401. {
  1402. struct tun_file *tfile;
  1403. DBG1(KERN_INFO, "tunX: tun_chr_open\n");
  1404. tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
  1405. if (!tfile)
  1406. return -ENOMEM;
  1407. atomic_set(&tfile->count, 0);
  1408. tfile->tun = NULL;
  1409. tfile->net = get_net(current->nsproxy->net_ns);
  1410. file->private_data = tfile;
  1411. return 0;
  1412. }
  1413. static int tun_chr_close(struct inode *inode, struct file *file)
  1414. {
  1415. struct tun_file *tfile = file->private_data;
  1416. struct tun_struct *tun;
  1417. tun = __tun_get(tfile);
  1418. if (tun) {
  1419. struct net_device *dev = tun->dev;
  1420. tun_debug(KERN_INFO, tun, "tun_chr_close\n");
  1421. __tun_detach(tun);
  1422. /* If desirable, unregister the netdevice. */
  1423. if (!(tun->flags & TUN_PERSIST)) {
  1424. rtnl_lock();
  1425. if (dev->reg_state == NETREG_REGISTERED)
  1426. unregister_netdevice(dev);
  1427. rtnl_unlock();
  1428. }
  1429. }
  1430. tun = tfile->tun;
  1431. if (tun)
  1432. sock_put(tun->socket.sk);
  1433. put_net(tfile->net);
  1434. kfree(tfile);
  1435. return 0;
  1436. }
  1437. static const struct file_operations tun_fops = {
  1438. .owner = THIS_MODULE,
  1439. .llseek = no_llseek,
  1440. .read = do_sync_read,
  1441. .aio_read = tun_chr_aio_read,
  1442. .write = do_sync_write,
  1443. .aio_write = tun_chr_aio_write,
  1444. .poll = tun_chr_poll,
  1445. .unlocked_ioctl = tun_chr_ioctl,
  1446. #ifdef CONFIG_COMPAT
  1447. .compat_ioctl = tun_chr_compat_ioctl,
  1448. #endif
  1449. .open = tun_chr_open,
  1450. .release = tun_chr_close,
  1451. .fasync = tun_chr_fasync
  1452. };
  1453. static struct miscdevice tun_miscdev = {
  1454. .minor = TUN_MINOR,
  1455. .name = "tun",
  1456. .nodename = "net/tun",
  1457. .fops = &tun_fops,
  1458. };
  1459. /* ethtool interface */
  1460. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1461. {
  1462. cmd->supported = 0;
  1463. cmd->advertising = 0;
  1464. ethtool_cmd_speed_set(cmd, SPEED_10);
  1465. cmd->duplex = DUPLEX_FULL;
  1466. cmd->port = PORT_TP;
  1467. cmd->phy_address = 0;
  1468. cmd->transceiver = XCVR_INTERNAL;
  1469. cmd->autoneg = AUTONEG_DISABLE;
  1470. cmd->maxtxpkt = 0;
  1471. cmd->maxrxpkt = 0;
  1472. return 0;
  1473. }
  1474. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1475. {
  1476. struct tun_struct *tun = netdev_priv(dev);
  1477. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1478. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1479. switch (tun->flags & TUN_TYPE_MASK) {
  1480. case TUN_TUN_DEV:
  1481. strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
  1482. break;
  1483. case TUN_TAP_DEV:
  1484. strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
  1485. break;
  1486. }
  1487. }
  1488. static u32 tun_get_msglevel(struct net_device *dev)
  1489. {
  1490. #ifdef TUN_DEBUG
  1491. struct tun_struct *tun = netdev_priv(dev);
  1492. return tun->debug;
  1493. #else
  1494. return -EOPNOTSUPP;
  1495. #endif
  1496. }
  1497. static void tun_set_msglevel(struct net_device *dev, u32 value)
  1498. {
  1499. #ifdef TUN_DEBUG
  1500. struct tun_struct *tun = netdev_priv(dev);
  1501. tun->debug = value;
  1502. #endif
  1503. }
  1504. static const struct ethtool_ops tun_ethtool_ops = {
  1505. .get_settings = tun_get_settings,
  1506. .get_drvinfo = tun_get_drvinfo,
  1507. .get_msglevel = tun_get_msglevel,
  1508. .set_msglevel = tun_set_msglevel,
  1509. .get_link = ethtool_op_get_link,
  1510. };
  1511. static int __init tun_init(void)
  1512. {
  1513. int ret = 0;
  1514. pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1515. pr_info("%s\n", DRV_COPYRIGHT);
  1516. ret = rtnl_link_register(&tun_link_ops);
  1517. if (ret) {
  1518. pr_err("Can't register link_ops\n");
  1519. goto err_linkops;
  1520. }
  1521. ret = misc_register(&tun_miscdev);
  1522. if (ret) {
  1523. pr_err("Can't register misc device %d\n", TUN_MINOR);
  1524. goto err_misc;
  1525. }
  1526. return 0;
  1527. err_misc:
  1528. rtnl_link_unregister(&tun_link_ops);
  1529. err_linkops:
  1530. return ret;
  1531. }
  1532. static void tun_cleanup(void)
  1533. {
  1534. misc_deregister(&tun_miscdev);
  1535. rtnl_link_unregister(&tun_link_ops);
  1536. }
  1537. /* Get an underlying socket object from tun file. Returns error unless file is
  1538. * attached to a device. The returned object works like a packet socket, it
  1539. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1540. * holding a reference to the file for as long as the socket is in use. */
  1541. struct socket *tun_get_socket(struct file *file)
  1542. {
  1543. struct tun_struct *tun;
  1544. if (file->f_op != &tun_fops)
  1545. return ERR_PTR(-EINVAL);
  1546. tun = tun_get(file);
  1547. if (!tun)
  1548. return ERR_PTR(-EBADFD);
  1549. tun_put(tun);
  1550. return &tun->socket;
  1551. }
  1552. EXPORT_SYMBOL_GPL(tun_get_socket);
  1553. module_init(tun_init);
  1554. module_exit(tun_cleanup);
  1555. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1556. MODULE_AUTHOR(DRV_COPYRIGHT);
  1557. MODULE_LICENSE("GPL");
  1558. MODULE_ALIAS_MISCDEV(TUN_MINOR);
  1559. MODULE_ALIAS("devname:net/tun");