tun.c 43 KB

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