tun.c 54 KB

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