tun.c 59 KB

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