tun.c 54 KB

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