tun.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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 <net/net_namespace.h>
  63. #include <asm/system.h>
  64. #include <asm/uaccess.h>
  65. #ifdef TUN_DEBUG
  66. static int debug;
  67. #endif
  68. /* Network device part of the driver */
  69. static LIST_HEAD(tun_dev_list);
  70. static const struct ethtool_ops tun_ethtool_ops;
  71. /* Net device open. */
  72. static int tun_net_open(struct net_device *dev)
  73. {
  74. netif_start_queue(dev);
  75. return 0;
  76. }
  77. /* Net device close. */
  78. static int tun_net_close(struct net_device *dev)
  79. {
  80. netif_stop_queue(dev);
  81. return 0;
  82. }
  83. /* Net device start xmit */
  84. static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
  85. {
  86. struct tun_struct *tun = netdev_priv(dev);
  87. DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
  88. /* Drop packet if interface is not attached */
  89. if (!tun->attached)
  90. goto drop;
  91. /* Packet dropping */
  92. if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
  93. if (!(tun->flags & TUN_ONE_QUEUE)) {
  94. /* Normal queueing mode. */
  95. /* Packet scheduler handles dropping of further packets. */
  96. netif_stop_queue(dev);
  97. /* We won't see all dropped packets individually, so overrun
  98. * error is more appropriate. */
  99. dev->stats.tx_fifo_errors++;
  100. } else {
  101. /* Single queue mode.
  102. * Driver handles dropping of all packets itself. */
  103. goto drop;
  104. }
  105. }
  106. /* Queue packet */
  107. skb_queue_tail(&tun->readq, skb);
  108. dev->trans_start = jiffies;
  109. /* Notify and wake up reader process */
  110. if (tun->flags & TUN_FASYNC)
  111. kill_fasync(&tun->fasync, SIGIO, POLL_IN);
  112. wake_up_interruptible(&tun->read_wait);
  113. return 0;
  114. drop:
  115. dev->stats.tx_dropped++;
  116. kfree_skb(skb);
  117. return 0;
  118. }
  119. /** Add the specified Ethernet address to this multicast filter. */
  120. static void
  121. add_multi(u32* filter, const u8* addr)
  122. {
  123. int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
  124. filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  125. }
  126. /** Remove the specified Ethernet addres from this multicast filter. */
  127. static void
  128. del_multi(u32* filter, const u8* addr)
  129. {
  130. int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
  131. filter[bit_nr >> 5] &= ~(1 << (bit_nr & 31));
  132. }
  133. /** Update the list of multicast groups to which the network device belongs.
  134. * This list is used to filter packets being sent from the character device to
  135. * the network device. */
  136. static void
  137. tun_net_mclist(struct net_device *dev)
  138. {
  139. struct tun_struct *tun = netdev_priv(dev);
  140. const struct dev_mc_list *mclist;
  141. int i;
  142. DBG(KERN_DEBUG "%s: tun_net_mclist: mc_count %d\n",
  143. dev->name, dev->mc_count);
  144. memset(tun->chr_filter, 0, sizeof tun->chr_filter);
  145. for (i = 0, mclist = dev->mc_list; i < dev->mc_count && mclist != NULL;
  146. i++, mclist = mclist->next) {
  147. add_multi(tun->net_filter, mclist->dmi_addr);
  148. DBG(KERN_DEBUG "%s: tun_net_mclist: %x:%x:%x:%x:%x:%x\n",
  149. dev->name,
  150. mclist->dmi_addr[0], mclist->dmi_addr[1], mclist->dmi_addr[2],
  151. mclist->dmi_addr[3], mclist->dmi_addr[4], mclist->dmi_addr[5]);
  152. }
  153. }
  154. #define MIN_MTU 68
  155. #define MAX_MTU 65535
  156. static int
  157. tun_net_change_mtu(struct net_device *dev, int new_mtu)
  158. {
  159. if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
  160. return -EINVAL;
  161. dev->mtu = new_mtu;
  162. return 0;
  163. }
  164. /* Initialize net device. */
  165. static void tun_net_init(struct net_device *dev)
  166. {
  167. struct tun_struct *tun = netdev_priv(dev);
  168. switch (tun->flags & TUN_TYPE_MASK) {
  169. case TUN_TUN_DEV:
  170. /* Point-to-Point TUN Device */
  171. dev->hard_header_len = 0;
  172. dev->addr_len = 0;
  173. dev->mtu = 1500;
  174. dev->change_mtu = tun_net_change_mtu;
  175. /* Zero header length */
  176. dev->type = ARPHRD_NONE;
  177. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  178. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  179. break;
  180. case TUN_TAP_DEV:
  181. /* Ethernet TAP Device */
  182. dev->set_multicast_list = tun_net_mclist;
  183. ether_setup(dev);
  184. dev->change_mtu = tun_net_change_mtu;
  185. /* random address already created for us by tun_set_iff, use it */
  186. memcpy(dev->dev_addr, tun->dev_addr, min(sizeof(tun->dev_addr), sizeof(dev->dev_addr)) );
  187. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  188. break;
  189. }
  190. }
  191. /* Character device part */
  192. /* Poll */
  193. static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
  194. {
  195. struct tun_struct *tun = file->private_data;
  196. unsigned int mask = POLLOUT | POLLWRNORM;
  197. if (!tun)
  198. return -EBADFD;
  199. DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
  200. poll_wait(file, &tun->read_wait, wait);
  201. if (!skb_queue_empty(&tun->readq))
  202. mask |= POLLIN | POLLRDNORM;
  203. return mask;
  204. }
  205. /* Get packet from user space buffer */
  206. static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
  207. {
  208. struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
  209. struct sk_buff *skb;
  210. size_t len = count, align = 0;
  211. if (!(tun->flags & TUN_NO_PI)) {
  212. if ((len -= sizeof(pi)) > count)
  213. return -EINVAL;
  214. if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
  215. return -EFAULT;
  216. }
  217. if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV)
  218. align = NET_IP_ALIGN;
  219. if (!(skb = alloc_skb(len + align, GFP_KERNEL))) {
  220. tun->dev->stats.rx_dropped++;
  221. return -ENOMEM;
  222. }
  223. if (align)
  224. skb_reserve(skb, align);
  225. if (memcpy_fromiovec(skb_put(skb, len), iv, len)) {
  226. tun->dev->stats.rx_dropped++;
  227. kfree_skb(skb);
  228. return -EFAULT;
  229. }
  230. switch (tun->flags & TUN_TYPE_MASK) {
  231. case TUN_TUN_DEV:
  232. skb_reset_mac_header(skb);
  233. skb->protocol = pi.proto;
  234. skb->dev = tun->dev;
  235. break;
  236. case TUN_TAP_DEV:
  237. skb->protocol = eth_type_trans(skb, tun->dev);
  238. break;
  239. };
  240. if (tun->flags & TUN_NOCHECKSUM)
  241. skb->ip_summed = CHECKSUM_UNNECESSARY;
  242. netif_rx_ni(skb);
  243. tun->dev->last_rx = jiffies;
  244. tun->dev->stats.rx_packets++;
  245. tun->dev->stats.rx_bytes += len;
  246. return count;
  247. }
  248. static inline size_t iov_total(const struct iovec *iv, unsigned long count)
  249. {
  250. unsigned long i;
  251. size_t len;
  252. for (i = 0, len = 0; i < count; i++)
  253. len += iv[i].iov_len;
  254. return len;
  255. }
  256. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  257. unsigned long count, loff_t pos)
  258. {
  259. struct tun_struct *tun = iocb->ki_filp->private_data;
  260. if (!tun)
  261. return -EBADFD;
  262. DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
  263. return tun_get_user(tun, (struct iovec *) iv, iov_total(iv, count));
  264. }
  265. /* Put packet to the user space buffer */
  266. static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
  267. struct sk_buff *skb,
  268. struct iovec *iv, int len)
  269. {
  270. struct tun_pi pi = { 0, skb->protocol };
  271. ssize_t total = 0;
  272. if (!(tun->flags & TUN_NO_PI)) {
  273. if ((len -= sizeof(pi)) < 0)
  274. return -EINVAL;
  275. if (len < skb->len) {
  276. /* Packet will be striped */
  277. pi.flags |= TUN_PKT_STRIP;
  278. }
  279. if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
  280. return -EFAULT;
  281. total += sizeof(pi);
  282. }
  283. len = min_t(int, skb->len, len);
  284. skb_copy_datagram_iovec(skb, 0, iv, len);
  285. total += len;
  286. tun->dev->stats.tx_packets++;
  287. tun->dev->stats.tx_bytes += len;
  288. return total;
  289. }
  290. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  291. unsigned long count, loff_t pos)
  292. {
  293. struct file *file = iocb->ki_filp;
  294. struct tun_struct *tun = file->private_data;
  295. DECLARE_WAITQUEUE(wait, current);
  296. struct sk_buff *skb;
  297. ssize_t len, ret = 0;
  298. if (!tun)
  299. return -EBADFD;
  300. DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
  301. len = iov_total(iv, count);
  302. if (len < 0)
  303. return -EINVAL;
  304. add_wait_queue(&tun->read_wait, &wait);
  305. while (len) {
  306. const u8 ones[ ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  307. u8 addr[ ETH_ALEN];
  308. int bit_nr;
  309. current->state = TASK_INTERRUPTIBLE;
  310. /* Read frames from the queue */
  311. if (!(skb=skb_dequeue(&tun->readq))) {
  312. if (file->f_flags & O_NONBLOCK) {
  313. ret = -EAGAIN;
  314. break;
  315. }
  316. if (signal_pending(current)) {
  317. ret = -ERESTARTSYS;
  318. break;
  319. }
  320. /* Nothing to read, let's sleep */
  321. schedule();
  322. continue;
  323. }
  324. netif_wake_queue(tun->dev);
  325. /** Decide whether to accept this packet. This code is designed to
  326. * behave identically to an Ethernet interface. Accept the packet if
  327. * - we are promiscuous.
  328. * - the packet is addressed to us.
  329. * - the packet is broadcast.
  330. * - the packet is multicast and
  331. * - we are multicast promiscous.
  332. * - we belong to the multicast group.
  333. */
  334. skb_copy_from_linear_data(skb, addr, min_t(size_t, sizeof addr,
  335. skb->len));
  336. bit_nr = ether_crc(sizeof addr, addr) >> 26;
  337. if ((tun->if_flags & IFF_PROMISC) ||
  338. memcmp(addr, tun->dev_addr, sizeof addr) == 0 ||
  339. memcmp(addr, ones, sizeof addr) == 0 ||
  340. (((addr[0] == 1 && addr[1] == 0 && addr[2] == 0x5e) ||
  341. (addr[0] == 0x33 && addr[1] == 0x33)) &&
  342. ((tun->if_flags & IFF_ALLMULTI) ||
  343. (tun->chr_filter[bit_nr >> 5] & (1 << (bit_nr & 31)))))) {
  344. DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %x:%x:%x:%x:%x:%x\n",
  345. tun->dev->name, addr[0], addr[1], addr[2],
  346. addr[3], addr[4], addr[5]);
  347. ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
  348. kfree_skb(skb);
  349. break;
  350. } else {
  351. DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %x:%x:%x:%x:%x:%x\n",
  352. tun->dev->name, addr[0], addr[1], addr[2],
  353. addr[3], addr[4], addr[5]);
  354. kfree_skb(skb);
  355. continue;
  356. }
  357. }
  358. current->state = TASK_RUNNING;
  359. remove_wait_queue(&tun->read_wait, &wait);
  360. return ret;
  361. }
  362. static void tun_setup(struct net_device *dev)
  363. {
  364. struct tun_struct *tun = netdev_priv(dev);
  365. skb_queue_head_init(&tun->readq);
  366. init_waitqueue_head(&tun->read_wait);
  367. tun->owner = -1;
  368. tun->group = -1;
  369. dev->open = tun_net_open;
  370. dev->hard_start_xmit = tun_net_xmit;
  371. dev->stop = tun_net_close;
  372. dev->ethtool_ops = &tun_ethtool_ops;
  373. dev->destructor = free_netdev;
  374. }
  375. static struct tun_struct *tun_get_by_name(const char *name)
  376. {
  377. struct tun_struct *tun;
  378. ASSERT_RTNL();
  379. list_for_each_entry(tun, &tun_dev_list, list) {
  380. if (!strncmp(tun->dev->name, name, IFNAMSIZ))
  381. return tun;
  382. }
  383. return NULL;
  384. }
  385. static int tun_set_iff(struct file *file, struct ifreq *ifr)
  386. {
  387. struct tun_struct *tun;
  388. struct net_device *dev;
  389. int err;
  390. tun = tun_get_by_name(ifr->ifr_name);
  391. if (tun) {
  392. if (tun->attached)
  393. return -EBUSY;
  394. /* Check permissions */
  395. if (((tun->owner != -1 &&
  396. current->euid != tun->owner) ||
  397. (tun->group != -1 &&
  398. current->egid != tun->group)) &&
  399. !capable(CAP_NET_ADMIN))
  400. return -EPERM;
  401. }
  402. else if (__dev_get_by_name(&init_net, ifr->ifr_name))
  403. return -EINVAL;
  404. else {
  405. char *name;
  406. unsigned long flags = 0;
  407. err = -EINVAL;
  408. if (!capable(CAP_NET_ADMIN))
  409. return -EPERM;
  410. /* Set dev type */
  411. if (ifr->ifr_flags & IFF_TUN) {
  412. /* TUN device */
  413. flags |= TUN_TUN_DEV;
  414. name = "tun%d";
  415. } else if (ifr->ifr_flags & IFF_TAP) {
  416. /* TAP device */
  417. flags |= TUN_TAP_DEV;
  418. name = "tap%d";
  419. } else
  420. goto failed;
  421. if (*ifr->ifr_name)
  422. name = ifr->ifr_name;
  423. dev = alloc_netdev(sizeof(struct tun_struct), name,
  424. tun_setup);
  425. if (!dev)
  426. return -ENOMEM;
  427. tun = netdev_priv(dev);
  428. tun->dev = dev;
  429. tun->flags = flags;
  430. /* Be promiscuous by default to maintain previous behaviour. */
  431. tun->if_flags = IFF_PROMISC;
  432. /* Generate random Ethernet address. */
  433. *(u16 *)tun->dev_addr = htons(0x00FF);
  434. get_random_bytes(tun->dev_addr + sizeof(u16), 4);
  435. memset(tun->chr_filter, 0, sizeof tun->chr_filter);
  436. tun_net_init(dev);
  437. if (strchr(dev->name, '%')) {
  438. err = dev_alloc_name(dev, dev->name);
  439. if (err < 0)
  440. goto err_free_dev;
  441. }
  442. err = register_netdevice(tun->dev);
  443. if (err < 0)
  444. goto err_free_dev;
  445. list_add(&tun->list, &tun_dev_list);
  446. }
  447. DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
  448. if (ifr->ifr_flags & IFF_NO_PI)
  449. tun->flags |= TUN_NO_PI;
  450. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  451. tun->flags |= TUN_ONE_QUEUE;
  452. file->private_data = tun;
  453. tun->attached = 1;
  454. strcpy(ifr->ifr_name, tun->dev->name);
  455. return 0;
  456. err_free_dev:
  457. free_netdev(dev);
  458. failed:
  459. return err;
  460. }
  461. static int tun_chr_ioctl(struct inode *inode, struct file *file,
  462. unsigned int cmd, unsigned long arg)
  463. {
  464. struct tun_struct *tun = file->private_data;
  465. void __user* argp = (void __user*)arg;
  466. struct ifreq ifr;
  467. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
  468. if (copy_from_user(&ifr, argp, sizeof ifr))
  469. return -EFAULT;
  470. if (cmd == TUNSETIFF && !tun) {
  471. int err;
  472. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  473. rtnl_lock();
  474. err = tun_set_iff(file, &ifr);
  475. rtnl_unlock();
  476. if (err)
  477. return err;
  478. if (copy_to_user(argp, &ifr, sizeof(ifr)))
  479. return -EFAULT;
  480. return 0;
  481. }
  482. if (!tun)
  483. return -EBADFD;
  484. DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
  485. switch (cmd) {
  486. case TUNSETNOCSUM:
  487. /* Disable/Enable checksum */
  488. if (arg)
  489. tun->flags |= TUN_NOCHECKSUM;
  490. else
  491. tun->flags &= ~TUN_NOCHECKSUM;
  492. DBG(KERN_INFO "%s: checksum %s\n",
  493. tun->dev->name, arg ? "disabled" : "enabled");
  494. break;
  495. case TUNSETPERSIST:
  496. /* Disable/Enable persist mode */
  497. if (arg)
  498. tun->flags |= TUN_PERSIST;
  499. else
  500. tun->flags &= ~TUN_PERSIST;
  501. DBG(KERN_INFO "%s: persist %s\n",
  502. tun->dev->name, arg ? "disabled" : "enabled");
  503. break;
  504. case TUNSETOWNER:
  505. /* Set owner of the device */
  506. tun->owner = (uid_t) arg;
  507. DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
  508. break;
  509. case TUNSETGROUP:
  510. /* Set group of the device */
  511. tun->group= (gid_t) arg;
  512. DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
  513. break;
  514. case TUNSETLINK:
  515. /* Only allow setting the type when the interface is down */
  516. if (tun->dev->flags & IFF_UP) {
  517. DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
  518. tun->dev->name);
  519. return -EBUSY;
  520. } else {
  521. tun->dev->type = (int) arg;
  522. DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
  523. }
  524. break;
  525. #ifdef TUN_DEBUG
  526. case TUNSETDEBUG:
  527. tun->debug = arg;
  528. break;
  529. #endif
  530. case SIOCGIFFLAGS:
  531. ifr.ifr_flags = tun->if_flags;
  532. if (copy_to_user( argp, &ifr, sizeof ifr))
  533. return -EFAULT;
  534. return 0;
  535. case SIOCSIFFLAGS:
  536. /** Set the character device's interface flags. Currently only
  537. * IFF_PROMISC and IFF_ALLMULTI are used. */
  538. tun->if_flags = ifr.ifr_flags;
  539. DBG(KERN_INFO "%s: interface flags 0x%lx\n",
  540. tun->dev->name, tun->if_flags);
  541. return 0;
  542. case SIOCGIFHWADDR:
  543. /* Note: the actual net device's address may be different */
  544. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
  545. min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
  546. if (copy_to_user( argp, &ifr, sizeof ifr))
  547. return -EFAULT;
  548. return 0;
  549. case SIOCSIFHWADDR:
  550. {
  551. /* try to set the actual net device's hw address */
  552. int ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  553. if (ret == 0) {
  554. /** Set the character device's hardware address. This is used when
  555. * filtering packets being sent from the network device to the character
  556. * device. */
  557. memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
  558. min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
  559. DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
  560. tun->dev->name,
  561. tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
  562. tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
  563. }
  564. return ret;
  565. }
  566. case SIOCADDMULTI:
  567. /** Add the specified group to the character device's multicast filter
  568. * list. */
  569. add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
  570. DBG(KERN_DEBUG "%s: add multi: %x:%x:%x:%x:%x:%x\n",
  571. tun->dev->name,
  572. (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
  573. (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
  574. (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
  575. return 0;
  576. case SIOCDELMULTI:
  577. /** Remove the specified group from the character device's multicast
  578. * filter list. */
  579. del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
  580. DBG(KERN_DEBUG "%s: del multi: %x:%x:%x:%x:%x:%x\n",
  581. tun->dev->name,
  582. (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
  583. (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
  584. (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
  585. return 0;
  586. default:
  587. return -EINVAL;
  588. };
  589. return 0;
  590. }
  591. static int tun_chr_fasync(int fd, struct file *file, int on)
  592. {
  593. struct tun_struct *tun = file->private_data;
  594. int ret;
  595. if (!tun)
  596. return -EBADFD;
  597. DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
  598. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  599. return ret;
  600. if (on) {
  601. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  602. if (ret)
  603. return ret;
  604. tun->flags |= TUN_FASYNC;
  605. } else
  606. tun->flags &= ~TUN_FASYNC;
  607. return 0;
  608. }
  609. static int tun_chr_open(struct inode *inode, struct file * file)
  610. {
  611. DBG1(KERN_INFO "tunX: tun_chr_open\n");
  612. file->private_data = NULL;
  613. return 0;
  614. }
  615. static int tun_chr_close(struct inode *inode, struct file *file)
  616. {
  617. struct tun_struct *tun = file->private_data;
  618. if (!tun)
  619. return 0;
  620. DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
  621. tun_chr_fasync(-1, file, 0);
  622. rtnl_lock();
  623. /* Detach from net device */
  624. file->private_data = NULL;
  625. tun->attached = 0;
  626. /* Drop read queue */
  627. skb_queue_purge(&tun->readq);
  628. if (!(tun->flags & TUN_PERSIST)) {
  629. list_del(&tun->list);
  630. unregister_netdevice(tun->dev);
  631. }
  632. rtnl_unlock();
  633. return 0;
  634. }
  635. static const struct file_operations tun_fops = {
  636. .owner = THIS_MODULE,
  637. .llseek = no_llseek,
  638. .read = do_sync_read,
  639. .aio_read = tun_chr_aio_read,
  640. .write = do_sync_write,
  641. .aio_write = tun_chr_aio_write,
  642. .poll = tun_chr_poll,
  643. .ioctl = tun_chr_ioctl,
  644. .open = tun_chr_open,
  645. .release = tun_chr_close,
  646. .fasync = tun_chr_fasync
  647. };
  648. static struct miscdevice tun_miscdev = {
  649. .minor = TUN_MINOR,
  650. .name = "tun",
  651. .fops = &tun_fops,
  652. };
  653. /* ethtool interface */
  654. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  655. {
  656. cmd->supported = 0;
  657. cmd->advertising = 0;
  658. cmd->speed = SPEED_10;
  659. cmd->duplex = DUPLEX_FULL;
  660. cmd->port = PORT_TP;
  661. cmd->phy_address = 0;
  662. cmd->transceiver = XCVR_INTERNAL;
  663. cmd->autoneg = AUTONEG_DISABLE;
  664. cmd->maxtxpkt = 0;
  665. cmd->maxrxpkt = 0;
  666. return 0;
  667. }
  668. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  669. {
  670. struct tun_struct *tun = netdev_priv(dev);
  671. strcpy(info->driver, DRV_NAME);
  672. strcpy(info->version, DRV_VERSION);
  673. strcpy(info->fw_version, "N/A");
  674. switch (tun->flags & TUN_TYPE_MASK) {
  675. case TUN_TUN_DEV:
  676. strcpy(info->bus_info, "tun");
  677. break;
  678. case TUN_TAP_DEV:
  679. strcpy(info->bus_info, "tap");
  680. break;
  681. }
  682. }
  683. static u32 tun_get_msglevel(struct net_device *dev)
  684. {
  685. #ifdef TUN_DEBUG
  686. struct tun_struct *tun = netdev_priv(dev);
  687. return tun->debug;
  688. #else
  689. return -EOPNOTSUPP;
  690. #endif
  691. }
  692. static void tun_set_msglevel(struct net_device *dev, u32 value)
  693. {
  694. #ifdef TUN_DEBUG
  695. struct tun_struct *tun = netdev_priv(dev);
  696. tun->debug = value;
  697. #endif
  698. }
  699. static u32 tun_get_link(struct net_device *dev)
  700. {
  701. struct tun_struct *tun = netdev_priv(dev);
  702. return tun->attached;
  703. }
  704. static u32 tun_get_rx_csum(struct net_device *dev)
  705. {
  706. struct tun_struct *tun = netdev_priv(dev);
  707. return (tun->flags & TUN_NOCHECKSUM) == 0;
  708. }
  709. static int tun_set_rx_csum(struct net_device *dev, u32 data)
  710. {
  711. struct tun_struct *tun = netdev_priv(dev);
  712. if (data)
  713. tun->flags &= ~TUN_NOCHECKSUM;
  714. else
  715. tun->flags |= TUN_NOCHECKSUM;
  716. return 0;
  717. }
  718. static const struct ethtool_ops tun_ethtool_ops = {
  719. .get_settings = tun_get_settings,
  720. .get_drvinfo = tun_get_drvinfo,
  721. .get_msglevel = tun_get_msglevel,
  722. .set_msglevel = tun_set_msglevel,
  723. .get_link = tun_get_link,
  724. .get_rx_csum = tun_get_rx_csum,
  725. .set_rx_csum = tun_set_rx_csum
  726. };
  727. static int __init tun_init(void)
  728. {
  729. int ret = 0;
  730. printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  731. printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
  732. ret = misc_register(&tun_miscdev);
  733. if (ret)
  734. printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
  735. return ret;
  736. }
  737. static void tun_cleanup(void)
  738. {
  739. struct tun_struct *tun, *nxt;
  740. misc_deregister(&tun_miscdev);
  741. rtnl_lock();
  742. list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) {
  743. DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
  744. unregister_netdevice(tun->dev);
  745. }
  746. rtnl_unlock();
  747. }
  748. module_init(tun_init);
  749. module_exit(tun_cleanup);
  750. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  751. MODULE_AUTHOR(DRV_COPYRIGHT);
  752. MODULE_LICENSE("GPL");
  753. MODULE_ALIAS_MISCDEV(TUN_MINOR);