tun.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  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. tun_chr_fasync(-1, file, 0);
  868. rtnl_lock();
  869. /* Detach from net device */
  870. file->private_data = NULL;
  871. tun->attached = 0;
  872. put_net(dev_net(tun->dev));
  873. /* Drop read queue */
  874. skb_queue_purge(&tun->readq);
  875. if (!(tun->flags & TUN_PERSIST)) {
  876. list_del(&tun->list);
  877. unregister_netdevice(tun->dev);
  878. }
  879. rtnl_unlock();
  880. return 0;
  881. }
  882. static const struct file_operations tun_fops = {
  883. .owner = THIS_MODULE,
  884. .llseek = no_llseek,
  885. .read = do_sync_read,
  886. .aio_read = tun_chr_aio_read,
  887. .write = do_sync_write,
  888. .aio_write = tun_chr_aio_write,
  889. .poll = tun_chr_poll,
  890. .ioctl = tun_chr_ioctl,
  891. .open = tun_chr_open,
  892. .release = tun_chr_close,
  893. .fasync = tun_chr_fasync
  894. };
  895. static struct miscdevice tun_miscdev = {
  896. .minor = TUN_MINOR,
  897. .name = "tun",
  898. .fops = &tun_fops,
  899. };
  900. /* ethtool interface */
  901. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  902. {
  903. cmd->supported = 0;
  904. cmd->advertising = 0;
  905. cmd->speed = SPEED_10;
  906. cmd->duplex = DUPLEX_FULL;
  907. cmd->port = PORT_TP;
  908. cmd->phy_address = 0;
  909. cmd->transceiver = XCVR_INTERNAL;
  910. cmd->autoneg = AUTONEG_DISABLE;
  911. cmd->maxtxpkt = 0;
  912. cmd->maxrxpkt = 0;
  913. return 0;
  914. }
  915. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  916. {
  917. struct tun_struct *tun = netdev_priv(dev);
  918. strcpy(info->driver, DRV_NAME);
  919. strcpy(info->version, DRV_VERSION);
  920. strcpy(info->fw_version, "N/A");
  921. switch (tun->flags & TUN_TYPE_MASK) {
  922. case TUN_TUN_DEV:
  923. strcpy(info->bus_info, "tun");
  924. break;
  925. case TUN_TAP_DEV:
  926. strcpy(info->bus_info, "tap");
  927. break;
  928. }
  929. }
  930. static u32 tun_get_msglevel(struct net_device *dev)
  931. {
  932. #ifdef TUN_DEBUG
  933. struct tun_struct *tun = netdev_priv(dev);
  934. return tun->debug;
  935. #else
  936. return -EOPNOTSUPP;
  937. #endif
  938. }
  939. static void tun_set_msglevel(struct net_device *dev, u32 value)
  940. {
  941. #ifdef TUN_DEBUG
  942. struct tun_struct *tun = netdev_priv(dev);
  943. tun->debug = value;
  944. #endif
  945. }
  946. static u32 tun_get_link(struct net_device *dev)
  947. {
  948. struct tun_struct *tun = netdev_priv(dev);
  949. return tun->attached;
  950. }
  951. static u32 tun_get_rx_csum(struct net_device *dev)
  952. {
  953. struct tun_struct *tun = netdev_priv(dev);
  954. return (tun->flags & TUN_NOCHECKSUM) == 0;
  955. }
  956. static int tun_set_rx_csum(struct net_device *dev, u32 data)
  957. {
  958. struct tun_struct *tun = netdev_priv(dev);
  959. if (data)
  960. tun->flags &= ~TUN_NOCHECKSUM;
  961. else
  962. tun->flags |= TUN_NOCHECKSUM;
  963. return 0;
  964. }
  965. static const struct ethtool_ops tun_ethtool_ops = {
  966. .get_settings = tun_get_settings,
  967. .get_drvinfo = tun_get_drvinfo,
  968. .get_msglevel = tun_get_msglevel,
  969. .set_msglevel = tun_set_msglevel,
  970. .get_link = tun_get_link,
  971. .get_rx_csum = tun_get_rx_csum,
  972. .set_rx_csum = tun_set_rx_csum
  973. };
  974. static int tun_init_net(struct net *net)
  975. {
  976. struct tun_net *tn;
  977. tn = kmalloc(sizeof(*tn), GFP_KERNEL);
  978. if (tn == NULL)
  979. return -ENOMEM;
  980. INIT_LIST_HEAD(&tn->dev_list);
  981. if (net_assign_generic(net, tun_net_id, tn)) {
  982. kfree(tn);
  983. return -ENOMEM;
  984. }
  985. return 0;
  986. }
  987. static void tun_exit_net(struct net *net)
  988. {
  989. struct tun_net *tn;
  990. struct tun_struct *tun, *nxt;
  991. tn = net_generic(net, tun_net_id);
  992. rtnl_lock();
  993. list_for_each_entry_safe(tun, nxt, &tn->dev_list, list) {
  994. DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
  995. unregister_netdevice(tun->dev);
  996. }
  997. rtnl_unlock();
  998. kfree(tn);
  999. }
  1000. static struct pernet_operations tun_net_ops = {
  1001. .init = tun_init_net,
  1002. .exit = tun_exit_net,
  1003. };
  1004. static int __init tun_init(void)
  1005. {
  1006. int ret = 0;
  1007. printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1008. printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
  1009. ret = register_pernet_gen_device(&tun_net_id, &tun_net_ops);
  1010. if (ret) {
  1011. printk(KERN_ERR "tun: Can't register pernet ops\n");
  1012. goto err_pernet;
  1013. }
  1014. ret = misc_register(&tun_miscdev);
  1015. if (ret) {
  1016. printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
  1017. goto err_misc;
  1018. }
  1019. return 0;
  1020. err_misc:
  1021. unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
  1022. err_pernet:
  1023. return ret;
  1024. }
  1025. static void tun_cleanup(void)
  1026. {
  1027. misc_deregister(&tun_miscdev);
  1028. unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
  1029. }
  1030. module_init(tun_init);
  1031. module_exit(tun_cleanup);
  1032. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1033. MODULE_AUTHOR(DRV_COPYRIGHT);
  1034. MODULE_LICENSE("GPL");
  1035. MODULE_ALIAS_MISCDEV(TUN_MINOR);