tun.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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. /* This is like a cut-down ethtool ops, except done via tun fd so no
  613. * privs required. */
  614. static int set_offload(struct net_device *dev, unsigned long arg)
  615. {
  616. unsigned int old_features, features;
  617. old_features = dev->features;
  618. /* Unset features, set them as we chew on the arg. */
  619. features = (old_features & ~(NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST
  620. |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6));
  621. if (arg & TUN_F_CSUM) {
  622. features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  623. arg &= ~TUN_F_CSUM;
  624. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  625. if (arg & TUN_F_TSO_ECN) {
  626. features |= NETIF_F_TSO_ECN;
  627. arg &= ~TUN_F_TSO_ECN;
  628. }
  629. if (arg & TUN_F_TSO4)
  630. features |= NETIF_F_TSO;
  631. if (arg & TUN_F_TSO6)
  632. features |= NETIF_F_TSO6;
  633. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  634. }
  635. }
  636. /* This gives the user a way to test for new features in future by
  637. * trying to set them. */
  638. if (arg)
  639. return -EINVAL;
  640. dev->features = features;
  641. if (old_features != dev->features)
  642. netdev_features_change(dev);
  643. return 0;
  644. }
  645. static int tun_chr_ioctl(struct inode *inode, struct file *file,
  646. unsigned int cmd, unsigned long arg)
  647. {
  648. struct tun_struct *tun = file->private_data;
  649. void __user* argp = (void __user*)arg;
  650. struct ifreq ifr;
  651. int ret;
  652. DECLARE_MAC_BUF(mac);
  653. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
  654. if (copy_from_user(&ifr, argp, sizeof ifr))
  655. return -EFAULT;
  656. if (cmd == TUNSETIFF && !tun) {
  657. int err;
  658. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  659. rtnl_lock();
  660. err = tun_set_iff(current->nsproxy->net_ns, file, &ifr);
  661. rtnl_unlock();
  662. if (err)
  663. return err;
  664. if (copy_to_user(argp, &ifr, sizeof(ifr)))
  665. return -EFAULT;
  666. return 0;
  667. }
  668. if (cmd == TUNGETFEATURES) {
  669. /* Currently this just means: "what IFF flags are valid?".
  670. * This is needed because we never checked for invalid flags on
  671. * TUNSETIFF. */
  672. return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
  673. IFF_VNET_HDR,
  674. (unsigned int __user*)argp);
  675. }
  676. if (!tun)
  677. return -EBADFD;
  678. DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
  679. switch (cmd) {
  680. case TUNSETNOCSUM:
  681. /* Disable/Enable checksum */
  682. if (arg)
  683. tun->flags |= TUN_NOCHECKSUM;
  684. else
  685. tun->flags &= ~TUN_NOCHECKSUM;
  686. DBG(KERN_INFO "%s: checksum %s\n",
  687. tun->dev->name, arg ? "disabled" : "enabled");
  688. break;
  689. case TUNSETPERSIST:
  690. /* Disable/Enable persist mode */
  691. if (arg)
  692. tun->flags |= TUN_PERSIST;
  693. else
  694. tun->flags &= ~TUN_PERSIST;
  695. DBG(KERN_INFO "%s: persist %s\n",
  696. tun->dev->name, arg ? "enabled" : "disabled");
  697. break;
  698. case TUNSETOWNER:
  699. /* Set owner of the device */
  700. tun->owner = (uid_t) arg;
  701. DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
  702. break;
  703. case TUNSETGROUP:
  704. /* Set group of the device */
  705. tun->group= (gid_t) arg;
  706. DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
  707. break;
  708. case TUNSETLINK:
  709. /* Only allow setting the type when the interface is down */
  710. rtnl_lock();
  711. if (tun->dev->flags & IFF_UP) {
  712. DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
  713. tun->dev->name);
  714. ret = -EBUSY;
  715. } else {
  716. tun->dev->type = (int) arg;
  717. DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
  718. ret = 0;
  719. }
  720. rtnl_unlock();
  721. return ret;
  722. #ifdef TUN_DEBUG
  723. case TUNSETDEBUG:
  724. tun->debug = arg;
  725. break;
  726. #endif
  727. case TUNSETOFFLOAD:
  728. rtnl_lock();
  729. ret = set_offload(tun->dev, arg);
  730. rtnl_unlock();
  731. return ret;
  732. case TUNSETTXFILTER:
  733. /* Can be set only for TAPs */
  734. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  735. return -EINVAL;
  736. rtnl_lock();
  737. ret = update_filter(&tun->txflt, (void __user *)arg);
  738. rtnl_unlock();
  739. return ret;
  740. case SIOCGIFHWADDR:
  741. /* Get hw addres */
  742. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  743. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  744. if (copy_to_user(argp, &ifr, sizeof ifr))
  745. return -EFAULT;
  746. return 0;
  747. case SIOCSIFHWADDR:
  748. /* Set hw address */
  749. DBG(KERN_DEBUG "%s: set hw address: %s\n",
  750. tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
  751. rtnl_lock();
  752. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  753. rtnl_unlock();
  754. return ret;
  755. default:
  756. return -EINVAL;
  757. };
  758. return 0;
  759. }
  760. static int tun_chr_fasync(int fd, struct file *file, int on)
  761. {
  762. struct tun_struct *tun = file->private_data;
  763. int ret;
  764. if (!tun)
  765. return -EBADFD;
  766. DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
  767. lock_kernel();
  768. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  769. goto out;
  770. if (on) {
  771. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  772. if (ret)
  773. goto out;
  774. tun->flags |= TUN_FASYNC;
  775. } else
  776. tun->flags &= ~TUN_FASYNC;
  777. ret = 0;
  778. out:
  779. unlock_kernel();
  780. return ret;
  781. }
  782. static int tun_chr_open(struct inode *inode, struct file * file)
  783. {
  784. cycle_kernel_lock();
  785. DBG1(KERN_INFO "tunX: tun_chr_open\n");
  786. file->private_data = NULL;
  787. return 0;
  788. }
  789. static int tun_chr_close(struct inode *inode, struct file *file)
  790. {
  791. struct tun_struct *tun = file->private_data;
  792. if (!tun)
  793. return 0;
  794. DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
  795. tun_chr_fasync(-1, file, 0);
  796. rtnl_lock();
  797. /* Detach from net device */
  798. file->private_data = NULL;
  799. tun->attached = 0;
  800. put_net(dev_net(tun->dev));
  801. /* Drop read queue */
  802. skb_queue_purge(&tun->readq);
  803. if (!(tun->flags & TUN_PERSIST)) {
  804. list_del(&tun->list);
  805. unregister_netdevice(tun->dev);
  806. }
  807. rtnl_unlock();
  808. return 0;
  809. }
  810. static const struct file_operations tun_fops = {
  811. .owner = THIS_MODULE,
  812. .llseek = no_llseek,
  813. .read = do_sync_read,
  814. .aio_read = tun_chr_aio_read,
  815. .write = do_sync_write,
  816. .aio_write = tun_chr_aio_write,
  817. .poll = tun_chr_poll,
  818. .ioctl = tun_chr_ioctl,
  819. .open = tun_chr_open,
  820. .release = tun_chr_close,
  821. .fasync = tun_chr_fasync
  822. };
  823. static struct miscdevice tun_miscdev = {
  824. .minor = TUN_MINOR,
  825. .name = "tun",
  826. .fops = &tun_fops,
  827. };
  828. /* ethtool interface */
  829. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  830. {
  831. cmd->supported = 0;
  832. cmd->advertising = 0;
  833. cmd->speed = SPEED_10;
  834. cmd->duplex = DUPLEX_FULL;
  835. cmd->port = PORT_TP;
  836. cmd->phy_address = 0;
  837. cmd->transceiver = XCVR_INTERNAL;
  838. cmd->autoneg = AUTONEG_DISABLE;
  839. cmd->maxtxpkt = 0;
  840. cmd->maxrxpkt = 0;
  841. return 0;
  842. }
  843. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  844. {
  845. struct tun_struct *tun = netdev_priv(dev);
  846. strcpy(info->driver, DRV_NAME);
  847. strcpy(info->version, DRV_VERSION);
  848. strcpy(info->fw_version, "N/A");
  849. switch (tun->flags & TUN_TYPE_MASK) {
  850. case TUN_TUN_DEV:
  851. strcpy(info->bus_info, "tun");
  852. break;
  853. case TUN_TAP_DEV:
  854. strcpy(info->bus_info, "tap");
  855. break;
  856. }
  857. }
  858. static u32 tun_get_msglevel(struct net_device *dev)
  859. {
  860. #ifdef TUN_DEBUG
  861. struct tun_struct *tun = netdev_priv(dev);
  862. return tun->debug;
  863. #else
  864. return -EOPNOTSUPP;
  865. #endif
  866. }
  867. static void tun_set_msglevel(struct net_device *dev, u32 value)
  868. {
  869. #ifdef TUN_DEBUG
  870. struct tun_struct *tun = netdev_priv(dev);
  871. tun->debug = value;
  872. #endif
  873. }
  874. static u32 tun_get_link(struct net_device *dev)
  875. {
  876. struct tun_struct *tun = netdev_priv(dev);
  877. return tun->attached;
  878. }
  879. static u32 tun_get_rx_csum(struct net_device *dev)
  880. {
  881. struct tun_struct *tun = netdev_priv(dev);
  882. return (tun->flags & TUN_NOCHECKSUM) == 0;
  883. }
  884. static int tun_set_rx_csum(struct net_device *dev, u32 data)
  885. {
  886. struct tun_struct *tun = netdev_priv(dev);
  887. if (data)
  888. tun->flags &= ~TUN_NOCHECKSUM;
  889. else
  890. tun->flags |= TUN_NOCHECKSUM;
  891. return 0;
  892. }
  893. static const struct ethtool_ops tun_ethtool_ops = {
  894. .get_settings = tun_get_settings,
  895. .get_drvinfo = tun_get_drvinfo,
  896. .get_msglevel = tun_get_msglevel,
  897. .set_msglevel = tun_set_msglevel,
  898. .get_link = tun_get_link,
  899. .get_rx_csum = tun_get_rx_csum,
  900. .set_rx_csum = tun_set_rx_csum
  901. };
  902. static int tun_init_net(struct net *net)
  903. {
  904. struct tun_net *tn;
  905. tn = kmalloc(sizeof(*tn), GFP_KERNEL);
  906. if (tn == NULL)
  907. return -ENOMEM;
  908. INIT_LIST_HEAD(&tn->dev_list);
  909. if (net_assign_generic(net, tun_net_id, tn)) {
  910. kfree(tn);
  911. return -ENOMEM;
  912. }
  913. return 0;
  914. }
  915. static void tun_exit_net(struct net *net)
  916. {
  917. struct tun_net *tn;
  918. struct tun_struct *tun, *nxt;
  919. tn = net_generic(net, tun_net_id);
  920. rtnl_lock();
  921. list_for_each_entry_safe(tun, nxt, &tn->dev_list, list) {
  922. DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
  923. unregister_netdevice(tun->dev);
  924. }
  925. rtnl_unlock();
  926. kfree(tn);
  927. }
  928. static struct pernet_operations tun_net_ops = {
  929. .init = tun_init_net,
  930. .exit = tun_exit_net,
  931. };
  932. static int __init tun_init(void)
  933. {
  934. int ret = 0;
  935. printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  936. printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
  937. ret = register_pernet_gen_device(&tun_net_id, &tun_net_ops);
  938. if (ret) {
  939. printk(KERN_ERR "tun: Can't register pernet ops\n");
  940. goto err_pernet;
  941. }
  942. ret = misc_register(&tun_miscdev);
  943. if (ret) {
  944. printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
  945. goto err_misc;
  946. }
  947. return 0;
  948. err_misc:
  949. unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
  950. err_pernet:
  951. return ret;
  952. }
  953. static void tun_cleanup(void)
  954. {
  955. misc_deregister(&tun_miscdev);
  956. unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
  957. }
  958. module_init(tun_init);
  959. module_exit(tun_cleanup);
  960. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  961. MODULE_AUTHOR(DRV_COPYRIGHT);
  962. MODULE_LICENSE("GPL");
  963. MODULE_ALIAS_MISCDEV(TUN_MINOR);