tun.c 53 KB

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