tun.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  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. static const struct net_device_ops tun_netdev_ops = {
  384. .ndo_uninit = tun_net_uninit,
  385. .ndo_open = tun_net_open,
  386. .ndo_stop = tun_net_close,
  387. .ndo_start_xmit = tun_net_xmit,
  388. .ndo_change_mtu = tun_net_change_mtu,
  389. .ndo_fix_features = tun_net_fix_features,
  390. };
  391. static const struct net_device_ops tap_netdev_ops = {
  392. .ndo_uninit = tun_net_uninit,
  393. .ndo_open = tun_net_open,
  394. .ndo_stop = tun_net_close,
  395. .ndo_start_xmit = tun_net_xmit,
  396. .ndo_change_mtu = tun_net_change_mtu,
  397. .ndo_fix_features = tun_net_fix_features,
  398. .ndo_set_multicast_list = tun_net_mclist,
  399. .ndo_set_mac_address = eth_mac_addr,
  400. .ndo_validate_addr = eth_validate_addr,
  401. };
  402. /* Initialize net device. */
  403. static void tun_net_init(struct net_device *dev)
  404. {
  405. struct tun_struct *tun = netdev_priv(dev);
  406. switch (tun->flags & TUN_TYPE_MASK) {
  407. case TUN_TUN_DEV:
  408. dev->netdev_ops = &tun_netdev_ops;
  409. /* Point-to-Point TUN Device */
  410. dev->hard_header_len = 0;
  411. dev->addr_len = 0;
  412. dev->mtu = 1500;
  413. /* Zero header length */
  414. dev->type = ARPHRD_NONE;
  415. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  416. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  417. break;
  418. case TUN_TAP_DEV:
  419. dev->netdev_ops = &tap_netdev_ops;
  420. /* Ethernet TAP Device */
  421. ether_setup(dev);
  422. random_ether_addr(dev->dev_addr);
  423. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  424. break;
  425. }
  426. }
  427. /* Character device part */
  428. /* Poll */
  429. static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
  430. {
  431. struct tun_file *tfile = file->private_data;
  432. struct tun_struct *tun = __tun_get(tfile);
  433. struct sock *sk;
  434. unsigned int mask = 0;
  435. if (!tun)
  436. return POLLERR;
  437. sk = tun->socket.sk;
  438. tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
  439. poll_wait(file, &tun->wq.wait, wait);
  440. if (!skb_queue_empty(&sk->sk_receive_queue))
  441. mask |= POLLIN | POLLRDNORM;
  442. if (sock_writeable(sk) ||
  443. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
  444. sock_writeable(sk)))
  445. mask |= POLLOUT | POLLWRNORM;
  446. if (tun->dev->reg_state != NETREG_REGISTERED)
  447. mask = POLLERR;
  448. tun_put(tun);
  449. return mask;
  450. }
  451. /* prepad is the amount to reserve at front. len is length after that.
  452. * linear is a hint as to how much to copy (usually headers). */
  453. static inline struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
  454. size_t prepad, size_t len,
  455. size_t linear, int noblock)
  456. {
  457. struct sock *sk = tun->socket.sk;
  458. struct sk_buff *skb;
  459. int err;
  460. sock_update_classid(sk);
  461. /* Under a page? Don't bother with paged skb. */
  462. if (prepad + len < PAGE_SIZE || !linear)
  463. linear = len;
  464. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  465. &err);
  466. if (!skb)
  467. return ERR_PTR(err);
  468. skb_reserve(skb, prepad);
  469. skb_put(skb, linear);
  470. skb->data_len = len - linear;
  471. skb->len += len - linear;
  472. return skb;
  473. }
  474. /* Get packet from user space buffer */
  475. static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
  476. const struct iovec *iv, size_t count,
  477. int noblock)
  478. {
  479. struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
  480. struct sk_buff *skb;
  481. size_t len = count, align = 0;
  482. struct virtio_net_hdr gso = { 0 };
  483. int offset = 0;
  484. if (!(tun->flags & TUN_NO_PI)) {
  485. if ((len -= sizeof(pi)) > count)
  486. return -EINVAL;
  487. if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
  488. return -EFAULT;
  489. offset += sizeof(pi);
  490. }
  491. if (tun->flags & TUN_VNET_HDR) {
  492. if ((len -= tun->vnet_hdr_sz) > count)
  493. return -EINVAL;
  494. if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
  495. return -EFAULT;
  496. if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  497. gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
  498. gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
  499. if (gso.hdr_len > len)
  500. return -EINVAL;
  501. offset += tun->vnet_hdr_sz;
  502. }
  503. if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
  504. align = NET_IP_ALIGN;
  505. if (unlikely(len < ETH_HLEN ||
  506. (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
  507. return -EINVAL;
  508. }
  509. skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
  510. if (IS_ERR(skb)) {
  511. if (PTR_ERR(skb) != -EAGAIN)
  512. tun->dev->stats.rx_dropped++;
  513. return PTR_ERR(skb);
  514. }
  515. if (skb_copy_datagram_from_iovec(skb, 0, iv, offset, len)) {
  516. tun->dev->stats.rx_dropped++;
  517. kfree_skb(skb);
  518. return -EFAULT;
  519. }
  520. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  521. if (!skb_partial_csum_set(skb, gso.csum_start,
  522. gso.csum_offset)) {
  523. tun->dev->stats.rx_frame_errors++;
  524. kfree_skb(skb);
  525. return -EINVAL;
  526. }
  527. }
  528. switch (tun->flags & TUN_TYPE_MASK) {
  529. case TUN_TUN_DEV:
  530. if (tun->flags & TUN_NO_PI) {
  531. switch (skb->data[0] & 0xf0) {
  532. case 0x40:
  533. pi.proto = htons(ETH_P_IP);
  534. break;
  535. case 0x60:
  536. pi.proto = htons(ETH_P_IPV6);
  537. break;
  538. default:
  539. tun->dev->stats.rx_dropped++;
  540. kfree_skb(skb);
  541. return -EINVAL;
  542. }
  543. }
  544. skb_reset_mac_header(skb);
  545. skb->protocol = pi.proto;
  546. skb->dev = tun->dev;
  547. break;
  548. case TUN_TAP_DEV:
  549. skb->protocol = eth_type_trans(skb, tun->dev);
  550. break;
  551. };
  552. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  553. pr_debug("GSO!\n");
  554. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  555. case VIRTIO_NET_HDR_GSO_TCPV4:
  556. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  557. break;
  558. case VIRTIO_NET_HDR_GSO_TCPV6:
  559. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  560. break;
  561. case VIRTIO_NET_HDR_GSO_UDP:
  562. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  563. break;
  564. default:
  565. tun->dev->stats.rx_frame_errors++;
  566. kfree_skb(skb);
  567. return -EINVAL;
  568. }
  569. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  570. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  571. skb_shinfo(skb)->gso_size = gso.gso_size;
  572. if (skb_shinfo(skb)->gso_size == 0) {
  573. tun->dev->stats.rx_frame_errors++;
  574. kfree_skb(skb);
  575. return -EINVAL;
  576. }
  577. /* Header must be checked, and gso_segs computed. */
  578. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  579. skb_shinfo(skb)->gso_segs = 0;
  580. }
  581. netif_rx_ni(skb);
  582. tun->dev->stats.rx_packets++;
  583. tun->dev->stats.rx_bytes += len;
  584. return count;
  585. }
  586. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  587. unsigned long count, loff_t pos)
  588. {
  589. struct file *file = iocb->ki_filp;
  590. struct tun_struct *tun = tun_get(file);
  591. ssize_t result;
  592. if (!tun)
  593. return -EBADFD;
  594. tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
  595. result = tun_get_user(tun, iv, iov_length(iv, count),
  596. file->f_flags & O_NONBLOCK);
  597. tun_put(tun);
  598. return result;
  599. }
  600. /* Put packet to the user space buffer */
  601. static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
  602. struct sk_buff *skb,
  603. const struct iovec *iv, int len)
  604. {
  605. struct tun_pi pi = { 0, skb->protocol };
  606. ssize_t total = 0;
  607. if (!(tun->flags & TUN_NO_PI)) {
  608. if ((len -= sizeof(pi)) < 0)
  609. return -EINVAL;
  610. if (len < skb->len) {
  611. /* Packet will be striped */
  612. pi.flags |= TUN_PKT_STRIP;
  613. }
  614. if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
  615. return -EFAULT;
  616. total += sizeof(pi);
  617. }
  618. if (tun->flags & TUN_VNET_HDR) {
  619. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  620. if ((len -= tun->vnet_hdr_sz) < 0)
  621. return -EINVAL;
  622. if (skb_is_gso(skb)) {
  623. struct skb_shared_info *sinfo = skb_shinfo(skb);
  624. /* This is a hint as to how much should be linear. */
  625. gso.hdr_len = skb_headlen(skb);
  626. gso.gso_size = sinfo->gso_size;
  627. if (sinfo->gso_type & SKB_GSO_TCPV4)
  628. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  629. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  630. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  631. else if (sinfo->gso_type & SKB_GSO_UDP)
  632. gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  633. else {
  634. pr_err("unexpected GSO type: "
  635. "0x%x, gso_size %d, hdr_len %d\n",
  636. sinfo->gso_type, gso.gso_size,
  637. gso.hdr_len);
  638. print_hex_dump(KERN_ERR, "tun: ",
  639. DUMP_PREFIX_NONE,
  640. 16, 1, skb->head,
  641. min((int)gso.hdr_len, 64), true);
  642. WARN_ON_ONCE(1);
  643. return -EINVAL;
  644. }
  645. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  646. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  647. } else
  648. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  649. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  650. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  651. gso.csum_start = skb_checksum_start_offset(skb);
  652. gso.csum_offset = skb->csum_offset;
  653. } /* else everything is zero */
  654. if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
  655. sizeof(gso))))
  656. return -EFAULT;
  657. total += tun->vnet_hdr_sz;
  658. }
  659. len = min_t(int, skb->len, len);
  660. skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
  661. total += skb->len;
  662. tun->dev->stats.tx_packets++;
  663. tun->dev->stats.tx_bytes += len;
  664. return total;
  665. }
  666. static ssize_t tun_do_read(struct tun_struct *tun,
  667. struct kiocb *iocb, const struct iovec *iv,
  668. ssize_t len, int noblock)
  669. {
  670. DECLARE_WAITQUEUE(wait, current);
  671. struct sk_buff *skb;
  672. ssize_t ret = 0;
  673. tun_debug(KERN_INFO, tun, "tun_chr_read\n");
  674. add_wait_queue(&tun->wq.wait, &wait);
  675. while (len) {
  676. current->state = TASK_INTERRUPTIBLE;
  677. /* Read frames from the queue */
  678. if (!(skb=skb_dequeue(&tun->socket.sk->sk_receive_queue))) {
  679. if (noblock) {
  680. ret = -EAGAIN;
  681. break;
  682. }
  683. if (signal_pending(current)) {
  684. ret = -ERESTARTSYS;
  685. break;
  686. }
  687. if (tun->dev->reg_state != NETREG_REGISTERED) {
  688. ret = -EIO;
  689. break;
  690. }
  691. /* Nothing to read, let's sleep */
  692. schedule();
  693. continue;
  694. }
  695. netif_wake_queue(tun->dev);
  696. ret = tun_put_user(tun, skb, iv, len);
  697. kfree_skb(skb);
  698. break;
  699. }
  700. current->state = TASK_RUNNING;
  701. remove_wait_queue(&tun->wq.wait, &wait);
  702. return ret;
  703. }
  704. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  705. unsigned long count, loff_t pos)
  706. {
  707. struct file *file = iocb->ki_filp;
  708. struct tun_file *tfile = file->private_data;
  709. struct tun_struct *tun = __tun_get(tfile);
  710. ssize_t len, ret;
  711. if (!tun)
  712. return -EBADFD;
  713. len = iov_length(iv, count);
  714. if (len < 0) {
  715. ret = -EINVAL;
  716. goto out;
  717. }
  718. ret = tun_do_read(tun, iocb, iv, len, file->f_flags & O_NONBLOCK);
  719. ret = min_t(ssize_t, ret, len);
  720. out:
  721. tun_put(tun);
  722. return ret;
  723. }
  724. static void tun_setup(struct net_device *dev)
  725. {
  726. struct tun_struct *tun = netdev_priv(dev);
  727. tun->owner = -1;
  728. tun->group = -1;
  729. dev->ethtool_ops = &tun_ethtool_ops;
  730. dev->destructor = tun_free_netdev;
  731. }
  732. /* Trivial set of netlink ops to allow deleting tun or tap
  733. * device with netlink.
  734. */
  735. static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
  736. {
  737. return -EINVAL;
  738. }
  739. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  740. .kind = DRV_NAME,
  741. .priv_size = sizeof(struct tun_struct),
  742. .setup = tun_setup,
  743. .validate = tun_validate,
  744. };
  745. static void tun_sock_write_space(struct sock *sk)
  746. {
  747. struct tun_struct *tun;
  748. wait_queue_head_t *wqueue;
  749. if (!sock_writeable(sk))
  750. return;
  751. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  752. return;
  753. wqueue = sk_sleep(sk);
  754. if (wqueue && waitqueue_active(wqueue))
  755. wake_up_interruptible_sync_poll(wqueue, POLLOUT |
  756. POLLWRNORM | POLLWRBAND);
  757. tun = tun_sk(sk)->tun;
  758. kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
  759. }
  760. static void tun_sock_destruct(struct sock *sk)
  761. {
  762. free_netdev(tun_sk(sk)->tun->dev);
  763. }
  764. static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
  765. struct msghdr *m, size_t total_len)
  766. {
  767. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  768. return tun_get_user(tun, m->msg_iov, total_len,
  769. m->msg_flags & MSG_DONTWAIT);
  770. }
  771. static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
  772. struct msghdr *m, size_t total_len,
  773. int flags)
  774. {
  775. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  776. int ret;
  777. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  778. return -EINVAL;
  779. ret = tun_do_read(tun, iocb, m->msg_iov, total_len,
  780. flags & MSG_DONTWAIT);
  781. if (ret > total_len) {
  782. m->msg_flags |= MSG_TRUNC;
  783. ret = flags & MSG_TRUNC ? ret : total_len;
  784. }
  785. return ret;
  786. }
  787. /* Ops structure to mimic raw sockets with tun */
  788. static const struct proto_ops tun_socket_ops = {
  789. .sendmsg = tun_sendmsg,
  790. .recvmsg = tun_recvmsg,
  791. };
  792. static struct proto tun_proto = {
  793. .name = "tun",
  794. .owner = THIS_MODULE,
  795. .obj_size = sizeof(struct tun_sock),
  796. };
  797. static int tun_flags(struct tun_struct *tun)
  798. {
  799. int flags = 0;
  800. if (tun->flags & TUN_TUN_DEV)
  801. flags |= IFF_TUN;
  802. else
  803. flags |= IFF_TAP;
  804. if (tun->flags & TUN_NO_PI)
  805. flags |= IFF_NO_PI;
  806. if (tun->flags & TUN_ONE_QUEUE)
  807. flags |= IFF_ONE_QUEUE;
  808. if (tun->flags & TUN_VNET_HDR)
  809. flags |= IFF_VNET_HDR;
  810. return flags;
  811. }
  812. static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
  813. char *buf)
  814. {
  815. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  816. return sprintf(buf, "0x%x\n", tun_flags(tun));
  817. }
  818. static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
  819. char *buf)
  820. {
  821. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  822. return sprintf(buf, "%d\n", tun->owner);
  823. }
  824. static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
  825. char *buf)
  826. {
  827. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  828. return sprintf(buf, "%d\n", tun->group);
  829. }
  830. static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
  831. static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
  832. static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
  833. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  834. {
  835. struct sock *sk;
  836. struct tun_struct *tun;
  837. struct net_device *dev;
  838. int err;
  839. dev = __dev_get_by_name(net, ifr->ifr_name);
  840. if (dev) {
  841. const struct cred *cred = current_cred();
  842. if (ifr->ifr_flags & IFF_TUN_EXCL)
  843. return -EBUSY;
  844. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  845. tun = netdev_priv(dev);
  846. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  847. tun = netdev_priv(dev);
  848. else
  849. return -EINVAL;
  850. if (((tun->owner != -1 && cred->euid != tun->owner) ||
  851. (tun->group != -1 && !in_egroup_p(tun->group))) &&
  852. !capable(CAP_NET_ADMIN))
  853. return -EPERM;
  854. err = security_tun_dev_attach(tun->socket.sk);
  855. if (err < 0)
  856. return err;
  857. err = tun_attach(tun, file);
  858. if (err < 0)
  859. return err;
  860. }
  861. else {
  862. char *name;
  863. unsigned long flags = 0;
  864. if (!capable(CAP_NET_ADMIN))
  865. return -EPERM;
  866. err = security_tun_dev_create();
  867. if (err < 0)
  868. return err;
  869. /* Set dev type */
  870. if (ifr->ifr_flags & IFF_TUN) {
  871. /* TUN device */
  872. flags |= TUN_TUN_DEV;
  873. name = "tun%d";
  874. } else if (ifr->ifr_flags & IFF_TAP) {
  875. /* TAP device */
  876. flags |= TUN_TAP_DEV;
  877. name = "tap%d";
  878. } else
  879. return -EINVAL;
  880. if (*ifr->ifr_name)
  881. name = ifr->ifr_name;
  882. dev = alloc_netdev(sizeof(struct tun_struct), name,
  883. tun_setup);
  884. if (!dev)
  885. return -ENOMEM;
  886. dev_net_set(dev, net);
  887. dev->rtnl_link_ops = &tun_link_ops;
  888. tun = netdev_priv(dev);
  889. tun->dev = dev;
  890. tun->flags = flags;
  891. tun->txflt.count = 0;
  892. tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  893. err = -ENOMEM;
  894. sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
  895. if (!sk)
  896. goto err_free_dev;
  897. tun->socket.wq = &tun->wq;
  898. init_waitqueue_head(&tun->wq.wait);
  899. tun->socket.ops = &tun_socket_ops;
  900. sock_init_data(&tun->socket, sk);
  901. sk->sk_write_space = tun_sock_write_space;
  902. sk->sk_sndbuf = INT_MAX;
  903. tun_sk(sk)->tun = tun;
  904. security_tun_dev_post_create(sk);
  905. tun_net_init(dev);
  906. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
  907. TUN_USER_FEATURES;
  908. dev->features = dev->hw_features;
  909. err = register_netdevice(tun->dev);
  910. if (err < 0)
  911. goto err_free_sk;
  912. if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
  913. device_create_file(&tun->dev->dev, &dev_attr_owner) ||
  914. device_create_file(&tun->dev->dev, &dev_attr_group))
  915. pr_err("Failed to create tun sysfs files\n");
  916. sk->sk_destruct = tun_sock_destruct;
  917. err = tun_attach(tun, file);
  918. if (err < 0)
  919. goto failed;
  920. }
  921. tun_debug(KERN_INFO, tun, "tun_set_iff\n");
  922. if (ifr->ifr_flags & IFF_NO_PI)
  923. tun->flags |= TUN_NO_PI;
  924. else
  925. tun->flags &= ~TUN_NO_PI;
  926. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  927. tun->flags |= TUN_ONE_QUEUE;
  928. else
  929. tun->flags &= ~TUN_ONE_QUEUE;
  930. if (ifr->ifr_flags & IFF_VNET_HDR)
  931. tun->flags |= TUN_VNET_HDR;
  932. else
  933. tun->flags &= ~TUN_VNET_HDR;
  934. /* Make sure persistent devices do not get stuck in
  935. * xoff state.
  936. */
  937. if (netif_running(tun->dev))
  938. netif_wake_queue(tun->dev);
  939. strcpy(ifr->ifr_name, tun->dev->name);
  940. return 0;
  941. err_free_sk:
  942. sock_put(sk);
  943. err_free_dev:
  944. free_netdev(dev);
  945. failed:
  946. return err;
  947. }
  948. static int tun_get_iff(struct net *net, struct tun_struct *tun,
  949. struct ifreq *ifr)
  950. {
  951. tun_debug(KERN_INFO, tun, "tun_get_iff\n");
  952. strcpy(ifr->ifr_name, tun->dev->name);
  953. ifr->ifr_flags = tun_flags(tun);
  954. return 0;
  955. }
  956. /* This is like a cut-down ethtool ops, except done via tun fd so no
  957. * privs required. */
  958. static int set_offload(struct tun_struct *tun, unsigned long arg)
  959. {
  960. u32 features = 0;
  961. if (arg & TUN_F_CSUM) {
  962. features |= NETIF_F_HW_CSUM;
  963. arg &= ~TUN_F_CSUM;
  964. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  965. if (arg & TUN_F_TSO_ECN) {
  966. features |= NETIF_F_TSO_ECN;
  967. arg &= ~TUN_F_TSO_ECN;
  968. }
  969. if (arg & TUN_F_TSO4)
  970. features |= NETIF_F_TSO;
  971. if (arg & TUN_F_TSO6)
  972. features |= NETIF_F_TSO6;
  973. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  974. }
  975. if (arg & TUN_F_UFO) {
  976. features |= NETIF_F_UFO;
  977. arg &= ~TUN_F_UFO;
  978. }
  979. }
  980. /* This gives the user a way to test for new features in future by
  981. * trying to set them. */
  982. if (arg)
  983. return -EINVAL;
  984. tun->set_features = features;
  985. netdev_update_features(tun->dev);
  986. return 0;
  987. }
  988. static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  989. unsigned long arg, int ifreq_len)
  990. {
  991. struct tun_file *tfile = file->private_data;
  992. struct tun_struct *tun;
  993. void __user* argp = (void __user*)arg;
  994. struct sock_fprog fprog;
  995. struct ifreq ifr;
  996. int sndbuf;
  997. int vnet_hdr_sz;
  998. int ret;
  999. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
  1000. if (copy_from_user(&ifr, argp, ifreq_len))
  1001. return -EFAULT;
  1002. if (cmd == TUNGETFEATURES) {
  1003. /* Currently this just means: "what IFF flags are valid?".
  1004. * This is needed because we never checked for invalid flags on
  1005. * TUNSETIFF. */
  1006. return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
  1007. IFF_VNET_HDR,
  1008. (unsigned int __user*)argp);
  1009. }
  1010. rtnl_lock();
  1011. tun = __tun_get(tfile);
  1012. if (cmd == TUNSETIFF && !tun) {
  1013. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  1014. ret = tun_set_iff(tfile->net, file, &ifr);
  1015. if (ret)
  1016. goto unlock;
  1017. if (copy_to_user(argp, &ifr, ifreq_len))
  1018. ret = -EFAULT;
  1019. goto unlock;
  1020. }
  1021. ret = -EBADFD;
  1022. if (!tun)
  1023. goto unlock;
  1024. tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %d\n", cmd);
  1025. ret = 0;
  1026. switch (cmd) {
  1027. case TUNGETIFF:
  1028. ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
  1029. if (ret)
  1030. break;
  1031. if (copy_to_user(argp, &ifr, ifreq_len))
  1032. ret = -EFAULT;
  1033. break;
  1034. case TUNSETNOCSUM:
  1035. /* Disable/Enable checksum */
  1036. /* [unimplemented] */
  1037. tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
  1038. arg ? "disabled" : "enabled");
  1039. break;
  1040. case TUNSETPERSIST:
  1041. /* Disable/Enable persist mode */
  1042. if (arg)
  1043. tun->flags |= TUN_PERSIST;
  1044. else
  1045. tun->flags &= ~TUN_PERSIST;
  1046. tun_debug(KERN_INFO, tun, "persist %s\n",
  1047. arg ? "enabled" : "disabled");
  1048. break;
  1049. case TUNSETOWNER:
  1050. /* Set owner of the device */
  1051. tun->owner = (uid_t) arg;
  1052. tun_debug(KERN_INFO, tun, "owner set to %d\n", tun->owner);
  1053. break;
  1054. case TUNSETGROUP:
  1055. /* Set group of the device */
  1056. tun->group= (gid_t) arg;
  1057. tun_debug(KERN_INFO, tun, "group set to %d\n", tun->group);
  1058. break;
  1059. case TUNSETLINK:
  1060. /* Only allow setting the type when the interface is down */
  1061. if (tun->dev->flags & IFF_UP) {
  1062. tun_debug(KERN_INFO, tun,
  1063. "Linktype set failed because interface is up\n");
  1064. ret = -EBUSY;
  1065. } else {
  1066. tun->dev->type = (int) arg;
  1067. tun_debug(KERN_INFO, tun, "linktype set to %d\n",
  1068. tun->dev->type);
  1069. ret = 0;
  1070. }
  1071. break;
  1072. #ifdef TUN_DEBUG
  1073. case TUNSETDEBUG:
  1074. tun->debug = arg;
  1075. break;
  1076. #endif
  1077. case TUNSETOFFLOAD:
  1078. ret = set_offload(tun, arg);
  1079. break;
  1080. case TUNSETTXFILTER:
  1081. /* Can be set only for TAPs */
  1082. ret = -EINVAL;
  1083. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1084. break;
  1085. ret = update_filter(&tun->txflt, (void __user *)arg);
  1086. break;
  1087. case SIOCGIFHWADDR:
  1088. /* Get hw address */
  1089. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  1090. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  1091. if (copy_to_user(argp, &ifr, ifreq_len))
  1092. ret = -EFAULT;
  1093. break;
  1094. case SIOCSIFHWADDR:
  1095. /* Set hw address */
  1096. tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
  1097. ifr.ifr_hwaddr.sa_data);
  1098. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  1099. break;
  1100. case TUNGETSNDBUF:
  1101. sndbuf = tun->socket.sk->sk_sndbuf;
  1102. if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
  1103. ret = -EFAULT;
  1104. break;
  1105. case TUNSETSNDBUF:
  1106. if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
  1107. ret = -EFAULT;
  1108. break;
  1109. }
  1110. tun->socket.sk->sk_sndbuf = sndbuf;
  1111. break;
  1112. case TUNGETVNETHDRSZ:
  1113. vnet_hdr_sz = tun->vnet_hdr_sz;
  1114. if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
  1115. ret = -EFAULT;
  1116. break;
  1117. case TUNSETVNETHDRSZ:
  1118. if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
  1119. ret = -EFAULT;
  1120. break;
  1121. }
  1122. if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
  1123. ret = -EINVAL;
  1124. break;
  1125. }
  1126. tun->vnet_hdr_sz = vnet_hdr_sz;
  1127. break;
  1128. case TUNATTACHFILTER:
  1129. /* Can be set only for TAPs */
  1130. ret = -EINVAL;
  1131. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1132. break;
  1133. ret = -EFAULT;
  1134. if (copy_from_user(&fprog, argp, sizeof(fprog)))
  1135. break;
  1136. ret = sk_attach_filter(&fprog, tun->socket.sk);
  1137. break;
  1138. case TUNDETACHFILTER:
  1139. /* Can be set only for TAPs */
  1140. ret = -EINVAL;
  1141. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1142. break;
  1143. ret = sk_detach_filter(tun->socket.sk);
  1144. break;
  1145. default:
  1146. ret = -EINVAL;
  1147. break;
  1148. }
  1149. unlock:
  1150. rtnl_unlock();
  1151. if (tun)
  1152. tun_put(tun);
  1153. return ret;
  1154. }
  1155. static long tun_chr_ioctl(struct file *file,
  1156. unsigned int cmd, unsigned long arg)
  1157. {
  1158. return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
  1159. }
  1160. #ifdef CONFIG_COMPAT
  1161. static long tun_chr_compat_ioctl(struct file *file,
  1162. unsigned int cmd, unsigned long arg)
  1163. {
  1164. switch (cmd) {
  1165. case TUNSETIFF:
  1166. case TUNGETIFF:
  1167. case TUNSETTXFILTER:
  1168. case TUNGETSNDBUF:
  1169. case TUNSETSNDBUF:
  1170. case SIOCGIFHWADDR:
  1171. case SIOCSIFHWADDR:
  1172. arg = (unsigned long)compat_ptr(arg);
  1173. break;
  1174. default:
  1175. arg = (compat_ulong_t)arg;
  1176. break;
  1177. }
  1178. /*
  1179. * compat_ifreq is shorter than ifreq, so we must not access beyond
  1180. * the end of that structure. All fields that are used in this
  1181. * driver are compatible though, we don't need to convert the
  1182. * contents.
  1183. */
  1184. return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
  1185. }
  1186. #endif /* CONFIG_COMPAT */
  1187. static int tun_chr_fasync(int fd, struct file *file, int on)
  1188. {
  1189. struct tun_struct *tun = tun_get(file);
  1190. int ret;
  1191. if (!tun)
  1192. return -EBADFD;
  1193. tun_debug(KERN_INFO, tun, "tun_chr_fasync %d\n", on);
  1194. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  1195. goto out;
  1196. if (on) {
  1197. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  1198. if (ret)
  1199. goto out;
  1200. tun->flags |= TUN_FASYNC;
  1201. } else
  1202. tun->flags &= ~TUN_FASYNC;
  1203. ret = 0;
  1204. out:
  1205. tun_put(tun);
  1206. return ret;
  1207. }
  1208. static int tun_chr_open(struct inode *inode, struct file * file)
  1209. {
  1210. struct tun_file *tfile;
  1211. DBG1(KERN_INFO, "tunX: tun_chr_open\n");
  1212. tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
  1213. if (!tfile)
  1214. return -ENOMEM;
  1215. atomic_set(&tfile->count, 0);
  1216. tfile->tun = NULL;
  1217. tfile->net = get_net(current->nsproxy->net_ns);
  1218. file->private_data = tfile;
  1219. return 0;
  1220. }
  1221. static int tun_chr_close(struct inode *inode, struct file *file)
  1222. {
  1223. struct tun_file *tfile = file->private_data;
  1224. struct tun_struct *tun;
  1225. tun = __tun_get(tfile);
  1226. if (tun) {
  1227. struct net_device *dev = tun->dev;
  1228. tun_debug(KERN_INFO, tun, "tun_chr_close\n");
  1229. __tun_detach(tun);
  1230. /* If desirable, unregister the netdevice. */
  1231. if (!(tun->flags & TUN_PERSIST)) {
  1232. rtnl_lock();
  1233. if (dev->reg_state == NETREG_REGISTERED)
  1234. unregister_netdevice(dev);
  1235. rtnl_unlock();
  1236. }
  1237. }
  1238. tun = tfile->tun;
  1239. if (tun)
  1240. sock_put(tun->socket.sk);
  1241. put_net(tfile->net);
  1242. kfree(tfile);
  1243. return 0;
  1244. }
  1245. static const struct file_operations tun_fops = {
  1246. .owner = THIS_MODULE,
  1247. .llseek = no_llseek,
  1248. .read = do_sync_read,
  1249. .aio_read = tun_chr_aio_read,
  1250. .write = do_sync_write,
  1251. .aio_write = tun_chr_aio_write,
  1252. .poll = tun_chr_poll,
  1253. .unlocked_ioctl = tun_chr_ioctl,
  1254. #ifdef CONFIG_COMPAT
  1255. .compat_ioctl = tun_chr_compat_ioctl,
  1256. #endif
  1257. .open = tun_chr_open,
  1258. .release = tun_chr_close,
  1259. .fasync = tun_chr_fasync
  1260. };
  1261. static struct miscdevice tun_miscdev = {
  1262. .minor = TUN_MINOR,
  1263. .name = "tun",
  1264. .nodename = "net/tun",
  1265. .fops = &tun_fops,
  1266. };
  1267. /* ethtool interface */
  1268. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1269. {
  1270. cmd->supported = 0;
  1271. cmd->advertising = 0;
  1272. ethtool_cmd_speed_set(cmd, SPEED_10);
  1273. cmd->duplex = DUPLEX_FULL;
  1274. cmd->port = PORT_TP;
  1275. cmd->phy_address = 0;
  1276. cmd->transceiver = XCVR_INTERNAL;
  1277. cmd->autoneg = AUTONEG_DISABLE;
  1278. cmd->maxtxpkt = 0;
  1279. cmd->maxrxpkt = 0;
  1280. return 0;
  1281. }
  1282. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1283. {
  1284. struct tun_struct *tun = netdev_priv(dev);
  1285. strcpy(info->driver, DRV_NAME);
  1286. strcpy(info->version, DRV_VERSION);
  1287. strcpy(info->fw_version, "N/A");
  1288. switch (tun->flags & TUN_TYPE_MASK) {
  1289. case TUN_TUN_DEV:
  1290. strcpy(info->bus_info, "tun");
  1291. break;
  1292. case TUN_TAP_DEV:
  1293. strcpy(info->bus_info, "tap");
  1294. break;
  1295. }
  1296. }
  1297. static u32 tun_get_msglevel(struct net_device *dev)
  1298. {
  1299. #ifdef TUN_DEBUG
  1300. struct tun_struct *tun = netdev_priv(dev);
  1301. return tun->debug;
  1302. #else
  1303. return -EOPNOTSUPP;
  1304. #endif
  1305. }
  1306. static void tun_set_msglevel(struct net_device *dev, u32 value)
  1307. {
  1308. #ifdef TUN_DEBUG
  1309. struct tun_struct *tun = netdev_priv(dev);
  1310. tun->debug = value;
  1311. #endif
  1312. }
  1313. static const struct ethtool_ops tun_ethtool_ops = {
  1314. .get_settings = tun_get_settings,
  1315. .get_drvinfo = tun_get_drvinfo,
  1316. .get_msglevel = tun_get_msglevel,
  1317. .set_msglevel = tun_set_msglevel,
  1318. .get_link = ethtool_op_get_link,
  1319. };
  1320. static int __init tun_init(void)
  1321. {
  1322. int ret = 0;
  1323. pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1324. pr_info("%s\n", DRV_COPYRIGHT);
  1325. ret = rtnl_link_register(&tun_link_ops);
  1326. if (ret) {
  1327. pr_err("Can't register link_ops\n");
  1328. goto err_linkops;
  1329. }
  1330. ret = misc_register(&tun_miscdev);
  1331. if (ret) {
  1332. pr_err("Can't register misc device %d\n", TUN_MINOR);
  1333. goto err_misc;
  1334. }
  1335. return 0;
  1336. err_misc:
  1337. rtnl_link_unregister(&tun_link_ops);
  1338. err_linkops:
  1339. return ret;
  1340. }
  1341. static void tun_cleanup(void)
  1342. {
  1343. misc_deregister(&tun_miscdev);
  1344. rtnl_link_unregister(&tun_link_ops);
  1345. }
  1346. /* Get an underlying socket object from tun file. Returns error unless file is
  1347. * attached to a device. The returned object works like a packet socket, it
  1348. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1349. * holding a reference to the file for as long as the socket is in use. */
  1350. struct socket *tun_get_socket(struct file *file)
  1351. {
  1352. struct tun_struct *tun;
  1353. if (file->f_op != &tun_fops)
  1354. return ERR_PTR(-EINVAL);
  1355. tun = tun_get(file);
  1356. if (!tun)
  1357. return ERR_PTR(-EBADFD);
  1358. tun_put(tun);
  1359. return &tun->socket;
  1360. }
  1361. EXPORT_SYMBOL_GPL(tun_get_socket);
  1362. module_init(tun_init);
  1363. module_exit(tun_cleanup);
  1364. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1365. MODULE_AUTHOR(DRV_COPYRIGHT);
  1366. MODULE_LICENSE("GPL");
  1367. MODULE_ALIAS_MISCDEV(TUN_MINOR);
  1368. MODULE_ALIAS("devname:net/tun");