tun.c 54 KB

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