tun.c 37 KB

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