wireless.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368
  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_RTNETLINK_DEBUG /* Debug RtNetlink API */
  101. #undef WE_EVENT_DEBUG /* Debug Event dispatcher */
  102. #undef WE_SPY_DEBUG /* Debug enhanced spy support */
  103. /* Options */
  104. //CONFIG_NET_WIRELESS_RTNETLINK /* Wireless requests over RtNetlink */
  105. #define WE_EVENT_RTNETLINK /* Propagate events using RtNetlink */
  106. #define WE_SET_EVENT /* Generate an event on some set commands */
  107. /************************* GLOBAL VARIABLES *************************/
  108. /*
  109. * You should not use global variables, because of re-entrancy.
  110. * On our case, it's only const, so it's OK...
  111. */
  112. /*
  113. * Meta-data about all the standard Wireless Extension request we
  114. * know about.
  115. */
  116. static const struct iw_ioctl_description standard_ioctl[] = {
  117. [SIOCSIWCOMMIT - SIOCIWFIRST] = {
  118. .header_type = IW_HEADER_TYPE_NULL,
  119. },
  120. [SIOCGIWNAME - SIOCIWFIRST] = {
  121. .header_type = IW_HEADER_TYPE_CHAR,
  122. .flags = IW_DESCR_FLAG_DUMP,
  123. },
  124. [SIOCSIWNWID - SIOCIWFIRST] = {
  125. .header_type = IW_HEADER_TYPE_PARAM,
  126. .flags = IW_DESCR_FLAG_EVENT,
  127. },
  128. [SIOCGIWNWID - SIOCIWFIRST] = {
  129. .header_type = IW_HEADER_TYPE_PARAM,
  130. .flags = IW_DESCR_FLAG_DUMP,
  131. },
  132. [SIOCSIWFREQ - SIOCIWFIRST] = {
  133. .header_type = IW_HEADER_TYPE_FREQ,
  134. .flags = IW_DESCR_FLAG_EVENT,
  135. },
  136. [SIOCGIWFREQ - SIOCIWFIRST] = {
  137. .header_type = IW_HEADER_TYPE_FREQ,
  138. .flags = IW_DESCR_FLAG_DUMP,
  139. },
  140. [SIOCSIWMODE - SIOCIWFIRST] = {
  141. .header_type = IW_HEADER_TYPE_UINT,
  142. .flags = IW_DESCR_FLAG_EVENT,
  143. },
  144. [SIOCGIWMODE - SIOCIWFIRST] = {
  145. .header_type = IW_HEADER_TYPE_UINT,
  146. .flags = IW_DESCR_FLAG_DUMP,
  147. },
  148. [SIOCSIWSENS - SIOCIWFIRST] = {
  149. .header_type = IW_HEADER_TYPE_PARAM,
  150. },
  151. [SIOCGIWSENS - SIOCIWFIRST] = {
  152. .header_type = IW_HEADER_TYPE_PARAM,
  153. },
  154. [SIOCSIWRANGE - SIOCIWFIRST] = {
  155. .header_type = IW_HEADER_TYPE_NULL,
  156. },
  157. [SIOCGIWRANGE - SIOCIWFIRST] = {
  158. .header_type = IW_HEADER_TYPE_POINT,
  159. .token_size = 1,
  160. .max_tokens = sizeof(struct iw_range),
  161. .flags = IW_DESCR_FLAG_DUMP,
  162. },
  163. [SIOCSIWPRIV - SIOCIWFIRST] = {
  164. .header_type = IW_HEADER_TYPE_NULL,
  165. },
  166. [SIOCGIWPRIV - SIOCIWFIRST] = { /* (handled directly by us) */
  167. .header_type = IW_HEADER_TYPE_POINT,
  168. .token_size = sizeof(struct iw_priv_args),
  169. .max_tokens = 16,
  170. .flags = IW_DESCR_FLAG_NOMAX,
  171. },
  172. [SIOCSIWSTATS - SIOCIWFIRST] = {
  173. .header_type = IW_HEADER_TYPE_NULL,
  174. },
  175. [SIOCGIWSTATS - SIOCIWFIRST] = { /* (handled directly by us) */
  176. .header_type = IW_HEADER_TYPE_POINT,
  177. .token_size = 1,
  178. .max_tokens = sizeof(struct iw_statistics),
  179. .flags = IW_DESCR_FLAG_DUMP,
  180. },
  181. [SIOCSIWSPY - SIOCIWFIRST] = {
  182. .header_type = IW_HEADER_TYPE_POINT,
  183. .token_size = sizeof(struct sockaddr),
  184. .max_tokens = IW_MAX_SPY,
  185. },
  186. [SIOCGIWSPY - SIOCIWFIRST] = {
  187. .header_type = IW_HEADER_TYPE_POINT,
  188. .token_size = sizeof(struct sockaddr) +
  189. sizeof(struct iw_quality),
  190. .max_tokens = IW_MAX_SPY,
  191. },
  192. [SIOCSIWTHRSPY - SIOCIWFIRST] = {
  193. .header_type = IW_HEADER_TYPE_POINT,
  194. .token_size = sizeof(struct iw_thrspy),
  195. .min_tokens = 1,
  196. .max_tokens = 1,
  197. },
  198. [SIOCGIWTHRSPY - SIOCIWFIRST] = {
  199. .header_type = IW_HEADER_TYPE_POINT,
  200. .token_size = sizeof(struct iw_thrspy),
  201. .min_tokens = 1,
  202. .max_tokens = 1,
  203. },
  204. [SIOCSIWAP - SIOCIWFIRST] = {
  205. .header_type = IW_HEADER_TYPE_ADDR,
  206. },
  207. [SIOCGIWAP - SIOCIWFIRST] = {
  208. .header_type = IW_HEADER_TYPE_ADDR,
  209. .flags = IW_DESCR_FLAG_DUMP,
  210. },
  211. [SIOCSIWMLME - SIOCIWFIRST] = {
  212. .header_type = IW_HEADER_TYPE_POINT,
  213. .token_size = 1,
  214. .min_tokens = sizeof(struct iw_mlme),
  215. .max_tokens = sizeof(struct iw_mlme),
  216. },
  217. [SIOCGIWAPLIST - SIOCIWFIRST] = {
  218. .header_type = IW_HEADER_TYPE_POINT,
  219. .token_size = sizeof(struct sockaddr) +
  220. sizeof(struct iw_quality),
  221. .max_tokens = IW_MAX_AP,
  222. .flags = IW_DESCR_FLAG_NOMAX,
  223. },
  224. [SIOCSIWSCAN - SIOCIWFIRST] = {
  225. .header_type = IW_HEADER_TYPE_POINT,
  226. .token_size = 1,
  227. .min_tokens = 0,
  228. .max_tokens = sizeof(struct iw_scan_req),
  229. },
  230. [SIOCGIWSCAN - SIOCIWFIRST] = {
  231. .header_type = IW_HEADER_TYPE_POINT,
  232. .token_size = 1,
  233. .max_tokens = IW_SCAN_MAX_DATA,
  234. .flags = IW_DESCR_FLAG_NOMAX,
  235. },
  236. [SIOCSIWESSID - SIOCIWFIRST] = {
  237. .header_type = IW_HEADER_TYPE_POINT,
  238. .token_size = 1,
  239. .max_tokens = IW_ESSID_MAX_SIZE,
  240. .flags = IW_DESCR_FLAG_EVENT,
  241. },
  242. [SIOCGIWESSID - SIOCIWFIRST] = {
  243. .header_type = IW_HEADER_TYPE_POINT,
  244. .token_size = 1,
  245. .max_tokens = IW_ESSID_MAX_SIZE,
  246. .flags = IW_DESCR_FLAG_DUMP,
  247. },
  248. [SIOCSIWNICKN - SIOCIWFIRST] = {
  249. .header_type = IW_HEADER_TYPE_POINT,
  250. .token_size = 1,
  251. .max_tokens = IW_ESSID_MAX_SIZE,
  252. },
  253. [SIOCGIWNICKN - SIOCIWFIRST] = {
  254. .header_type = IW_HEADER_TYPE_POINT,
  255. .token_size = 1,
  256. .max_tokens = IW_ESSID_MAX_SIZE,
  257. },
  258. [SIOCSIWRATE - SIOCIWFIRST] = {
  259. .header_type = IW_HEADER_TYPE_PARAM,
  260. },
  261. [SIOCGIWRATE - SIOCIWFIRST] = {
  262. .header_type = IW_HEADER_TYPE_PARAM,
  263. },
  264. [SIOCSIWRTS - SIOCIWFIRST] = {
  265. .header_type = IW_HEADER_TYPE_PARAM,
  266. },
  267. [SIOCGIWRTS - SIOCIWFIRST] = {
  268. .header_type = IW_HEADER_TYPE_PARAM,
  269. },
  270. [SIOCSIWFRAG - SIOCIWFIRST] = {
  271. .header_type = IW_HEADER_TYPE_PARAM,
  272. },
  273. [SIOCGIWFRAG - SIOCIWFIRST] = {
  274. .header_type = IW_HEADER_TYPE_PARAM,
  275. },
  276. [SIOCSIWTXPOW - SIOCIWFIRST] = {
  277. .header_type = IW_HEADER_TYPE_PARAM,
  278. },
  279. [SIOCGIWTXPOW - SIOCIWFIRST] = {
  280. .header_type = IW_HEADER_TYPE_PARAM,
  281. },
  282. [SIOCSIWRETRY - SIOCIWFIRST] = {
  283. .header_type = IW_HEADER_TYPE_PARAM,
  284. },
  285. [SIOCGIWRETRY - SIOCIWFIRST] = {
  286. .header_type = IW_HEADER_TYPE_PARAM,
  287. },
  288. [SIOCSIWENCODE - SIOCIWFIRST] = {
  289. .header_type = IW_HEADER_TYPE_POINT,
  290. .token_size = 1,
  291. .max_tokens = IW_ENCODING_TOKEN_MAX,
  292. .flags = IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT,
  293. },
  294. [SIOCGIWENCODE - SIOCIWFIRST] = {
  295. .header_type = IW_HEADER_TYPE_POINT,
  296. .token_size = 1,
  297. .max_tokens = IW_ENCODING_TOKEN_MAX,
  298. .flags = IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT,
  299. },
  300. [SIOCSIWPOWER - SIOCIWFIRST] = {
  301. .header_type = IW_HEADER_TYPE_PARAM,
  302. },
  303. [SIOCGIWPOWER - SIOCIWFIRST] = {
  304. .header_type = IW_HEADER_TYPE_PARAM,
  305. },
  306. [SIOCSIWGENIE - SIOCIWFIRST] = {
  307. .header_type = IW_HEADER_TYPE_POINT,
  308. .token_size = 1,
  309. .max_tokens = IW_GENERIC_IE_MAX,
  310. },
  311. [SIOCGIWGENIE - SIOCIWFIRST] = {
  312. .header_type = IW_HEADER_TYPE_POINT,
  313. .token_size = 1,
  314. .max_tokens = IW_GENERIC_IE_MAX,
  315. },
  316. [SIOCSIWAUTH - SIOCIWFIRST] = {
  317. .header_type = IW_HEADER_TYPE_PARAM,
  318. },
  319. [SIOCGIWAUTH - SIOCIWFIRST] = {
  320. .header_type = IW_HEADER_TYPE_PARAM,
  321. },
  322. [SIOCSIWENCODEEXT - SIOCIWFIRST] = {
  323. .header_type = IW_HEADER_TYPE_POINT,
  324. .token_size = 1,
  325. .min_tokens = sizeof(struct iw_encode_ext),
  326. .max_tokens = sizeof(struct iw_encode_ext) +
  327. IW_ENCODING_TOKEN_MAX,
  328. },
  329. [SIOCGIWENCODEEXT - SIOCIWFIRST] = {
  330. .header_type = IW_HEADER_TYPE_POINT,
  331. .token_size = 1,
  332. .min_tokens = sizeof(struct iw_encode_ext),
  333. .max_tokens = sizeof(struct iw_encode_ext) +
  334. IW_ENCODING_TOKEN_MAX,
  335. },
  336. [SIOCSIWPMKSA - SIOCIWFIRST] = {
  337. .header_type = IW_HEADER_TYPE_POINT,
  338. .token_size = 1,
  339. .min_tokens = sizeof(struct iw_pmksa),
  340. .max_tokens = sizeof(struct iw_pmksa),
  341. },
  342. };
  343. static const unsigned standard_ioctl_num = ARRAY_SIZE(standard_ioctl);
  344. /*
  345. * Meta-data about all the additional standard Wireless Extension events
  346. * we know about.
  347. */
  348. static const struct iw_ioctl_description standard_event[] = {
  349. [IWEVTXDROP - IWEVFIRST] = {
  350. .header_type = IW_HEADER_TYPE_ADDR,
  351. },
  352. [IWEVQUAL - IWEVFIRST] = {
  353. .header_type = IW_HEADER_TYPE_QUAL,
  354. },
  355. [IWEVCUSTOM - IWEVFIRST] = {
  356. .header_type = IW_HEADER_TYPE_POINT,
  357. .token_size = 1,
  358. .max_tokens = IW_CUSTOM_MAX,
  359. },
  360. [IWEVREGISTERED - IWEVFIRST] = {
  361. .header_type = IW_HEADER_TYPE_ADDR,
  362. },
  363. [IWEVEXPIRED - IWEVFIRST] = {
  364. .header_type = IW_HEADER_TYPE_ADDR,
  365. },
  366. [IWEVGENIE - IWEVFIRST] = {
  367. .header_type = IW_HEADER_TYPE_POINT,
  368. .token_size = 1,
  369. .max_tokens = IW_GENERIC_IE_MAX,
  370. },
  371. [IWEVMICHAELMICFAILURE - IWEVFIRST] = {
  372. .header_type = IW_HEADER_TYPE_POINT,
  373. .token_size = 1,
  374. .max_tokens = sizeof(struct iw_michaelmicfailure),
  375. },
  376. [IWEVASSOCREQIE - IWEVFIRST] = {
  377. .header_type = IW_HEADER_TYPE_POINT,
  378. .token_size = 1,
  379. .max_tokens = IW_GENERIC_IE_MAX,
  380. },
  381. [IWEVASSOCRESPIE - IWEVFIRST] = {
  382. .header_type = IW_HEADER_TYPE_POINT,
  383. .token_size = 1,
  384. .max_tokens = IW_GENERIC_IE_MAX,
  385. },
  386. [IWEVPMKIDCAND - IWEVFIRST] = {
  387. .header_type = IW_HEADER_TYPE_POINT,
  388. .token_size = 1,
  389. .max_tokens = sizeof(struct iw_pmkid_cand),
  390. },
  391. };
  392. static const unsigned standard_event_num = ARRAY_SIZE(standard_event);
  393. /* Size (in bytes) of the various private data types */
  394. static const char iw_priv_type_size[] = {
  395. 0, /* IW_PRIV_TYPE_NONE */
  396. 1, /* IW_PRIV_TYPE_BYTE */
  397. 1, /* IW_PRIV_TYPE_CHAR */
  398. 0, /* Not defined */
  399. sizeof(__u32), /* IW_PRIV_TYPE_INT */
  400. sizeof(struct iw_freq), /* IW_PRIV_TYPE_FLOAT */
  401. sizeof(struct sockaddr), /* IW_PRIV_TYPE_ADDR */
  402. 0, /* Not defined */
  403. };
  404. /* Size (in bytes) of various events */
  405. static const int event_type_size[] = {
  406. IW_EV_LCP_LEN, /* IW_HEADER_TYPE_NULL */
  407. 0,
  408. IW_EV_CHAR_LEN, /* IW_HEADER_TYPE_CHAR */
  409. 0,
  410. IW_EV_UINT_LEN, /* IW_HEADER_TYPE_UINT */
  411. IW_EV_FREQ_LEN, /* IW_HEADER_TYPE_FREQ */
  412. IW_EV_ADDR_LEN, /* IW_HEADER_TYPE_ADDR */
  413. 0,
  414. IW_EV_POINT_LEN, /* Without variable payload */
  415. IW_EV_PARAM_LEN, /* IW_HEADER_TYPE_PARAM */
  416. IW_EV_QUAL_LEN, /* IW_HEADER_TYPE_QUAL */
  417. };
  418. /* Size (in bytes) of various events, as packed */
  419. static const int event_type_pk_size[] = {
  420. IW_EV_LCP_PK_LEN, /* IW_HEADER_TYPE_NULL */
  421. 0,
  422. IW_EV_CHAR_PK_LEN, /* IW_HEADER_TYPE_CHAR */
  423. 0,
  424. IW_EV_UINT_PK_LEN, /* IW_HEADER_TYPE_UINT */
  425. IW_EV_FREQ_PK_LEN, /* IW_HEADER_TYPE_FREQ */
  426. IW_EV_ADDR_PK_LEN, /* IW_HEADER_TYPE_ADDR */
  427. 0,
  428. IW_EV_POINT_PK_LEN, /* Without variable payload */
  429. IW_EV_PARAM_PK_LEN, /* IW_HEADER_TYPE_PARAM */
  430. IW_EV_QUAL_PK_LEN, /* IW_HEADER_TYPE_QUAL */
  431. };
  432. /************************ COMMON SUBROUTINES ************************/
  433. /*
  434. * Stuff that may be used in various place or doesn't fit in one
  435. * of the section below.
  436. */
  437. /* ---------------------------------------------------------------- */
  438. /*
  439. * Return the driver handler associated with a specific Wireless Extension.
  440. * Called from various place, so make sure it remains efficient.
  441. */
  442. static inline iw_handler get_handler(struct net_device *dev,
  443. unsigned int cmd)
  444. {
  445. /* Don't "optimise" the following variable, it will crash */
  446. unsigned int index; /* *MUST* be unsigned */
  447. /* Check if we have some wireless handlers defined */
  448. if (dev->wireless_handlers == NULL)
  449. return NULL;
  450. /* Try as a standard command */
  451. index = cmd - SIOCIWFIRST;
  452. if (index < dev->wireless_handlers->num_standard)
  453. return dev->wireless_handlers->standard[index];
  454. /* Try as a private command */
  455. index = cmd - SIOCIWFIRSTPRIV;
  456. if (index < dev->wireless_handlers->num_private)
  457. return dev->wireless_handlers->private[index];
  458. /* Not found */
  459. return NULL;
  460. }
  461. /* ---------------------------------------------------------------- */
  462. /*
  463. * Get statistics out of the driver
  464. */
  465. static inline struct iw_statistics *get_wireless_stats(struct net_device *dev)
  466. {
  467. /* New location */
  468. if ((dev->wireless_handlers != NULL) &&
  469. (dev->wireless_handlers->get_wireless_stats != NULL))
  470. return dev->wireless_handlers->get_wireless_stats(dev);
  471. /* Not found */
  472. return (struct iw_statistics *) NULL;
  473. }
  474. /* ---------------------------------------------------------------- */
  475. /*
  476. * Call the commit handler in the driver
  477. * (if exist and if conditions are right)
  478. *
  479. * Note : our current commit strategy is currently pretty dumb,
  480. * but we will be able to improve on that...
  481. * The goal is to try to agreagate as many changes as possible
  482. * before doing the commit. Drivers that will define a commit handler
  483. * are usually those that need a reset after changing parameters, so
  484. * we want to minimise the number of reset.
  485. * A cool idea is to use a timer : at each "set" command, we re-set the
  486. * timer, when the timer eventually fires, we call the driver.
  487. * Hopefully, more on that later.
  488. *
  489. * Also, I'm waiting to see how many people will complain about the
  490. * netif_running(dev) test. I'm open on that one...
  491. * Hopefully, the driver will remember to do a commit in "open()" ;-)
  492. */
  493. static inline int call_commit_handler(struct net_device * dev)
  494. {
  495. if ((netif_running(dev)) &&
  496. (dev->wireless_handlers->standard[0] != NULL)) {
  497. /* Call the commit handler on the driver */
  498. return dev->wireless_handlers->standard[0](dev, NULL,
  499. NULL, NULL);
  500. } else
  501. return 0; /* Command completed successfully */
  502. }
  503. /* ---------------------------------------------------------------- */
  504. /*
  505. * Calculate size of private arguments
  506. */
  507. static inline int get_priv_size(__u16 args)
  508. {
  509. int num = args & IW_PRIV_SIZE_MASK;
  510. int type = (args & IW_PRIV_TYPE_MASK) >> 12;
  511. return num * iw_priv_type_size[type];
  512. }
  513. /* ---------------------------------------------------------------- */
  514. /*
  515. * Re-calculate the size of private arguments
  516. */
  517. static inline int adjust_priv_size(__u16 args,
  518. union iwreq_data * wrqu)
  519. {
  520. int num = wrqu->data.length;
  521. int max = args & IW_PRIV_SIZE_MASK;
  522. int type = (args & IW_PRIV_TYPE_MASK) >> 12;
  523. /* Make sure the driver doesn't goof up */
  524. if (max < num)
  525. num = max;
  526. return num * iw_priv_type_size[type];
  527. }
  528. /* ---------------------------------------------------------------- */
  529. /*
  530. * Standard Wireless Handler : get wireless stats
  531. * Allow programatic access to /proc/net/wireless even if /proc
  532. * doesn't exist... Also more efficient...
  533. */
  534. static int iw_handler_get_iwstats(struct net_device * dev,
  535. struct iw_request_info * info,
  536. union iwreq_data * wrqu,
  537. char * extra)
  538. {
  539. /* Get stats from the driver */
  540. struct iw_statistics *stats;
  541. stats = get_wireless_stats(dev);
  542. if (stats != (struct iw_statistics *) NULL) {
  543. /* Copy statistics to extra */
  544. memcpy(extra, stats, sizeof(struct iw_statistics));
  545. wrqu->data.length = sizeof(struct iw_statistics);
  546. /* Check if we need to clear the updated flag */
  547. if (wrqu->data.flags != 0)
  548. stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
  549. return 0;
  550. } else
  551. return -EOPNOTSUPP;
  552. }
  553. /* ---------------------------------------------------------------- */
  554. /*
  555. * Standard Wireless Handler : get iwpriv definitions
  556. * Export the driver private handler definition
  557. * They will be picked up by tools like iwpriv...
  558. */
  559. static int iw_handler_get_private(struct net_device * dev,
  560. struct iw_request_info * info,
  561. union iwreq_data * wrqu,
  562. char * extra)
  563. {
  564. /* Check if the driver has something to export */
  565. if ((dev->wireless_handlers->num_private_args == 0) ||
  566. (dev->wireless_handlers->private_args == NULL))
  567. return -EOPNOTSUPP;
  568. /* Check if there is enough buffer up there */
  569. if (wrqu->data.length < dev->wireless_handlers->num_private_args) {
  570. /* User space can't know in advance how large the buffer
  571. * needs to be. Give it a hint, so that we can support
  572. * any size buffer we want somewhat efficiently... */
  573. wrqu->data.length = dev->wireless_handlers->num_private_args;
  574. return -E2BIG;
  575. }
  576. /* Set the number of available ioctls. */
  577. wrqu->data.length = dev->wireless_handlers->num_private_args;
  578. /* Copy structure to the user buffer. */
  579. memcpy(extra, dev->wireless_handlers->private_args,
  580. sizeof(struct iw_priv_args) * wrqu->data.length);
  581. return 0;
  582. }
  583. /******************** /proc/net/wireless SUPPORT ********************/
  584. /*
  585. * The /proc/net/wireless file is a human readable user-space interface
  586. * exporting various wireless specific statistics from the wireless devices.
  587. * This is the most popular part of the Wireless Extensions ;-)
  588. *
  589. * This interface is a pure clone of /proc/net/dev (in net/core/dev.c).
  590. * The content of the file is basically the content of "struct iw_statistics".
  591. */
  592. #ifdef CONFIG_PROC_FS
  593. /* ---------------------------------------------------------------- */
  594. /*
  595. * Print one entry (line) of /proc/net/wireless
  596. */
  597. static __inline__ void wireless_seq_printf_stats(struct seq_file *seq,
  598. struct net_device *dev)
  599. {
  600. /* Get stats from the driver */
  601. struct iw_statistics *stats = get_wireless_stats(dev);
  602. if (stats) {
  603. seq_printf(seq, "%6s: %04x %3d%c %3d%c %3d%c %6d %6d %6d "
  604. "%6d %6d %6d\n",
  605. dev->name, stats->status, stats->qual.qual,
  606. stats->qual.updated & IW_QUAL_QUAL_UPDATED
  607. ? '.' : ' ',
  608. ((__s32) stats->qual.level) -
  609. ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
  610. stats->qual.updated & IW_QUAL_LEVEL_UPDATED
  611. ? '.' : ' ',
  612. ((__s32) stats->qual.noise) -
  613. ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
  614. stats->qual.updated & IW_QUAL_NOISE_UPDATED
  615. ? '.' : ' ',
  616. stats->discard.nwid, stats->discard.code,
  617. stats->discard.fragment, stats->discard.retries,
  618. stats->discard.misc, stats->miss.beacon);
  619. stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
  620. }
  621. }
  622. /* ---------------------------------------------------------------- */
  623. /*
  624. * Print info for /proc/net/wireless (print all entries)
  625. */
  626. static int wireless_seq_show(struct seq_file *seq, void *v)
  627. {
  628. if (v == SEQ_START_TOKEN)
  629. seq_printf(seq, "Inter-| sta-| Quality | Discarded "
  630. "packets | Missed | WE\n"
  631. " face | tus | link level noise | nwid "
  632. "crypt frag retry misc | beacon | %d\n",
  633. WIRELESS_EXT);
  634. else
  635. wireless_seq_printf_stats(seq, v);
  636. return 0;
  637. }
  638. static struct seq_operations wireless_seq_ops = {
  639. .start = dev_seq_start,
  640. .next = dev_seq_next,
  641. .stop = dev_seq_stop,
  642. .show = wireless_seq_show,
  643. };
  644. static int wireless_seq_open(struct inode *inode, struct file *file)
  645. {
  646. return seq_open(file, &wireless_seq_ops);
  647. }
  648. static const struct file_operations wireless_seq_fops = {
  649. .owner = THIS_MODULE,
  650. .open = wireless_seq_open,
  651. .read = seq_read,
  652. .llseek = seq_lseek,
  653. .release = seq_release,
  654. };
  655. int __init wireless_proc_init(void)
  656. {
  657. /* Create /proc/net/wireless entry */
  658. if (!proc_net_fops_create("wireless", S_IRUGO, &wireless_seq_fops))
  659. return -ENOMEM;
  660. return 0;
  661. }
  662. #endif /* CONFIG_PROC_FS */
  663. /************************** IOCTL SUPPORT **************************/
  664. /*
  665. * The original user space API to configure all those Wireless Extensions
  666. * is through IOCTLs.
  667. * In there, we check if we need to call the new driver API (iw_handler)
  668. * or just call the driver ioctl handler.
  669. */
  670. /* ---------------------------------------------------------------- */
  671. /*
  672. * Wrapper to call a standard Wireless Extension handler.
  673. * We do various checks and also take care of moving data between
  674. * user space and kernel space.
  675. */
  676. static int ioctl_standard_call(struct net_device * dev,
  677. struct ifreq * ifr,
  678. unsigned int cmd,
  679. iw_handler handler)
  680. {
  681. struct iwreq * iwr = (struct iwreq *) ifr;
  682. const struct iw_ioctl_description * descr;
  683. struct iw_request_info info;
  684. int ret = -EINVAL;
  685. /* Get the description of the IOCTL */
  686. if ((cmd - SIOCIWFIRST) >= standard_ioctl_num)
  687. return -EOPNOTSUPP;
  688. descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
  689. #ifdef WE_IOCTL_DEBUG
  690. printk(KERN_DEBUG "%s (WE) : Found standard handler for 0x%04X\n",
  691. ifr->ifr_name, cmd);
  692. 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);
  693. #endif /* WE_IOCTL_DEBUG */
  694. /* Prepare the call */
  695. info.cmd = cmd;
  696. info.flags = 0;
  697. /* Check if we have a pointer to user space data or not */
  698. if (descr->header_type != IW_HEADER_TYPE_POINT) {
  699. /* No extra arguments. Trivial to handle */
  700. ret = handler(dev, &info, &(iwr->u), NULL);
  701. #ifdef WE_SET_EVENT
  702. /* Generate an event to notify listeners of the change */
  703. if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
  704. ((ret == 0) || (ret == -EIWCOMMIT)))
  705. wireless_send_event(dev, cmd, &(iwr->u), NULL);
  706. #endif /* WE_SET_EVENT */
  707. } else {
  708. char * extra;
  709. int extra_size;
  710. int user_length = 0;
  711. int err;
  712. int essid_compat = 0;
  713. /* Calculate space needed by arguments. Always allocate
  714. * for max space. Easier, and won't last long... */
  715. extra_size = descr->max_tokens * descr->token_size;
  716. /* Check need for ESSID compatibility for WE < 21 */
  717. switch (cmd) {
  718. case SIOCSIWESSID:
  719. case SIOCGIWESSID:
  720. case SIOCSIWNICKN:
  721. case SIOCGIWNICKN:
  722. if (iwr->u.data.length == descr->max_tokens + 1)
  723. essid_compat = 1;
  724. else if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
  725. char essid[IW_ESSID_MAX_SIZE + 1];
  726. err = copy_from_user(essid, iwr->u.data.pointer,
  727. iwr->u.data.length *
  728. descr->token_size);
  729. if (err)
  730. return -EFAULT;
  731. if (essid[iwr->u.data.length - 1] == '\0')
  732. essid_compat = 1;
  733. }
  734. break;
  735. default:
  736. break;
  737. }
  738. iwr->u.data.length -= essid_compat;
  739. /* Check what user space is giving us */
  740. if (IW_IS_SET(cmd)) {
  741. /* Check NULL pointer */
  742. if ((iwr->u.data.pointer == NULL) &&
  743. (iwr->u.data.length != 0))
  744. return -EFAULT;
  745. /* Check if number of token fits within bounds */
  746. if (iwr->u.data.length > descr->max_tokens)
  747. return -E2BIG;
  748. if (iwr->u.data.length < descr->min_tokens)
  749. return -EINVAL;
  750. } else {
  751. /* Check NULL pointer */
  752. if (iwr->u.data.pointer == NULL)
  753. return -EFAULT;
  754. /* Save user space buffer size for checking */
  755. user_length = iwr->u.data.length;
  756. /* Don't check if user_length > max to allow forward
  757. * compatibility. The test user_length < min is
  758. * implied by the test at the end. */
  759. /* Support for very large requests */
  760. if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
  761. (user_length > descr->max_tokens)) {
  762. /* Allow userspace to GET more than max so
  763. * we can support any size GET requests.
  764. * There is still a limit : -ENOMEM. */
  765. extra_size = user_length * descr->token_size;
  766. /* Note : user_length is originally a __u16,
  767. * and token_size is controlled by us,
  768. * so extra_size won't get negative and
  769. * won't overflow... */
  770. }
  771. }
  772. #ifdef WE_IOCTL_DEBUG
  773. printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
  774. dev->name, extra_size);
  775. #endif /* WE_IOCTL_DEBUG */
  776. /* Create the kernel buffer */
  777. /* kzalloc ensures NULL-termination for essid_compat */
  778. extra = kzalloc(extra_size, GFP_KERNEL);
  779. if (extra == NULL) {
  780. return -ENOMEM;
  781. }
  782. /* If it is a SET, get all the extra data in here */
  783. if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
  784. err = copy_from_user(extra, iwr->u.data.pointer,
  785. iwr->u.data.length *
  786. descr->token_size);
  787. if (err) {
  788. kfree(extra);
  789. return -EFAULT;
  790. }
  791. #ifdef WE_IOCTL_DEBUG
  792. printk(KERN_DEBUG "%s (WE) : Got %d bytes\n",
  793. dev->name,
  794. iwr->u.data.length * descr->token_size);
  795. #endif /* WE_IOCTL_DEBUG */
  796. }
  797. /* Call the handler */
  798. ret = handler(dev, &info, &(iwr->u), extra);
  799. iwr->u.data.length += essid_compat;
  800. /* If we have something to return to the user */
  801. if (!ret && IW_IS_GET(cmd)) {
  802. /* Check if there is enough buffer up there */
  803. if (user_length < iwr->u.data.length) {
  804. kfree(extra);
  805. return -E2BIG;
  806. }
  807. err = copy_to_user(iwr->u.data.pointer, extra,
  808. iwr->u.data.length *
  809. descr->token_size);
  810. if (err)
  811. ret = -EFAULT;
  812. #ifdef WE_IOCTL_DEBUG
  813. printk(KERN_DEBUG "%s (WE) : Wrote %d bytes\n",
  814. dev->name,
  815. iwr->u.data.length * descr->token_size);
  816. #endif /* WE_IOCTL_DEBUG */
  817. }
  818. #ifdef WE_SET_EVENT
  819. /* Generate an event to notify listeners of the change */
  820. if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
  821. ((ret == 0) || (ret == -EIWCOMMIT))) {
  822. if (descr->flags & IW_DESCR_FLAG_RESTRICT)
  823. /* If the event is restricted, don't
  824. * export the payload */
  825. wireless_send_event(dev, cmd, &(iwr->u), NULL);
  826. else
  827. wireless_send_event(dev, cmd, &(iwr->u),
  828. extra);
  829. }
  830. #endif /* WE_SET_EVENT */
  831. /* Cleanup - I told you it wasn't that long ;-) */
  832. kfree(extra);
  833. }
  834. /* Call commit handler if needed and defined */
  835. if (ret == -EIWCOMMIT)
  836. ret = call_commit_handler(dev);
  837. /* Here, we will generate the appropriate event if needed */
  838. return ret;
  839. }
  840. /* ---------------------------------------------------------------- */
  841. /*
  842. * Wrapper to call a private Wireless Extension handler.
  843. * We do various checks and also take care of moving data between
  844. * user space and kernel space.
  845. * It's not as nice and slimline as the standard wrapper. The cause
  846. * is struct iw_priv_args, which was not really designed for the
  847. * job we are going here.
  848. *
  849. * IMPORTANT : This function prevent to set and get data on the same
  850. * IOCTL and enforce the SET/GET convention. Not doing it would be
  851. * far too hairy...
  852. * If you need to set and get data at the same time, please don't use
  853. * a iw_handler but process it in your ioctl handler (i.e. use the
  854. * old driver API).
  855. */
  856. static inline int ioctl_private_call(struct net_device * dev,
  857. struct ifreq * ifr,
  858. unsigned int cmd,
  859. iw_handler handler)
  860. {
  861. struct iwreq * iwr = (struct iwreq *) ifr;
  862. const struct iw_priv_args * descr = NULL;
  863. struct iw_request_info info;
  864. int extra_size = 0;
  865. int i;
  866. int ret = -EINVAL;
  867. /* Get the description of the IOCTL */
  868. for (i = 0; i < dev->wireless_handlers->num_private_args; i++)
  869. if (cmd == dev->wireless_handlers->private_args[i].cmd) {
  870. descr = &(dev->wireless_handlers->private_args[i]);
  871. break;
  872. }
  873. #ifdef WE_IOCTL_DEBUG
  874. printk(KERN_DEBUG "%s (WE) : Found private handler for 0x%04X\n",
  875. ifr->ifr_name, cmd);
  876. if (descr) {
  877. printk(KERN_DEBUG "%s (WE) : Name %s, set %X, get %X\n",
  878. dev->name, descr->name,
  879. descr->set_args, descr->get_args);
  880. }
  881. #endif /* WE_IOCTL_DEBUG */
  882. /* Compute the size of the set/get arguments */
  883. if (descr != NULL) {
  884. if (IW_IS_SET(cmd)) {
  885. int offset = 0; /* For sub-ioctls */
  886. /* Check for sub-ioctl handler */
  887. if (descr->name[0] == '\0')
  888. /* Reserve one int for sub-ioctl index */
  889. offset = sizeof(__u32);
  890. /* Size of set arguments */
  891. extra_size = get_priv_size(descr->set_args);
  892. /* Does it fits in iwr ? */
  893. if ((descr->set_args & IW_PRIV_SIZE_FIXED) &&
  894. ((extra_size + offset) <= IFNAMSIZ))
  895. extra_size = 0;
  896. } else {
  897. /* Size of get arguments */
  898. extra_size = get_priv_size(descr->get_args);
  899. /* Does it fits in iwr ? */
  900. if ((descr->get_args & IW_PRIV_SIZE_FIXED) &&
  901. (extra_size <= IFNAMSIZ))
  902. extra_size = 0;
  903. }
  904. }
  905. /* Prepare the call */
  906. info.cmd = cmd;
  907. info.flags = 0;
  908. /* Check if we have a pointer to user space data or not. */
  909. if (extra_size == 0) {
  910. /* No extra arguments. Trivial to handle */
  911. ret = handler(dev, &info, &(iwr->u), (char *) &(iwr->u));
  912. } else {
  913. char * extra;
  914. int err;
  915. /* Check what user space is giving us */
  916. if (IW_IS_SET(cmd)) {
  917. /* Check NULL pointer */
  918. if ((iwr->u.data.pointer == NULL) &&
  919. (iwr->u.data.length != 0))
  920. return -EFAULT;
  921. /* Does it fits within bounds ? */
  922. if (iwr->u.data.length > (descr->set_args &
  923. IW_PRIV_SIZE_MASK))
  924. return -E2BIG;
  925. } else {
  926. /* Check NULL pointer */
  927. if (iwr->u.data.pointer == NULL)
  928. return -EFAULT;
  929. }
  930. #ifdef WE_IOCTL_DEBUG
  931. printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
  932. dev->name, extra_size);
  933. #endif /* WE_IOCTL_DEBUG */
  934. /* Always allocate for max space. Easier, and won't last
  935. * long... */
  936. extra = kmalloc(extra_size, GFP_KERNEL);
  937. if (extra == NULL) {
  938. return -ENOMEM;
  939. }
  940. /* If it is a SET, get all the extra data in here */
  941. if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
  942. err = copy_from_user(extra, iwr->u.data.pointer,
  943. extra_size);
  944. if (err) {
  945. kfree(extra);
  946. return -EFAULT;
  947. }
  948. #ifdef WE_IOCTL_DEBUG
  949. printk(KERN_DEBUG "%s (WE) : Got %d elem\n",
  950. dev->name, iwr->u.data.length);
  951. #endif /* WE_IOCTL_DEBUG */
  952. }
  953. /* Call the handler */
  954. ret = handler(dev, &info, &(iwr->u), extra);
  955. /* If we have something to return to the user */
  956. if (!ret && IW_IS_GET(cmd)) {
  957. /* Adjust for the actual length if it's variable,
  958. * avoid leaking kernel bits outside. */
  959. if (!(descr->get_args & IW_PRIV_SIZE_FIXED)) {
  960. extra_size = adjust_priv_size(descr->get_args,
  961. &(iwr->u));
  962. }
  963. err = copy_to_user(iwr->u.data.pointer, extra,
  964. extra_size);
  965. if (err)
  966. ret = -EFAULT;
  967. #ifdef WE_IOCTL_DEBUG
  968. printk(KERN_DEBUG "%s (WE) : Wrote %d elem\n",
  969. dev->name, iwr->u.data.length);
  970. #endif /* WE_IOCTL_DEBUG */
  971. }
  972. /* Cleanup - I told you it wasn't that long ;-) */
  973. kfree(extra);
  974. }
  975. /* Call commit handler if needed and defined */
  976. if (ret == -EIWCOMMIT)
  977. ret = call_commit_handler(dev);
  978. return ret;
  979. }
  980. /* ---------------------------------------------------------------- */
  981. /*
  982. * Main IOCTl dispatcher. Called from the main networking code
  983. * (dev_ioctl() in net/core/dev.c).
  984. * Check the type of IOCTL and call the appropriate wrapper...
  985. */
  986. int wireless_process_ioctl(struct ifreq *ifr, unsigned int cmd)
  987. {
  988. struct net_device *dev;
  989. iw_handler handler;
  990. /* Permissions are already checked in dev_ioctl() before calling us.
  991. * The copy_to/from_user() of ifr is also dealt with in there */
  992. /* Make sure the device exist */
  993. if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL)
  994. return -ENODEV;
  995. /* A bunch of special cases, then the generic case...
  996. * Note that 'cmd' is already filtered in dev_ioctl() with
  997. * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
  998. switch (cmd) {
  999. case SIOCGIWSTATS:
  1000. /* Get Wireless Stats */
  1001. return ioctl_standard_call(dev,
  1002. ifr,
  1003. cmd,
  1004. &iw_handler_get_iwstats);
  1005. case SIOCGIWPRIV:
  1006. /* Check if we have some wireless handlers defined */
  1007. if (dev->wireless_handlers != NULL) {
  1008. /* We export to user space the definition of
  1009. * the private handler ourselves */
  1010. return ioctl_standard_call(dev,
  1011. ifr,
  1012. cmd,
  1013. &iw_handler_get_private);
  1014. }
  1015. // ## Fall-through for old API ##
  1016. default:
  1017. /* Generic IOCTL */
  1018. /* Basic check */
  1019. if (!netif_device_present(dev))
  1020. return -ENODEV;
  1021. /* New driver API : try to find the handler */
  1022. handler = get_handler(dev, cmd);
  1023. if (handler != NULL) {
  1024. /* Standard and private are not the same */
  1025. if (cmd < SIOCIWFIRSTPRIV)
  1026. return ioctl_standard_call(dev,
  1027. ifr,
  1028. cmd,
  1029. handler);
  1030. else
  1031. return ioctl_private_call(dev,
  1032. ifr,
  1033. cmd,
  1034. handler);
  1035. }
  1036. /* Old driver API : call driver ioctl handler */
  1037. if (dev->do_ioctl) {
  1038. return dev->do_ioctl(dev, ifr, cmd);
  1039. }
  1040. return -EOPNOTSUPP;
  1041. }
  1042. /* Not reached */
  1043. return -EINVAL;
  1044. }
  1045. /********************** RTNETLINK REQUEST API **********************/
  1046. /*
  1047. * The alternate user space API to configure all those Wireless Extensions
  1048. * is through RtNetlink.
  1049. * This API support only the new driver API (iw_handler).
  1050. *
  1051. * This RtNetlink API use the same query/reply model as the ioctl API.
  1052. * Maximum effort has been done to fit in the RtNetlink model, and
  1053. * we support both RtNetlink Set and RtNelink Get operations.
  1054. * On the other hand, we don't offer Dump operations because of the
  1055. * following reasons :
  1056. * o Large number of parameters, most optional
  1057. * o Large size of some parameters (> 100 bytes)
  1058. * o Each parameters need to be extracted from hardware
  1059. * o Scan requests can take seconds and disable network activity.
  1060. * Because of this high cost/overhead, we want to return only the
  1061. * parameters the user application is really interested in.
  1062. * We could offer partial Dump using the IW_DESCR_FLAG_DUMP flag.
  1063. *
  1064. * The API uses the standard RtNetlink socket. When the RtNetlink code
  1065. * find a IFLA_WIRELESS field in a RtNetlink SET_LINK request,
  1066. * it calls here.
  1067. */
  1068. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  1069. /* ---------------------------------------------------------------- */
  1070. /*
  1071. * Wrapper to call a standard Wireless Extension GET handler.
  1072. * We do various checks and call the handler with the proper args.
  1073. */
  1074. static int rtnetlink_standard_get(struct net_device * dev,
  1075. struct iw_event * request,
  1076. int request_len,
  1077. iw_handler handler,
  1078. char ** p_buf,
  1079. int * p_len)
  1080. {
  1081. const struct iw_ioctl_description * descr = NULL;
  1082. unsigned int cmd;
  1083. union iwreq_data * wrqu;
  1084. int hdr_len;
  1085. struct iw_request_info info;
  1086. char * buffer = NULL;
  1087. int buffer_size = 0;
  1088. int ret = -EINVAL;
  1089. /* Get the description of the Request */
  1090. cmd = request->cmd;
  1091. if ((cmd - SIOCIWFIRST) >= standard_ioctl_num)
  1092. return -EOPNOTSUPP;
  1093. descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
  1094. #ifdef WE_RTNETLINK_DEBUG
  1095. printk(KERN_DEBUG "%s (WE.r) : Found standard handler for 0x%04X\n",
  1096. dev->name, cmd);
  1097. printk(KERN_DEBUG "%s (WE.r) : 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);
  1098. #endif /* WE_RTNETLINK_DEBUG */
  1099. /* Check if wrqu is complete */
  1100. hdr_len = event_type_size[descr->header_type];
  1101. if (request_len < hdr_len) {
  1102. #ifdef WE_RTNETLINK_DEBUG
  1103. printk(KERN_DEBUG
  1104. "%s (WE.r) : Wireless request too short (%d)\n",
  1105. dev->name, request_len);
  1106. #endif /* WE_RTNETLINK_DEBUG */
  1107. return -EINVAL;
  1108. }
  1109. /* Prepare the call */
  1110. info.cmd = cmd;
  1111. info.flags = 0;
  1112. /* Check if we have extra data in the reply or not */
  1113. if (descr->header_type != IW_HEADER_TYPE_POINT) {
  1114. /* Create the kernel buffer that we will return.
  1115. * It's at an offset to match the TYPE_POINT case... */
  1116. buffer_size = request_len + IW_EV_POINT_OFF;
  1117. buffer = kmalloc(buffer_size, GFP_KERNEL);
  1118. if (buffer == NULL) {
  1119. return -ENOMEM;
  1120. }
  1121. /* Copy event data */
  1122. memcpy(buffer + IW_EV_POINT_OFF, request, request_len);
  1123. /* Use our own copy of wrqu */
  1124. wrqu = (union iwreq_data *) (buffer + IW_EV_POINT_OFF
  1125. + IW_EV_LCP_PK_LEN);
  1126. /* No extra arguments. Trivial to handle */
  1127. ret = handler(dev, &info, wrqu, NULL);
  1128. } else {
  1129. union iwreq_data wrqu_point;
  1130. char * extra = NULL;
  1131. int extra_size = 0;
  1132. /* Get a temp copy of wrqu (skip pointer) */
  1133. memcpy(((char *) &wrqu_point) + IW_EV_POINT_OFF,
  1134. ((char *) request) + IW_EV_LCP_PK_LEN,
  1135. IW_EV_POINT_LEN - IW_EV_LCP_PK_LEN);
  1136. /* Calculate space needed by arguments. Always allocate
  1137. * for max space. Easier, and won't last long... */
  1138. extra_size = descr->max_tokens * descr->token_size;
  1139. /* Support for very large requests */
  1140. if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
  1141. (wrqu_point.data.length > descr->max_tokens))
  1142. extra_size = (wrqu_point.data.length
  1143. * descr->token_size);
  1144. buffer_size = extra_size + IW_EV_POINT_PK_LEN + IW_EV_POINT_OFF;
  1145. #ifdef WE_RTNETLINK_DEBUG
  1146. printk(KERN_DEBUG "%s (WE.r) : Malloc %d bytes (%d bytes)\n",
  1147. dev->name, extra_size, buffer_size);
  1148. #endif /* WE_RTNETLINK_DEBUG */
  1149. /* Create the kernel buffer that we will return */
  1150. buffer = kmalloc(buffer_size, GFP_KERNEL);
  1151. if (buffer == NULL) {
  1152. return -ENOMEM;
  1153. }
  1154. /* Put wrqu in the right place (just before extra).
  1155. * Leave space for IWE header and dummy pointer...
  1156. * Note that IW_EV_LCP_PK_LEN==4 bytes, so it's still aligned.
  1157. */
  1158. memcpy(buffer + IW_EV_LCP_PK_LEN + IW_EV_POINT_OFF,
  1159. ((char *) &wrqu_point) + IW_EV_POINT_OFF,
  1160. IW_EV_POINT_PK_LEN - IW_EV_LCP_PK_LEN);
  1161. wrqu = (union iwreq_data *) (buffer + IW_EV_LCP_PK_LEN);
  1162. /* Extra comes logically after that. Offset +12 bytes. */
  1163. extra = buffer + IW_EV_POINT_OFF + IW_EV_POINT_PK_LEN;
  1164. /* Call the handler */
  1165. ret = handler(dev, &info, wrqu, extra);
  1166. /* Calculate real returned length */
  1167. extra_size = (wrqu->data.length * descr->token_size);
  1168. /* Re-adjust reply size */
  1169. request->len = extra_size + IW_EV_POINT_PK_LEN;
  1170. /* Put the iwe header where it should, i.e. scrap the
  1171. * dummy pointer. */
  1172. memcpy(buffer + IW_EV_POINT_OFF, request, IW_EV_LCP_PK_LEN);
  1173. #ifdef WE_RTNETLINK_DEBUG
  1174. printk(KERN_DEBUG "%s (WE.r) : Reply 0x%04X, hdr_len %d, tokens %d, extra_size %d, buffer_size %d\n", dev->name, cmd, hdr_len, wrqu->data.length, extra_size, buffer_size);
  1175. #endif /* WE_RTNETLINK_DEBUG */
  1176. /* Check if there is enough buffer up there */
  1177. if (wrqu_point.data.length < wrqu->data.length)
  1178. ret = -E2BIG;
  1179. }
  1180. /* Return the buffer to the caller */
  1181. if (!ret) {
  1182. *p_buf = buffer;
  1183. *p_len = request->len;
  1184. } else {
  1185. /* Cleanup */
  1186. if (buffer)
  1187. kfree(buffer);
  1188. }
  1189. return ret;
  1190. }
  1191. /* ---------------------------------------------------------------- */
  1192. /*
  1193. * Wrapper to call a standard Wireless Extension SET handler.
  1194. * We do various checks and call the handler with the proper args.
  1195. */
  1196. static inline int rtnetlink_standard_set(struct net_device * dev,
  1197. struct iw_event * request,
  1198. int request_len,
  1199. iw_handler handler)
  1200. {
  1201. const struct iw_ioctl_description * descr = NULL;
  1202. unsigned int cmd;
  1203. union iwreq_data * wrqu;
  1204. union iwreq_data wrqu_point;
  1205. int hdr_len;
  1206. char * extra = NULL;
  1207. int extra_size = 0;
  1208. struct iw_request_info info;
  1209. int ret = -EINVAL;
  1210. /* Get the description of the Request */
  1211. cmd = request->cmd;
  1212. if ((cmd - SIOCIWFIRST) >= standard_ioctl_num)
  1213. return -EOPNOTSUPP;
  1214. descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
  1215. #ifdef WE_RTNETLINK_DEBUG
  1216. printk(KERN_DEBUG "%s (WE.r) : Found standard SET handler for 0x%04X\n",
  1217. dev->name, cmd);
  1218. printk(KERN_DEBUG "%s (WE.r) : 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);
  1219. #endif /* WE_RTNETLINK_DEBUG */
  1220. /* Extract fixed header from request. This is properly aligned. */
  1221. wrqu = (union iwreq_data *) (((char *) request) + IW_EV_LCP_PK_LEN);
  1222. /* Check if wrqu is complete */
  1223. hdr_len = event_type_pk_size[descr->header_type];
  1224. if (request_len < hdr_len) {
  1225. #ifdef WE_RTNETLINK_DEBUG
  1226. printk(KERN_DEBUG
  1227. "%s (WE.r) : Wireless request too short (%d)\n",
  1228. dev->name, request_len);
  1229. #endif /* WE_RTNETLINK_DEBUG */
  1230. return -EINVAL;
  1231. }
  1232. /* Prepare the call */
  1233. info.cmd = cmd;
  1234. info.flags = 0;
  1235. /* Check if we have extra data in the request or not */
  1236. if (descr->header_type != IW_HEADER_TYPE_POINT) {
  1237. /* No extra arguments. Trivial to handle */
  1238. ret = handler(dev, &info, wrqu, NULL);
  1239. } else {
  1240. int extra_len;
  1241. /* Put wrqu in the right place (skip pointer) */
  1242. memcpy(((char *) &wrqu_point) + IW_EV_POINT_OFF,
  1243. wrqu, IW_EV_POINT_PK_LEN - IW_EV_LCP_PK_LEN);
  1244. /* Don't forget about the event code... */
  1245. wrqu = &wrqu_point;
  1246. /* Check if number of token fits within bounds */
  1247. if (wrqu_point.data.length > descr->max_tokens)
  1248. return -E2BIG;
  1249. if (wrqu_point.data.length < descr->min_tokens)
  1250. return -EINVAL;
  1251. /* Real length of payload */
  1252. extra_len = wrqu_point.data.length * descr->token_size;
  1253. /* Check if request is self consistent */
  1254. if ((request_len - hdr_len) < extra_len) {
  1255. #ifdef WE_RTNETLINK_DEBUG
  1256. printk(KERN_DEBUG "%s (WE.r) : Wireless request data too short (%d)\n",
  1257. dev->name, extra_size);
  1258. #endif /* WE_RTNETLINK_DEBUG */
  1259. return -EINVAL;
  1260. }
  1261. #ifdef WE_RTNETLINK_DEBUG
  1262. printk(KERN_DEBUG "%s (WE.r) : Malloc %d bytes\n",
  1263. dev->name, extra_size);
  1264. #endif /* WE_RTNETLINK_DEBUG */
  1265. /* Always allocate for max space. Easier, and won't last
  1266. * long... */
  1267. extra_size = descr->max_tokens * descr->token_size;
  1268. extra = kmalloc(extra_size, GFP_KERNEL);
  1269. if (extra == NULL)
  1270. return -ENOMEM;
  1271. /* Copy extra in aligned buffer */
  1272. memcpy(extra, ((char *) request) + hdr_len, extra_len);
  1273. /* Call the handler */
  1274. ret = handler(dev, &info, &wrqu_point, extra);
  1275. }
  1276. #ifdef WE_SET_EVENT
  1277. /* Generate an event to notify listeners of the change */
  1278. if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
  1279. ((ret == 0) || (ret == -EIWCOMMIT))) {
  1280. if (descr->flags & IW_DESCR_FLAG_RESTRICT)
  1281. /* If the event is restricted, don't
  1282. * export the payload */
  1283. wireless_send_event(dev, cmd, wrqu, NULL);
  1284. else
  1285. wireless_send_event(dev, cmd, wrqu, extra);
  1286. }
  1287. #endif /* WE_SET_EVENT */
  1288. /* Cleanup - I told you it wasn't that long ;-) */
  1289. if (extra)
  1290. kfree(extra);
  1291. /* Call commit handler if needed and defined */
  1292. if (ret == -EIWCOMMIT)
  1293. ret = call_commit_handler(dev);
  1294. return ret;
  1295. }
  1296. /* ---------------------------------------------------------------- */
  1297. /*
  1298. * Wrapper to call a private Wireless Extension GET handler.
  1299. * Same as above...
  1300. * It's not as nice and slimline as the standard wrapper. The cause
  1301. * is struct iw_priv_args, which was not really designed for the
  1302. * job we are going here.
  1303. *
  1304. * IMPORTANT : This function prevent to set and get data on the same
  1305. * IOCTL and enforce the SET/GET convention. Not doing it would be
  1306. * far too hairy...
  1307. * If you need to set and get data at the same time, please don't use
  1308. * a iw_handler but process it in your ioctl handler (i.e. use the
  1309. * old driver API).
  1310. */
  1311. static inline int rtnetlink_private_get(struct net_device * dev,
  1312. struct iw_event * request,
  1313. int request_len,
  1314. iw_handler handler,
  1315. char ** p_buf,
  1316. int * p_len)
  1317. {
  1318. const struct iw_priv_args * descr = NULL;
  1319. unsigned int cmd;
  1320. union iwreq_data * wrqu;
  1321. int hdr_len;
  1322. struct iw_request_info info;
  1323. int extra_size = 0;
  1324. int i;
  1325. char * buffer = NULL;
  1326. int buffer_size = 0;
  1327. int ret = -EINVAL;
  1328. /* Get the description of the Request */
  1329. cmd = request->cmd;
  1330. for (i = 0; i < dev->wireless_handlers->num_private_args; i++)
  1331. if (cmd == dev->wireless_handlers->private_args[i].cmd) {
  1332. descr = &(dev->wireless_handlers->private_args[i]);
  1333. break;
  1334. }
  1335. if (descr == NULL)
  1336. return -EOPNOTSUPP;
  1337. #ifdef WE_RTNETLINK_DEBUG
  1338. printk(KERN_DEBUG "%s (WE.r) : Found private handler for 0x%04X\n",
  1339. dev->name, cmd);
  1340. printk(KERN_DEBUG "%s (WE.r) : Name %s, set %X, get %X\n",
  1341. dev->name, descr->name, descr->set_args, descr->get_args);
  1342. #endif /* WE_RTNETLINK_DEBUG */
  1343. /* Compute the max size of the get arguments */
  1344. extra_size = get_priv_size(descr->get_args);
  1345. /* Does it fits in wrqu ? */
  1346. if ((descr->get_args & IW_PRIV_SIZE_FIXED) &&
  1347. (extra_size <= IFNAMSIZ)) {
  1348. hdr_len = extra_size;
  1349. extra_size = 0;
  1350. } else {
  1351. hdr_len = IW_EV_POINT_PK_LEN;
  1352. }
  1353. /* Check if wrqu is complete */
  1354. if (request_len < hdr_len) {
  1355. #ifdef WE_RTNETLINK_DEBUG
  1356. printk(KERN_DEBUG
  1357. "%s (WE.r) : Wireless request too short (%d)\n",
  1358. dev->name, request_len);
  1359. #endif /* WE_RTNETLINK_DEBUG */
  1360. return -EINVAL;
  1361. }
  1362. /* Prepare the call */
  1363. info.cmd = cmd;
  1364. info.flags = 0;
  1365. /* Check if we have a pointer to user space data or not. */
  1366. if (extra_size == 0) {
  1367. /* Create the kernel buffer that we will return.
  1368. * It's at an offset to match the TYPE_POINT case... */
  1369. buffer_size = request_len + IW_EV_POINT_OFF;
  1370. buffer = kmalloc(buffer_size, GFP_KERNEL);
  1371. if (buffer == NULL) {
  1372. return -ENOMEM;
  1373. }
  1374. /* Copy event data */
  1375. memcpy(buffer + IW_EV_POINT_OFF, request, request_len);
  1376. /* Use our own copy of wrqu */
  1377. wrqu = (union iwreq_data *) (buffer + IW_EV_POINT_OFF
  1378. + IW_EV_LCP_PK_LEN);
  1379. /* No extra arguments. Trivial to handle */
  1380. ret = handler(dev, &info, wrqu, (char *) wrqu);
  1381. } else {
  1382. char * extra;
  1383. /* Buffer for full reply */
  1384. buffer_size = extra_size + IW_EV_POINT_PK_LEN + IW_EV_POINT_OFF;
  1385. #ifdef WE_RTNETLINK_DEBUG
  1386. printk(KERN_DEBUG "%s (WE.r) : Malloc %d bytes (%d bytes)\n",
  1387. dev->name, extra_size, buffer_size);
  1388. #endif /* WE_RTNETLINK_DEBUG */
  1389. /* Create the kernel buffer that we will return */
  1390. buffer = kmalloc(buffer_size, GFP_KERNEL);
  1391. if (buffer == NULL) {
  1392. return -ENOMEM;
  1393. }
  1394. /* Put wrqu in the right place (just before extra).
  1395. * Leave space for IWE header and dummy pointer...
  1396. * Note that IW_EV_LCP_PK_LEN==4 bytes, so it's still aligned.
  1397. */
  1398. memcpy(buffer + IW_EV_LCP_PK_LEN + IW_EV_POINT_OFF,
  1399. ((char *) request) + IW_EV_LCP_PK_LEN,
  1400. IW_EV_POINT_PK_LEN - IW_EV_LCP_PK_LEN);
  1401. wrqu = (union iwreq_data *) (buffer + IW_EV_LCP_PK_LEN);
  1402. /* Extra comes logically after that. Offset +12 bytes. */
  1403. extra = buffer + IW_EV_POINT_OFF + IW_EV_POINT_PK_LEN;
  1404. /* Call the handler */
  1405. ret = handler(dev, &info, wrqu, extra);
  1406. /* Adjust for the actual length if it's variable,
  1407. * avoid leaking kernel bits outside. */
  1408. if (!(descr->get_args & IW_PRIV_SIZE_FIXED))
  1409. extra_size = adjust_priv_size(descr->get_args, wrqu);
  1410. /* Re-adjust reply size */
  1411. request->len = extra_size + IW_EV_POINT_PK_LEN;
  1412. /* Put the iwe header where it should, i.e. scrap the
  1413. * dummy pointer. */
  1414. memcpy(buffer + IW_EV_POINT_OFF, request, IW_EV_LCP_PK_LEN);
  1415. #ifdef WE_RTNETLINK_DEBUG
  1416. printk(KERN_DEBUG "%s (WE.r) : Reply 0x%04X, hdr_len %d, tokens %d, extra_size %d, buffer_size %d\n", dev->name, cmd, hdr_len, wrqu->data.length, extra_size, buffer_size);
  1417. #endif /* WE_RTNETLINK_DEBUG */
  1418. }
  1419. /* Return the buffer to the caller */
  1420. if (!ret) {
  1421. *p_buf = buffer;
  1422. *p_len = request->len;
  1423. } else {
  1424. /* Cleanup */
  1425. if (buffer)
  1426. kfree(buffer);
  1427. }
  1428. return ret;
  1429. }
  1430. /* ---------------------------------------------------------------- */
  1431. /*
  1432. * Wrapper to call a private Wireless Extension SET handler.
  1433. * Same as above...
  1434. * It's not as nice and slimline as the standard wrapper. The cause
  1435. * is struct iw_priv_args, which was not really designed for the
  1436. * job we are going here.
  1437. *
  1438. * IMPORTANT : This function prevent to set and get data on the same
  1439. * IOCTL and enforce the SET/GET convention. Not doing it would be
  1440. * far too hairy...
  1441. * If you need to set and get data at the same time, please don't use
  1442. * a iw_handler but process it in your ioctl handler (i.e. use the
  1443. * old driver API).
  1444. */
  1445. static inline int rtnetlink_private_set(struct net_device * dev,
  1446. struct iw_event * request,
  1447. int request_len,
  1448. iw_handler handler)
  1449. {
  1450. const struct iw_priv_args * descr = NULL;
  1451. unsigned int cmd;
  1452. union iwreq_data * wrqu;
  1453. union iwreq_data wrqu_point;
  1454. int hdr_len;
  1455. char * extra = NULL;
  1456. int extra_size = 0;
  1457. int offset = 0; /* For sub-ioctls */
  1458. struct iw_request_info info;
  1459. int i;
  1460. int ret = -EINVAL;
  1461. /* Get the description of the Request */
  1462. cmd = request->cmd;
  1463. for (i = 0; i < dev->wireless_handlers->num_private_args; i++)
  1464. if (cmd == dev->wireless_handlers->private_args[i].cmd) {
  1465. descr = &(dev->wireless_handlers->private_args[i]);
  1466. break;
  1467. }
  1468. if (descr == NULL)
  1469. return -EOPNOTSUPP;
  1470. #ifdef WE_RTNETLINK_DEBUG
  1471. printk(KERN_DEBUG "%s (WE.r) : Found private handler for 0x%04X\n",
  1472. ifr->ifr_name, cmd);
  1473. printk(KERN_DEBUG "%s (WE.r) : Name %s, set %X, get %X\n",
  1474. dev->name, descr->name, descr->set_args, descr->get_args);
  1475. #endif /* WE_RTNETLINK_DEBUG */
  1476. /* Compute the size of the set arguments */
  1477. /* Check for sub-ioctl handler */
  1478. if (descr->name[0] == '\0')
  1479. /* Reserve one int for sub-ioctl index */
  1480. offset = sizeof(__u32);
  1481. /* Size of set arguments */
  1482. extra_size = get_priv_size(descr->set_args);
  1483. /* Does it fits in wrqu ? */
  1484. if ((descr->set_args & IW_PRIV_SIZE_FIXED) &&
  1485. (extra_size <= IFNAMSIZ)) {
  1486. hdr_len = IW_EV_LCP_PK_LEN + extra_size;
  1487. extra_size = 0;
  1488. } else {
  1489. hdr_len = IW_EV_POINT_PK_LEN;
  1490. }
  1491. /* Extract fixed header from request. This is properly aligned. */
  1492. wrqu = (union iwreq_data *) (((char *) request) + IW_EV_LCP_PK_LEN);
  1493. /* Check if wrqu is complete */
  1494. if (request_len < hdr_len) {
  1495. #ifdef WE_RTNETLINK_DEBUG
  1496. printk(KERN_DEBUG
  1497. "%s (WE.r) : Wireless request too short (%d)\n",
  1498. dev->name, request_len);
  1499. #endif /* WE_RTNETLINK_DEBUG */
  1500. return -EINVAL;
  1501. }
  1502. /* Prepare the call */
  1503. info.cmd = cmd;
  1504. info.flags = 0;
  1505. /* Check if we have a pointer to user space data or not. */
  1506. if (extra_size == 0) {
  1507. /* No extra arguments. Trivial to handle */
  1508. ret = handler(dev, &info, wrqu, (char *) wrqu);
  1509. } else {
  1510. int extra_len;
  1511. /* Put wrqu in the right place (skip pointer) */
  1512. memcpy(((char *) &wrqu_point) + IW_EV_POINT_OFF,
  1513. wrqu, IW_EV_POINT_PK_LEN - IW_EV_LCP_PK_LEN);
  1514. /* Does it fits within bounds ? */
  1515. if (wrqu_point.data.length > (descr->set_args &
  1516. IW_PRIV_SIZE_MASK))
  1517. return -E2BIG;
  1518. /* Real length of payload */
  1519. extra_len = adjust_priv_size(descr->set_args, &wrqu_point);
  1520. /* Check if request is self consistent */
  1521. if ((request_len - hdr_len) < extra_len) {
  1522. #ifdef WE_RTNETLINK_DEBUG
  1523. printk(KERN_DEBUG "%s (WE.r) : Wireless request data too short (%d)\n",
  1524. dev->name, extra_size);
  1525. #endif /* WE_RTNETLINK_DEBUG */
  1526. return -EINVAL;
  1527. }
  1528. #ifdef WE_RTNETLINK_DEBUG
  1529. printk(KERN_DEBUG "%s (WE.r) : Malloc %d bytes\n",
  1530. dev->name, extra_size);
  1531. #endif /* WE_RTNETLINK_DEBUG */
  1532. /* Always allocate for max space. Easier, and won't last
  1533. * long... */
  1534. extra = kmalloc(extra_size, GFP_KERNEL);
  1535. if (extra == NULL)
  1536. return -ENOMEM;
  1537. /* Copy extra in aligned buffer */
  1538. memcpy(extra, ((char *) request) + hdr_len, extra_len);
  1539. /* Call the handler */
  1540. ret = handler(dev, &info, &wrqu_point, extra);
  1541. /* Cleanup - I told you it wasn't that long ;-) */
  1542. kfree(extra);
  1543. }
  1544. /* Call commit handler if needed and defined */
  1545. if (ret == -EIWCOMMIT)
  1546. ret = call_commit_handler(dev);
  1547. return ret;
  1548. }
  1549. /* ---------------------------------------------------------------- */
  1550. /*
  1551. * Main RtNetlink dispatcher. Called from the main networking code
  1552. * (do_getlink() in net/core/rtnetlink.c).
  1553. * Check the type of Request and call the appropriate wrapper...
  1554. */
  1555. int wireless_rtnetlink_get(struct net_device * dev,
  1556. char * data,
  1557. int len,
  1558. char ** p_buf,
  1559. int * p_len)
  1560. {
  1561. struct iw_event * request = (struct iw_event *) data;
  1562. iw_handler handler;
  1563. /* Check length */
  1564. if (len < IW_EV_LCP_PK_LEN) {
  1565. printk(KERN_DEBUG "%s (WE.r) : RtNetlink request too short (%d)\n",
  1566. dev->name, len);
  1567. return -EINVAL;
  1568. }
  1569. /* ReCheck length (len may have padding) */
  1570. if (request->len > len) {
  1571. printk(KERN_DEBUG "%s (WE.r) : RtNetlink request len invalid (%d-%d)\n",
  1572. dev->name, request->len, len);
  1573. return -EINVAL;
  1574. }
  1575. /* Only accept GET requests in here */
  1576. if (!IW_IS_GET(request->cmd))
  1577. return -EOPNOTSUPP;
  1578. /* If command is `get the encoding parameters', check if
  1579. * the user has the right to do it */
  1580. if (request->cmd == SIOCGIWENCODE ||
  1581. request->cmd == SIOCGIWENCODEEXT) {
  1582. if (!capable(CAP_NET_ADMIN))
  1583. return -EPERM;
  1584. }
  1585. /* Special cases */
  1586. if (request->cmd == SIOCGIWSTATS)
  1587. /* Get Wireless Stats */
  1588. return rtnetlink_standard_get(dev,
  1589. request,
  1590. request->len,
  1591. &iw_handler_get_iwstats,
  1592. p_buf, p_len);
  1593. if (request->cmd == SIOCGIWPRIV) {
  1594. /* Check if we have some wireless handlers defined */
  1595. if (dev->wireless_handlers == NULL)
  1596. return -EOPNOTSUPP;
  1597. /* Get Wireless Stats */
  1598. return rtnetlink_standard_get(dev,
  1599. request,
  1600. request->len,
  1601. &iw_handler_get_private,
  1602. p_buf, p_len);
  1603. }
  1604. /* Basic check */
  1605. if (!netif_device_present(dev))
  1606. return -ENODEV;
  1607. /* Try to find the handler */
  1608. handler = get_handler(dev, request->cmd);
  1609. if (handler != NULL) {
  1610. /* Standard and private are not the same */
  1611. if (request->cmd < SIOCIWFIRSTPRIV)
  1612. return rtnetlink_standard_get(dev,
  1613. request,
  1614. request->len,
  1615. handler,
  1616. p_buf, p_len);
  1617. else
  1618. return rtnetlink_private_get(dev,
  1619. request,
  1620. request->len,
  1621. handler,
  1622. p_buf, p_len);
  1623. }
  1624. return -EOPNOTSUPP;
  1625. }
  1626. /* ---------------------------------------------------------------- */
  1627. /*
  1628. * Main RtNetlink dispatcher. Called from the main networking code
  1629. * (do_setlink() in net/core/rtnetlink.c).
  1630. * Check the type of Request and call the appropriate wrapper...
  1631. */
  1632. int wireless_rtnetlink_set(struct net_device * dev,
  1633. char * data,
  1634. int len)
  1635. {
  1636. struct iw_event * request = (struct iw_event *) data;
  1637. iw_handler handler;
  1638. /* Check length */
  1639. if (len < IW_EV_LCP_PK_LEN) {
  1640. printk(KERN_DEBUG "%s (WE.r) : RtNetlink request too short (%d)\n",
  1641. dev->name, len);
  1642. return -EINVAL;
  1643. }
  1644. /* ReCheck length (len may have padding) */
  1645. if (request->len > len) {
  1646. printk(KERN_DEBUG "%s (WE.r) : RtNetlink request len invalid (%d-%d)\n",
  1647. dev->name, request->len, len);
  1648. return -EINVAL;
  1649. }
  1650. /* Only accept SET requests in here */
  1651. if (!IW_IS_SET(request->cmd))
  1652. return -EOPNOTSUPP;
  1653. /* Basic check */
  1654. if (!netif_device_present(dev))
  1655. return -ENODEV;
  1656. /* New driver API : try to find the handler */
  1657. handler = get_handler(dev, request->cmd);
  1658. if (handler != NULL) {
  1659. /* Standard and private are not the same */
  1660. if (request->cmd < SIOCIWFIRSTPRIV)
  1661. return rtnetlink_standard_set(dev,
  1662. request,
  1663. request->len,
  1664. handler);
  1665. else
  1666. return rtnetlink_private_set(dev,
  1667. request,
  1668. request->len,
  1669. handler);
  1670. }
  1671. return -EOPNOTSUPP;
  1672. }
  1673. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  1674. /************************* EVENT PROCESSING *************************/
  1675. /*
  1676. * Process events generated by the wireless layer or the driver.
  1677. * Most often, the event will be propagated through rtnetlink
  1678. */
  1679. #ifdef WE_EVENT_RTNETLINK
  1680. /* ---------------------------------------------------------------- */
  1681. /*
  1682. * Locking...
  1683. * ----------
  1684. *
  1685. * Thanks to Herbert Xu <herbert@gondor.apana.org.au> for fixing
  1686. * the locking issue in here and implementing this code !
  1687. *
  1688. * The issue : wireless_send_event() is often called in interrupt context,
  1689. * while the Netlink layer can never be called in interrupt context.
  1690. * The fully formed RtNetlink events are queued, and then a tasklet is run
  1691. * to feed those to Netlink.
  1692. * The skb_queue is interrupt safe, and its lock is not held while calling
  1693. * Netlink, so there is no possibility of dealock.
  1694. * Jean II
  1695. */
  1696. static struct sk_buff_head wireless_nlevent_queue;
  1697. static int __init wireless_nlevent_init(void)
  1698. {
  1699. skb_queue_head_init(&wireless_nlevent_queue);
  1700. return 0;
  1701. }
  1702. subsys_initcall(wireless_nlevent_init);
  1703. static void wireless_nlevent_process(unsigned long data)
  1704. {
  1705. struct sk_buff *skb;
  1706. while ((skb = skb_dequeue(&wireless_nlevent_queue)))
  1707. rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
  1708. }
  1709. static DECLARE_TASKLET(wireless_nlevent_tasklet, wireless_nlevent_process, 0);
  1710. /* ---------------------------------------------------------------- */
  1711. /*
  1712. * Fill a rtnetlink message with our event data.
  1713. * Note that we propage only the specified event and don't dump the
  1714. * current wireless config. Dumping the wireless config is far too
  1715. * expensive (for each parameter, the driver need to query the hardware).
  1716. */
  1717. static inline int rtnetlink_fill_iwinfo(struct sk_buff * skb,
  1718. struct net_device * dev,
  1719. int type,
  1720. char * event,
  1721. int event_len)
  1722. {
  1723. struct ifinfomsg *r;
  1724. struct nlmsghdr *nlh;
  1725. unsigned char *b = skb->tail;
  1726. nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(*r));
  1727. r = NLMSG_DATA(nlh);
  1728. r->ifi_family = AF_UNSPEC;
  1729. r->__ifi_pad = 0;
  1730. r->ifi_type = dev->type;
  1731. r->ifi_index = dev->ifindex;
  1732. r->ifi_flags = dev_get_flags(dev);
  1733. r->ifi_change = 0; /* Wireless changes don't affect those flags */
  1734. /* Add the wireless events in the netlink packet */
  1735. RTA_PUT(skb, IFLA_WIRELESS, event_len, event);
  1736. nlh->nlmsg_len = skb->tail - b;
  1737. return skb->len;
  1738. nlmsg_failure:
  1739. rtattr_failure:
  1740. skb_trim(skb, b - skb->data);
  1741. return -1;
  1742. }
  1743. /* ---------------------------------------------------------------- */
  1744. /*
  1745. * Create and broadcast and send it on the standard rtnetlink socket
  1746. * This is a pure clone rtmsg_ifinfo() in net/core/rtnetlink.c
  1747. * Andrzej Krzysztofowicz mandated that I used a IFLA_XXX field
  1748. * within a RTM_NEWLINK event.
  1749. */
  1750. static inline void rtmsg_iwinfo(struct net_device * dev,
  1751. char * event,
  1752. int event_len)
  1753. {
  1754. struct sk_buff *skb;
  1755. int size = NLMSG_GOODSIZE;
  1756. skb = alloc_skb(size, GFP_ATOMIC);
  1757. if (!skb)
  1758. return;
  1759. if (rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK,
  1760. event, event_len) < 0) {
  1761. kfree_skb(skb);
  1762. return;
  1763. }
  1764. NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
  1765. skb_queue_tail(&wireless_nlevent_queue, skb);
  1766. tasklet_schedule(&wireless_nlevent_tasklet);
  1767. }
  1768. #endif /* WE_EVENT_RTNETLINK */
  1769. /* ---------------------------------------------------------------- */
  1770. /*
  1771. * Main event dispatcher. Called from other parts and drivers.
  1772. * Send the event on the appropriate channels.
  1773. * May be called from interrupt context.
  1774. */
  1775. void wireless_send_event(struct net_device * dev,
  1776. unsigned int cmd,
  1777. union iwreq_data * wrqu,
  1778. char * extra)
  1779. {
  1780. const struct iw_ioctl_description * descr = NULL;
  1781. int extra_len = 0;
  1782. struct iw_event *event; /* Mallocated whole event */
  1783. int event_len; /* Its size */
  1784. int hdr_len; /* Size of the event header */
  1785. int wrqu_off = 0; /* Offset in wrqu */
  1786. /* Don't "optimise" the following variable, it will crash */
  1787. unsigned cmd_index; /* *MUST* be unsigned */
  1788. /* Get the description of the Event */
  1789. if (cmd <= SIOCIWLAST) {
  1790. cmd_index = cmd - SIOCIWFIRST;
  1791. if (cmd_index < standard_ioctl_num)
  1792. descr = &(standard_ioctl[cmd_index]);
  1793. } else {
  1794. cmd_index = cmd - IWEVFIRST;
  1795. if (cmd_index < standard_event_num)
  1796. descr = &(standard_event[cmd_index]);
  1797. }
  1798. /* Don't accept unknown events */
  1799. if (descr == NULL) {
  1800. /* Note : we don't return an error to the driver, because
  1801. * the driver would not know what to do about it. It can't
  1802. * return an error to the user, because the event is not
  1803. * initiated by a user request.
  1804. * The best the driver could do is to log an error message.
  1805. * We will do it ourselves instead...
  1806. */
  1807. printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
  1808. dev->name, cmd);
  1809. return;
  1810. }
  1811. #ifdef WE_EVENT_DEBUG
  1812. printk(KERN_DEBUG "%s (WE) : Got event 0x%04X\n",
  1813. dev->name, cmd);
  1814. 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);
  1815. #endif /* WE_EVENT_DEBUG */
  1816. /* Check extra parameters and set extra_len */
  1817. if (descr->header_type == IW_HEADER_TYPE_POINT) {
  1818. /* Check if number of token fits within bounds */
  1819. if (wrqu->data.length > descr->max_tokens) {
  1820. printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length);
  1821. return;
  1822. }
  1823. if (wrqu->data.length < descr->min_tokens) {
  1824. printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length);
  1825. return;
  1826. }
  1827. /* Calculate extra_len - extra is NULL for restricted events */
  1828. if (extra != NULL)
  1829. extra_len = wrqu->data.length * descr->token_size;
  1830. /* Always at an offset in wrqu */
  1831. wrqu_off = IW_EV_POINT_OFF;
  1832. #ifdef WE_EVENT_DEBUG
  1833. printk(KERN_DEBUG "%s (WE) : Event 0x%04X, tokens %d, extra_len %d\n", dev->name, cmd, wrqu->data.length, extra_len);
  1834. #endif /* WE_EVENT_DEBUG */
  1835. }
  1836. /* Total length of the event */
  1837. hdr_len = event_type_size[descr->header_type];
  1838. event_len = hdr_len + extra_len;
  1839. #ifdef WE_EVENT_DEBUG
  1840. 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);
  1841. #endif /* WE_EVENT_DEBUG */
  1842. /* Create temporary buffer to hold the event */
  1843. event = kmalloc(event_len, GFP_ATOMIC);
  1844. if (event == NULL)
  1845. return;
  1846. /* Fill event */
  1847. event->len = event_len;
  1848. event->cmd = cmd;
  1849. memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
  1850. if (extra != NULL)
  1851. memcpy(((char *) event) + hdr_len, extra, extra_len);
  1852. #ifdef WE_EVENT_RTNETLINK
  1853. /* Send via the RtNetlink event channel */
  1854. rtmsg_iwinfo(dev, (char *) event, event_len);
  1855. #endif /* WE_EVENT_RTNETLINK */
  1856. /* Cleanup */
  1857. kfree(event);
  1858. return; /* Always success, I guess ;-) */
  1859. }
  1860. /********************** ENHANCED IWSPY SUPPORT **********************/
  1861. /*
  1862. * In the old days, the driver was handling spy support all by itself.
  1863. * Now, the driver can delegate this task to Wireless Extensions.
  1864. * It needs to use those standard spy iw_handler in struct iw_handler_def,
  1865. * push data to us via wireless_spy_update() and include struct iw_spy_data
  1866. * in its private part (and export it in net_device->wireless_data->spy_data).
  1867. * One of the main advantage of centralising spy support here is that
  1868. * it becomes much easier to improve and extend it without having to touch
  1869. * the drivers. One example is the addition of the Spy-Threshold events.
  1870. */
  1871. /* ---------------------------------------------------------------- */
  1872. /*
  1873. * Return the pointer to the spy data in the driver.
  1874. * Because this is called on the Rx path via wireless_spy_update(),
  1875. * we want it to be efficient...
  1876. */
  1877. static inline struct iw_spy_data * get_spydata(struct net_device *dev)
  1878. {
  1879. /* This is the new way */
  1880. if (dev->wireless_data)
  1881. return(dev->wireless_data->spy_data);
  1882. return NULL;
  1883. }
  1884. /*------------------------------------------------------------------*/
  1885. /*
  1886. * Standard Wireless Handler : set Spy List
  1887. */
  1888. int iw_handler_set_spy(struct net_device * dev,
  1889. struct iw_request_info * info,
  1890. union iwreq_data * wrqu,
  1891. char * extra)
  1892. {
  1893. struct iw_spy_data * spydata = get_spydata(dev);
  1894. struct sockaddr * address = (struct sockaddr *) extra;
  1895. /* Make sure driver is not buggy or using the old API */
  1896. if (!spydata)
  1897. return -EOPNOTSUPP;
  1898. /* Disable spy collection while we copy the addresses.
  1899. * While we copy addresses, any call to wireless_spy_update()
  1900. * will NOP. This is OK, as anyway the addresses are changing. */
  1901. spydata->spy_number = 0;
  1902. /* We want to operate without locking, because wireless_spy_update()
  1903. * most likely will happen in the interrupt handler, and therefore
  1904. * have its own locking constraints and needs performance.
  1905. * The rtnl_lock() make sure we don't race with the other iw_handlers.
  1906. * This make sure wireless_spy_update() "see" that the spy list
  1907. * is temporarily disabled. */
  1908. smp_wmb();
  1909. /* Are there are addresses to copy? */
  1910. if (wrqu->data.length > 0) {
  1911. int i;
  1912. /* Copy addresses */
  1913. for (i = 0; i < wrqu->data.length; i++)
  1914. memcpy(spydata->spy_address[i], address[i].sa_data,
  1915. ETH_ALEN);
  1916. /* Reset stats */
  1917. memset(spydata->spy_stat, 0,
  1918. sizeof(struct iw_quality) * IW_MAX_SPY);
  1919. #ifdef WE_SPY_DEBUG
  1920. printk(KERN_DEBUG "iw_handler_set_spy() : wireless_data %p, spydata %p, num %d\n", dev->wireless_data, spydata, wrqu->data.length);
  1921. for (i = 0; i < wrqu->data.length; i++)
  1922. printk(KERN_DEBUG
  1923. "%02X:%02X:%02X:%02X:%02X:%02X \n",
  1924. spydata->spy_address[i][0],
  1925. spydata->spy_address[i][1],
  1926. spydata->spy_address[i][2],
  1927. spydata->spy_address[i][3],
  1928. spydata->spy_address[i][4],
  1929. spydata->spy_address[i][5]);
  1930. #endif /* WE_SPY_DEBUG */
  1931. }
  1932. /* Make sure above is updated before re-enabling */
  1933. smp_wmb();
  1934. /* Enable addresses */
  1935. spydata->spy_number = wrqu->data.length;
  1936. return 0;
  1937. }
  1938. /*------------------------------------------------------------------*/
  1939. /*
  1940. * Standard Wireless Handler : get Spy List
  1941. */
  1942. int iw_handler_get_spy(struct net_device * dev,
  1943. struct iw_request_info * info,
  1944. union iwreq_data * wrqu,
  1945. char * extra)
  1946. {
  1947. struct iw_spy_data * spydata = get_spydata(dev);
  1948. struct sockaddr * address = (struct sockaddr *) extra;
  1949. int i;
  1950. /* Make sure driver is not buggy or using the old API */
  1951. if (!spydata)
  1952. return -EOPNOTSUPP;
  1953. wrqu->data.length = spydata->spy_number;
  1954. /* Copy addresses. */
  1955. for (i = 0; i < spydata->spy_number; i++) {
  1956. memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
  1957. address[i].sa_family = AF_UNIX;
  1958. }
  1959. /* Copy stats to the user buffer (just after). */
  1960. if (spydata->spy_number > 0)
  1961. memcpy(extra + (sizeof(struct sockaddr) *spydata->spy_number),
  1962. spydata->spy_stat,
  1963. sizeof(struct iw_quality) * spydata->spy_number);
  1964. /* Reset updated flags. */
  1965. for (i = 0; i < spydata->spy_number; i++)
  1966. spydata->spy_stat[i].updated &= ~IW_QUAL_ALL_UPDATED;
  1967. return 0;
  1968. }
  1969. /*------------------------------------------------------------------*/
  1970. /*
  1971. * Standard Wireless Handler : set spy threshold
  1972. */
  1973. int iw_handler_set_thrspy(struct net_device * dev,
  1974. struct iw_request_info *info,
  1975. union iwreq_data * wrqu,
  1976. char * extra)
  1977. {
  1978. struct iw_spy_data * spydata = get_spydata(dev);
  1979. struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
  1980. /* Make sure driver is not buggy or using the old API */
  1981. if (!spydata)
  1982. return -EOPNOTSUPP;
  1983. /* Just do it */
  1984. memcpy(&(spydata->spy_thr_low), &(threshold->low),
  1985. 2 * sizeof(struct iw_quality));
  1986. /* Clear flag */
  1987. memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
  1988. #ifdef WE_SPY_DEBUG
  1989. printk(KERN_DEBUG "iw_handler_set_thrspy() : low %d ; high %d\n", spydata->spy_thr_low.level, spydata->spy_thr_high.level);
  1990. #endif /* WE_SPY_DEBUG */
  1991. return 0;
  1992. }
  1993. /*------------------------------------------------------------------*/
  1994. /*
  1995. * Standard Wireless Handler : get spy threshold
  1996. */
  1997. int iw_handler_get_thrspy(struct net_device * dev,
  1998. struct iw_request_info *info,
  1999. union iwreq_data * wrqu,
  2000. char * extra)
  2001. {
  2002. struct iw_spy_data * spydata = get_spydata(dev);
  2003. struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
  2004. /* Make sure driver is not buggy or using the old API */
  2005. if (!spydata)
  2006. return -EOPNOTSUPP;
  2007. /* Just do it */
  2008. memcpy(&(threshold->low), &(spydata->spy_thr_low),
  2009. 2 * sizeof(struct iw_quality));
  2010. return 0;
  2011. }
  2012. /*------------------------------------------------------------------*/
  2013. /*
  2014. * Prepare and send a Spy Threshold event
  2015. */
  2016. static void iw_send_thrspy_event(struct net_device * dev,
  2017. struct iw_spy_data * spydata,
  2018. unsigned char * address,
  2019. struct iw_quality * wstats)
  2020. {
  2021. union iwreq_data wrqu;
  2022. struct iw_thrspy threshold;
  2023. /* Init */
  2024. wrqu.data.length = 1;
  2025. wrqu.data.flags = 0;
  2026. /* Copy address */
  2027. memcpy(threshold.addr.sa_data, address, ETH_ALEN);
  2028. threshold.addr.sa_family = ARPHRD_ETHER;
  2029. /* Copy stats */
  2030. memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality));
  2031. /* Copy also thresholds */
  2032. memcpy(&(threshold.low), &(spydata->spy_thr_low),
  2033. 2 * sizeof(struct iw_quality));
  2034. #ifdef WE_SPY_DEBUG
  2035. printk(KERN_DEBUG "iw_send_thrspy_event() : address %02X:%02X:%02X:%02X:%02X:%02X, level %d, up = %d\n",
  2036. threshold.addr.sa_data[0],
  2037. threshold.addr.sa_data[1],
  2038. threshold.addr.sa_data[2],
  2039. threshold.addr.sa_data[3],
  2040. threshold.addr.sa_data[4],
  2041. threshold.addr.sa_data[5], threshold.qual.level);
  2042. #endif /* WE_SPY_DEBUG */
  2043. /* Send event to user space */
  2044. wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
  2045. }
  2046. /* ---------------------------------------------------------------- */
  2047. /*
  2048. * Call for the driver to update the spy data.
  2049. * For now, the spy data is a simple array. As the size of the array is
  2050. * small, this is good enough. If we wanted to support larger number of
  2051. * spy addresses, we should use something more efficient...
  2052. */
  2053. void wireless_spy_update(struct net_device * dev,
  2054. unsigned char * address,
  2055. struct iw_quality * wstats)
  2056. {
  2057. struct iw_spy_data * spydata = get_spydata(dev);
  2058. int i;
  2059. int match = -1;
  2060. /* Make sure driver is not buggy or using the old API */
  2061. if (!spydata)
  2062. return;
  2063. #ifdef WE_SPY_DEBUG
  2064. 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]);
  2065. #endif /* WE_SPY_DEBUG */
  2066. /* Update all records that match */
  2067. for (i = 0; i < spydata->spy_number; i++)
  2068. if (!compare_ether_addr(address, spydata->spy_address[i])) {
  2069. memcpy(&(spydata->spy_stat[i]), wstats,
  2070. sizeof(struct iw_quality));
  2071. match = i;
  2072. }
  2073. /* Generate an event if we cross the spy threshold.
  2074. * To avoid event storms, we have a simple hysteresis : we generate
  2075. * event only when we go under the low threshold or above the
  2076. * high threshold. */
  2077. if (match >= 0) {
  2078. if (spydata->spy_thr_under[match]) {
  2079. if (wstats->level > spydata->spy_thr_high.level) {
  2080. spydata->spy_thr_under[match] = 0;
  2081. iw_send_thrspy_event(dev, spydata,
  2082. address, wstats);
  2083. }
  2084. } else {
  2085. if (wstats->level < spydata->spy_thr_low.level) {
  2086. spydata->spy_thr_under[match] = 1;
  2087. iw_send_thrspy_event(dev, spydata,
  2088. address, wstats);
  2089. }
  2090. }
  2091. }
  2092. }
  2093. EXPORT_SYMBOL(iw_handler_get_spy);
  2094. EXPORT_SYMBOL(iw_handler_get_thrspy);
  2095. EXPORT_SYMBOL(iw_handler_set_spy);
  2096. EXPORT_SYMBOL(iw_handler_set_thrspy);
  2097. EXPORT_SYMBOL(wireless_send_event);
  2098. EXPORT_SYMBOL(wireless_spy_update);