wireless.c 49 KB

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