wireless.c 46 KB

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