tun.c 23 KB

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