tun.c 36 KB

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