tun.c 22 KB

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