tun.c 36 KB

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