tun.c 23 KB

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