tun.c 55 KB

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