tun.c 56 KB

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