tun.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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/smp_lock.h>
  45. #include <linux/poll.h>
  46. #include <linux/fcntl.h>
  47. #include <linux/init.h>
  48. #include <linux/skbuff.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/etherdevice.h>
  51. #include <linux/miscdevice.h>
  52. #include <linux/ethtool.h>
  53. #include <linux/rtnetlink.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 sk_buff_head readq;
  95. struct net_device *dev;
  96. struct fasync_struct *fasync;
  97. struct tap_filter txflt;
  98. struct sock *sk;
  99. struct socket socket;
  100. #ifdef TUN_DEBUG
  101. int debug;
  102. #endif
  103. };
  104. struct tun_sock {
  105. struct sock sk;
  106. struct tun_struct *tun;
  107. };
  108. static inline struct tun_sock *tun_sk(struct sock *sk)
  109. {
  110. return container_of(sk, struct tun_sock, sk);
  111. }
  112. static int tun_attach(struct tun_struct *tun, struct file *file)
  113. {
  114. struct tun_file *tfile = file->private_data;
  115. const struct cred *cred = current_cred();
  116. int err;
  117. ASSERT_RTNL();
  118. /* Check permissions */
  119. if (((tun->owner != -1 && cred->euid != tun->owner) ||
  120. (tun->group != -1 && !in_egroup_p(tun->group))) &&
  121. !capable(CAP_NET_ADMIN))
  122. return -EPERM;
  123. netif_tx_lock_bh(tun->dev);
  124. err = -EINVAL;
  125. if (tfile->tun)
  126. goto out;
  127. err = -EBUSY;
  128. if (tun->tfile)
  129. goto out;
  130. err = 0;
  131. tfile->tun = tun;
  132. tun->tfile = tfile;
  133. dev_hold(tun->dev);
  134. sock_hold(tun->sk);
  135. atomic_inc(&tfile->count);
  136. out:
  137. netif_tx_unlock_bh(tun->dev);
  138. return err;
  139. }
  140. static void __tun_detach(struct tun_struct *tun)
  141. {
  142. /* Detach from net device */
  143. netif_tx_lock_bh(tun->dev);
  144. tun->tfile = NULL;
  145. netif_tx_unlock_bh(tun->dev);
  146. /* Drop read queue */
  147. skb_queue_purge(&tun->readq);
  148. /* Drop the extra count on the net device */
  149. dev_put(tun->dev);
  150. }
  151. static void tun_detach(struct tun_struct *tun)
  152. {
  153. rtnl_lock();
  154. __tun_detach(tun);
  155. rtnl_unlock();
  156. }
  157. static struct tun_struct *__tun_get(struct tun_file *tfile)
  158. {
  159. struct tun_struct *tun = NULL;
  160. if (atomic_inc_not_zero(&tfile->count))
  161. tun = tfile->tun;
  162. return tun;
  163. }
  164. static struct tun_struct *tun_get(struct file *file)
  165. {
  166. return __tun_get(file->private_data);
  167. }
  168. static void tun_put(struct tun_struct *tun)
  169. {
  170. struct tun_file *tfile = tun->tfile;
  171. if (atomic_dec_and_test(&tfile->count))
  172. tun_detach(tfile->tun);
  173. }
  174. /* TAP filterting */
  175. static void addr_hash_set(u32 *mask, const u8 *addr)
  176. {
  177. int n = ether_crc(ETH_ALEN, addr) >> 26;
  178. mask[n >> 5] |= (1 << (n & 31));
  179. }
  180. static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
  181. {
  182. int n = ether_crc(ETH_ALEN, addr) >> 26;
  183. return mask[n >> 5] & (1 << (n & 31));
  184. }
  185. static int update_filter(struct tap_filter *filter, void __user *arg)
  186. {
  187. struct { u8 u[ETH_ALEN]; } *addr;
  188. struct tun_filter uf;
  189. int err, alen, n, nexact;
  190. if (copy_from_user(&uf, arg, sizeof(uf)))
  191. return -EFAULT;
  192. if (!uf.count) {
  193. /* Disabled */
  194. filter->count = 0;
  195. return 0;
  196. }
  197. alen = ETH_ALEN * uf.count;
  198. addr = kmalloc(alen, GFP_KERNEL);
  199. if (!addr)
  200. return -ENOMEM;
  201. if (copy_from_user(addr, arg + sizeof(uf), alen)) {
  202. err = -EFAULT;
  203. goto done;
  204. }
  205. /* The filter is updated without holding any locks. Which is
  206. * perfectly safe. We disable it first and in the worst
  207. * case we'll accept a few undesired packets. */
  208. filter->count = 0;
  209. wmb();
  210. /* Use first set of addresses as an exact filter */
  211. for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
  212. memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
  213. nexact = n;
  214. /* Remaining multicast addresses are hashed,
  215. * unicast will leave the filter disabled. */
  216. memset(filter->mask, 0, sizeof(filter->mask));
  217. for (; n < uf.count; n++) {
  218. if (!is_multicast_ether_addr(addr[n].u)) {
  219. err = 0; /* no filter */
  220. goto done;
  221. }
  222. addr_hash_set(filter->mask, addr[n].u);
  223. }
  224. /* For ALLMULTI just set the mask to all ones.
  225. * This overrides the mask populated above. */
  226. if ((uf.flags & TUN_FLT_ALLMULTI))
  227. memset(filter->mask, ~0, sizeof(filter->mask));
  228. /* Now enable the filter */
  229. wmb();
  230. filter->count = nexact;
  231. /* Return the number of exact filters */
  232. err = nexact;
  233. done:
  234. kfree(addr);
  235. return err;
  236. }
  237. /* Returns: 0 - drop, !=0 - accept */
  238. static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
  239. {
  240. /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
  241. * at this point. */
  242. struct ethhdr *eh = (struct ethhdr *) skb->data;
  243. int i;
  244. /* Exact match */
  245. for (i = 0; i < filter->count; i++)
  246. if (!compare_ether_addr(eh->h_dest, filter->addr[i]))
  247. return 1;
  248. /* Inexact match (multicast only) */
  249. if (is_multicast_ether_addr(eh->h_dest))
  250. return addr_hash_test(filter->mask, eh->h_dest);
  251. return 0;
  252. }
  253. /*
  254. * Checks whether the packet is accepted or not.
  255. * Returns: 0 - drop, !=0 - accept
  256. */
  257. static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
  258. {
  259. if (!filter->count)
  260. return 1;
  261. return run_filter(filter, skb);
  262. }
  263. /* Network device part of the driver */
  264. static const struct ethtool_ops tun_ethtool_ops;
  265. /* Net device detach from fd. */
  266. static void tun_net_uninit(struct net_device *dev)
  267. {
  268. struct tun_struct *tun = netdev_priv(dev);
  269. struct tun_file *tfile = tun->tfile;
  270. /* Inform the methods they need to stop using the dev.
  271. */
  272. if (tfile) {
  273. wake_up_all(&tun->socket.wait);
  274. if (atomic_dec_and_test(&tfile->count))
  275. __tun_detach(tun);
  276. }
  277. }
  278. static void tun_free_netdev(struct net_device *dev)
  279. {
  280. struct tun_struct *tun = netdev_priv(dev);
  281. sock_put(tun->sk);
  282. }
  283. /* Net device open. */
  284. static int tun_net_open(struct net_device *dev)
  285. {
  286. netif_start_queue(dev);
  287. return 0;
  288. }
  289. /* Net device close. */
  290. static int tun_net_close(struct net_device *dev)
  291. {
  292. netif_stop_queue(dev);
  293. return 0;
  294. }
  295. /* Net device start xmit */
  296. static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
  297. {
  298. struct tun_struct *tun = netdev_priv(dev);
  299. DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
  300. /* Drop packet if interface is not attached */
  301. if (!tun->tfile)
  302. goto drop;
  303. /* Drop if the filter does not like it.
  304. * This is a noop if the filter is disabled.
  305. * Filter can be enabled only for the TAP devices. */
  306. if (!check_filter(&tun->txflt, skb))
  307. goto drop;
  308. if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
  309. if (!(tun->flags & TUN_ONE_QUEUE)) {
  310. /* Normal queueing mode. */
  311. /* Packet scheduler handles dropping of further packets. */
  312. netif_stop_queue(dev);
  313. /* We won't see all dropped packets individually, so overrun
  314. * error is more appropriate. */
  315. dev->stats.tx_fifo_errors++;
  316. } else {
  317. /* Single queue mode.
  318. * Driver handles dropping of all packets itself. */
  319. goto drop;
  320. }
  321. }
  322. /* Enqueue packet */
  323. skb_queue_tail(&tun->readq, skb);
  324. dev->trans_start = jiffies;
  325. /* Notify and wake up reader process */
  326. if (tun->flags & TUN_FASYNC)
  327. kill_fasync(&tun->fasync, SIGIO, POLL_IN);
  328. wake_up_interruptible(&tun->socket.wait);
  329. return NETDEV_TX_OK;
  330. drop:
  331. dev->stats.tx_dropped++;
  332. kfree_skb(skb);
  333. return NETDEV_TX_OK;
  334. }
  335. static void tun_net_mclist(struct net_device *dev)
  336. {
  337. /*
  338. * This callback is supposed to deal with mc filter in
  339. * _rx_ path and has nothing to do with the _tx_ path.
  340. * In rx path we always accept everything userspace gives us.
  341. */
  342. return;
  343. }
  344. #define MIN_MTU 68
  345. #define MAX_MTU 65535
  346. static int
  347. tun_net_change_mtu(struct net_device *dev, int new_mtu)
  348. {
  349. if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
  350. return -EINVAL;
  351. dev->mtu = new_mtu;
  352. return 0;
  353. }
  354. static const struct net_device_ops tun_netdev_ops = {
  355. .ndo_uninit = tun_net_uninit,
  356. .ndo_open = tun_net_open,
  357. .ndo_stop = tun_net_close,
  358. .ndo_start_xmit = tun_net_xmit,
  359. .ndo_change_mtu = tun_net_change_mtu,
  360. };
  361. static const struct net_device_ops tap_netdev_ops = {
  362. .ndo_uninit = tun_net_uninit,
  363. .ndo_open = tun_net_open,
  364. .ndo_stop = tun_net_close,
  365. .ndo_start_xmit = tun_net_xmit,
  366. .ndo_change_mtu = tun_net_change_mtu,
  367. .ndo_set_multicast_list = tun_net_mclist,
  368. .ndo_set_mac_address = eth_mac_addr,
  369. .ndo_validate_addr = eth_validate_addr,
  370. };
  371. /* Initialize net device. */
  372. static void tun_net_init(struct net_device *dev)
  373. {
  374. struct tun_struct *tun = netdev_priv(dev);
  375. switch (tun->flags & TUN_TYPE_MASK) {
  376. case TUN_TUN_DEV:
  377. dev->netdev_ops = &tun_netdev_ops;
  378. /* Point-to-Point TUN Device */
  379. dev->hard_header_len = 0;
  380. dev->addr_len = 0;
  381. dev->mtu = 1500;
  382. /* Zero header length */
  383. dev->type = ARPHRD_NONE;
  384. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  385. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  386. break;
  387. case TUN_TAP_DEV:
  388. dev->netdev_ops = &tap_netdev_ops;
  389. /* Ethernet TAP Device */
  390. ether_setup(dev);
  391. random_ether_addr(dev->dev_addr);
  392. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  393. break;
  394. }
  395. }
  396. /* Character device part */
  397. /* Poll */
  398. static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
  399. {
  400. struct tun_file *tfile = file->private_data;
  401. struct tun_struct *tun = __tun_get(tfile);
  402. struct sock *sk;
  403. unsigned int mask = 0;
  404. if (!tun)
  405. return POLLERR;
  406. sk = tun->sk;
  407. DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
  408. poll_wait(file, &tun->socket.wait, wait);
  409. if (!skb_queue_empty(&tun->readq))
  410. mask |= POLLIN | POLLRDNORM;
  411. if (sock_writeable(sk) ||
  412. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
  413. sock_writeable(sk)))
  414. mask |= POLLOUT | POLLWRNORM;
  415. if (tun->dev->reg_state != NETREG_REGISTERED)
  416. mask = POLLERR;
  417. tun_put(tun);
  418. return mask;
  419. }
  420. /* prepad is the amount to reserve at front. len is length after that.
  421. * linear is a hint as to how much to copy (usually headers). */
  422. static inline struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
  423. size_t prepad, size_t len,
  424. size_t linear, int noblock)
  425. {
  426. struct sock *sk = tun->sk;
  427. struct sk_buff *skb;
  428. int err;
  429. /* Under a page? Don't bother with paged skb. */
  430. if (prepad + len < PAGE_SIZE || !linear)
  431. linear = len;
  432. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  433. &err);
  434. if (!skb)
  435. return ERR_PTR(err);
  436. skb_reserve(skb, prepad);
  437. skb_put(skb, linear);
  438. skb->data_len = len - linear;
  439. skb->len += len - linear;
  440. return skb;
  441. }
  442. /* Get packet from user space buffer */
  443. static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
  444. const struct iovec *iv, size_t count,
  445. int noblock)
  446. {
  447. struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
  448. struct sk_buff *skb;
  449. size_t len = count, align = 0;
  450. struct virtio_net_hdr gso = { 0 };
  451. int offset = 0;
  452. if (!(tun->flags & TUN_NO_PI)) {
  453. if ((len -= sizeof(pi)) > count)
  454. return -EINVAL;
  455. if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
  456. return -EFAULT;
  457. offset += sizeof(pi);
  458. }
  459. if (tun->flags & TUN_VNET_HDR) {
  460. if ((len -= sizeof(gso)) > count)
  461. return -EINVAL;
  462. if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
  463. return -EFAULT;
  464. if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  465. gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
  466. gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
  467. if (gso.hdr_len > len)
  468. return -EINVAL;
  469. offset += sizeof(gso);
  470. }
  471. if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
  472. align = NET_IP_ALIGN;
  473. if (unlikely(len < ETH_HLEN ||
  474. (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
  475. return -EINVAL;
  476. }
  477. skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
  478. if (IS_ERR(skb)) {
  479. if (PTR_ERR(skb) != -EAGAIN)
  480. tun->dev->stats.rx_dropped++;
  481. return PTR_ERR(skb);
  482. }
  483. if (skb_copy_datagram_from_iovec(skb, 0, iv, offset, len)) {
  484. tun->dev->stats.rx_dropped++;
  485. kfree_skb(skb);
  486. return -EFAULT;
  487. }
  488. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  489. if (!skb_partial_csum_set(skb, gso.csum_start,
  490. gso.csum_offset)) {
  491. tun->dev->stats.rx_frame_errors++;
  492. kfree_skb(skb);
  493. return -EINVAL;
  494. }
  495. } else if (tun->flags & TUN_NOCHECKSUM)
  496. skb->ip_summed = CHECKSUM_UNNECESSARY;
  497. switch (tun->flags & TUN_TYPE_MASK) {
  498. case TUN_TUN_DEV:
  499. if (tun->flags & TUN_NO_PI) {
  500. switch (skb->data[0] & 0xf0) {
  501. case 0x40:
  502. pi.proto = htons(ETH_P_IP);
  503. break;
  504. case 0x60:
  505. pi.proto = htons(ETH_P_IPV6);
  506. break;
  507. default:
  508. tun->dev->stats.rx_dropped++;
  509. kfree_skb(skb);
  510. return -EINVAL;
  511. }
  512. }
  513. skb_reset_mac_header(skb);
  514. skb->protocol = pi.proto;
  515. skb->dev = tun->dev;
  516. break;
  517. case TUN_TAP_DEV:
  518. skb->protocol = eth_type_trans(skb, tun->dev);
  519. break;
  520. };
  521. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  522. pr_debug("GSO!\n");
  523. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  524. case VIRTIO_NET_HDR_GSO_TCPV4:
  525. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  526. break;
  527. case VIRTIO_NET_HDR_GSO_TCPV6:
  528. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  529. break;
  530. case VIRTIO_NET_HDR_GSO_UDP:
  531. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  532. break;
  533. default:
  534. tun->dev->stats.rx_frame_errors++;
  535. kfree_skb(skb);
  536. return -EINVAL;
  537. }
  538. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  539. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  540. skb_shinfo(skb)->gso_size = gso.gso_size;
  541. if (skb_shinfo(skb)->gso_size == 0) {
  542. tun->dev->stats.rx_frame_errors++;
  543. kfree_skb(skb);
  544. return -EINVAL;
  545. }
  546. /* Header must be checked, and gso_segs computed. */
  547. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  548. skb_shinfo(skb)->gso_segs = 0;
  549. }
  550. netif_rx_ni(skb);
  551. tun->dev->stats.rx_packets++;
  552. tun->dev->stats.rx_bytes += len;
  553. return count;
  554. }
  555. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  556. unsigned long count, loff_t pos)
  557. {
  558. struct file *file = iocb->ki_filp;
  559. struct tun_struct *tun = tun_get(file);
  560. ssize_t result;
  561. if (!tun)
  562. return -EBADFD;
  563. DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
  564. result = tun_get_user(tun, iv, iov_length(iv, count),
  565. file->f_flags & O_NONBLOCK);
  566. tun_put(tun);
  567. return result;
  568. }
  569. /* Put packet to the user space buffer */
  570. static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
  571. struct sk_buff *skb,
  572. const struct iovec *iv, int len)
  573. {
  574. struct tun_pi pi = { 0, skb->protocol };
  575. ssize_t total = 0;
  576. if (!(tun->flags & TUN_NO_PI)) {
  577. if ((len -= sizeof(pi)) < 0)
  578. return -EINVAL;
  579. if (len < skb->len) {
  580. /* Packet will be striped */
  581. pi.flags |= TUN_PKT_STRIP;
  582. }
  583. if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
  584. return -EFAULT;
  585. total += sizeof(pi);
  586. }
  587. if (tun->flags & TUN_VNET_HDR) {
  588. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  589. if ((len -= sizeof(gso)) < 0)
  590. return -EINVAL;
  591. if (skb_is_gso(skb)) {
  592. struct skb_shared_info *sinfo = skb_shinfo(skb);
  593. /* This is a hint as to how much should be linear. */
  594. gso.hdr_len = skb_headlen(skb);
  595. gso.gso_size = sinfo->gso_size;
  596. if (sinfo->gso_type & SKB_GSO_TCPV4)
  597. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  598. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  599. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  600. else if (sinfo->gso_type & SKB_GSO_UDP)
  601. gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  602. else
  603. BUG();
  604. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  605. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  606. } else
  607. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  608. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  609. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  610. gso.csum_start = skb->csum_start - skb_headroom(skb);
  611. gso.csum_offset = skb->csum_offset;
  612. } /* else everything is zero */
  613. if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
  614. sizeof(gso))))
  615. return -EFAULT;
  616. total += sizeof(gso);
  617. }
  618. len = min_t(int, skb->len, len);
  619. skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
  620. total += len;
  621. tun->dev->stats.tx_packets++;
  622. tun->dev->stats.tx_bytes += len;
  623. return total;
  624. }
  625. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  626. unsigned long count, loff_t pos)
  627. {
  628. struct file *file = iocb->ki_filp;
  629. struct tun_file *tfile = file->private_data;
  630. struct tun_struct *tun = __tun_get(tfile);
  631. DECLARE_WAITQUEUE(wait, current);
  632. struct sk_buff *skb;
  633. ssize_t len, ret = 0;
  634. if (!tun)
  635. return -EBADFD;
  636. DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
  637. len = iov_length(iv, count);
  638. if (len < 0) {
  639. ret = -EINVAL;
  640. goto out;
  641. }
  642. add_wait_queue(&tun->socket.wait, &wait);
  643. while (len) {
  644. current->state = TASK_INTERRUPTIBLE;
  645. /* Read frames from the queue */
  646. if (!(skb=skb_dequeue(&tun->readq))) {
  647. if (file->f_flags & O_NONBLOCK) {
  648. ret = -EAGAIN;
  649. break;
  650. }
  651. if (signal_pending(current)) {
  652. ret = -ERESTARTSYS;
  653. break;
  654. }
  655. if (tun->dev->reg_state != NETREG_REGISTERED) {
  656. ret = -EIO;
  657. break;
  658. }
  659. /* Nothing to read, let's sleep */
  660. schedule();
  661. continue;
  662. }
  663. netif_wake_queue(tun->dev);
  664. ret = tun_put_user(tun, skb, iv, len);
  665. kfree_skb(skb);
  666. break;
  667. }
  668. current->state = TASK_RUNNING;
  669. remove_wait_queue(&tun->socket.wait, &wait);
  670. out:
  671. tun_put(tun);
  672. return ret;
  673. }
  674. static void tun_setup(struct net_device *dev)
  675. {
  676. struct tun_struct *tun = netdev_priv(dev);
  677. skb_queue_head_init(&tun->readq);
  678. tun->owner = -1;
  679. tun->group = -1;
  680. dev->ethtool_ops = &tun_ethtool_ops;
  681. dev->destructor = tun_free_netdev;
  682. }
  683. /* Trivial set of netlink ops to allow deleting tun or tap
  684. * device with netlink.
  685. */
  686. static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
  687. {
  688. return -EINVAL;
  689. }
  690. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  691. .kind = DRV_NAME,
  692. .priv_size = sizeof(struct tun_struct),
  693. .setup = tun_setup,
  694. .validate = tun_validate,
  695. };
  696. static void tun_sock_write_space(struct sock *sk)
  697. {
  698. struct tun_struct *tun;
  699. if (!sock_writeable(sk))
  700. return;
  701. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  702. return;
  703. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  704. wake_up_interruptible_sync(sk->sk_sleep);
  705. tun = container_of(sk, struct tun_sock, sk)->tun;
  706. kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
  707. }
  708. static void tun_sock_destruct(struct sock *sk)
  709. {
  710. free_netdev(container_of(sk, struct tun_sock, sk)->tun->dev);
  711. }
  712. static struct proto tun_proto = {
  713. .name = "tun",
  714. .owner = THIS_MODULE,
  715. .obj_size = sizeof(struct tun_sock),
  716. };
  717. static int tun_flags(struct tun_struct *tun)
  718. {
  719. int flags = 0;
  720. if (tun->flags & TUN_TUN_DEV)
  721. flags |= IFF_TUN;
  722. else
  723. flags |= IFF_TAP;
  724. if (tun->flags & TUN_NO_PI)
  725. flags |= IFF_NO_PI;
  726. if (tun->flags & TUN_ONE_QUEUE)
  727. flags |= IFF_ONE_QUEUE;
  728. if (tun->flags & TUN_VNET_HDR)
  729. flags |= IFF_VNET_HDR;
  730. return flags;
  731. }
  732. static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
  733. char *buf)
  734. {
  735. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  736. return sprintf(buf, "0x%x\n", tun_flags(tun));
  737. }
  738. static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
  739. char *buf)
  740. {
  741. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  742. return sprintf(buf, "%d\n", tun->owner);
  743. }
  744. static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
  745. char *buf)
  746. {
  747. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  748. return sprintf(buf, "%d\n", tun->group);
  749. }
  750. static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
  751. static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
  752. static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
  753. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  754. {
  755. struct sock *sk;
  756. struct tun_struct *tun;
  757. struct net_device *dev;
  758. int err;
  759. dev = __dev_get_by_name(net, ifr->ifr_name);
  760. if (dev) {
  761. if (ifr->ifr_flags & IFF_TUN_EXCL)
  762. return -EBUSY;
  763. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  764. tun = netdev_priv(dev);
  765. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  766. tun = netdev_priv(dev);
  767. else
  768. return -EINVAL;
  769. err = tun_attach(tun, file);
  770. if (err < 0)
  771. return err;
  772. }
  773. else {
  774. char *name;
  775. unsigned long flags = 0;
  776. err = -EINVAL;
  777. if (!capable(CAP_NET_ADMIN))
  778. return -EPERM;
  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. goto failed;
  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. tun->sk = sk;
  811. container_of(sk, struct tun_sock, sk)->tun = tun;
  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->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->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. lock_kernel();
  1045. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  1046. goto out;
  1047. if (on) {
  1048. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  1049. if (ret)
  1050. goto out;
  1051. tun->flags |= TUN_FASYNC;
  1052. } else
  1053. tun->flags &= ~TUN_FASYNC;
  1054. ret = 0;
  1055. out:
  1056. unlock_kernel();
  1057. tun_put(tun);
  1058. return ret;
  1059. }
  1060. static int tun_chr_open(struct inode *inode, struct file * file)
  1061. {
  1062. struct tun_file *tfile;
  1063. cycle_kernel_lock();
  1064. DBG1(KERN_INFO "tunX: tun_chr_open\n");
  1065. tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
  1066. if (!tfile)
  1067. return -ENOMEM;
  1068. atomic_set(&tfile->count, 0);
  1069. tfile->tun = NULL;
  1070. tfile->net = get_net(current->nsproxy->net_ns);
  1071. file->private_data = tfile;
  1072. return 0;
  1073. }
  1074. static int tun_chr_close(struct inode *inode, struct file *file)
  1075. {
  1076. struct tun_file *tfile = file->private_data;
  1077. struct tun_struct *tun;
  1078. tun = __tun_get(tfile);
  1079. if (tun) {
  1080. struct net_device *dev = tun->dev;
  1081. DBG(KERN_INFO "%s: tun_chr_close\n", dev->name);
  1082. __tun_detach(tun);
  1083. /* If desireable, unregister the netdevice. */
  1084. if (!(tun->flags & TUN_PERSIST)) {
  1085. rtnl_lock();
  1086. if (dev->reg_state == NETREG_REGISTERED)
  1087. unregister_netdevice(dev);
  1088. rtnl_unlock();
  1089. }
  1090. }
  1091. tun = tfile->tun;
  1092. if (tun)
  1093. sock_put(tun->sk);
  1094. put_net(tfile->net);
  1095. kfree(tfile);
  1096. return 0;
  1097. }
  1098. static const struct file_operations tun_fops = {
  1099. .owner = THIS_MODULE,
  1100. .llseek = no_llseek,
  1101. .read = do_sync_read,
  1102. .aio_read = tun_chr_aio_read,
  1103. .write = do_sync_write,
  1104. .aio_write = tun_chr_aio_write,
  1105. .poll = tun_chr_poll,
  1106. .unlocked_ioctl = tun_chr_ioctl,
  1107. .open = tun_chr_open,
  1108. .release = tun_chr_close,
  1109. .fasync = tun_chr_fasync
  1110. };
  1111. static struct miscdevice tun_miscdev = {
  1112. .minor = TUN_MINOR,
  1113. .name = "tun",
  1114. .devnode = "net/tun",
  1115. .fops = &tun_fops,
  1116. };
  1117. /* ethtool interface */
  1118. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1119. {
  1120. cmd->supported = 0;
  1121. cmd->advertising = 0;
  1122. cmd->speed = SPEED_10;
  1123. cmd->duplex = DUPLEX_FULL;
  1124. cmd->port = PORT_TP;
  1125. cmd->phy_address = 0;
  1126. cmd->transceiver = XCVR_INTERNAL;
  1127. cmd->autoneg = AUTONEG_DISABLE;
  1128. cmd->maxtxpkt = 0;
  1129. cmd->maxrxpkt = 0;
  1130. return 0;
  1131. }
  1132. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1133. {
  1134. struct tun_struct *tun = netdev_priv(dev);
  1135. strcpy(info->driver, DRV_NAME);
  1136. strcpy(info->version, DRV_VERSION);
  1137. strcpy(info->fw_version, "N/A");
  1138. switch (tun->flags & TUN_TYPE_MASK) {
  1139. case TUN_TUN_DEV:
  1140. strcpy(info->bus_info, "tun");
  1141. break;
  1142. case TUN_TAP_DEV:
  1143. strcpy(info->bus_info, "tap");
  1144. break;
  1145. }
  1146. }
  1147. static u32 tun_get_msglevel(struct net_device *dev)
  1148. {
  1149. #ifdef TUN_DEBUG
  1150. struct tun_struct *tun = netdev_priv(dev);
  1151. return tun->debug;
  1152. #else
  1153. return -EOPNOTSUPP;
  1154. #endif
  1155. }
  1156. static void tun_set_msglevel(struct net_device *dev, u32 value)
  1157. {
  1158. #ifdef TUN_DEBUG
  1159. struct tun_struct *tun = netdev_priv(dev);
  1160. tun->debug = value;
  1161. #endif
  1162. }
  1163. static u32 tun_get_link(struct net_device *dev)
  1164. {
  1165. struct tun_struct *tun = netdev_priv(dev);
  1166. return !!tun->tfile;
  1167. }
  1168. static u32 tun_get_rx_csum(struct net_device *dev)
  1169. {
  1170. struct tun_struct *tun = netdev_priv(dev);
  1171. return (tun->flags & TUN_NOCHECKSUM) == 0;
  1172. }
  1173. static int tun_set_rx_csum(struct net_device *dev, u32 data)
  1174. {
  1175. struct tun_struct *tun = netdev_priv(dev);
  1176. if (data)
  1177. tun->flags &= ~TUN_NOCHECKSUM;
  1178. else
  1179. tun->flags |= TUN_NOCHECKSUM;
  1180. return 0;
  1181. }
  1182. static const struct ethtool_ops tun_ethtool_ops = {
  1183. .get_settings = tun_get_settings,
  1184. .get_drvinfo = tun_get_drvinfo,
  1185. .get_msglevel = tun_get_msglevel,
  1186. .set_msglevel = tun_set_msglevel,
  1187. .get_link = tun_get_link,
  1188. .get_rx_csum = tun_get_rx_csum,
  1189. .set_rx_csum = tun_set_rx_csum
  1190. };
  1191. static int __init tun_init(void)
  1192. {
  1193. int ret = 0;
  1194. printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1195. printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
  1196. ret = rtnl_link_register(&tun_link_ops);
  1197. if (ret) {
  1198. printk(KERN_ERR "tun: Can't register link_ops\n");
  1199. goto err_linkops;
  1200. }
  1201. ret = misc_register(&tun_miscdev);
  1202. if (ret) {
  1203. printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
  1204. goto err_misc;
  1205. }
  1206. return 0;
  1207. err_misc:
  1208. rtnl_link_unregister(&tun_link_ops);
  1209. err_linkops:
  1210. return ret;
  1211. }
  1212. static void tun_cleanup(void)
  1213. {
  1214. misc_deregister(&tun_miscdev);
  1215. rtnl_link_unregister(&tun_link_ops);
  1216. }
  1217. module_init(tun_init);
  1218. module_exit(tun_cleanup);
  1219. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1220. MODULE_AUTHOR(DRV_COPYRIGHT);
  1221. MODULE_LICENSE("GPL");
  1222. MODULE_ALIAS_MISCDEV(TUN_MINOR);