tun.c 28 KB

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