tun.c 33 KB

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