tun.c 28 KB

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