tun.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. * Brian Braunstein <linuxkernel@bristyle.com> 2007/03/23
  21. * Fixed hw address handling. Now net_device.dev_addr is kept consistent
  22. * with tun.dev_addr when the address is set by this module.
  23. *
  24. * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
  25. * Add TUNSETLINK ioctl to set the link encapsulation
  26. *
  27. * Mark Smith <markzzzsmith@yahoo.com.au>
  28. * Use random_ether_addr() for tap MAC address.
  29. *
  30. * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
  31. * Fixes in packet dropping, queue length setting and queue wakeup.
  32. * Increased default tx queue length.
  33. * Added ethtool API.
  34. * Minor cleanups
  35. *
  36. * Daniel Podlejski <underley@underley.eu.org>
  37. * Modifications for 2.3.99-pre5 kernel.
  38. */
  39. #define DRV_NAME "tun"
  40. #define DRV_VERSION "1.6"
  41. #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
  42. #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
  43. #include <linux/module.h>
  44. #include <linux/errno.h>
  45. #include <linux/kernel.h>
  46. #include <linux/major.h>
  47. #include <linux/slab.h>
  48. #include <linux/poll.h>
  49. #include <linux/fcntl.h>
  50. #include <linux/init.h>
  51. #include <linux/skbuff.h>
  52. #include <linux/netdevice.h>
  53. #include <linux/etherdevice.h>
  54. #include <linux/miscdevice.h>
  55. #include <linux/ethtool.h>
  56. #include <linux/rtnetlink.h>
  57. #include <linux/if.h>
  58. #include <linux/if_arp.h>
  59. #include <linux/if_ether.h>
  60. #include <linux/if_tun.h>
  61. #include <linux/crc32.h>
  62. #include <net/net_namespace.h>
  63. #include <asm/system.h>
  64. #include <asm/uaccess.h>
  65. #ifdef TUN_DEBUG
  66. static int debug;
  67. #endif
  68. /* Network device part of the driver */
  69. static LIST_HEAD(tun_dev_list);
  70. static const struct ethtool_ops tun_ethtool_ops;
  71. /* Net device open. */
  72. static int tun_net_open(struct net_device *dev)
  73. {
  74. netif_start_queue(dev);
  75. return 0;
  76. }
  77. /* Net device close. */
  78. static int tun_net_close(struct net_device *dev)
  79. {
  80. netif_stop_queue(dev);
  81. return 0;
  82. }
  83. /* Net device start xmit */
  84. static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
  85. {
  86. struct tun_struct *tun = netdev_priv(dev);
  87. DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
  88. /* Drop packet if interface is not attached */
  89. if (!tun->attached)
  90. goto drop;
  91. /* Packet dropping */
  92. if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
  93. if (!(tun->flags & TUN_ONE_QUEUE)) {
  94. /* Normal queueing mode. */
  95. /* Packet scheduler handles dropping of further packets. */
  96. netif_stop_queue(dev);
  97. /* We won't see all dropped packets individually, so overrun
  98. * error is more appropriate. */
  99. dev->stats.tx_fifo_errors++;
  100. } else {
  101. /* Single queue mode.
  102. * Driver handles dropping of all packets itself. */
  103. goto drop;
  104. }
  105. }
  106. /* Queue packet */
  107. skb_queue_tail(&tun->readq, skb);
  108. dev->trans_start = jiffies;
  109. /* Notify and wake up reader process */
  110. if (tun->flags & TUN_FASYNC)
  111. kill_fasync(&tun->fasync, SIGIO, POLL_IN);
  112. wake_up_interruptible(&tun->read_wait);
  113. return 0;
  114. drop:
  115. dev->stats.tx_dropped++;
  116. kfree_skb(skb);
  117. return 0;
  118. }
  119. /** Add the specified Ethernet address to this multicast filter. */
  120. static void
  121. add_multi(u32* filter, const u8* addr)
  122. {
  123. int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
  124. filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  125. }
  126. /** Remove the specified Ethernet addres from this multicast filter. */
  127. static void
  128. del_multi(u32* filter, const u8* addr)
  129. {
  130. int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
  131. filter[bit_nr >> 5] &= ~(1 << (bit_nr & 31));
  132. }
  133. /** Update the list of multicast groups to which the network device belongs.
  134. * This list is used to filter packets being sent from the character device to
  135. * the network device. */
  136. static void
  137. tun_net_mclist(struct net_device *dev)
  138. {
  139. struct tun_struct *tun = netdev_priv(dev);
  140. const struct dev_mc_list *mclist;
  141. int i;
  142. DECLARE_MAC_BUF(mac);
  143. DBG(KERN_DEBUG "%s: tun_net_mclist: mc_count %d\n",
  144. dev->name, dev->mc_count);
  145. memset(tun->chr_filter, 0, sizeof tun->chr_filter);
  146. for (i = 0, mclist = dev->mc_list; i < dev->mc_count && mclist != NULL;
  147. i++, mclist = mclist->next) {
  148. add_multi(tun->net_filter, mclist->dmi_addr);
  149. DBG(KERN_DEBUG "%s: tun_net_mclist: %s\n",
  150. dev->name, print_mac(mac, mclist->dmi_addr));
  151. }
  152. }
  153. #define MIN_MTU 68
  154. #define MAX_MTU 65535
  155. static int
  156. tun_net_change_mtu(struct net_device *dev, int new_mtu)
  157. {
  158. if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
  159. return -EINVAL;
  160. dev->mtu = new_mtu;
  161. return 0;
  162. }
  163. /* Initialize net device. */
  164. static void tun_net_init(struct net_device *dev)
  165. {
  166. struct tun_struct *tun = netdev_priv(dev);
  167. switch (tun->flags & TUN_TYPE_MASK) {
  168. case TUN_TUN_DEV:
  169. /* Point-to-Point TUN Device */
  170. dev->hard_header_len = 0;
  171. dev->addr_len = 0;
  172. dev->mtu = 1500;
  173. dev->change_mtu = tun_net_change_mtu;
  174. /* Zero header length */
  175. dev->type = ARPHRD_NONE;
  176. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  177. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  178. break;
  179. case TUN_TAP_DEV:
  180. /* Ethernet TAP Device */
  181. dev->set_multicast_list = tun_net_mclist;
  182. ether_setup(dev);
  183. dev->change_mtu = tun_net_change_mtu;
  184. /* random address already created for us by tun_set_iff, use it */
  185. memcpy(dev->dev_addr, tun->dev_addr, min(sizeof(tun->dev_addr), sizeof(dev->dev_addr)) );
  186. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  187. break;
  188. }
  189. }
  190. /* Character device part */
  191. /* Poll */
  192. static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
  193. {
  194. struct tun_struct *tun = file->private_data;
  195. unsigned int mask = POLLOUT | POLLWRNORM;
  196. if (!tun)
  197. return -EBADFD;
  198. DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
  199. poll_wait(file, &tun->read_wait, wait);
  200. if (!skb_queue_empty(&tun->readq))
  201. mask |= POLLIN | POLLRDNORM;
  202. return mask;
  203. }
  204. /* Get packet from user space buffer */
  205. static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
  206. {
  207. struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
  208. struct sk_buff *skb;
  209. size_t len = count, align = 0;
  210. if (!(tun->flags & TUN_NO_PI)) {
  211. if ((len -= sizeof(pi)) > count)
  212. return -EINVAL;
  213. if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
  214. return -EFAULT;
  215. }
  216. if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV)
  217. align = NET_IP_ALIGN;
  218. if (!(skb = alloc_skb(len + align, GFP_KERNEL))) {
  219. tun->dev->stats.rx_dropped++;
  220. return -ENOMEM;
  221. }
  222. if (align)
  223. skb_reserve(skb, align);
  224. if (memcpy_fromiovec(skb_put(skb, len), iv, len)) {
  225. tun->dev->stats.rx_dropped++;
  226. kfree_skb(skb);
  227. return -EFAULT;
  228. }
  229. switch (tun->flags & TUN_TYPE_MASK) {
  230. case TUN_TUN_DEV:
  231. skb_reset_mac_header(skb);
  232. skb->protocol = pi.proto;
  233. skb->dev = tun->dev;
  234. break;
  235. case TUN_TAP_DEV:
  236. skb->protocol = eth_type_trans(skb, tun->dev);
  237. break;
  238. };
  239. if (tun->flags & TUN_NOCHECKSUM)
  240. skb->ip_summed = CHECKSUM_UNNECESSARY;
  241. netif_rx_ni(skb);
  242. tun->dev->last_rx = jiffies;
  243. tun->dev->stats.rx_packets++;
  244. tun->dev->stats.rx_bytes += len;
  245. return count;
  246. }
  247. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  248. unsigned long count, loff_t pos)
  249. {
  250. struct tun_struct *tun = iocb->ki_filp->private_data;
  251. if (!tun)
  252. return -EBADFD;
  253. DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
  254. return tun_get_user(tun, (struct iovec *) iv, iov_length(iv, count));
  255. }
  256. /* Put packet to the user space buffer */
  257. static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
  258. struct sk_buff *skb,
  259. struct iovec *iv, int len)
  260. {
  261. struct tun_pi pi = { 0, skb->protocol };
  262. ssize_t total = 0;
  263. if (!(tun->flags & TUN_NO_PI)) {
  264. if ((len -= sizeof(pi)) < 0)
  265. return -EINVAL;
  266. if (len < skb->len) {
  267. /* Packet will be striped */
  268. pi.flags |= TUN_PKT_STRIP;
  269. }
  270. if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
  271. return -EFAULT;
  272. total += sizeof(pi);
  273. }
  274. len = min_t(int, skb->len, len);
  275. skb_copy_datagram_iovec(skb, 0, iv, len);
  276. total += len;
  277. tun->dev->stats.tx_packets++;
  278. tun->dev->stats.tx_bytes += len;
  279. return total;
  280. }
  281. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  282. unsigned long count, loff_t pos)
  283. {
  284. struct file *file = iocb->ki_filp;
  285. struct tun_struct *tun = file->private_data;
  286. DECLARE_WAITQUEUE(wait, current);
  287. struct sk_buff *skb;
  288. ssize_t len, ret = 0;
  289. DECLARE_MAC_BUF(mac);
  290. if (!tun)
  291. return -EBADFD;
  292. DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
  293. len = iov_length(iv, count);
  294. if (len < 0)
  295. return -EINVAL;
  296. add_wait_queue(&tun->read_wait, &wait);
  297. while (len) {
  298. const u8 ones[ ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  299. u8 addr[ ETH_ALEN];
  300. int bit_nr;
  301. current->state = TASK_INTERRUPTIBLE;
  302. /* Read frames from the queue */
  303. if (!(skb=skb_dequeue(&tun->readq))) {
  304. if (file->f_flags & O_NONBLOCK) {
  305. ret = -EAGAIN;
  306. break;
  307. }
  308. if (signal_pending(current)) {
  309. ret = -ERESTARTSYS;
  310. break;
  311. }
  312. /* Nothing to read, let's sleep */
  313. schedule();
  314. continue;
  315. }
  316. netif_wake_queue(tun->dev);
  317. /** Decide whether to accept this packet. This code is designed to
  318. * behave identically to an Ethernet interface. Accept the packet if
  319. * - we are promiscuous.
  320. * - the packet is addressed to us.
  321. * - the packet is broadcast.
  322. * - the packet is multicast and
  323. * - we are multicast promiscous.
  324. * - we belong to the multicast group.
  325. */
  326. skb_copy_from_linear_data(skb, addr, min_t(size_t, sizeof addr,
  327. skb->len));
  328. bit_nr = ether_crc(sizeof addr, addr) >> 26;
  329. if ((tun->if_flags & IFF_PROMISC) ||
  330. memcmp(addr, tun->dev_addr, sizeof addr) == 0 ||
  331. memcmp(addr, ones, sizeof addr) == 0 ||
  332. (((addr[0] == 1 && addr[1] == 0 && addr[2] == 0x5e) ||
  333. (addr[0] == 0x33 && addr[1] == 0x33)) &&
  334. ((tun->if_flags & IFF_ALLMULTI) ||
  335. (tun->chr_filter[bit_nr >> 5] & (1 << (bit_nr & 31)))))) {
  336. DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %s\n",
  337. tun->dev->name, print_mac(mac, addr));
  338. ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
  339. kfree_skb(skb);
  340. break;
  341. } else {
  342. DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %s\n",
  343. tun->dev->name, print_mac(mac, addr));
  344. kfree_skb(skb);
  345. continue;
  346. }
  347. }
  348. current->state = TASK_RUNNING;
  349. remove_wait_queue(&tun->read_wait, &wait);
  350. return ret;
  351. }
  352. static void tun_setup(struct net_device *dev)
  353. {
  354. struct tun_struct *tun = netdev_priv(dev);
  355. skb_queue_head_init(&tun->readq);
  356. init_waitqueue_head(&tun->read_wait);
  357. tun->owner = -1;
  358. tun->group = -1;
  359. dev->open = tun_net_open;
  360. dev->hard_start_xmit = tun_net_xmit;
  361. dev->stop = tun_net_close;
  362. dev->ethtool_ops = &tun_ethtool_ops;
  363. dev->destructor = free_netdev;
  364. }
  365. static struct tun_struct *tun_get_by_name(const char *name)
  366. {
  367. struct tun_struct *tun;
  368. ASSERT_RTNL();
  369. list_for_each_entry(tun, &tun_dev_list, list) {
  370. if (!strncmp(tun->dev->name, name, IFNAMSIZ))
  371. return tun;
  372. }
  373. return NULL;
  374. }
  375. static int tun_set_iff(struct file *file, struct ifreq *ifr)
  376. {
  377. struct tun_struct *tun;
  378. struct net_device *dev;
  379. int err;
  380. tun = tun_get_by_name(ifr->ifr_name);
  381. if (tun) {
  382. if (tun->attached)
  383. return -EBUSY;
  384. /* Check permissions */
  385. if (((tun->owner != -1 &&
  386. current->euid != tun->owner) ||
  387. (tun->group != -1 &&
  388. current->egid != tun->group)) &&
  389. !capable(CAP_NET_ADMIN))
  390. return -EPERM;
  391. }
  392. else if (__dev_get_by_name(&init_net, ifr->ifr_name))
  393. return -EINVAL;
  394. else {
  395. char *name;
  396. unsigned long flags = 0;
  397. err = -EINVAL;
  398. if (!capable(CAP_NET_ADMIN))
  399. return -EPERM;
  400. /* Set dev type */
  401. if (ifr->ifr_flags & IFF_TUN) {
  402. /* TUN device */
  403. flags |= TUN_TUN_DEV;
  404. name = "tun%d";
  405. } else if (ifr->ifr_flags & IFF_TAP) {
  406. /* TAP device */
  407. flags |= TUN_TAP_DEV;
  408. name = "tap%d";
  409. } else
  410. goto failed;
  411. if (*ifr->ifr_name)
  412. name = ifr->ifr_name;
  413. dev = alloc_netdev(sizeof(struct tun_struct), name,
  414. tun_setup);
  415. if (!dev)
  416. return -ENOMEM;
  417. tun = netdev_priv(dev);
  418. tun->dev = dev;
  419. tun->flags = flags;
  420. /* Be promiscuous by default to maintain previous behaviour. */
  421. tun->if_flags = IFF_PROMISC;
  422. /* Generate random Ethernet address. */
  423. *(__be16 *)tun->dev_addr = htons(0x00FF);
  424. get_random_bytes(tun->dev_addr + sizeof(u16), 4);
  425. memset(tun->chr_filter, 0, sizeof tun->chr_filter);
  426. tun_net_init(dev);
  427. if (strchr(dev->name, '%')) {
  428. err = dev_alloc_name(dev, dev->name);
  429. if (err < 0)
  430. goto err_free_dev;
  431. }
  432. err = register_netdevice(tun->dev);
  433. if (err < 0)
  434. goto err_free_dev;
  435. list_add(&tun->list, &tun_dev_list);
  436. }
  437. DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
  438. if (ifr->ifr_flags & IFF_NO_PI)
  439. tun->flags |= TUN_NO_PI;
  440. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  441. tun->flags |= TUN_ONE_QUEUE;
  442. file->private_data = tun;
  443. tun->attached = 1;
  444. strcpy(ifr->ifr_name, tun->dev->name);
  445. return 0;
  446. err_free_dev:
  447. free_netdev(dev);
  448. failed:
  449. return err;
  450. }
  451. static int tun_chr_ioctl(struct inode *inode, struct file *file,
  452. unsigned int cmd, unsigned long arg)
  453. {
  454. struct tun_struct *tun = file->private_data;
  455. void __user* argp = (void __user*)arg;
  456. struct ifreq ifr;
  457. DECLARE_MAC_BUF(mac);
  458. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
  459. if (copy_from_user(&ifr, argp, sizeof ifr))
  460. return -EFAULT;
  461. if (cmd == TUNSETIFF && !tun) {
  462. int err;
  463. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  464. rtnl_lock();
  465. err = tun_set_iff(file, &ifr);
  466. rtnl_unlock();
  467. if (err)
  468. return err;
  469. if (copy_to_user(argp, &ifr, sizeof(ifr)))
  470. return -EFAULT;
  471. return 0;
  472. }
  473. if (!tun)
  474. return -EBADFD;
  475. DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
  476. switch (cmd) {
  477. case TUNSETNOCSUM:
  478. /* Disable/Enable checksum */
  479. if (arg)
  480. tun->flags |= TUN_NOCHECKSUM;
  481. else
  482. tun->flags &= ~TUN_NOCHECKSUM;
  483. DBG(KERN_INFO "%s: checksum %s\n",
  484. tun->dev->name, arg ? "disabled" : "enabled");
  485. break;
  486. case TUNSETPERSIST:
  487. /* Disable/Enable persist mode */
  488. if (arg)
  489. tun->flags |= TUN_PERSIST;
  490. else
  491. tun->flags &= ~TUN_PERSIST;
  492. DBG(KERN_INFO "%s: persist %s\n",
  493. tun->dev->name, arg ? "enabled" : "disabled");
  494. break;
  495. case TUNSETOWNER:
  496. /* Set owner of the device */
  497. tun->owner = (uid_t) arg;
  498. DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
  499. break;
  500. case TUNSETGROUP:
  501. /* Set group of the device */
  502. tun->group= (gid_t) arg;
  503. DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
  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. /* Note: the actual net device's address may be different */
  535. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
  536. min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
  537. if (copy_to_user( argp, &ifr, sizeof ifr))
  538. return -EFAULT;
  539. return 0;
  540. case SIOCSIFHWADDR:
  541. {
  542. /* try to set the actual net device's hw address */
  543. int ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  544. if (ret == 0) {
  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. }
  555. return ret;
  556. }
  557. case SIOCADDMULTI:
  558. /** Add the specified group to the character device's multicast filter
  559. * list. */
  560. add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
  561. DBG(KERN_DEBUG "%s: add multi: %s\n",
  562. tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
  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: %s\n",
  569. tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
  570. return 0;
  571. default:
  572. return -EINVAL;
  573. };
  574. return 0;
  575. }
  576. static int tun_chr_fasync(int fd, struct file *file, int on)
  577. {
  578. struct tun_struct *tun = file->private_data;
  579. int ret;
  580. if (!tun)
  581. return -EBADFD;
  582. DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
  583. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  584. return ret;
  585. if (on) {
  586. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  587. if (ret)
  588. return ret;
  589. tun->flags |= TUN_FASYNC;
  590. } else
  591. tun->flags &= ~TUN_FASYNC;
  592. return 0;
  593. }
  594. static int tun_chr_open(struct inode *inode, struct file * file)
  595. {
  596. DBG1(KERN_INFO "tunX: tun_chr_open\n");
  597. file->private_data = NULL;
  598. return 0;
  599. }
  600. static int tun_chr_close(struct inode *inode, struct file *file)
  601. {
  602. struct tun_struct *tun = file->private_data;
  603. if (!tun)
  604. return 0;
  605. DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
  606. tun_chr_fasync(-1, file, 0);
  607. rtnl_lock();
  608. /* Detach from net device */
  609. file->private_data = NULL;
  610. tun->attached = 0;
  611. /* Drop read queue */
  612. skb_queue_purge(&tun->readq);
  613. if (!(tun->flags & TUN_PERSIST)) {
  614. list_del(&tun->list);
  615. unregister_netdevice(tun->dev);
  616. }
  617. rtnl_unlock();
  618. return 0;
  619. }
  620. static const struct file_operations tun_fops = {
  621. .owner = THIS_MODULE,
  622. .llseek = no_llseek,
  623. .read = do_sync_read,
  624. .aio_read = tun_chr_aio_read,
  625. .write = do_sync_write,
  626. .aio_write = tun_chr_aio_write,
  627. .poll = tun_chr_poll,
  628. .ioctl = tun_chr_ioctl,
  629. .open = tun_chr_open,
  630. .release = tun_chr_close,
  631. .fasync = tun_chr_fasync
  632. };
  633. static struct miscdevice tun_miscdev = {
  634. .minor = TUN_MINOR,
  635. .name = "tun",
  636. .fops = &tun_fops,
  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 const 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);