tun.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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. /* Only allow setting the type when the interface is down */
  549. if (tun->dev->flags & IFF_UP) {
  550. DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
  551. tun->dev->name);
  552. return -EBUSY;
  553. } else {
  554. tun->dev->type = (int) arg;
  555. DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
  556. }
  557. break;
  558. #ifdef TUN_DEBUG
  559. case TUNSETDEBUG:
  560. tun->debug = arg;
  561. break;
  562. #endif
  563. case SIOCGIFFLAGS:
  564. ifr.ifr_flags = tun->if_flags;
  565. if (copy_to_user( argp, &ifr, sizeof ifr))
  566. return -EFAULT;
  567. return 0;
  568. case SIOCSIFFLAGS:
  569. /** Set the character device's interface flags. Currently only
  570. * IFF_PROMISC and IFF_ALLMULTI are used. */
  571. tun->if_flags = ifr.ifr_flags;
  572. DBG(KERN_INFO "%s: interface flags 0x%lx\n",
  573. tun->dev->name, tun->if_flags);
  574. return 0;
  575. case SIOCGIFHWADDR:
  576. /* Note: the actual net device's address may be different */
  577. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
  578. min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
  579. if (copy_to_user( argp, &ifr, sizeof ifr))
  580. return -EFAULT;
  581. return 0;
  582. case SIOCSIFHWADDR:
  583. {
  584. /* try to set the actual net device's hw address */
  585. int ret;
  586. rtnl_lock();
  587. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  588. rtnl_unlock();
  589. if (ret == 0) {
  590. /** Set the character device's hardware address. This is used when
  591. * filtering packets being sent from the network device to the character
  592. * device. */
  593. memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
  594. min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
  595. DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
  596. tun->dev->name,
  597. tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
  598. tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
  599. }
  600. return ret;
  601. }
  602. case SIOCADDMULTI:
  603. /** Add the specified group to the character device's multicast filter
  604. * list. */
  605. add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
  606. DBG(KERN_DEBUG "%s: add multi: %s\n",
  607. tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
  608. return 0;
  609. case SIOCDELMULTI:
  610. /** Remove the specified group from the character device's multicast
  611. * filter list. */
  612. del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
  613. DBG(KERN_DEBUG "%s: del multi: %s\n",
  614. tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
  615. return 0;
  616. default:
  617. return -EINVAL;
  618. };
  619. return 0;
  620. }
  621. static int tun_chr_fasync(int fd, struct file *file, int on)
  622. {
  623. struct tun_struct *tun = file->private_data;
  624. int ret;
  625. if (!tun)
  626. return -EBADFD;
  627. DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
  628. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  629. return ret;
  630. if (on) {
  631. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  632. if (ret)
  633. return ret;
  634. tun->flags |= TUN_FASYNC;
  635. } else
  636. tun->flags &= ~TUN_FASYNC;
  637. return 0;
  638. }
  639. static int tun_chr_open(struct inode *inode, struct file * file)
  640. {
  641. DBG1(KERN_INFO "tunX: tun_chr_open\n");
  642. file->private_data = NULL;
  643. return 0;
  644. }
  645. static int tun_chr_close(struct inode *inode, struct file *file)
  646. {
  647. struct tun_struct *tun = file->private_data;
  648. if (!tun)
  649. return 0;
  650. DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
  651. tun_chr_fasync(-1, file, 0);
  652. rtnl_lock();
  653. /* Detach from net device */
  654. file->private_data = NULL;
  655. tun->attached = 0;
  656. put_net(dev_net(tun->dev));
  657. /* Drop read queue */
  658. skb_queue_purge(&tun->readq);
  659. if (!(tun->flags & TUN_PERSIST)) {
  660. list_del(&tun->list);
  661. unregister_netdevice(tun->dev);
  662. }
  663. rtnl_unlock();
  664. return 0;
  665. }
  666. static const struct file_operations tun_fops = {
  667. .owner = THIS_MODULE,
  668. .llseek = no_llseek,
  669. .read = do_sync_read,
  670. .aio_read = tun_chr_aio_read,
  671. .write = do_sync_write,
  672. .aio_write = tun_chr_aio_write,
  673. .poll = tun_chr_poll,
  674. .ioctl = tun_chr_ioctl,
  675. .open = tun_chr_open,
  676. .release = tun_chr_close,
  677. .fasync = tun_chr_fasync
  678. };
  679. static struct miscdevice tun_miscdev = {
  680. .minor = TUN_MINOR,
  681. .name = "tun",
  682. .fops = &tun_fops,
  683. };
  684. /* ethtool interface */
  685. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  686. {
  687. cmd->supported = 0;
  688. cmd->advertising = 0;
  689. cmd->speed = SPEED_10;
  690. cmd->duplex = DUPLEX_FULL;
  691. cmd->port = PORT_TP;
  692. cmd->phy_address = 0;
  693. cmd->transceiver = XCVR_INTERNAL;
  694. cmd->autoneg = AUTONEG_DISABLE;
  695. cmd->maxtxpkt = 0;
  696. cmd->maxrxpkt = 0;
  697. return 0;
  698. }
  699. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  700. {
  701. struct tun_struct *tun = netdev_priv(dev);
  702. strcpy(info->driver, DRV_NAME);
  703. strcpy(info->version, DRV_VERSION);
  704. strcpy(info->fw_version, "N/A");
  705. switch (tun->flags & TUN_TYPE_MASK) {
  706. case TUN_TUN_DEV:
  707. strcpy(info->bus_info, "tun");
  708. break;
  709. case TUN_TAP_DEV:
  710. strcpy(info->bus_info, "tap");
  711. break;
  712. }
  713. }
  714. static u32 tun_get_msglevel(struct net_device *dev)
  715. {
  716. #ifdef TUN_DEBUG
  717. struct tun_struct *tun = netdev_priv(dev);
  718. return tun->debug;
  719. #else
  720. return -EOPNOTSUPP;
  721. #endif
  722. }
  723. static void tun_set_msglevel(struct net_device *dev, u32 value)
  724. {
  725. #ifdef TUN_DEBUG
  726. struct tun_struct *tun = netdev_priv(dev);
  727. tun->debug = value;
  728. #endif
  729. }
  730. static u32 tun_get_link(struct net_device *dev)
  731. {
  732. struct tun_struct *tun = netdev_priv(dev);
  733. return tun->attached;
  734. }
  735. static u32 tun_get_rx_csum(struct net_device *dev)
  736. {
  737. struct tun_struct *tun = netdev_priv(dev);
  738. return (tun->flags & TUN_NOCHECKSUM) == 0;
  739. }
  740. static int tun_set_rx_csum(struct net_device *dev, u32 data)
  741. {
  742. struct tun_struct *tun = netdev_priv(dev);
  743. if (data)
  744. tun->flags &= ~TUN_NOCHECKSUM;
  745. else
  746. tun->flags |= TUN_NOCHECKSUM;
  747. return 0;
  748. }
  749. static const struct ethtool_ops tun_ethtool_ops = {
  750. .get_settings = tun_get_settings,
  751. .get_drvinfo = tun_get_drvinfo,
  752. .get_msglevel = tun_get_msglevel,
  753. .set_msglevel = tun_set_msglevel,
  754. .get_link = tun_get_link,
  755. .get_rx_csum = tun_get_rx_csum,
  756. .set_rx_csum = tun_set_rx_csum
  757. };
  758. static int tun_init_net(struct net *net)
  759. {
  760. struct tun_net *tn;
  761. tn = kmalloc(sizeof(*tn), GFP_KERNEL);
  762. if (tn == NULL)
  763. return -ENOMEM;
  764. INIT_LIST_HEAD(&tn->dev_list);
  765. if (net_assign_generic(net, tun_net_id, tn)) {
  766. kfree(tn);
  767. return -ENOMEM;
  768. }
  769. return 0;
  770. }
  771. static void tun_exit_net(struct net *net)
  772. {
  773. struct tun_net *tn;
  774. struct tun_struct *tun, *nxt;
  775. tn = net_generic(net, tun_net_id);
  776. rtnl_lock();
  777. list_for_each_entry_safe(tun, nxt, &tn->dev_list, list) {
  778. DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
  779. unregister_netdevice(tun->dev);
  780. }
  781. rtnl_unlock();
  782. kfree(tn);
  783. }
  784. static struct pernet_operations tun_net_ops = {
  785. .init = tun_init_net,
  786. .exit = tun_exit_net,
  787. };
  788. static int __init tun_init(void)
  789. {
  790. int ret = 0;
  791. printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  792. printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
  793. ret = register_pernet_gen_device(&tun_net_id, &tun_net_ops);
  794. if (ret) {
  795. printk(KERN_ERR "tun: Can't register pernet ops\n");
  796. goto err_pernet;
  797. }
  798. ret = misc_register(&tun_miscdev);
  799. if (ret) {
  800. printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
  801. goto err_misc;
  802. }
  803. return 0;
  804. err_misc:
  805. unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
  806. err_pernet:
  807. return ret;
  808. }
  809. static void tun_cleanup(void)
  810. {
  811. misc_deregister(&tun_miscdev);
  812. unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
  813. }
  814. module_init(tun_init);
  815. module_exit(tun_cleanup);
  816. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  817. MODULE_AUTHOR(DRV_COPYRIGHT);
  818. MODULE_LICENSE("GPL");
  819. MODULE_ALIAS_MISCDEV(TUN_MINOR);