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