tun.c 30 KB

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