tun.c 38 KB

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