tun.c 37 KB

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