tun.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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)
  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. return -EINVAL;
  465. }
  466. skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
  467. if (IS_ERR(skb)) {
  468. if (PTR_ERR(skb) != -EAGAIN)
  469. tun->dev->stats.rx_dropped++;
  470. return PTR_ERR(skb);
  471. }
  472. if (skb_copy_datagram_from_iovec(skb, 0, iv, len)) {
  473. tun->dev->stats.rx_dropped++;
  474. kfree_skb(skb);
  475. return -EFAULT;
  476. }
  477. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  478. if (!skb_partial_csum_set(skb, gso.csum_start,
  479. gso.csum_offset)) {
  480. tun->dev->stats.rx_frame_errors++;
  481. kfree_skb(skb);
  482. return -EINVAL;
  483. }
  484. } else if (tun->flags & TUN_NOCHECKSUM)
  485. skb->ip_summed = CHECKSUM_UNNECESSARY;
  486. switch (tun->flags & TUN_TYPE_MASK) {
  487. case TUN_TUN_DEV:
  488. if (tun->flags & TUN_NO_PI) {
  489. switch (skb->data[0] & 0xf0) {
  490. case 0x40:
  491. pi.proto = htons(ETH_P_IP);
  492. break;
  493. case 0x60:
  494. pi.proto = htons(ETH_P_IPV6);
  495. break;
  496. default:
  497. tun->dev->stats.rx_dropped++;
  498. kfree_skb(skb);
  499. return -EINVAL;
  500. }
  501. }
  502. skb_reset_mac_header(skb);
  503. skb->protocol = pi.proto;
  504. skb->dev = tun->dev;
  505. break;
  506. case TUN_TAP_DEV:
  507. skb->protocol = eth_type_trans(skb, tun->dev);
  508. break;
  509. };
  510. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  511. pr_debug("GSO!\n");
  512. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  513. case VIRTIO_NET_HDR_GSO_TCPV4:
  514. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  515. break;
  516. case VIRTIO_NET_HDR_GSO_TCPV6:
  517. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  518. break;
  519. default:
  520. tun->dev->stats.rx_frame_errors++;
  521. kfree_skb(skb);
  522. return -EINVAL;
  523. }
  524. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  525. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  526. skb_shinfo(skb)->gso_size = gso.gso_size;
  527. if (skb_shinfo(skb)->gso_size == 0) {
  528. tun->dev->stats.rx_frame_errors++;
  529. kfree_skb(skb);
  530. return -EINVAL;
  531. }
  532. /* Header must be checked, and gso_segs computed. */
  533. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  534. skb_shinfo(skb)->gso_segs = 0;
  535. }
  536. netif_rx_ni(skb);
  537. tun->dev->stats.rx_packets++;
  538. tun->dev->stats.rx_bytes += len;
  539. return count;
  540. }
  541. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  542. unsigned long count, loff_t pos)
  543. {
  544. struct file *file = iocb->ki_filp;
  545. struct tun_struct *tun = tun_get(file);
  546. ssize_t result;
  547. if (!tun)
  548. return -EBADFD;
  549. DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
  550. result = tun_get_user(tun, (struct iovec *)iv, iov_length(iv, count),
  551. file->f_flags & O_NONBLOCK);
  552. tun_put(tun);
  553. return result;
  554. }
  555. /* Put packet to the user space buffer */
  556. static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
  557. struct sk_buff *skb,
  558. struct iovec *iv, int len)
  559. {
  560. struct tun_pi pi = { 0, skb->protocol };
  561. ssize_t total = 0;
  562. if (!(tun->flags & TUN_NO_PI)) {
  563. if ((len -= sizeof(pi)) < 0)
  564. return -EINVAL;
  565. if (len < skb->len) {
  566. /* Packet will be striped */
  567. pi.flags |= TUN_PKT_STRIP;
  568. }
  569. if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
  570. return -EFAULT;
  571. total += sizeof(pi);
  572. }
  573. if (tun->flags & TUN_VNET_HDR) {
  574. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  575. if ((len -= sizeof(gso)) < 0)
  576. return -EINVAL;
  577. if (skb_is_gso(skb)) {
  578. struct skb_shared_info *sinfo = skb_shinfo(skb);
  579. /* This is a hint as to how much should be linear. */
  580. gso.hdr_len = skb_headlen(skb);
  581. gso.gso_size = sinfo->gso_size;
  582. if (sinfo->gso_type & SKB_GSO_TCPV4)
  583. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  584. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  585. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  586. else
  587. BUG();
  588. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  589. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  590. } else
  591. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  592. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  593. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  594. gso.csum_start = skb->csum_start - skb_headroom(skb);
  595. gso.csum_offset = skb->csum_offset;
  596. } /* else everything is zero */
  597. if (unlikely(memcpy_toiovec(iv, (void *)&gso, sizeof(gso))))
  598. return -EFAULT;
  599. total += sizeof(gso);
  600. }
  601. len = min_t(int, skb->len, len);
  602. skb_copy_datagram_iovec(skb, 0, iv, len);
  603. total += len;
  604. tun->dev->stats.tx_packets++;
  605. tun->dev->stats.tx_bytes += len;
  606. return total;
  607. }
  608. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  609. unsigned long count, loff_t pos)
  610. {
  611. struct file *file = iocb->ki_filp;
  612. struct tun_file *tfile = file->private_data;
  613. struct tun_struct *tun = __tun_get(tfile);
  614. DECLARE_WAITQUEUE(wait, current);
  615. struct sk_buff *skb;
  616. ssize_t len, ret = 0;
  617. if (!tun)
  618. return -EBADFD;
  619. DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
  620. len = iov_length(iv, count);
  621. if (len < 0) {
  622. ret = -EINVAL;
  623. goto out;
  624. }
  625. add_wait_queue(&tfile->read_wait, &wait);
  626. while (len) {
  627. current->state = TASK_INTERRUPTIBLE;
  628. /* Read frames from the queue */
  629. if (!(skb=skb_dequeue(&tun->readq))) {
  630. if (file->f_flags & O_NONBLOCK) {
  631. ret = -EAGAIN;
  632. break;
  633. }
  634. if (signal_pending(current)) {
  635. ret = -ERESTARTSYS;
  636. break;
  637. }
  638. if (tun->dev->reg_state != NETREG_REGISTERED) {
  639. ret = -EIO;
  640. break;
  641. }
  642. /* Nothing to read, let's sleep */
  643. schedule();
  644. continue;
  645. }
  646. netif_wake_queue(tun->dev);
  647. ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
  648. kfree_skb(skb);
  649. break;
  650. }
  651. current->state = TASK_RUNNING;
  652. remove_wait_queue(&tfile->read_wait, &wait);
  653. out:
  654. tun_put(tun);
  655. return ret;
  656. }
  657. static void tun_setup(struct net_device *dev)
  658. {
  659. struct tun_struct *tun = netdev_priv(dev);
  660. skb_queue_head_init(&tun->readq);
  661. tun->owner = -1;
  662. tun->group = -1;
  663. dev->ethtool_ops = &tun_ethtool_ops;
  664. dev->destructor = free_netdev;
  665. }
  666. /* Trivial set of netlink ops to allow deleting tun or tap
  667. * device with netlink.
  668. */
  669. static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
  670. {
  671. return -EINVAL;
  672. }
  673. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  674. .kind = DRV_NAME,
  675. .priv_size = sizeof(struct tun_struct),
  676. .setup = tun_setup,
  677. .validate = tun_validate,
  678. };
  679. static void tun_sock_write_space(struct sock *sk)
  680. {
  681. struct tun_struct *tun;
  682. if (!sock_writeable(sk))
  683. return;
  684. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  685. wake_up_interruptible_sync(sk->sk_sleep);
  686. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  687. return;
  688. tun = container_of(sk, struct tun_sock, sk)->tun;
  689. kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
  690. }
  691. static void tun_sock_destruct(struct sock *sk)
  692. {
  693. dev_put(container_of(sk, struct tun_sock, sk)->tun->dev);
  694. }
  695. static struct proto tun_proto = {
  696. .name = "tun",
  697. .owner = THIS_MODULE,
  698. .obj_size = sizeof(struct tun_sock),
  699. };
  700. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  701. {
  702. struct sock *sk;
  703. struct tun_struct *tun;
  704. struct net_device *dev;
  705. struct tun_file *tfile = file->private_data;
  706. int err;
  707. dev = __dev_get_by_name(net, ifr->ifr_name);
  708. if (dev) {
  709. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  710. tun = netdev_priv(dev);
  711. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  712. tun = netdev_priv(dev);
  713. else
  714. return -EINVAL;
  715. err = tun_attach(tun, file);
  716. if (err < 0)
  717. return err;
  718. }
  719. else {
  720. char *name;
  721. unsigned long flags = 0;
  722. err = -EINVAL;
  723. if (!capable(CAP_NET_ADMIN))
  724. return -EPERM;
  725. /* Set dev type */
  726. if (ifr->ifr_flags & IFF_TUN) {
  727. /* TUN device */
  728. flags |= TUN_TUN_DEV;
  729. name = "tun%d";
  730. } else if (ifr->ifr_flags & IFF_TAP) {
  731. /* TAP device */
  732. flags |= TUN_TAP_DEV;
  733. name = "tap%d";
  734. } else
  735. goto failed;
  736. if (*ifr->ifr_name)
  737. name = ifr->ifr_name;
  738. dev = alloc_netdev(sizeof(struct tun_struct), name,
  739. tun_setup);
  740. if (!dev)
  741. return -ENOMEM;
  742. dev_net_set(dev, net);
  743. dev->rtnl_link_ops = &tun_link_ops;
  744. tun = netdev_priv(dev);
  745. tun->dev = dev;
  746. tun->flags = flags;
  747. tun->txflt.count = 0;
  748. err = -ENOMEM;
  749. sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
  750. if (!sk)
  751. goto err_free_dev;
  752. /* This ref count is for tun->sk. */
  753. dev_hold(dev);
  754. sock_init_data(&tun->socket, sk);
  755. sk->sk_write_space = tun_sock_write_space;
  756. sk->sk_destruct = tun_sock_destruct;
  757. sk->sk_sndbuf = INT_MAX;
  758. sk->sk_sleep = &tfile->read_wait;
  759. tun->sk = sk;
  760. container_of(sk, struct tun_sock, sk)->tun = tun;
  761. tun_net_init(dev);
  762. if (strchr(dev->name, '%')) {
  763. err = dev_alloc_name(dev, dev->name);
  764. if (err < 0)
  765. goto err_free_sk;
  766. }
  767. err = -EINVAL;
  768. err = register_netdevice(tun->dev);
  769. if (err < 0)
  770. goto err_free_dev;
  771. err = tun_attach(tun, file);
  772. if (err < 0)
  773. goto err_free_dev;
  774. }
  775. DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
  776. if (ifr->ifr_flags & IFF_NO_PI)
  777. tun->flags |= TUN_NO_PI;
  778. else
  779. tun->flags &= ~TUN_NO_PI;
  780. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  781. tun->flags |= TUN_ONE_QUEUE;
  782. else
  783. tun->flags &= ~TUN_ONE_QUEUE;
  784. if (ifr->ifr_flags & IFF_VNET_HDR)
  785. tun->flags |= TUN_VNET_HDR;
  786. else
  787. tun->flags &= ~TUN_VNET_HDR;
  788. /* Make sure persistent devices do not get stuck in
  789. * xoff state.
  790. */
  791. if (netif_running(tun->dev))
  792. netif_wake_queue(tun->dev);
  793. strcpy(ifr->ifr_name, tun->dev->name);
  794. return 0;
  795. err_free_sk:
  796. sock_put(sk);
  797. err_free_dev:
  798. free_netdev(dev);
  799. failed:
  800. return err;
  801. }
  802. static int tun_get_iff(struct net *net, struct file *file, struct ifreq *ifr)
  803. {
  804. struct tun_struct *tun = tun_get(file);
  805. if (!tun)
  806. return -EBADFD;
  807. DBG(KERN_INFO "%s: tun_get_iff\n", tun->dev->name);
  808. strcpy(ifr->ifr_name, tun->dev->name);
  809. ifr->ifr_flags = 0;
  810. if (ifr->ifr_flags & TUN_TUN_DEV)
  811. ifr->ifr_flags |= IFF_TUN;
  812. else
  813. ifr->ifr_flags |= IFF_TAP;
  814. if (tun->flags & TUN_NO_PI)
  815. ifr->ifr_flags |= IFF_NO_PI;
  816. if (tun->flags & TUN_ONE_QUEUE)
  817. ifr->ifr_flags |= IFF_ONE_QUEUE;
  818. if (tun->flags & TUN_VNET_HDR)
  819. ifr->ifr_flags |= IFF_VNET_HDR;
  820. tun_put(tun);
  821. return 0;
  822. }
  823. /* This is like a cut-down ethtool ops, except done via tun fd so no
  824. * privs required. */
  825. static int set_offload(struct net_device *dev, unsigned long arg)
  826. {
  827. unsigned int old_features, features;
  828. old_features = dev->features;
  829. /* Unset features, set them as we chew on the arg. */
  830. features = (old_features & ~(NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST
  831. |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6));
  832. if (arg & TUN_F_CSUM) {
  833. features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  834. arg &= ~TUN_F_CSUM;
  835. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  836. if (arg & TUN_F_TSO_ECN) {
  837. features |= NETIF_F_TSO_ECN;
  838. arg &= ~TUN_F_TSO_ECN;
  839. }
  840. if (arg & TUN_F_TSO4)
  841. features |= NETIF_F_TSO;
  842. if (arg & TUN_F_TSO6)
  843. features |= NETIF_F_TSO6;
  844. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  845. }
  846. }
  847. /* This gives the user a way to test for new features in future by
  848. * trying to set them. */
  849. if (arg)
  850. return -EINVAL;
  851. dev->features = features;
  852. if (old_features != dev->features)
  853. netdev_features_change(dev);
  854. return 0;
  855. }
  856. static int tun_chr_ioctl(struct inode *inode, struct file *file,
  857. unsigned int cmd, unsigned long arg)
  858. {
  859. struct tun_file *tfile = file->private_data;
  860. struct tun_struct *tun;
  861. void __user* argp = (void __user*)arg;
  862. struct ifreq ifr;
  863. int sndbuf;
  864. int ret;
  865. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
  866. if (copy_from_user(&ifr, argp, sizeof ifr))
  867. return -EFAULT;
  868. if (cmd == TUNGETFEATURES) {
  869. /* Currently this just means: "what IFF flags are valid?".
  870. * This is needed because we never checked for invalid flags on
  871. * TUNSETIFF. */
  872. return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
  873. IFF_VNET_HDR,
  874. (unsigned int __user*)argp);
  875. }
  876. tun = __tun_get(tfile);
  877. if (cmd == TUNSETIFF && !tun) {
  878. int err;
  879. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  880. rtnl_lock();
  881. err = tun_set_iff(tfile->net, file, &ifr);
  882. rtnl_unlock();
  883. if (err)
  884. return err;
  885. if (copy_to_user(argp, &ifr, sizeof(ifr)))
  886. return -EFAULT;
  887. return 0;
  888. }
  889. if (!tun)
  890. return -EBADFD;
  891. DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
  892. ret = 0;
  893. switch (cmd) {
  894. case TUNGETIFF:
  895. ret = tun_get_iff(current->nsproxy->net_ns, file, &ifr);
  896. if (ret)
  897. break;
  898. if (copy_to_user(argp, &ifr, sizeof(ifr)))
  899. ret = -EFAULT;
  900. break;
  901. case TUNSETNOCSUM:
  902. /* Disable/Enable checksum */
  903. if (arg)
  904. tun->flags |= TUN_NOCHECKSUM;
  905. else
  906. tun->flags &= ~TUN_NOCHECKSUM;
  907. DBG(KERN_INFO "%s: checksum %s\n",
  908. tun->dev->name, arg ? "disabled" : "enabled");
  909. break;
  910. case TUNSETPERSIST:
  911. /* Disable/Enable persist mode */
  912. if (arg)
  913. tun->flags |= TUN_PERSIST;
  914. else
  915. tun->flags &= ~TUN_PERSIST;
  916. DBG(KERN_INFO "%s: persist %s\n",
  917. tun->dev->name, arg ? "enabled" : "disabled");
  918. break;
  919. case TUNSETOWNER:
  920. /* Set owner of the device */
  921. tun->owner = (uid_t) arg;
  922. DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
  923. break;
  924. case TUNSETGROUP:
  925. /* Set group of the device */
  926. tun->group= (gid_t) arg;
  927. DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
  928. break;
  929. case TUNSETLINK:
  930. /* Only allow setting the type when the interface is down */
  931. rtnl_lock();
  932. if (tun->dev->flags & IFF_UP) {
  933. DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
  934. tun->dev->name);
  935. ret = -EBUSY;
  936. } else {
  937. tun->dev->type = (int) arg;
  938. DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
  939. ret = 0;
  940. }
  941. rtnl_unlock();
  942. break;
  943. #ifdef TUN_DEBUG
  944. case TUNSETDEBUG:
  945. tun->debug = arg;
  946. break;
  947. #endif
  948. case TUNSETOFFLOAD:
  949. rtnl_lock();
  950. ret = set_offload(tun->dev, arg);
  951. rtnl_unlock();
  952. break;
  953. case TUNSETTXFILTER:
  954. /* Can be set only for TAPs */
  955. ret = -EINVAL;
  956. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  957. break;
  958. rtnl_lock();
  959. ret = update_filter(&tun->txflt, (void __user *)arg);
  960. rtnl_unlock();
  961. break;
  962. case SIOCGIFHWADDR:
  963. /* Get hw addres */
  964. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  965. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  966. if (copy_to_user(argp, &ifr, sizeof ifr))
  967. ret = -EFAULT;
  968. break;
  969. case SIOCSIFHWADDR:
  970. /* Set hw address */
  971. DBG(KERN_DEBUG "%s: set hw address: %pM\n",
  972. tun->dev->name, ifr.ifr_hwaddr.sa_data);
  973. rtnl_lock();
  974. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  975. rtnl_unlock();
  976. break;
  977. case TUNGETSNDBUF:
  978. sndbuf = tun->sk->sk_sndbuf;
  979. if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
  980. ret = -EFAULT;
  981. break;
  982. case TUNSETSNDBUF:
  983. if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
  984. ret = -EFAULT;
  985. break;
  986. }
  987. tun->sk->sk_sndbuf = sndbuf;
  988. break;
  989. default:
  990. ret = -EINVAL;
  991. break;
  992. };
  993. tun_put(tun);
  994. return ret;
  995. }
  996. static int tun_chr_fasync(int fd, struct file *file, int on)
  997. {
  998. struct tun_struct *tun = tun_get(file);
  999. int ret;
  1000. if (!tun)
  1001. return -EBADFD;
  1002. DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
  1003. lock_kernel();
  1004. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  1005. goto out;
  1006. if (on) {
  1007. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  1008. if (ret)
  1009. goto out;
  1010. tun->flags |= TUN_FASYNC;
  1011. } else
  1012. tun->flags &= ~TUN_FASYNC;
  1013. ret = 0;
  1014. out:
  1015. unlock_kernel();
  1016. tun_put(tun);
  1017. return ret;
  1018. }
  1019. static int tun_chr_open(struct inode *inode, struct file * file)
  1020. {
  1021. struct tun_file *tfile;
  1022. cycle_kernel_lock();
  1023. DBG1(KERN_INFO "tunX: tun_chr_open\n");
  1024. tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
  1025. if (!tfile)
  1026. return -ENOMEM;
  1027. atomic_set(&tfile->count, 0);
  1028. tfile->tun = NULL;
  1029. tfile->net = get_net(current->nsproxy->net_ns);
  1030. init_waitqueue_head(&tfile->read_wait);
  1031. file->private_data = tfile;
  1032. return 0;
  1033. }
  1034. static int tun_chr_close(struct inode *inode, struct file *file)
  1035. {
  1036. struct tun_file *tfile = file->private_data;
  1037. struct tun_struct *tun = __tun_get(tfile);
  1038. if (tun) {
  1039. DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
  1040. rtnl_lock();
  1041. __tun_detach(tun);
  1042. /* If desireable, unregister the netdevice. */
  1043. if (!(tun->flags & TUN_PERSIST)) {
  1044. sock_put(tun->sk);
  1045. unregister_netdevice(tun->dev);
  1046. }
  1047. rtnl_unlock();
  1048. }
  1049. put_net(tfile->net);
  1050. kfree(tfile);
  1051. return 0;
  1052. }
  1053. static const struct file_operations tun_fops = {
  1054. .owner = THIS_MODULE,
  1055. .llseek = no_llseek,
  1056. .read = do_sync_read,
  1057. .aio_read = tun_chr_aio_read,
  1058. .write = do_sync_write,
  1059. .aio_write = tun_chr_aio_write,
  1060. .poll = tun_chr_poll,
  1061. .ioctl = tun_chr_ioctl,
  1062. .open = tun_chr_open,
  1063. .release = tun_chr_close,
  1064. .fasync = tun_chr_fasync
  1065. };
  1066. static struct miscdevice tun_miscdev = {
  1067. .minor = TUN_MINOR,
  1068. .name = "tun",
  1069. .fops = &tun_fops,
  1070. };
  1071. /* ethtool interface */
  1072. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1073. {
  1074. cmd->supported = 0;
  1075. cmd->advertising = 0;
  1076. cmd->speed = SPEED_10;
  1077. cmd->duplex = DUPLEX_FULL;
  1078. cmd->port = PORT_TP;
  1079. cmd->phy_address = 0;
  1080. cmd->transceiver = XCVR_INTERNAL;
  1081. cmd->autoneg = AUTONEG_DISABLE;
  1082. cmd->maxtxpkt = 0;
  1083. cmd->maxrxpkt = 0;
  1084. return 0;
  1085. }
  1086. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1087. {
  1088. struct tun_struct *tun = netdev_priv(dev);
  1089. strcpy(info->driver, DRV_NAME);
  1090. strcpy(info->version, DRV_VERSION);
  1091. strcpy(info->fw_version, "N/A");
  1092. switch (tun->flags & TUN_TYPE_MASK) {
  1093. case TUN_TUN_DEV:
  1094. strcpy(info->bus_info, "tun");
  1095. break;
  1096. case TUN_TAP_DEV:
  1097. strcpy(info->bus_info, "tap");
  1098. break;
  1099. }
  1100. }
  1101. static u32 tun_get_msglevel(struct net_device *dev)
  1102. {
  1103. #ifdef TUN_DEBUG
  1104. struct tun_struct *tun = netdev_priv(dev);
  1105. return tun->debug;
  1106. #else
  1107. return -EOPNOTSUPP;
  1108. #endif
  1109. }
  1110. static void tun_set_msglevel(struct net_device *dev, u32 value)
  1111. {
  1112. #ifdef TUN_DEBUG
  1113. struct tun_struct *tun = netdev_priv(dev);
  1114. tun->debug = value;
  1115. #endif
  1116. }
  1117. static u32 tun_get_link(struct net_device *dev)
  1118. {
  1119. struct tun_struct *tun = netdev_priv(dev);
  1120. return !!tun->tfile;
  1121. }
  1122. static u32 tun_get_rx_csum(struct net_device *dev)
  1123. {
  1124. struct tun_struct *tun = netdev_priv(dev);
  1125. return (tun->flags & TUN_NOCHECKSUM) == 0;
  1126. }
  1127. static int tun_set_rx_csum(struct net_device *dev, u32 data)
  1128. {
  1129. struct tun_struct *tun = netdev_priv(dev);
  1130. if (data)
  1131. tun->flags &= ~TUN_NOCHECKSUM;
  1132. else
  1133. tun->flags |= TUN_NOCHECKSUM;
  1134. return 0;
  1135. }
  1136. static const struct ethtool_ops tun_ethtool_ops = {
  1137. .get_settings = tun_get_settings,
  1138. .get_drvinfo = tun_get_drvinfo,
  1139. .get_msglevel = tun_get_msglevel,
  1140. .set_msglevel = tun_set_msglevel,
  1141. .get_link = tun_get_link,
  1142. .get_rx_csum = tun_get_rx_csum,
  1143. .set_rx_csum = tun_set_rx_csum
  1144. };
  1145. static int __init tun_init(void)
  1146. {
  1147. int ret = 0;
  1148. printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1149. printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
  1150. ret = rtnl_link_register(&tun_link_ops);
  1151. if (ret) {
  1152. printk(KERN_ERR "tun: Can't register link_ops\n");
  1153. goto err_linkops;
  1154. }
  1155. ret = misc_register(&tun_miscdev);
  1156. if (ret) {
  1157. printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
  1158. goto err_misc;
  1159. }
  1160. return 0;
  1161. err_misc:
  1162. rtnl_link_unregister(&tun_link_ops);
  1163. err_linkops:
  1164. return ret;
  1165. }
  1166. static void tun_cleanup(void)
  1167. {
  1168. misc_deregister(&tun_miscdev);
  1169. rtnl_link_unregister(&tun_link_ops);
  1170. }
  1171. module_init(tun_init);
  1172. module_exit(tun_cleanup);
  1173. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1174. MODULE_AUTHOR(DRV_COPYRIGHT);
  1175. MODULE_LICENSE("GPL");
  1176. MODULE_ALIAS_MISCDEV(TUN_MINOR);