tun.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253
  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. if (!(tun->flags & TUN_ONE_QUEUE)) {
  586. /* Normal queueing mode. */
  587. /* Packet scheduler handles dropping of further packets. */
  588. netif_stop_subqueue(dev, txq);
  589. /* We won't see all dropped packets individually, so overrun
  590. * error is more appropriate. */
  591. dev->stats.tx_fifo_errors++;
  592. } else {
  593. /* Single queue mode.
  594. * Driver handles dropping of all packets itself. */
  595. goto drop;
  596. }
  597. }
  598. /* Orphan the skb - required as we might hang on to it
  599. * for indefinite time. */
  600. if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
  601. goto drop;
  602. skb_orphan(skb);
  603. /* Enqueue packet */
  604. skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
  605. /* Notify and wake up reader process */
  606. if (tfile->flags & TUN_FASYNC)
  607. kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
  608. wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
  609. POLLRDNORM | POLLRDBAND);
  610. rcu_read_unlock();
  611. return NETDEV_TX_OK;
  612. drop:
  613. dev->stats.tx_dropped++;
  614. skb_tx_error(skb);
  615. kfree_skb(skb);
  616. rcu_read_unlock();
  617. return NETDEV_TX_OK;
  618. }
  619. static void tun_net_mclist(struct net_device *dev)
  620. {
  621. /*
  622. * This callback is supposed to deal with mc filter in
  623. * _rx_ path and has nothing to do with the _tx_ path.
  624. * In rx path we always accept everything userspace gives us.
  625. */
  626. }
  627. #define MIN_MTU 68
  628. #define MAX_MTU 65535
  629. static int
  630. tun_net_change_mtu(struct net_device *dev, int new_mtu)
  631. {
  632. if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
  633. return -EINVAL;
  634. dev->mtu = new_mtu;
  635. return 0;
  636. }
  637. static netdev_features_t tun_net_fix_features(struct net_device *dev,
  638. netdev_features_t features)
  639. {
  640. struct tun_struct *tun = netdev_priv(dev);
  641. return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
  642. }
  643. #ifdef CONFIG_NET_POLL_CONTROLLER
  644. static void tun_poll_controller(struct net_device *dev)
  645. {
  646. /*
  647. * Tun only receives frames when:
  648. * 1) the char device endpoint gets data from user space
  649. * 2) the tun socket gets a sendmsg call from user space
  650. * Since both of those are syncronous operations, we are guaranteed
  651. * never to have pending data when we poll for it
  652. * so theres nothing to do here but return.
  653. * We need this though so netpoll recognizes us as an interface that
  654. * supports polling, which enables bridge devices in virt setups to
  655. * still use netconsole
  656. */
  657. return;
  658. }
  659. #endif
  660. static const struct net_device_ops tun_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_select_queue = tun_select_queue,
  668. #ifdef CONFIG_NET_POLL_CONTROLLER
  669. .ndo_poll_controller = tun_poll_controller,
  670. #endif
  671. };
  672. static const struct net_device_ops tap_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_set_rx_mode = tun_net_mclist,
  680. .ndo_set_mac_address = eth_mac_addr,
  681. .ndo_validate_addr = eth_validate_addr,
  682. .ndo_select_queue = tun_select_queue,
  683. #ifdef CONFIG_NET_POLL_CONTROLLER
  684. .ndo_poll_controller = tun_poll_controller,
  685. #endif
  686. };
  687. static int tun_flow_init(struct tun_struct *tun)
  688. {
  689. int i;
  690. tun->flow_cache = kmem_cache_create("tun_flow_cache",
  691. sizeof(struct tun_flow_entry), 0, 0,
  692. NULL);
  693. if (!tun->flow_cache)
  694. return -ENOMEM;
  695. for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
  696. INIT_HLIST_HEAD(&tun->flows[i]);
  697. tun->ageing_time = TUN_FLOW_EXPIRE;
  698. setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
  699. mod_timer(&tun->flow_gc_timer,
  700. round_jiffies_up(jiffies + tun->ageing_time));
  701. return 0;
  702. }
  703. static void tun_flow_uninit(struct tun_struct *tun)
  704. {
  705. del_timer_sync(&tun->flow_gc_timer);
  706. tun_flow_flush(tun);
  707. /* Wait for completion of call_rcu()'s */
  708. rcu_barrier();
  709. kmem_cache_destroy(tun->flow_cache);
  710. }
  711. /* Initialize net device. */
  712. static void tun_net_init(struct net_device *dev)
  713. {
  714. struct tun_struct *tun = netdev_priv(dev);
  715. switch (tun->flags & TUN_TYPE_MASK) {
  716. case TUN_TUN_DEV:
  717. dev->netdev_ops = &tun_netdev_ops;
  718. /* Point-to-Point TUN Device */
  719. dev->hard_header_len = 0;
  720. dev->addr_len = 0;
  721. dev->mtu = 1500;
  722. /* Zero header length */
  723. dev->type = ARPHRD_NONE;
  724. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  725. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  726. break;
  727. case TUN_TAP_DEV:
  728. dev->netdev_ops = &tap_netdev_ops;
  729. /* Ethernet TAP Device */
  730. ether_setup(dev);
  731. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  732. eth_hw_addr_random(dev);
  733. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  734. break;
  735. }
  736. }
  737. /* Character device part */
  738. /* Poll */
  739. static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
  740. {
  741. struct tun_file *tfile = file->private_data;
  742. struct tun_struct *tun = __tun_get(tfile);
  743. struct sock *sk;
  744. unsigned int mask = 0;
  745. if (!tun)
  746. return POLLERR;
  747. sk = tfile->socket.sk;
  748. tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
  749. poll_wait(file, &tfile->wq.wait, wait);
  750. if (!skb_queue_empty(&sk->sk_receive_queue))
  751. mask |= POLLIN | POLLRDNORM;
  752. if (sock_writeable(sk) ||
  753. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
  754. sock_writeable(sk)))
  755. mask |= POLLOUT | POLLWRNORM;
  756. if (tun->dev->reg_state != NETREG_REGISTERED)
  757. mask = POLLERR;
  758. tun_put(tun);
  759. return mask;
  760. }
  761. /* prepad is the amount to reserve at front. len is length after that.
  762. * linear is a hint as to how much to copy (usually headers). */
  763. static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
  764. size_t prepad, size_t len,
  765. size_t linear, int noblock)
  766. {
  767. struct sock *sk = tfile->socket.sk;
  768. struct sk_buff *skb;
  769. int err;
  770. /* Under a page? Don't bother with paged skb. */
  771. if (prepad + len < PAGE_SIZE || !linear)
  772. linear = len;
  773. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  774. &err);
  775. if (!skb)
  776. return ERR_PTR(err);
  777. skb_reserve(skb, prepad);
  778. skb_put(skb, linear);
  779. skb->data_len = len - linear;
  780. skb->len += len - linear;
  781. return skb;
  782. }
  783. /* set skb frags from iovec, this can move to core network code for reuse */
  784. static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
  785. int offset, size_t count)
  786. {
  787. int len = iov_length(from, count) - offset;
  788. int copy = skb_headlen(skb);
  789. int size, offset1 = 0;
  790. int i = 0;
  791. /* Skip over from offset */
  792. while (count && (offset >= from->iov_len)) {
  793. offset -= from->iov_len;
  794. ++from;
  795. --count;
  796. }
  797. /* copy up to skb headlen */
  798. while (count && (copy > 0)) {
  799. size = min_t(unsigned int, copy, from->iov_len - offset);
  800. if (copy_from_user(skb->data + offset1, from->iov_base + offset,
  801. size))
  802. return -EFAULT;
  803. if (copy > size) {
  804. ++from;
  805. --count;
  806. offset = 0;
  807. } else
  808. offset += size;
  809. copy -= size;
  810. offset1 += size;
  811. }
  812. if (len == offset1)
  813. return 0;
  814. while (count--) {
  815. struct page *page[MAX_SKB_FRAGS];
  816. int num_pages;
  817. unsigned long base;
  818. unsigned long truesize;
  819. len = from->iov_len - offset;
  820. if (!len) {
  821. offset = 0;
  822. ++from;
  823. continue;
  824. }
  825. base = (unsigned long)from->iov_base + offset;
  826. size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
  827. if (i + size > MAX_SKB_FRAGS)
  828. return -EMSGSIZE;
  829. num_pages = get_user_pages_fast(base, size, 0, &page[i]);
  830. if (num_pages != size) {
  831. for (i = 0; i < num_pages; i++)
  832. put_page(page[i]);
  833. return -EFAULT;
  834. }
  835. truesize = size * PAGE_SIZE;
  836. skb->data_len += len;
  837. skb->len += len;
  838. skb->truesize += truesize;
  839. atomic_add(truesize, &skb->sk->sk_wmem_alloc);
  840. while (len) {
  841. int off = base & ~PAGE_MASK;
  842. int size = min_t(int, len, PAGE_SIZE - off);
  843. __skb_fill_page_desc(skb, i, page[i], off, size);
  844. skb_shinfo(skb)->nr_frags++;
  845. /* increase sk_wmem_alloc */
  846. base += size;
  847. len -= size;
  848. i++;
  849. }
  850. offset = 0;
  851. ++from;
  852. }
  853. return 0;
  854. }
  855. /* Get packet from user space buffer */
  856. static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
  857. void *msg_control, const struct iovec *iv,
  858. size_t total_len, size_t count, int noblock)
  859. {
  860. struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
  861. struct sk_buff *skb;
  862. size_t len = total_len, align = NET_SKB_PAD;
  863. struct virtio_net_hdr gso = { 0 };
  864. int offset = 0;
  865. int copylen;
  866. bool zerocopy = false;
  867. int err;
  868. if (!(tun->flags & TUN_NO_PI)) {
  869. if ((len -= sizeof(pi)) > total_len)
  870. return -EINVAL;
  871. if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
  872. return -EFAULT;
  873. offset += sizeof(pi);
  874. }
  875. if (tun->flags & TUN_VNET_HDR) {
  876. if ((len -= tun->vnet_hdr_sz) > total_len)
  877. return -EINVAL;
  878. if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
  879. return -EFAULT;
  880. if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  881. gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
  882. gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
  883. if (gso.hdr_len > len)
  884. return -EINVAL;
  885. offset += tun->vnet_hdr_sz;
  886. }
  887. if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
  888. align += NET_IP_ALIGN;
  889. if (unlikely(len < ETH_HLEN ||
  890. (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
  891. return -EINVAL;
  892. }
  893. if (msg_control)
  894. zerocopy = true;
  895. if (zerocopy) {
  896. /* Userspace may produce vectors with count greater than
  897. * MAX_SKB_FRAGS, so we need to linearize parts of the skb
  898. * to let the rest of data to be fit in the frags.
  899. */
  900. if (count > MAX_SKB_FRAGS) {
  901. copylen = iov_length(iv, count - MAX_SKB_FRAGS);
  902. if (copylen < offset)
  903. copylen = 0;
  904. else
  905. copylen -= offset;
  906. } else
  907. copylen = 0;
  908. /* There are 256 bytes to be copied in skb, so there is enough
  909. * room for skb expand head in case it is used.
  910. * The rest of the buffer is mapped from userspace.
  911. */
  912. if (copylen < gso.hdr_len)
  913. copylen = gso.hdr_len;
  914. if (!copylen)
  915. copylen = GOODCOPY_LEN;
  916. } else
  917. copylen = len;
  918. skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock);
  919. if (IS_ERR(skb)) {
  920. if (PTR_ERR(skb) != -EAGAIN)
  921. tun->dev->stats.rx_dropped++;
  922. return PTR_ERR(skb);
  923. }
  924. if (zerocopy)
  925. err = zerocopy_sg_from_iovec(skb, iv, offset, count);
  926. else
  927. err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
  928. if (err) {
  929. tun->dev->stats.rx_dropped++;
  930. kfree_skb(skb);
  931. return -EFAULT;
  932. }
  933. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  934. if (!skb_partial_csum_set(skb, gso.csum_start,
  935. gso.csum_offset)) {
  936. tun->dev->stats.rx_frame_errors++;
  937. kfree_skb(skb);
  938. return -EINVAL;
  939. }
  940. }
  941. switch (tun->flags & TUN_TYPE_MASK) {
  942. case TUN_TUN_DEV:
  943. if (tun->flags & TUN_NO_PI) {
  944. switch (skb->data[0] & 0xf0) {
  945. case 0x40:
  946. pi.proto = htons(ETH_P_IP);
  947. break;
  948. case 0x60:
  949. pi.proto = htons(ETH_P_IPV6);
  950. break;
  951. default:
  952. tun->dev->stats.rx_dropped++;
  953. kfree_skb(skb);
  954. return -EINVAL;
  955. }
  956. }
  957. skb_reset_mac_header(skb);
  958. skb->protocol = pi.proto;
  959. skb->dev = tun->dev;
  960. break;
  961. case TUN_TAP_DEV:
  962. skb->protocol = eth_type_trans(skb, tun->dev);
  963. break;
  964. }
  965. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  966. pr_debug("GSO!\n");
  967. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  968. case VIRTIO_NET_HDR_GSO_TCPV4:
  969. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  970. break;
  971. case VIRTIO_NET_HDR_GSO_TCPV6:
  972. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  973. break;
  974. case VIRTIO_NET_HDR_GSO_UDP:
  975. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  976. break;
  977. default:
  978. tun->dev->stats.rx_frame_errors++;
  979. kfree_skb(skb);
  980. return -EINVAL;
  981. }
  982. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  983. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  984. skb_shinfo(skb)->gso_size = gso.gso_size;
  985. if (skb_shinfo(skb)->gso_size == 0) {
  986. tun->dev->stats.rx_frame_errors++;
  987. kfree_skb(skb);
  988. return -EINVAL;
  989. }
  990. /* Header must be checked, and gso_segs computed. */
  991. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  992. skb_shinfo(skb)->gso_segs = 0;
  993. }
  994. /* copy skb_ubuf_info for callback when skb has no error */
  995. if (zerocopy) {
  996. skb_shinfo(skb)->destructor_arg = msg_control;
  997. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  998. }
  999. netif_rx_ni(skb);
  1000. tun->dev->stats.rx_packets++;
  1001. tun->dev->stats.rx_bytes += len;
  1002. tun_flow_update(tun, skb, tfile->queue_index);
  1003. return total_len;
  1004. }
  1005. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  1006. unsigned long count, loff_t pos)
  1007. {
  1008. struct file *file = iocb->ki_filp;
  1009. struct tun_struct *tun = tun_get(file);
  1010. struct tun_file *tfile = file->private_data;
  1011. ssize_t result;
  1012. if (!tun)
  1013. return -EBADFD;
  1014. tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
  1015. result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
  1016. count, file->f_flags & O_NONBLOCK);
  1017. tun_put(tun);
  1018. return result;
  1019. }
  1020. /* Put packet to the user space buffer */
  1021. static ssize_t tun_put_user(struct tun_struct *tun,
  1022. struct tun_file *tfile,
  1023. struct sk_buff *skb,
  1024. const struct iovec *iv, int len)
  1025. {
  1026. struct tun_pi pi = { 0, skb->protocol };
  1027. ssize_t total = 0;
  1028. if (!(tun->flags & TUN_NO_PI)) {
  1029. if ((len -= sizeof(pi)) < 0)
  1030. return -EINVAL;
  1031. if (len < skb->len) {
  1032. /* Packet will be striped */
  1033. pi.flags |= TUN_PKT_STRIP;
  1034. }
  1035. if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
  1036. return -EFAULT;
  1037. total += sizeof(pi);
  1038. }
  1039. if (tun->flags & TUN_VNET_HDR) {
  1040. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  1041. if ((len -= tun->vnet_hdr_sz) < 0)
  1042. return -EINVAL;
  1043. if (skb_is_gso(skb)) {
  1044. struct skb_shared_info *sinfo = skb_shinfo(skb);
  1045. /* This is a hint as to how much should be linear. */
  1046. gso.hdr_len = skb_headlen(skb);
  1047. gso.gso_size = sinfo->gso_size;
  1048. if (sinfo->gso_type & SKB_GSO_TCPV4)
  1049. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  1050. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  1051. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  1052. else if (sinfo->gso_type & SKB_GSO_UDP)
  1053. gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  1054. else {
  1055. pr_err("unexpected GSO type: "
  1056. "0x%x, gso_size %d, hdr_len %d\n",
  1057. sinfo->gso_type, gso.gso_size,
  1058. gso.hdr_len);
  1059. print_hex_dump(KERN_ERR, "tun: ",
  1060. DUMP_PREFIX_NONE,
  1061. 16, 1, skb->head,
  1062. min((int)gso.hdr_len, 64), true);
  1063. WARN_ON_ONCE(1);
  1064. return -EINVAL;
  1065. }
  1066. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  1067. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  1068. } else
  1069. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  1070. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1071. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  1072. gso.csum_start = skb_checksum_start_offset(skb);
  1073. gso.csum_offset = skb->csum_offset;
  1074. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  1075. gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
  1076. } /* else everything is zero */
  1077. if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
  1078. sizeof(gso))))
  1079. return -EFAULT;
  1080. total += tun->vnet_hdr_sz;
  1081. }
  1082. len = min_t(int, skb->len, len);
  1083. skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
  1084. total += skb->len;
  1085. tun->dev->stats.tx_packets++;
  1086. tun->dev->stats.tx_bytes += len;
  1087. return total;
  1088. }
  1089. static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
  1090. struct kiocb *iocb, const struct iovec *iv,
  1091. ssize_t len, int noblock)
  1092. {
  1093. DECLARE_WAITQUEUE(wait, current);
  1094. struct sk_buff *skb;
  1095. ssize_t ret = 0;
  1096. tun_debug(KERN_INFO, tun, "tun_do_read\n");
  1097. if (unlikely(!noblock))
  1098. add_wait_queue(&tfile->wq.wait, &wait);
  1099. while (len) {
  1100. current->state = TASK_INTERRUPTIBLE;
  1101. /* Read frames from the queue */
  1102. if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
  1103. if (noblock) {
  1104. ret = -EAGAIN;
  1105. break;
  1106. }
  1107. if (signal_pending(current)) {
  1108. ret = -ERESTARTSYS;
  1109. break;
  1110. }
  1111. if (tun->dev->reg_state != NETREG_REGISTERED) {
  1112. ret = -EIO;
  1113. break;
  1114. }
  1115. /* Nothing to read, let's sleep */
  1116. schedule();
  1117. continue;
  1118. }
  1119. netif_wake_subqueue(tun->dev, tfile->queue_index);
  1120. ret = tun_put_user(tun, tfile, skb, iv, len);
  1121. kfree_skb(skb);
  1122. break;
  1123. }
  1124. current->state = TASK_RUNNING;
  1125. if (unlikely(!noblock))
  1126. remove_wait_queue(&tfile->wq.wait, &wait);
  1127. return ret;
  1128. }
  1129. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  1130. unsigned long count, loff_t pos)
  1131. {
  1132. struct file *file = iocb->ki_filp;
  1133. struct tun_file *tfile = file->private_data;
  1134. struct tun_struct *tun = __tun_get(tfile);
  1135. ssize_t len, ret;
  1136. if (!tun)
  1137. return -EBADFD;
  1138. len = iov_length(iv, count);
  1139. if (len < 0) {
  1140. ret = -EINVAL;
  1141. goto out;
  1142. }
  1143. ret = tun_do_read(tun, tfile, iocb, iv, len,
  1144. file->f_flags & O_NONBLOCK);
  1145. ret = min_t(ssize_t, ret, len);
  1146. out:
  1147. tun_put(tun);
  1148. return ret;
  1149. }
  1150. static void tun_free_netdev(struct net_device *dev)
  1151. {
  1152. struct tun_struct *tun = netdev_priv(dev);
  1153. tun_flow_uninit(tun);
  1154. free_netdev(dev);
  1155. }
  1156. static void tun_setup(struct net_device *dev)
  1157. {
  1158. struct tun_struct *tun = netdev_priv(dev);
  1159. tun->owner = INVALID_UID;
  1160. tun->group = INVALID_GID;
  1161. dev->ethtool_ops = &tun_ethtool_ops;
  1162. dev->destructor = tun_free_netdev;
  1163. }
  1164. /* Trivial set of netlink ops to allow deleting tun or tap
  1165. * device with netlink.
  1166. */
  1167. static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
  1168. {
  1169. return -EINVAL;
  1170. }
  1171. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  1172. .kind = DRV_NAME,
  1173. .priv_size = sizeof(struct tun_struct),
  1174. .setup = tun_setup,
  1175. .validate = tun_validate,
  1176. };
  1177. static void tun_sock_write_space(struct sock *sk)
  1178. {
  1179. struct tun_file *tfile;
  1180. wait_queue_head_t *wqueue;
  1181. if (!sock_writeable(sk))
  1182. return;
  1183. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  1184. return;
  1185. wqueue = sk_sleep(sk);
  1186. if (wqueue && waitqueue_active(wqueue))
  1187. wake_up_interruptible_sync_poll(wqueue, POLLOUT |
  1188. POLLWRNORM | POLLWRBAND);
  1189. tfile = container_of(sk, struct tun_file, sk);
  1190. kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
  1191. }
  1192. static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
  1193. struct msghdr *m, size_t total_len)
  1194. {
  1195. int ret;
  1196. struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  1197. struct tun_struct *tun = __tun_get(tfile);
  1198. if (!tun)
  1199. return -EBADFD;
  1200. ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
  1201. m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
  1202. tun_put(tun);
  1203. return ret;
  1204. }
  1205. static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
  1206. struct msghdr *m, size_t total_len,
  1207. int flags)
  1208. {
  1209. struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  1210. struct tun_struct *tun = __tun_get(tfile);
  1211. int ret;
  1212. if (!tun)
  1213. return -EBADFD;
  1214. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  1215. return -EINVAL;
  1216. ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
  1217. flags & MSG_DONTWAIT);
  1218. if (ret > total_len) {
  1219. m->msg_flags |= MSG_TRUNC;
  1220. ret = flags & MSG_TRUNC ? ret : total_len;
  1221. }
  1222. tun_put(tun);
  1223. return ret;
  1224. }
  1225. static int tun_release(struct socket *sock)
  1226. {
  1227. if (sock->sk)
  1228. sock_put(sock->sk);
  1229. return 0;
  1230. }
  1231. /* Ops structure to mimic raw sockets with tun */
  1232. static const struct proto_ops tun_socket_ops = {
  1233. .sendmsg = tun_sendmsg,
  1234. .recvmsg = tun_recvmsg,
  1235. .release = tun_release,
  1236. };
  1237. static struct proto tun_proto = {
  1238. .name = "tun",
  1239. .owner = THIS_MODULE,
  1240. .obj_size = sizeof(struct tun_file),
  1241. };
  1242. static int tun_flags(struct tun_struct *tun)
  1243. {
  1244. int flags = 0;
  1245. if (tun->flags & TUN_TUN_DEV)
  1246. flags |= IFF_TUN;
  1247. else
  1248. flags |= IFF_TAP;
  1249. if (tun->flags & TUN_NO_PI)
  1250. flags |= IFF_NO_PI;
  1251. if (tun->flags & TUN_ONE_QUEUE)
  1252. flags |= IFF_ONE_QUEUE;
  1253. if (tun->flags & TUN_VNET_HDR)
  1254. flags |= IFF_VNET_HDR;
  1255. if (tun->flags & TUN_TAP_MQ)
  1256. flags |= IFF_MULTI_QUEUE;
  1257. return flags;
  1258. }
  1259. static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
  1260. char *buf)
  1261. {
  1262. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  1263. return sprintf(buf, "0x%x\n", tun_flags(tun));
  1264. }
  1265. static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
  1266. char *buf)
  1267. {
  1268. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  1269. return uid_valid(tun->owner)?
  1270. sprintf(buf, "%u\n",
  1271. from_kuid_munged(current_user_ns(), tun->owner)):
  1272. sprintf(buf, "-1\n");
  1273. }
  1274. static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
  1275. char *buf)
  1276. {
  1277. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  1278. return gid_valid(tun->group) ?
  1279. sprintf(buf, "%u\n",
  1280. from_kgid_munged(current_user_ns(), tun->group)):
  1281. sprintf(buf, "-1\n");
  1282. }
  1283. static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
  1284. static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
  1285. static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
  1286. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  1287. {
  1288. struct tun_struct *tun;
  1289. struct tun_file *tfile = file->private_data;
  1290. struct net_device *dev;
  1291. int err;
  1292. dev = __dev_get_by_name(net, ifr->ifr_name);
  1293. if (dev) {
  1294. if (ifr->ifr_flags & IFF_TUN_EXCL)
  1295. return -EBUSY;
  1296. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  1297. tun = netdev_priv(dev);
  1298. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  1299. tun = netdev_priv(dev);
  1300. else
  1301. return -EINVAL;
  1302. if (tun_not_capable(tun))
  1303. return -EPERM;
  1304. err = security_tun_dev_attach(tfile->socket.sk);
  1305. if (err < 0)
  1306. return err;
  1307. err = tun_attach(tun, file);
  1308. if (err < 0)
  1309. return err;
  1310. }
  1311. else {
  1312. char *name;
  1313. unsigned long flags = 0;
  1314. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1315. return -EPERM;
  1316. err = security_tun_dev_create();
  1317. if (err < 0)
  1318. return err;
  1319. /* Set dev type */
  1320. if (ifr->ifr_flags & IFF_TUN) {
  1321. /* TUN device */
  1322. flags |= TUN_TUN_DEV;
  1323. name = "tun%d";
  1324. } else if (ifr->ifr_flags & IFF_TAP) {
  1325. /* TAP device */
  1326. flags |= TUN_TAP_DEV;
  1327. name = "tap%d";
  1328. } else
  1329. return -EINVAL;
  1330. if (*ifr->ifr_name)
  1331. name = ifr->ifr_name;
  1332. dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
  1333. tun_setup,
  1334. MAX_TAP_QUEUES, MAX_TAP_QUEUES);
  1335. if (!dev)
  1336. return -ENOMEM;
  1337. dev_net_set(dev, net);
  1338. dev->rtnl_link_ops = &tun_link_ops;
  1339. tun = netdev_priv(dev);
  1340. tun->dev = dev;
  1341. tun->flags = flags;
  1342. tun->txflt.count = 0;
  1343. tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  1344. tun->filter_attached = false;
  1345. tun->sndbuf = tfile->socket.sk->sk_sndbuf;
  1346. spin_lock_init(&tun->lock);
  1347. security_tun_dev_post_create(&tfile->sk);
  1348. tun_net_init(dev);
  1349. if (tun_flow_init(tun))
  1350. goto err_free_dev;
  1351. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
  1352. TUN_USER_FEATURES;
  1353. dev->features = dev->hw_features;
  1354. err = tun_attach(tun, file);
  1355. if (err < 0)
  1356. goto err_free_dev;
  1357. err = register_netdevice(tun->dev);
  1358. if (err < 0)
  1359. goto err_free_dev;
  1360. if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
  1361. device_create_file(&tun->dev->dev, &dev_attr_owner) ||
  1362. device_create_file(&tun->dev->dev, &dev_attr_group))
  1363. pr_err("Failed to create tun sysfs files\n");
  1364. netif_carrier_on(tun->dev);
  1365. }
  1366. tun_debug(KERN_INFO, tun, "tun_set_iff\n");
  1367. if (ifr->ifr_flags & IFF_NO_PI)
  1368. tun->flags |= TUN_NO_PI;
  1369. else
  1370. tun->flags &= ~TUN_NO_PI;
  1371. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  1372. tun->flags |= TUN_ONE_QUEUE;
  1373. else
  1374. tun->flags &= ~TUN_ONE_QUEUE;
  1375. if (ifr->ifr_flags & IFF_VNET_HDR)
  1376. tun->flags |= TUN_VNET_HDR;
  1377. else
  1378. tun->flags &= ~TUN_VNET_HDR;
  1379. if (ifr->ifr_flags & IFF_MULTI_QUEUE)
  1380. tun->flags |= TUN_TAP_MQ;
  1381. else
  1382. tun->flags &= ~TUN_TAP_MQ;
  1383. /* Make sure persistent devices do not get stuck in
  1384. * xoff state.
  1385. */
  1386. if (netif_running(tun->dev))
  1387. netif_tx_wake_all_queues(tun->dev);
  1388. strcpy(ifr->ifr_name, tun->dev->name);
  1389. return 0;
  1390. err_free_dev:
  1391. free_netdev(dev);
  1392. return err;
  1393. }
  1394. static void tun_get_iff(struct net *net, struct tun_struct *tun,
  1395. struct ifreq *ifr)
  1396. {
  1397. tun_debug(KERN_INFO, tun, "tun_get_iff\n");
  1398. strcpy(ifr->ifr_name, tun->dev->name);
  1399. ifr->ifr_flags = tun_flags(tun);
  1400. }
  1401. /* This is like a cut-down ethtool ops, except done via tun fd so no
  1402. * privs required. */
  1403. static int set_offload(struct tun_struct *tun, unsigned long arg)
  1404. {
  1405. netdev_features_t features = 0;
  1406. if (arg & TUN_F_CSUM) {
  1407. features |= NETIF_F_HW_CSUM;
  1408. arg &= ~TUN_F_CSUM;
  1409. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  1410. if (arg & TUN_F_TSO_ECN) {
  1411. features |= NETIF_F_TSO_ECN;
  1412. arg &= ~TUN_F_TSO_ECN;
  1413. }
  1414. if (arg & TUN_F_TSO4)
  1415. features |= NETIF_F_TSO;
  1416. if (arg & TUN_F_TSO6)
  1417. features |= NETIF_F_TSO6;
  1418. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  1419. }
  1420. if (arg & TUN_F_UFO) {
  1421. features |= NETIF_F_UFO;
  1422. arg &= ~TUN_F_UFO;
  1423. }
  1424. }
  1425. /* This gives the user a way to test for new features in future by
  1426. * trying to set them. */
  1427. if (arg)
  1428. return -EINVAL;
  1429. tun->set_features = features;
  1430. netdev_update_features(tun->dev);
  1431. return 0;
  1432. }
  1433. static void tun_detach_filter(struct tun_struct *tun, int n)
  1434. {
  1435. int i;
  1436. struct tun_file *tfile;
  1437. for (i = 0; i < n; i++) {
  1438. tfile = rcu_dereference_protected(tun->tfiles[i],
  1439. lockdep_rtnl_is_held());
  1440. sk_detach_filter(tfile->socket.sk);
  1441. }
  1442. tun->filter_attached = false;
  1443. }
  1444. static int tun_attach_filter(struct tun_struct *tun)
  1445. {
  1446. int i, ret = 0;
  1447. struct tun_file *tfile;
  1448. for (i = 0; i < tun->numqueues; i++) {
  1449. tfile = rcu_dereference_protected(tun->tfiles[i],
  1450. lockdep_rtnl_is_held());
  1451. ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
  1452. if (ret) {
  1453. tun_detach_filter(tun, i);
  1454. return ret;
  1455. }
  1456. }
  1457. tun->filter_attached = true;
  1458. return ret;
  1459. }
  1460. static void tun_set_sndbuf(struct tun_struct *tun)
  1461. {
  1462. struct tun_file *tfile;
  1463. int i;
  1464. for (i = 0; i < tun->numqueues; i++) {
  1465. tfile = rcu_dereference_protected(tun->tfiles[i],
  1466. lockdep_rtnl_is_held());
  1467. tfile->socket.sk->sk_sndbuf = tun->sndbuf;
  1468. }
  1469. }
  1470. static int tun_set_queue(struct file *file, struct ifreq *ifr)
  1471. {
  1472. struct tun_file *tfile = file->private_data;
  1473. struct tun_struct *tun;
  1474. struct net_device *dev;
  1475. int ret = 0;
  1476. rtnl_lock();
  1477. if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
  1478. dev = __dev_get_by_name(tfile->net, ifr->ifr_name);
  1479. if (!dev) {
  1480. ret = -EINVAL;
  1481. goto unlock;
  1482. }
  1483. tun = netdev_priv(dev);
  1484. if (dev->netdev_ops != &tap_netdev_ops &&
  1485. dev->netdev_ops != &tun_netdev_ops)
  1486. ret = -EINVAL;
  1487. else if (tun_not_capable(tun))
  1488. ret = -EPERM;
  1489. else
  1490. ret = tun_attach(tun, file);
  1491. } else if (ifr->ifr_flags & IFF_DETACH_QUEUE)
  1492. __tun_detach(tfile, false);
  1493. else
  1494. ret = -EINVAL;
  1495. unlock:
  1496. rtnl_unlock();
  1497. return ret;
  1498. }
  1499. static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  1500. unsigned long arg, int ifreq_len)
  1501. {
  1502. struct tun_file *tfile = file->private_data;
  1503. struct tun_struct *tun;
  1504. void __user* argp = (void __user*)arg;
  1505. struct ifreq ifr;
  1506. kuid_t owner;
  1507. kgid_t group;
  1508. int sndbuf;
  1509. int vnet_hdr_sz;
  1510. int ret;
  1511. if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
  1512. if (copy_from_user(&ifr, argp, ifreq_len))
  1513. return -EFAULT;
  1514. } else {
  1515. memset(&ifr, 0, sizeof(ifr));
  1516. }
  1517. if (cmd == TUNGETFEATURES) {
  1518. /* Currently this just means: "what IFF flags are valid?".
  1519. * This is needed because we never checked for invalid flags on
  1520. * TUNSETIFF. */
  1521. return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
  1522. IFF_VNET_HDR | IFF_MULTI_QUEUE,
  1523. (unsigned int __user*)argp);
  1524. } else if (cmd == TUNSETQUEUE)
  1525. return tun_set_queue(file, &ifr);
  1526. ret = 0;
  1527. rtnl_lock();
  1528. tun = __tun_get(tfile);
  1529. if (cmd == TUNSETIFF && !tun) {
  1530. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  1531. ret = tun_set_iff(tfile->net, file, &ifr);
  1532. if (ret)
  1533. goto unlock;
  1534. if (copy_to_user(argp, &ifr, ifreq_len))
  1535. ret = -EFAULT;
  1536. goto unlock;
  1537. }
  1538. ret = -EBADFD;
  1539. if (!tun)
  1540. goto unlock;
  1541. tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
  1542. ret = 0;
  1543. switch (cmd) {
  1544. case TUNGETIFF:
  1545. tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
  1546. if (copy_to_user(argp, &ifr, ifreq_len))
  1547. ret = -EFAULT;
  1548. break;
  1549. case TUNSETNOCSUM:
  1550. /* Disable/Enable checksum */
  1551. /* [unimplemented] */
  1552. tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
  1553. arg ? "disabled" : "enabled");
  1554. break;
  1555. case TUNSETPERSIST:
  1556. /* Disable/Enable persist mode. Keep an extra reference to the
  1557. * module to prevent the module being unprobed.
  1558. */
  1559. if (arg) {
  1560. tun->flags |= TUN_PERSIST;
  1561. __module_get(THIS_MODULE);
  1562. } else {
  1563. tun->flags &= ~TUN_PERSIST;
  1564. module_put(THIS_MODULE);
  1565. }
  1566. tun_debug(KERN_INFO, tun, "persist %s\n",
  1567. arg ? "enabled" : "disabled");
  1568. break;
  1569. case TUNSETOWNER:
  1570. /* Set owner of the device */
  1571. owner = make_kuid(current_user_ns(), arg);
  1572. if (!uid_valid(owner)) {
  1573. ret = -EINVAL;
  1574. break;
  1575. }
  1576. tun->owner = owner;
  1577. tun_debug(KERN_INFO, tun, "owner set to %u\n",
  1578. from_kuid(&init_user_ns, tun->owner));
  1579. break;
  1580. case TUNSETGROUP:
  1581. /* Set group of the device */
  1582. group = make_kgid(current_user_ns(), arg);
  1583. if (!gid_valid(group)) {
  1584. ret = -EINVAL;
  1585. break;
  1586. }
  1587. tun->group = group;
  1588. tun_debug(KERN_INFO, tun, "group set to %u\n",
  1589. from_kgid(&init_user_ns, tun->group));
  1590. break;
  1591. case TUNSETLINK:
  1592. /* Only allow setting the type when the interface is down */
  1593. if (tun->dev->flags & IFF_UP) {
  1594. tun_debug(KERN_INFO, tun,
  1595. "Linktype set failed because interface is up\n");
  1596. ret = -EBUSY;
  1597. } else {
  1598. tun->dev->type = (int) arg;
  1599. tun_debug(KERN_INFO, tun, "linktype set to %d\n",
  1600. tun->dev->type);
  1601. ret = 0;
  1602. }
  1603. break;
  1604. #ifdef TUN_DEBUG
  1605. case TUNSETDEBUG:
  1606. tun->debug = arg;
  1607. break;
  1608. #endif
  1609. case TUNSETOFFLOAD:
  1610. ret = set_offload(tun, arg);
  1611. break;
  1612. case TUNSETTXFILTER:
  1613. /* Can be set only for TAPs */
  1614. ret = -EINVAL;
  1615. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1616. break;
  1617. ret = update_filter(&tun->txflt, (void __user *)arg);
  1618. break;
  1619. case SIOCGIFHWADDR:
  1620. /* Get hw address */
  1621. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  1622. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  1623. if (copy_to_user(argp, &ifr, ifreq_len))
  1624. ret = -EFAULT;
  1625. break;
  1626. case SIOCSIFHWADDR:
  1627. /* Set hw address */
  1628. tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
  1629. ifr.ifr_hwaddr.sa_data);
  1630. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  1631. break;
  1632. case TUNGETSNDBUF:
  1633. sndbuf = tfile->socket.sk->sk_sndbuf;
  1634. if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
  1635. ret = -EFAULT;
  1636. break;
  1637. case TUNSETSNDBUF:
  1638. if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
  1639. ret = -EFAULT;
  1640. break;
  1641. }
  1642. tun->sndbuf = sndbuf;
  1643. tun_set_sndbuf(tun);
  1644. break;
  1645. case TUNGETVNETHDRSZ:
  1646. vnet_hdr_sz = tun->vnet_hdr_sz;
  1647. if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
  1648. ret = -EFAULT;
  1649. break;
  1650. case TUNSETVNETHDRSZ:
  1651. if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
  1652. ret = -EFAULT;
  1653. break;
  1654. }
  1655. if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
  1656. ret = -EINVAL;
  1657. break;
  1658. }
  1659. tun->vnet_hdr_sz = vnet_hdr_sz;
  1660. break;
  1661. case TUNATTACHFILTER:
  1662. /* Can be set only for TAPs */
  1663. ret = -EINVAL;
  1664. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1665. break;
  1666. ret = -EFAULT;
  1667. if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
  1668. break;
  1669. ret = tun_attach_filter(tun);
  1670. break;
  1671. case TUNDETACHFILTER:
  1672. /* Can be set only for TAPs */
  1673. ret = -EINVAL;
  1674. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1675. break;
  1676. ret = 0;
  1677. tun_detach_filter(tun, tun->numqueues);
  1678. break;
  1679. default:
  1680. ret = -EINVAL;
  1681. break;
  1682. }
  1683. unlock:
  1684. rtnl_unlock();
  1685. if (tun)
  1686. tun_put(tun);
  1687. return ret;
  1688. }
  1689. static long tun_chr_ioctl(struct file *file,
  1690. unsigned int cmd, unsigned long arg)
  1691. {
  1692. return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
  1693. }
  1694. #ifdef CONFIG_COMPAT
  1695. static long tun_chr_compat_ioctl(struct file *file,
  1696. unsigned int cmd, unsigned long arg)
  1697. {
  1698. switch (cmd) {
  1699. case TUNSETIFF:
  1700. case TUNGETIFF:
  1701. case TUNSETTXFILTER:
  1702. case TUNGETSNDBUF:
  1703. case TUNSETSNDBUF:
  1704. case SIOCGIFHWADDR:
  1705. case SIOCSIFHWADDR:
  1706. arg = (unsigned long)compat_ptr(arg);
  1707. break;
  1708. default:
  1709. arg = (compat_ulong_t)arg;
  1710. break;
  1711. }
  1712. /*
  1713. * compat_ifreq is shorter than ifreq, so we must not access beyond
  1714. * the end of that structure. All fields that are used in this
  1715. * driver are compatible though, we don't need to convert the
  1716. * contents.
  1717. */
  1718. return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
  1719. }
  1720. #endif /* CONFIG_COMPAT */
  1721. static int tun_chr_fasync(int fd, struct file *file, int on)
  1722. {
  1723. struct tun_file *tfile = file->private_data;
  1724. int ret;
  1725. if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
  1726. goto out;
  1727. if (on) {
  1728. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  1729. if (ret)
  1730. goto out;
  1731. tfile->flags |= TUN_FASYNC;
  1732. } else
  1733. tfile->flags &= ~TUN_FASYNC;
  1734. ret = 0;
  1735. out:
  1736. return ret;
  1737. }
  1738. static int tun_chr_open(struct inode *inode, struct file * file)
  1739. {
  1740. struct tun_file *tfile;
  1741. DBG1(KERN_INFO, "tunX: tun_chr_open\n");
  1742. tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
  1743. &tun_proto);
  1744. if (!tfile)
  1745. return -ENOMEM;
  1746. rcu_assign_pointer(tfile->tun, NULL);
  1747. tfile->net = get_net(current->nsproxy->net_ns);
  1748. tfile->flags = 0;
  1749. rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
  1750. init_waitqueue_head(&tfile->wq.wait);
  1751. tfile->socket.file = file;
  1752. tfile->socket.ops = &tun_socket_ops;
  1753. sock_init_data(&tfile->socket, &tfile->sk);
  1754. sk_change_net(&tfile->sk, tfile->net);
  1755. tfile->sk.sk_write_space = tun_sock_write_space;
  1756. tfile->sk.sk_sndbuf = INT_MAX;
  1757. file->private_data = tfile;
  1758. set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
  1759. return 0;
  1760. }
  1761. static int tun_chr_close(struct inode *inode, struct file *file)
  1762. {
  1763. struct tun_file *tfile = file->private_data;
  1764. struct net *net = tfile->net;
  1765. tun_detach(tfile, true);
  1766. put_net(net);
  1767. return 0;
  1768. }
  1769. static const struct file_operations tun_fops = {
  1770. .owner = THIS_MODULE,
  1771. .llseek = no_llseek,
  1772. .read = do_sync_read,
  1773. .aio_read = tun_chr_aio_read,
  1774. .write = do_sync_write,
  1775. .aio_write = tun_chr_aio_write,
  1776. .poll = tun_chr_poll,
  1777. .unlocked_ioctl = tun_chr_ioctl,
  1778. #ifdef CONFIG_COMPAT
  1779. .compat_ioctl = tun_chr_compat_ioctl,
  1780. #endif
  1781. .open = tun_chr_open,
  1782. .release = tun_chr_close,
  1783. .fasync = tun_chr_fasync
  1784. };
  1785. static struct miscdevice tun_miscdev = {
  1786. .minor = TUN_MINOR,
  1787. .name = "tun",
  1788. .nodename = "net/tun",
  1789. .fops = &tun_fops,
  1790. };
  1791. /* ethtool interface */
  1792. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1793. {
  1794. cmd->supported = 0;
  1795. cmd->advertising = 0;
  1796. ethtool_cmd_speed_set(cmd, SPEED_10);
  1797. cmd->duplex = DUPLEX_FULL;
  1798. cmd->port = PORT_TP;
  1799. cmd->phy_address = 0;
  1800. cmd->transceiver = XCVR_INTERNAL;
  1801. cmd->autoneg = AUTONEG_DISABLE;
  1802. cmd->maxtxpkt = 0;
  1803. cmd->maxrxpkt = 0;
  1804. return 0;
  1805. }
  1806. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1807. {
  1808. struct tun_struct *tun = netdev_priv(dev);
  1809. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1810. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1811. switch (tun->flags & TUN_TYPE_MASK) {
  1812. case TUN_TUN_DEV:
  1813. strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
  1814. break;
  1815. case TUN_TAP_DEV:
  1816. strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
  1817. break;
  1818. }
  1819. }
  1820. static u32 tun_get_msglevel(struct net_device *dev)
  1821. {
  1822. #ifdef TUN_DEBUG
  1823. struct tun_struct *tun = netdev_priv(dev);
  1824. return tun->debug;
  1825. #else
  1826. return -EOPNOTSUPP;
  1827. #endif
  1828. }
  1829. static void tun_set_msglevel(struct net_device *dev, u32 value)
  1830. {
  1831. #ifdef TUN_DEBUG
  1832. struct tun_struct *tun = netdev_priv(dev);
  1833. tun->debug = value;
  1834. #endif
  1835. }
  1836. static const struct ethtool_ops tun_ethtool_ops = {
  1837. .get_settings = tun_get_settings,
  1838. .get_drvinfo = tun_get_drvinfo,
  1839. .get_msglevel = tun_get_msglevel,
  1840. .set_msglevel = tun_set_msglevel,
  1841. .get_link = ethtool_op_get_link,
  1842. };
  1843. static int __init tun_init(void)
  1844. {
  1845. int ret = 0;
  1846. pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1847. pr_info("%s\n", DRV_COPYRIGHT);
  1848. ret = rtnl_link_register(&tun_link_ops);
  1849. if (ret) {
  1850. pr_err("Can't register link_ops\n");
  1851. goto err_linkops;
  1852. }
  1853. ret = misc_register(&tun_miscdev);
  1854. if (ret) {
  1855. pr_err("Can't register misc device %d\n", TUN_MINOR);
  1856. goto err_misc;
  1857. }
  1858. return 0;
  1859. err_misc:
  1860. rtnl_link_unregister(&tun_link_ops);
  1861. err_linkops:
  1862. return ret;
  1863. }
  1864. static void tun_cleanup(void)
  1865. {
  1866. misc_deregister(&tun_miscdev);
  1867. rtnl_link_unregister(&tun_link_ops);
  1868. }
  1869. /* Get an underlying socket object from tun file. Returns error unless file is
  1870. * attached to a device. The returned object works like a packet socket, it
  1871. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1872. * holding a reference to the file for as long as the socket is in use. */
  1873. struct socket *tun_get_socket(struct file *file)
  1874. {
  1875. struct tun_file *tfile;
  1876. if (file->f_op != &tun_fops)
  1877. return ERR_PTR(-EINVAL);
  1878. tfile = file->private_data;
  1879. if (!tfile)
  1880. return ERR_PTR(-EBADFD);
  1881. return &tfile->socket;
  1882. }
  1883. EXPORT_SYMBOL_GPL(tun_get_socket);
  1884. module_init(tun_init);
  1885. module_exit(tun_cleanup);
  1886. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1887. MODULE_AUTHOR(DRV_COPYRIGHT);
  1888. MODULE_LICENSE("GPL");
  1889. MODULE_ALIAS_MISCDEV(TUN_MINOR);
  1890. MODULE_ALIAS("devname:net/tun");