tun.c 28 KB

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