tun.c 54 KB

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