tun.c 60 KB

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