tun.c 37 KB

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