tun.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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/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. /* Writev */
  244. static ssize_t tun_chr_writev(struct file * file, const struct iovec *iv,
  245. unsigned long count, loff_t *pos)
  246. {
  247. struct tun_struct *tun = file->private_data;
  248. if (!tun)
  249. return -EBADFD;
  250. DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
  251. return tun_get_user(tun, (struct iovec *) iv, iov_total(iv, count));
  252. }
  253. /* Write */
  254. static ssize_t tun_chr_write(struct file * file, const char __user * buf,
  255. size_t count, loff_t *pos)
  256. {
  257. struct iovec iv = { (void __user *) buf, count };
  258. return tun_chr_writev(file, &iv, 1, pos);
  259. }
  260. /* Put packet to the user space buffer */
  261. static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
  262. struct sk_buff *skb,
  263. struct iovec *iv, int len)
  264. {
  265. struct tun_pi pi = { 0, skb->protocol };
  266. ssize_t total = 0;
  267. if (!(tun->flags & TUN_NO_PI)) {
  268. if ((len -= sizeof(pi)) < 0)
  269. return -EINVAL;
  270. if (len < skb->len) {
  271. /* Packet will be striped */
  272. pi.flags |= TUN_PKT_STRIP;
  273. }
  274. if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
  275. return -EFAULT;
  276. total += sizeof(pi);
  277. }
  278. len = min_t(int, skb->len, len);
  279. skb_copy_datagram_iovec(skb, 0, iv, len);
  280. total += len;
  281. tun->stats.tx_packets++;
  282. tun->stats.tx_bytes += len;
  283. return total;
  284. }
  285. /* Readv */
  286. static ssize_t tun_chr_readv(struct file *file, const struct iovec *iv,
  287. unsigned long count, loff_t *pos)
  288. {
  289. struct tun_struct *tun = file->private_data;
  290. DECLARE_WAITQUEUE(wait, current);
  291. struct sk_buff *skb;
  292. ssize_t len, ret = 0;
  293. if (!tun)
  294. return -EBADFD;
  295. DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
  296. len = iov_total(iv, count);
  297. if (len < 0)
  298. return -EINVAL;
  299. add_wait_queue(&tun->read_wait, &wait);
  300. while (len) {
  301. const u8 ones[ ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  302. u8 addr[ ETH_ALEN];
  303. int bit_nr;
  304. current->state = TASK_INTERRUPTIBLE;
  305. /* Read frames from the queue */
  306. if (!(skb=skb_dequeue(&tun->readq))) {
  307. if (file->f_flags & O_NONBLOCK) {
  308. ret = -EAGAIN;
  309. break;
  310. }
  311. if (signal_pending(current)) {
  312. ret = -ERESTARTSYS;
  313. break;
  314. }
  315. /* Nothing to read, let's sleep */
  316. schedule();
  317. continue;
  318. }
  319. netif_wake_queue(tun->dev);
  320. /** Decide whether to accept this packet. This code is designed to
  321. * behave identically to an Ethernet interface. Accept the packet if
  322. * - we are promiscuous.
  323. * - the packet is addressed to us.
  324. * - the packet is broadcast.
  325. * - the packet is multicast and
  326. * - we are multicast promiscous.
  327. * - we belong to the multicast group.
  328. */
  329. memcpy(addr, skb->data,
  330. min_t(size_t, sizeof addr, skb->len));
  331. bit_nr = ether_crc(sizeof addr, addr) >> 26;
  332. if ((tun->if_flags & IFF_PROMISC) ||
  333. memcmp(addr, tun->dev_addr, sizeof addr) == 0 ||
  334. memcmp(addr, ones, sizeof addr) == 0 ||
  335. (((addr[0] == 1 && addr[1] == 0 && addr[2] == 0x5e) ||
  336. (addr[0] == 0x33 && addr[1] == 0x33)) &&
  337. ((tun->if_flags & IFF_ALLMULTI) ||
  338. (tun->chr_filter[bit_nr >> 5] & (1 << (bit_nr & 31)))))) {
  339. DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %x:%x:%x:%x:%x:%x\n",
  340. tun->dev->name, addr[0], addr[1], addr[2],
  341. addr[3], addr[4], addr[5]);
  342. ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
  343. kfree_skb(skb);
  344. break;
  345. } else {
  346. DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %x:%x:%x:%x:%x:%x\n",
  347. tun->dev->name, addr[0], addr[1], addr[2],
  348. addr[3], addr[4], addr[5]);
  349. kfree_skb(skb);
  350. continue;
  351. }
  352. }
  353. current->state = TASK_RUNNING;
  354. remove_wait_queue(&tun->read_wait, &wait);
  355. return ret;
  356. }
  357. /* Read */
  358. static ssize_t tun_chr_read(struct file * file, char __user * buf,
  359. size_t count, loff_t *pos)
  360. {
  361. struct iovec iv = { buf, count };
  362. return tun_chr_readv(file, &iv, 1, pos);
  363. }
  364. static void tun_setup(struct net_device *dev)
  365. {
  366. struct tun_struct *tun = netdev_priv(dev);
  367. skb_queue_head_init(&tun->readq);
  368. init_waitqueue_head(&tun->read_wait);
  369. tun->owner = -1;
  370. SET_MODULE_OWNER(dev);
  371. dev->open = tun_net_open;
  372. dev->hard_start_xmit = tun_net_xmit;
  373. dev->stop = tun_net_close;
  374. dev->get_stats = tun_net_stats;
  375. dev->ethtool_ops = &tun_ethtool_ops;
  376. dev->destructor = free_netdev;
  377. }
  378. static struct tun_struct *tun_get_by_name(const char *name)
  379. {
  380. struct tun_struct *tun;
  381. ASSERT_RTNL();
  382. list_for_each_entry(tun, &tun_dev_list, list) {
  383. if (!strncmp(tun->dev->name, name, IFNAMSIZ))
  384. return tun;
  385. }
  386. return NULL;
  387. }
  388. static int tun_set_iff(struct file *file, struct ifreq *ifr)
  389. {
  390. struct tun_struct *tun;
  391. struct net_device *dev;
  392. int err;
  393. tun = tun_get_by_name(ifr->ifr_name);
  394. if (tun) {
  395. if (tun->attached)
  396. return -EBUSY;
  397. /* Check permissions */
  398. if (tun->owner != -1 &&
  399. current->euid != tun->owner && !capable(CAP_NET_ADMIN))
  400. return -EPERM;
  401. }
  402. else if (__dev_get_by_name(ifr->ifr_name))
  403. return -EINVAL;
  404. else {
  405. char *name;
  406. unsigned long flags = 0;
  407. err = -EINVAL;
  408. if (!capable(CAP_NET_ADMIN))
  409. return -EPERM;
  410. /* Set dev type */
  411. if (ifr->ifr_flags & IFF_TUN) {
  412. /* TUN device */
  413. flags |= TUN_TUN_DEV;
  414. name = "tun%d";
  415. } else if (ifr->ifr_flags & IFF_TAP) {
  416. /* TAP device */
  417. flags |= TUN_TAP_DEV;
  418. name = "tap%d";
  419. } else
  420. goto failed;
  421. if (*ifr->ifr_name)
  422. name = ifr->ifr_name;
  423. dev = alloc_netdev(sizeof(struct tun_struct), name,
  424. tun_setup);
  425. if (!dev)
  426. return -ENOMEM;
  427. tun = netdev_priv(dev);
  428. tun->dev = dev;
  429. tun->flags = flags;
  430. /* Be promiscuous by default to maintain previous behaviour. */
  431. tun->if_flags = IFF_PROMISC;
  432. /* Generate random Ethernet address. */
  433. *(u16 *)tun->dev_addr = htons(0x00FF);
  434. get_random_bytes(tun->dev_addr + sizeof(u16), 4);
  435. memset(tun->chr_filter, 0, sizeof tun->chr_filter);
  436. tun_net_init(dev);
  437. if (strchr(dev->name, '%')) {
  438. err = dev_alloc_name(dev, dev->name);
  439. if (err < 0)
  440. goto err_free_dev;
  441. }
  442. err = register_netdevice(tun->dev);
  443. if (err < 0)
  444. goto err_free_dev;
  445. list_add(&tun->list, &tun_dev_list);
  446. }
  447. DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
  448. if (ifr->ifr_flags & IFF_NO_PI)
  449. tun->flags |= TUN_NO_PI;
  450. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  451. tun->flags |= TUN_ONE_QUEUE;
  452. file->private_data = tun;
  453. tun->attached = 1;
  454. strcpy(ifr->ifr_name, tun->dev->name);
  455. return 0;
  456. err_free_dev:
  457. free_netdev(dev);
  458. failed:
  459. return err;
  460. }
  461. static int tun_chr_ioctl(struct inode *inode, struct file *file,
  462. unsigned int cmd, unsigned long arg)
  463. {
  464. struct tun_struct *tun = file->private_data;
  465. void __user* argp = (void __user*)arg;
  466. struct ifreq ifr;
  467. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
  468. if (copy_from_user(&ifr, argp, sizeof ifr))
  469. return -EFAULT;
  470. if (cmd == TUNSETIFF && !tun) {
  471. int err;
  472. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  473. rtnl_lock();
  474. err = tun_set_iff(file, &ifr);
  475. rtnl_unlock();
  476. if (err)
  477. return err;
  478. if (copy_to_user(argp, &ifr, sizeof(ifr)))
  479. return -EFAULT;
  480. return 0;
  481. }
  482. if (!tun)
  483. return -EBADFD;
  484. DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
  485. switch (cmd) {
  486. case TUNSETNOCSUM:
  487. /* Disable/Enable checksum */
  488. if (arg)
  489. tun->flags |= TUN_NOCHECKSUM;
  490. else
  491. tun->flags &= ~TUN_NOCHECKSUM;
  492. DBG(KERN_INFO "%s: checksum %s\n",
  493. tun->dev->name, arg ? "disabled" : "enabled");
  494. break;
  495. case TUNSETPERSIST:
  496. /* Disable/Enable persist mode */
  497. if (arg)
  498. tun->flags |= TUN_PERSIST;
  499. else
  500. tun->flags &= ~TUN_PERSIST;
  501. DBG(KERN_INFO "%s: persist %s\n",
  502. tun->dev->name, arg ? "disabled" : "enabled");
  503. break;
  504. case TUNSETOWNER:
  505. /* Set owner of the device */
  506. tun->owner = (uid_t) arg;
  507. DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
  508. break;
  509. case TUNSETLINK:
  510. /* Only allow setting the type when the interface is down */
  511. if (tun->dev->flags & IFF_UP) {
  512. DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
  513. tun->dev->name);
  514. return -EBUSY;
  515. } else {
  516. tun->dev->type = (int) arg;
  517. DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
  518. }
  519. break;
  520. #ifdef TUN_DEBUG
  521. case TUNSETDEBUG:
  522. tun->debug = arg;
  523. break;
  524. #endif
  525. case SIOCGIFFLAGS:
  526. ifr.ifr_flags = tun->if_flags;
  527. if (copy_to_user( argp, &ifr, sizeof ifr))
  528. return -EFAULT;
  529. return 0;
  530. case SIOCSIFFLAGS:
  531. /** Set the character device's interface flags. Currently only
  532. * IFF_PROMISC and IFF_ALLMULTI are used. */
  533. tun->if_flags = ifr.ifr_flags;
  534. DBG(KERN_INFO "%s: interface flags 0x%lx\n",
  535. tun->dev->name, tun->if_flags);
  536. return 0;
  537. case SIOCGIFHWADDR:
  538. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
  539. min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
  540. if (copy_to_user( argp, &ifr, sizeof ifr))
  541. return -EFAULT;
  542. return 0;
  543. case SIOCSIFHWADDR:
  544. /** Set the character device's hardware address. This is used when
  545. * filtering packets being sent from the network device to the character
  546. * device. */
  547. memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
  548. min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
  549. DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
  550. tun->dev->name,
  551. tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
  552. tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
  553. return 0;
  554. case SIOCADDMULTI:
  555. /** Add the specified group to the character device's multicast filter
  556. * list. */
  557. add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
  558. DBG(KERN_DEBUG "%s: add multi: %x:%x:%x:%x:%x:%x\n",
  559. tun->dev->name,
  560. (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
  561. (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
  562. (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
  563. return 0;
  564. case SIOCDELMULTI:
  565. /** Remove the specified group from the character device's multicast
  566. * filter list. */
  567. del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
  568. DBG(KERN_DEBUG "%s: del multi: %x:%x:%x:%x:%x:%x\n",
  569. tun->dev->name,
  570. (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
  571. (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
  572. (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
  573. return 0;
  574. default:
  575. return -EINVAL;
  576. };
  577. return 0;
  578. }
  579. static int tun_chr_fasync(int fd, struct file *file, int on)
  580. {
  581. struct tun_struct *tun = file->private_data;
  582. int ret;
  583. if (!tun)
  584. return -EBADFD;
  585. DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
  586. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  587. return ret;
  588. if (on) {
  589. ret = f_setown(file, current->pid, 0);
  590. if (ret)
  591. return ret;
  592. tun->flags |= TUN_FASYNC;
  593. } else
  594. tun->flags &= ~TUN_FASYNC;
  595. return 0;
  596. }
  597. static int tun_chr_open(struct inode *inode, struct file * file)
  598. {
  599. DBG1(KERN_INFO "tunX: tun_chr_open\n");
  600. file->private_data = NULL;
  601. return 0;
  602. }
  603. static int tun_chr_close(struct inode *inode, struct file *file)
  604. {
  605. struct tun_struct *tun = file->private_data;
  606. if (!tun)
  607. return 0;
  608. DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
  609. tun_chr_fasync(-1, file, 0);
  610. rtnl_lock();
  611. /* Detach from net device */
  612. file->private_data = NULL;
  613. tun->attached = 0;
  614. /* Drop read queue */
  615. skb_queue_purge(&tun->readq);
  616. if (!(tun->flags & TUN_PERSIST)) {
  617. list_del(&tun->list);
  618. unregister_netdevice(tun->dev);
  619. }
  620. rtnl_unlock();
  621. return 0;
  622. }
  623. static struct file_operations tun_fops = {
  624. .owner = THIS_MODULE,
  625. .llseek = no_llseek,
  626. .read = tun_chr_read,
  627. .readv = tun_chr_readv,
  628. .write = tun_chr_write,
  629. .writev = tun_chr_writev,
  630. .poll = tun_chr_poll,
  631. .ioctl = tun_chr_ioctl,
  632. .open = tun_chr_open,
  633. .release = tun_chr_close,
  634. .fasync = tun_chr_fasync
  635. };
  636. static struct miscdevice tun_miscdev = {
  637. .minor = TUN_MINOR,
  638. .name = "tun",
  639. .fops = &tun_fops,
  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 const 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);