tun.c 37 KB

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