tun.c 32 KB

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