tun.c 33 KB

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