tun.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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. /* Initialize net device. */
  257. static void tun_net_init(struct net_device *dev)
  258. {
  259. struct tun_struct *tun = netdev_priv(dev);
  260. switch (tun->flags & TUN_TYPE_MASK) {
  261. case TUN_TUN_DEV:
  262. /* Point-to-Point TUN Device */
  263. dev->hard_header_len = 0;
  264. dev->addr_len = 0;
  265. dev->mtu = 1500;
  266. dev->change_mtu = tun_net_change_mtu;
  267. /* Zero header length */
  268. dev->type = ARPHRD_NONE;
  269. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  270. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  271. break;
  272. case TUN_TAP_DEV:
  273. /* Ethernet TAP Device */
  274. ether_setup(dev);
  275. dev->change_mtu = tun_net_change_mtu;
  276. dev->set_multicast_list = tun_net_mclist;
  277. random_ether_addr(dev->dev_addr);
  278. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  279. break;
  280. }
  281. }
  282. /* Character device part */
  283. /* Poll */
  284. static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
  285. {
  286. struct tun_struct *tun = file->private_data;
  287. unsigned int mask = POLLOUT | POLLWRNORM;
  288. if (!tun)
  289. return -EBADFD;
  290. DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
  291. poll_wait(file, &tun->read_wait, wait);
  292. if (!skb_queue_empty(&tun->readq))
  293. mask |= POLLIN | POLLRDNORM;
  294. return mask;
  295. }
  296. /* Get packet from user space buffer */
  297. static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
  298. {
  299. struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
  300. struct sk_buff *skb;
  301. size_t len = count, align = 0;
  302. struct virtio_net_hdr gso = { 0 };
  303. if (!(tun->flags & TUN_NO_PI)) {
  304. if ((len -= sizeof(pi)) > count)
  305. return -EINVAL;
  306. if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
  307. return -EFAULT;
  308. }
  309. if (tun->flags & TUN_VNET_HDR) {
  310. if ((len -= sizeof(gso)) > count)
  311. return -EINVAL;
  312. if (memcpy_fromiovec((void *)&gso, iv, sizeof(gso)))
  313. return -EFAULT;
  314. if (gso.hdr_len > len)
  315. return -EINVAL;
  316. }
  317. if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
  318. align = NET_IP_ALIGN;
  319. if (unlikely(len < ETH_HLEN))
  320. return -EINVAL;
  321. }
  322. if (!(skb = alloc_skb(len + align, GFP_KERNEL))) {
  323. tun->dev->stats.rx_dropped++;
  324. return -ENOMEM;
  325. }
  326. if (align)
  327. skb_reserve(skb, align);
  328. if (memcpy_fromiovec(skb_put(skb, len), iv, len)) {
  329. tun->dev->stats.rx_dropped++;
  330. kfree_skb(skb);
  331. return -EFAULT;
  332. }
  333. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  334. if (!skb_partial_csum_set(skb, gso.csum_start,
  335. gso.csum_offset)) {
  336. tun->dev->stats.rx_frame_errors++;
  337. kfree_skb(skb);
  338. return -EINVAL;
  339. }
  340. } else if (tun->flags & TUN_NOCHECKSUM)
  341. skb->ip_summed = CHECKSUM_UNNECESSARY;
  342. switch (tun->flags & TUN_TYPE_MASK) {
  343. case TUN_TUN_DEV:
  344. if (tun->flags & TUN_NO_PI) {
  345. switch (skb->data[0] & 0xf0) {
  346. case 0x40:
  347. pi.proto = htons(ETH_P_IP);
  348. break;
  349. case 0x60:
  350. pi.proto = htons(ETH_P_IPV6);
  351. break;
  352. default:
  353. tun->dev->stats.rx_dropped++;
  354. kfree_skb(skb);
  355. return -EINVAL;
  356. }
  357. }
  358. skb_reset_mac_header(skb);
  359. skb->protocol = pi.proto;
  360. skb->dev = tun->dev;
  361. break;
  362. case TUN_TAP_DEV:
  363. skb->protocol = eth_type_trans(skb, tun->dev);
  364. break;
  365. };
  366. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  367. pr_debug("GSO!\n");
  368. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  369. case VIRTIO_NET_HDR_GSO_TCPV4:
  370. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  371. break;
  372. case VIRTIO_NET_HDR_GSO_TCPV6:
  373. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  374. break;
  375. default:
  376. tun->dev->stats.rx_frame_errors++;
  377. kfree_skb(skb);
  378. return -EINVAL;
  379. }
  380. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  381. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  382. skb_shinfo(skb)->gso_size = gso.gso_size;
  383. if (skb_shinfo(skb)->gso_size == 0) {
  384. tun->dev->stats.rx_frame_errors++;
  385. kfree_skb(skb);
  386. return -EINVAL;
  387. }
  388. /* Header must be checked, and gso_segs computed. */
  389. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  390. skb_shinfo(skb)->gso_segs = 0;
  391. }
  392. netif_rx_ni(skb);
  393. tun->dev->last_rx = jiffies;
  394. tun->dev->stats.rx_packets++;
  395. tun->dev->stats.rx_bytes += len;
  396. return count;
  397. }
  398. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  399. unsigned long count, loff_t pos)
  400. {
  401. struct tun_struct *tun = iocb->ki_filp->private_data;
  402. if (!tun)
  403. return -EBADFD;
  404. DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
  405. return tun_get_user(tun, (struct iovec *) iv, iov_length(iv, count));
  406. }
  407. /* Put packet to the user space buffer */
  408. static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
  409. struct sk_buff *skb,
  410. struct iovec *iv, int len)
  411. {
  412. struct tun_pi pi = { 0, skb->protocol };
  413. ssize_t total = 0;
  414. if (!(tun->flags & TUN_NO_PI)) {
  415. if ((len -= sizeof(pi)) < 0)
  416. return -EINVAL;
  417. if (len < skb->len) {
  418. /* Packet will be striped */
  419. pi.flags |= TUN_PKT_STRIP;
  420. }
  421. if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
  422. return -EFAULT;
  423. total += sizeof(pi);
  424. }
  425. if (tun->flags & TUN_VNET_HDR) {
  426. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  427. if ((len -= sizeof(gso)) < 0)
  428. return -EINVAL;
  429. if (skb_is_gso(skb)) {
  430. struct skb_shared_info *sinfo = skb_shinfo(skb);
  431. /* This is a hint as to how much should be linear. */
  432. gso.hdr_len = skb_headlen(skb);
  433. gso.gso_size = sinfo->gso_size;
  434. if (sinfo->gso_type & SKB_GSO_TCPV4)
  435. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  436. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  437. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  438. else
  439. BUG();
  440. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  441. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  442. } else
  443. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  444. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  445. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  446. gso.csum_start = skb->csum_start - skb_headroom(skb);
  447. gso.csum_offset = skb->csum_offset;
  448. } /* else everything is zero */
  449. if (unlikely(memcpy_toiovec(iv, (void *)&gso, sizeof(gso))))
  450. return -EFAULT;
  451. total += sizeof(gso);
  452. }
  453. len = min_t(int, skb->len, len);
  454. skb_copy_datagram_iovec(skb, 0, iv, len);
  455. total += len;
  456. tun->dev->stats.tx_packets++;
  457. tun->dev->stats.tx_bytes += len;
  458. return total;
  459. }
  460. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  461. unsigned long count, loff_t pos)
  462. {
  463. struct file *file = iocb->ki_filp;
  464. struct tun_struct *tun = file->private_data;
  465. DECLARE_WAITQUEUE(wait, current);
  466. struct sk_buff *skb;
  467. ssize_t len, ret = 0;
  468. if (!tun)
  469. return -EBADFD;
  470. DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
  471. len = iov_length(iv, count);
  472. if (len < 0)
  473. return -EINVAL;
  474. add_wait_queue(&tun->read_wait, &wait);
  475. while (len) {
  476. current->state = TASK_INTERRUPTIBLE;
  477. /* Read frames from the queue */
  478. if (!(skb=skb_dequeue(&tun->readq))) {
  479. if (file->f_flags & O_NONBLOCK) {
  480. ret = -EAGAIN;
  481. break;
  482. }
  483. if (signal_pending(current)) {
  484. ret = -ERESTARTSYS;
  485. break;
  486. }
  487. /* Nothing to read, let's sleep */
  488. schedule();
  489. continue;
  490. }
  491. netif_wake_queue(tun->dev);
  492. ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
  493. kfree_skb(skb);
  494. break;
  495. }
  496. current->state = TASK_RUNNING;
  497. remove_wait_queue(&tun->read_wait, &wait);
  498. return ret;
  499. }
  500. static void tun_setup(struct net_device *dev)
  501. {
  502. struct tun_struct *tun = netdev_priv(dev);
  503. skb_queue_head_init(&tun->readq);
  504. init_waitqueue_head(&tun->read_wait);
  505. tun->owner = -1;
  506. tun->group = -1;
  507. dev->open = tun_net_open;
  508. dev->hard_start_xmit = tun_net_xmit;
  509. dev->stop = tun_net_close;
  510. dev->ethtool_ops = &tun_ethtool_ops;
  511. dev->destructor = free_netdev;
  512. dev->features |= NETIF_F_NETNS_LOCAL;
  513. }
  514. static struct tun_struct *tun_get_by_name(struct tun_net *tn, const char *name)
  515. {
  516. struct tun_struct *tun;
  517. ASSERT_RTNL();
  518. list_for_each_entry(tun, &tn->dev_list, list) {
  519. if (!strncmp(tun->dev->name, name, IFNAMSIZ))
  520. return tun;
  521. }
  522. return NULL;
  523. }
  524. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  525. {
  526. struct tun_net *tn;
  527. struct tun_struct *tun;
  528. struct net_device *dev;
  529. int err;
  530. tn = net_generic(net, tun_net_id);
  531. tun = tun_get_by_name(tn, ifr->ifr_name);
  532. if (tun) {
  533. if (tun->attached)
  534. return -EBUSY;
  535. /* Check permissions */
  536. if (((tun->owner != -1 &&
  537. current->euid != tun->owner) ||
  538. (tun->group != -1 &&
  539. current->egid != tun->group)) &&
  540. !capable(CAP_NET_ADMIN))
  541. return -EPERM;
  542. }
  543. else if (__dev_get_by_name(net, ifr->ifr_name))
  544. return -EINVAL;
  545. else {
  546. char *name;
  547. unsigned long flags = 0;
  548. err = -EINVAL;
  549. if (!capable(CAP_NET_ADMIN))
  550. return -EPERM;
  551. /* Set dev type */
  552. if (ifr->ifr_flags & IFF_TUN) {
  553. /* TUN device */
  554. flags |= TUN_TUN_DEV;
  555. name = "tun%d";
  556. } else if (ifr->ifr_flags & IFF_TAP) {
  557. /* TAP device */
  558. flags |= TUN_TAP_DEV;
  559. name = "tap%d";
  560. } else
  561. goto failed;
  562. if (*ifr->ifr_name)
  563. name = ifr->ifr_name;
  564. dev = alloc_netdev(sizeof(struct tun_struct), name,
  565. tun_setup);
  566. if (!dev)
  567. return -ENOMEM;
  568. dev_net_set(dev, net);
  569. tun = netdev_priv(dev);
  570. tun->dev = dev;
  571. tun->flags = flags;
  572. tun->txflt.count = 0;
  573. tun_net_init(dev);
  574. if (strchr(dev->name, '%')) {
  575. err = dev_alloc_name(dev, dev->name);
  576. if (err < 0)
  577. goto err_free_dev;
  578. }
  579. err = register_netdevice(tun->dev);
  580. if (err < 0)
  581. goto err_free_dev;
  582. list_add(&tun->list, &tn->dev_list);
  583. }
  584. DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
  585. if (ifr->ifr_flags & IFF_NO_PI)
  586. tun->flags |= TUN_NO_PI;
  587. else
  588. tun->flags &= ~TUN_NO_PI;
  589. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  590. tun->flags |= TUN_ONE_QUEUE;
  591. else
  592. tun->flags &= ~TUN_ONE_QUEUE;
  593. if (ifr->ifr_flags & IFF_VNET_HDR)
  594. tun->flags |= TUN_VNET_HDR;
  595. else
  596. tun->flags &= ~TUN_VNET_HDR;
  597. file->private_data = tun;
  598. tun->attached = 1;
  599. get_net(dev_net(tun->dev));
  600. /* Make sure persistent devices do not get stuck in
  601. * xoff state.
  602. */
  603. if (netif_running(tun->dev))
  604. netif_wake_queue(tun->dev);
  605. strcpy(ifr->ifr_name, tun->dev->name);
  606. return 0;
  607. err_free_dev:
  608. free_netdev(dev);
  609. failed:
  610. return err;
  611. }
  612. static int tun_get_iff(struct net *net, struct file *file, struct ifreq *ifr)
  613. {
  614. struct tun_struct *tun = file->private_data;
  615. if (!tun)
  616. return -EBADFD;
  617. DBG(KERN_INFO "%s: tun_get_iff\n", tun->dev->name);
  618. strcpy(ifr->ifr_name, tun->dev->name);
  619. ifr->ifr_flags = 0;
  620. if (ifr->ifr_flags & TUN_TUN_DEV)
  621. ifr->ifr_flags |= IFF_TUN;
  622. else
  623. ifr->ifr_flags |= IFF_TAP;
  624. if (tun->flags & TUN_NO_PI)
  625. ifr->ifr_flags |= IFF_NO_PI;
  626. if (tun->flags & TUN_ONE_QUEUE)
  627. ifr->ifr_flags |= IFF_ONE_QUEUE;
  628. if (tun->flags & TUN_VNET_HDR)
  629. ifr->ifr_flags |= IFF_VNET_HDR;
  630. return 0;
  631. }
  632. /* This is like a cut-down ethtool ops, except done via tun fd so no
  633. * privs required. */
  634. static int set_offload(struct net_device *dev, unsigned long arg)
  635. {
  636. unsigned int old_features, features;
  637. old_features = dev->features;
  638. /* Unset features, set them as we chew on the arg. */
  639. features = (old_features & ~(NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST
  640. |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6));
  641. if (arg & TUN_F_CSUM) {
  642. features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  643. arg &= ~TUN_F_CSUM;
  644. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  645. if (arg & TUN_F_TSO_ECN) {
  646. features |= NETIF_F_TSO_ECN;
  647. arg &= ~TUN_F_TSO_ECN;
  648. }
  649. if (arg & TUN_F_TSO4)
  650. features |= NETIF_F_TSO;
  651. if (arg & TUN_F_TSO6)
  652. features |= NETIF_F_TSO6;
  653. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  654. }
  655. }
  656. /* This gives the user a way to test for new features in future by
  657. * trying to set them. */
  658. if (arg)
  659. return -EINVAL;
  660. dev->features = features;
  661. if (old_features != dev->features)
  662. netdev_features_change(dev);
  663. return 0;
  664. }
  665. static int tun_chr_ioctl(struct inode *inode, struct file *file,
  666. unsigned int cmd, unsigned long arg)
  667. {
  668. struct tun_struct *tun = file->private_data;
  669. void __user* argp = (void __user*)arg;
  670. struct ifreq ifr;
  671. int ret;
  672. DECLARE_MAC_BUF(mac);
  673. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
  674. if (copy_from_user(&ifr, argp, sizeof ifr))
  675. return -EFAULT;
  676. if (cmd == TUNSETIFF && !tun) {
  677. int err;
  678. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  679. rtnl_lock();
  680. err = tun_set_iff(current->nsproxy->net_ns, file, &ifr);
  681. rtnl_unlock();
  682. if (err)
  683. return err;
  684. if (copy_to_user(argp, &ifr, sizeof(ifr)))
  685. return -EFAULT;
  686. return 0;
  687. }
  688. if (cmd == TUNGETFEATURES) {
  689. /* Currently this just means: "what IFF flags are valid?".
  690. * This is needed because we never checked for invalid flags on
  691. * TUNSETIFF. */
  692. return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
  693. IFF_VNET_HDR,
  694. (unsigned int __user*)argp);
  695. }
  696. if (!tun)
  697. return -EBADFD;
  698. DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
  699. switch (cmd) {
  700. case TUNGETIFF:
  701. ret = tun_get_iff(current->nsproxy->net_ns, file, &ifr);
  702. if (ret)
  703. return ret;
  704. if (copy_to_user(argp, &ifr, sizeof(ifr)))
  705. return -EFAULT;
  706. break;
  707. case TUNSETNOCSUM:
  708. /* Disable/Enable checksum */
  709. if (arg)
  710. tun->flags |= TUN_NOCHECKSUM;
  711. else
  712. tun->flags &= ~TUN_NOCHECKSUM;
  713. DBG(KERN_INFO "%s: checksum %s\n",
  714. tun->dev->name, arg ? "disabled" : "enabled");
  715. break;
  716. case TUNSETPERSIST:
  717. /* Disable/Enable persist mode */
  718. if (arg)
  719. tun->flags |= TUN_PERSIST;
  720. else
  721. tun->flags &= ~TUN_PERSIST;
  722. DBG(KERN_INFO "%s: persist %s\n",
  723. tun->dev->name, arg ? "enabled" : "disabled");
  724. break;
  725. case TUNSETOWNER:
  726. /* Set owner of the device */
  727. tun->owner = (uid_t) arg;
  728. DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
  729. break;
  730. case TUNSETGROUP:
  731. /* Set group of the device */
  732. tun->group= (gid_t) arg;
  733. DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
  734. break;
  735. case TUNSETLINK:
  736. /* Only allow setting the type when the interface is down */
  737. rtnl_lock();
  738. if (tun->dev->flags & IFF_UP) {
  739. DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
  740. tun->dev->name);
  741. ret = -EBUSY;
  742. } else {
  743. tun->dev->type = (int) arg;
  744. DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
  745. ret = 0;
  746. }
  747. rtnl_unlock();
  748. return ret;
  749. #ifdef TUN_DEBUG
  750. case TUNSETDEBUG:
  751. tun->debug = arg;
  752. break;
  753. #endif
  754. case TUNSETOFFLOAD:
  755. rtnl_lock();
  756. ret = set_offload(tun->dev, arg);
  757. rtnl_unlock();
  758. return ret;
  759. case TUNSETTXFILTER:
  760. /* Can be set only for TAPs */
  761. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  762. return -EINVAL;
  763. rtnl_lock();
  764. ret = update_filter(&tun->txflt, (void __user *)arg);
  765. rtnl_unlock();
  766. return ret;
  767. case SIOCGIFHWADDR:
  768. /* Get hw addres */
  769. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  770. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  771. if (copy_to_user(argp, &ifr, sizeof ifr))
  772. return -EFAULT;
  773. return 0;
  774. case SIOCSIFHWADDR:
  775. /* Set hw address */
  776. DBG(KERN_DEBUG "%s: set hw address: %s\n",
  777. tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
  778. rtnl_lock();
  779. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  780. rtnl_unlock();
  781. return ret;
  782. default:
  783. return -EINVAL;
  784. };
  785. return 0;
  786. }
  787. static int tun_chr_fasync(int fd, struct file *file, int on)
  788. {
  789. struct tun_struct *tun = file->private_data;
  790. int ret;
  791. if (!tun)
  792. return -EBADFD;
  793. DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
  794. lock_kernel();
  795. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  796. goto out;
  797. if (on) {
  798. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  799. if (ret)
  800. goto out;
  801. tun->flags |= TUN_FASYNC;
  802. } else
  803. tun->flags &= ~TUN_FASYNC;
  804. ret = 0;
  805. out:
  806. unlock_kernel();
  807. return ret;
  808. }
  809. static int tun_chr_open(struct inode *inode, struct file * file)
  810. {
  811. cycle_kernel_lock();
  812. DBG1(KERN_INFO "tunX: tun_chr_open\n");
  813. file->private_data = NULL;
  814. return 0;
  815. }
  816. static int tun_chr_close(struct inode *inode, struct file *file)
  817. {
  818. struct tun_struct *tun = file->private_data;
  819. if (!tun)
  820. return 0;
  821. DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
  822. tun_chr_fasync(-1, file, 0);
  823. rtnl_lock();
  824. /* Detach from net device */
  825. file->private_data = NULL;
  826. tun->attached = 0;
  827. put_net(dev_net(tun->dev));
  828. /* Drop read queue */
  829. skb_queue_purge(&tun->readq);
  830. if (!(tun->flags & TUN_PERSIST)) {
  831. list_del(&tun->list);
  832. unregister_netdevice(tun->dev);
  833. }
  834. rtnl_unlock();
  835. return 0;
  836. }
  837. static const struct file_operations tun_fops = {
  838. .owner = THIS_MODULE,
  839. .llseek = no_llseek,
  840. .read = do_sync_read,
  841. .aio_read = tun_chr_aio_read,
  842. .write = do_sync_write,
  843. .aio_write = tun_chr_aio_write,
  844. .poll = tun_chr_poll,
  845. .ioctl = tun_chr_ioctl,
  846. .open = tun_chr_open,
  847. .release = tun_chr_close,
  848. .fasync = tun_chr_fasync
  849. };
  850. static struct miscdevice tun_miscdev = {
  851. .minor = TUN_MINOR,
  852. .name = "tun",
  853. .fops = &tun_fops,
  854. };
  855. /* ethtool interface */
  856. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  857. {
  858. cmd->supported = 0;
  859. cmd->advertising = 0;
  860. cmd->speed = SPEED_10;
  861. cmd->duplex = DUPLEX_FULL;
  862. cmd->port = PORT_TP;
  863. cmd->phy_address = 0;
  864. cmd->transceiver = XCVR_INTERNAL;
  865. cmd->autoneg = AUTONEG_DISABLE;
  866. cmd->maxtxpkt = 0;
  867. cmd->maxrxpkt = 0;
  868. return 0;
  869. }
  870. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  871. {
  872. struct tun_struct *tun = netdev_priv(dev);
  873. strcpy(info->driver, DRV_NAME);
  874. strcpy(info->version, DRV_VERSION);
  875. strcpy(info->fw_version, "N/A");
  876. switch (tun->flags & TUN_TYPE_MASK) {
  877. case TUN_TUN_DEV:
  878. strcpy(info->bus_info, "tun");
  879. break;
  880. case TUN_TAP_DEV:
  881. strcpy(info->bus_info, "tap");
  882. break;
  883. }
  884. }
  885. static u32 tun_get_msglevel(struct net_device *dev)
  886. {
  887. #ifdef TUN_DEBUG
  888. struct tun_struct *tun = netdev_priv(dev);
  889. return tun->debug;
  890. #else
  891. return -EOPNOTSUPP;
  892. #endif
  893. }
  894. static void tun_set_msglevel(struct net_device *dev, u32 value)
  895. {
  896. #ifdef TUN_DEBUG
  897. struct tun_struct *tun = netdev_priv(dev);
  898. tun->debug = value;
  899. #endif
  900. }
  901. static u32 tun_get_link(struct net_device *dev)
  902. {
  903. struct tun_struct *tun = netdev_priv(dev);
  904. return tun->attached;
  905. }
  906. static u32 tun_get_rx_csum(struct net_device *dev)
  907. {
  908. struct tun_struct *tun = netdev_priv(dev);
  909. return (tun->flags & TUN_NOCHECKSUM) == 0;
  910. }
  911. static int tun_set_rx_csum(struct net_device *dev, u32 data)
  912. {
  913. struct tun_struct *tun = netdev_priv(dev);
  914. if (data)
  915. tun->flags &= ~TUN_NOCHECKSUM;
  916. else
  917. tun->flags |= TUN_NOCHECKSUM;
  918. return 0;
  919. }
  920. static const struct ethtool_ops tun_ethtool_ops = {
  921. .get_settings = tun_get_settings,
  922. .get_drvinfo = tun_get_drvinfo,
  923. .get_msglevel = tun_get_msglevel,
  924. .set_msglevel = tun_set_msglevel,
  925. .get_link = tun_get_link,
  926. .get_rx_csum = tun_get_rx_csum,
  927. .set_rx_csum = tun_set_rx_csum
  928. };
  929. static int tun_init_net(struct net *net)
  930. {
  931. struct tun_net *tn;
  932. tn = kmalloc(sizeof(*tn), GFP_KERNEL);
  933. if (tn == NULL)
  934. return -ENOMEM;
  935. INIT_LIST_HEAD(&tn->dev_list);
  936. if (net_assign_generic(net, tun_net_id, tn)) {
  937. kfree(tn);
  938. return -ENOMEM;
  939. }
  940. return 0;
  941. }
  942. static void tun_exit_net(struct net *net)
  943. {
  944. struct tun_net *tn;
  945. struct tun_struct *tun, *nxt;
  946. tn = net_generic(net, tun_net_id);
  947. rtnl_lock();
  948. list_for_each_entry_safe(tun, nxt, &tn->dev_list, list) {
  949. DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
  950. unregister_netdevice(tun->dev);
  951. }
  952. rtnl_unlock();
  953. kfree(tn);
  954. }
  955. static struct pernet_operations tun_net_ops = {
  956. .init = tun_init_net,
  957. .exit = tun_exit_net,
  958. };
  959. static int __init tun_init(void)
  960. {
  961. int ret = 0;
  962. printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  963. printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
  964. ret = register_pernet_gen_device(&tun_net_id, &tun_net_ops);
  965. if (ret) {
  966. printk(KERN_ERR "tun: Can't register pernet ops\n");
  967. goto err_pernet;
  968. }
  969. ret = misc_register(&tun_miscdev);
  970. if (ret) {
  971. printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
  972. goto err_misc;
  973. }
  974. return 0;
  975. err_misc:
  976. unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
  977. err_pernet:
  978. return ret;
  979. }
  980. static void tun_cleanup(void)
  981. {
  982. misc_deregister(&tun_miscdev);
  983. unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
  984. }
  985. module_init(tun_init);
  986. module_exit(tun_cleanup);
  987. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  988. MODULE_AUTHOR(DRV_COPYRIGHT);
  989. MODULE_LICENSE("GPL");
  990. MODULE_ALIAS_MISCDEV(TUN_MINOR);