tun.c 22 KB

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