wireless.c 70 KB

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