tun.c 23 KB

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