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