tun.c 21 KB

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