tun.c 22 KB

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