tun.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  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 DRV_NAME "tun"
  36. #define DRV_VERSION "1.6"
  37. #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
  38. #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
  39. #include <linux/module.h>
  40. #include <linux/errno.h>
  41. #include <linux/kernel.h>
  42. #include <linux/major.h>
  43. #include <linux/slab.h>
  44. #include <linux/smp_lock.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/if.h>
  55. #include <linux/if_arp.h>
  56. #include <linux/if_ether.h>
  57. #include <linux/if_tun.h>
  58. #include <linux/crc32.h>
  59. #include <linux/nsproxy.h>
  60. #include <linux/virtio_net.h>
  61. #include <net/net_namespace.h>
  62. #include <net/netns/generic.h>
  63. #include <asm/system.h>
  64. #include <asm/uaccess.h>
  65. /* Uncomment to enable debugging */
  66. /* #define TUN_DEBUG 1 */
  67. #ifdef TUN_DEBUG
  68. static int debug;
  69. #define DBG if(tun->debug)printk
  70. #define DBG1 if(debug==2)printk
  71. #else
  72. #define DBG( a... )
  73. #define DBG1( a... )
  74. #endif
  75. #define FLT_EXACT_COUNT 8
  76. struct tap_filter {
  77. unsigned int count; /* Number of addrs. Zero means disabled */
  78. u32 mask[2]; /* Mask of the hashed addrs */
  79. unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
  80. };
  81. struct tun_file {
  82. struct tun_struct *tun;
  83. struct net *net;
  84. wait_queue_head_t read_wait;
  85. };
  86. struct tun_struct {
  87. struct tun_file *tfile;
  88. unsigned int flags;
  89. uid_t owner;
  90. gid_t group;
  91. struct sk_buff_head readq;
  92. struct net_device *dev;
  93. struct fasync_struct *fasync;
  94. struct tap_filter txflt;
  95. #ifdef TUN_DEBUG
  96. int debug;
  97. #endif
  98. };
  99. static int tun_attach(struct tun_struct *tun, struct file *file)
  100. {
  101. struct tun_file *tfile = file->private_data;
  102. const struct cred *cred = current_cred();
  103. int err;
  104. ASSERT_RTNL();
  105. /* Check permissions */
  106. if (((tun->owner != -1 && cred->euid != tun->owner) ||
  107. (tun->group != -1 && cred->egid != tun->group)) &&
  108. !capable(CAP_NET_ADMIN))
  109. return -EPERM;
  110. netif_tx_lock_bh(tun->dev);
  111. err = -EINVAL;
  112. if (tfile->tun)
  113. goto out;
  114. err = -EBUSY;
  115. if (tun->tfile)
  116. goto out;
  117. err = 0;
  118. tfile->tun = tun;
  119. tun->tfile = tfile;
  120. out:
  121. netif_tx_unlock_bh(tun->dev);
  122. return err;
  123. }
  124. static void __tun_detach(struct tun_struct *tun)
  125. {
  126. struct tun_file *tfile = tun->tfile;
  127. /* Detach from net device */
  128. netif_tx_lock_bh(tun->dev);
  129. tfile->tun = NULL;
  130. tun->tfile = NULL;
  131. netif_tx_unlock_bh(tun->dev);
  132. /* Drop read queue */
  133. skb_queue_purge(&tun->readq);
  134. }
  135. static struct tun_struct *__tun_get(struct tun_file *tfile)
  136. {
  137. return tfile->tun;
  138. }
  139. static struct tun_struct *tun_get(struct file *file)
  140. {
  141. return __tun_get(file->private_data);
  142. }
  143. static void tun_put(struct tun_struct *tun)
  144. {
  145. /* Noop for now */
  146. }
  147. /* TAP filterting */
  148. static void addr_hash_set(u32 *mask, const u8 *addr)
  149. {
  150. int n = ether_crc(ETH_ALEN, addr) >> 26;
  151. mask[n >> 5] |= (1 << (n & 31));
  152. }
  153. static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
  154. {
  155. int n = ether_crc(ETH_ALEN, addr) >> 26;
  156. return mask[n >> 5] & (1 << (n & 31));
  157. }
  158. static int update_filter(struct tap_filter *filter, void __user *arg)
  159. {
  160. struct { u8 u[ETH_ALEN]; } *addr;
  161. struct tun_filter uf;
  162. int err, alen, n, nexact;
  163. if (copy_from_user(&uf, arg, sizeof(uf)))
  164. return -EFAULT;
  165. if (!uf.count) {
  166. /* Disabled */
  167. filter->count = 0;
  168. return 0;
  169. }
  170. alen = ETH_ALEN * uf.count;
  171. addr = kmalloc(alen, GFP_KERNEL);
  172. if (!addr)
  173. return -ENOMEM;
  174. if (copy_from_user(addr, arg + sizeof(uf), alen)) {
  175. err = -EFAULT;
  176. goto done;
  177. }
  178. /* The filter is updated without holding any locks. Which is
  179. * perfectly safe. We disable it first and in the worst
  180. * case we'll accept a few undesired packets. */
  181. filter->count = 0;
  182. wmb();
  183. /* Use first set of addresses as an exact filter */
  184. for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
  185. memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
  186. nexact = n;
  187. /* The rest is hashed */
  188. memset(filter->mask, 0, sizeof(filter->mask));
  189. for (; n < uf.count; n++)
  190. addr_hash_set(filter->mask, addr[n].u);
  191. /* For ALLMULTI just set the mask to all ones.
  192. * This overrides the mask populated above. */
  193. if ((uf.flags & TUN_FLT_ALLMULTI))
  194. memset(filter->mask, ~0, sizeof(filter->mask));
  195. /* Now enable the filter */
  196. wmb();
  197. filter->count = nexact;
  198. /* Return the number of exact filters */
  199. err = nexact;
  200. done:
  201. kfree(addr);
  202. return err;
  203. }
  204. /* Returns: 0 - drop, !=0 - accept */
  205. static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
  206. {
  207. /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
  208. * at this point. */
  209. struct ethhdr *eh = (struct ethhdr *) skb->data;
  210. int i;
  211. /* Exact match */
  212. for (i = 0; i < filter->count; i++)
  213. if (!compare_ether_addr(eh->h_dest, filter->addr[i]))
  214. return 1;
  215. /* Inexact match (multicast only) */
  216. if (is_multicast_ether_addr(eh->h_dest))
  217. return addr_hash_test(filter->mask, eh->h_dest);
  218. return 0;
  219. }
  220. /*
  221. * Checks whether the packet is accepted or not.
  222. * Returns: 0 - drop, !=0 - accept
  223. */
  224. static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
  225. {
  226. if (!filter->count)
  227. return 1;
  228. return run_filter(filter, skb);
  229. }
  230. /* Network device part of the driver */
  231. static const struct ethtool_ops tun_ethtool_ops;
  232. /* Net device open. */
  233. static int tun_net_open(struct net_device *dev)
  234. {
  235. netif_start_queue(dev);
  236. return 0;
  237. }
  238. /* Net device close. */
  239. static int tun_net_close(struct net_device *dev)
  240. {
  241. netif_stop_queue(dev);
  242. return 0;
  243. }
  244. /* Net device start xmit */
  245. static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
  246. {
  247. struct tun_struct *tun = netdev_priv(dev);
  248. DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
  249. /* Drop packet if interface is not attached */
  250. if (!tun->tfile)
  251. goto drop;
  252. /* Drop if the filter does not like it.
  253. * This is a noop if the filter is disabled.
  254. * Filter can be enabled only for the TAP devices. */
  255. if (!check_filter(&tun->txflt, skb))
  256. goto drop;
  257. if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
  258. if (!(tun->flags & TUN_ONE_QUEUE)) {
  259. /* Normal queueing mode. */
  260. /* Packet scheduler handles dropping of further packets. */
  261. netif_stop_queue(dev);
  262. /* We won't see all dropped packets individually, so overrun
  263. * error is more appropriate. */
  264. dev->stats.tx_fifo_errors++;
  265. } else {
  266. /* Single queue mode.
  267. * Driver handles dropping of all packets itself. */
  268. goto drop;
  269. }
  270. }
  271. /* Enqueue packet */
  272. skb_queue_tail(&tun->readq, skb);
  273. dev->trans_start = jiffies;
  274. /* Notify and wake up reader process */
  275. if (tun->flags & TUN_FASYNC)
  276. kill_fasync(&tun->fasync, SIGIO, POLL_IN);
  277. wake_up_interruptible(&tun->tfile->read_wait);
  278. return 0;
  279. drop:
  280. dev->stats.tx_dropped++;
  281. kfree_skb(skb);
  282. return 0;
  283. }
  284. static void tun_net_mclist(struct net_device *dev)
  285. {
  286. /*
  287. * This callback is supposed to deal with mc filter in
  288. * _rx_ path and has nothing to do with the _tx_ path.
  289. * In rx path we always accept everything userspace gives us.
  290. */
  291. return;
  292. }
  293. #define MIN_MTU 68
  294. #define MAX_MTU 65535
  295. static int
  296. tun_net_change_mtu(struct net_device *dev, int new_mtu)
  297. {
  298. if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
  299. return -EINVAL;
  300. dev->mtu = new_mtu;
  301. return 0;
  302. }
  303. static const struct net_device_ops tun_netdev_ops = {
  304. .ndo_open = tun_net_open,
  305. .ndo_stop = tun_net_close,
  306. .ndo_start_xmit = tun_net_xmit,
  307. .ndo_change_mtu = tun_net_change_mtu,
  308. };
  309. static const struct net_device_ops tap_netdev_ops = {
  310. .ndo_open = tun_net_open,
  311. .ndo_stop = tun_net_close,
  312. .ndo_start_xmit = tun_net_xmit,
  313. .ndo_change_mtu = tun_net_change_mtu,
  314. .ndo_set_multicast_list = tun_net_mclist,
  315. .ndo_set_mac_address = eth_mac_addr,
  316. .ndo_validate_addr = eth_validate_addr,
  317. };
  318. /* Initialize net device. */
  319. static void tun_net_init(struct net_device *dev)
  320. {
  321. struct tun_struct *tun = netdev_priv(dev);
  322. switch (tun->flags & TUN_TYPE_MASK) {
  323. case TUN_TUN_DEV:
  324. dev->netdev_ops = &tun_netdev_ops;
  325. /* Point-to-Point TUN Device */
  326. dev->hard_header_len = 0;
  327. dev->addr_len = 0;
  328. dev->mtu = 1500;
  329. /* Zero header length */
  330. dev->type = ARPHRD_NONE;
  331. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  332. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  333. break;
  334. case TUN_TAP_DEV:
  335. dev->netdev_ops = &tap_netdev_ops;
  336. /* Ethernet TAP Device */
  337. ether_setup(dev);
  338. random_ether_addr(dev->dev_addr);
  339. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  340. break;
  341. }
  342. }
  343. /* Character device part */
  344. /* Poll */
  345. static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
  346. {
  347. struct tun_file *tfile = file->private_data;
  348. struct tun_struct *tun = __tun_get(tfile);
  349. unsigned int mask = POLLOUT | POLLWRNORM;
  350. if (!tun)
  351. return POLLERR;
  352. DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
  353. poll_wait(file, &tfile->read_wait, wait);
  354. if (!skb_queue_empty(&tun->readq))
  355. mask |= POLLIN | POLLRDNORM;
  356. tun_put(tun);
  357. return mask;
  358. }
  359. /* prepad is the amount to reserve at front. len is length after that.
  360. * linear is a hint as to how much to copy (usually headers). */
  361. static struct sk_buff *tun_alloc_skb(size_t prepad, size_t len, size_t linear,
  362. gfp_t gfp)
  363. {
  364. struct sk_buff *skb;
  365. unsigned int i;
  366. skb = alloc_skb(prepad + len, gfp|__GFP_NOWARN);
  367. if (skb) {
  368. skb_reserve(skb, prepad);
  369. skb_put(skb, len);
  370. return skb;
  371. }
  372. /* Under a page? Don't bother with paged skb. */
  373. if (prepad + len < PAGE_SIZE)
  374. return NULL;
  375. /* Start with a normal skb, and add pages. */
  376. skb = alloc_skb(prepad + linear, gfp);
  377. if (!skb)
  378. return NULL;
  379. skb_reserve(skb, prepad);
  380. skb_put(skb, linear);
  381. len -= linear;
  382. for (i = 0; i < MAX_SKB_FRAGS; i++) {
  383. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  384. f->page = alloc_page(gfp|__GFP_ZERO);
  385. if (!f->page)
  386. break;
  387. f->page_offset = 0;
  388. f->size = PAGE_SIZE;
  389. skb->data_len += PAGE_SIZE;
  390. skb->len += PAGE_SIZE;
  391. skb->truesize += PAGE_SIZE;
  392. skb_shinfo(skb)->nr_frags++;
  393. if (len < PAGE_SIZE) {
  394. len = 0;
  395. break;
  396. }
  397. len -= PAGE_SIZE;
  398. }
  399. /* Too large, or alloc fail? */
  400. if (unlikely(len)) {
  401. kfree_skb(skb);
  402. skb = NULL;
  403. }
  404. return skb;
  405. }
  406. /* Get packet from user space buffer */
  407. static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
  408. {
  409. struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
  410. struct sk_buff *skb;
  411. size_t len = count, align = 0;
  412. struct virtio_net_hdr gso = { 0 };
  413. if (!(tun->flags & TUN_NO_PI)) {
  414. if ((len -= sizeof(pi)) > count)
  415. return -EINVAL;
  416. if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
  417. return -EFAULT;
  418. }
  419. if (tun->flags & TUN_VNET_HDR) {
  420. if ((len -= sizeof(gso)) > count)
  421. return -EINVAL;
  422. if (memcpy_fromiovec((void *)&gso, iv, sizeof(gso)))
  423. return -EFAULT;
  424. if (gso.hdr_len > len)
  425. return -EINVAL;
  426. }
  427. if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
  428. align = NET_IP_ALIGN;
  429. if (unlikely(len < ETH_HLEN))
  430. return -EINVAL;
  431. }
  432. if (!(skb = tun_alloc_skb(align, len, gso.hdr_len, GFP_KERNEL))) {
  433. tun->dev->stats.rx_dropped++;
  434. return -ENOMEM;
  435. }
  436. if (skb_copy_datagram_from_iovec(skb, 0, iv, len)) {
  437. tun->dev->stats.rx_dropped++;
  438. kfree_skb(skb);
  439. return -EFAULT;
  440. }
  441. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  442. if (!skb_partial_csum_set(skb, gso.csum_start,
  443. gso.csum_offset)) {
  444. tun->dev->stats.rx_frame_errors++;
  445. kfree_skb(skb);
  446. return -EINVAL;
  447. }
  448. } else if (tun->flags & TUN_NOCHECKSUM)
  449. skb->ip_summed = CHECKSUM_UNNECESSARY;
  450. switch (tun->flags & TUN_TYPE_MASK) {
  451. case TUN_TUN_DEV:
  452. if (tun->flags & TUN_NO_PI) {
  453. switch (skb->data[0] & 0xf0) {
  454. case 0x40:
  455. pi.proto = htons(ETH_P_IP);
  456. break;
  457. case 0x60:
  458. pi.proto = htons(ETH_P_IPV6);
  459. break;
  460. default:
  461. tun->dev->stats.rx_dropped++;
  462. kfree_skb(skb);
  463. return -EINVAL;
  464. }
  465. }
  466. skb_reset_mac_header(skb);
  467. skb->protocol = pi.proto;
  468. skb->dev = tun->dev;
  469. break;
  470. case TUN_TAP_DEV:
  471. skb->protocol = eth_type_trans(skb, tun->dev);
  472. break;
  473. };
  474. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  475. pr_debug("GSO!\n");
  476. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  477. case VIRTIO_NET_HDR_GSO_TCPV4:
  478. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  479. break;
  480. case VIRTIO_NET_HDR_GSO_TCPV6:
  481. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  482. break;
  483. default:
  484. tun->dev->stats.rx_frame_errors++;
  485. kfree_skb(skb);
  486. return -EINVAL;
  487. }
  488. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  489. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  490. skb_shinfo(skb)->gso_size = gso.gso_size;
  491. if (skb_shinfo(skb)->gso_size == 0) {
  492. tun->dev->stats.rx_frame_errors++;
  493. kfree_skb(skb);
  494. return -EINVAL;
  495. }
  496. /* Header must be checked, and gso_segs computed. */
  497. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  498. skb_shinfo(skb)->gso_segs = 0;
  499. }
  500. netif_rx_ni(skb);
  501. tun->dev->stats.rx_packets++;
  502. tun->dev->stats.rx_bytes += len;
  503. return count;
  504. }
  505. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  506. unsigned long count, loff_t pos)
  507. {
  508. struct tun_struct *tun = tun_get(iocb->ki_filp);
  509. ssize_t result;
  510. if (!tun)
  511. return -EBADFD;
  512. DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
  513. result = tun_get_user(tun, (struct iovec *) iv, iov_length(iv, count));
  514. tun_put(tun);
  515. return result;
  516. }
  517. /* Put packet to the user space buffer */
  518. static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
  519. struct sk_buff *skb,
  520. struct iovec *iv, int len)
  521. {
  522. struct tun_pi pi = { 0, skb->protocol };
  523. ssize_t total = 0;
  524. if (!(tun->flags & TUN_NO_PI)) {
  525. if ((len -= sizeof(pi)) < 0)
  526. return -EINVAL;
  527. if (len < skb->len) {
  528. /* Packet will be striped */
  529. pi.flags |= TUN_PKT_STRIP;
  530. }
  531. if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
  532. return -EFAULT;
  533. total += sizeof(pi);
  534. }
  535. if (tun->flags & TUN_VNET_HDR) {
  536. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  537. if ((len -= sizeof(gso)) < 0)
  538. return -EINVAL;
  539. if (skb_is_gso(skb)) {
  540. struct skb_shared_info *sinfo = skb_shinfo(skb);
  541. /* This is a hint as to how much should be linear. */
  542. gso.hdr_len = skb_headlen(skb);
  543. gso.gso_size = sinfo->gso_size;
  544. if (sinfo->gso_type & SKB_GSO_TCPV4)
  545. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  546. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  547. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  548. else
  549. BUG();
  550. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  551. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  552. } else
  553. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  554. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  555. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  556. gso.csum_start = skb->csum_start - skb_headroom(skb);
  557. gso.csum_offset = skb->csum_offset;
  558. } /* else everything is zero */
  559. if (unlikely(memcpy_toiovec(iv, (void *)&gso, sizeof(gso))))
  560. return -EFAULT;
  561. total += sizeof(gso);
  562. }
  563. len = min_t(int, skb->len, len);
  564. skb_copy_datagram_iovec(skb, 0, iv, len);
  565. total += len;
  566. tun->dev->stats.tx_packets++;
  567. tun->dev->stats.tx_bytes += len;
  568. return total;
  569. }
  570. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  571. unsigned long count, loff_t pos)
  572. {
  573. struct file *file = iocb->ki_filp;
  574. struct tun_file *tfile = file->private_data;
  575. struct tun_struct *tun = __tun_get(tfile);
  576. DECLARE_WAITQUEUE(wait, current);
  577. struct sk_buff *skb;
  578. ssize_t len, ret = 0;
  579. if (!tun)
  580. return -EBADFD;
  581. DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
  582. len = iov_length(iv, count);
  583. if (len < 0) {
  584. ret = -EINVAL;
  585. goto out;
  586. }
  587. add_wait_queue(&tfile->read_wait, &wait);
  588. while (len) {
  589. current->state = TASK_INTERRUPTIBLE;
  590. /* Read frames from the queue */
  591. if (!(skb=skb_dequeue(&tun->readq))) {
  592. if (file->f_flags & O_NONBLOCK) {
  593. ret = -EAGAIN;
  594. break;
  595. }
  596. if (signal_pending(current)) {
  597. ret = -ERESTARTSYS;
  598. break;
  599. }
  600. /* Nothing to read, let's sleep */
  601. schedule();
  602. continue;
  603. }
  604. netif_wake_queue(tun->dev);
  605. ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
  606. kfree_skb(skb);
  607. break;
  608. }
  609. current->state = TASK_RUNNING;
  610. remove_wait_queue(&tfile->read_wait, &wait);
  611. out:
  612. tun_put(tun);
  613. return ret;
  614. }
  615. static void tun_setup(struct net_device *dev)
  616. {
  617. struct tun_struct *tun = netdev_priv(dev);
  618. skb_queue_head_init(&tun->readq);
  619. tun->owner = -1;
  620. tun->group = -1;
  621. dev->ethtool_ops = &tun_ethtool_ops;
  622. dev->destructor = free_netdev;
  623. dev->features |= NETIF_F_NETNS_LOCAL;
  624. }
  625. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  626. {
  627. struct tun_struct *tun;
  628. struct net_device *dev;
  629. int err;
  630. dev = __dev_get_by_name(net, ifr->ifr_name);
  631. if (dev) {
  632. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  633. tun = netdev_priv(dev);
  634. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  635. tun = netdev_priv(dev);
  636. else
  637. return -EINVAL;
  638. err = tun_attach(tun, file);
  639. if (err < 0)
  640. return err;
  641. }
  642. else {
  643. char *name;
  644. unsigned long flags = 0;
  645. err = -EINVAL;
  646. if (!capable(CAP_NET_ADMIN))
  647. return -EPERM;
  648. /* Set dev type */
  649. if (ifr->ifr_flags & IFF_TUN) {
  650. /* TUN device */
  651. flags |= TUN_TUN_DEV;
  652. name = "tun%d";
  653. } else if (ifr->ifr_flags & IFF_TAP) {
  654. /* TAP device */
  655. flags |= TUN_TAP_DEV;
  656. name = "tap%d";
  657. } else
  658. goto failed;
  659. if (*ifr->ifr_name)
  660. name = ifr->ifr_name;
  661. dev = alloc_netdev(sizeof(struct tun_struct), name,
  662. tun_setup);
  663. if (!dev)
  664. return -ENOMEM;
  665. dev_net_set(dev, net);
  666. tun = netdev_priv(dev);
  667. tun->dev = dev;
  668. tun->flags = flags;
  669. tun->txflt.count = 0;
  670. tun_net_init(dev);
  671. if (strchr(dev->name, '%')) {
  672. err = dev_alloc_name(dev, dev->name);
  673. if (err < 0)
  674. goto err_free_dev;
  675. }
  676. err = register_netdevice(tun->dev);
  677. if (err < 0)
  678. goto err_free_dev;
  679. err = tun_attach(tun, file);
  680. if (err < 0)
  681. goto err_free_dev;
  682. }
  683. DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
  684. if (ifr->ifr_flags & IFF_NO_PI)
  685. tun->flags |= TUN_NO_PI;
  686. else
  687. tun->flags &= ~TUN_NO_PI;
  688. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  689. tun->flags |= TUN_ONE_QUEUE;
  690. else
  691. tun->flags &= ~TUN_ONE_QUEUE;
  692. if (ifr->ifr_flags & IFF_VNET_HDR)
  693. tun->flags |= TUN_VNET_HDR;
  694. else
  695. tun->flags &= ~TUN_VNET_HDR;
  696. /* Make sure persistent devices do not get stuck in
  697. * xoff state.
  698. */
  699. if (netif_running(tun->dev))
  700. netif_wake_queue(tun->dev);
  701. strcpy(ifr->ifr_name, tun->dev->name);
  702. return 0;
  703. err_free_dev:
  704. free_netdev(dev);
  705. failed:
  706. return err;
  707. }
  708. static int tun_get_iff(struct net *net, struct file *file, struct ifreq *ifr)
  709. {
  710. struct tun_struct *tun = tun_get(file);
  711. if (!tun)
  712. return -EBADFD;
  713. DBG(KERN_INFO "%s: tun_get_iff\n", tun->dev->name);
  714. strcpy(ifr->ifr_name, tun->dev->name);
  715. ifr->ifr_flags = 0;
  716. if (ifr->ifr_flags & TUN_TUN_DEV)
  717. ifr->ifr_flags |= IFF_TUN;
  718. else
  719. ifr->ifr_flags |= IFF_TAP;
  720. if (tun->flags & TUN_NO_PI)
  721. ifr->ifr_flags |= IFF_NO_PI;
  722. if (tun->flags & TUN_ONE_QUEUE)
  723. ifr->ifr_flags |= IFF_ONE_QUEUE;
  724. if (tun->flags & TUN_VNET_HDR)
  725. ifr->ifr_flags |= IFF_VNET_HDR;
  726. tun_put(tun);
  727. return 0;
  728. }
  729. /* This is like a cut-down ethtool ops, except done via tun fd so no
  730. * privs required. */
  731. static int set_offload(struct net_device *dev, unsigned long arg)
  732. {
  733. unsigned int old_features, features;
  734. old_features = dev->features;
  735. /* Unset features, set them as we chew on the arg. */
  736. features = (old_features & ~(NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST
  737. |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6));
  738. if (arg & TUN_F_CSUM) {
  739. features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  740. arg &= ~TUN_F_CSUM;
  741. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  742. if (arg & TUN_F_TSO_ECN) {
  743. features |= NETIF_F_TSO_ECN;
  744. arg &= ~TUN_F_TSO_ECN;
  745. }
  746. if (arg & TUN_F_TSO4)
  747. features |= NETIF_F_TSO;
  748. if (arg & TUN_F_TSO6)
  749. features |= NETIF_F_TSO6;
  750. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  751. }
  752. }
  753. /* This gives the user a way to test for new features in future by
  754. * trying to set them. */
  755. if (arg)
  756. return -EINVAL;
  757. dev->features = features;
  758. if (old_features != dev->features)
  759. netdev_features_change(dev);
  760. return 0;
  761. }
  762. static int tun_chr_ioctl(struct inode *inode, struct file *file,
  763. unsigned int cmd, unsigned long arg)
  764. {
  765. struct tun_file *tfile = file->private_data;
  766. struct tun_struct *tun;
  767. void __user* argp = (void __user*)arg;
  768. struct ifreq ifr;
  769. int ret;
  770. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
  771. if (copy_from_user(&ifr, argp, sizeof ifr))
  772. return -EFAULT;
  773. if (cmd == TUNGETFEATURES) {
  774. /* Currently this just means: "what IFF flags are valid?".
  775. * This is needed because we never checked for invalid flags on
  776. * TUNSETIFF. */
  777. return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
  778. IFF_VNET_HDR,
  779. (unsigned int __user*)argp);
  780. }
  781. tun = __tun_get(tfile);
  782. if (cmd == TUNSETIFF && !tun) {
  783. int err;
  784. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  785. rtnl_lock();
  786. err = tun_set_iff(tfile->net, file, &ifr);
  787. rtnl_unlock();
  788. if (err)
  789. return err;
  790. if (copy_to_user(argp, &ifr, sizeof(ifr)))
  791. return -EFAULT;
  792. return 0;
  793. }
  794. if (!tun)
  795. return -EBADFD;
  796. DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
  797. ret = 0;
  798. switch (cmd) {
  799. case TUNGETIFF:
  800. ret = tun_get_iff(current->nsproxy->net_ns, file, &ifr);
  801. if (ret)
  802. break;
  803. if (copy_to_user(argp, &ifr, sizeof(ifr)))
  804. ret = -EFAULT;
  805. break;
  806. case TUNSETNOCSUM:
  807. /* Disable/Enable checksum */
  808. if (arg)
  809. tun->flags |= TUN_NOCHECKSUM;
  810. else
  811. tun->flags &= ~TUN_NOCHECKSUM;
  812. DBG(KERN_INFO "%s: checksum %s\n",
  813. tun->dev->name, arg ? "disabled" : "enabled");
  814. break;
  815. case TUNSETPERSIST:
  816. /* Disable/Enable persist mode */
  817. if (arg)
  818. tun->flags |= TUN_PERSIST;
  819. else
  820. tun->flags &= ~TUN_PERSIST;
  821. DBG(KERN_INFO "%s: persist %s\n",
  822. tun->dev->name, arg ? "enabled" : "disabled");
  823. break;
  824. case TUNSETOWNER:
  825. /* Set owner of the device */
  826. tun->owner = (uid_t) arg;
  827. DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
  828. break;
  829. case TUNSETGROUP:
  830. /* Set group of the device */
  831. tun->group= (gid_t) arg;
  832. DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
  833. break;
  834. case TUNSETLINK:
  835. /* Only allow setting the type when the interface is down */
  836. rtnl_lock();
  837. if (tun->dev->flags & IFF_UP) {
  838. DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
  839. tun->dev->name);
  840. ret = -EBUSY;
  841. } else {
  842. tun->dev->type = (int) arg;
  843. DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
  844. ret = 0;
  845. }
  846. rtnl_unlock();
  847. break;
  848. #ifdef TUN_DEBUG
  849. case TUNSETDEBUG:
  850. tun->debug = arg;
  851. break;
  852. #endif
  853. case TUNSETOFFLOAD:
  854. rtnl_lock();
  855. ret = set_offload(tun->dev, arg);
  856. rtnl_unlock();
  857. break;
  858. case TUNSETTXFILTER:
  859. /* Can be set only for TAPs */
  860. ret = -EINVAL;
  861. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  862. break;
  863. rtnl_lock();
  864. ret = update_filter(&tun->txflt, (void __user *)arg);
  865. rtnl_unlock();
  866. break;
  867. case SIOCGIFHWADDR:
  868. /* Get hw addres */
  869. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  870. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  871. if (copy_to_user(argp, &ifr, sizeof ifr))
  872. ret = -EFAULT;
  873. break;
  874. case SIOCSIFHWADDR:
  875. /* Set hw address */
  876. DBG(KERN_DEBUG "%s: set hw address: %pM\n",
  877. tun->dev->name, ifr.ifr_hwaddr.sa_data);
  878. rtnl_lock();
  879. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  880. rtnl_unlock();
  881. break;
  882. default:
  883. ret = -EINVAL;
  884. break;
  885. };
  886. tun_put(tun);
  887. return ret;
  888. }
  889. static int tun_chr_fasync(int fd, struct file *file, int on)
  890. {
  891. struct tun_struct *tun = tun_get(file);
  892. int ret;
  893. if (!tun)
  894. return -EBADFD;
  895. DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
  896. lock_kernel();
  897. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  898. goto out;
  899. if (on) {
  900. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  901. if (ret)
  902. goto out;
  903. tun->flags |= TUN_FASYNC;
  904. } else
  905. tun->flags &= ~TUN_FASYNC;
  906. ret = 0;
  907. out:
  908. unlock_kernel();
  909. tun_put(tun);
  910. return ret;
  911. }
  912. static int tun_chr_open(struct inode *inode, struct file * file)
  913. {
  914. struct tun_file *tfile;
  915. cycle_kernel_lock();
  916. DBG1(KERN_INFO "tunX: tun_chr_open\n");
  917. tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
  918. if (!tfile)
  919. return -ENOMEM;
  920. tfile->tun = NULL;
  921. tfile->net = get_net(current->nsproxy->net_ns);
  922. init_waitqueue_head(&tfile->read_wait);
  923. file->private_data = tfile;
  924. return 0;
  925. }
  926. static int tun_chr_close(struct inode *inode, struct file *file)
  927. {
  928. struct tun_file *tfile = file->private_data;
  929. struct tun_struct *tun = __tun_get(tfile);
  930. if (tun) {
  931. DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
  932. rtnl_lock();
  933. __tun_detach(tun);
  934. /* If desireable, unregister the netdevice. */
  935. if (!(tun->flags & TUN_PERSIST))
  936. unregister_netdevice(tun->dev);
  937. rtnl_unlock();
  938. }
  939. put_net(tfile->net);
  940. kfree(tfile);
  941. return 0;
  942. }
  943. static const struct file_operations tun_fops = {
  944. .owner = THIS_MODULE,
  945. .llseek = no_llseek,
  946. .read = do_sync_read,
  947. .aio_read = tun_chr_aio_read,
  948. .write = do_sync_write,
  949. .aio_write = tun_chr_aio_write,
  950. .poll = tun_chr_poll,
  951. .ioctl = tun_chr_ioctl,
  952. .open = tun_chr_open,
  953. .release = tun_chr_close,
  954. .fasync = tun_chr_fasync
  955. };
  956. static struct miscdevice tun_miscdev = {
  957. .minor = TUN_MINOR,
  958. .name = "tun",
  959. .fops = &tun_fops,
  960. };
  961. /* ethtool interface */
  962. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  963. {
  964. cmd->supported = 0;
  965. cmd->advertising = 0;
  966. cmd->speed = SPEED_10;
  967. cmd->duplex = DUPLEX_FULL;
  968. cmd->port = PORT_TP;
  969. cmd->phy_address = 0;
  970. cmd->transceiver = XCVR_INTERNAL;
  971. cmd->autoneg = AUTONEG_DISABLE;
  972. cmd->maxtxpkt = 0;
  973. cmd->maxrxpkt = 0;
  974. return 0;
  975. }
  976. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  977. {
  978. struct tun_struct *tun = netdev_priv(dev);
  979. strcpy(info->driver, DRV_NAME);
  980. strcpy(info->version, DRV_VERSION);
  981. strcpy(info->fw_version, "N/A");
  982. switch (tun->flags & TUN_TYPE_MASK) {
  983. case TUN_TUN_DEV:
  984. strcpy(info->bus_info, "tun");
  985. break;
  986. case TUN_TAP_DEV:
  987. strcpy(info->bus_info, "tap");
  988. break;
  989. }
  990. }
  991. static u32 tun_get_msglevel(struct net_device *dev)
  992. {
  993. #ifdef TUN_DEBUG
  994. struct tun_struct *tun = netdev_priv(dev);
  995. return tun->debug;
  996. #else
  997. return -EOPNOTSUPP;
  998. #endif
  999. }
  1000. static void tun_set_msglevel(struct net_device *dev, u32 value)
  1001. {
  1002. #ifdef TUN_DEBUG
  1003. struct tun_struct *tun = netdev_priv(dev);
  1004. tun->debug = value;
  1005. #endif
  1006. }
  1007. static u32 tun_get_link(struct net_device *dev)
  1008. {
  1009. struct tun_struct *tun = netdev_priv(dev);
  1010. return !!tun->tfile;
  1011. }
  1012. static u32 tun_get_rx_csum(struct net_device *dev)
  1013. {
  1014. struct tun_struct *tun = netdev_priv(dev);
  1015. return (tun->flags & TUN_NOCHECKSUM) == 0;
  1016. }
  1017. static int tun_set_rx_csum(struct net_device *dev, u32 data)
  1018. {
  1019. struct tun_struct *tun = netdev_priv(dev);
  1020. if (data)
  1021. tun->flags &= ~TUN_NOCHECKSUM;
  1022. else
  1023. tun->flags |= TUN_NOCHECKSUM;
  1024. return 0;
  1025. }
  1026. static const struct ethtool_ops tun_ethtool_ops = {
  1027. .get_settings = tun_get_settings,
  1028. .get_drvinfo = tun_get_drvinfo,
  1029. .get_msglevel = tun_get_msglevel,
  1030. .set_msglevel = tun_set_msglevel,
  1031. .get_link = tun_get_link,
  1032. .get_rx_csum = tun_get_rx_csum,
  1033. .set_rx_csum = tun_set_rx_csum
  1034. };
  1035. static int tun_init_net(struct net *net)
  1036. {
  1037. return 0;
  1038. }
  1039. static void tun_exit_net(struct net *net)
  1040. {
  1041. struct net_device *dev, *next;
  1042. rtnl_lock();
  1043. for_each_netdev_safe(net, dev, next) {
  1044. if (dev->ethtool_ops != &tun_ethtool_ops)
  1045. continue;
  1046. DBG(KERN_INFO "%s cleaned up\n", dev->name);
  1047. unregister_netdevice(dev);
  1048. }
  1049. rtnl_unlock();
  1050. }
  1051. static struct pernet_operations tun_net_ops = {
  1052. .init = tun_init_net,
  1053. .exit = tun_exit_net,
  1054. };
  1055. static int __init tun_init(void)
  1056. {
  1057. int ret = 0;
  1058. printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1059. printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
  1060. ret = register_pernet_device(&tun_net_ops);
  1061. if (ret) {
  1062. printk(KERN_ERR "tun: Can't register pernet ops\n");
  1063. goto err_pernet;
  1064. }
  1065. ret = misc_register(&tun_miscdev);
  1066. if (ret) {
  1067. printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
  1068. goto err_misc;
  1069. }
  1070. return 0;
  1071. err_misc:
  1072. unregister_pernet_device(&tun_net_ops);
  1073. err_pernet:
  1074. return ret;
  1075. }
  1076. static void tun_cleanup(void)
  1077. {
  1078. misc_deregister(&tun_miscdev);
  1079. unregister_pernet_device(&tun_net_ops);
  1080. }
  1081. module_init(tun_init);
  1082. module_exit(tun_cleanup);
  1083. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1084. MODULE_AUTHOR(DRV_COPYRIGHT);
  1085. MODULE_LICENSE("GPL");
  1086. MODULE_ALIAS_MISCDEV(TUN_MINOR);