tun.c 32 KB

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