tun.c 21 KB

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