tun.c 59 KB

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