tun.c 21 KB

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