tun.c 54 KB

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