tun.c 27 KB

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