tun.c 33 KB

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