tun.c 56 KB

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