tun.c 59 KB

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