tun.c 21 KB

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