tun.c 28 KB

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