tun.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426
  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/if_vlan.h>
  60. #include <linux/crc32.h>
  61. #include <linux/nsproxy.h>
  62. #include <linux/virtio_net.h>
  63. #include <linux/rcupdate.h>
  64. #include <net/net_namespace.h>
  65. #include <net/netns/generic.h>
  66. #include <net/rtnetlink.h>
  67. #include <net/sock.h>
  68. #include <linux/seq_file.h>
  69. #include <linux/uio.h>
  70. #include <asm/uaccess.h>
  71. /* Uncomment to enable debugging */
  72. /* #define TUN_DEBUG 1 */
  73. #ifdef TUN_DEBUG
  74. static int debug;
  75. #define tun_debug(level, tun, fmt, args...) \
  76. do { \
  77. if (tun->debug) \
  78. netdev_printk(level, tun->dev, fmt, ##args); \
  79. } while (0)
  80. #define DBG1(level, fmt, args...) \
  81. do { \
  82. if (debug == 2) \
  83. printk(level fmt, ##args); \
  84. } while (0)
  85. #else
  86. #define tun_debug(level, tun, fmt, args...) \
  87. do { \
  88. if (0) \
  89. netdev_printk(level, tun->dev, fmt, ##args); \
  90. } while (0)
  91. #define DBG1(level, fmt, args...) \
  92. do { \
  93. if (0) \
  94. printk(level fmt, ##args); \
  95. } while (0)
  96. #endif
  97. /* TUN device flags */
  98. /* IFF_ATTACH_QUEUE is never stored in device flags,
  99. * overload it to mean fasync when stored there.
  100. */
  101. #define TUN_FASYNC IFF_ATTACH_QUEUE
  102. /* High bits in flags field are unused. */
  103. #define TUN_VNET_LE 0x80000000
  104. #define TUN_VNET_BE 0x40000000
  105. #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
  106. IFF_MULTI_QUEUE)
  107. #define GOODCOPY_LEN 128
  108. #define FLT_EXACT_COUNT 8
  109. struct tap_filter {
  110. unsigned int count; /* Number of addrs. Zero means disabled */
  111. u32 mask[2]; /* Mask of the hashed addrs */
  112. unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
  113. };
  114. /* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal
  115. * to max number of VCPUs in guest. */
  116. #define MAX_TAP_QUEUES 256
  117. #define MAX_TAP_FLOWS 4096
  118. #define TUN_FLOW_EXPIRE (3 * HZ)
  119. /* A tun_file connects an open character device to a tuntap netdevice. It
  120. * also contains all socket related structures (except sock_fprog and tap_filter)
  121. * to serve as one transmit queue for tuntap device. The sock_fprog and
  122. * tap_filter were kept in tun_struct since they were used for filtering for the
  123. * netdevice not for a specific queue (at least I didn't see the requirement for
  124. * this).
  125. *
  126. * RCU usage:
  127. * The tun_file and tun_struct are loosely coupled, the pointer from one to the
  128. * other can only be read while rcu_read_lock or rtnl_lock is held.
  129. */
  130. struct tun_file {
  131. struct sock sk;
  132. struct socket socket;
  133. struct socket_wq wq;
  134. struct tun_struct __rcu *tun;
  135. struct fasync_struct *fasync;
  136. /* only used for fasnyc */
  137. unsigned int flags;
  138. union {
  139. u16 queue_index;
  140. unsigned int ifindex;
  141. };
  142. struct list_head next;
  143. struct tun_struct *detached;
  144. };
  145. struct tun_flow_entry {
  146. struct hlist_node hash_link;
  147. struct rcu_head rcu;
  148. struct tun_struct *tun;
  149. u32 rxhash;
  150. u32 rps_rxhash;
  151. int queue_index;
  152. unsigned long updated;
  153. };
  154. #define TUN_NUM_FLOW_ENTRIES 1024
  155. /* Since the socket were moved to tun_file, to preserve the behavior of persist
  156. * device, socket filter, sndbuf and vnet header size were restore when the
  157. * file were attached to a persist device.
  158. */
  159. struct tun_struct {
  160. struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
  161. unsigned int numqueues;
  162. unsigned int flags;
  163. kuid_t owner;
  164. kgid_t group;
  165. struct net_device *dev;
  166. netdev_features_t set_features;
  167. #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
  168. NETIF_F_TSO6|NETIF_F_UFO)
  169. int vnet_hdr_sz;
  170. int sndbuf;
  171. struct tap_filter txflt;
  172. struct sock_fprog fprog;
  173. /* protected by rtnl lock */
  174. bool filter_attached;
  175. #ifdef TUN_DEBUG
  176. int debug;
  177. #endif
  178. spinlock_t lock;
  179. struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
  180. struct timer_list flow_gc_timer;
  181. unsigned long ageing_time;
  182. unsigned int numdisabled;
  183. struct list_head disabled;
  184. void *security;
  185. u32 flow_count;
  186. };
  187. #ifdef CONFIG_TUN_VNET_CROSS_LE
  188. static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
  189. {
  190. return tun->flags & TUN_VNET_BE ? false :
  191. virtio_legacy_is_little_endian();
  192. }
  193. static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
  194. {
  195. int be = !!(tun->flags & TUN_VNET_BE);
  196. if (put_user(be, argp))
  197. return -EFAULT;
  198. return 0;
  199. }
  200. static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
  201. {
  202. int be;
  203. if (get_user(be, argp))
  204. return -EFAULT;
  205. if (be)
  206. tun->flags |= TUN_VNET_BE;
  207. else
  208. tun->flags &= ~TUN_VNET_BE;
  209. return 0;
  210. }
  211. #else
  212. static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
  213. {
  214. return virtio_legacy_is_little_endian();
  215. }
  216. static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
  217. {
  218. return -EINVAL;
  219. }
  220. static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
  221. {
  222. return -EINVAL;
  223. }
  224. #endif /* CONFIG_TUN_VNET_CROSS_LE */
  225. static inline bool tun_is_little_endian(struct tun_struct *tun)
  226. {
  227. return tun->flags & TUN_VNET_LE ||
  228. tun_legacy_is_little_endian(tun);
  229. }
  230. static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
  231. {
  232. return __virtio16_to_cpu(tun_is_little_endian(tun), val);
  233. }
  234. static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
  235. {
  236. return __cpu_to_virtio16(tun_is_little_endian(tun), val);
  237. }
  238. static inline u32 tun_hashfn(u32 rxhash)
  239. {
  240. return rxhash & 0x3ff;
  241. }
  242. static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
  243. {
  244. struct tun_flow_entry *e;
  245. hlist_for_each_entry_rcu(e, head, hash_link) {
  246. if (e->rxhash == rxhash)
  247. return e;
  248. }
  249. return NULL;
  250. }
  251. static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
  252. struct hlist_head *head,
  253. u32 rxhash, u16 queue_index)
  254. {
  255. struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
  256. if (e) {
  257. tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
  258. rxhash, queue_index);
  259. e->updated = jiffies;
  260. e->rxhash = rxhash;
  261. e->rps_rxhash = 0;
  262. e->queue_index = queue_index;
  263. e->tun = tun;
  264. hlist_add_head_rcu(&e->hash_link, head);
  265. ++tun->flow_count;
  266. }
  267. return e;
  268. }
  269. static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
  270. {
  271. tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
  272. e->rxhash, e->queue_index);
  273. hlist_del_rcu(&e->hash_link);
  274. kfree_rcu(e, rcu);
  275. --tun->flow_count;
  276. }
  277. static void tun_flow_flush(struct tun_struct *tun)
  278. {
  279. int i;
  280. spin_lock_bh(&tun->lock);
  281. for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
  282. struct tun_flow_entry *e;
  283. struct hlist_node *n;
  284. hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
  285. tun_flow_delete(tun, e);
  286. }
  287. spin_unlock_bh(&tun->lock);
  288. }
  289. static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
  290. {
  291. int i;
  292. spin_lock_bh(&tun->lock);
  293. for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
  294. struct tun_flow_entry *e;
  295. struct hlist_node *n;
  296. hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
  297. if (e->queue_index == queue_index)
  298. tun_flow_delete(tun, e);
  299. }
  300. }
  301. spin_unlock_bh(&tun->lock);
  302. }
  303. static void tun_flow_cleanup(unsigned long data)
  304. {
  305. struct tun_struct *tun = (struct tun_struct *)data;
  306. unsigned long delay = tun->ageing_time;
  307. unsigned long next_timer = jiffies + delay;
  308. unsigned long count = 0;
  309. int i;
  310. tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
  311. spin_lock_bh(&tun->lock);
  312. for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
  313. struct tun_flow_entry *e;
  314. struct hlist_node *n;
  315. hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
  316. unsigned long this_timer;
  317. count++;
  318. this_timer = e->updated + delay;
  319. if (time_before_eq(this_timer, jiffies))
  320. tun_flow_delete(tun, e);
  321. else if (time_before(this_timer, next_timer))
  322. next_timer = this_timer;
  323. }
  324. }
  325. if (count)
  326. mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
  327. spin_unlock_bh(&tun->lock);
  328. }
  329. static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
  330. struct tun_file *tfile)
  331. {
  332. struct hlist_head *head;
  333. struct tun_flow_entry *e;
  334. unsigned long delay = tun->ageing_time;
  335. u16 queue_index = tfile->queue_index;
  336. if (!rxhash)
  337. return;
  338. else
  339. head = &tun->flows[tun_hashfn(rxhash)];
  340. rcu_read_lock();
  341. /* We may get a very small possibility of OOO during switching, not
  342. * worth to optimize.*/
  343. if (tun->numqueues == 1 || tfile->detached)
  344. goto unlock;
  345. e = tun_flow_find(head, rxhash);
  346. if (likely(e)) {
  347. /* TODO: keep queueing to old queue until it's empty? */
  348. e->queue_index = queue_index;
  349. e->updated = jiffies;
  350. sock_rps_record_flow_hash(e->rps_rxhash);
  351. } else {
  352. spin_lock_bh(&tun->lock);
  353. if (!tun_flow_find(head, rxhash) &&
  354. tun->flow_count < MAX_TAP_FLOWS)
  355. tun_flow_create(tun, head, rxhash, queue_index);
  356. if (!timer_pending(&tun->flow_gc_timer))
  357. mod_timer(&tun->flow_gc_timer,
  358. round_jiffies_up(jiffies + delay));
  359. spin_unlock_bh(&tun->lock);
  360. }
  361. unlock:
  362. rcu_read_unlock();
  363. }
  364. /**
  365. * Save the hash received in the stack receive path and update the
  366. * flow_hash table accordingly.
  367. */
  368. static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
  369. {
  370. if (unlikely(e->rps_rxhash != hash))
  371. e->rps_rxhash = hash;
  372. }
  373. /* We try to identify a flow through its rxhash first. The reason that
  374. * we do not check rxq no. is because some cards(e.g 82599), chooses
  375. * the rxq based on the txq where the last packet of the flow comes. As
  376. * the userspace application move between processors, we may get a
  377. * different rxq no. here. If we could not get rxhash, then we would
  378. * hope the rxq no. may help here.
  379. */
  380. static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
  381. void *accel_priv, select_queue_fallback_t fallback)
  382. {
  383. struct tun_struct *tun = netdev_priv(dev);
  384. struct tun_flow_entry *e;
  385. u32 txq = 0;
  386. u32 numqueues = 0;
  387. rcu_read_lock();
  388. numqueues = ACCESS_ONCE(tun->numqueues);
  389. txq = skb_get_hash(skb);
  390. if (txq) {
  391. e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
  392. if (e) {
  393. tun_flow_save_rps_rxhash(e, txq);
  394. txq = e->queue_index;
  395. } else
  396. /* use multiply and shift instead of expensive divide */
  397. txq = ((u64)txq * numqueues) >> 32;
  398. } else if (likely(skb_rx_queue_recorded(skb))) {
  399. txq = skb_get_rx_queue(skb);
  400. while (unlikely(txq >= numqueues))
  401. txq -= numqueues;
  402. }
  403. rcu_read_unlock();
  404. return txq;
  405. }
  406. static inline bool tun_not_capable(struct tun_struct *tun)
  407. {
  408. const struct cred *cred = current_cred();
  409. struct net *net = dev_net(tun->dev);
  410. return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
  411. (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
  412. !ns_capable(net->user_ns, CAP_NET_ADMIN);
  413. }
  414. static void tun_set_real_num_queues(struct tun_struct *tun)
  415. {
  416. netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
  417. netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
  418. }
  419. static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
  420. {
  421. tfile->detached = tun;
  422. list_add_tail(&tfile->next, &tun->disabled);
  423. ++tun->numdisabled;
  424. }
  425. static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
  426. {
  427. struct tun_struct *tun = tfile->detached;
  428. tfile->detached = NULL;
  429. list_del_init(&tfile->next);
  430. --tun->numdisabled;
  431. return tun;
  432. }
  433. static void tun_queue_purge(struct tun_file *tfile)
  434. {
  435. skb_queue_purge(&tfile->sk.sk_receive_queue);
  436. skb_queue_purge(&tfile->sk.sk_error_queue);
  437. }
  438. static void __tun_detach(struct tun_file *tfile, bool clean)
  439. {
  440. struct tun_file *ntfile;
  441. struct tun_struct *tun;
  442. tun = rtnl_dereference(tfile->tun);
  443. if (tun && !tfile->detached) {
  444. u16 index = tfile->queue_index;
  445. BUG_ON(index >= tun->numqueues);
  446. rcu_assign_pointer(tun->tfiles[index],
  447. tun->tfiles[tun->numqueues - 1]);
  448. ntfile = rtnl_dereference(tun->tfiles[index]);
  449. ntfile->queue_index = index;
  450. --tun->numqueues;
  451. if (clean) {
  452. RCU_INIT_POINTER(tfile->tun, NULL);
  453. sock_put(&tfile->sk);
  454. } else
  455. tun_disable_queue(tun, tfile);
  456. synchronize_net();
  457. tun_flow_delete_by_queue(tun, tun->numqueues + 1);
  458. /* Drop read queue */
  459. tun_queue_purge(tfile);
  460. tun_set_real_num_queues(tun);
  461. } else if (tfile->detached && clean) {
  462. tun = tun_enable_queue(tfile);
  463. sock_put(&tfile->sk);
  464. }
  465. if (clean) {
  466. if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
  467. netif_carrier_off(tun->dev);
  468. if (!(tun->flags & IFF_PERSIST) &&
  469. tun->dev->reg_state == NETREG_REGISTERED)
  470. unregister_netdevice(tun->dev);
  471. }
  472. sock_put(&tfile->sk);
  473. }
  474. }
  475. static void tun_detach(struct tun_file *tfile, bool clean)
  476. {
  477. rtnl_lock();
  478. __tun_detach(tfile, clean);
  479. rtnl_unlock();
  480. }
  481. static void tun_detach_all(struct net_device *dev)
  482. {
  483. struct tun_struct *tun = netdev_priv(dev);
  484. struct tun_file *tfile, *tmp;
  485. int i, n = tun->numqueues;
  486. for (i = 0; i < n; i++) {
  487. tfile = rtnl_dereference(tun->tfiles[i]);
  488. BUG_ON(!tfile);
  489. tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
  490. tfile->socket.sk->sk_data_ready(tfile->socket.sk);
  491. RCU_INIT_POINTER(tfile->tun, NULL);
  492. --tun->numqueues;
  493. }
  494. list_for_each_entry(tfile, &tun->disabled, next) {
  495. tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
  496. tfile->socket.sk->sk_data_ready(tfile->socket.sk);
  497. RCU_INIT_POINTER(tfile->tun, NULL);
  498. }
  499. BUG_ON(tun->numqueues != 0);
  500. synchronize_net();
  501. for (i = 0; i < n; i++) {
  502. tfile = rtnl_dereference(tun->tfiles[i]);
  503. /* Drop read queue */
  504. tun_queue_purge(tfile);
  505. sock_put(&tfile->sk);
  506. }
  507. list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
  508. tun_enable_queue(tfile);
  509. tun_queue_purge(tfile);
  510. sock_put(&tfile->sk);
  511. }
  512. BUG_ON(tun->numdisabled != 0);
  513. if (tun->flags & IFF_PERSIST)
  514. module_put(THIS_MODULE);
  515. }
  516. static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter)
  517. {
  518. struct tun_file *tfile = file->private_data;
  519. int err;
  520. err = security_tun_dev_attach(tfile->socket.sk, tun->security);
  521. if (err < 0)
  522. goto out;
  523. err = -EINVAL;
  524. if (rtnl_dereference(tfile->tun) && !tfile->detached)
  525. goto out;
  526. err = -EBUSY;
  527. if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
  528. goto out;
  529. err = -E2BIG;
  530. if (!tfile->detached &&
  531. tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
  532. goto out;
  533. err = 0;
  534. /* Re-attach the filter to persist device */
  535. if (!skip_filter && (tun->filter_attached == true)) {
  536. err = __sk_attach_filter(&tun->fprog, tfile->socket.sk,
  537. lockdep_rtnl_is_held());
  538. if (!err)
  539. goto out;
  540. }
  541. tfile->queue_index = tun->numqueues;
  542. tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
  543. rcu_assign_pointer(tfile->tun, tun);
  544. rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
  545. tun->numqueues++;
  546. if (tfile->detached)
  547. tun_enable_queue(tfile);
  548. else
  549. sock_hold(&tfile->sk);
  550. tun_set_real_num_queues(tun);
  551. /* device is allowed to go away first, so no need to hold extra
  552. * refcnt.
  553. */
  554. out:
  555. return err;
  556. }
  557. static struct tun_struct *__tun_get(struct tun_file *tfile)
  558. {
  559. struct tun_struct *tun;
  560. rcu_read_lock();
  561. tun = rcu_dereference(tfile->tun);
  562. if (tun)
  563. dev_hold(tun->dev);
  564. rcu_read_unlock();
  565. return tun;
  566. }
  567. static struct tun_struct *tun_get(struct file *file)
  568. {
  569. return __tun_get(file->private_data);
  570. }
  571. static void tun_put(struct tun_struct *tun)
  572. {
  573. dev_put(tun->dev);
  574. }
  575. /* TAP filtering */
  576. static void addr_hash_set(u32 *mask, const u8 *addr)
  577. {
  578. int n = ether_crc(ETH_ALEN, addr) >> 26;
  579. mask[n >> 5] |= (1 << (n & 31));
  580. }
  581. static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
  582. {
  583. int n = ether_crc(ETH_ALEN, addr) >> 26;
  584. return mask[n >> 5] & (1 << (n & 31));
  585. }
  586. static int update_filter(struct tap_filter *filter, void __user *arg)
  587. {
  588. struct { u8 u[ETH_ALEN]; } *addr;
  589. struct tun_filter uf;
  590. int err, alen, n, nexact;
  591. if (copy_from_user(&uf, arg, sizeof(uf)))
  592. return -EFAULT;
  593. if (!uf.count) {
  594. /* Disabled */
  595. filter->count = 0;
  596. return 0;
  597. }
  598. alen = ETH_ALEN * uf.count;
  599. addr = kmalloc(alen, GFP_KERNEL);
  600. if (!addr)
  601. return -ENOMEM;
  602. if (copy_from_user(addr, arg + sizeof(uf), alen)) {
  603. err = -EFAULT;
  604. goto done;
  605. }
  606. /* The filter is updated without holding any locks. Which is
  607. * perfectly safe. We disable it first and in the worst
  608. * case we'll accept a few undesired packets. */
  609. filter->count = 0;
  610. wmb();
  611. /* Use first set of addresses as an exact filter */
  612. for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
  613. memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
  614. nexact = n;
  615. /* Remaining multicast addresses are hashed,
  616. * unicast will leave the filter disabled. */
  617. memset(filter->mask, 0, sizeof(filter->mask));
  618. for (; n < uf.count; n++) {
  619. if (!is_multicast_ether_addr(addr[n].u)) {
  620. err = 0; /* no filter */
  621. goto done;
  622. }
  623. addr_hash_set(filter->mask, addr[n].u);
  624. }
  625. /* For ALLMULTI just set the mask to all ones.
  626. * This overrides the mask populated above. */
  627. if ((uf.flags & TUN_FLT_ALLMULTI))
  628. memset(filter->mask, ~0, sizeof(filter->mask));
  629. /* Now enable the filter */
  630. wmb();
  631. filter->count = nexact;
  632. /* Return the number of exact filters */
  633. err = nexact;
  634. done:
  635. kfree(addr);
  636. return err;
  637. }
  638. /* Returns: 0 - drop, !=0 - accept */
  639. static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
  640. {
  641. /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
  642. * at this point. */
  643. struct ethhdr *eh = (struct ethhdr *) skb->data;
  644. int i;
  645. /* Exact match */
  646. for (i = 0; i < filter->count; i++)
  647. if (ether_addr_equal(eh->h_dest, filter->addr[i]))
  648. return 1;
  649. /* Inexact match (multicast only) */
  650. if (is_multicast_ether_addr(eh->h_dest))
  651. return addr_hash_test(filter->mask, eh->h_dest);
  652. return 0;
  653. }
  654. /*
  655. * Checks whether the packet is accepted or not.
  656. * Returns: 0 - drop, !=0 - accept
  657. */
  658. static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
  659. {
  660. if (!filter->count)
  661. return 1;
  662. return run_filter(filter, skb);
  663. }
  664. /* Network device part of the driver */
  665. static const struct ethtool_ops tun_ethtool_ops;
  666. /* Net device detach from fd. */
  667. static void tun_net_uninit(struct net_device *dev)
  668. {
  669. tun_detach_all(dev);
  670. }
  671. /* Net device open. */
  672. static int tun_net_open(struct net_device *dev)
  673. {
  674. netif_tx_start_all_queues(dev);
  675. return 0;
  676. }
  677. /* Net device close. */
  678. static int tun_net_close(struct net_device *dev)
  679. {
  680. netif_tx_stop_all_queues(dev);
  681. return 0;
  682. }
  683. /* Net device start xmit */
  684. static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
  685. {
  686. struct tun_struct *tun = netdev_priv(dev);
  687. int txq = skb->queue_mapping;
  688. struct tun_file *tfile;
  689. u32 numqueues = 0;
  690. rcu_read_lock();
  691. tfile = rcu_dereference(tun->tfiles[txq]);
  692. numqueues = ACCESS_ONCE(tun->numqueues);
  693. /* Drop packet if interface is not attached */
  694. if (txq >= numqueues)
  695. goto drop;
  696. if (numqueues == 1) {
  697. /* Select queue was not called for the skbuff, so we extract the
  698. * RPS hash and save it into the flow_table here.
  699. */
  700. __u32 rxhash;
  701. rxhash = skb_get_hash(skb);
  702. if (rxhash) {
  703. struct tun_flow_entry *e;
  704. e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)],
  705. rxhash);
  706. if (e)
  707. tun_flow_save_rps_rxhash(e, rxhash);
  708. }
  709. }
  710. tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
  711. BUG_ON(!tfile);
  712. /* Drop if the filter does not like it.
  713. * This is a noop if the filter is disabled.
  714. * Filter can be enabled only for the TAP devices. */
  715. if (!check_filter(&tun->txflt, skb))
  716. goto drop;
  717. if (tfile->socket.sk->sk_filter &&
  718. sk_filter(tfile->socket.sk, skb))
  719. goto drop;
  720. /* Limit the number of packets queued by dividing txq length with the
  721. * number of queues.
  722. */
  723. if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) * numqueues
  724. >= dev->tx_queue_len)
  725. goto drop;
  726. if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
  727. goto drop;
  728. if (skb->sk && sk_fullsock(skb->sk)) {
  729. sock_tx_timestamp(skb->sk, &skb_shinfo(skb)->tx_flags);
  730. sw_tx_timestamp(skb);
  731. }
  732. /* Orphan the skb - required as we might hang on to it
  733. * for indefinite time.
  734. */
  735. skb_orphan(skb);
  736. nf_reset(skb);
  737. /* Enqueue packet */
  738. skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
  739. /* Notify and wake up reader process */
  740. if (tfile->flags & TUN_FASYNC)
  741. kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
  742. tfile->socket.sk->sk_data_ready(tfile->socket.sk);
  743. rcu_read_unlock();
  744. return NETDEV_TX_OK;
  745. drop:
  746. dev->stats.tx_dropped++;
  747. skb_tx_error(skb);
  748. kfree_skb(skb);
  749. rcu_read_unlock();
  750. return NET_XMIT_DROP;
  751. }
  752. static void tun_net_mclist(struct net_device *dev)
  753. {
  754. /*
  755. * This callback is supposed to deal with mc filter in
  756. * _rx_ path and has nothing to do with the _tx_ path.
  757. * In rx path we always accept everything userspace gives us.
  758. */
  759. }
  760. #define MIN_MTU 68
  761. #define MAX_MTU 65535
  762. static int
  763. tun_net_change_mtu(struct net_device *dev, int new_mtu)
  764. {
  765. if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
  766. return -EINVAL;
  767. dev->mtu = new_mtu;
  768. return 0;
  769. }
  770. static netdev_features_t tun_net_fix_features(struct net_device *dev,
  771. netdev_features_t features)
  772. {
  773. struct tun_struct *tun = netdev_priv(dev);
  774. return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
  775. }
  776. #ifdef CONFIG_NET_POLL_CONTROLLER
  777. static void tun_poll_controller(struct net_device *dev)
  778. {
  779. /*
  780. * Tun only receives frames when:
  781. * 1) the char device endpoint gets data from user space
  782. * 2) the tun socket gets a sendmsg call from user space
  783. * Since both of those are synchronous operations, we are guaranteed
  784. * never to have pending data when we poll for it
  785. * so there is nothing to do here but return.
  786. * We need this though so netpoll recognizes us as an interface that
  787. * supports polling, which enables bridge devices in virt setups to
  788. * still use netconsole
  789. */
  790. return;
  791. }
  792. #endif
  793. static const struct net_device_ops tun_netdev_ops = {
  794. .ndo_uninit = tun_net_uninit,
  795. .ndo_open = tun_net_open,
  796. .ndo_stop = tun_net_close,
  797. .ndo_start_xmit = tun_net_xmit,
  798. .ndo_change_mtu = tun_net_change_mtu,
  799. .ndo_fix_features = tun_net_fix_features,
  800. .ndo_select_queue = tun_select_queue,
  801. #ifdef CONFIG_NET_POLL_CONTROLLER
  802. .ndo_poll_controller = tun_poll_controller,
  803. #endif
  804. };
  805. static const struct net_device_ops tap_netdev_ops = {
  806. .ndo_uninit = tun_net_uninit,
  807. .ndo_open = tun_net_open,
  808. .ndo_stop = tun_net_close,
  809. .ndo_start_xmit = tun_net_xmit,
  810. .ndo_change_mtu = tun_net_change_mtu,
  811. .ndo_fix_features = tun_net_fix_features,
  812. .ndo_set_rx_mode = tun_net_mclist,
  813. .ndo_set_mac_address = eth_mac_addr,
  814. .ndo_validate_addr = eth_validate_addr,
  815. .ndo_select_queue = tun_select_queue,
  816. #ifdef CONFIG_NET_POLL_CONTROLLER
  817. .ndo_poll_controller = tun_poll_controller,
  818. #endif
  819. .ndo_features_check = passthru_features_check,
  820. };
  821. static void tun_flow_init(struct tun_struct *tun)
  822. {
  823. int i;
  824. for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
  825. INIT_HLIST_HEAD(&tun->flows[i]);
  826. tun->ageing_time = TUN_FLOW_EXPIRE;
  827. setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
  828. mod_timer(&tun->flow_gc_timer,
  829. round_jiffies_up(jiffies + tun->ageing_time));
  830. }
  831. static void tun_flow_uninit(struct tun_struct *tun)
  832. {
  833. del_timer_sync(&tun->flow_gc_timer);
  834. tun_flow_flush(tun);
  835. }
  836. /* Initialize net device. */
  837. static void tun_net_init(struct net_device *dev)
  838. {
  839. struct tun_struct *tun = netdev_priv(dev);
  840. switch (tun->flags & TUN_TYPE_MASK) {
  841. case IFF_TUN:
  842. dev->netdev_ops = &tun_netdev_ops;
  843. /* Point-to-Point TUN Device */
  844. dev->hard_header_len = 0;
  845. dev->addr_len = 0;
  846. dev->mtu = 1500;
  847. /* Zero header length */
  848. dev->type = ARPHRD_NONE;
  849. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  850. break;
  851. case IFF_TAP:
  852. dev->netdev_ops = &tap_netdev_ops;
  853. /* Ethernet TAP Device */
  854. ether_setup(dev);
  855. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  856. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  857. eth_hw_addr_random(dev);
  858. break;
  859. }
  860. }
  861. /* Character device part */
  862. /* Poll */
  863. static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
  864. {
  865. struct tun_file *tfile = file->private_data;
  866. struct tun_struct *tun = __tun_get(tfile);
  867. struct sock *sk;
  868. unsigned int mask = 0;
  869. if (!tun)
  870. return POLLERR;
  871. sk = tfile->socket.sk;
  872. tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
  873. poll_wait(file, sk_sleep(sk), wait);
  874. if (!skb_queue_empty(&sk->sk_receive_queue))
  875. mask |= POLLIN | POLLRDNORM;
  876. if (sock_writeable(sk) ||
  877. (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
  878. sock_writeable(sk)))
  879. mask |= POLLOUT | POLLWRNORM;
  880. if (tun->dev->reg_state != NETREG_REGISTERED)
  881. mask = POLLERR;
  882. tun_put(tun);
  883. return mask;
  884. }
  885. /* prepad is the amount to reserve at front. len is length after that.
  886. * linear is a hint as to how much to copy (usually headers). */
  887. static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
  888. size_t prepad, size_t len,
  889. size_t linear, int noblock)
  890. {
  891. struct sock *sk = tfile->socket.sk;
  892. struct sk_buff *skb;
  893. int err;
  894. /* Under a page? Don't bother with paged skb. */
  895. if (prepad + len < PAGE_SIZE || !linear)
  896. linear = len;
  897. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  898. &err, 0);
  899. if (!skb)
  900. return ERR_PTR(err);
  901. skb_reserve(skb, prepad);
  902. skb_put(skb, linear);
  903. skb->data_len = len - linear;
  904. skb->len += len - linear;
  905. return skb;
  906. }
  907. /* Get packet from user space buffer */
  908. static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
  909. void *msg_control, struct iov_iter *from,
  910. int noblock)
  911. {
  912. struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
  913. struct sk_buff *skb;
  914. size_t total_len = iov_iter_count(from);
  915. size_t len = total_len, align = NET_SKB_PAD, linear;
  916. struct virtio_net_hdr gso = { 0 };
  917. int good_linear;
  918. int copylen;
  919. bool zerocopy = false;
  920. int err;
  921. u32 rxhash;
  922. ssize_t n;
  923. if (!(tun->flags & IFF_NO_PI)) {
  924. if (len < sizeof(pi))
  925. return -EINVAL;
  926. len -= sizeof(pi);
  927. n = copy_from_iter(&pi, sizeof(pi), from);
  928. if (n != sizeof(pi))
  929. return -EFAULT;
  930. }
  931. if (tun->flags & IFF_VNET_HDR) {
  932. int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
  933. if (len < vnet_hdr_sz)
  934. return -EINVAL;
  935. len -= vnet_hdr_sz;
  936. n = copy_from_iter(&gso, sizeof(gso), from);
  937. if (n != sizeof(gso))
  938. return -EFAULT;
  939. if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  940. tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
  941. gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2);
  942. if (tun16_to_cpu(tun, gso.hdr_len) > len)
  943. return -EINVAL;
  944. iov_iter_advance(from, vnet_hdr_sz - sizeof(gso));
  945. }
  946. if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
  947. align += NET_IP_ALIGN;
  948. if (unlikely(len < ETH_HLEN ||
  949. (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
  950. return -EINVAL;
  951. }
  952. good_linear = SKB_MAX_HEAD(align);
  953. if (msg_control) {
  954. struct iov_iter i = *from;
  955. /* There are 256 bytes to be copied in skb, so there is
  956. * enough room for skb expand head in case it is used.
  957. * The rest of the buffer is mapped from userspace.
  958. */
  959. copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
  960. if (copylen > good_linear)
  961. copylen = good_linear;
  962. linear = copylen;
  963. iov_iter_advance(&i, copylen);
  964. if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
  965. zerocopy = true;
  966. }
  967. if (!zerocopy) {
  968. copylen = len;
  969. if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
  970. linear = good_linear;
  971. else
  972. linear = tun16_to_cpu(tun, gso.hdr_len);
  973. }
  974. skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
  975. if (IS_ERR(skb)) {
  976. if (PTR_ERR(skb) != -EAGAIN)
  977. tun->dev->stats.rx_dropped++;
  978. return PTR_ERR(skb);
  979. }
  980. if (zerocopy)
  981. err = zerocopy_sg_from_iter(skb, from);
  982. else {
  983. err = skb_copy_datagram_from_iter(skb, 0, from, len);
  984. if (!err && msg_control) {
  985. struct ubuf_info *uarg = msg_control;
  986. uarg->callback(uarg, false);
  987. }
  988. }
  989. if (err) {
  990. tun->dev->stats.rx_dropped++;
  991. kfree_skb(skb);
  992. return -EFAULT;
  993. }
  994. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  995. if (!skb_partial_csum_set(skb, tun16_to_cpu(tun, gso.csum_start),
  996. tun16_to_cpu(tun, gso.csum_offset))) {
  997. tun->dev->stats.rx_frame_errors++;
  998. kfree_skb(skb);
  999. return -EINVAL;
  1000. }
  1001. }
  1002. switch (tun->flags & TUN_TYPE_MASK) {
  1003. case IFF_TUN:
  1004. if (tun->flags & IFF_NO_PI) {
  1005. u8 ip_version = skb->len ? (skb->data[0] >> 4) : 0;
  1006. switch (ip_version) {
  1007. case 4:
  1008. pi.proto = htons(ETH_P_IP);
  1009. break;
  1010. case 6:
  1011. pi.proto = htons(ETH_P_IPV6);
  1012. break;
  1013. default:
  1014. tun->dev->stats.rx_dropped++;
  1015. kfree_skb(skb);
  1016. return -EINVAL;
  1017. }
  1018. }
  1019. skb_reset_mac_header(skb);
  1020. skb->protocol = pi.proto;
  1021. skb->dev = tun->dev;
  1022. break;
  1023. case IFF_TAP:
  1024. skb->protocol = eth_type_trans(skb, tun->dev);
  1025. break;
  1026. }
  1027. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  1028. pr_debug("GSO!\n");
  1029. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  1030. case VIRTIO_NET_HDR_GSO_TCPV4:
  1031. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  1032. break;
  1033. case VIRTIO_NET_HDR_GSO_TCPV6:
  1034. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  1035. break;
  1036. case VIRTIO_NET_HDR_GSO_UDP:
  1037. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  1038. break;
  1039. default:
  1040. tun->dev->stats.rx_frame_errors++;
  1041. kfree_skb(skb);
  1042. return -EINVAL;
  1043. }
  1044. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  1045. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  1046. skb_shinfo(skb)->gso_size = tun16_to_cpu(tun, gso.gso_size);
  1047. if (skb_shinfo(skb)->gso_size == 0) {
  1048. tun->dev->stats.rx_frame_errors++;
  1049. kfree_skb(skb);
  1050. return -EINVAL;
  1051. }
  1052. /* Header must be checked, and gso_segs computed. */
  1053. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  1054. skb_shinfo(skb)->gso_segs = 0;
  1055. }
  1056. /* copy skb_ubuf_info for callback when skb has no error */
  1057. if (zerocopy) {
  1058. skb_shinfo(skb)->destructor_arg = msg_control;
  1059. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  1060. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  1061. }
  1062. skb_reset_network_header(skb);
  1063. skb_probe_transport_header(skb, 0);
  1064. rxhash = skb_get_hash(skb);
  1065. netif_rx_ni(skb);
  1066. tun->dev->stats.rx_packets++;
  1067. tun->dev->stats.rx_bytes += len;
  1068. tun_flow_update(tun, rxhash, tfile);
  1069. return total_len;
  1070. }
  1071. static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
  1072. {
  1073. struct file *file = iocb->ki_filp;
  1074. struct tun_struct *tun = tun_get(file);
  1075. struct tun_file *tfile = file->private_data;
  1076. ssize_t result;
  1077. if (!tun)
  1078. return -EBADFD;
  1079. result = tun_get_user(tun, tfile, NULL, from, file->f_flags & O_NONBLOCK);
  1080. tun_put(tun);
  1081. return result;
  1082. }
  1083. /* Put packet to the user space buffer */
  1084. static ssize_t tun_put_user(struct tun_struct *tun,
  1085. struct tun_file *tfile,
  1086. struct sk_buff *skb,
  1087. struct iov_iter *iter)
  1088. {
  1089. struct tun_pi pi = { 0, skb->protocol };
  1090. ssize_t total;
  1091. int vlan_offset = 0;
  1092. int vlan_hlen = 0;
  1093. int vnet_hdr_sz = 0;
  1094. if (skb_vlan_tag_present(skb))
  1095. vlan_hlen = VLAN_HLEN;
  1096. if (tun->flags & IFF_VNET_HDR)
  1097. vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
  1098. total = skb->len + vlan_hlen + vnet_hdr_sz;
  1099. if (!(tun->flags & IFF_NO_PI)) {
  1100. if (iov_iter_count(iter) < sizeof(pi))
  1101. return -EINVAL;
  1102. total += sizeof(pi);
  1103. if (iov_iter_count(iter) < total) {
  1104. /* Packet will be striped */
  1105. pi.flags |= TUN_PKT_STRIP;
  1106. }
  1107. if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
  1108. return -EFAULT;
  1109. }
  1110. if (vnet_hdr_sz) {
  1111. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  1112. if (iov_iter_count(iter) < vnet_hdr_sz)
  1113. return -EINVAL;
  1114. if (skb_is_gso(skb)) {
  1115. struct skb_shared_info *sinfo = skb_shinfo(skb);
  1116. /* This is a hint as to how much should be linear. */
  1117. gso.hdr_len = cpu_to_tun16(tun, skb_headlen(skb));
  1118. gso.gso_size = cpu_to_tun16(tun, sinfo->gso_size);
  1119. if (sinfo->gso_type & SKB_GSO_TCPV4)
  1120. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  1121. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  1122. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  1123. else if (sinfo->gso_type & SKB_GSO_UDP)
  1124. gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  1125. else {
  1126. pr_err("unexpected GSO type: "
  1127. "0x%x, gso_size %d, hdr_len %d\n",
  1128. sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
  1129. tun16_to_cpu(tun, gso.hdr_len));
  1130. print_hex_dump(KERN_ERR, "tun: ",
  1131. DUMP_PREFIX_NONE,
  1132. 16, 1, skb->head,
  1133. min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
  1134. WARN_ON_ONCE(1);
  1135. return -EINVAL;
  1136. }
  1137. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  1138. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  1139. } else
  1140. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  1141. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1142. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  1143. gso.csum_start = cpu_to_tun16(tun, skb_checksum_start_offset(skb) +
  1144. vlan_hlen);
  1145. gso.csum_offset = cpu_to_tun16(tun, skb->csum_offset);
  1146. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  1147. gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
  1148. } /* else everything is zero */
  1149. if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
  1150. return -EFAULT;
  1151. iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
  1152. }
  1153. if (vlan_hlen) {
  1154. int ret;
  1155. struct {
  1156. __be16 h_vlan_proto;
  1157. __be16 h_vlan_TCI;
  1158. } veth;
  1159. veth.h_vlan_proto = skb->vlan_proto;
  1160. veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
  1161. vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
  1162. ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
  1163. if (ret || !iov_iter_count(iter))
  1164. goto done;
  1165. ret = copy_to_iter(&veth, sizeof(veth), iter);
  1166. if (ret != sizeof(veth) || !iov_iter_count(iter))
  1167. goto done;
  1168. }
  1169. skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
  1170. done:
  1171. tun->dev->stats.tx_packets++;
  1172. tun->dev->stats.tx_bytes += skb->len + vlan_hlen;
  1173. return total;
  1174. }
  1175. static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
  1176. struct iov_iter *to,
  1177. int noblock)
  1178. {
  1179. struct sk_buff *skb;
  1180. ssize_t ret;
  1181. int peeked, err, off = 0;
  1182. tun_debug(KERN_INFO, tun, "tun_do_read\n");
  1183. if (!iov_iter_count(to))
  1184. return 0;
  1185. /* Read frames from queue */
  1186. skb = __skb_recv_datagram(tfile->socket.sk, noblock ? MSG_DONTWAIT : 0,
  1187. &peeked, &off, &err);
  1188. if (!skb)
  1189. return err;
  1190. ret = tun_put_user(tun, tfile, skb, to);
  1191. if (unlikely(ret < 0))
  1192. kfree_skb(skb);
  1193. else
  1194. consume_skb(skb);
  1195. return ret;
  1196. }
  1197. static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1198. {
  1199. struct file *file = iocb->ki_filp;
  1200. struct tun_file *tfile = file->private_data;
  1201. struct tun_struct *tun = __tun_get(tfile);
  1202. ssize_t len = iov_iter_count(to), ret;
  1203. if (!tun)
  1204. return -EBADFD;
  1205. ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK);
  1206. ret = min_t(ssize_t, ret, len);
  1207. if (ret > 0)
  1208. iocb->ki_pos = ret;
  1209. tun_put(tun);
  1210. return ret;
  1211. }
  1212. static void tun_free_netdev(struct net_device *dev)
  1213. {
  1214. struct tun_struct *tun = netdev_priv(dev);
  1215. BUG_ON(!(list_empty(&tun->disabled)));
  1216. tun_flow_uninit(tun);
  1217. security_tun_dev_free_security(tun->security);
  1218. free_netdev(dev);
  1219. }
  1220. static void tun_setup(struct net_device *dev)
  1221. {
  1222. struct tun_struct *tun = netdev_priv(dev);
  1223. tun->owner = INVALID_UID;
  1224. tun->group = INVALID_GID;
  1225. dev->ethtool_ops = &tun_ethtool_ops;
  1226. dev->destructor = tun_free_netdev;
  1227. /* We prefer our own queue length */
  1228. dev->tx_queue_len = TUN_READQ_SIZE;
  1229. }
  1230. /* Trivial set of netlink ops to allow deleting tun or tap
  1231. * device with netlink.
  1232. */
  1233. static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
  1234. {
  1235. /* NL_SET_ERR_MSG(extack,
  1236. "tun/tap creation via rtnetlink is not supported."); */
  1237. return -EOPNOTSUPP;
  1238. }
  1239. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  1240. .kind = DRV_NAME,
  1241. .priv_size = sizeof(struct tun_struct),
  1242. .setup = tun_setup,
  1243. .validate = tun_validate,
  1244. };
  1245. static void tun_sock_write_space(struct sock *sk)
  1246. {
  1247. struct tun_file *tfile;
  1248. wait_queue_head_t *wqueue;
  1249. if (!sock_writeable(sk))
  1250. return;
  1251. if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
  1252. return;
  1253. wqueue = sk_sleep(sk);
  1254. if (wqueue && waitqueue_active(wqueue))
  1255. wake_up_interruptible_sync_poll(wqueue, POLLOUT |
  1256. POLLWRNORM | POLLWRBAND);
  1257. tfile = container_of(sk, struct tun_file, sk);
  1258. kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
  1259. }
  1260. static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
  1261. {
  1262. int ret;
  1263. struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  1264. struct tun_struct *tun = __tun_get(tfile);
  1265. if (!tun)
  1266. return -EBADFD;
  1267. ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter,
  1268. m->msg_flags & MSG_DONTWAIT);
  1269. tun_put(tun);
  1270. return ret;
  1271. }
  1272. static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
  1273. int flags)
  1274. {
  1275. struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  1276. struct tun_struct *tun = __tun_get(tfile);
  1277. int ret;
  1278. if (!tun)
  1279. return -EBADFD;
  1280. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
  1281. ret = -EINVAL;
  1282. goto out;
  1283. }
  1284. if (flags & MSG_ERRQUEUE) {
  1285. ret = sock_recv_errqueue(sock->sk, m, total_len,
  1286. SOL_PACKET, TUN_TX_TIMESTAMP);
  1287. goto out;
  1288. }
  1289. ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT);
  1290. if (ret > (ssize_t)total_len) {
  1291. m->msg_flags |= MSG_TRUNC;
  1292. ret = flags & MSG_TRUNC ? ret : total_len;
  1293. }
  1294. out:
  1295. tun_put(tun);
  1296. return ret;
  1297. }
  1298. /* Ops structure to mimic raw sockets with tun */
  1299. static const struct proto_ops tun_socket_ops = {
  1300. .sendmsg = tun_sendmsg,
  1301. .recvmsg = tun_recvmsg,
  1302. };
  1303. static struct proto tun_proto = {
  1304. .name = "tun",
  1305. .owner = THIS_MODULE,
  1306. .obj_size = sizeof(struct tun_file),
  1307. };
  1308. static int tun_flags(struct tun_struct *tun)
  1309. {
  1310. return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
  1311. }
  1312. static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
  1313. char *buf)
  1314. {
  1315. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  1316. return sprintf(buf, "0x%x\n", tun_flags(tun));
  1317. }
  1318. static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
  1319. char *buf)
  1320. {
  1321. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  1322. return uid_valid(tun->owner)?
  1323. sprintf(buf, "%u\n",
  1324. from_kuid_munged(current_user_ns(), tun->owner)):
  1325. sprintf(buf, "-1\n");
  1326. }
  1327. static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
  1328. char *buf)
  1329. {
  1330. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  1331. return gid_valid(tun->group) ?
  1332. sprintf(buf, "%u\n",
  1333. from_kgid_munged(current_user_ns(), tun->group)):
  1334. sprintf(buf, "-1\n");
  1335. }
  1336. static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
  1337. static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
  1338. static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
  1339. static struct attribute *tun_dev_attrs[] = {
  1340. &dev_attr_tun_flags.attr,
  1341. &dev_attr_owner.attr,
  1342. &dev_attr_group.attr,
  1343. NULL
  1344. };
  1345. static const struct attribute_group tun_attr_group = {
  1346. .attrs = tun_dev_attrs
  1347. };
  1348. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  1349. {
  1350. struct tun_struct *tun;
  1351. struct tun_file *tfile = file->private_data;
  1352. struct net_device *dev;
  1353. int err;
  1354. if (tfile->detached)
  1355. return -EINVAL;
  1356. dev = __dev_get_by_name(net, ifr->ifr_name);
  1357. if (dev) {
  1358. if (ifr->ifr_flags & IFF_TUN_EXCL)
  1359. return -EBUSY;
  1360. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  1361. tun = netdev_priv(dev);
  1362. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  1363. tun = netdev_priv(dev);
  1364. else
  1365. return -EINVAL;
  1366. if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
  1367. !!(tun->flags & IFF_MULTI_QUEUE))
  1368. return -EINVAL;
  1369. if (tun_not_capable(tun))
  1370. return -EPERM;
  1371. err = security_tun_dev_open(tun->security);
  1372. if (err < 0)
  1373. return err;
  1374. err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER);
  1375. if (err < 0)
  1376. return err;
  1377. if (tun->flags & IFF_MULTI_QUEUE &&
  1378. (tun->numqueues + tun->numdisabled > 1)) {
  1379. /* One or more queue has already been attached, no need
  1380. * to initialize the device again.
  1381. */
  1382. return 0;
  1383. }
  1384. }
  1385. else {
  1386. char *name;
  1387. unsigned long flags = 0;
  1388. int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
  1389. MAX_TAP_QUEUES : 1;
  1390. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1391. return -EPERM;
  1392. err = security_tun_dev_create();
  1393. if (err < 0)
  1394. return err;
  1395. /* Set dev type */
  1396. if (ifr->ifr_flags & IFF_TUN) {
  1397. /* TUN device */
  1398. flags |= IFF_TUN;
  1399. name = "tun%d";
  1400. } else if (ifr->ifr_flags & IFF_TAP) {
  1401. /* TAP device */
  1402. flags |= IFF_TAP;
  1403. name = "tap%d";
  1404. } else
  1405. return -EINVAL;
  1406. if (*ifr->ifr_name)
  1407. name = ifr->ifr_name;
  1408. dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
  1409. NET_NAME_UNKNOWN, tun_setup, queues,
  1410. queues);
  1411. if (!dev)
  1412. return -ENOMEM;
  1413. err = dev_get_valid_name(net, dev, name);
  1414. if (err < 0)
  1415. goto err_free_dev;
  1416. dev_net_set(dev, net);
  1417. dev->rtnl_link_ops = &tun_link_ops;
  1418. dev->ifindex = tfile->ifindex;
  1419. dev->sysfs_groups[0] = &tun_attr_group;
  1420. tun = netdev_priv(dev);
  1421. tun->dev = dev;
  1422. tun->flags = flags;
  1423. tun->txflt.count = 0;
  1424. tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  1425. tun->filter_attached = false;
  1426. tun->sndbuf = tfile->socket.sk->sk_sndbuf;
  1427. spin_lock_init(&tun->lock);
  1428. err = security_tun_dev_alloc_security(&tun->security);
  1429. if (err < 0)
  1430. goto err_free_dev;
  1431. tun_net_init(dev);
  1432. tun_flow_init(tun);
  1433. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
  1434. TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
  1435. NETIF_F_HW_VLAN_STAG_TX;
  1436. dev->features = dev->hw_features;
  1437. dev->vlan_features = dev->features &
  1438. ~(NETIF_F_HW_VLAN_CTAG_TX |
  1439. NETIF_F_HW_VLAN_STAG_TX);
  1440. INIT_LIST_HEAD(&tun->disabled);
  1441. err = tun_attach(tun, file, false);
  1442. if (err < 0)
  1443. goto err_free_flow;
  1444. err = register_netdevice(tun->dev);
  1445. if (err < 0)
  1446. goto err_detach;
  1447. }
  1448. netif_carrier_on(tun->dev);
  1449. tun_debug(KERN_INFO, tun, "tun_set_iff\n");
  1450. tun->flags = (tun->flags & ~TUN_FEATURES) |
  1451. (ifr->ifr_flags & TUN_FEATURES);
  1452. /* Make sure persistent devices do not get stuck in
  1453. * xoff state.
  1454. */
  1455. if (netif_running(tun->dev))
  1456. netif_tx_wake_all_queues(tun->dev);
  1457. strcpy(ifr->ifr_name, tun->dev->name);
  1458. return 0;
  1459. err_detach:
  1460. tun_detach_all(dev);
  1461. err_free_flow:
  1462. tun_flow_uninit(tun);
  1463. security_tun_dev_free_security(tun->security);
  1464. err_free_dev:
  1465. free_netdev(dev);
  1466. return err;
  1467. }
  1468. static void tun_get_iff(struct net *net, struct tun_struct *tun,
  1469. struct ifreq *ifr)
  1470. {
  1471. tun_debug(KERN_INFO, tun, "tun_get_iff\n");
  1472. strcpy(ifr->ifr_name, tun->dev->name);
  1473. ifr->ifr_flags = tun_flags(tun);
  1474. }
  1475. /* This is like a cut-down ethtool ops, except done via tun fd so no
  1476. * privs required. */
  1477. static int set_offload(struct tun_struct *tun, unsigned long arg)
  1478. {
  1479. netdev_features_t features = 0;
  1480. if (arg & TUN_F_CSUM) {
  1481. features |= NETIF_F_HW_CSUM;
  1482. arg &= ~TUN_F_CSUM;
  1483. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  1484. if (arg & TUN_F_TSO_ECN) {
  1485. features |= NETIF_F_TSO_ECN;
  1486. arg &= ~TUN_F_TSO_ECN;
  1487. }
  1488. if (arg & TUN_F_TSO4)
  1489. features |= NETIF_F_TSO;
  1490. if (arg & TUN_F_TSO6)
  1491. features |= NETIF_F_TSO6;
  1492. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  1493. }
  1494. if (arg & TUN_F_UFO) {
  1495. features |= NETIF_F_UFO;
  1496. arg &= ~TUN_F_UFO;
  1497. }
  1498. }
  1499. /* This gives the user a way to test for new features in future by
  1500. * trying to set them. */
  1501. if (arg)
  1502. return -EINVAL;
  1503. tun->set_features = features;
  1504. netdev_update_features(tun->dev);
  1505. return 0;
  1506. }
  1507. static void tun_detach_filter(struct tun_struct *tun, int n)
  1508. {
  1509. int i;
  1510. struct tun_file *tfile;
  1511. for (i = 0; i < n; i++) {
  1512. tfile = rtnl_dereference(tun->tfiles[i]);
  1513. __sk_detach_filter(tfile->socket.sk, lockdep_rtnl_is_held());
  1514. }
  1515. tun->filter_attached = false;
  1516. }
  1517. static int tun_attach_filter(struct tun_struct *tun)
  1518. {
  1519. int i, ret = 0;
  1520. struct tun_file *tfile;
  1521. for (i = 0; i < tun->numqueues; i++) {
  1522. tfile = rtnl_dereference(tun->tfiles[i]);
  1523. ret = __sk_attach_filter(&tun->fprog, tfile->socket.sk,
  1524. lockdep_rtnl_is_held());
  1525. if (ret) {
  1526. tun_detach_filter(tun, i);
  1527. return ret;
  1528. }
  1529. }
  1530. tun->filter_attached = true;
  1531. return ret;
  1532. }
  1533. static void tun_set_sndbuf(struct tun_struct *tun)
  1534. {
  1535. struct tun_file *tfile;
  1536. int i;
  1537. for (i = 0; i < tun->numqueues; i++) {
  1538. tfile = rtnl_dereference(tun->tfiles[i]);
  1539. tfile->socket.sk->sk_sndbuf = tun->sndbuf;
  1540. }
  1541. }
  1542. static int tun_set_queue(struct file *file, struct ifreq *ifr)
  1543. {
  1544. struct tun_file *tfile = file->private_data;
  1545. struct tun_struct *tun;
  1546. int ret = 0;
  1547. rtnl_lock();
  1548. if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
  1549. tun = tfile->detached;
  1550. if (!tun) {
  1551. ret = -EINVAL;
  1552. goto unlock;
  1553. }
  1554. ret = security_tun_dev_attach_queue(tun->security);
  1555. if (ret < 0)
  1556. goto unlock;
  1557. ret = tun_attach(tun, file, false);
  1558. } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
  1559. tun = rtnl_dereference(tfile->tun);
  1560. if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
  1561. ret = -EINVAL;
  1562. else
  1563. __tun_detach(tfile, false);
  1564. } else
  1565. ret = -EINVAL;
  1566. unlock:
  1567. rtnl_unlock();
  1568. return ret;
  1569. }
  1570. static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  1571. unsigned long arg, int ifreq_len)
  1572. {
  1573. struct tun_file *tfile = file->private_data;
  1574. struct tun_struct *tun;
  1575. void __user* argp = (void __user*)arg;
  1576. struct ifreq ifr;
  1577. kuid_t owner;
  1578. kgid_t group;
  1579. int sndbuf;
  1580. int vnet_hdr_sz;
  1581. unsigned int ifindex;
  1582. int le;
  1583. int ret;
  1584. if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
  1585. if (copy_from_user(&ifr, argp, ifreq_len))
  1586. return -EFAULT;
  1587. } else {
  1588. memset(&ifr, 0, sizeof(ifr));
  1589. }
  1590. if (cmd == TUNGETFEATURES) {
  1591. /* Currently this just means: "what IFF flags are valid?".
  1592. * This is needed because we never checked for invalid flags on
  1593. * TUNSETIFF.
  1594. */
  1595. return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
  1596. (unsigned int __user*)argp);
  1597. } else if (cmd == TUNSETQUEUE)
  1598. return tun_set_queue(file, &ifr);
  1599. ret = 0;
  1600. rtnl_lock();
  1601. tun = __tun_get(tfile);
  1602. if (cmd == TUNSETIFF && !tun) {
  1603. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  1604. ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr);
  1605. if (ret)
  1606. goto unlock;
  1607. if (copy_to_user(argp, &ifr, ifreq_len))
  1608. ret = -EFAULT;
  1609. goto unlock;
  1610. }
  1611. if (cmd == TUNSETIFINDEX) {
  1612. ret = -EPERM;
  1613. if (tun)
  1614. goto unlock;
  1615. ret = -EFAULT;
  1616. if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
  1617. goto unlock;
  1618. ret = 0;
  1619. tfile->ifindex = ifindex;
  1620. goto unlock;
  1621. }
  1622. ret = -EBADFD;
  1623. if (!tun)
  1624. goto unlock;
  1625. tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
  1626. ret = 0;
  1627. switch (cmd) {
  1628. case TUNGETIFF:
  1629. tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
  1630. if (tfile->detached)
  1631. ifr.ifr_flags |= IFF_DETACH_QUEUE;
  1632. if (!tfile->socket.sk->sk_filter)
  1633. ifr.ifr_flags |= IFF_NOFILTER;
  1634. if (copy_to_user(argp, &ifr, ifreq_len))
  1635. ret = -EFAULT;
  1636. break;
  1637. case TUNSETNOCSUM:
  1638. /* Disable/Enable checksum */
  1639. /* [unimplemented] */
  1640. tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
  1641. arg ? "disabled" : "enabled");
  1642. break;
  1643. case TUNSETPERSIST:
  1644. /* Disable/Enable persist mode. Keep an extra reference to the
  1645. * module to prevent the module being unprobed.
  1646. */
  1647. if (arg && !(tun->flags & IFF_PERSIST)) {
  1648. tun->flags |= IFF_PERSIST;
  1649. __module_get(THIS_MODULE);
  1650. }
  1651. if (!arg && (tun->flags & IFF_PERSIST)) {
  1652. tun->flags &= ~IFF_PERSIST;
  1653. module_put(THIS_MODULE);
  1654. }
  1655. tun_debug(KERN_INFO, tun, "persist %s\n",
  1656. arg ? "enabled" : "disabled");
  1657. break;
  1658. case TUNSETOWNER:
  1659. /* Set owner of the device */
  1660. owner = make_kuid(current_user_ns(), arg);
  1661. if (!uid_valid(owner)) {
  1662. ret = -EINVAL;
  1663. break;
  1664. }
  1665. tun->owner = owner;
  1666. tun_debug(KERN_INFO, tun, "owner set to %u\n",
  1667. from_kuid(&init_user_ns, tun->owner));
  1668. break;
  1669. case TUNSETGROUP:
  1670. /* Set group of the device */
  1671. group = make_kgid(current_user_ns(), arg);
  1672. if (!gid_valid(group)) {
  1673. ret = -EINVAL;
  1674. break;
  1675. }
  1676. tun->group = group;
  1677. tun_debug(KERN_INFO, tun, "group set to %u\n",
  1678. from_kgid(&init_user_ns, tun->group));
  1679. break;
  1680. case TUNSETLINK:
  1681. /* Only allow setting the type when the interface is down */
  1682. if (tun->dev->flags & IFF_UP) {
  1683. tun_debug(KERN_INFO, tun,
  1684. "Linktype set failed because interface is up\n");
  1685. ret = -EBUSY;
  1686. } else {
  1687. tun->dev->type = (int) arg;
  1688. tun_debug(KERN_INFO, tun, "linktype set to %d\n",
  1689. tun->dev->type);
  1690. ret = 0;
  1691. }
  1692. break;
  1693. #ifdef TUN_DEBUG
  1694. case TUNSETDEBUG:
  1695. tun->debug = arg;
  1696. break;
  1697. #endif
  1698. case TUNSETOFFLOAD:
  1699. ret = set_offload(tun, arg);
  1700. break;
  1701. case TUNSETTXFILTER:
  1702. /* Can be set only for TAPs */
  1703. ret = -EINVAL;
  1704. if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
  1705. break;
  1706. ret = update_filter(&tun->txflt, (void __user *)arg);
  1707. break;
  1708. case SIOCGIFHWADDR:
  1709. /* Get hw address */
  1710. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  1711. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  1712. if (copy_to_user(argp, &ifr, ifreq_len))
  1713. ret = -EFAULT;
  1714. break;
  1715. case SIOCSIFHWADDR:
  1716. /* Set hw address */
  1717. tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
  1718. ifr.ifr_hwaddr.sa_data);
  1719. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  1720. break;
  1721. case TUNGETSNDBUF:
  1722. sndbuf = tfile->socket.sk->sk_sndbuf;
  1723. if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
  1724. ret = -EFAULT;
  1725. break;
  1726. case TUNSETSNDBUF:
  1727. if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
  1728. ret = -EFAULT;
  1729. break;
  1730. }
  1731. if (sndbuf <= 0) {
  1732. ret = -EINVAL;
  1733. break;
  1734. }
  1735. tun->sndbuf = sndbuf;
  1736. tun_set_sndbuf(tun);
  1737. break;
  1738. case TUNGETVNETHDRSZ:
  1739. vnet_hdr_sz = tun->vnet_hdr_sz;
  1740. if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
  1741. ret = -EFAULT;
  1742. break;
  1743. case TUNSETVNETHDRSZ:
  1744. if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
  1745. ret = -EFAULT;
  1746. break;
  1747. }
  1748. if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
  1749. ret = -EINVAL;
  1750. break;
  1751. }
  1752. tun->vnet_hdr_sz = vnet_hdr_sz;
  1753. break;
  1754. case TUNGETVNETLE:
  1755. le = !!(tun->flags & TUN_VNET_LE);
  1756. if (put_user(le, (int __user *)argp))
  1757. ret = -EFAULT;
  1758. break;
  1759. case TUNSETVNETLE:
  1760. if (get_user(le, (int __user *)argp)) {
  1761. ret = -EFAULT;
  1762. break;
  1763. }
  1764. if (le)
  1765. tun->flags |= TUN_VNET_LE;
  1766. else
  1767. tun->flags &= ~TUN_VNET_LE;
  1768. break;
  1769. case TUNGETVNETBE:
  1770. ret = tun_get_vnet_be(tun, argp);
  1771. break;
  1772. case TUNSETVNETBE:
  1773. ret = tun_set_vnet_be(tun, argp);
  1774. break;
  1775. case TUNATTACHFILTER:
  1776. /* Can be set only for TAPs */
  1777. ret = -EINVAL;
  1778. if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
  1779. break;
  1780. ret = -EFAULT;
  1781. if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
  1782. break;
  1783. ret = tun_attach_filter(tun);
  1784. break;
  1785. case TUNDETACHFILTER:
  1786. /* Can be set only for TAPs */
  1787. ret = -EINVAL;
  1788. if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
  1789. break;
  1790. ret = 0;
  1791. tun_detach_filter(tun, tun->numqueues);
  1792. break;
  1793. case TUNGETFILTER:
  1794. ret = -EINVAL;
  1795. if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
  1796. break;
  1797. ret = -EFAULT;
  1798. if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
  1799. break;
  1800. ret = 0;
  1801. break;
  1802. default:
  1803. ret = -EINVAL;
  1804. break;
  1805. }
  1806. unlock:
  1807. rtnl_unlock();
  1808. if (tun)
  1809. tun_put(tun);
  1810. return ret;
  1811. }
  1812. static long tun_chr_ioctl(struct file *file,
  1813. unsigned int cmd, unsigned long arg)
  1814. {
  1815. return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
  1816. }
  1817. #ifdef CONFIG_COMPAT
  1818. static long tun_chr_compat_ioctl(struct file *file,
  1819. unsigned int cmd, unsigned long arg)
  1820. {
  1821. switch (cmd) {
  1822. case TUNSETIFF:
  1823. case TUNGETIFF:
  1824. case TUNSETTXFILTER:
  1825. case TUNGETSNDBUF:
  1826. case TUNSETSNDBUF:
  1827. case SIOCGIFHWADDR:
  1828. case SIOCSIFHWADDR:
  1829. arg = (unsigned long)compat_ptr(arg);
  1830. break;
  1831. default:
  1832. arg = (compat_ulong_t)arg;
  1833. break;
  1834. }
  1835. /*
  1836. * compat_ifreq is shorter than ifreq, so we must not access beyond
  1837. * the end of that structure. All fields that are used in this
  1838. * driver are compatible though, we don't need to convert the
  1839. * contents.
  1840. */
  1841. return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
  1842. }
  1843. #endif /* CONFIG_COMPAT */
  1844. static int tun_chr_fasync(int fd, struct file *file, int on)
  1845. {
  1846. struct tun_file *tfile = file->private_data;
  1847. int ret;
  1848. if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
  1849. goto out;
  1850. if (on) {
  1851. __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  1852. tfile->flags |= TUN_FASYNC;
  1853. } else
  1854. tfile->flags &= ~TUN_FASYNC;
  1855. ret = 0;
  1856. out:
  1857. return ret;
  1858. }
  1859. static int tun_chr_open(struct inode *inode, struct file * file)
  1860. {
  1861. struct net *net = current->nsproxy->net_ns;
  1862. struct tun_file *tfile;
  1863. DBG1(KERN_INFO, "tunX: tun_chr_open\n");
  1864. tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
  1865. &tun_proto, 0);
  1866. if (!tfile)
  1867. return -ENOMEM;
  1868. RCU_INIT_POINTER(tfile->tun, NULL);
  1869. tfile->flags = 0;
  1870. tfile->ifindex = 0;
  1871. init_waitqueue_head(&tfile->wq.wait);
  1872. RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
  1873. tfile->socket.file = file;
  1874. tfile->socket.ops = &tun_socket_ops;
  1875. sock_init_data(&tfile->socket, &tfile->sk);
  1876. tfile->sk.sk_write_space = tun_sock_write_space;
  1877. tfile->sk.sk_sndbuf = INT_MAX;
  1878. file->private_data = tfile;
  1879. INIT_LIST_HEAD(&tfile->next);
  1880. sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
  1881. return 0;
  1882. }
  1883. static int tun_chr_close(struct inode *inode, struct file *file)
  1884. {
  1885. struct tun_file *tfile = file->private_data;
  1886. tun_detach(tfile, true);
  1887. return 0;
  1888. }
  1889. #ifdef CONFIG_PROC_FS
  1890. static void tun_chr_show_fdinfo(struct seq_file *m, struct file *f)
  1891. {
  1892. struct tun_struct *tun;
  1893. struct ifreq ifr;
  1894. memset(&ifr, 0, sizeof(ifr));
  1895. rtnl_lock();
  1896. tun = tun_get(f);
  1897. if (tun)
  1898. tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
  1899. rtnl_unlock();
  1900. if (tun)
  1901. tun_put(tun);
  1902. seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
  1903. }
  1904. #endif
  1905. static const struct file_operations tun_fops = {
  1906. .owner = THIS_MODULE,
  1907. .llseek = no_llseek,
  1908. .read_iter = tun_chr_read_iter,
  1909. .write_iter = tun_chr_write_iter,
  1910. .poll = tun_chr_poll,
  1911. .unlocked_ioctl = tun_chr_ioctl,
  1912. #ifdef CONFIG_COMPAT
  1913. .compat_ioctl = tun_chr_compat_ioctl,
  1914. #endif
  1915. .open = tun_chr_open,
  1916. .release = tun_chr_close,
  1917. .fasync = tun_chr_fasync,
  1918. #ifdef CONFIG_PROC_FS
  1919. .show_fdinfo = tun_chr_show_fdinfo,
  1920. #endif
  1921. };
  1922. static struct miscdevice tun_miscdev = {
  1923. .minor = TUN_MINOR,
  1924. .name = "tun",
  1925. .nodename = "net/tun",
  1926. .fops = &tun_fops,
  1927. };
  1928. /* ethtool interface */
  1929. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1930. {
  1931. cmd->supported = 0;
  1932. cmd->advertising = 0;
  1933. ethtool_cmd_speed_set(cmd, SPEED_10);
  1934. cmd->duplex = DUPLEX_FULL;
  1935. cmd->port = PORT_TP;
  1936. cmd->phy_address = 0;
  1937. cmd->transceiver = XCVR_INTERNAL;
  1938. cmd->autoneg = AUTONEG_DISABLE;
  1939. cmd->maxtxpkt = 0;
  1940. cmd->maxrxpkt = 0;
  1941. return 0;
  1942. }
  1943. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1944. {
  1945. struct tun_struct *tun = netdev_priv(dev);
  1946. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1947. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1948. switch (tun->flags & TUN_TYPE_MASK) {
  1949. case IFF_TUN:
  1950. strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
  1951. break;
  1952. case IFF_TAP:
  1953. strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
  1954. break;
  1955. }
  1956. }
  1957. static u32 tun_get_msglevel(struct net_device *dev)
  1958. {
  1959. #ifdef TUN_DEBUG
  1960. struct tun_struct *tun = netdev_priv(dev);
  1961. return tun->debug;
  1962. #else
  1963. return -EOPNOTSUPP;
  1964. #endif
  1965. }
  1966. static void tun_set_msglevel(struct net_device *dev, u32 value)
  1967. {
  1968. #ifdef TUN_DEBUG
  1969. struct tun_struct *tun = netdev_priv(dev);
  1970. tun->debug = value;
  1971. #endif
  1972. }
  1973. static const struct ethtool_ops tun_ethtool_ops = {
  1974. .get_settings = tun_get_settings,
  1975. .get_drvinfo = tun_get_drvinfo,
  1976. .get_msglevel = tun_get_msglevel,
  1977. .set_msglevel = tun_set_msglevel,
  1978. .get_link = ethtool_op_get_link,
  1979. .get_ts_info = ethtool_op_get_ts_info,
  1980. };
  1981. static int __init tun_init(void)
  1982. {
  1983. int ret = 0;
  1984. pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1985. pr_info("%s\n", DRV_COPYRIGHT);
  1986. ret = rtnl_link_register(&tun_link_ops);
  1987. if (ret) {
  1988. pr_err("Can't register link_ops\n");
  1989. goto err_linkops;
  1990. }
  1991. ret = misc_register(&tun_miscdev);
  1992. if (ret) {
  1993. pr_err("Can't register misc device %d\n", TUN_MINOR);
  1994. goto err_misc;
  1995. }
  1996. return 0;
  1997. err_misc:
  1998. rtnl_link_unregister(&tun_link_ops);
  1999. err_linkops:
  2000. return ret;
  2001. }
  2002. static void tun_cleanup(void)
  2003. {
  2004. misc_deregister(&tun_miscdev);
  2005. rtnl_link_unregister(&tun_link_ops);
  2006. }
  2007. /* Get an underlying socket object from tun file. Returns error unless file is
  2008. * attached to a device. The returned object works like a packet socket, it
  2009. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  2010. * holding a reference to the file for as long as the socket is in use. */
  2011. struct socket *tun_get_socket(struct file *file)
  2012. {
  2013. struct tun_file *tfile;
  2014. if (file->f_op != &tun_fops)
  2015. return ERR_PTR(-EINVAL);
  2016. tfile = file->private_data;
  2017. if (!tfile)
  2018. return ERR_PTR(-EBADFD);
  2019. return &tfile->socket;
  2020. }
  2021. EXPORT_SYMBOL_GPL(tun_get_socket);
  2022. module_init(tun_init);
  2023. module_exit(tun_cleanup);
  2024. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  2025. MODULE_AUTHOR(DRV_COPYRIGHT);
  2026. MODULE_LICENSE("GPL");
  2027. MODULE_ALIAS_MISCDEV(TUN_MINOR);
  2028. MODULE_ALIAS("devname:net/tun");