tun.c 39 KB

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