tun.c 53 KB

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