tun.c 54 KB

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