tun.c 33 KB

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