tun.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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. netdev_features_t 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 netdev_features_t tun_net_fix_features(struct net_device *dev,
  379. netdev_features_t features)
  380. {
  381. struct tun_struct *tun = netdev_priv(dev);
  382. return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
  383. }
  384. #ifdef CONFIG_NET_POLL_CONTROLLER
  385. static void tun_poll_controller(struct net_device *dev)
  386. {
  387. /*
  388. * Tun only receives frames when:
  389. * 1) the char device endpoint gets data from user space
  390. * 2) the tun socket gets a sendmsg call from user space
  391. * Since both of those are syncronous operations, we are guaranteed
  392. * never to have pending data when we poll for it
  393. * so theres nothing to do here but return.
  394. * We need this though so netpoll recognizes us as an interface that
  395. * supports polling, which enables bridge devices in virt setups to
  396. * still use netconsole
  397. */
  398. return;
  399. }
  400. #endif
  401. static const struct net_device_ops tun_netdev_ops = {
  402. .ndo_uninit = tun_net_uninit,
  403. .ndo_open = tun_net_open,
  404. .ndo_stop = tun_net_close,
  405. .ndo_start_xmit = tun_net_xmit,
  406. .ndo_change_mtu = tun_net_change_mtu,
  407. .ndo_fix_features = tun_net_fix_features,
  408. #ifdef CONFIG_NET_POLL_CONTROLLER
  409. .ndo_poll_controller = tun_poll_controller,
  410. #endif
  411. };
  412. static const struct net_device_ops tap_netdev_ops = {
  413. .ndo_uninit = tun_net_uninit,
  414. .ndo_open = tun_net_open,
  415. .ndo_stop = tun_net_close,
  416. .ndo_start_xmit = tun_net_xmit,
  417. .ndo_change_mtu = tun_net_change_mtu,
  418. .ndo_fix_features = tun_net_fix_features,
  419. .ndo_set_rx_mode = tun_net_mclist,
  420. .ndo_set_mac_address = eth_mac_addr,
  421. .ndo_validate_addr = eth_validate_addr,
  422. #ifdef CONFIG_NET_POLL_CONTROLLER
  423. .ndo_poll_controller = tun_poll_controller,
  424. #endif
  425. };
  426. /* Initialize net device. */
  427. static void tun_net_init(struct net_device *dev)
  428. {
  429. struct tun_struct *tun = netdev_priv(dev);
  430. switch (tun->flags & TUN_TYPE_MASK) {
  431. case TUN_TUN_DEV:
  432. dev->netdev_ops = &tun_netdev_ops;
  433. /* Point-to-Point TUN Device */
  434. dev->hard_header_len = 0;
  435. dev->addr_len = 0;
  436. dev->mtu = 1500;
  437. /* Zero header length */
  438. dev->type = ARPHRD_NONE;
  439. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  440. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  441. break;
  442. case TUN_TAP_DEV:
  443. dev->netdev_ops = &tap_netdev_ops;
  444. /* Ethernet TAP Device */
  445. ether_setup(dev);
  446. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  447. random_ether_addr(dev->dev_addr);
  448. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  449. break;
  450. }
  451. }
  452. /* Character device part */
  453. /* Poll */
  454. static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
  455. {
  456. struct tun_file *tfile = file->private_data;
  457. struct tun_struct *tun = __tun_get(tfile);
  458. struct sock *sk;
  459. unsigned int mask = 0;
  460. if (!tun)
  461. return POLLERR;
  462. sk = tun->socket.sk;
  463. tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
  464. poll_wait(file, &tun->wq.wait, wait);
  465. if (!skb_queue_empty(&sk->sk_receive_queue))
  466. mask |= POLLIN | POLLRDNORM;
  467. if (sock_writeable(sk) ||
  468. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
  469. sock_writeable(sk)))
  470. mask |= POLLOUT | POLLWRNORM;
  471. if (tun->dev->reg_state != NETREG_REGISTERED)
  472. mask = POLLERR;
  473. tun_put(tun);
  474. return mask;
  475. }
  476. /* prepad is the amount to reserve at front. len is length after that.
  477. * linear is a hint as to how much to copy (usually headers). */
  478. static struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
  479. size_t prepad, size_t len,
  480. size_t linear, int noblock)
  481. {
  482. struct sock *sk = tun->socket.sk;
  483. struct sk_buff *skb;
  484. int err;
  485. sock_update_classid(sk);
  486. /* Under a page? Don't bother with paged skb. */
  487. if (prepad + len < PAGE_SIZE || !linear)
  488. linear = len;
  489. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  490. &err);
  491. if (!skb)
  492. return ERR_PTR(err);
  493. skb_reserve(skb, prepad);
  494. skb_put(skb, linear);
  495. skb->data_len = len - linear;
  496. skb->len += len - linear;
  497. return skb;
  498. }
  499. /* Get packet from user space buffer */
  500. static ssize_t tun_get_user(struct tun_struct *tun,
  501. const struct iovec *iv, size_t count,
  502. int noblock)
  503. {
  504. struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
  505. struct sk_buff *skb;
  506. size_t len = count, align = NET_SKB_PAD;
  507. struct virtio_net_hdr gso = { 0 };
  508. int offset = 0;
  509. if (!(tun->flags & TUN_NO_PI)) {
  510. if ((len -= sizeof(pi)) > count)
  511. return -EINVAL;
  512. if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
  513. return -EFAULT;
  514. offset += sizeof(pi);
  515. }
  516. if (tun->flags & TUN_VNET_HDR) {
  517. if ((len -= tun->vnet_hdr_sz) > count)
  518. return -EINVAL;
  519. if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
  520. return -EFAULT;
  521. if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  522. gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
  523. gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
  524. if (gso.hdr_len > len)
  525. return -EINVAL;
  526. offset += tun->vnet_hdr_sz;
  527. }
  528. if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
  529. align += NET_IP_ALIGN;
  530. if (unlikely(len < ETH_HLEN ||
  531. (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
  532. return -EINVAL;
  533. }
  534. skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
  535. if (IS_ERR(skb)) {
  536. if (PTR_ERR(skb) != -EAGAIN)
  537. tun->dev->stats.rx_dropped++;
  538. return PTR_ERR(skb);
  539. }
  540. if (skb_copy_datagram_from_iovec(skb, 0, iv, offset, len)) {
  541. tun->dev->stats.rx_dropped++;
  542. kfree_skb(skb);
  543. return -EFAULT;
  544. }
  545. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  546. if (!skb_partial_csum_set(skb, gso.csum_start,
  547. gso.csum_offset)) {
  548. tun->dev->stats.rx_frame_errors++;
  549. kfree_skb(skb);
  550. return -EINVAL;
  551. }
  552. }
  553. switch (tun->flags & TUN_TYPE_MASK) {
  554. case TUN_TUN_DEV:
  555. if (tun->flags & TUN_NO_PI) {
  556. switch (skb->data[0] & 0xf0) {
  557. case 0x40:
  558. pi.proto = htons(ETH_P_IP);
  559. break;
  560. case 0x60:
  561. pi.proto = htons(ETH_P_IPV6);
  562. break;
  563. default:
  564. tun->dev->stats.rx_dropped++;
  565. kfree_skb(skb);
  566. return -EINVAL;
  567. }
  568. }
  569. skb_reset_mac_header(skb);
  570. skb->protocol = pi.proto;
  571. skb->dev = tun->dev;
  572. break;
  573. case TUN_TAP_DEV:
  574. skb->protocol = eth_type_trans(skb, tun->dev);
  575. break;
  576. }
  577. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  578. pr_debug("GSO!\n");
  579. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  580. case VIRTIO_NET_HDR_GSO_TCPV4:
  581. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  582. break;
  583. case VIRTIO_NET_HDR_GSO_TCPV6:
  584. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  585. break;
  586. case VIRTIO_NET_HDR_GSO_UDP:
  587. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  588. break;
  589. default:
  590. tun->dev->stats.rx_frame_errors++;
  591. kfree_skb(skb);
  592. return -EINVAL;
  593. }
  594. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  595. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  596. skb_shinfo(skb)->gso_size = gso.gso_size;
  597. if (skb_shinfo(skb)->gso_size == 0) {
  598. tun->dev->stats.rx_frame_errors++;
  599. kfree_skb(skb);
  600. return -EINVAL;
  601. }
  602. /* Header must be checked, and gso_segs computed. */
  603. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  604. skb_shinfo(skb)->gso_segs = 0;
  605. }
  606. netif_rx_ni(skb);
  607. tun->dev->stats.rx_packets++;
  608. tun->dev->stats.rx_bytes += len;
  609. return count;
  610. }
  611. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  612. unsigned long count, loff_t pos)
  613. {
  614. struct file *file = iocb->ki_filp;
  615. struct tun_struct *tun = tun_get(file);
  616. ssize_t result;
  617. if (!tun)
  618. return -EBADFD;
  619. tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
  620. result = tun_get_user(tun, iv, iov_length(iv, count),
  621. file->f_flags & O_NONBLOCK);
  622. tun_put(tun);
  623. return result;
  624. }
  625. /* Put packet to the user space buffer */
  626. static ssize_t tun_put_user(struct tun_struct *tun,
  627. struct sk_buff *skb,
  628. const struct iovec *iv, int len)
  629. {
  630. struct tun_pi pi = { 0, skb->protocol };
  631. ssize_t total = 0;
  632. if (!(tun->flags & TUN_NO_PI)) {
  633. if ((len -= sizeof(pi)) < 0)
  634. return -EINVAL;
  635. if (len < skb->len) {
  636. /* Packet will be striped */
  637. pi.flags |= TUN_PKT_STRIP;
  638. }
  639. if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
  640. return -EFAULT;
  641. total += sizeof(pi);
  642. }
  643. if (tun->flags & TUN_VNET_HDR) {
  644. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  645. if ((len -= tun->vnet_hdr_sz) < 0)
  646. return -EINVAL;
  647. if (skb_is_gso(skb)) {
  648. struct skb_shared_info *sinfo = skb_shinfo(skb);
  649. /* This is a hint as to how much should be linear. */
  650. gso.hdr_len = skb_headlen(skb);
  651. gso.gso_size = sinfo->gso_size;
  652. if (sinfo->gso_type & SKB_GSO_TCPV4)
  653. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  654. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  655. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  656. else if (sinfo->gso_type & SKB_GSO_UDP)
  657. gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  658. else {
  659. pr_err("unexpected GSO type: "
  660. "0x%x, gso_size %d, hdr_len %d\n",
  661. sinfo->gso_type, gso.gso_size,
  662. gso.hdr_len);
  663. print_hex_dump(KERN_ERR, "tun: ",
  664. DUMP_PREFIX_NONE,
  665. 16, 1, skb->head,
  666. min((int)gso.hdr_len, 64), true);
  667. WARN_ON_ONCE(1);
  668. return -EINVAL;
  669. }
  670. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  671. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  672. } else
  673. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  674. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  675. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  676. gso.csum_start = skb_checksum_start_offset(skb);
  677. gso.csum_offset = skb->csum_offset;
  678. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  679. gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
  680. } /* else everything is zero */
  681. if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
  682. sizeof(gso))))
  683. return -EFAULT;
  684. total += tun->vnet_hdr_sz;
  685. }
  686. len = min_t(int, skb->len, len);
  687. skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
  688. total += skb->len;
  689. tun->dev->stats.tx_packets++;
  690. tun->dev->stats.tx_bytes += len;
  691. return total;
  692. }
  693. static ssize_t tun_do_read(struct tun_struct *tun,
  694. struct kiocb *iocb, const struct iovec *iv,
  695. ssize_t len, int noblock)
  696. {
  697. DECLARE_WAITQUEUE(wait, current);
  698. struct sk_buff *skb;
  699. ssize_t ret = 0;
  700. tun_debug(KERN_INFO, tun, "tun_chr_read\n");
  701. if (unlikely(!noblock))
  702. add_wait_queue(&tun->wq.wait, &wait);
  703. while (len) {
  704. current->state = TASK_INTERRUPTIBLE;
  705. /* Read frames from the queue */
  706. if (!(skb=skb_dequeue(&tun->socket.sk->sk_receive_queue))) {
  707. if (noblock) {
  708. ret = -EAGAIN;
  709. break;
  710. }
  711. if (signal_pending(current)) {
  712. ret = -ERESTARTSYS;
  713. break;
  714. }
  715. if (tun->dev->reg_state != NETREG_REGISTERED) {
  716. ret = -EIO;
  717. break;
  718. }
  719. /* Nothing to read, let's sleep */
  720. schedule();
  721. continue;
  722. }
  723. netif_wake_queue(tun->dev);
  724. ret = tun_put_user(tun, skb, iv, len);
  725. kfree_skb(skb);
  726. break;
  727. }
  728. current->state = TASK_RUNNING;
  729. if (unlikely(!noblock))
  730. remove_wait_queue(&tun->wq.wait, &wait);
  731. return ret;
  732. }
  733. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  734. unsigned long count, loff_t pos)
  735. {
  736. struct file *file = iocb->ki_filp;
  737. struct tun_file *tfile = file->private_data;
  738. struct tun_struct *tun = __tun_get(tfile);
  739. ssize_t len, ret;
  740. if (!tun)
  741. return -EBADFD;
  742. len = iov_length(iv, count);
  743. if (len < 0) {
  744. ret = -EINVAL;
  745. goto out;
  746. }
  747. ret = tun_do_read(tun, iocb, iv, len, file->f_flags & O_NONBLOCK);
  748. ret = min_t(ssize_t, ret, len);
  749. out:
  750. tun_put(tun);
  751. return ret;
  752. }
  753. static void tun_setup(struct net_device *dev)
  754. {
  755. struct tun_struct *tun = netdev_priv(dev);
  756. tun->owner = -1;
  757. tun->group = -1;
  758. dev->ethtool_ops = &tun_ethtool_ops;
  759. dev->destructor = tun_free_netdev;
  760. }
  761. /* Trivial set of netlink ops to allow deleting tun or tap
  762. * device with netlink.
  763. */
  764. static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
  765. {
  766. return -EINVAL;
  767. }
  768. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  769. .kind = DRV_NAME,
  770. .priv_size = sizeof(struct tun_struct),
  771. .setup = tun_setup,
  772. .validate = tun_validate,
  773. };
  774. static void tun_sock_write_space(struct sock *sk)
  775. {
  776. struct tun_struct *tun;
  777. wait_queue_head_t *wqueue;
  778. if (!sock_writeable(sk))
  779. return;
  780. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  781. return;
  782. wqueue = sk_sleep(sk);
  783. if (wqueue && waitqueue_active(wqueue))
  784. wake_up_interruptible_sync_poll(wqueue, POLLOUT |
  785. POLLWRNORM | POLLWRBAND);
  786. tun = tun_sk(sk)->tun;
  787. kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
  788. }
  789. static void tun_sock_destruct(struct sock *sk)
  790. {
  791. free_netdev(tun_sk(sk)->tun->dev);
  792. }
  793. static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
  794. struct msghdr *m, size_t total_len)
  795. {
  796. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  797. return tun_get_user(tun, m->msg_iov, total_len,
  798. m->msg_flags & MSG_DONTWAIT);
  799. }
  800. static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
  801. struct msghdr *m, size_t total_len,
  802. int flags)
  803. {
  804. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  805. int ret;
  806. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  807. return -EINVAL;
  808. ret = tun_do_read(tun, iocb, m->msg_iov, total_len,
  809. flags & MSG_DONTWAIT);
  810. if (ret > total_len) {
  811. m->msg_flags |= MSG_TRUNC;
  812. ret = flags & MSG_TRUNC ? ret : total_len;
  813. }
  814. return ret;
  815. }
  816. /* Ops structure to mimic raw sockets with tun */
  817. static const struct proto_ops tun_socket_ops = {
  818. .sendmsg = tun_sendmsg,
  819. .recvmsg = tun_recvmsg,
  820. };
  821. static struct proto tun_proto = {
  822. .name = "tun",
  823. .owner = THIS_MODULE,
  824. .obj_size = sizeof(struct tun_sock),
  825. };
  826. static int tun_flags(struct tun_struct *tun)
  827. {
  828. int flags = 0;
  829. if (tun->flags & TUN_TUN_DEV)
  830. flags |= IFF_TUN;
  831. else
  832. flags |= IFF_TAP;
  833. if (tun->flags & TUN_NO_PI)
  834. flags |= IFF_NO_PI;
  835. if (tun->flags & TUN_ONE_QUEUE)
  836. flags |= IFF_ONE_QUEUE;
  837. if (tun->flags & TUN_VNET_HDR)
  838. flags |= IFF_VNET_HDR;
  839. return flags;
  840. }
  841. static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
  842. char *buf)
  843. {
  844. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  845. return sprintf(buf, "0x%x\n", tun_flags(tun));
  846. }
  847. static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
  848. char *buf)
  849. {
  850. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  851. return sprintf(buf, "%d\n", tun->owner);
  852. }
  853. static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
  854. char *buf)
  855. {
  856. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  857. return sprintf(buf, "%d\n", tun->group);
  858. }
  859. static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
  860. static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
  861. static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
  862. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  863. {
  864. struct sock *sk;
  865. struct tun_struct *tun;
  866. struct net_device *dev;
  867. int err;
  868. dev = __dev_get_by_name(net, ifr->ifr_name);
  869. if (dev) {
  870. const struct cred *cred = current_cred();
  871. if (ifr->ifr_flags & IFF_TUN_EXCL)
  872. return -EBUSY;
  873. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  874. tun = netdev_priv(dev);
  875. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  876. tun = netdev_priv(dev);
  877. else
  878. return -EINVAL;
  879. if (((tun->owner != -1 && cred->euid != tun->owner) ||
  880. (tun->group != -1 && !in_egroup_p(tun->group))) &&
  881. !capable(CAP_NET_ADMIN))
  882. return -EPERM;
  883. err = security_tun_dev_attach(tun->socket.sk);
  884. if (err < 0)
  885. return err;
  886. err = tun_attach(tun, file);
  887. if (err < 0)
  888. return err;
  889. }
  890. else {
  891. char *name;
  892. unsigned long flags = 0;
  893. if (!capable(CAP_NET_ADMIN))
  894. return -EPERM;
  895. err = security_tun_dev_create();
  896. if (err < 0)
  897. return err;
  898. /* Set dev type */
  899. if (ifr->ifr_flags & IFF_TUN) {
  900. /* TUN device */
  901. flags |= TUN_TUN_DEV;
  902. name = "tun%d";
  903. } else if (ifr->ifr_flags & IFF_TAP) {
  904. /* TAP device */
  905. flags |= TUN_TAP_DEV;
  906. name = "tap%d";
  907. } else
  908. return -EINVAL;
  909. if (*ifr->ifr_name)
  910. name = ifr->ifr_name;
  911. dev = alloc_netdev(sizeof(struct tun_struct), name,
  912. tun_setup);
  913. if (!dev)
  914. return -ENOMEM;
  915. dev_net_set(dev, net);
  916. dev->rtnl_link_ops = &tun_link_ops;
  917. tun = netdev_priv(dev);
  918. tun->dev = dev;
  919. tun->flags = flags;
  920. tun->txflt.count = 0;
  921. tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  922. err = -ENOMEM;
  923. sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
  924. if (!sk)
  925. goto err_free_dev;
  926. tun->socket.wq = &tun->wq;
  927. init_waitqueue_head(&tun->wq.wait);
  928. tun->socket.ops = &tun_socket_ops;
  929. sock_init_data(&tun->socket, sk);
  930. sk->sk_write_space = tun_sock_write_space;
  931. sk->sk_sndbuf = INT_MAX;
  932. tun_sk(sk)->tun = tun;
  933. security_tun_dev_post_create(sk);
  934. tun_net_init(dev);
  935. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
  936. TUN_USER_FEATURES;
  937. dev->features = dev->hw_features;
  938. err = register_netdevice(tun->dev);
  939. if (err < 0)
  940. goto err_free_sk;
  941. if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
  942. device_create_file(&tun->dev->dev, &dev_attr_owner) ||
  943. device_create_file(&tun->dev->dev, &dev_attr_group))
  944. pr_err("Failed to create tun sysfs files\n");
  945. sk->sk_destruct = tun_sock_destruct;
  946. err = tun_attach(tun, file);
  947. if (err < 0)
  948. goto failed;
  949. }
  950. tun_debug(KERN_INFO, tun, "tun_set_iff\n");
  951. if (ifr->ifr_flags & IFF_NO_PI)
  952. tun->flags |= TUN_NO_PI;
  953. else
  954. tun->flags &= ~TUN_NO_PI;
  955. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  956. tun->flags |= TUN_ONE_QUEUE;
  957. else
  958. tun->flags &= ~TUN_ONE_QUEUE;
  959. if (ifr->ifr_flags & IFF_VNET_HDR)
  960. tun->flags |= TUN_VNET_HDR;
  961. else
  962. tun->flags &= ~TUN_VNET_HDR;
  963. /* Make sure persistent devices do not get stuck in
  964. * xoff state.
  965. */
  966. if (netif_running(tun->dev))
  967. netif_wake_queue(tun->dev);
  968. strcpy(ifr->ifr_name, tun->dev->name);
  969. return 0;
  970. err_free_sk:
  971. sock_put(sk);
  972. err_free_dev:
  973. free_netdev(dev);
  974. failed:
  975. return err;
  976. }
  977. static int tun_get_iff(struct net *net, struct tun_struct *tun,
  978. struct ifreq *ifr)
  979. {
  980. tun_debug(KERN_INFO, tun, "tun_get_iff\n");
  981. strcpy(ifr->ifr_name, tun->dev->name);
  982. ifr->ifr_flags = tun_flags(tun);
  983. return 0;
  984. }
  985. /* This is like a cut-down ethtool ops, except done via tun fd so no
  986. * privs required. */
  987. static int set_offload(struct tun_struct *tun, unsigned long arg)
  988. {
  989. netdev_features_t features = 0;
  990. if (arg & TUN_F_CSUM) {
  991. features |= NETIF_F_HW_CSUM;
  992. arg &= ~TUN_F_CSUM;
  993. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  994. if (arg & TUN_F_TSO_ECN) {
  995. features |= NETIF_F_TSO_ECN;
  996. arg &= ~TUN_F_TSO_ECN;
  997. }
  998. if (arg & TUN_F_TSO4)
  999. features |= NETIF_F_TSO;
  1000. if (arg & TUN_F_TSO6)
  1001. features |= NETIF_F_TSO6;
  1002. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  1003. }
  1004. if (arg & TUN_F_UFO) {
  1005. features |= NETIF_F_UFO;
  1006. arg &= ~TUN_F_UFO;
  1007. }
  1008. }
  1009. /* This gives the user a way to test for new features in future by
  1010. * trying to set them. */
  1011. if (arg)
  1012. return -EINVAL;
  1013. tun->set_features = features;
  1014. netdev_update_features(tun->dev);
  1015. return 0;
  1016. }
  1017. static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  1018. unsigned long arg, int ifreq_len)
  1019. {
  1020. struct tun_file *tfile = file->private_data;
  1021. struct tun_struct *tun;
  1022. void __user* argp = (void __user*)arg;
  1023. struct sock_fprog fprog;
  1024. struct ifreq ifr;
  1025. int sndbuf;
  1026. int vnet_hdr_sz;
  1027. int ret;
  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. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1315. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1316. switch (tun->flags & TUN_TYPE_MASK) {
  1317. case TUN_TUN_DEV:
  1318. strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
  1319. break;
  1320. case TUN_TAP_DEV:
  1321. strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
  1322. break;
  1323. }
  1324. }
  1325. static u32 tun_get_msglevel(struct net_device *dev)
  1326. {
  1327. #ifdef TUN_DEBUG
  1328. struct tun_struct *tun = netdev_priv(dev);
  1329. return tun->debug;
  1330. #else
  1331. return -EOPNOTSUPP;
  1332. #endif
  1333. }
  1334. static void tun_set_msglevel(struct net_device *dev, u32 value)
  1335. {
  1336. #ifdef TUN_DEBUG
  1337. struct tun_struct *tun = netdev_priv(dev);
  1338. tun->debug = value;
  1339. #endif
  1340. }
  1341. static const struct ethtool_ops tun_ethtool_ops = {
  1342. .get_settings = tun_get_settings,
  1343. .get_drvinfo = tun_get_drvinfo,
  1344. .get_msglevel = tun_get_msglevel,
  1345. .set_msglevel = tun_set_msglevel,
  1346. .get_link = ethtool_op_get_link,
  1347. };
  1348. static int __init tun_init(void)
  1349. {
  1350. int ret = 0;
  1351. pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1352. pr_info("%s\n", DRV_COPYRIGHT);
  1353. ret = rtnl_link_register(&tun_link_ops);
  1354. if (ret) {
  1355. pr_err("Can't register link_ops\n");
  1356. goto err_linkops;
  1357. }
  1358. ret = misc_register(&tun_miscdev);
  1359. if (ret) {
  1360. pr_err("Can't register misc device %d\n", TUN_MINOR);
  1361. goto err_misc;
  1362. }
  1363. return 0;
  1364. err_misc:
  1365. rtnl_link_unregister(&tun_link_ops);
  1366. err_linkops:
  1367. return ret;
  1368. }
  1369. static void tun_cleanup(void)
  1370. {
  1371. misc_deregister(&tun_miscdev);
  1372. rtnl_link_unregister(&tun_link_ops);
  1373. }
  1374. /* Get an underlying socket object from tun file. Returns error unless file is
  1375. * attached to a device. The returned object works like a packet socket, it
  1376. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1377. * holding a reference to the file for as long as the socket is in use. */
  1378. struct socket *tun_get_socket(struct file *file)
  1379. {
  1380. struct tun_struct *tun;
  1381. if (file->f_op != &tun_fops)
  1382. return ERR_PTR(-EINVAL);
  1383. tun = tun_get(file);
  1384. if (!tun)
  1385. return ERR_PTR(-EBADFD);
  1386. tun_put(tun);
  1387. return &tun->socket;
  1388. }
  1389. EXPORT_SYMBOL_GPL(tun_get_socket);
  1390. module_init(tun_init);
  1391. module_exit(tun_cleanup);
  1392. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1393. MODULE_AUTHOR(DRV_COPYRIGHT);
  1394. MODULE_LICENSE("GPL");
  1395. MODULE_ALIAS_MISCDEV(TUN_MINOR);
  1396. MODULE_ALIAS("devname:net/tun");