wireless.c 46 KB

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