wireless.c 70 KB

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