tun.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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. return -EFAULT;
  215. skb->dev = tun->dev;
  216. switch (tun->flags & TUN_TYPE_MASK) {
  217. case TUN_TUN_DEV:
  218. skb->mac.raw = skb->data;
  219. skb->protocol = pi.proto;
  220. break;
  221. case TUN_TAP_DEV:
  222. skb->protocol = eth_type_trans(skb, tun->dev);
  223. break;
  224. };
  225. if (tun->flags & TUN_NOCHECKSUM)
  226. skb->ip_summed = CHECKSUM_UNNECESSARY;
  227. netif_rx_ni(skb);
  228. tun->dev->last_rx = jiffies;
  229. tun->stats.rx_packets++;
  230. tun->stats.rx_bytes += len;
  231. return count;
  232. }
  233. static inline size_t iov_total(const struct iovec *iv, unsigned long count)
  234. {
  235. unsigned long i;
  236. size_t len;
  237. for (i = 0, len = 0; i < count; i++)
  238. len += iv[i].iov_len;
  239. return len;
  240. }
  241. /* Writev */
  242. static ssize_t tun_chr_writev(struct file * file, const struct iovec *iv,
  243. unsigned long count, loff_t *pos)
  244. {
  245. struct tun_struct *tun = file->private_data;
  246. if (!tun)
  247. return -EBADFD;
  248. DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
  249. return tun_get_user(tun, (struct iovec *) iv, iov_total(iv, count));
  250. }
  251. /* Write */
  252. static ssize_t tun_chr_write(struct file * file, const char __user * buf,
  253. size_t count, loff_t *pos)
  254. {
  255. struct iovec iv = { (void __user *) buf, count };
  256. return tun_chr_writev(file, &iv, 1, pos);
  257. }
  258. /* Put packet to the user space buffer */
  259. static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
  260. struct sk_buff *skb,
  261. struct iovec *iv, int len)
  262. {
  263. struct tun_pi pi = { 0, skb->protocol };
  264. ssize_t total = 0;
  265. if (!(tun->flags & TUN_NO_PI)) {
  266. if ((len -= sizeof(pi)) < 0)
  267. return -EINVAL;
  268. if (len < skb->len) {
  269. /* Packet will be striped */
  270. pi.flags |= TUN_PKT_STRIP;
  271. }
  272. if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
  273. return -EFAULT;
  274. total += sizeof(pi);
  275. }
  276. len = min_t(int, skb->len, len);
  277. skb_copy_datagram_iovec(skb, 0, iv, len);
  278. total += len;
  279. tun->stats.tx_packets++;
  280. tun->stats.tx_bytes += len;
  281. return total;
  282. }
  283. /* Readv */
  284. static ssize_t tun_chr_readv(struct file *file, const struct iovec *iv,
  285. unsigned long count, loff_t *pos)
  286. {
  287. struct tun_struct *tun = file->private_data;
  288. DECLARE_WAITQUEUE(wait, current);
  289. struct sk_buff *skb;
  290. ssize_t len, ret = 0;
  291. if (!tun)
  292. return -EBADFD;
  293. DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
  294. len = iov_total(iv, count);
  295. if (len < 0)
  296. return -EINVAL;
  297. add_wait_queue(&tun->read_wait, &wait);
  298. while (len) {
  299. const u8 ones[ ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  300. u8 addr[ ETH_ALEN];
  301. int bit_nr;
  302. current->state = TASK_INTERRUPTIBLE;
  303. /* Read frames from the queue */
  304. if (!(skb=skb_dequeue(&tun->readq))) {
  305. if (file->f_flags & O_NONBLOCK) {
  306. ret = -EAGAIN;
  307. break;
  308. }
  309. if (signal_pending(current)) {
  310. ret = -ERESTARTSYS;
  311. break;
  312. }
  313. /* Nothing to read, let's sleep */
  314. schedule();
  315. continue;
  316. }
  317. netif_wake_queue(tun->dev);
  318. /** Decide whether to accept this packet. This code is designed to
  319. * behave identically to an Ethernet interface. Accept the packet if
  320. * - we are promiscuous.
  321. * - the packet is addressed to us.
  322. * - the packet is broadcast.
  323. * - the packet is multicast and
  324. * - we are multicast promiscous.
  325. * - we belong to the multicast group.
  326. */
  327. memcpy(addr, skb->data,
  328. min_t(size_t, sizeof addr, skb->len));
  329. bit_nr = ether_crc(sizeof addr, addr) >> 26;
  330. if ((tun->if_flags & IFF_PROMISC) ||
  331. memcmp(addr, tun->dev_addr, sizeof addr) == 0 ||
  332. memcmp(addr, ones, sizeof addr) == 0 ||
  333. (((addr[0] == 1 && addr[1] == 0 && addr[2] == 0x5e) ||
  334. (addr[0] == 0x33 && addr[1] == 0x33)) &&
  335. ((tun->if_flags & IFF_ALLMULTI) ||
  336. (tun->chr_filter[bit_nr >> 5] & (1 << (bit_nr & 31)))))) {
  337. DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %x:%x:%x:%x:%x:%x\n",
  338. tun->dev->name, addr[0], addr[1], addr[2],
  339. addr[3], addr[4], addr[5]);
  340. ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
  341. kfree_skb(skb);
  342. break;
  343. } else {
  344. DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %x:%x:%x:%x:%x:%x\n",
  345. tun->dev->name, addr[0], addr[1], addr[2],
  346. addr[3], addr[4], addr[5]);
  347. kfree_skb(skb);
  348. continue;
  349. }
  350. }
  351. current->state = TASK_RUNNING;
  352. remove_wait_queue(&tun->read_wait, &wait);
  353. return ret;
  354. }
  355. /* Read */
  356. static ssize_t tun_chr_read(struct file * file, char __user * buf,
  357. size_t count, loff_t *pos)
  358. {
  359. struct iovec iv = { buf, count };
  360. return tun_chr_readv(file, &iv, 1, pos);
  361. }
  362. static void tun_setup(struct net_device *dev)
  363. {
  364. struct tun_struct *tun = netdev_priv(dev);
  365. skb_queue_head_init(&tun->readq);
  366. init_waitqueue_head(&tun->read_wait);
  367. tun->owner = -1;
  368. SET_MODULE_OWNER(dev);
  369. dev->open = tun_net_open;
  370. dev->hard_start_xmit = tun_net_xmit;
  371. dev->stop = tun_net_close;
  372. dev->get_stats = tun_net_stats;
  373. dev->ethtool_ops = &tun_ethtool_ops;
  374. dev->destructor = free_netdev;
  375. }
  376. static struct tun_struct *tun_get_by_name(const char *name)
  377. {
  378. struct tun_struct *tun;
  379. ASSERT_RTNL();
  380. list_for_each_entry(tun, &tun_dev_list, list) {
  381. if (!strncmp(tun->dev->name, name, IFNAMSIZ))
  382. return tun;
  383. }
  384. return NULL;
  385. }
  386. static int tun_set_iff(struct file *file, struct ifreq *ifr)
  387. {
  388. struct tun_struct *tun;
  389. struct net_device *dev;
  390. int err;
  391. tun = tun_get_by_name(ifr->ifr_name);
  392. if (tun) {
  393. if (tun->attached)
  394. return -EBUSY;
  395. /* Check permissions */
  396. if (tun->owner != -1 &&
  397. current->euid != tun->owner && !capable(CAP_NET_ADMIN))
  398. return -EPERM;
  399. }
  400. else if (__dev_get_by_name(ifr->ifr_name))
  401. return -EINVAL;
  402. else {
  403. char *name;
  404. unsigned long flags = 0;
  405. err = -EINVAL;
  406. /* Set dev type */
  407. if (ifr->ifr_flags & IFF_TUN) {
  408. /* TUN device */
  409. flags |= TUN_TUN_DEV;
  410. name = "tun%d";
  411. } else if (ifr->ifr_flags & IFF_TAP) {
  412. /* TAP device */
  413. flags |= TUN_TAP_DEV;
  414. name = "tap%d";
  415. } else
  416. goto failed;
  417. if (*ifr->ifr_name)
  418. name = ifr->ifr_name;
  419. dev = alloc_netdev(sizeof(struct tun_struct), name,
  420. tun_setup);
  421. if (!dev)
  422. return -ENOMEM;
  423. tun = netdev_priv(dev);
  424. tun->dev = dev;
  425. tun->flags = flags;
  426. /* Be promiscuous by default to maintain previous behaviour. */
  427. tun->if_flags = IFF_PROMISC;
  428. /* Generate random Ethernet address. */
  429. *(u16 *)tun->dev_addr = htons(0x00FF);
  430. get_random_bytes(tun->dev_addr + sizeof(u16), 4);
  431. memset(tun->chr_filter, 0, sizeof tun->chr_filter);
  432. tun_net_init(dev);
  433. if (strchr(dev->name, '%')) {
  434. err = dev_alloc_name(dev, dev->name);
  435. if (err < 0)
  436. goto err_free_dev;
  437. }
  438. err = register_netdevice(tun->dev);
  439. if (err < 0)
  440. goto err_free_dev;
  441. list_add(&tun->list, &tun_dev_list);
  442. }
  443. DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
  444. if (ifr->ifr_flags & IFF_NO_PI)
  445. tun->flags |= TUN_NO_PI;
  446. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  447. tun->flags |= TUN_ONE_QUEUE;
  448. file->private_data = tun;
  449. tun->attached = 1;
  450. strcpy(ifr->ifr_name, tun->dev->name);
  451. return 0;
  452. err_free_dev:
  453. free_netdev(dev);
  454. failed:
  455. return err;
  456. }
  457. static int tun_chr_ioctl(struct inode *inode, struct file *file,
  458. unsigned int cmd, unsigned long arg)
  459. {
  460. struct tun_struct *tun = file->private_data;
  461. void __user* argp = (void __user*)arg;
  462. struct ifreq ifr;
  463. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
  464. if (copy_from_user(&ifr, argp, sizeof ifr))
  465. return -EFAULT;
  466. if (cmd == TUNSETIFF && !tun) {
  467. int err;
  468. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  469. rtnl_lock();
  470. err = tun_set_iff(file, &ifr);
  471. rtnl_unlock();
  472. if (err)
  473. return err;
  474. if (copy_to_user(argp, &ifr, sizeof(ifr)))
  475. return -EFAULT;
  476. return 0;
  477. }
  478. if (!tun)
  479. return -EBADFD;
  480. DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
  481. switch (cmd) {
  482. case TUNSETNOCSUM:
  483. /* Disable/Enable checksum */
  484. if (arg)
  485. tun->flags |= TUN_NOCHECKSUM;
  486. else
  487. tun->flags &= ~TUN_NOCHECKSUM;
  488. DBG(KERN_INFO "%s: checksum %s\n",
  489. tun->dev->name, arg ? "disabled" : "enabled");
  490. break;
  491. case TUNSETPERSIST:
  492. /* Disable/Enable persist mode */
  493. if (arg)
  494. tun->flags |= TUN_PERSIST;
  495. else
  496. tun->flags &= ~TUN_PERSIST;
  497. DBG(KERN_INFO "%s: persist %s\n",
  498. tun->dev->name, arg ? "disabled" : "enabled");
  499. break;
  500. case TUNSETOWNER:
  501. /* Set owner of the device */
  502. tun->owner = (uid_t) arg;
  503. DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
  504. break;
  505. case TUNSETLINK:
  506. /* Only allow setting the type when the interface is down */
  507. if (tun->dev->flags & IFF_UP) {
  508. DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
  509. tun->dev->name);
  510. return -EBUSY;
  511. } else {
  512. tun->dev->type = (int) arg;
  513. DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
  514. }
  515. break;
  516. #ifdef TUN_DEBUG
  517. case TUNSETDEBUG:
  518. tun->debug = arg;
  519. break;
  520. #endif
  521. case SIOCGIFFLAGS:
  522. ifr.ifr_flags = tun->if_flags;
  523. if (copy_to_user( argp, &ifr, sizeof ifr))
  524. return -EFAULT;
  525. return 0;
  526. case SIOCSIFFLAGS:
  527. /** Set the character device's interface flags. Currently only
  528. * IFF_PROMISC and IFF_ALLMULTI are used. */
  529. tun->if_flags = ifr.ifr_flags;
  530. DBG(KERN_INFO "%s: interface flags 0x%lx\n",
  531. tun->dev->name, tun->if_flags);
  532. return 0;
  533. case SIOCGIFHWADDR:
  534. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
  535. min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
  536. if (copy_to_user( argp, &ifr, sizeof ifr))
  537. return -EFAULT;
  538. return 0;
  539. case SIOCSIFHWADDR:
  540. /** Set the character device's hardware address. This is used when
  541. * filtering packets being sent from the network device to the character
  542. * device. */
  543. memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
  544. min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
  545. DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
  546. tun->dev->name,
  547. tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
  548. tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
  549. return 0;
  550. case SIOCADDMULTI:
  551. /** Add the specified group to the character device's multicast filter
  552. * list. */
  553. add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
  554. DBG(KERN_DEBUG "%s: add multi: %x:%x:%x:%x:%x:%x\n",
  555. tun->dev->name,
  556. (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
  557. (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
  558. (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
  559. return 0;
  560. case SIOCDELMULTI:
  561. /** Remove the specified group from the character device's multicast
  562. * filter list. */
  563. del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
  564. DBG(KERN_DEBUG "%s: del 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. default:
  571. return -EINVAL;
  572. };
  573. return 0;
  574. }
  575. static int tun_chr_fasync(int fd, struct file *file, int on)
  576. {
  577. struct tun_struct *tun = file->private_data;
  578. int ret;
  579. if (!tun)
  580. return -EBADFD;
  581. DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
  582. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  583. return ret;
  584. if (on) {
  585. ret = f_setown(file, current->pid, 0);
  586. if (ret)
  587. return ret;
  588. tun->flags |= TUN_FASYNC;
  589. } else
  590. tun->flags &= ~TUN_FASYNC;
  591. return 0;
  592. }
  593. static int tun_chr_open(struct inode *inode, struct file * file)
  594. {
  595. DBG1(KERN_INFO "tunX: tun_chr_open\n");
  596. file->private_data = NULL;
  597. return 0;
  598. }
  599. static int tun_chr_close(struct inode *inode, struct file *file)
  600. {
  601. struct tun_struct *tun = file->private_data;
  602. if (!tun)
  603. return 0;
  604. DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
  605. tun_chr_fasync(-1, file, 0);
  606. rtnl_lock();
  607. /* Detach from net device */
  608. file->private_data = NULL;
  609. tun->attached = 0;
  610. /* Drop read queue */
  611. skb_queue_purge(&tun->readq);
  612. if (!(tun->flags & TUN_PERSIST)) {
  613. list_del(&tun->list);
  614. unregister_netdevice(tun->dev);
  615. }
  616. rtnl_unlock();
  617. return 0;
  618. }
  619. static struct file_operations tun_fops = {
  620. .owner = THIS_MODULE,
  621. .llseek = no_llseek,
  622. .read = tun_chr_read,
  623. .readv = tun_chr_readv,
  624. .write = tun_chr_write,
  625. .writev = tun_chr_writev,
  626. .poll = tun_chr_poll,
  627. .ioctl = tun_chr_ioctl,
  628. .open = tun_chr_open,
  629. .release = tun_chr_close,
  630. .fasync = tun_chr_fasync
  631. };
  632. static struct miscdevice tun_miscdev = {
  633. .minor = TUN_MINOR,
  634. .name = "tun",
  635. .fops = &tun_fops,
  636. .devfs_name = "net/tun",
  637. };
  638. /* ethtool interface */
  639. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  640. {
  641. cmd->supported = 0;
  642. cmd->advertising = 0;
  643. cmd->speed = SPEED_10;
  644. cmd->duplex = DUPLEX_FULL;
  645. cmd->port = PORT_TP;
  646. cmd->phy_address = 0;
  647. cmd->transceiver = XCVR_INTERNAL;
  648. cmd->autoneg = AUTONEG_DISABLE;
  649. cmd->maxtxpkt = 0;
  650. cmd->maxrxpkt = 0;
  651. return 0;
  652. }
  653. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  654. {
  655. struct tun_struct *tun = netdev_priv(dev);
  656. strcpy(info->driver, DRV_NAME);
  657. strcpy(info->version, DRV_VERSION);
  658. strcpy(info->fw_version, "N/A");
  659. switch (tun->flags & TUN_TYPE_MASK) {
  660. case TUN_TUN_DEV:
  661. strcpy(info->bus_info, "tun");
  662. break;
  663. case TUN_TAP_DEV:
  664. strcpy(info->bus_info, "tap");
  665. break;
  666. }
  667. }
  668. static u32 tun_get_msglevel(struct net_device *dev)
  669. {
  670. #ifdef TUN_DEBUG
  671. struct tun_struct *tun = netdev_priv(dev);
  672. return tun->debug;
  673. #else
  674. return -EOPNOTSUPP;
  675. #endif
  676. }
  677. static void tun_set_msglevel(struct net_device *dev, u32 value)
  678. {
  679. #ifdef TUN_DEBUG
  680. struct tun_struct *tun = netdev_priv(dev);
  681. tun->debug = value;
  682. #endif
  683. }
  684. static u32 tun_get_link(struct net_device *dev)
  685. {
  686. struct tun_struct *tun = netdev_priv(dev);
  687. return tun->attached;
  688. }
  689. static u32 tun_get_rx_csum(struct net_device *dev)
  690. {
  691. struct tun_struct *tun = netdev_priv(dev);
  692. return (tun->flags & TUN_NOCHECKSUM) == 0;
  693. }
  694. static int tun_set_rx_csum(struct net_device *dev, u32 data)
  695. {
  696. struct tun_struct *tun = netdev_priv(dev);
  697. if (data)
  698. tun->flags &= ~TUN_NOCHECKSUM;
  699. else
  700. tun->flags |= TUN_NOCHECKSUM;
  701. return 0;
  702. }
  703. static struct ethtool_ops tun_ethtool_ops = {
  704. .get_settings = tun_get_settings,
  705. .get_drvinfo = tun_get_drvinfo,
  706. .get_msglevel = tun_get_msglevel,
  707. .set_msglevel = tun_set_msglevel,
  708. .get_link = tun_get_link,
  709. .get_rx_csum = tun_get_rx_csum,
  710. .set_rx_csum = tun_set_rx_csum
  711. };
  712. static int __init tun_init(void)
  713. {
  714. int ret = 0;
  715. printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  716. printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
  717. ret = misc_register(&tun_miscdev);
  718. if (ret)
  719. printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
  720. return ret;
  721. }
  722. static void tun_cleanup(void)
  723. {
  724. struct tun_struct *tun, *nxt;
  725. misc_deregister(&tun_miscdev);
  726. rtnl_lock();
  727. list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) {
  728. DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
  729. unregister_netdevice(tun->dev);
  730. }
  731. rtnl_unlock();
  732. }
  733. module_init(tun_init);
  734. module_exit(tun_cleanup);
  735. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  736. MODULE_AUTHOR(DRV_COPYRIGHT);
  737. MODULE_LICENSE("GPL");
  738. MODULE_ALIAS_MISCDEV(TUN_MINOR);