tun.c 53 KB

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