wireless.c 46 KB

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