tun.c 54 KB

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