tun.c 28 KB

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