tun.c 37 KB

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