tun.c 38 KB

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