tun.c 33 KB

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