tun.c 53 KB

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