tun.c 31 KB

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