tun.c 56 KB

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