tun.c 24 KB

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