wext.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  1. /*
  2. * This file implement the Wireless Extensions APIs.
  3. *
  4. * Authors : Jean Tourrilhes - HPL - <jt@hpl.hp.com>
  5. * Copyright (c) 1997-2007 Jean Tourrilhes, All Rights Reserved.
  6. *
  7. * (As all part of the Linux kernel, this file is GPL)
  8. */
  9. /************************** DOCUMENTATION **************************/
  10. /*
  11. * API definition :
  12. * --------------
  13. * See <linux/wireless.h> for details of the APIs and the rest.
  14. *
  15. * History :
  16. * -------
  17. *
  18. * v1 - 5.12.01 - Jean II
  19. * o Created this file.
  20. *
  21. * v2 - 13.12.01 - Jean II
  22. * o Move /proc/net/wireless stuff from net/core/dev.c to here
  23. * o Make Wireless Extension IOCTLs go through here
  24. * o Added iw_handler handling ;-)
  25. * o Added standard ioctl description
  26. * o Initial dumb commit strategy based on orinoco.c
  27. *
  28. * v3 - 19.12.01 - Jean II
  29. * o Make sure we don't go out of standard_ioctl[] in ioctl_standard_call
  30. * o Add event dispatcher function
  31. * o Add event description
  32. * o Propagate events as rtnetlink IFLA_WIRELESS option
  33. * o Generate event on selected SET requests
  34. *
  35. * v4 - 18.04.02 - Jean II
  36. * o Fix stupid off by one in iw_ioctl_description : IW_ESSID_MAX_SIZE + 1
  37. *
  38. * v5 - 21.06.02 - Jean II
  39. * o Add IW_PRIV_TYPE_ADDR in priv_type_size (+cleanup)
  40. * o Reshuffle IW_HEADER_TYPE_XXX to map IW_PRIV_TYPE_XXX changes
  41. * o Add IWEVCUSTOM for driver specific event/scanning token
  42. * o Turn on WE_STRICT_WRITE by default + kernel warning
  43. * o Fix WE_STRICT_WRITE in ioctl_export_private() (32 => iw_num)
  44. * o Fix off-by-one in test (extra_size <= IFNAMSIZ)
  45. *
  46. * v6 - 9.01.03 - Jean II
  47. * o Add common spy support : iw_handler_set_spy(), wireless_spy_update()
  48. * o Add enhanced spy support : iw_handler_set_thrspy() and event.
  49. * o Add WIRELESS_EXT version display in /proc/net/wireless
  50. *
  51. * v6 - 18.06.04 - Jean II
  52. * o Change get_spydata() method for added safety
  53. * o Remove spy #ifdef, they are always on -> cleaner code
  54. * o Allow any size GET request if user specifies length > max
  55. * and if request has IW_DESCR_FLAG_NOMAX flag or is SIOCGIWPRIV
  56. * o Start migrating get_wireless_stats to struct iw_handler_def
  57. * o Add wmb() in iw_handler_set_spy() for non-coherent archs/cpus
  58. * Based on patch from Pavel Roskin <proski@gnu.org> :
  59. * o Fix kernel data leak to user space in private handler handling
  60. *
  61. * v7 - 18.3.05 - Jean II
  62. * o Remove (struct iw_point *)->pointer from events and streams
  63. * o Remove spy_offset from struct iw_handler_def
  64. * o Start deprecating dev->get_wireless_stats, output a warning
  65. * o If IW_QUAL_DBM is set, show dBm values in /proc/net/wireless
  66. * o Don't lose INVALID/DBM flags when clearing UPDATED flags (iwstats)
  67. *
  68. * v8 - 17.02.06 - Jean II
  69. * o RtNetlink requests support (SET/GET)
  70. *
  71. * v8b - 03.08.06 - Herbert Xu
  72. * o Fix Wireless Event locking issues.
  73. *
  74. * v9 - 14.3.06 - Jean II
  75. * o Change length in ESSID and NICK to strlen() instead of strlen()+1
  76. * o Make standard_ioctl_num and standard_event_num unsigned
  77. * o Remove (struct net_device *)->get_wireless_stats()
  78. *
  79. * v10 - 16.3.07 - Jean II
  80. * o Prevent leaking of kernel space in stream on 64 bits.
  81. */
  82. /***************************** INCLUDES *****************************/
  83. #include <linux/module.h>
  84. #include <linux/types.h> /* off_t */
  85. #include <linux/netdevice.h> /* struct ifreq, dev_get_by_name() */
  86. #include <linux/proc_fs.h>
  87. #include <linux/rtnetlink.h> /* rtnetlink stuff */
  88. #include <linux/seq_file.h>
  89. #include <linux/init.h> /* for __init */
  90. #include <linux/if_arp.h> /* ARPHRD_ETHER */
  91. #include <linux/etherdevice.h> /* compare_ether_addr */
  92. #include <linux/interrupt.h>
  93. #include <net/net_namespace.h>
  94. #include <linux/wireless.h> /* Pretty obvious */
  95. #include <net/iw_handler.h> /* New driver API */
  96. #include <net/netlink.h>
  97. #include <net/wext.h>
  98. #include <asm/uaccess.h> /* copy_to_user() */
  99. /************************* GLOBAL VARIABLES *************************/
  100. /*
  101. * You should not use global variables, because of re-entrancy.
  102. * On our case, it's only const, so it's OK...
  103. */
  104. /*
  105. * Meta-data about all the standard Wireless Extension request we
  106. * know about.
  107. */
  108. static const struct iw_ioctl_description standard_ioctl[] = {
  109. [SIOCSIWCOMMIT - SIOCIWFIRST] = {
  110. .header_type = IW_HEADER_TYPE_NULL,
  111. },
  112. [SIOCGIWNAME - SIOCIWFIRST] = {
  113. .header_type = IW_HEADER_TYPE_CHAR,
  114. .flags = IW_DESCR_FLAG_DUMP,
  115. },
  116. [SIOCSIWNWID - SIOCIWFIRST] = {
  117. .header_type = IW_HEADER_TYPE_PARAM,
  118. .flags = IW_DESCR_FLAG_EVENT,
  119. },
  120. [SIOCGIWNWID - SIOCIWFIRST] = {
  121. .header_type = IW_HEADER_TYPE_PARAM,
  122. .flags = IW_DESCR_FLAG_DUMP,
  123. },
  124. [SIOCSIWFREQ - SIOCIWFIRST] = {
  125. .header_type = IW_HEADER_TYPE_FREQ,
  126. .flags = IW_DESCR_FLAG_EVENT,
  127. },
  128. [SIOCGIWFREQ - SIOCIWFIRST] = {
  129. .header_type = IW_HEADER_TYPE_FREQ,
  130. .flags = IW_DESCR_FLAG_DUMP,
  131. },
  132. [SIOCSIWMODE - SIOCIWFIRST] = {
  133. .header_type = IW_HEADER_TYPE_UINT,
  134. .flags = IW_DESCR_FLAG_EVENT,
  135. },
  136. [SIOCGIWMODE - SIOCIWFIRST] = {
  137. .header_type = IW_HEADER_TYPE_UINT,
  138. .flags = IW_DESCR_FLAG_DUMP,
  139. },
  140. [SIOCSIWSENS - SIOCIWFIRST] = {
  141. .header_type = IW_HEADER_TYPE_PARAM,
  142. },
  143. [SIOCGIWSENS - SIOCIWFIRST] = {
  144. .header_type = IW_HEADER_TYPE_PARAM,
  145. },
  146. [SIOCSIWRANGE - SIOCIWFIRST] = {
  147. .header_type = IW_HEADER_TYPE_NULL,
  148. },
  149. [SIOCGIWRANGE - SIOCIWFIRST] = {
  150. .header_type = IW_HEADER_TYPE_POINT,
  151. .token_size = 1,
  152. .max_tokens = sizeof(struct iw_range),
  153. .flags = IW_DESCR_FLAG_DUMP,
  154. },
  155. [SIOCSIWPRIV - SIOCIWFIRST] = {
  156. .header_type = IW_HEADER_TYPE_NULL,
  157. },
  158. [SIOCGIWPRIV - SIOCIWFIRST] = { /* (handled directly by us) */
  159. .header_type = IW_HEADER_TYPE_POINT,
  160. .token_size = sizeof(struct iw_priv_args),
  161. .max_tokens = 16,
  162. .flags = IW_DESCR_FLAG_NOMAX,
  163. },
  164. [SIOCSIWSTATS - SIOCIWFIRST] = {
  165. .header_type = IW_HEADER_TYPE_NULL,
  166. },
  167. [SIOCGIWSTATS - SIOCIWFIRST] = { /* (handled directly by us) */
  168. .header_type = IW_HEADER_TYPE_POINT,
  169. .token_size = 1,
  170. .max_tokens = sizeof(struct iw_statistics),
  171. .flags = IW_DESCR_FLAG_DUMP,
  172. },
  173. [SIOCSIWSPY - SIOCIWFIRST] = {
  174. .header_type = IW_HEADER_TYPE_POINT,
  175. .token_size = sizeof(struct sockaddr),
  176. .max_tokens = IW_MAX_SPY,
  177. },
  178. [SIOCGIWSPY - SIOCIWFIRST] = {
  179. .header_type = IW_HEADER_TYPE_POINT,
  180. .token_size = sizeof(struct sockaddr) +
  181. sizeof(struct iw_quality),
  182. .max_tokens = IW_MAX_SPY,
  183. },
  184. [SIOCSIWTHRSPY - SIOCIWFIRST] = {
  185. .header_type = IW_HEADER_TYPE_POINT,
  186. .token_size = sizeof(struct iw_thrspy),
  187. .min_tokens = 1,
  188. .max_tokens = 1,
  189. },
  190. [SIOCGIWTHRSPY - SIOCIWFIRST] = {
  191. .header_type = IW_HEADER_TYPE_POINT,
  192. .token_size = sizeof(struct iw_thrspy),
  193. .min_tokens = 1,
  194. .max_tokens = 1,
  195. },
  196. [SIOCSIWAP - SIOCIWFIRST] = {
  197. .header_type = IW_HEADER_TYPE_ADDR,
  198. },
  199. [SIOCGIWAP - SIOCIWFIRST] = {
  200. .header_type = IW_HEADER_TYPE_ADDR,
  201. .flags = IW_DESCR_FLAG_DUMP,
  202. },
  203. [SIOCSIWMLME - SIOCIWFIRST] = {
  204. .header_type = IW_HEADER_TYPE_POINT,
  205. .token_size = 1,
  206. .min_tokens = sizeof(struct iw_mlme),
  207. .max_tokens = sizeof(struct iw_mlme),
  208. },
  209. [SIOCGIWAPLIST - SIOCIWFIRST] = {
  210. .header_type = IW_HEADER_TYPE_POINT,
  211. .token_size = sizeof(struct sockaddr) +
  212. sizeof(struct iw_quality),
  213. .max_tokens = IW_MAX_AP,
  214. .flags = IW_DESCR_FLAG_NOMAX,
  215. },
  216. [SIOCSIWSCAN - SIOCIWFIRST] = {
  217. .header_type = IW_HEADER_TYPE_POINT,
  218. .token_size = 1,
  219. .min_tokens = 0,
  220. .max_tokens = sizeof(struct iw_scan_req),
  221. },
  222. [SIOCGIWSCAN - SIOCIWFIRST] = {
  223. .header_type = IW_HEADER_TYPE_POINT,
  224. .token_size = 1,
  225. .max_tokens = IW_SCAN_MAX_DATA,
  226. .flags = IW_DESCR_FLAG_NOMAX,
  227. },
  228. [SIOCSIWESSID - SIOCIWFIRST] = {
  229. .header_type = IW_HEADER_TYPE_POINT,
  230. .token_size = 1,
  231. .max_tokens = IW_ESSID_MAX_SIZE,
  232. .flags = IW_DESCR_FLAG_EVENT,
  233. },
  234. [SIOCGIWESSID - SIOCIWFIRST] = {
  235. .header_type = IW_HEADER_TYPE_POINT,
  236. .token_size = 1,
  237. .max_tokens = IW_ESSID_MAX_SIZE,
  238. .flags = IW_DESCR_FLAG_DUMP,
  239. },
  240. [SIOCSIWNICKN - SIOCIWFIRST] = {
  241. .header_type = IW_HEADER_TYPE_POINT,
  242. .token_size = 1,
  243. .max_tokens = IW_ESSID_MAX_SIZE,
  244. },
  245. [SIOCGIWNICKN - SIOCIWFIRST] = {
  246. .header_type = IW_HEADER_TYPE_POINT,
  247. .token_size = 1,
  248. .max_tokens = IW_ESSID_MAX_SIZE,
  249. },
  250. [SIOCSIWRATE - SIOCIWFIRST] = {
  251. .header_type = IW_HEADER_TYPE_PARAM,
  252. },
  253. [SIOCGIWRATE - SIOCIWFIRST] = {
  254. .header_type = IW_HEADER_TYPE_PARAM,
  255. },
  256. [SIOCSIWRTS - SIOCIWFIRST] = {
  257. .header_type = IW_HEADER_TYPE_PARAM,
  258. },
  259. [SIOCGIWRTS - SIOCIWFIRST] = {
  260. .header_type = IW_HEADER_TYPE_PARAM,
  261. },
  262. [SIOCSIWFRAG - SIOCIWFIRST] = {
  263. .header_type = IW_HEADER_TYPE_PARAM,
  264. },
  265. [SIOCGIWFRAG - SIOCIWFIRST] = {
  266. .header_type = IW_HEADER_TYPE_PARAM,
  267. },
  268. [SIOCSIWTXPOW - SIOCIWFIRST] = {
  269. .header_type = IW_HEADER_TYPE_PARAM,
  270. },
  271. [SIOCGIWTXPOW - SIOCIWFIRST] = {
  272. .header_type = IW_HEADER_TYPE_PARAM,
  273. },
  274. [SIOCSIWRETRY - SIOCIWFIRST] = {
  275. .header_type = IW_HEADER_TYPE_PARAM,
  276. },
  277. [SIOCGIWRETRY - SIOCIWFIRST] = {
  278. .header_type = IW_HEADER_TYPE_PARAM,
  279. },
  280. [SIOCSIWENCODE - SIOCIWFIRST] = {
  281. .header_type = IW_HEADER_TYPE_POINT,
  282. .token_size = 1,
  283. .max_tokens = IW_ENCODING_TOKEN_MAX,
  284. .flags = IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT,
  285. },
  286. [SIOCGIWENCODE - SIOCIWFIRST] = {
  287. .header_type = IW_HEADER_TYPE_POINT,
  288. .token_size = 1,
  289. .max_tokens = IW_ENCODING_TOKEN_MAX,
  290. .flags = IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT,
  291. },
  292. [SIOCSIWPOWER - SIOCIWFIRST] = {
  293. .header_type = IW_HEADER_TYPE_PARAM,
  294. },
  295. [SIOCGIWPOWER - SIOCIWFIRST] = {
  296. .header_type = IW_HEADER_TYPE_PARAM,
  297. },
  298. [SIOCSIWGENIE - SIOCIWFIRST] = {
  299. .header_type = IW_HEADER_TYPE_POINT,
  300. .token_size = 1,
  301. .max_tokens = IW_GENERIC_IE_MAX,
  302. },
  303. [SIOCGIWGENIE - SIOCIWFIRST] = {
  304. .header_type = IW_HEADER_TYPE_POINT,
  305. .token_size = 1,
  306. .max_tokens = IW_GENERIC_IE_MAX,
  307. },
  308. [SIOCSIWAUTH - SIOCIWFIRST] = {
  309. .header_type = IW_HEADER_TYPE_PARAM,
  310. },
  311. [SIOCGIWAUTH - SIOCIWFIRST] = {
  312. .header_type = IW_HEADER_TYPE_PARAM,
  313. },
  314. [SIOCSIWENCODEEXT - SIOCIWFIRST] = {
  315. .header_type = IW_HEADER_TYPE_POINT,
  316. .token_size = 1,
  317. .min_tokens = sizeof(struct iw_encode_ext),
  318. .max_tokens = sizeof(struct iw_encode_ext) +
  319. IW_ENCODING_TOKEN_MAX,
  320. },
  321. [SIOCGIWENCODEEXT - SIOCIWFIRST] = {
  322. .header_type = IW_HEADER_TYPE_POINT,
  323. .token_size = 1,
  324. .min_tokens = sizeof(struct iw_encode_ext),
  325. .max_tokens = sizeof(struct iw_encode_ext) +
  326. IW_ENCODING_TOKEN_MAX,
  327. },
  328. [SIOCSIWPMKSA - SIOCIWFIRST] = {
  329. .header_type = IW_HEADER_TYPE_POINT,
  330. .token_size = 1,
  331. .min_tokens = sizeof(struct iw_pmksa),
  332. .max_tokens = sizeof(struct iw_pmksa),
  333. },
  334. };
  335. static const unsigned standard_ioctl_num = ARRAY_SIZE(standard_ioctl);
  336. /*
  337. * Meta-data about all the additional standard Wireless Extension events
  338. * we know about.
  339. */
  340. static const struct iw_ioctl_description standard_event[] = {
  341. [IWEVTXDROP - IWEVFIRST] = {
  342. .header_type = IW_HEADER_TYPE_ADDR,
  343. },
  344. [IWEVQUAL - IWEVFIRST] = {
  345. .header_type = IW_HEADER_TYPE_QUAL,
  346. },
  347. [IWEVCUSTOM - IWEVFIRST] = {
  348. .header_type = IW_HEADER_TYPE_POINT,
  349. .token_size = 1,
  350. .max_tokens = IW_CUSTOM_MAX,
  351. },
  352. [IWEVREGISTERED - IWEVFIRST] = {
  353. .header_type = IW_HEADER_TYPE_ADDR,
  354. },
  355. [IWEVEXPIRED - IWEVFIRST] = {
  356. .header_type = IW_HEADER_TYPE_ADDR,
  357. },
  358. [IWEVGENIE - IWEVFIRST] = {
  359. .header_type = IW_HEADER_TYPE_POINT,
  360. .token_size = 1,
  361. .max_tokens = IW_GENERIC_IE_MAX,
  362. },
  363. [IWEVMICHAELMICFAILURE - IWEVFIRST] = {
  364. .header_type = IW_HEADER_TYPE_POINT,
  365. .token_size = 1,
  366. .max_tokens = sizeof(struct iw_michaelmicfailure),
  367. },
  368. [IWEVASSOCREQIE - IWEVFIRST] = {
  369. .header_type = IW_HEADER_TYPE_POINT,
  370. .token_size = 1,
  371. .max_tokens = IW_GENERIC_IE_MAX,
  372. },
  373. [IWEVASSOCRESPIE - IWEVFIRST] = {
  374. .header_type = IW_HEADER_TYPE_POINT,
  375. .token_size = 1,
  376. .max_tokens = IW_GENERIC_IE_MAX,
  377. },
  378. [IWEVPMKIDCAND - IWEVFIRST] = {
  379. .header_type = IW_HEADER_TYPE_POINT,
  380. .token_size = 1,
  381. .max_tokens = sizeof(struct iw_pmkid_cand),
  382. },
  383. };
  384. static const unsigned standard_event_num = ARRAY_SIZE(standard_event);
  385. /* Size (in bytes) of the various private data types */
  386. static const char iw_priv_type_size[] = {
  387. 0, /* IW_PRIV_TYPE_NONE */
  388. 1, /* IW_PRIV_TYPE_BYTE */
  389. 1, /* IW_PRIV_TYPE_CHAR */
  390. 0, /* Not defined */
  391. sizeof(__u32), /* IW_PRIV_TYPE_INT */
  392. sizeof(struct iw_freq), /* IW_PRIV_TYPE_FLOAT */
  393. sizeof(struct sockaddr), /* IW_PRIV_TYPE_ADDR */
  394. 0, /* Not defined */
  395. };
  396. /* Size (in bytes) of various events */
  397. static const int event_type_size[] = {
  398. IW_EV_LCP_LEN, /* IW_HEADER_TYPE_NULL */
  399. 0,
  400. IW_EV_CHAR_LEN, /* IW_HEADER_TYPE_CHAR */
  401. 0,
  402. IW_EV_UINT_LEN, /* IW_HEADER_TYPE_UINT */
  403. IW_EV_FREQ_LEN, /* IW_HEADER_TYPE_FREQ */
  404. IW_EV_ADDR_LEN, /* IW_HEADER_TYPE_ADDR */
  405. 0,
  406. IW_EV_POINT_LEN, /* Without variable payload */
  407. IW_EV_PARAM_LEN, /* IW_HEADER_TYPE_PARAM */
  408. IW_EV_QUAL_LEN, /* IW_HEADER_TYPE_QUAL */
  409. };
  410. /************************ COMMON SUBROUTINES ************************/
  411. /*
  412. * Stuff that may be used in various place or doesn't fit in one
  413. * of the section below.
  414. */
  415. /* ---------------------------------------------------------------- */
  416. /*
  417. * Return the driver handler associated with a specific Wireless Extension.
  418. */
  419. static iw_handler get_handler(struct net_device *dev, unsigned int cmd)
  420. {
  421. /* Don't "optimise" the following variable, it will crash */
  422. unsigned int index; /* *MUST* be unsigned */
  423. /* Check if we have some wireless handlers defined */
  424. if (dev->wireless_handlers == NULL)
  425. return NULL;
  426. /* Try as a standard command */
  427. index = cmd - SIOCIWFIRST;
  428. if (index < dev->wireless_handlers->num_standard)
  429. return dev->wireless_handlers->standard[index];
  430. /* Try as a private command */
  431. index = cmd - SIOCIWFIRSTPRIV;
  432. if (index < dev->wireless_handlers->num_private)
  433. return dev->wireless_handlers->private[index];
  434. /* Not found */
  435. return NULL;
  436. }
  437. /* ---------------------------------------------------------------- */
  438. /*
  439. * Get statistics out of the driver
  440. */
  441. static struct iw_statistics *get_wireless_stats(struct net_device *dev)
  442. {
  443. /* New location */
  444. if ((dev->wireless_handlers != NULL) &&
  445. (dev->wireless_handlers->get_wireless_stats != NULL))
  446. return dev->wireless_handlers->get_wireless_stats(dev);
  447. /* Not found */
  448. return NULL;
  449. }
  450. /* ---------------------------------------------------------------- */
  451. /*
  452. * Call the commit handler in the driver
  453. * (if exist and if conditions are right)
  454. *
  455. * Note : our current commit strategy is currently pretty dumb,
  456. * but we will be able to improve on that...
  457. * The goal is to try to agreagate as many changes as possible
  458. * before doing the commit. Drivers that will define a commit handler
  459. * are usually those that need a reset after changing parameters, so
  460. * we want to minimise the number of reset.
  461. * A cool idea is to use a timer : at each "set" command, we re-set the
  462. * timer, when the timer eventually fires, we call the driver.
  463. * Hopefully, more on that later.
  464. *
  465. * Also, I'm waiting to see how many people will complain about the
  466. * netif_running(dev) test. I'm open on that one...
  467. * Hopefully, the driver will remember to do a commit in "open()" ;-)
  468. */
  469. static int call_commit_handler(struct net_device *dev)
  470. {
  471. if ((netif_running(dev)) &&
  472. (dev->wireless_handlers->standard[0] != NULL))
  473. /* Call the commit handler on the driver */
  474. return dev->wireless_handlers->standard[0](dev, NULL,
  475. NULL, NULL);
  476. else
  477. return 0; /* Command completed successfully */
  478. }
  479. /* ---------------------------------------------------------------- */
  480. /*
  481. * Calculate size of private arguments
  482. */
  483. static int get_priv_size(__u16 args)
  484. {
  485. int num = args & IW_PRIV_SIZE_MASK;
  486. int type = (args & IW_PRIV_TYPE_MASK) >> 12;
  487. return num * iw_priv_type_size[type];
  488. }
  489. /* ---------------------------------------------------------------- */
  490. /*
  491. * Re-calculate the size of private arguments
  492. */
  493. static int adjust_priv_size(__u16 args, struct iw_point *iwp)
  494. {
  495. int num = iwp->length;
  496. int max = args & IW_PRIV_SIZE_MASK;
  497. int type = (args & IW_PRIV_TYPE_MASK) >> 12;
  498. /* Make sure the driver doesn't goof up */
  499. if (max < num)
  500. num = max;
  501. return num * iw_priv_type_size[type];
  502. }
  503. /* ---------------------------------------------------------------- */
  504. /*
  505. * Standard Wireless Handler : get wireless stats
  506. * Allow programatic access to /proc/net/wireless even if /proc
  507. * doesn't exist... Also more efficient...
  508. */
  509. static int iw_handler_get_iwstats(struct net_device * dev,
  510. struct iw_request_info * info,
  511. union iwreq_data * wrqu,
  512. char * extra)
  513. {
  514. /* Get stats from the driver */
  515. struct iw_statistics *stats;
  516. stats = get_wireless_stats(dev);
  517. if (stats) {
  518. /* Copy statistics to extra */
  519. memcpy(extra, stats, sizeof(struct iw_statistics));
  520. wrqu->data.length = sizeof(struct iw_statistics);
  521. /* Check if we need to clear the updated flag */
  522. if (wrqu->data.flags != 0)
  523. stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
  524. return 0;
  525. } else
  526. return -EOPNOTSUPP;
  527. }
  528. /* ---------------------------------------------------------------- */
  529. /*
  530. * Standard Wireless Handler : get iwpriv definitions
  531. * Export the driver private handler definition
  532. * They will be picked up by tools like iwpriv...
  533. */
  534. static int iw_handler_get_private(struct net_device * dev,
  535. struct iw_request_info * info,
  536. union iwreq_data * wrqu,
  537. char * extra)
  538. {
  539. /* Check if the driver has something to export */
  540. if ((dev->wireless_handlers->num_private_args == 0) ||
  541. (dev->wireless_handlers->private_args == NULL))
  542. return -EOPNOTSUPP;
  543. /* Check if there is enough buffer up there */
  544. if (wrqu->data.length < dev->wireless_handlers->num_private_args) {
  545. /* User space can't know in advance how large the buffer
  546. * needs to be. Give it a hint, so that we can support
  547. * any size buffer we want somewhat efficiently... */
  548. wrqu->data.length = dev->wireless_handlers->num_private_args;
  549. return -E2BIG;
  550. }
  551. /* Set the number of available ioctls. */
  552. wrqu->data.length = dev->wireless_handlers->num_private_args;
  553. /* Copy structure to the user buffer. */
  554. memcpy(extra, dev->wireless_handlers->private_args,
  555. sizeof(struct iw_priv_args) * wrqu->data.length);
  556. return 0;
  557. }
  558. /******************** /proc/net/wireless SUPPORT ********************/
  559. /*
  560. * The /proc/net/wireless file is a human readable user-space interface
  561. * exporting various wireless specific statistics from the wireless devices.
  562. * This is the most popular part of the Wireless Extensions ;-)
  563. *
  564. * This interface is a pure clone of /proc/net/dev (in net/core/dev.c).
  565. * The content of the file is basically the content of "struct iw_statistics".
  566. */
  567. #ifdef CONFIG_PROC_FS
  568. /* ---------------------------------------------------------------- */
  569. /*
  570. * Print one entry (line) of /proc/net/wireless
  571. */
  572. static void wireless_seq_printf_stats(struct seq_file *seq,
  573. struct net_device *dev)
  574. {
  575. /* Get stats from the driver */
  576. struct iw_statistics *stats = get_wireless_stats(dev);
  577. if (stats) {
  578. seq_printf(seq, "%6s: %04x %3d%c %3d%c %3d%c %6d %6d %6d "
  579. "%6d %6d %6d\n",
  580. dev->name, stats->status, stats->qual.qual,
  581. stats->qual.updated & IW_QUAL_QUAL_UPDATED
  582. ? '.' : ' ',
  583. ((__s32) stats->qual.level) -
  584. ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
  585. stats->qual.updated & IW_QUAL_LEVEL_UPDATED
  586. ? '.' : ' ',
  587. ((__s32) stats->qual.noise) -
  588. ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
  589. stats->qual.updated & IW_QUAL_NOISE_UPDATED
  590. ? '.' : ' ',
  591. stats->discard.nwid, stats->discard.code,
  592. stats->discard.fragment, stats->discard.retries,
  593. stats->discard.misc, stats->miss.beacon);
  594. stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
  595. }
  596. }
  597. /* ---------------------------------------------------------------- */
  598. /*
  599. * Print info for /proc/net/wireless (print all entries)
  600. */
  601. static int wireless_seq_show(struct seq_file *seq, void *v)
  602. {
  603. if (v == SEQ_START_TOKEN)
  604. seq_printf(seq, "Inter-| sta-| Quality | Discarded "
  605. "packets | Missed | WE\n"
  606. " face | tus | link level noise | nwid "
  607. "crypt frag retry misc | beacon | %d\n",
  608. WIRELESS_EXT);
  609. else
  610. wireless_seq_printf_stats(seq, v);
  611. return 0;
  612. }
  613. static void *wireless_dev_seq_start(struct seq_file *seq, loff_t *pos)
  614. {
  615. rtnl_lock();
  616. return dev_seq_start(seq, pos);
  617. }
  618. static void wireless_dev_seq_stop(struct seq_file *seq, void *v)
  619. {
  620. dev_seq_stop(seq, v);
  621. rtnl_unlock();
  622. }
  623. static const struct seq_operations wireless_seq_ops = {
  624. .start = wireless_dev_seq_start,
  625. .next = dev_seq_next,
  626. .stop = wireless_dev_seq_stop,
  627. .show = wireless_seq_show,
  628. };
  629. static int seq_open_wireless(struct inode *inode, struct file *file)
  630. {
  631. return seq_open_net(inode, file, &wireless_seq_ops,
  632. sizeof(struct seq_net_private));
  633. }
  634. static const struct file_operations wireless_seq_fops = {
  635. .owner = THIS_MODULE,
  636. .open = seq_open_wireless,
  637. .read = seq_read,
  638. .llseek = seq_lseek,
  639. .release = seq_release_net,
  640. };
  641. int wext_proc_init(struct net *net)
  642. {
  643. /* Create /proc/net/wireless entry */
  644. if (!proc_net_fops_create(net, "wireless", S_IRUGO, &wireless_seq_fops))
  645. return -ENOMEM;
  646. return 0;
  647. }
  648. void wext_proc_exit(struct net *net)
  649. {
  650. proc_net_remove(net, "wireless");
  651. }
  652. #endif /* CONFIG_PROC_FS */
  653. /************************** IOCTL SUPPORT **************************/
  654. /*
  655. * The original user space API to configure all those Wireless Extensions
  656. * is through IOCTLs.
  657. * In there, we check if we need to call the new driver API (iw_handler)
  658. * or just call the driver ioctl handler.
  659. */
  660. /* ---------------------------------------------------------------- */
  661. static int ioctl_standard_iw_point(struct iw_point *iwp, unsigned int cmd,
  662. const struct iw_ioctl_description *descr,
  663. iw_handler handler, struct net_device *dev,
  664. struct iw_request_info *info)
  665. {
  666. int err, extra_size, user_length = 0, essid_compat = 0;
  667. char *extra;
  668. /* Calculate space needed by arguments. Always allocate
  669. * for max space.
  670. */
  671. extra_size = descr->max_tokens * descr->token_size;
  672. /* Check need for ESSID compatibility for WE < 21 */
  673. switch (cmd) {
  674. case SIOCSIWESSID:
  675. case SIOCGIWESSID:
  676. case SIOCSIWNICKN:
  677. case SIOCGIWNICKN:
  678. if (iwp->length == descr->max_tokens + 1)
  679. essid_compat = 1;
  680. else if (IW_IS_SET(cmd) && (iwp->length != 0)) {
  681. char essid[IW_ESSID_MAX_SIZE + 1];
  682. err = copy_from_user(essid, iwp->pointer,
  683. iwp->length *
  684. descr->token_size);
  685. if (err)
  686. return -EFAULT;
  687. if (essid[iwp->length - 1] == '\0')
  688. essid_compat = 1;
  689. }
  690. break;
  691. default:
  692. break;
  693. }
  694. iwp->length -= essid_compat;
  695. /* Check what user space is giving us */
  696. if (IW_IS_SET(cmd)) {
  697. /* Check NULL pointer */
  698. if (!iwp->pointer && iwp->length != 0)
  699. return -EFAULT;
  700. /* Check if number of token fits within bounds */
  701. if (iwp->length > descr->max_tokens)
  702. return -E2BIG;
  703. if (iwp->length < descr->min_tokens)
  704. return -EINVAL;
  705. } else {
  706. /* Check NULL pointer */
  707. if (!iwp->pointer)
  708. return -EFAULT;
  709. /* Save user space buffer size for checking */
  710. user_length = iwp->length;
  711. /* Don't check if user_length > max to allow forward
  712. * compatibility. The test user_length < min is
  713. * implied by the test at the end.
  714. */
  715. /* Support for very large requests */
  716. if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
  717. (user_length > descr->max_tokens)) {
  718. /* Allow userspace to GET more than max so
  719. * we can support any size GET requests.
  720. * There is still a limit : -ENOMEM.
  721. */
  722. extra_size = user_length * descr->token_size;
  723. /* Note : user_length is originally a __u16,
  724. * and token_size is controlled by us,
  725. * so extra_size won't get negative and
  726. * won't overflow...
  727. */
  728. }
  729. }
  730. /* kzalloc() ensures NULL-termination for essid_compat. */
  731. extra = kzalloc(extra_size, GFP_KERNEL);
  732. if (!extra)
  733. return -ENOMEM;
  734. /* If it is a SET, get all the extra data in here */
  735. if (IW_IS_SET(cmd) && (iwp->length != 0)) {
  736. if (copy_from_user(extra, iwp->pointer,
  737. iwp->length *
  738. descr->token_size)) {
  739. err = -EFAULT;
  740. goto out;
  741. }
  742. }
  743. err = handler(dev, info, (union iwreq_data *) iwp, extra);
  744. iwp->length += essid_compat;
  745. /* If we have something to return to the user */
  746. if (!err && IW_IS_GET(cmd)) {
  747. /* Check if there is enough buffer up there */
  748. if (user_length < iwp->length) {
  749. err = -E2BIG;
  750. goto out;
  751. }
  752. if (copy_to_user(iwp->pointer, extra,
  753. iwp->length *
  754. descr->token_size)) {
  755. err = -EFAULT;
  756. goto out;
  757. }
  758. }
  759. /* Generate an event to notify listeners of the change */
  760. if ((descr->flags & IW_DESCR_FLAG_EVENT) && err == -EIWCOMMIT) {
  761. union iwreq_data *data = (union iwreq_data *) iwp;
  762. if (descr->flags & IW_DESCR_FLAG_RESTRICT)
  763. /* If the event is restricted, don't
  764. * export the payload.
  765. */
  766. wireless_send_event(dev, cmd, data, NULL);
  767. else
  768. wireless_send_event(dev, cmd, data, extra);
  769. }
  770. out:
  771. kfree(extra);
  772. return err;
  773. }
  774. /*
  775. * Wrapper to call a standard Wireless Extension handler.
  776. * We do various checks and also take care of moving data between
  777. * user space and kernel space.
  778. */
  779. static int ioctl_standard_call(struct net_device * dev,
  780. struct iwreq *iwr,
  781. unsigned int cmd,
  782. struct iw_request_info *info,
  783. iw_handler handler)
  784. {
  785. const struct iw_ioctl_description * descr;
  786. int ret = -EINVAL;
  787. /* Get the description of the IOCTL */
  788. if ((cmd - SIOCIWFIRST) >= standard_ioctl_num)
  789. return -EOPNOTSUPP;
  790. descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
  791. /* Check if we have a pointer to user space data or not */
  792. if (descr->header_type != IW_HEADER_TYPE_POINT) {
  793. /* No extra arguments. Trivial to handle */
  794. ret = handler(dev, info, &(iwr->u), NULL);
  795. /* Generate an event to notify listeners of the change */
  796. if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
  797. ((ret == 0) || (ret == -EIWCOMMIT)))
  798. wireless_send_event(dev, cmd, &(iwr->u), NULL);
  799. } else {
  800. ret = ioctl_standard_iw_point(&iwr->u.data, cmd, descr,
  801. handler, dev, info);
  802. }
  803. /* Call commit handler if needed and defined */
  804. if (ret == -EIWCOMMIT)
  805. ret = call_commit_handler(dev);
  806. /* Here, we will generate the appropriate event if needed */
  807. return ret;
  808. }
  809. /* ---------------------------------------------------------------- */
  810. /*
  811. * Wrapper to call a private Wireless Extension handler.
  812. * We do various checks and also take care of moving data between
  813. * user space and kernel space.
  814. * It's not as nice and slimline as the standard wrapper. The cause
  815. * is struct iw_priv_args, which was not really designed for the
  816. * job we are going here.
  817. *
  818. * IMPORTANT : This function prevent to set and get data on the same
  819. * IOCTL and enforce the SET/GET convention. Not doing it would be
  820. * far too hairy...
  821. * If you need to set and get data at the same time, please don't use
  822. * a iw_handler but process it in your ioctl handler (i.e. use the
  823. * old driver API).
  824. */
  825. static int get_priv_descr_and_size(struct net_device *dev, unsigned int cmd,
  826. const struct iw_priv_args **descrp)
  827. {
  828. const struct iw_priv_args *descr;
  829. int i, extra_size;
  830. descr = NULL;
  831. for (i = 0; i < dev->wireless_handlers->num_private_args; i++) {
  832. if (cmd == dev->wireless_handlers->private_args[i].cmd) {
  833. descr = &dev->wireless_handlers->private_args[i];
  834. break;
  835. }
  836. }
  837. extra_size = 0;
  838. if (descr) {
  839. if (IW_IS_SET(cmd)) {
  840. int offset = 0; /* For sub-ioctls */
  841. /* Check for sub-ioctl handler */
  842. if (descr->name[0] == '\0')
  843. /* Reserve one int for sub-ioctl index */
  844. offset = sizeof(__u32);
  845. /* Size of set arguments */
  846. extra_size = get_priv_size(descr->set_args);
  847. /* Does it fits in iwr ? */
  848. if ((descr->set_args & IW_PRIV_SIZE_FIXED) &&
  849. ((extra_size + offset) <= IFNAMSIZ))
  850. extra_size = 0;
  851. } else {
  852. /* Size of get arguments */
  853. extra_size = get_priv_size(descr->get_args);
  854. /* Does it fits in iwr ? */
  855. if ((descr->get_args & IW_PRIV_SIZE_FIXED) &&
  856. (extra_size <= IFNAMSIZ))
  857. extra_size = 0;
  858. }
  859. }
  860. *descrp = descr;
  861. return extra_size;
  862. }
  863. static int ioctl_private_iw_point(struct iw_point *iwp, unsigned int cmd,
  864. const struct iw_priv_args *descr,
  865. iw_handler handler, struct net_device *dev,
  866. struct iw_request_info *info, int extra_size)
  867. {
  868. char *extra;
  869. int err;
  870. /* Check what user space is giving us */
  871. if (IW_IS_SET(cmd)) {
  872. if (!iwp->pointer && iwp->length != 0)
  873. return -EFAULT;
  874. if (iwp->length > (descr->set_args & IW_PRIV_SIZE_MASK))
  875. return -E2BIG;
  876. } else if (!iwp->pointer)
  877. return -EFAULT;
  878. extra = kmalloc(extra_size, GFP_KERNEL);
  879. if (!extra)
  880. return -ENOMEM;
  881. /* If it is a SET, get all the extra data in here */
  882. if (IW_IS_SET(cmd) && (iwp->length != 0)) {
  883. if (copy_from_user(extra, iwp->pointer, extra_size)) {
  884. err = -EFAULT;
  885. goto out;
  886. }
  887. }
  888. /* Call the handler */
  889. err = handler(dev, info, (union iwreq_data *) iwp, extra);
  890. /* If we have something to return to the user */
  891. if (!err && IW_IS_GET(cmd)) {
  892. /* Adjust for the actual length if it's variable,
  893. * avoid leaking kernel bits outside.
  894. */
  895. if (!(descr->get_args & IW_PRIV_SIZE_FIXED))
  896. extra_size = adjust_priv_size(descr->get_args, iwp);
  897. if (copy_to_user(iwp->pointer, extra, extra_size))
  898. err = -EFAULT;
  899. }
  900. out:
  901. kfree(extra);
  902. return err;
  903. }
  904. static int ioctl_private_call(struct net_device *dev, struct iwreq *iwr,
  905. unsigned int cmd, struct iw_request_info *info,
  906. iw_handler handler)
  907. {
  908. int extra_size = 0, ret = -EINVAL;
  909. const struct iw_priv_args *descr;
  910. extra_size = get_priv_descr_and_size(dev, cmd, &descr);
  911. /* Check if we have a pointer to user space data or not. */
  912. if (extra_size == 0) {
  913. /* No extra arguments. Trivial to handle */
  914. ret = handler(dev, info, &(iwr->u), (char *) &(iwr->u));
  915. } else {
  916. ret = ioctl_private_iw_point(&iwr->u.data, cmd, descr,
  917. handler, dev, info, extra_size);
  918. }
  919. /* Call commit handler if needed and defined */
  920. if (ret == -EIWCOMMIT)
  921. ret = call_commit_handler(dev);
  922. return ret;
  923. }
  924. /* ---------------------------------------------------------------- */
  925. typedef int (*wext_ioctl_func)(struct net_device *, struct iwreq *,
  926. unsigned int, struct iw_request_info *,
  927. iw_handler);
  928. /*
  929. * Main IOCTl dispatcher.
  930. * Check the type of IOCTL and call the appropriate wrapper...
  931. */
  932. static int wireless_process_ioctl(struct net *net, struct ifreq *ifr,
  933. unsigned int cmd,
  934. struct iw_request_info *info,
  935. wext_ioctl_func standard,
  936. wext_ioctl_func private)
  937. {
  938. struct iwreq *iwr = (struct iwreq *) ifr;
  939. struct net_device *dev;
  940. iw_handler handler;
  941. /* Permissions are already checked in dev_ioctl() before calling us.
  942. * The copy_to/from_user() of ifr is also dealt with in there */
  943. /* Make sure the device exist */
  944. if ((dev = __dev_get_by_name(net, ifr->ifr_name)) == NULL)
  945. return -ENODEV;
  946. /* A bunch of special cases, then the generic case...
  947. * Note that 'cmd' is already filtered in dev_ioctl() with
  948. * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
  949. if (cmd == SIOCGIWSTATS)
  950. return standard(dev, iwr, cmd, info,
  951. &iw_handler_get_iwstats);
  952. if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
  953. return standard(dev, iwr, cmd, info,
  954. &iw_handler_get_private);
  955. /* Basic check */
  956. if (!netif_device_present(dev))
  957. return -ENODEV;
  958. /* New driver API : try to find the handler */
  959. handler = get_handler(dev, cmd);
  960. if (handler) {
  961. /* Standard and private are not the same */
  962. if (cmd < SIOCIWFIRSTPRIV)
  963. return standard(dev, iwr, cmd, info, handler);
  964. else
  965. return private(dev, iwr, cmd, info, handler);
  966. }
  967. /* Old driver API : call driver ioctl handler */
  968. if (dev->netdev_ops->ndo_do_ioctl)
  969. return dev->netdev_ops->ndo_do_ioctl(dev, ifr, cmd);
  970. return -EOPNOTSUPP;
  971. }
  972. /* If command is `set a parameter', or `get the encoding parameters',
  973. * check if the user has the right to do it.
  974. */
  975. static int wext_permission_check(unsigned int cmd)
  976. {
  977. if ((IW_IS_SET(cmd) || cmd == SIOCGIWENCODE || cmd == SIOCGIWENCODEEXT)
  978. && !capable(CAP_NET_ADMIN))
  979. return -EPERM;
  980. return 0;
  981. }
  982. /* entry point from dev ioctl */
  983. static int wext_ioctl_dispatch(struct net *net, struct ifreq *ifr,
  984. unsigned int cmd, struct iw_request_info *info,
  985. wext_ioctl_func standard,
  986. wext_ioctl_func private)
  987. {
  988. int ret = wext_permission_check(cmd);
  989. if (ret)
  990. return ret;
  991. dev_load(net, ifr->ifr_name);
  992. rtnl_lock();
  993. ret = wireless_process_ioctl(net, ifr, cmd, info, standard, private);
  994. rtnl_unlock();
  995. return ret;
  996. }
  997. int wext_handle_ioctl(struct net *net, struct ifreq *ifr, unsigned int cmd,
  998. void __user *arg)
  999. {
  1000. struct iw_request_info info = { .cmd = cmd, .flags = 0 };
  1001. int ret;
  1002. ret = wext_ioctl_dispatch(net, ifr, cmd, &info,
  1003. ioctl_standard_call,
  1004. ioctl_private_call);
  1005. if (ret >= 0 &&
  1006. IW_IS_GET(cmd) &&
  1007. copy_to_user(arg, ifr, sizeof(struct iwreq)))
  1008. return -EFAULT;
  1009. return ret;
  1010. }
  1011. #ifdef CONFIG_COMPAT
  1012. static int compat_standard_call(struct net_device *dev,
  1013. struct iwreq *iwr,
  1014. unsigned int cmd,
  1015. struct iw_request_info *info,
  1016. iw_handler handler)
  1017. {
  1018. const struct iw_ioctl_description *descr;
  1019. struct compat_iw_point *iwp_compat;
  1020. struct iw_point iwp;
  1021. int err;
  1022. descr = standard_ioctl + (cmd - SIOCIWFIRST);
  1023. if (descr->header_type != IW_HEADER_TYPE_POINT)
  1024. return ioctl_standard_call(dev, iwr, cmd, info, handler);
  1025. iwp_compat = (struct compat_iw_point *) &iwr->u.data;
  1026. iwp.pointer = compat_ptr(iwp_compat->pointer);
  1027. iwp.length = iwp_compat->length;
  1028. iwp.flags = iwp_compat->flags;
  1029. err = ioctl_standard_iw_point(&iwp, cmd, descr, handler, dev, info);
  1030. iwp_compat->pointer = ptr_to_compat(iwp.pointer);
  1031. iwp_compat->length = iwp.length;
  1032. iwp_compat->flags = iwp.flags;
  1033. return err;
  1034. }
  1035. static int compat_private_call(struct net_device *dev, struct iwreq *iwr,
  1036. unsigned int cmd, struct iw_request_info *info,
  1037. iw_handler handler)
  1038. {
  1039. const struct iw_priv_args *descr;
  1040. int ret, extra_size;
  1041. extra_size = get_priv_descr_and_size(dev, cmd, &descr);
  1042. /* Check if we have a pointer to user space data or not. */
  1043. if (extra_size == 0) {
  1044. /* No extra arguments. Trivial to handle */
  1045. ret = handler(dev, info, &(iwr->u), (char *) &(iwr->u));
  1046. } else {
  1047. struct compat_iw_point *iwp_compat;
  1048. struct iw_point iwp;
  1049. iwp_compat = (struct compat_iw_point *) &iwr->u.data;
  1050. iwp.pointer = compat_ptr(iwp_compat->pointer);
  1051. iwp.length = iwp_compat->length;
  1052. iwp.flags = iwp_compat->flags;
  1053. ret = ioctl_private_iw_point(&iwp, cmd, descr,
  1054. handler, dev, info, extra_size);
  1055. iwp_compat->pointer = ptr_to_compat(iwp.pointer);
  1056. iwp_compat->length = iwp.length;
  1057. iwp_compat->flags = iwp.flags;
  1058. }
  1059. /* Call commit handler if needed and defined */
  1060. if (ret == -EIWCOMMIT)
  1061. ret = call_commit_handler(dev);
  1062. return ret;
  1063. }
  1064. int compat_wext_handle_ioctl(struct net *net, unsigned int cmd,
  1065. unsigned long arg)
  1066. {
  1067. void __user *argp = (void __user *)arg;
  1068. struct iw_request_info info;
  1069. struct iwreq iwr;
  1070. char *colon;
  1071. int ret;
  1072. if (copy_from_user(&iwr, argp, sizeof(struct iwreq)))
  1073. return -EFAULT;
  1074. iwr.ifr_name[IFNAMSIZ-1] = 0;
  1075. colon = strchr(iwr.ifr_name, ':');
  1076. if (colon)
  1077. *colon = 0;
  1078. info.cmd = cmd;
  1079. info.flags = IW_REQUEST_FLAG_COMPAT;
  1080. ret = wext_ioctl_dispatch(net, (struct ifreq *) &iwr, cmd, &info,
  1081. compat_standard_call,
  1082. compat_private_call);
  1083. if (ret >= 0 &&
  1084. IW_IS_GET(cmd) &&
  1085. copy_to_user(argp, &iwr, sizeof(struct iwreq)))
  1086. return -EFAULT;
  1087. return ret;
  1088. }
  1089. #endif
  1090. /************************* EVENT PROCESSING *************************/
  1091. /*
  1092. * Process events generated by the wireless layer or the driver.
  1093. * Most often, the event will be propagated through rtnetlink
  1094. */
  1095. /* ---------------------------------------------------------------- */
  1096. /*
  1097. * Locking...
  1098. * ----------
  1099. *
  1100. * Thanks to Herbert Xu <herbert@gondor.apana.org.au> for fixing
  1101. * the locking issue in here and implementing this code !
  1102. *
  1103. * The issue : wireless_send_event() is often called in interrupt context,
  1104. * while the Netlink layer can never be called in interrupt context.
  1105. * The fully formed RtNetlink events are queued, and then a tasklet is run
  1106. * to feed those to Netlink.
  1107. * The skb_queue is interrupt safe, and its lock is not held while calling
  1108. * Netlink, so there is no possibility of dealock.
  1109. * Jean II
  1110. */
  1111. static struct sk_buff_head wireless_nlevent_queue;
  1112. static int __init wireless_nlevent_init(void)
  1113. {
  1114. skb_queue_head_init(&wireless_nlevent_queue);
  1115. return 0;
  1116. }
  1117. subsys_initcall(wireless_nlevent_init);
  1118. static void wireless_nlevent_process(unsigned long data)
  1119. {
  1120. struct sk_buff *skb;
  1121. while ((skb = skb_dequeue(&wireless_nlevent_queue)))
  1122. rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
  1123. }
  1124. static DECLARE_TASKLET(wireless_nlevent_tasklet, wireless_nlevent_process, 0);
  1125. /* ---------------------------------------------------------------- */
  1126. /*
  1127. * Fill a rtnetlink message with our event data.
  1128. * Note that we propage only the specified event and don't dump the
  1129. * current wireless config. Dumping the wireless config is far too
  1130. * expensive (for each parameter, the driver need to query the hardware).
  1131. */
  1132. static int rtnetlink_fill_iwinfo(struct sk_buff *skb, struct net_device *dev,
  1133. int type, char *event, int event_len)
  1134. {
  1135. struct ifinfomsg *r;
  1136. struct nlmsghdr *nlh;
  1137. nlh = nlmsg_put(skb, 0, 0, type, sizeof(*r), 0);
  1138. if (nlh == NULL)
  1139. return -EMSGSIZE;
  1140. r = nlmsg_data(nlh);
  1141. r->ifi_family = AF_UNSPEC;
  1142. r->__ifi_pad = 0;
  1143. r->ifi_type = dev->type;
  1144. r->ifi_index = dev->ifindex;
  1145. r->ifi_flags = dev_get_flags(dev);
  1146. r->ifi_change = 0; /* Wireless changes don't affect those flags */
  1147. NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
  1148. /* Add the wireless events in the netlink packet */
  1149. NLA_PUT(skb, IFLA_WIRELESS, event_len, event);
  1150. return nlmsg_end(skb, nlh);
  1151. nla_put_failure:
  1152. nlmsg_cancel(skb, nlh);
  1153. return -EMSGSIZE;
  1154. }
  1155. /* ---------------------------------------------------------------- */
  1156. /*
  1157. * Create and broadcast and send it on the standard rtnetlink socket
  1158. * This is a pure clone rtmsg_ifinfo() in net/core/rtnetlink.c
  1159. * Andrzej Krzysztofowicz mandated that I used a IFLA_XXX field
  1160. * within a RTM_NEWLINK event.
  1161. */
  1162. static void rtmsg_iwinfo(struct net_device *dev, char *event, int event_len)
  1163. {
  1164. struct sk_buff *skb;
  1165. int err;
  1166. if (!net_eq(dev_net(dev), &init_net))
  1167. return;
  1168. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1169. if (!skb)
  1170. return;
  1171. err = rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK, event, event_len);
  1172. if (err < 0) {
  1173. WARN_ON(err == -EMSGSIZE);
  1174. kfree_skb(skb);
  1175. return;
  1176. }
  1177. NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
  1178. skb_queue_tail(&wireless_nlevent_queue, skb);
  1179. tasklet_schedule(&wireless_nlevent_tasklet);
  1180. }
  1181. /* ---------------------------------------------------------------- */
  1182. /*
  1183. * Main event dispatcher. Called from other parts and drivers.
  1184. * Send the event on the appropriate channels.
  1185. * May be called from interrupt context.
  1186. */
  1187. void wireless_send_event(struct net_device * dev,
  1188. unsigned int cmd,
  1189. union iwreq_data * wrqu,
  1190. char * extra)
  1191. {
  1192. const struct iw_ioctl_description * descr = NULL;
  1193. int extra_len = 0;
  1194. struct iw_event *event; /* Mallocated whole event */
  1195. int event_len; /* Its size */
  1196. int hdr_len; /* Size of the event header */
  1197. int wrqu_off = 0; /* Offset in wrqu */
  1198. /* Don't "optimise" the following variable, it will crash */
  1199. unsigned cmd_index; /* *MUST* be unsigned */
  1200. /* Get the description of the Event */
  1201. if (cmd <= SIOCIWLAST) {
  1202. cmd_index = cmd - SIOCIWFIRST;
  1203. if (cmd_index < standard_ioctl_num)
  1204. descr = &(standard_ioctl[cmd_index]);
  1205. } else {
  1206. cmd_index = cmd - IWEVFIRST;
  1207. if (cmd_index < standard_event_num)
  1208. descr = &(standard_event[cmd_index]);
  1209. }
  1210. /* Don't accept unknown events */
  1211. if (descr == NULL) {
  1212. /* Note : we don't return an error to the driver, because
  1213. * the driver would not know what to do about it. It can't
  1214. * return an error to the user, because the event is not
  1215. * initiated by a user request.
  1216. * The best the driver could do is to log an error message.
  1217. * We will do it ourselves instead...
  1218. */
  1219. printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
  1220. dev->name, cmd);
  1221. return;
  1222. }
  1223. /* Check extra parameters and set extra_len */
  1224. if (descr->header_type == IW_HEADER_TYPE_POINT) {
  1225. /* Check if number of token fits within bounds */
  1226. if (wrqu->data.length > descr->max_tokens) {
  1227. printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length);
  1228. return;
  1229. }
  1230. if (wrqu->data.length < descr->min_tokens) {
  1231. printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length);
  1232. return;
  1233. }
  1234. /* Calculate extra_len - extra is NULL for restricted events */
  1235. if (extra != NULL)
  1236. extra_len = wrqu->data.length * descr->token_size;
  1237. /* Always at an offset in wrqu */
  1238. wrqu_off = IW_EV_POINT_OFF;
  1239. }
  1240. /* Total length of the event */
  1241. hdr_len = event_type_size[descr->header_type];
  1242. event_len = hdr_len + extra_len;
  1243. /* Create temporary buffer to hold the event */
  1244. event = kmalloc(event_len, GFP_ATOMIC);
  1245. if (event == NULL)
  1246. return;
  1247. /* Fill event */
  1248. event->len = event_len;
  1249. event->cmd = cmd;
  1250. memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
  1251. if (extra)
  1252. memcpy(((char *) event) + hdr_len, extra, extra_len);
  1253. /* Send via the RtNetlink event channel */
  1254. rtmsg_iwinfo(dev, (char *) event, event_len);
  1255. /* Cleanup */
  1256. kfree(event);
  1257. return; /* Always success, I guess ;-) */
  1258. }
  1259. EXPORT_SYMBOL(wireless_send_event);
  1260. /********************** ENHANCED IWSPY SUPPORT **********************/
  1261. /*
  1262. * In the old days, the driver was handling spy support all by itself.
  1263. * Now, the driver can delegate this task to Wireless Extensions.
  1264. * It needs to use those standard spy iw_handler in struct iw_handler_def,
  1265. * push data to us via wireless_spy_update() and include struct iw_spy_data
  1266. * in its private part (and export it in net_device->wireless_data->spy_data).
  1267. * One of the main advantage of centralising spy support here is that
  1268. * it becomes much easier to improve and extend it without having to touch
  1269. * the drivers. One example is the addition of the Spy-Threshold events.
  1270. */
  1271. /* ---------------------------------------------------------------- */
  1272. /*
  1273. * Return the pointer to the spy data in the driver.
  1274. * Because this is called on the Rx path via wireless_spy_update(),
  1275. * we want it to be efficient...
  1276. */
  1277. static inline struct iw_spy_data *get_spydata(struct net_device *dev)
  1278. {
  1279. /* This is the new way */
  1280. if (dev->wireless_data)
  1281. return dev->wireless_data->spy_data;
  1282. return NULL;
  1283. }
  1284. /*------------------------------------------------------------------*/
  1285. /*
  1286. * Standard Wireless Handler : set Spy List
  1287. */
  1288. int iw_handler_set_spy(struct net_device * dev,
  1289. struct iw_request_info * info,
  1290. union iwreq_data * wrqu,
  1291. char * extra)
  1292. {
  1293. struct iw_spy_data * spydata = get_spydata(dev);
  1294. struct sockaddr * address = (struct sockaddr *) extra;
  1295. /* Make sure driver is not buggy or using the old API */
  1296. if (!spydata)
  1297. return -EOPNOTSUPP;
  1298. /* Disable spy collection while we copy the addresses.
  1299. * While we copy addresses, any call to wireless_spy_update()
  1300. * will NOP. This is OK, as anyway the addresses are changing. */
  1301. spydata->spy_number = 0;
  1302. /* We want to operate without locking, because wireless_spy_update()
  1303. * most likely will happen in the interrupt handler, and therefore
  1304. * have its own locking constraints and needs performance.
  1305. * The rtnl_lock() make sure we don't race with the other iw_handlers.
  1306. * This make sure wireless_spy_update() "see" that the spy list
  1307. * is temporarily disabled. */
  1308. smp_wmb();
  1309. /* Are there are addresses to copy? */
  1310. if (wrqu->data.length > 0) {
  1311. int i;
  1312. /* Copy addresses */
  1313. for (i = 0; i < wrqu->data.length; i++)
  1314. memcpy(spydata->spy_address[i], address[i].sa_data,
  1315. ETH_ALEN);
  1316. /* Reset stats */
  1317. memset(spydata->spy_stat, 0,
  1318. sizeof(struct iw_quality) * IW_MAX_SPY);
  1319. }
  1320. /* Make sure above is updated before re-enabling */
  1321. smp_wmb();
  1322. /* Enable addresses */
  1323. spydata->spy_number = wrqu->data.length;
  1324. return 0;
  1325. }
  1326. EXPORT_SYMBOL(iw_handler_set_spy);
  1327. /*------------------------------------------------------------------*/
  1328. /*
  1329. * Standard Wireless Handler : get Spy List
  1330. */
  1331. int iw_handler_get_spy(struct net_device * dev,
  1332. struct iw_request_info * info,
  1333. union iwreq_data * wrqu,
  1334. char * extra)
  1335. {
  1336. struct iw_spy_data * spydata = get_spydata(dev);
  1337. struct sockaddr * address = (struct sockaddr *) extra;
  1338. int i;
  1339. /* Make sure driver is not buggy or using the old API */
  1340. if (!spydata)
  1341. return -EOPNOTSUPP;
  1342. wrqu->data.length = spydata->spy_number;
  1343. /* Copy addresses. */
  1344. for (i = 0; i < spydata->spy_number; i++) {
  1345. memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
  1346. address[i].sa_family = AF_UNIX;
  1347. }
  1348. /* Copy stats to the user buffer (just after). */
  1349. if (spydata->spy_number > 0)
  1350. memcpy(extra + (sizeof(struct sockaddr) *spydata->spy_number),
  1351. spydata->spy_stat,
  1352. sizeof(struct iw_quality) * spydata->spy_number);
  1353. /* Reset updated flags. */
  1354. for (i = 0; i < spydata->spy_number; i++)
  1355. spydata->spy_stat[i].updated &= ~IW_QUAL_ALL_UPDATED;
  1356. return 0;
  1357. }
  1358. EXPORT_SYMBOL(iw_handler_get_spy);
  1359. /*------------------------------------------------------------------*/
  1360. /*
  1361. * Standard Wireless Handler : set spy threshold
  1362. */
  1363. int iw_handler_set_thrspy(struct net_device * dev,
  1364. struct iw_request_info *info,
  1365. union iwreq_data * wrqu,
  1366. char * extra)
  1367. {
  1368. struct iw_spy_data * spydata = get_spydata(dev);
  1369. struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
  1370. /* Make sure driver is not buggy or using the old API */
  1371. if (!spydata)
  1372. return -EOPNOTSUPP;
  1373. /* Just do it */
  1374. memcpy(&(spydata->spy_thr_low), &(threshold->low),
  1375. 2 * sizeof(struct iw_quality));
  1376. /* Clear flag */
  1377. memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
  1378. return 0;
  1379. }
  1380. EXPORT_SYMBOL(iw_handler_set_thrspy);
  1381. /*------------------------------------------------------------------*/
  1382. /*
  1383. * Standard Wireless Handler : get spy threshold
  1384. */
  1385. int iw_handler_get_thrspy(struct net_device * dev,
  1386. struct iw_request_info *info,
  1387. union iwreq_data * wrqu,
  1388. char * extra)
  1389. {
  1390. struct iw_spy_data * spydata = get_spydata(dev);
  1391. struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
  1392. /* Make sure driver is not buggy or using the old API */
  1393. if (!spydata)
  1394. return -EOPNOTSUPP;
  1395. /* Just do it */
  1396. memcpy(&(threshold->low), &(spydata->spy_thr_low),
  1397. 2 * sizeof(struct iw_quality));
  1398. return 0;
  1399. }
  1400. EXPORT_SYMBOL(iw_handler_get_thrspy);
  1401. /*------------------------------------------------------------------*/
  1402. /*
  1403. * Prepare and send a Spy Threshold event
  1404. */
  1405. static void iw_send_thrspy_event(struct net_device * dev,
  1406. struct iw_spy_data * spydata,
  1407. unsigned char * address,
  1408. struct iw_quality * wstats)
  1409. {
  1410. union iwreq_data wrqu;
  1411. struct iw_thrspy threshold;
  1412. /* Init */
  1413. wrqu.data.length = 1;
  1414. wrqu.data.flags = 0;
  1415. /* Copy address */
  1416. memcpy(threshold.addr.sa_data, address, ETH_ALEN);
  1417. threshold.addr.sa_family = ARPHRD_ETHER;
  1418. /* Copy stats */
  1419. memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality));
  1420. /* Copy also thresholds */
  1421. memcpy(&(threshold.low), &(spydata->spy_thr_low),
  1422. 2 * sizeof(struct iw_quality));
  1423. /* Send event to user space */
  1424. wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
  1425. }
  1426. /* ---------------------------------------------------------------- */
  1427. /*
  1428. * Call for the driver to update the spy data.
  1429. * For now, the spy data is a simple array. As the size of the array is
  1430. * small, this is good enough. If we wanted to support larger number of
  1431. * spy addresses, we should use something more efficient...
  1432. */
  1433. void wireless_spy_update(struct net_device * dev,
  1434. unsigned char * address,
  1435. struct iw_quality * wstats)
  1436. {
  1437. struct iw_spy_data * spydata = get_spydata(dev);
  1438. int i;
  1439. int match = -1;
  1440. /* Make sure driver is not buggy or using the old API */
  1441. if (!spydata)
  1442. return;
  1443. /* Update all records that match */
  1444. for (i = 0; i < spydata->spy_number; i++)
  1445. if (!compare_ether_addr(address, spydata->spy_address[i])) {
  1446. memcpy(&(spydata->spy_stat[i]), wstats,
  1447. sizeof(struct iw_quality));
  1448. match = i;
  1449. }
  1450. /* Generate an event if we cross the spy threshold.
  1451. * To avoid event storms, we have a simple hysteresis : we generate
  1452. * event only when we go under the low threshold or above the
  1453. * high threshold. */
  1454. if (match >= 0) {
  1455. if (spydata->spy_thr_under[match]) {
  1456. if (wstats->level > spydata->spy_thr_high.level) {
  1457. spydata->spy_thr_under[match] = 0;
  1458. iw_send_thrspy_event(dev, spydata,
  1459. address, wstats);
  1460. }
  1461. } else {
  1462. if (wstats->level < spydata->spy_thr_low.level) {
  1463. spydata->spy_thr_under[match] = 1;
  1464. iw_send_thrspy_event(dev, spydata,
  1465. address, wstats);
  1466. }
  1467. }
  1468. }
  1469. }
  1470. EXPORT_SYMBOL(wireless_spy_update);