tun.c 28 KB

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