tun.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  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. * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
  21. * Add TUNSETLINK ioctl to set the link encapsulation
  22. *
  23. * Mark Smith <markzzzsmith@yahoo.com.au>
  24. * Use random_ether_addr() for tap MAC address.
  25. *
  26. * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
  27. * Fixes in packet dropping, queue length setting and queue wakeup.
  28. * Increased default tx queue length.
  29. * Added ethtool API.
  30. * Minor cleanups
  31. *
  32. * Daniel Podlejski <underley@underley.eu.org>
  33. * Modifications for 2.3.99-pre5 kernel.
  34. */
  35. #define DRV_NAME "tun"
  36. #define DRV_VERSION "1.6"
  37. #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
  38. #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
  39. #include <linux/module.h>
  40. #include <linux/errno.h>
  41. #include <linux/kernel.h>
  42. #include <linux/major.h>
  43. #include <linux/slab.h>
  44. #include <linux/smp_lock.h>
  45. #include <linux/poll.h>
  46. #include <linux/fcntl.h>
  47. #include <linux/init.h>
  48. #include <linux/skbuff.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/etherdevice.h>
  51. #include <linux/miscdevice.h>
  52. #include <linux/ethtool.h>
  53. #include <linux/rtnetlink.h>
  54. #include <linux/if.h>
  55. #include <linux/if_arp.h>
  56. #include <linux/if_ether.h>
  57. #include <linux/if_tun.h>
  58. #include <linux/crc32.h>
  59. #include <linux/nsproxy.h>
  60. #include <linux/virtio_net.h>
  61. #include <net/net_namespace.h>
  62. #include <net/netns/generic.h>
  63. #include <net/rtnetlink.h>
  64. #include <net/sock.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. #define FLT_EXACT_COUNT 8
  78. struct tap_filter {
  79. unsigned int count; /* Number of addrs. Zero means disabled */
  80. u32 mask[2]; /* Mask of the hashed addrs */
  81. unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
  82. };
  83. struct tun_file {
  84. atomic_t count;
  85. struct tun_struct *tun;
  86. struct net *net;
  87. };
  88. struct tun_sock;
  89. struct tun_struct {
  90. struct tun_file *tfile;
  91. unsigned int flags;
  92. uid_t owner;
  93. gid_t group;
  94. struct sk_buff_head readq;
  95. struct net_device *dev;
  96. struct fasync_struct *fasync;
  97. struct tap_filter txflt;
  98. struct sock *sk;
  99. struct socket socket;
  100. #ifdef TUN_DEBUG
  101. int debug;
  102. #endif
  103. };
  104. struct tun_sock {
  105. struct sock sk;
  106. struct tun_struct *tun;
  107. };
  108. static inline struct tun_sock *tun_sk(struct sock *sk)
  109. {
  110. return container_of(sk, struct tun_sock, sk);
  111. }
  112. static int tun_attach(struct tun_struct *tun, struct file *file)
  113. {
  114. struct tun_file *tfile = file->private_data;
  115. int err;
  116. ASSERT_RTNL();
  117. netif_tx_lock_bh(tun->dev);
  118. err = -EINVAL;
  119. if (tfile->tun)
  120. goto out;
  121. err = -EBUSY;
  122. if (tun->tfile)
  123. goto out;
  124. err = 0;
  125. tfile->tun = tun;
  126. tun->tfile = tfile;
  127. dev_hold(tun->dev);
  128. sock_hold(tun->sk);
  129. atomic_inc(&tfile->count);
  130. out:
  131. netif_tx_unlock_bh(tun->dev);
  132. return err;
  133. }
  134. static void __tun_detach(struct tun_struct *tun)
  135. {
  136. /* Detach from net device */
  137. netif_tx_lock_bh(tun->dev);
  138. tun->tfile = NULL;
  139. netif_tx_unlock_bh(tun->dev);
  140. /* Drop read queue */
  141. skb_queue_purge(&tun->readq);
  142. /* Drop the extra count on the net device */
  143. dev_put(tun->dev);
  144. }
  145. static void tun_detach(struct tun_struct *tun)
  146. {
  147. rtnl_lock();
  148. __tun_detach(tun);
  149. rtnl_unlock();
  150. }
  151. static struct tun_struct *__tun_get(struct tun_file *tfile)
  152. {
  153. struct tun_struct *tun = NULL;
  154. if (atomic_inc_not_zero(&tfile->count))
  155. tun = tfile->tun;
  156. return tun;
  157. }
  158. static struct tun_struct *tun_get(struct file *file)
  159. {
  160. return __tun_get(file->private_data);
  161. }
  162. static void tun_put(struct tun_struct *tun)
  163. {
  164. struct tun_file *tfile = tun->tfile;
  165. if (atomic_dec_and_test(&tfile->count))
  166. tun_detach(tfile->tun);
  167. }
  168. /* TAP filterting */
  169. static void addr_hash_set(u32 *mask, const u8 *addr)
  170. {
  171. int n = ether_crc(ETH_ALEN, addr) >> 26;
  172. mask[n >> 5] |= (1 << (n & 31));
  173. }
  174. static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
  175. {
  176. int n = ether_crc(ETH_ALEN, addr) >> 26;
  177. return mask[n >> 5] & (1 << (n & 31));
  178. }
  179. static int update_filter(struct tap_filter *filter, void __user *arg)
  180. {
  181. struct { u8 u[ETH_ALEN]; } *addr;
  182. struct tun_filter uf;
  183. int err, alen, n, nexact;
  184. if (copy_from_user(&uf, arg, sizeof(uf)))
  185. return -EFAULT;
  186. if (!uf.count) {
  187. /* Disabled */
  188. filter->count = 0;
  189. return 0;
  190. }
  191. alen = ETH_ALEN * uf.count;
  192. addr = kmalloc(alen, GFP_KERNEL);
  193. if (!addr)
  194. return -ENOMEM;
  195. if (copy_from_user(addr, arg + sizeof(uf), alen)) {
  196. err = -EFAULT;
  197. goto done;
  198. }
  199. /* The filter is updated without holding any locks. Which is
  200. * perfectly safe. We disable it first and in the worst
  201. * case we'll accept a few undesired packets. */
  202. filter->count = 0;
  203. wmb();
  204. /* Use first set of addresses as an exact filter */
  205. for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
  206. memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
  207. nexact = n;
  208. /* Remaining multicast addresses are hashed,
  209. * unicast will leave the filter disabled. */
  210. memset(filter->mask, 0, sizeof(filter->mask));
  211. for (; n < uf.count; n++) {
  212. if (!is_multicast_ether_addr(addr[n].u)) {
  213. err = 0; /* no filter */
  214. goto done;
  215. }
  216. addr_hash_set(filter->mask, addr[n].u);
  217. }
  218. /* For ALLMULTI just set the mask to all ones.
  219. * This overrides the mask populated above. */
  220. if ((uf.flags & TUN_FLT_ALLMULTI))
  221. memset(filter->mask, ~0, sizeof(filter->mask));
  222. /* Now enable the filter */
  223. wmb();
  224. filter->count = nexact;
  225. /* Return the number of exact filters */
  226. err = nexact;
  227. done:
  228. kfree(addr);
  229. return err;
  230. }
  231. /* Returns: 0 - drop, !=0 - accept */
  232. static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
  233. {
  234. /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
  235. * at this point. */
  236. struct ethhdr *eh = (struct ethhdr *) skb->data;
  237. int i;
  238. /* Exact match */
  239. for (i = 0; i < filter->count; i++)
  240. if (!compare_ether_addr(eh->h_dest, filter->addr[i]))
  241. return 1;
  242. /* Inexact match (multicast only) */
  243. if (is_multicast_ether_addr(eh->h_dest))
  244. return addr_hash_test(filter->mask, eh->h_dest);
  245. return 0;
  246. }
  247. /*
  248. * Checks whether the packet is accepted or not.
  249. * Returns: 0 - drop, !=0 - accept
  250. */
  251. static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
  252. {
  253. if (!filter->count)
  254. return 1;
  255. return run_filter(filter, skb);
  256. }
  257. /* Network device part of the driver */
  258. static const struct ethtool_ops tun_ethtool_ops;
  259. /* Net device detach from fd. */
  260. static void tun_net_uninit(struct net_device *dev)
  261. {
  262. struct tun_struct *tun = netdev_priv(dev);
  263. struct tun_file *tfile = tun->tfile;
  264. /* Inform the methods they need to stop using the dev.
  265. */
  266. if (tfile) {
  267. wake_up_all(&tun->socket.wait);
  268. if (atomic_dec_and_test(&tfile->count))
  269. __tun_detach(tun);
  270. }
  271. }
  272. static void tun_free_netdev(struct net_device *dev)
  273. {
  274. struct tun_struct *tun = netdev_priv(dev);
  275. sock_put(tun->sk);
  276. }
  277. /* Net device open. */
  278. static int tun_net_open(struct net_device *dev)
  279. {
  280. netif_start_queue(dev);
  281. return 0;
  282. }
  283. /* Net device close. */
  284. static int tun_net_close(struct net_device *dev)
  285. {
  286. netif_stop_queue(dev);
  287. return 0;
  288. }
  289. /* Net device start xmit */
  290. static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
  291. {
  292. struct tun_struct *tun = netdev_priv(dev);
  293. DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
  294. /* Drop packet if interface is not attached */
  295. if (!tun->tfile)
  296. goto drop;
  297. /* Drop if the filter does not like it.
  298. * This is a noop if the filter is disabled.
  299. * Filter can be enabled only for the TAP devices. */
  300. if (!check_filter(&tun->txflt, skb))
  301. goto drop;
  302. if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
  303. if (!(tun->flags & TUN_ONE_QUEUE)) {
  304. /* Normal queueing mode. */
  305. /* Packet scheduler handles dropping of further packets. */
  306. netif_stop_queue(dev);
  307. /* We won't see all dropped packets individually, so overrun
  308. * error is more appropriate. */
  309. dev->stats.tx_fifo_errors++;
  310. } else {
  311. /* Single queue mode.
  312. * Driver handles dropping of all packets itself. */
  313. goto drop;
  314. }
  315. }
  316. /* Enqueue packet */
  317. skb_queue_tail(&tun->readq, skb);
  318. dev->trans_start = jiffies;
  319. /* Notify and wake up reader process */
  320. if (tun->flags & TUN_FASYNC)
  321. kill_fasync(&tun->fasync, SIGIO, POLL_IN);
  322. wake_up_interruptible(&tun->socket.wait);
  323. return 0;
  324. drop:
  325. dev->stats.tx_dropped++;
  326. kfree_skb(skb);
  327. return 0;
  328. }
  329. static void tun_net_mclist(struct net_device *dev)
  330. {
  331. /*
  332. * This callback is supposed to deal with mc filter in
  333. * _rx_ path and has nothing to do with the _tx_ path.
  334. * In rx path we always accept everything userspace gives us.
  335. */
  336. return;
  337. }
  338. #define MIN_MTU 68
  339. #define MAX_MTU 65535
  340. static int
  341. tun_net_change_mtu(struct net_device *dev, int new_mtu)
  342. {
  343. if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
  344. return -EINVAL;
  345. dev->mtu = new_mtu;
  346. return 0;
  347. }
  348. static const struct net_device_ops tun_netdev_ops = {
  349. .ndo_uninit = tun_net_uninit,
  350. .ndo_open = tun_net_open,
  351. .ndo_stop = tun_net_close,
  352. .ndo_start_xmit = tun_net_xmit,
  353. .ndo_change_mtu = tun_net_change_mtu,
  354. };
  355. static const struct net_device_ops tap_netdev_ops = {
  356. .ndo_uninit = tun_net_uninit,
  357. .ndo_open = tun_net_open,
  358. .ndo_stop = tun_net_close,
  359. .ndo_start_xmit = tun_net_xmit,
  360. .ndo_change_mtu = tun_net_change_mtu,
  361. .ndo_set_multicast_list = tun_net_mclist,
  362. .ndo_set_mac_address = eth_mac_addr,
  363. .ndo_validate_addr = eth_validate_addr,
  364. };
  365. /* Initialize net device. */
  366. static void tun_net_init(struct net_device *dev)
  367. {
  368. struct tun_struct *tun = netdev_priv(dev);
  369. switch (tun->flags & TUN_TYPE_MASK) {
  370. case TUN_TUN_DEV:
  371. dev->netdev_ops = &tun_netdev_ops;
  372. /* Point-to-Point TUN Device */
  373. dev->hard_header_len = 0;
  374. dev->addr_len = 0;
  375. dev->mtu = 1500;
  376. /* Zero header length */
  377. dev->type = ARPHRD_NONE;
  378. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  379. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  380. break;
  381. case TUN_TAP_DEV:
  382. dev->netdev_ops = &tap_netdev_ops;
  383. /* Ethernet TAP Device */
  384. ether_setup(dev);
  385. random_ether_addr(dev->dev_addr);
  386. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  387. break;
  388. }
  389. }
  390. /* Character device part */
  391. /* Poll */
  392. static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
  393. {
  394. struct tun_file *tfile = file->private_data;
  395. struct tun_struct *tun = __tun_get(tfile);
  396. struct sock *sk;
  397. unsigned int mask = 0;
  398. if (!tun)
  399. return POLLERR;
  400. sk = tun->sk;
  401. DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
  402. poll_wait(file, &tun->socket.wait, wait);
  403. if (!skb_queue_empty(&tun->readq))
  404. mask |= POLLIN | POLLRDNORM;
  405. if (sock_writeable(sk) ||
  406. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
  407. sock_writeable(sk)))
  408. mask |= POLLOUT | POLLWRNORM;
  409. if (tun->dev->reg_state != NETREG_REGISTERED)
  410. mask = POLLERR;
  411. tun_put(tun);
  412. return mask;
  413. }
  414. /* prepad is the amount to reserve at front. len is length after that.
  415. * linear is a hint as to how much to copy (usually headers). */
  416. static inline struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
  417. size_t prepad, size_t len,
  418. size_t linear, int noblock)
  419. {
  420. struct sock *sk = tun->sk;
  421. struct sk_buff *skb;
  422. int err;
  423. /* Under a page? Don't bother with paged skb. */
  424. if (prepad + len < PAGE_SIZE || !linear)
  425. linear = len;
  426. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  427. &err);
  428. if (!skb)
  429. return ERR_PTR(err);
  430. skb_reserve(skb, prepad);
  431. skb_put(skb, linear);
  432. skb->data_len = len - linear;
  433. skb->len += len - linear;
  434. return skb;
  435. }
  436. /* Get packet from user space buffer */
  437. static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
  438. const struct iovec *iv, size_t count,
  439. int noblock)
  440. {
  441. struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
  442. struct sk_buff *skb;
  443. size_t len = count, align = 0;
  444. struct virtio_net_hdr gso = { 0 };
  445. int offset = 0;
  446. if (!(tun->flags & TUN_NO_PI)) {
  447. if ((len -= sizeof(pi)) > count)
  448. return -EINVAL;
  449. if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
  450. return -EFAULT;
  451. offset += sizeof(pi);
  452. }
  453. if (tun->flags & TUN_VNET_HDR) {
  454. if ((len -= sizeof(gso)) > count)
  455. return -EINVAL;
  456. if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
  457. return -EFAULT;
  458. if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  459. gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
  460. gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
  461. if (gso.hdr_len > len)
  462. return -EINVAL;
  463. offset += sizeof(gso);
  464. }
  465. if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
  466. align = NET_IP_ALIGN;
  467. if (unlikely(len < ETH_HLEN ||
  468. (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
  469. return -EINVAL;
  470. }
  471. skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
  472. if (IS_ERR(skb)) {
  473. if (PTR_ERR(skb) != -EAGAIN)
  474. tun->dev->stats.rx_dropped++;
  475. return PTR_ERR(skb);
  476. }
  477. if (skb_copy_datagram_from_iovec(skb, 0, iv, offset, len)) {
  478. tun->dev->stats.rx_dropped++;
  479. kfree_skb(skb);
  480. return -EFAULT;
  481. }
  482. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  483. if (!skb_partial_csum_set(skb, gso.csum_start,
  484. gso.csum_offset)) {
  485. tun->dev->stats.rx_frame_errors++;
  486. kfree_skb(skb);
  487. return -EINVAL;
  488. }
  489. } else if (tun->flags & TUN_NOCHECKSUM)
  490. skb->ip_summed = CHECKSUM_UNNECESSARY;
  491. switch (tun->flags & TUN_TYPE_MASK) {
  492. case TUN_TUN_DEV:
  493. if (tun->flags & TUN_NO_PI) {
  494. switch (skb->data[0] & 0xf0) {
  495. case 0x40:
  496. pi.proto = htons(ETH_P_IP);
  497. break;
  498. case 0x60:
  499. pi.proto = htons(ETH_P_IPV6);
  500. break;
  501. default:
  502. tun->dev->stats.rx_dropped++;
  503. kfree_skb(skb);
  504. return -EINVAL;
  505. }
  506. }
  507. skb_reset_mac_header(skb);
  508. skb->protocol = pi.proto;
  509. skb->dev = tun->dev;
  510. break;
  511. case TUN_TAP_DEV:
  512. skb->protocol = eth_type_trans(skb, tun->dev);
  513. break;
  514. };
  515. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  516. pr_debug("GSO!\n");
  517. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  518. case VIRTIO_NET_HDR_GSO_TCPV4:
  519. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  520. break;
  521. case VIRTIO_NET_HDR_GSO_TCPV6:
  522. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  523. break;
  524. default:
  525. tun->dev->stats.rx_frame_errors++;
  526. kfree_skb(skb);
  527. return -EINVAL;
  528. }
  529. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  530. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  531. skb_shinfo(skb)->gso_size = gso.gso_size;
  532. if (skb_shinfo(skb)->gso_size == 0) {
  533. tun->dev->stats.rx_frame_errors++;
  534. kfree_skb(skb);
  535. return -EINVAL;
  536. }
  537. /* Header must be checked, and gso_segs computed. */
  538. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  539. skb_shinfo(skb)->gso_segs = 0;
  540. }
  541. netif_rx_ni(skb);
  542. tun->dev->stats.rx_packets++;
  543. tun->dev->stats.rx_bytes += len;
  544. return count;
  545. }
  546. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  547. unsigned long count, loff_t pos)
  548. {
  549. struct file *file = iocb->ki_filp;
  550. struct tun_struct *tun = tun_get(file);
  551. ssize_t result;
  552. if (!tun)
  553. return -EBADFD;
  554. DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
  555. result = tun_get_user(tun, iv, iov_length(iv, count),
  556. file->f_flags & O_NONBLOCK);
  557. tun_put(tun);
  558. return result;
  559. }
  560. /* Put packet to the user space buffer */
  561. static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
  562. struct sk_buff *skb,
  563. const struct iovec *iv, int len)
  564. {
  565. struct tun_pi pi = { 0, skb->protocol };
  566. ssize_t total = 0;
  567. if (!(tun->flags & TUN_NO_PI)) {
  568. if ((len -= sizeof(pi)) < 0)
  569. return -EINVAL;
  570. if (len < skb->len) {
  571. /* Packet will be striped */
  572. pi.flags |= TUN_PKT_STRIP;
  573. }
  574. if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
  575. return -EFAULT;
  576. total += sizeof(pi);
  577. }
  578. if (tun->flags & TUN_VNET_HDR) {
  579. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  580. if ((len -= sizeof(gso)) < 0)
  581. return -EINVAL;
  582. if (skb_is_gso(skb)) {
  583. struct skb_shared_info *sinfo = skb_shinfo(skb);
  584. /* This is a hint as to how much should be linear. */
  585. gso.hdr_len = skb_headlen(skb);
  586. gso.gso_size = sinfo->gso_size;
  587. if (sinfo->gso_type & SKB_GSO_TCPV4)
  588. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  589. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  590. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  591. else
  592. BUG();
  593. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  594. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  595. } else
  596. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  597. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  598. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  599. gso.csum_start = skb->csum_start - skb_headroom(skb);
  600. gso.csum_offset = skb->csum_offset;
  601. } /* else everything is zero */
  602. if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
  603. sizeof(gso))))
  604. return -EFAULT;
  605. total += sizeof(gso);
  606. }
  607. len = min_t(int, skb->len, len);
  608. skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
  609. total += len;
  610. tun->dev->stats.tx_packets++;
  611. tun->dev->stats.tx_bytes += len;
  612. return total;
  613. }
  614. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  615. unsigned long count, loff_t pos)
  616. {
  617. struct file *file = iocb->ki_filp;
  618. struct tun_file *tfile = file->private_data;
  619. struct tun_struct *tun = __tun_get(tfile);
  620. DECLARE_WAITQUEUE(wait, current);
  621. struct sk_buff *skb;
  622. ssize_t len, ret = 0;
  623. if (!tun)
  624. return -EBADFD;
  625. DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
  626. len = iov_length(iv, count);
  627. if (len < 0) {
  628. ret = -EINVAL;
  629. goto out;
  630. }
  631. add_wait_queue(&tun->socket.wait, &wait);
  632. while (len) {
  633. current->state = TASK_INTERRUPTIBLE;
  634. /* Read frames from the queue */
  635. if (!(skb=skb_dequeue(&tun->readq))) {
  636. if (file->f_flags & O_NONBLOCK) {
  637. ret = -EAGAIN;
  638. break;
  639. }
  640. if (signal_pending(current)) {
  641. ret = -ERESTARTSYS;
  642. break;
  643. }
  644. if (tun->dev->reg_state != NETREG_REGISTERED) {
  645. ret = -EIO;
  646. break;
  647. }
  648. /* Nothing to read, let's sleep */
  649. schedule();
  650. continue;
  651. }
  652. netif_wake_queue(tun->dev);
  653. ret = tun_put_user(tun, skb, iv, len);
  654. kfree_skb(skb);
  655. break;
  656. }
  657. current->state = TASK_RUNNING;
  658. remove_wait_queue(&tun->socket.wait, &wait);
  659. out:
  660. tun_put(tun);
  661. return ret;
  662. }
  663. static void tun_setup(struct net_device *dev)
  664. {
  665. struct tun_struct *tun = netdev_priv(dev);
  666. skb_queue_head_init(&tun->readq);
  667. tun->owner = -1;
  668. tun->group = -1;
  669. dev->ethtool_ops = &tun_ethtool_ops;
  670. dev->destructor = tun_free_netdev;
  671. }
  672. /* Trivial set of netlink ops to allow deleting tun or tap
  673. * device with netlink.
  674. */
  675. static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
  676. {
  677. return -EINVAL;
  678. }
  679. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  680. .kind = DRV_NAME,
  681. .priv_size = sizeof(struct tun_struct),
  682. .setup = tun_setup,
  683. .validate = tun_validate,
  684. };
  685. static void tun_sock_write_space(struct sock *sk)
  686. {
  687. struct tun_struct *tun;
  688. if (!sock_writeable(sk))
  689. return;
  690. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  691. return;
  692. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  693. wake_up_interruptible_sync(sk->sk_sleep);
  694. tun = container_of(sk, struct tun_sock, sk)->tun;
  695. kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
  696. }
  697. static void tun_sock_destruct(struct sock *sk)
  698. {
  699. free_netdev(container_of(sk, struct tun_sock, sk)->tun->dev);
  700. }
  701. static struct proto tun_proto = {
  702. .name = "tun",
  703. .owner = THIS_MODULE,
  704. .obj_size = sizeof(struct tun_sock),
  705. };
  706. static int tun_flags(struct tun_struct *tun)
  707. {
  708. int flags = 0;
  709. if (tun->flags & TUN_TUN_DEV)
  710. flags |= IFF_TUN;
  711. else
  712. flags |= IFF_TAP;
  713. if (tun->flags & TUN_NO_PI)
  714. flags |= IFF_NO_PI;
  715. if (tun->flags & TUN_ONE_QUEUE)
  716. flags |= IFF_ONE_QUEUE;
  717. if (tun->flags & TUN_VNET_HDR)
  718. flags |= IFF_VNET_HDR;
  719. return flags;
  720. }
  721. static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
  722. char *buf)
  723. {
  724. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  725. return sprintf(buf, "0x%x\n", tun_flags(tun));
  726. }
  727. static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
  728. char *buf)
  729. {
  730. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  731. return sprintf(buf, "%d\n", tun->owner);
  732. }
  733. static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
  734. char *buf)
  735. {
  736. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  737. return sprintf(buf, "%d\n", tun->group);
  738. }
  739. static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
  740. static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
  741. static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
  742. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  743. {
  744. struct sock *sk;
  745. struct tun_struct *tun;
  746. struct net_device *dev;
  747. int err;
  748. dev = __dev_get_by_name(net, ifr->ifr_name);
  749. if (dev) {
  750. const struct cred *cred = current_cred();
  751. if (ifr->ifr_flags & IFF_TUN_EXCL)
  752. return -EBUSY;
  753. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  754. tun = netdev_priv(dev);
  755. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  756. tun = netdev_priv(dev);
  757. else
  758. return -EINVAL;
  759. if (((tun->owner != -1 && cred->euid != tun->owner) ||
  760. (tun->group != -1 && !in_egroup_p(tun->group))) &&
  761. !capable(CAP_NET_ADMIN))
  762. return -EPERM;
  763. err = security_tun_dev_attach(tun->sk);
  764. if (err < 0)
  765. return err;
  766. err = tun_attach(tun, file);
  767. if (err < 0)
  768. return err;
  769. }
  770. else {
  771. char *name;
  772. unsigned long flags = 0;
  773. err = -EINVAL;
  774. if (!capable(CAP_NET_ADMIN))
  775. return -EPERM;
  776. err = security_tun_dev_create();
  777. if (err < 0)
  778. return err;
  779. /* Set dev type */
  780. if (ifr->ifr_flags & IFF_TUN) {
  781. /* TUN device */
  782. flags |= TUN_TUN_DEV;
  783. name = "tun%d";
  784. } else if (ifr->ifr_flags & IFF_TAP) {
  785. /* TAP device */
  786. flags |= TUN_TAP_DEV;
  787. name = "tap%d";
  788. } else
  789. goto failed;
  790. if (*ifr->ifr_name)
  791. name = ifr->ifr_name;
  792. dev = alloc_netdev(sizeof(struct tun_struct), name,
  793. tun_setup);
  794. if (!dev)
  795. return -ENOMEM;
  796. dev_net_set(dev, net);
  797. dev->rtnl_link_ops = &tun_link_ops;
  798. tun = netdev_priv(dev);
  799. tun->dev = dev;
  800. tun->flags = flags;
  801. tun->txflt.count = 0;
  802. err = -ENOMEM;
  803. sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
  804. if (!sk)
  805. goto err_free_dev;
  806. init_waitqueue_head(&tun->socket.wait);
  807. sock_init_data(&tun->socket, sk);
  808. sk->sk_write_space = tun_sock_write_space;
  809. sk->sk_sndbuf = INT_MAX;
  810. tun->sk = sk;
  811. container_of(sk, struct tun_sock, sk)->tun = tun;
  812. security_tun_dev_post_create(sk);
  813. tun_net_init(dev);
  814. if (strchr(dev->name, '%')) {
  815. err = dev_alloc_name(dev, dev->name);
  816. if (err < 0)
  817. goto err_free_sk;
  818. }
  819. err = -EINVAL;
  820. err = register_netdevice(tun->dev);
  821. if (err < 0)
  822. goto err_free_sk;
  823. if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
  824. device_create_file(&tun->dev->dev, &dev_attr_owner) ||
  825. device_create_file(&tun->dev->dev, &dev_attr_group))
  826. printk(KERN_ERR "Failed to create tun sysfs files\n");
  827. sk->sk_destruct = tun_sock_destruct;
  828. err = tun_attach(tun, file);
  829. if (err < 0)
  830. goto failed;
  831. }
  832. DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
  833. if (ifr->ifr_flags & IFF_NO_PI)
  834. tun->flags |= TUN_NO_PI;
  835. else
  836. tun->flags &= ~TUN_NO_PI;
  837. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  838. tun->flags |= TUN_ONE_QUEUE;
  839. else
  840. tun->flags &= ~TUN_ONE_QUEUE;
  841. if (ifr->ifr_flags & IFF_VNET_HDR)
  842. tun->flags |= TUN_VNET_HDR;
  843. else
  844. tun->flags &= ~TUN_VNET_HDR;
  845. /* Make sure persistent devices do not get stuck in
  846. * xoff state.
  847. */
  848. if (netif_running(tun->dev))
  849. netif_wake_queue(tun->dev);
  850. strcpy(ifr->ifr_name, tun->dev->name);
  851. return 0;
  852. err_free_sk:
  853. sock_put(sk);
  854. err_free_dev:
  855. free_netdev(dev);
  856. failed:
  857. return err;
  858. }
  859. static int tun_get_iff(struct net *net, struct tun_struct *tun,
  860. struct ifreq *ifr)
  861. {
  862. DBG(KERN_INFO "%s: tun_get_iff\n", tun->dev->name);
  863. strcpy(ifr->ifr_name, tun->dev->name);
  864. ifr->ifr_flags = tun_flags(tun);
  865. return 0;
  866. }
  867. /* This is like a cut-down ethtool ops, except done via tun fd so no
  868. * privs required. */
  869. static int set_offload(struct net_device *dev, unsigned long arg)
  870. {
  871. unsigned int old_features, features;
  872. old_features = dev->features;
  873. /* Unset features, set them as we chew on the arg. */
  874. features = (old_features & ~(NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST
  875. |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6));
  876. if (arg & TUN_F_CSUM) {
  877. features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  878. arg &= ~TUN_F_CSUM;
  879. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  880. if (arg & TUN_F_TSO_ECN) {
  881. features |= NETIF_F_TSO_ECN;
  882. arg &= ~TUN_F_TSO_ECN;
  883. }
  884. if (arg & TUN_F_TSO4)
  885. features |= NETIF_F_TSO;
  886. if (arg & TUN_F_TSO6)
  887. features |= NETIF_F_TSO6;
  888. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  889. }
  890. }
  891. /* This gives the user a way to test for new features in future by
  892. * trying to set them. */
  893. if (arg)
  894. return -EINVAL;
  895. dev->features = features;
  896. if (old_features != dev->features)
  897. netdev_features_change(dev);
  898. return 0;
  899. }
  900. static long tun_chr_ioctl(struct file *file, unsigned int cmd,
  901. unsigned long arg)
  902. {
  903. struct tun_file *tfile = file->private_data;
  904. struct tun_struct *tun;
  905. void __user* argp = (void __user*)arg;
  906. struct ifreq ifr;
  907. int sndbuf;
  908. int ret;
  909. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
  910. if (copy_from_user(&ifr, argp, sizeof ifr))
  911. return -EFAULT;
  912. if (cmd == TUNGETFEATURES) {
  913. /* Currently this just means: "what IFF flags are valid?".
  914. * This is needed because we never checked for invalid flags on
  915. * TUNSETIFF. */
  916. return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
  917. IFF_VNET_HDR,
  918. (unsigned int __user*)argp);
  919. }
  920. rtnl_lock();
  921. tun = __tun_get(tfile);
  922. if (cmd == TUNSETIFF && !tun) {
  923. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  924. ret = tun_set_iff(tfile->net, file, &ifr);
  925. if (ret)
  926. goto unlock;
  927. if (copy_to_user(argp, &ifr, sizeof(ifr)))
  928. ret = -EFAULT;
  929. goto unlock;
  930. }
  931. ret = -EBADFD;
  932. if (!tun)
  933. goto unlock;
  934. DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
  935. ret = 0;
  936. switch (cmd) {
  937. case TUNGETIFF:
  938. ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
  939. if (ret)
  940. break;
  941. if (copy_to_user(argp, &ifr, sizeof(ifr)))
  942. ret = -EFAULT;
  943. break;
  944. case TUNSETNOCSUM:
  945. /* Disable/Enable checksum */
  946. if (arg)
  947. tun->flags |= TUN_NOCHECKSUM;
  948. else
  949. tun->flags &= ~TUN_NOCHECKSUM;
  950. DBG(KERN_INFO "%s: checksum %s\n",
  951. tun->dev->name, arg ? "disabled" : "enabled");
  952. break;
  953. case TUNSETPERSIST:
  954. /* Disable/Enable persist mode */
  955. if (arg)
  956. tun->flags |= TUN_PERSIST;
  957. else
  958. tun->flags &= ~TUN_PERSIST;
  959. DBG(KERN_INFO "%s: persist %s\n",
  960. tun->dev->name, arg ? "enabled" : "disabled");
  961. break;
  962. case TUNSETOWNER:
  963. /* Set owner of the device */
  964. tun->owner = (uid_t) arg;
  965. DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
  966. break;
  967. case TUNSETGROUP:
  968. /* Set group of the device */
  969. tun->group= (gid_t) arg;
  970. DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
  971. break;
  972. case TUNSETLINK:
  973. /* Only allow setting the type when the interface is down */
  974. if (tun->dev->flags & IFF_UP) {
  975. DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
  976. tun->dev->name);
  977. ret = -EBUSY;
  978. } else {
  979. tun->dev->type = (int) arg;
  980. DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
  981. ret = 0;
  982. }
  983. break;
  984. #ifdef TUN_DEBUG
  985. case TUNSETDEBUG:
  986. tun->debug = arg;
  987. break;
  988. #endif
  989. case TUNSETOFFLOAD:
  990. ret = set_offload(tun->dev, arg);
  991. break;
  992. case TUNSETTXFILTER:
  993. /* Can be set only for TAPs */
  994. ret = -EINVAL;
  995. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  996. break;
  997. ret = update_filter(&tun->txflt, (void __user *)arg);
  998. break;
  999. case SIOCGIFHWADDR:
  1000. /* Get hw addres */
  1001. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  1002. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  1003. if (copy_to_user(argp, &ifr, sizeof ifr))
  1004. ret = -EFAULT;
  1005. break;
  1006. case SIOCSIFHWADDR:
  1007. /* Set hw address */
  1008. DBG(KERN_DEBUG "%s: set hw address: %pM\n",
  1009. tun->dev->name, ifr.ifr_hwaddr.sa_data);
  1010. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  1011. break;
  1012. case TUNGETSNDBUF:
  1013. sndbuf = tun->sk->sk_sndbuf;
  1014. if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
  1015. ret = -EFAULT;
  1016. break;
  1017. case TUNSETSNDBUF:
  1018. if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
  1019. ret = -EFAULT;
  1020. break;
  1021. }
  1022. tun->sk->sk_sndbuf = sndbuf;
  1023. break;
  1024. default:
  1025. ret = -EINVAL;
  1026. break;
  1027. };
  1028. unlock:
  1029. rtnl_unlock();
  1030. if (tun)
  1031. tun_put(tun);
  1032. return ret;
  1033. }
  1034. static int tun_chr_fasync(int fd, struct file *file, int on)
  1035. {
  1036. struct tun_struct *tun = tun_get(file);
  1037. int ret;
  1038. if (!tun)
  1039. return -EBADFD;
  1040. DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
  1041. lock_kernel();
  1042. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  1043. goto out;
  1044. if (on) {
  1045. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  1046. if (ret)
  1047. goto out;
  1048. tun->flags |= TUN_FASYNC;
  1049. } else
  1050. tun->flags &= ~TUN_FASYNC;
  1051. ret = 0;
  1052. out:
  1053. unlock_kernel();
  1054. tun_put(tun);
  1055. return ret;
  1056. }
  1057. static int tun_chr_open(struct inode *inode, struct file * file)
  1058. {
  1059. struct tun_file *tfile;
  1060. cycle_kernel_lock();
  1061. DBG1(KERN_INFO "tunX: tun_chr_open\n");
  1062. tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
  1063. if (!tfile)
  1064. return -ENOMEM;
  1065. atomic_set(&tfile->count, 0);
  1066. tfile->tun = NULL;
  1067. tfile->net = get_net(current->nsproxy->net_ns);
  1068. file->private_data = tfile;
  1069. return 0;
  1070. }
  1071. static int tun_chr_close(struct inode *inode, struct file *file)
  1072. {
  1073. struct tun_file *tfile = file->private_data;
  1074. struct tun_struct *tun;
  1075. tun = __tun_get(tfile);
  1076. if (tun) {
  1077. struct net_device *dev = tun->dev;
  1078. DBG(KERN_INFO "%s: tun_chr_close\n", dev->name);
  1079. __tun_detach(tun);
  1080. /* If desireable, unregister the netdevice. */
  1081. if (!(tun->flags & TUN_PERSIST)) {
  1082. rtnl_lock();
  1083. if (dev->reg_state == NETREG_REGISTERED)
  1084. unregister_netdevice(dev);
  1085. rtnl_unlock();
  1086. }
  1087. }
  1088. tun = tfile->tun;
  1089. if (tun)
  1090. sock_put(tun->sk);
  1091. put_net(tfile->net);
  1092. kfree(tfile);
  1093. return 0;
  1094. }
  1095. static const struct file_operations tun_fops = {
  1096. .owner = THIS_MODULE,
  1097. .llseek = no_llseek,
  1098. .read = do_sync_read,
  1099. .aio_read = tun_chr_aio_read,
  1100. .write = do_sync_write,
  1101. .aio_write = tun_chr_aio_write,
  1102. .poll = tun_chr_poll,
  1103. .unlocked_ioctl = tun_chr_ioctl,
  1104. .open = tun_chr_open,
  1105. .release = tun_chr_close,
  1106. .fasync = tun_chr_fasync
  1107. };
  1108. static struct miscdevice tun_miscdev = {
  1109. .minor = TUN_MINOR,
  1110. .name = "tun",
  1111. .devnode = "net/tun",
  1112. .fops = &tun_fops,
  1113. };
  1114. /* ethtool interface */
  1115. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1116. {
  1117. cmd->supported = 0;
  1118. cmd->advertising = 0;
  1119. cmd->speed = SPEED_10;
  1120. cmd->duplex = DUPLEX_FULL;
  1121. cmd->port = PORT_TP;
  1122. cmd->phy_address = 0;
  1123. cmd->transceiver = XCVR_INTERNAL;
  1124. cmd->autoneg = AUTONEG_DISABLE;
  1125. cmd->maxtxpkt = 0;
  1126. cmd->maxrxpkt = 0;
  1127. return 0;
  1128. }
  1129. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1130. {
  1131. struct tun_struct *tun = netdev_priv(dev);
  1132. strcpy(info->driver, DRV_NAME);
  1133. strcpy(info->version, DRV_VERSION);
  1134. strcpy(info->fw_version, "N/A");
  1135. switch (tun->flags & TUN_TYPE_MASK) {
  1136. case TUN_TUN_DEV:
  1137. strcpy(info->bus_info, "tun");
  1138. break;
  1139. case TUN_TAP_DEV:
  1140. strcpy(info->bus_info, "tap");
  1141. break;
  1142. }
  1143. }
  1144. static u32 tun_get_msglevel(struct net_device *dev)
  1145. {
  1146. #ifdef TUN_DEBUG
  1147. struct tun_struct *tun = netdev_priv(dev);
  1148. return tun->debug;
  1149. #else
  1150. return -EOPNOTSUPP;
  1151. #endif
  1152. }
  1153. static void tun_set_msglevel(struct net_device *dev, u32 value)
  1154. {
  1155. #ifdef TUN_DEBUG
  1156. struct tun_struct *tun = netdev_priv(dev);
  1157. tun->debug = value;
  1158. #endif
  1159. }
  1160. static u32 tun_get_link(struct net_device *dev)
  1161. {
  1162. struct tun_struct *tun = netdev_priv(dev);
  1163. return !!tun->tfile;
  1164. }
  1165. static u32 tun_get_rx_csum(struct net_device *dev)
  1166. {
  1167. struct tun_struct *tun = netdev_priv(dev);
  1168. return (tun->flags & TUN_NOCHECKSUM) == 0;
  1169. }
  1170. static int tun_set_rx_csum(struct net_device *dev, u32 data)
  1171. {
  1172. struct tun_struct *tun = netdev_priv(dev);
  1173. if (data)
  1174. tun->flags &= ~TUN_NOCHECKSUM;
  1175. else
  1176. tun->flags |= TUN_NOCHECKSUM;
  1177. return 0;
  1178. }
  1179. static const struct ethtool_ops tun_ethtool_ops = {
  1180. .get_settings = tun_get_settings,
  1181. .get_drvinfo = tun_get_drvinfo,
  1182. .get_msglevel = tun_get_msglevel,
  1183. .set_msglevel = tun_set_msglevel,
  1184. .get_link = tun_get_link,
  1185. .get_rx_csum = tun_get_rx_csum,
  1186. .set_rx_csum = tun_set_rx_csum
  1187. };
  1188. static int __init tun_init(void)
  1189. {
  1190. int ret = 0;
  1191. printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1192. printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
  1193. ret = rtnl_link_register(&tun_link_ops);
  1194. if (ret) {
  1195. printk(KERN_ERR "tun: Can't register link_ops\n");
  1196. goto err_linkops;
  1197. }
  1198. ret = misc_register(&tun_miscdev);
  1199. if (ret) {
  1200. printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
  1201. goto err_misc;
  1202. }
  1203. return 0;
  1204. err_misc:
  1205. rtnl_link_unregister(&tun_link_ops);
  1206. err_linkops:
  1207. return ret;
  1208. }
  1209. static void tun_cleanup(void)
  1210. {
  1211. misc_deregister(&tun_miscdev);
  1212. rtnl_link_unregister(&tun_link_ops);
  1213. }
  1214. module_init(tun_init);
  1215. module_exit(tun_cleanup);
  1216. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1217. MODULE_AUTHOR(DRV_COPYRIGHT);
  1218. MODULE_LICENSE("GPL");
  1219. MODULE_ALIAS_MISCDEV(TUN_MINOR);