tun.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  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. sock_update_classid(sk);
  432. /* Under a page? Don't bother with paged skb. */
  433. if (prepad + len < PAGE_SIZE || !linear)
  434. linear = len;
  435. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  436. &err);
  437. if (!skb)
  438. return ERR_PTR(err);
  439. skb_reserve(skb, prepad);
  440. skb_put(skb, linear);
  441. skb->data_len = len - linear;
  442. skb->len += len - linear;
  443. return skb;
  444. }
  445. /* Get packet from user space buffer */
  446. static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
  447. const struct iovec *iv, size_t count,
  448. int noblock)
  449. {
  450. struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
  451. struct sk_buff *skb;
  452. size_t len = count, align = 0;
  453. struct virtio_net_hdr gso = { 0 };
  454. int offset = 0;
  455. if (!(tun->flags & TUN_NO_PI)) {
  456. if ((len -= sizeof(pi)) > count)
  457. return -EINVAL;
  458. if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
  459. return -EFAULT;
  460. offset += sizeof(pi);
  461. }
  462. if (tun->flags & TUN_VNET_HDR) {
  463. if ((len -= tun->vnet_hdr_sz) > count)
  464. return -EINVAL;
  465. if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
  466. return -EFAULT;
  467. if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  468. gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
  469. gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
  470. if (gso.hdr_len > len)
  471. return -EINVAL;
  472. offset += tun->vnet_hdr_sz;
  473. }
  474. if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
  475. align = NET_IP_ALIGN;
  476. if (unlikely(len < ETH_HLEN ||
  477. (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
  478. return -EINVAL;
  479. }
  480. skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
  481. if (IS_ERR(skb)) {
  482. if (PTR_ERR(skb) != -EAGAIN)
  483. tun->dev->stats.rx_dropped++;
  484. return PTR_ERR(skb);
  485. }
  486. if (skb_copy_datagram_from_iovec(skb, 0, iv, offset, len)) {
  487. tun->dev->stats.rx_dropped++;
  488. kfree_skb(skb);
  489. return -EFAULT;
  490. }
  491. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  492. if (!skb_partial_csum_set(skb, gso.csum_start,
  493. gso.csum_offset)) {
  494. tun->dev->stats.rx_frame_errors++;
  495. kfree_skb(skb);
  496. return -EINVAL;
  497. }
  498. } else if (tun->flags & TUN_NOCHECKSUM)
  499. skb->ip_summed = CHECKSUM_UNNECESSARY;
  500. switch (tun->flags & TUN_TYPE_MASK) {
  501. case TUN_TUN_DEV:
  502. if (tun->flags & TUN_NO_PI) {
  503. switch (skb->data[0] & 0xf0) {
  504. case 0x40:
  505. pi.proto = htons(ETH_P_IP);
  506. break;
  507. case 0x60:
  508. pi.proto = htons(ETH_P_IPV6);
  509. break;
  510. default:
  511. tun->dev->stats.rx_dropped++;
  512. kfree_skb(skb);
  513. return -EINVAL;
  514. }
  515. }
  516. skb_reset_mac_header(skb);
  517. skb->protocol = pi.proto;
  518. skb->dev = tun->dev;
  519. break;
  520. case TUN_TAP_DEV:
  521. skb->protocol = eth_type_trans(skb, tun->dev);
  522. break;
  523. };
  524. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  525. pr_debug("GSO!\n");
  526. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  527. case VIRTIO_NET_HDR_GSO_TCPV4:
  528. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  529. break;
  530. case VIRTIO_NET_HDR_GSO_TCPV6:
  531. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  532. break;
  533. case VIRTIO_NET_HDR_GSO_UDP:
  534. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  535. break;
  536. default:
  537. tun->dev->stats.rx_frame_errors++;
  538. kfree_skb(skb);
  539. return -EINVAL;
  540. }
  541. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  542. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  543. skb_shinfo(skb)->gso_size = gso.gso_size;
  544. if (skb_shinfo(skb)->gso_size == 0) {
  545. tun->dev->stats.rx_frame_errors++;
  546. kfree_skb(skb);
  547. return -EINVAL;
  548. }
  549. /* Header must be checked, and gso_segs computed. */
  550. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  551. skb_shinfo(skb)->gso_segs = 0;
  552. }
  553. netif_rx_ni(skb);
  554. tun->dev->stats.rx_packets++;
  555. tun->dev->stats.rx_bytes += len;
  556. return count;
  557. }
  558. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  559. unsigned long count, loff_t pos)
  560. {
  561. struct file *file = iocb->ki_filp;
  562. struct tun_struct *tun = tun_get(file);
  563. ssize_t result;
  564. if (!tun)
  565. return -EBADFD;
  566. DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
  567. result = tun_get_user(tun, iv, iov_length(iv, count),
  568. file->f_flags & O_NONBLOCK);
  569. tun_put(tun);
  570. return result;
  571. }
  572. /* Put packet to the user space buffer */
  573. static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
  574. struct sk_buff *skb,
  575. const struct iovec *iv, int len)
  576. {
  577. struct tun_pi pi = { 0, skb->protocol };
  578. ssize_t total = 0;
  579. if (!(tun->flags & TUN_NO_PI)) {
  580. if ((len -= sizeof(pi)) < 0)
  581. return -EINVAL;
  582. if (len < skb->len) {
  583. /* Packet will be striped */
  584. pi.flags |= TUN_PKT_STRIP;
  585. }
  586. if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
  587. return -EFAULT;
  588. total += sizeof(pi);
  589. }
  590. if (tun->flags & TUN_VNET_HDR) {
  591. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  592. if ((len -= tun->vnet_hdr_sz) < 0)
  593. return -EINVAL;
  594. if (skb_is_gso(skb)) {
  595. struct skb_shared_info *sinfo = skb_shinfo(skb);
  596. /* This is a hint as to how much should be linear. */
  597. gso.hdr_len = skb_headlen(skb);
  598. gso.gso_size = sinfo->gso_size;
  599. if (sinfo->gso_type & SKB_GSO_TCPV4)
  600. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  601. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  602. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  603. else if (sinfo->gso_type & SKB_GSO_UDP)
  604. gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  605. else
  606. BUG();
  607. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  608. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  609. } else
  610. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  611. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  612. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  613. gso.csum_start = skb->csum_start - skb_headroom(skb);
  614. gso.csum_offset = skb->csum_offset;
  615. } /* else everything is zero */
  616. if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
  617. sizeof(gso))))
  618. return -EFAULT;
  619. total += tun->vnet_hdr_sz;
  620. }
  621. len = min_t(int, skb->len, len);
  622. skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
  623. total += skb->len;
  624. tun->dev->stats.tx_packets++;
  625. tun->dev->stats.tx_bytes += len;
  626. return total;
  627. }
  628. static ssize_t tun_do_read(struct tun_struct *tun,
  629. struct kiocb *iocb, const struct iovec *iv,
  630. ssize_t len, int noblock)
  631. {
  632. DECLARE_WAITQUEUE(wait, current);
  633. struct sk_buff *skb;
  634. ssize_t ret = 0;
  635. DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
  636. add_wait_queue(&tun->wq.wait, &wait);
  637. while (len) {
  638. current->state = TASK_INTERRUPTIBLE;
  639. /* Read frames from the queue */
  640. if (!(skb=skb_dequeue(&tun->socket.sk->sk_receive_queue))) {
  641. if (noblock) {
  642. ret = -EAGAIN;
  643. break;
  644. }
  645. if (signal_pending(current)) {
  646. ret = -ERESTARTSYS;
  647. break;
  648. }
  649. if (tun->dev->reg_state != NETREG_REGISTERED) {
  650. ret = -EIO;
  651. break;
  652. }
  653. /* Nothing to read, let's sleep */
  654. schedule();
  655. continue;
  656. }
  657. netif_wake_queue(tun->dev);
  658. ret = tun_put_user(tun, skb, iv, len);
  659. kfree_skb(skb);
  660. break;
  661. }
  662. current->state = TASK_RUNNING;
  663. remove_wait_queue(&tun->wq.wait, &wait);
  664. return ret;
  665. }
  666. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  667. unsigned long count, loff_t pos)
  668. {
  669. struct file *file = iocb->ki_filp;
  670. struct tun_file *tfile = file->private_data;
  671. struct tun_struct *tun = __tun_get(tfile);
  672. ssize_t len, ret;
  673. if (!tun)
  674. return -EBADFD;
  675. len = iov_length(iv, count);
  676. if (len < 0) {
  677. ret = -EINVAL;
  678. goto out;
  679. }
  680. ret = tun_do_read(tun, iocb, iv, len, file->f_flags & O_NONBLOCK);
  681. ret = min_t(ssize_t, ret, len);
  682. out:
  683. tun_put(tun);
  684. return ret;
  685. }
  686. static void tun_setup(struct net_device *dev)
  687. {
  688. struct tun_struct *tun = netdev_priv(dev);
  689. tun->owner = -1;
  690. tun->group = -1;
  691. dev->ethtool_ops = &tun_ethtool_ops;
  692. dev->destructor = tun_free_netdev;
  693. }
  694. /* Trivial set of netlink ops to allow deleting tun or tap
  695. * device with netlink.
  696. */
  697. static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
  698. {
  699. return -EINVAL;
  700. }
  701. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  702. .kind = DRV_NAME,
  703. .priv_size = sizeof(struct tun_struct),
  704. .setup = tun_setup,
  705. .validate = tun_validate,
  706. };
  707. static void tun_sock_write_space(struct sock *sk)
  708. {
  709. struct tun_struct *tun;
  710. wait_queue_head_t *wqueue;
  711. if (!sock_writeable(sk))
  712. return;
  713. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  714. return;
  715. wqueue = sk_sleep(sk);
  716. if (wqueue && waitqueue_active(wqueue))
  717. wake_up_interruptible_sync_poll(wqueue, POLLOUT |
  718. POLLWRNORM | POLLWRBAND);
  719. tun = tun_sk(sk)->tun;
  720. kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
  721. }
  722. static void tun_sock_destruct(struct sock *sk)
  723. {
  724. free_netdev(tun_sk(sk)->tun->dev);
  725. }
  726. static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
  727. struct msghdr *m, size_t total_len)
  728. {
  729. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  730. return tun_get_user(tun, m->msg_iov, total_len,
  731. m->msg_flags & MSG_DONTWAIT);
  732. }
  733. static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
  734. struct msghdr *m, size_t total_len,
  735. int flags)
  736. {
  737. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  738. int ret;
  739. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  740. return -EINVAL;
  741. ret = tun_do_read(tun, iocb, m->msg_iov, total_len,
  742. flags & MSG_DONTWAIT);
  743. if (ret > total_len) {
  744. m->msg_flags |= MSG_TRUNC;
  745. ret = flags & MSG_TRUNC ? ret : total_len;
  746. }
  747. return ret;
  748. }
  749. /* Ops structure to mimic raw sockets with tun */
  750. static const struct proto_ops tun_socket_ops = {
  751. .sendmsg = tun_sendmsg,
  752. .recvmsg = tun_recvmsg,
  753. };
  754. static struct proto tun_proto = {
  755. .name = "tun",
  756. .owner = THIS_MODULE,
  757. .obj_size = sizeof(struct tun_sock),
  758. };
  759. static int tun_flags(struct tun_struct *tun)
  760. {
  761. int flags = 0;
  762. if (tun->flags & TUN_TUN_DEV)
  763. flags |= IFF_TUN;
  764. else
  765. flags |= IFF_TAP;
  766. if (tun->flags & TUN_NO_PI)
  767. flags |= IFF_NO_PI;
  768. if (tun->flags & TUN_ONE_QUEUE)
  769. flags |= IFF_ONE_QUEUE;
  770. if (tun->flags & TUN_VNET_HDR)
  771. flags |= IFF_VNET_HDR;
  772. return flags;
  773. }
  774. static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
  775. char *buf)
  776. {
  777. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  778. return sprintf(buf, "0x%x\n", tun_flags(tun));
  779. }
  780. static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
  781. char *buf)
  782. {
  783. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  784. return sprintf(buf, "%d\n", tun->owner);
  785. }
  786. static ssize_t tun_show_group(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, "%d\n", tun->group);
  791. }
  792. static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
  793. static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
  794. static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
  795. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  796. {
  797. struct sock *sk;
  798. struct tun_struct *tun;
  799. struct net_device *dev;
  800. int err;
  801. dev = __dev_get_by_name(net, ifr->ifr_name);
  802. if (dev) {
  803. const struct cred *cred = current_cred();
  804. if (ifr->ifr_flags & IFF_TUN_EXCL)
  805. return -EBUSY;
  806. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  807. tun = netdev_priv(dev);
  808. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  809. tun = netdev_priv(dev);
  810. else
  811. return -EINVAL;
  812. if (((tun->owner != -1 && cred->euid != tun->owner) ||
  813. (tun->group != -1 && !in_egroup_p(tun->group))) &&
  814. !capable(CAP_NET_ADMIN))
  815. return -EPERM;
  816. err = security_tun_dev_attach(tun->socket.sk);
  817. if (err < 0)
  818. return err;
  819. err = tun_attach(tun, file);
  820. if (err < 0)
  821. return err;
  822. }
  823. else {
  824. char *name;
  825. unsigned long flags = 0;
  826. if (!capable(CAP_NET_ADMIN))
  827. return -EPERM;
  828. err = security_tun_dev_create();
  829. if (err < 0)
  830. return err;
  831. /* Set dev type */
  832. if (ifr->ifr_flags & IFF_TUN) {
  833. /* TUN device */
  834. flags |= TUN_TUN_DEV;
  835. name = "tun%d";
  836. } else if (ifr->ifr_flags & IFF_TAP) {
  837. /* TAP device */
  838. flags |= TUN_TAP_DEV;
  839. name = "tap%d";
  840. } else
  841. return -EINVAL;
  842. if (*ifr->ifr_name)
  843. name = ifr->ifr_name;
  844. dev = alloc_netdev(sizeof(struct tun_struct), name,
  845. tun_setup);
  846. if (!dev)
  847. return -ENOMEM;
  848. dev_net_set(dev, net);
  849. dev->rtnl_link_ops = &tun_link_ops;
  850. tun = netdev_priv(dev);
  851. tun->dev = dev;
  852. tun->flags = flags;
  853. tun->txflt.count = 0;
  854. tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  855. err = -ENOMEM;
  856. sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
  857. if (!sk)
  858. goto err_free_dev;
  859. tun->socket.wq = &tun->wq;
  860. init_waitqueue_head(&tun->wq.wait);
  861. tun->socket.ops = &tun_socket_ops;
  862. sock_init_data(&tun->socket, sk);
  863. sk->sk_write_space = tun_sock_write_space;
  864. sk->sk_sndbuf = INT_MAX;
  865. tun_sk(sk)->tun = tun;
  866. security_tun_dev_post_create(sk);
  867. tun_net_init(dev);
  868. if (strchr(dev->name, '%')) {
  869. err = dev_alloc_name(dev, dev->name);
  870. if (err < 0)
  871. goto err_free_sk;
  872. }
  873. err = register_netdevice(tun->dev);
  874. if (err < 0)
  875. goto err_free_sk;
  876. if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
  877. device_create_file(&tun->dev->dev, &dev_attr_owner) ||
  878. device_create_file(&tun->dev->dev, &dev_attr_group))
  879. printk(KERN_ERR "Failed to create tun sysfs files\n");
  880. sk->sk_destruct = tun_sock_destruct;
  881. err = tun_attach(tun, file);
  882. if (err < 0)
  883. goto failed;
  884. }
  885. DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
  886. if (ifr->ifr_flags & IFF_NO_PI)
  887. tun->flags |= TUN_NO_PI;
  888. else
  889. tun->flags &= ~TUN_NO_PI;
  890. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  891. tun->flags |= TUN_ONE_QUEUE;
  892. else
  893. tun->flags &= ~TUN_ONE_QUEUE;
  894. if (ifr->ifr_flags & IFF_VNET_HDR)
  895. tun->flags |= TUN_VNET_HDR;
  896. else
  897. tun->flags &= ~TUN_VNET_HDR;
  898. /* Make sure persistent devices do not get stuck in
  899. * xoff state.
  900. */
  901. if (netif_running(tun->dev))
  902. netif_wake_queue(tun->dev);
  903. strcpy(ifr->ifr_name, tun->dev->name);
  904. return 0;
  905. err_free_sk:
  906. sock_put(sk);
  907. err_free_dev:
  908. free_netdev(dev);
  909. failed:
  910. return err;
  911. }
  912. static int tun_get_iff(struct net *net, struct tun_struct *tun,
  913. struct ifreq *ifr)
  914. {
  915. DBG(KERN_INFO "%s: tun_get_iff\n", tun->dev->name);
  916. strcpy(ifr->ifr_name, tun->dev->name);
  917. ifr->ifr_flags = tun_flags(tun);
  918. return 0;
  919. }
  920. /* This is like a cut-down ethtool ops, except done via tun fd so no
  921. * privs required. */
  922. static int set_offload(struct net_device *dev, unsigned long arg)
  923. {
  924. unsigned int old_features, features;
  925. old_features = dev->features;
  926. /* Unset features, set them as we chew on the arg. */
  927. features = (old_features & ~(NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST
  928. |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6
  929. |NETIF_F_UFO));
  930. if (arg & TUN_F_CSUM) {
  931. features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  932. arg &= ~TUN_F_CSUM;
  933. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  934. if (arg & TUN_F_TSO_ECN) {
  935. features |= NETIF_F_TSO_ECN;
  936. arg &= ~TUN_F_TSO_ECN;
  937. }
  938. if (arg & TUN_F_TSO4)
  939. features |= NETIF_F_TSO;
  940. if (arg & TUN_F_TSO6)
  941. features |= NETIF_F_TSO6;
  942. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  943. }
  944. if (arg & TUN_F_UFO) {
  945. features |= NETIF_F_UFO;
  946. arg &= ~TUN_F_UFO;
  947. }
  948. }
  949. /* This gives the user a way to test for new features in future by
  950. * trying to set them. */
  951. if (arg)
  952. return -EINVAL;
  953. dev->features = features;
  954. if (old_features != dev->features)
  955. netdev_features_change(dev);
  956. return 0;
  957. }
  958. static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  959. unsigned long arg, int ifreq_len)
  960. {
  961. struct tun_file *tfile = file->private_data;
  962. struct tun_struct *tun;
  963. void __user* argp = (void __user*)arg;
  964. struct sock_fprog fprog;
  965. struct ifreq ifr;
  966. int sndbuf;
  967. int vnet_hdr_sz;
  968. int ret;
  969. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
  970. if (copy_from_user(&ifr, argp, ifreq_len))
  971. return -EFAULT;
  972. if (cmd == TUNGETFEATURES) {
  973. /* Currently this just means: "what IFF flags are valid?".
  974. * This is needed because we never checked for invalid flags on
  975. * TUNSETIFF. */
  976. return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
  977. IFF_VNET_HDR,
  978. (unsigned int __user*)argp);
  979. }
  980. rtnl_lock();
  981. tun = __tun_get(tfile);
  982. if (cmd == TUNSETIFF && !tun) {
  983. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  984. ret = tun_set_iff(tfile->net, file, &ifr);
  985. if (ret)
  986. goto unlock;
  987. if (copy_to_user(argp, &ifr, ifreq_len))
  988. ret = -EFAULT;
  989. goto unlock;
  990. }
  991. ret = -EBADFD;
  992. if (!tun)
  993. goto unlock;
  994. DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
  995. ret = 0;
  996. switch (cmd) {
  997. case TUNGETIFF:
  998. ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
  999. if (ret)
  1000. break;
  1001. if (copy_to_user(argp, &ifr, ifreq_len))
  1002. ret = -EFAULT;
  1003. break;
  1004. case TUNSETNOCSUM:
  1005. /* Disable/Enable checksum */
  1006. if (arg)
  1007. tun->flags |= TUN_NOCHECKSUM;
  1008. else
  1009. tun->flags &= ~TUN_NOCHECKSUM;
  1010. DBG(KERN_INFO "%s: checksum %s\n",
  1011. tun->dev->name, arg ? "disabled" : "enabled");
  1012. break;
  1013. case TUNSETPERSIST:
  1014. /* Disable/Enable persist mode */
  1015. if (arg)
  1016. tun->flags |= TUN_PERSIST;
  1017. else
  1018. tun->flags &= ~TUN_PERSIST;
  1019. DBG(KERN_INFO "%s: persist %s\n",
  1020. tun->dev->name, arg ? "enabled" : "disabled");
  1021. break;
  1022. case TUNSETOWNER:
  1023. /* Set owner of the device */
  1024. tun->owner = (uid_t) arg;
  1025. DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
  1026. break;
  1027. case TUNSETGROUP:
  1028. /* Set group of the device */
  1029. tun->group= (gid_t) arg;
  1030. DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
  1031. break;
  1032. case TUNSETLINK:
  1033. /* Only allow setting the type when the interface is down */
  1034. if (tun->dev->flags & IFF_UP) {
  1035. DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
  1036. tun->dev->name);
  1037. ret = -EBUSY;
  1038. } else {
  1039. tun->dev->type = (int) arg;
  1040. DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
  1041. ret = 0;
  1042. }
  1043. break;
  1044. #ifdef TUN_DEBUG
  1045. case TUNSETDEBUG:
  1046. tun->debug = arg;
  1047. break;
  1048. #endif
  1049. case TUNSETOFFLOAD:
  1050. ret = set_offload(tun->dev, arg);
  1051. break;
  1052. case TUNSETTXFILTER:
  1053. /* Can be set only for TAPs */
  1054. ret = -EINVAL;
  1055. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1056. break;
  1057. ret = update_filter(&tun->txflt, (void __user *)arg);
  1058. break;
  1059. case SIOCGIFHWADDR:
  1060. /* Get hw addres */
  1061. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  1062. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  1063. if (copy_to_user(argp, &ifr, ifreq_len))
  1064. ret = -EFAULT;
  1065. break;
  1066. case SIOCSIFHWADDR:
  1067. /* Set hw address */
  1068. DBG(KERN_DEBUG "%s: set hw address: %pM\n",
  1069. tun->dev->name, ifr.ifr_hwaddr.sa_data);
  1070. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  1071. break;
  1072. case TUNGETSNDBUF:
  1073. sndbuf = tun->socket.sk->sk_sndbuf;
  1074. if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
  1075. ret = -EFAULT;
  1076. break;
  1077. case TUNSETSNDBUF:
  1078. if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
  1079. ret = -EFAULT;
  1080. break;
  1081. }
  1082. tun->socket.sk->sk_sndbuf = sndbuf;
  1083. break;
  1084. case TUNGETVNETHDRSZ:
  1085. vnet_hdr_sz = tun->vnet_hdr_sz;
  1086. if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
  1087. ret = -EFAULT;
  1088. break;
  1089. case TUNSETVNETHDRSZ:
  1090. if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
  1091. ret = -EFAULT;
  1092. break;
  1093. }
  1094. if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
  1095. ret = -EINVAL;
  1096. break;
  1097. }
  1098. tun->vnet_hdr_sz = vnet_hdr_sz;
  1099. break;
  1100. case TUNATTACHFILTER:
  1101. /* Can be set only for TAPs */
  1102. ret = -EINVAL;
  1103. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1104. break;
  1105. ret = -EFAULT;
  1106. if (copy_from_user(&fprog, argp, sizeof(fprog)))
  1107. break;
  1108. ret = sk_attach_filter(&fprog, tun->socket.sk);
  1109. break;
  1110. case TUNDETACHFILTER:
  1111. /* Can be set only for TAPs */
  1112. ret = -EINVAL;
  1113. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1114. break;
  1115. ret = sk_detach_filter(tun->socket.sk);
  1116. break;
  1117. default:
  1118. ret = -EINVAL;
  1119. break;
  1120. }
  1121. unlock:
  1122. rtnl_unlock();
  1123. if (tun)
  1124. tun_put(tun);
  1125. return ret;
  1126. }
  1127. static long tun_chr_ioctl(struct file *file,
  1128. unsigned int cmd, unsigned long arg)
  1129. {
  1130. return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
  1131. }
  1132. #ifdef CONFIG_COMPAT
  1133. static long tun_chr_compat_ioctl(struct file *file,
  1134. unsigned int cmd, unsigned long arg)
  1135. {
  1136. switch (cmd) {
  1137. case TUNSETIFF:
  1138. case TUNGETIFF:
  1139. case TUNSETTXFILTER:
  1140. case TUNGETSNDBUF:
  1141. case TUNSETSNDBUF:
  1142. case SIOCGIFHWADDR:
  1143. case SIOCSIFHWADDR:
  1144. arg = (unsigned long)compat_ptr(arg);
  1145. break;
  1146. default:
  1147. arg = (compat_ulong_t)arg;
  1148. break;
  1149. }
  1150. /*
  1151. * compat_ifreq is shorter than ifreq, so we must not access beyond
  1152. * the end of that structure. All fields that are used in this
  1153. * driver are compatible though, we don't need to convert the
  1154. * contents.
  1155. */
  1156. return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
  1157. }
  1158. #endif /* CONFIG_COMPAT */
  1159. static int tun_chr_fasync(int fd, struct file *file, int on)
  1160. {
  1161. struct tun_struct *tun = tun_get(file);
  1162. int ret;
  1163. if (!tun)
  1164. return -EBADFD;
  1165. DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
  1166. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  1167. goto out;
  1168. if (on) {
  1169. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  1170. if (ret)
  1171. goto out;
  1172. tun->flags |= TUN_FASYNC;
  1173. } else
  1174. tun->flags &= ~TUN_FASYNC;
  1175. ret = 0;
  1176. out:
  1177. tun_put(tun);
  1178. return ret;
  1179. }
  1180. static int tun_chr_open(struct inode *inode, struct file * file)
  1181. {
  1182. struct tun_file *tfile;
  1183. DBG1(KERN_INFO "tunX: tun_chr_open\n");
  1184. tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
  1185. if (!tfile)
  1186. return -ENOMEM;
  1187. atomic_set(&tfile->count, 0);
  1188. tfile->tun = NULL;
  1189. tfile->net = get_net(current->nsproxy->net_ns);
  1190. file->private_data = tfile;
  1191. return 0;
  1192. }
  1193. static int tun_chr_close(struct inode *inode, struct file *file)
  1194. {
  1195. struct tun_file *tfile = file->private_data;
  1196. struct tun_struct *tun;
  1197. tun = __tun_get(tfile);
  1198. if (tun) {
  1199. struct net_device *dev = tun->dev;
  1200. DBG(KERN_INFO "%s: tun_chr_close\n", dev->name);
  1201. __tun_detach(tun);
  1202. /* If desirable, unregister the netdevice. */
  1203. if (!(tun->flags & TUN_PERSIST)) {
  1204. rtnl_lock();
  1205. if (dev->reg_state == NETREG_REGISTERED)
  1206. unregister_netdevice(dev);
  1207. rtnl_unlock();
  1208. }
  1209. }
  1210. tun = tfile->tun;
  1211. if (tun)
  1212. sock_put(tun->socket.sk);
  1213. put_net(tfile->net);
  1214. kfree(tfile);
  1215. return 0;
  1216. }
  1217. static const struct file_operations tun_fops = {
  1218. .owner = THIS_MODULE,
  1219. .llseek = no_llseek,
  1220. .read = do_sync_read,
  1221. .aio_read = tun_chr_aio_read,
  1222. .write = do_sync_write,
  1223. .aio_write = tun_chr_aio_write,
  1224. .poll = tun_chr_poll,
  1225. .unlocked_ioctl = tun_chr_ioctl,
  1226. #ifdef CONFIG_COMPAT
  1227. .compat_ioctl = tun_chr_compat_ioctl,
  1228. #endif
  1229. .open = tun_chr_open,
  1230. .release = tun_chr_close,
  1231. .fasync = tun_chr_fasync
  1232. };
  1233. static struct miscdevice tun_miscdev = {
  1234. .minor = TUN_MINOR,
  1235. .name = "tun",
  1236. .nodename = "net/tun",
  1237. .fops = &tun_fops,
  1238. };
  1239. /* ethtool interface */
  1240. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1241. {
  1242. cmd->supported = 0;
  1243. cmd->advertising = 0;
  1244. cmd->speed = SPEED_10;
  1245. cmd->duplex = DUPLEX_FULL;
  1246. cmd->port = PORT_TP;
  1247. cmd->phy_address = 0;
  1248. cmd->transceiver = XCVR_INTERNAL;
  1249. cmd->autoneg = AUTONEG_DISABLE;
  1250. cmd->maxtxpkt = 0;
  1251. cmd->maxrxpkt = 0;
  1252. return 0;
  1253. }
  1254. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1255. {
  1256. struct tun_struct *tun = netdev_priv(dev);
  1257. strcpy(info->driver, DRV_NAME);
  1258. strcpy(info->version, DRV_VERSION);
  1259. strcpy(info->fw_version, "N/A");
  1260. switch (tun->flags & TUN_TYPE_MASK) {
  1261. case TUN_TUN_DEV:
  1262. strcpy(info->bus_info, "tun");
  1263. break;
  1264. case TUN_TAP_DEV:
  1265. strcpy(info->bus_info, "tap");
  1266. break;
  1267. }
  1268. }
  1269. static u32 tun_get_msglevel(struct net_device *dev)
  1270. {
  1271. #ifdef TUN_DEBUG
  1272. struct tun_struct *tun = netdev_priv(dev);
  1273. return tun->debug;
  1274. #else
  1275. return -EOPNOTSUPP;
  1276. #endif
  1277. }
  1278. static void tun_set_msglevel(struct net_device *dev, u32 value)
  1279. {
  1280. #ifdef TUN_DEBUG
  1281. struct tun_struct *tun = netdev_priv(dev);
  1282. tun->debug = value;
  1283. #endif
  1284. }
  1285. static u32 tun_get_link(struct net_device *dev)
  1286. {
  1287. struct tun_struct *tun = netdev_priv(dev);
  1288. return !!tun->tfile;
  1289. }
  1290. static u32 tun_get_rx_csum(struct net_device *dev)
  1291. {
  1292. struct tun_struct *tun = netdev_priv(dev);
  1293. return (tun->flags & TUN_NOCHECKSUM) == 0;
  1294. }
  1295. static int tun_set_rx_csum(struct net_device *dev, u32 data)
  1296. {
  1297. struct tun_struct *tun = netdev_priv(dev);
  1298. if (data)
  1299. tun->flags &= ~TUN_NOCHECKSUM;
  1300. else
  1301. tun->flags |= TUN_NOCHECKSUM;
  1302. return 0;
  1303. }
  1304. static const struct ethtool_ops tun_ethtool_ops = {
  1305. .get_settings = tun_get_settings,
  1306. .get_drvinfo = tun_get_drvinfo,
  1307. .get_msglevel = tun_get_msglevel,
  1308. .set_msglevel = tun_set_msglevel,
  1309. .get_link = tun_get_link,
  1310. .get_rx_csum = tun_get_rx_csum,
  1311. .set_rx_csum = tun_set_rx_csum
  1312. };
  1313. static int __init tun_init(void)
  1314. {
  1315. int ret = 0;
  1316. printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1317. printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
  1318. ret = rtnl_link_register(&tun_link_ops);
  1319. if (ret) {
  1320. printk(KERN_ERR "tun: Can't register link_ops\n");
  1321. goto err_linkops;
  1322. }
  1323. ret = misc_register(&tun_miscdev);
  1324. if (ret) {
  1325. printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
  1326. goto err_misc;
  1327. }
  1328. return 0;
  1329. err_misc:
  1330. rtnl_link_unregister(&tun_link_ops);
  1331. err_linkops:
  1332. return ret;
  1333. }
  1334. static void tun_cleanup(void)
  1335. {
  1336. misc_deregister(&tun_miscdev);
  1337. rtnl_link_unregister(&tun_link_ops);
  1338. }
  1339. /* Get an underlying socket object from tun file. Returns error unless file is
  1340. * attached to a device. The returned object works like a packet socket, it
  1341. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1342. * holding a reference to the file for as long as the socket is in use. */
  1343. struct socket *tun_get_socket(struct file *file)
  1344. {
  1345. struct tun_struct *tun;
  1346. if (file->f_op != &tun_fops)
  1347. return ERR_PTR(-EINVAL);
  1348. tun = tun_get(file);
  1349. if (!tun)
  1350. return ERR_PTR(-EBADFD);
  1351. tun_put(tun);
  1352. return &tun->socket;
  1353. }
  1354. EXPORT_SYMBOL_GPL(tun_get_socket);
  1355. module_init(tun_init);
  1356. module_exit(tun_cleanup);
  1357. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1358. MODULE_AUTHOR(DRV_COPYRIGHT);
  1359. MODULE_LICENSE("GPL");
  1360. MODULE_ALIAS_MISCDEV(TUN_MINOR);
  1361. MODULE_ALIAS("devname:net/tun");