tun.c 56 KB

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