wireless.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322
  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. /* Calculate space needed by arguments. Always allocate
  698. * for max space. Easier, and won't last long... */
  699. extra_size = descr->max_tokens * descr->token_size;
  700. /* Check what user space is giving us */
  701. if(IW_IS_SET(cmd)) {
  702. /* Check NULL pointer */
  703. if((iwr->u.data.pointer == NULL) &&
  704. (iwr->u.data.length != 0))
  705. return -EFAULT;
  706. /* Check if number of token fits within bounds */
  707. if(iwr->u.data.length > descr->max_tokens)
  708. return -E2BIG;
  709. if(iwr->u.data.length < descr->min_tokens)
  710. return -EINVAL;
  711. } else {
  712. /* Check NULL pointer */
  713. if(iwr->u.data.pointer == NULL)
  714. return -EFAULT;
  715. /* Save user space buffer size for checking */
  716. user_length = iwr->u.data.length;
  717. /* Don't check if user_length > max to allow forward
  718. * compatibility. The test user_length < min is
  719. * implied by the test at the end. */
  720. /* Support for very large requests */
  721. if((descr->flags & IW_DESCR_FLAG_NOMAX) &&
  722. (user_length > descr->max_tokens)) {
  723. /* Allow userspace to GET more than max so
  724. * we can support any size GET requests.
  725. * There is still a limit : -ENOMEM. */
  726. extra_size = user_length * descr->token_size;
  727. /* Note : user_length is originally a __u16,
  728. * and token_size is controlled by us,
  729. * so extra_size won't get negative and
  730. * won't overflow... */
  731. }
  732. }
  733. #ifdef WE_IOCTL_DEBUG
  734. printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
  735. dev->name, extra_size);
  736. #endif /* WE_IOCTL_DEBUG */
  737. /* Create the kernel buffer */
  738. extra = kmalloc(extra_size, GFP_KERNEL);
  739. if (extra == NULL) {
  740. return -ENOMEM;
  741. }
  742. /* If it is a SET, get all the extra data in here */
  743. if(IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
  744. err = copy_from_user(extra, iwr->u.data.pointer,
  745. iwr->u.data.length *
  746. descr->token_size);
  747. if (err) {
  748. kfree(extra);
  749. return -EFAULT;
  750. }
  751. #ifdef WE_IOCTL_DEBUG
  752. printk(KERN_DEBUG "%s (WE) : Got %d bytes\n",
  753. dev->name,
  754. iwr->u.data.length * descr->token_size);
  755. #endif /* WE_IOCTL_DEBUG */
  756. }
  757. /* Call the handler */
  758. ret = handler(dev, &info, &(iwr->u), extra);
  759. /* If we have something to return to the user */
  760. if (!ret && IW_IS_GET(cmd)) {
  761. /* Check if there is enough buffer up there */
  762. if(user_length < iwr->u.data.length) {
  763. kfree(extra);
  764. return -E2BIG;
  765. }
  766. err = copy_to_user(iwr->u.data.pointer, extra,
  767. iwr->u.data.length *
  768. descr->token_size);
  769. if (err)
  770. ret = -EFAULT;
  771. #ifdef WE_IOCTL_DEBUG
  772. printk(KERN_DEBUG "%s (WE) : Wrote %d bytes\n",
  773. dev->name,
  774. iwr->u.data.length * descr->token_size);
  775. #endif /* WE_IOCTL_DEBUG */
  776. }
  777. #ifdef WE_SET_EVENT
  778. /* Generate an event to notify listeners of the change */
  779. if((descr->flags & IW_DESCR_FLAG_EVENT) &&
  780. ((ret == 0) || (ret == -EIWCOMMIT))) {
  781. if(descr->flags & IW_DESCR_FLAG_RESTRICT)
  782. /* If the event is restricted, don't
  783. * export the payload */
  784. wireless_send_event(dev, cmd, &(iwr->u), NULL);
  785. else
  786. wireless_send_event(dev, cmd, &(iwr->u),
  787. extra);
  788. }
  789. #endif /* WE_SET_EVENT */
  790. /* Cleanup - I told you it wasn't that long ;-) */
  791. kfree(extra);
  792. }
  793. /* Call commit handler if needed and defined */
  794. if(ret == -EIWCOMMIT)
  795. ret = call_commit_handler(dev);
  796. /* Here, we will generate the appropriate event if needed */
  797. return ret;
  798. }
  799. /* ---------------------------------------------------------------- */
  800. /*
  801. * Wrapper to call a private Wireless Extension handler.
  802. * We do various checks and also take care of moving data between
  803. * user space and kernel space.
  804. * It's not as nice and slimline as the standard wrapper. The cause
  805. * is struct iw_priv_args, which was not really designed for the
  806. * job we are going here.
  807. *
  808. * IMPORTANT : This function prevent to set and get data on the same
  809. * IOCTL and enforce the SET/GET convention. Not doing it would be
  810. * far too hairy...
  811. * If you need to set and get data at the same time, please don't use
  812. * a iw_handler but process it in your ioctl handler (i.e. use the
  813. * old driver API).
  814. */
  815. static inline int ioctl_private_call(struct net_device * dev,
  816. struct ifreq * ifr,
  817. unsigned int cmd,
  818. iw_handler handler)
  819. {
  820. struct iwreq * iwr = (struct iwreq *) ifr;
  821. const struct iw_priv_args * descr = NULL;
  822. struct iw_request_info info;
  823. int extra_size = 0;
  824. int i;
  825. int ret = -EINVAL;
  826. /* Get the description of the IOCTL */
  827. for(i = 0; i < dev->wireless_handlers->num_private_args; i++)
  828. if(cmd == dev->wireless_handlers->private_args[i].cmd) {
  829. descr = &(dev->wireless_handlers->private_args[i]);
  830. break;
  831. }
  832. #ifdef WE_IOCTL_DEBUG
  833. printk(KERN_DEBUG "%s (WE) : Found private handler for 0x%04X\n",
  834. ifr->ifr_name, cmd);
  835. if(descr) {
  836. printk(KERN_DEBUG "%s (WE) : Name %s, set %X, get %X\n",
  837. dev->name, descr->name,
  838. descr->set_args, descr->get_args);
  839. }
  840. #endif /* WE_IOCTL_DEBUG */
  841. /* Compute the size of the set/get arguments */
  842. if(descr != NULL) {
  843. if(IW_IS_SET(cmd)) {
  844. int offset = 0; /* For sub-ioctls */
  845. /* Check for sub-ioctl handler */
  846. if(descr->name[0] == '\0')
  847. /* Reserve one int for sub-ioctl index */
  848. offset = sizeof(__u32);
  849. /* Size of set arguments */
  850. extra_size = get_priv_size(descr->set_args);
  851. /* Does it fits in iwr ? */
  852. if((descr->set_args & IW_PRIV_SIZE_FIXED) &&
  853. ((extra_size + offset) <= IFNAMSIZ))
  854. extra_size = 0;
  855. } else {
  856. /* Size of get arguments */
  857. extra_size = get_priv_size(descr->get_args);
  858. /* Does it fits in iwr ? */
  859. if((descr->get_args & IW_PRIV_SIZE_FIXED) &&
  860. (extra_size <= IFNAMSIZ))
  861. extra_size = 0;
  862. }
  863. }
  864. /* Prepare the call */
  865. info.cmd = cmd;
  866. info.flags = 0;
  867. /* Check if we have a pointer to user space data or not. */
  868. if(extra_size == 0) {
  869. /* No extra arguments. Trivial to handle */
  870. ret = handler(dev, &info, &(iwr->u), (char *) &(iwr->u));
  871. } else {
  872. char * extra;
  873. int err;
  874. /* Check what user space is giving us */
  875. if(IW_IS_SET(cmd)) {
  876. /* Check NULL pointer */
  877. if((iwr->u.data.pointer == NULL) &&
  878. (iwr->u.data.length != 0))
  879. return -EFAULT;
  880. /* Does it fits within bounds ? */
  881. if(iwr->u.data.length > (descr->set_args &
  882. IW_PRIV_SIZE_MASK))
  883. return -E2BIG;
  884. } else {
  885. /* Check NULL pointer */
  886. if(iwr->u.data.pointer == NULL)
  887. return -EFAULT;
  888. }
  889. #ifdef WE_IOCTL_DEBUG
  890. printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
  891. dev->name, extra_size);
  892. #endif /* WE_IOCTL_DEBUG */
  893. /* Always allocate for max space. Easier, and won't last
  894. * long... */
  895. extra = kmalloc(extra_size, GFP_KERNEL);
  896. if (extra == NULL) {
  897. return -ENOMEM;
  898. }
  899. /* If it is a SET, get all the extra data in here */
  900. if(IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
  901. err = copy_from_user(extra, iwr->u.data.pointer,
  902. extra_size);
  903. if (err) {
  904. kfree(extra);
  905. return -EFAULT;
  906. }
  907. #ifdef WE_IOCTL_DEBUG
  908. printk(KERN_DEBUG "%s (WE) : Got %d elem\n",
  909. dev->name, iwr->u.data.length);
  910. #endif /* WE_IOCTL_DEBUG */
  911. }
  912. /* Call the handler */
  913. ret = handler(dev, &info, &(iwr->u), extra);
  914. /* If we have something to return to the user */
  915. if (!ret && IW_IS_GET(cmd)) {
  916. /* Adjust for the actual length if it's variable,
  917. * avoid leaking kernel bits outside. */
  918. if (!(descr->get_args & IW_PRIV_SIZE_FIXED)) {
  919. extra_size = adjust_priv_size(descr->get_args,
  920. &(iwr->u));
  921. }
  922. err = copy_to_user(iwr->u.data.pointer, extra,
  923. extra_size);
  924. if (err)
  925. ret = -EFAULT;
  926. #ifdef WE_IOCTL_DEBUG
  927. printk(KERN_DEBUG "%s (WE) : Wrote %d elem\n",
  928. dev->name, iwr->u.data.length);
  929. #endif /* WE_IOCTL_DEBUG */
  930. }
  931. /* Cleanup - I told you it wasn't that long ;-) */
  932. kfree(extra);
  933. }
  934. /* Call commit handler if needed and defined */
  935. if(ret == -EIWCOMMIT)
  936. ret = call_commit_handler(dev);
  937. return ret;
  938. }
  939. /* ---------------------------------------------------------------- */
  940. /*
  941. * Main IOCTl dispatcher. Called from the main networking code
  942. * (dev_ioctl() in net/core/dev.c).
  943. * Check the type of IOCTL and call the appropriate wrapper...
  944. */
  945. int wireless_process_ioctl(struct ifreq *ifr, unsigned int cmd)
  946. {
  947. struct net_device *dev;
  948. iw_handler handler;
  949. /* Permissions are already checked in dev_ioctl() before calling us.
  950. * The copy_to/from_user() of ifr is also dealt with in there */
  951. /* Make sure the device exist */
  952. if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL)
  953. return -ENODEV;
  954. /* A bunch of special cases, then the generic case...
  955. * Note that 'cmd' is already filtered in dev_ioctl() with
  956. * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
  957. switch(cmd)
  958. {
  959. case SIOCGIWSTATS:
  960. /* Get Wireless Stats */
  961. return ioctl_standard_call(dev,
  962. ifr,
  963. cmd,
  964. &iw_handler_get_iwstats);
  965. case SIOCGIWPRIV:
  966. /* Check if we have some wireless handlers defined */
  967. if(dev->wireless_handlers != NULL) {
  968. /* We export to user space the definition of
  969. * the private handler ourselves */
  970. return ioctl_standard_call(dev,
  971. ifr,
  972. cmd,
  973. &iw_handler_get_private);
  974. }
  975. // ## Fall-through for old API ##
  976. default:
  977. /* Generic IOCTL */
  978. /* Basic check */
  979. if (!netif_device_present(dev))
  980. return -ENODEV;
  981. /* New driver API : try to find the handler */
  982. handler = get_handler(dev, cmd);
  983. if(handler != NULL) {
  984. /* Standard and private are not the same */
  985. if(cmd < SIOCIWFIRSTPRIV)
  986. return ioctl_standard_call(dev,
  987. ifr,
  988. cmd,
  989. handler);
  990. else
  991. return ioctl_private_call(dev,
  992. ifr,
  993. cmd,
  994. handler);
  995. }
  996. /* Old driver API : call driver ioctl handler */
  997. if (dev->do_ioctl) {
  998. return dev->do_ioctl(dev, ifr, cmd);
  999. }
  1000. return -EOPNOTSUPP;
  1001. }
  1002. /* Not reached */
  1003. return -EINVAL;
  1004. }
  1005. /********************** RTNETLINK REQUEST API **********************/
  1006. /*
  1007. * The alternate user space API to configure all those Wireless Extensions
  1008. * is through RtNetlink.
  1009. * This API support only the new driver API (iw_handler).
  1010. *
  1011. * This RtNetlink API use the same query/reply model as the ioctl API.
  1012. * Maximum effort has been done to fit in the RtNetlink model, and
  1013. * we support both RtNetlink Set and RtNelink Get operations.
  1014. * On the other hand, we don't offer Dump operations because of the
  1015. * following reasons :
  1016. * o Large number of parameters, most optional
  1017. * o Large size of some parameters (> 100 bytes)
  1018. * o Each parameters need to be extracted from hardware
  1019. * o Scan requests can take seconds and disable network activity.
  1020. * Because of this high cost/overhead, we want to return only the
  1021. * parameters the user application is really interested in.
  1022. * We could offer partial Dump using the IW_DESCR_FLAG_DUMP flag.
  1023. *
  1024. * The API uses the standard RtNetlink socket. When the RtNetlink code
  1025. * find a IFLA_WIRELESS field in a RtNetlink SET_LINK request,
  1026. * it calls here.
  1027. */
  1028. #ifdef CONFIG_NET_WIRELESS_RTNETLINK
  1029. /* ---------------------------------------------------------------- */
  1030. /*
  1031. * Wrapper to call a standard Wireless Extension GET handler.
  1032. * We do various checks and call the handler with the proper args.
  1033. */
  1034. static int rtnetlink_standard_get(struct net_device * dev,
  1035. struct iw_event * request,
  1036. int request_len,
  1037. iw_handler handler,
  1038. char ** p_buf,
  1039. int * p_len)
  1040. {
  1041. const struct iw_ioctl_description * descr = NULL;
  1042. unsigned int cmd;
  1043. union iwreq_data * wrqu;
  1044. int hdr_len;
  1045. struct iw_request_info info;
  1046. char * buffer = NULL;
  1047. int buffer_size = 0;
  1048. int ret = -EINVAL;
  1049. /* Get the description of the Request */
  1050. cmd = request->cmd;
  1051. if((cmd - SIOCIWFIRST) >= standard_ioctl_num)
  1052. return -EOPNOTSUPP;
  1053. descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
  1054. #ifdef WE_RTNETLINK_DEBUG
  1055. printk(KERN_DEBUG "%s (WE.r) : Found standard handler for 0x%04X\n",
  1056. dev->name, cmd);
  1057. 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);
  1058. #endif /* WE_RTNETLINK_DEBUG */
  1059. /* Check if wrqu is complete */
  1060. hdr_len = event_type_size[descr->header_type];
  1061. if(request_len < hdr_len) {
  1062. #ifdef WE_RTNETLINK_DEBUG
  1063. printk(KERN_DEBUG
  1064. "%s (WE.r) : Wireless request too short (%d)\n",
  1065. dev->name, request_len);
  1066. #endif /* WE_RTNETLINK_DEBUG */
  1067. return -EINVAL;
  1068. }
  1069. /* Prepare the call */
  1070. info.cmd = cmd;
  1071. info.flags = 0;
  1072. /* Check if we have extra data in the reply or not */
  1073. if(descr->header_type != IW_HEADER_TYPE_POINT) {
  1074. /* Create the kernel buffer that we will return.
  1075. * It's at an offset to match the TYPE_POINT case... */
  1076. buffer_size = request_len + IW_EV_POINT_OFF;
  1077. buffer = kmalloc(buffer_size, GFP_KERNEL);
  1078. if (buffer == NULL) {
  1079. return -ENOMEM;
  1080. }
  1081. /* Copy event data */
  1082. memcpy(buffer + IW_EV_POINT_OFF, request, request_len);
  1083. /* Use our own copy of wrqu */
  1084. wrqu = (union iwreq_data *) (buffer + IW_EV_POINT_OFF
  1085. + IW_EV_LCP_LEN);
  1086. /* No extra arguments. Trivial to handle */
  1087. ret = handler(dev, &info, wrqu, NULL);
  1088. } else {
  1089. union iwreq_data wrqu_point;
  1090. char * extra = NULL;
  1091. int extra_size = 0;
  1092. /* Get a temp copy of wrqu (skip pointer) */
  1093. memcpy(((char *) &wrqu_point) + IW_EV_POINT_OFF,
  1094. ((char *) request) + IW_EV_LCP_LEN,
  1095. IW_EV_POINT_LEN - IW_EV_LCP_LEN);
  1096. /* Calculate space needed by arguments. Always allocate
  1097. * for max space. Easier, and won't last long... */
  1098. extra_size = descr->max_tokens * descr->token_size;
  1099. /* Support for very large requests */
  1100. if((descr->flags & IW_DESCR_FLAG_NOMAX) &&
  1101. (wrqu_point.data.length > descr->max_tokens))
  1102. extra_size = (wrqu_point.data.length
  1103. * descr->token_size);
  1104. buffer_size = extra_size + IW_EV_POINT_LEN + IW_EV_POINT_OFF;
  1105. #ifdef WE_RTNETLINK_DEBUG
  1106. printk(KERN_DEBUG "%s (WE.r) : Malloc %d bytes (%d bytes)\n",
  1107. dev->name, extra_size, buffer_size);
  1108. #endif /* WE_RTNETLINK_DEBUG */
  1109. /* Create the kernel buffer that we will return */
  1110. buffer = kmalloc(buffer_size, GFP_KERNEL);
  1111. if (buffer == NULL) {
  1112. return -ENOMEM;
  1113. }
  1114. /* Put wrqu in the right place (just before extra).
  1115. * Leave space for IWE header and dummy pointer...
  1116. * Note that IW_EV_LCP_LEN==4 bytes, so it's still aligned...
  1117. */
  1118. memcpy(buffer + IW_EV_LCP_LEN + IW_EV_POINT_OFF,
  1119. ((char *) &wrqu_point) + IW_EV_POINT_OFF,
  1120. IW_EV_POINT_LEN - IW_EV_LCP_LEN);
  1121. wrqu = (union iwreq_data *) (buffer + IW_EV_LCP_LEN);
  1122. /* Extra comes logically after that. Offset +12 bytes. */
  1123. extra = buffer + IW_EV_POINT_OFF + IW_EV_POINT_LEN;
  1124. /* Call the handler */
  1125. ret = handler(dev, &info, wrqu, extra);
  1126. /* Calculate real returned length */
  1127. extra_size = (wrqu->data.length * descr->token_size);
  1128. /* Re-adjust reply size */
  1129. request->len = extra_size + IW_EV_POINT_LEN;
  1130. /* Put the iwe header where it should, i.e. scrap the
  1131. * dummy pointer. */
  1132. memcpy(buffer + IW_EV_POINT_OFF, request, IW_EV_LCP_LEN);
  1133. #ifdef WE_RTNETLINK_DEBUG
  1134. 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);
  1135. #endif /* WE_RTNETLINK_DEBUG */
  1136. /* Check if there is enough buffer up there */
  1137. if(wrqu_point.data.length < wrqu->data.length)
  1138. ret = -E2BIG;
  1139. }
  1140. /* Return the buffer to the caller */
  1141. if (!ret) {
  1142. *p_buf = buffer;
  1143. *p_len = request->len;
  1144. } else {
  1145. /* Cleanup */
  1146. if(buffer)
  1147. kfree(buffer);
  1148. }
  1149. return ret;
  1150. }
  1151. /* ---------------------------------------------------------------- */
  1152. /*
  1153. * Wrapper to call a standard Wireless Extension SET handler.
  1154. * We do various checks and call the handler with the proper args.
  1155. */
  1156. static inline int rtnetlink_standard_set(struct net_device * dev,
  1157. struct iw_event * request,
  1158. int request_len,
  1159. iw_handler handler)
  1160. {
  1161. const struct iw_ioctl_description * descr = NULL;
  1162. unsigned int cmd;
  1163. union iwreq_data * wrqu;
  1164. union iwreq_data wrqu_point;
  1165. int hdr_len;
  1166. char * extra = NULL;
  1167. int extra_size = 0;
  1168. struct iw_request_info info;
  1169. int ret = -EINVAL;
  1170. /* Get the description of the Request */
  1171. cmd = request->cmd;
  1172. if((cmd - SIOCIWFIRST) >= standard_ioctl_num)
  1173. return -EOPNOTSUPP;
  1174. descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
  1175. #ifdef WE_RTNETLINK_DEBUG
  1176. printk(KERN_DEBUG "%s (WE.r) : Found standard SET handler for 0x%04X\n",
  1177. dev->name, cmd);
  1178. 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);
  1179. #endif /* WE_RTNETLINK_DEBUG */
  1180. /* Extract fixed header from request. This is properly aligned. */
  1181. wrqu = &request->u;
  1182. /* Check if wrqu is complete */
  1183. hdr_len = event_type_size[descr->header_type];
  1184. if(request_len < hdr_len) {
  1185. #ifdef WE_RTNETLINK_DEBUG
  1186. printk(KERN_DEBUG
  1187. "%s (WE.r) : Wireless request too short (%d)\n",
  1188. dev->name, request_len);
  1189. #endif /* WE_RTNETLINK_DEBUG */
  1190. return -EINVAL;
  1191. }
  1192. /* Prepare the call */
  1193. info.cmd = cmd;
  1194. info.flags = 0;
  1195. /* Check if we have extra data in the request or not */
  1196. if(descr->header_type != IW_HEADER_TYPE_POINT) {
  1197. /* No extra arguments. Trivial to handle */
  1198. ret = handler(dev, &info, wrqu, NULL);
  1199. } else {
  1200. int extra_len;
  1201. /* Put wrqu in the right place (skip pointer) */
  1202. memcpy(((char *) &wrqu_point) + IW_EV_POINT_OFF,
  1203. wrqu, IW_EV_POINT_LEN - IW_EV_LCP_LEN);
  1204. /* Don't forget about the event code... */
  1205. wrqu = &wrqu_point;
  1206. /* Check if number of token fits within bounds */
  1207. if(wrqu_point.data.length > descr->max_tokens)
  1208. return -E2BIG;
  1209. if(wrqu_point.data.length < descr->min_tokens)
  1210. return -EINVAL;
  1211. /* Real length of payload */
  1212. extra_len = wrqu_point.data.length * descr->token_size;
  1213. /* Check if request is self consistent */
  1214. if((request_len - hdr_len) < extra_len) {
  1215. #ifdef WE_RTNETLINK_DEBUG
  1216. printk(KERN_DEBUG "%s (WE.r) : Wireless request data too short (%d)\n",
  1217. dev->name, extra_size);
  1218. #endif /* WE_RTNETLINK_DEBUG */
  1219. return -EINVAL;
  1220. }
  1221. #ifdef WE_RTNETLINK_DEBUG
  1222. printk(KERN_DEBUG "%s (WE.r) : Malloc %d bytes\n",
  1223. dev->name, extra_size);
  1224. #endif /* WE_RTNETLINK_DEBUG */
  1225. /* Always allocate for max space. Easier, and won't last
  1226. * long... */
  1227. extra_size = descr->max_tokens * descr->token_size;
  1228. extra = kmalloc(extra_size, GFP_KERNEL);
  1229. if (extra == NULL)
  1230. return -ENOMEM;
  1231. /* Copy extra in aligned buffer */
  1232. memcpy(extra, ((char *) request) + hdr_len, extra_len);
  1233. /* Call the handler */
  1234. ret = handler(dev, &info, &wrqu_point, extra);
  1235. }
  1236. #ifdef WE_SET_EVENT
  1237. /* Generate an event to notify listeners of the change */
  1238. if((descr->flags & IW_DESCR_FLAG_EVENT) &&
  1239. ((ret == 0) || (ret == -EIWCOMMIT))) {
  1240. if(descr->flags & IW_DESCR_FLAG_RESTRICT)
  1241. /* If the event is restricted, don't
  1242. * export the payload */
  1243. wireless_send_event(dev, cmd, wrqu, NULL);
  1244. else
  1245. wireless_send_event(dev, cmd, wrqu, extra);
  1246. }
  1247. #endif /* WE_SET_EVENT */
  1248. /* Cleanup - I told you it wasn't that long ;-) */
  1249. if(extra)
  1250. kfree(extra);
  1251. /* Call commit handler if needed and defined */
  1252. if(ret == -EIWCOMMIT)
  1253. ret = call_commit_handler(dev);
  1254. return ret;
  1255. }
  1256. /* ---------------------------------------------------------------- */
  1257. /*
  1258. * Wrapper to call a private Wireless Extension GET handler.
  1259. * Same as above...
  1260. * It's not as nice and slimline as the standard wrapper. The cause
  1261. * is struct iw_priv_args, which was not really designed for the
  1262. * job we are going here.
  1263. *
  1264. * IMPORTANT : This function prevent to set and get data on the same
  1265. * IOCTL and enforce the SET/GET convention. Not doing it would be
  1266. * far too hairy...
  1267. * If you need to set and get data at the same time, please don't use
  1268. * a iw_handler but process it in your ioctl handler (i.e. use the
  1269. * old driver API).
  1270. */
  1271. static inline int rtnetlink_private_get(struct net_device * dev,
  1272. struct iw_event * request,
  1273. int request_len,
  1274. iw_handler handler,
  1275. char ** p_buf,
  1276. int * p_len)
  1277. {
  1278. const struct iw_priv_args * descr = NULL;
  1279. unsigned int cmd;
  1280. union iwreq_data * wrqu;
  1281. int hdr_len;
  1282. struct iw_request_info info;
  1283. int extra_size = 0;
  1284. int i;
  1285. char * buffer = NULL;
  1286. int buffer_size = 0;
  1287. int ret = -EINVAL;
  1288. /* Get the description of the Request */
  1289. cmd = request->cmd;
  1290. for(i = 0; i < dev->wireless_handlers->num_private_args; i++)
  1291. if(cmd == dev->wireless_handlers->private_args[i].cmd) {
  1292. descr = &(dev->wireless_handlers->private_args[i]);
  1293. break;
  1294. }
  1295. if(descr == NULL)
  1296. return -EOPNOTSUPP;
  1297. #ifdef WE_RTNETLINK_DEBUG
  1298. printk(KERN_DEBUG "%s (WE.r) : Found private handler for 0x%04X\n",
  1299. dev->name, cmd);
  1300. printk(KERN_DEBUG "%s (WE.r) : Name %s, set %X, get %X\n",
  1301. dev->name, descr->name, descr->set_args, descr->get_args);
  1302. #endif /* WE_RTNETLINK_DEBUG */
  1303. /* Compute the max size of the get arguments */
  1304. extra_size = get_priv_size(descr->get_args);
  1305. /* Does it fits in wrqu ? */
  1306. if((descr->get_args & IW_PRIV_SIZE_FIXED) &&
  1307. (extra_size <= IFNAMSIZ)) {
  1308. hdr_len = extra_size;
  1309. extra_size = 0;
  1310. } else {
  1311. hdr_len = IW_EV_POINT_LEN;
  1312. }
  1313. /* Check if wrqu is complete */
  1314. if(request_len < hdr_len) {
  1315. #ifdef WE_RTNETLINK_DEBUG
  1316. printk(KERN_DEBUG
  1317. "%s (WE.r) : Wireless request too short (%d)\n",
  1318. dev->name, request_len);
  1319. #endif /* WE_RTNETLINK_DEBUG */
  1320. return -EINVAL;
  1321. }
  1322. /* Prepare the call */
  1323. info.cmd = cmd;
  1324. info.flags = 0;
  1325. /* Check if we have a pointer to user space data or not. */
  1326. if(extra_size == 0) {
  1327. /* Create the kernel buffer that we will return.
  1328. * It's at an offset to match the TYPE_POINT case... */
  1329. buffer_size = request_len + IW_EV_POINT_OFF;
  1330. buffer = kmalloc(buffer_size, GFP_KERNEL);
  1331. if (buffer == NULL) {
  1332. return -ENOMEM;
  1333. }
  1334. /* Copy event data */
  1335. memcpy(buffer + IW_EV_POINT_OFF, request, request_len);
  1336. /* Use our own copy of wrqu */
  1337. wrqu = (union iwreq_data *) (buffer + IW_EV_POINT_OFF
  1338. + IW_EV_LCP_LEN);
  1339. /* No extra arguments. Trivial to handle */
  1340. ret = handler(dev, &info, wrqu, (char *) wrqu);
  1341. } else {
  1342. char * extra;
  1343. /* Buffer for full reply */
  1344. buffer_size = extra_size + IW_EV_POINT_LEN + IW_EV_POINT_OFF;
  1345. #ifdef WE_RTNETLINK_DEBUG
  1346. printk(KERN_DEBUG "%s (WE.r) : Malloc %d bytes (%d bytes)\n",
  1347. dev->name, extra_size, buffer_size);
  1348. #endif /* WE_RTNETLINK_DEBUG */
  1349. /* Create the kernel buffer that we will return */
  1350. buffer = kmalloc(buffer_size, GFP_KERNEL);
  1351. if (buffer == NULL) {
  1352. return -ENOMEM;
  1353. }
  1354. /* Put wrqu in the right place (just before extra).
  1355. * Leave space for IWE header and dummy pointer...
  1356. * Note that IW_EV_LCP_LEN==4 bytes, so it's still aligned...
  1357. */
  1358. memcpy(buffer + IW_EV_LCP_LEN + IW_EV_POINT_OFF,
  1359. ((char *) request) + IW_EV_LCP_LEN,
  1360. IW_EV_POINT_LEN - IW_EV_LCP_LEN);
  1361. wrqu = (union iwreq_data *) (buffer + IW_EV_LCP_LEN);
  1362. /* Extra comes logically after that. Offset +12 bytes. */
  1363. extra = buffer + IW_EV_POINT_OFF + IW_EV_POINT_LEN;
  1364. /* Call the handler */
  1365. ret = handler(dev, &info, wrqu, extra);
  1366. /* Adjust for the actual length if it's variable,
  1367. * avoid leaking kernel bits outside. */
  1368. if (!(descr->get_args & IW_PRIV_SIZE_FIXED))
  1369. extra_size = adjust_priv_size(descr->get_args, wrqu);
  1370. /* Re-adjust reply size */
  1371. request->len = extra_size + IW_EV_POINT_LEN;
  1372. /* Put the iwe header where it should, i.e. scrap the
  1373. * dummy pointer. */
  1374. memcpy(buffer + IW_EV_POINT_OFF, request, IW_EV_LCP_LEN);
  1375. #ifdef WE_RTNETLINK_DEBUG
  1376. 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);
  1377. #endif /* WE_RTNETLINK_DEBUG */
  1378. }
  1379. /* Return the buffer to the caller */
  1380. if (!ret) {
  1381. *p_buf = buffer;
  1382. *p_len = request->len;
  1383. } else {
  1384. /* Cleanup */
  1385. if(buffer)
  1386. kfree(buffer);
  1387. }
  1388. return ret;
  1389. }
  1390. /* ---------------------------------------------------------------- */
  1391. /*
  1392. * Wrapper to call a private Wireless Extension SET handler.
  1393. * Same as above...
  1394. * It's not as nice and slimline as the standard wrapper. The cause
  1395. * is struct iw_priv_args, which was not really designed for the
  1396. * job we are going here.
  1397. *
  1398. * IMPORTANT : This function prevent to set and get data on the same
  1399. * IOCTL and enforce the SET/GET convention. Not doing it would be
  1400. * far too hairy...
  1401. * If you need to set and get data at the same time, please don't use
  1402. * a iw_handler but process it in your ioctl handler (i.e. use the
  1403. * old driver API).
  1404. */
  1405. static inline int rtnetlink_private_set(struct net_device * dev,
  1406. struct iw_event * request,
  1407. int request_len,
  1408. iw_handler handler)
  1409. {
  1410. const struct iw_priv_args * descr = NULL;
  1411. unsigned int cmd;
  1412. union iwreq_data * wrqu;
  1413. union iwreq_data wrqu_point;
  1414. int hdr_len;
  1415. char * extra = NULL;
  1416. int extra_size = 0;
  1417. int offset = 0; /* For sub-ioctls */
  1418. struct iw_request_info info;
  1419. int i;
  1420. int ret = -EINVAL;
  1421. /* Get the description of the Request */
  1422. cmd = request->cmd;
  1423. for(i = 0; i < dev->wireless_handlers->num_private_args; i++)
  1424. if(cmd == dev->wireless_handlers->private_args[i].cmd) {
  1425. descr = &(dev->wireless_handlers->private_args[i]);
  1426. break;
  1427. }
  1428. if(descr == NULL)
  1429. return -EOPNOTSUPP;
  1430. #ifdef WE_RTNETLINK_DEBUG
  1431. printk(KERN_DEBUG "%s (WE.r) : Found private handler for 0x%04X\n",
  1432. ifr->ifr_name, cmd);
  1433. printk(KERN_DEBUG "%s (WE.r) : Name %s, set %X, get %X\n",
  1434. dev->name, descr->name, descr->set_args, descr->get_args);
  1435. #endif /* WE_RTNETLINK_DEBUG */
  1436. /* Compute the size of the set arguments */
  1437. /* Check for sub-ioctl handler */
  1438. if(descr->name[0] == '\0')
  1439. /* Reserve one int for sub-ioctl index */
  1440. offset = sizeof(__u32);
  1441. /* Size of set arguments */
  1442. extra_size = get_priv_size(descr->set_args);
  1443. /* Does it fits in wrqu ? */
  1444. if((descr->set_args & IW_PRIV_SIZE_FIXED) &&
  1445. (extra_size <= IFNAMSIZ)) {
  1446. hdr_len = IW_EV_LCP_LEN + extra_size;
  1447. extra_size = 0;
  1448. } else {
  1449. hdr_len = IW_EV_POINT_LEN;
  1450. }
  1451. /* Extract fixed header from request. This is properly aligned. */
  1452. wrqu = &request->u;
  1453. /* Check if wrqu is complete */
  1454. if(request_len < hdr_len) {
  1455. #ifdef WE_RTNETLINK_DEBUG
  1456. printk(KERN_DEBUG
  1457. "%s (WE.r) : Wireless request too short (%d)\n",
  1458. dev->name, request_len);
  1459. #endif /* WE_RTNETLINK_DEBUG */
  1460. return -EINVAL;
  1461. }
  1462. /* Prepare the call */
  1463. info.cmd = cmd;
  1464. info.flags = 0;
  1465. /* Check if we have a pointer to user space data or not. */
  1466. if(extra_size == 0) {
  1467. /* No extra arguments. Trivial to handle */
  1468. ret = handler(dev, &info, wrqu, (char *) wrqu);
  1469. } else {
  1470. int extra_len;
  1471. /* Put wrqu in the right place (skip pointer) */
  1472. memcpy(((char *) &wrqu_point) + IW_EV_POINT_OFF,
  1473. wrqu, IW_EV_POINT_LEN - IW_EV_LCP_LEN);
  1474. /* Does it fits within bounds ? */
  1475. if(wrqu_point.data.length > (descr->set_args &
  1476. IW_PRIV_SIZE_MASK))
  1477. return -E2BIG;
  1478. /* Real length of payload */
  1479. extra_len = adjust_priv_size(descr->set_args, &wrqu_point);
  1480. /* Check if request is self consistent */
  1481. if((request_len - hdr_len) < extra_len) {
  1482. #ifdef WE_RTNETLINK_DEBUG
  1483. printk(KERN_DEBUG "%s (WE.r) : Wireless request data too short (%d)\n",
  1484. dev->name, extra_size);
  1485. #endif /* WE_RTNETLINK_DEBUG */
  1486. return -EINVAL;
  1487. }
  1488. #ifdef WE_RTNETLINK_DEBUG
  1489. printk(KERN_DEBUG "%s (WE.r) : Malloc %d bytes\n",
  1490. dev->name, extra_size);
  1491. #endif /* WE_RTNETLINK_DEBUG */
  1492. /* Always allocate for max space. Easier, and won't last
  1493. * long... */
  1494. extra = kmalloc(extra_size, GFP_KERNEL);
  1495. if (extra == NULL)
  1496. return -ENOMEM;
  1497. /* Copy extra in aligned buffer */
  1498. memcpy(extra, ((char *) request) + hdr_len, extra_len);
  1499. /* Call the handler */
  1500. ret = handler(dev, &info, &wrqu_point, extra);
  1501. /* Cleanup - I told you it wasn't that long ;-) */
  1502. kfree(extra);
  1503. }
  1504. /* Call commit handler if needed and defined */
  1505. if(ret == -EIWCOMMIT)
  1506. ret = call_commit_handler(dev);
  1507. return ret;
  1508. }
  1509. /* ---------------------------------------------------------------- */
  1510. /*
  1511. * Main RtNetlink dispatcher. Called from the main networking code
  1512. * (do_getlink() in net/core/rtnetlink.c).
  1513. * Check the type of Request and call the appropriate wrapper...
  1514. */
  1515. int wireless_rtnetlink_get(struct net_device * dev,
  1516. char * data,
  1517. int len,
  1518. char ** p_buf,
  1519. int * p_len)
  1520. {
  1521. struct iw_event * request = (struct iw_event *) data;
  1522. iw_handler handler;
  1523. /* Check length */
  1524. if(len < IW_EV_LCP_LEN) {
  1525. printk(KERN_DEBUG "%s (WE.r) : RtNetlink request too short (%d)\n",
  1526. dev->name, len);
  1527. return -EINVAL;
  1528. }
  1529. /* ReCheck length (len may have padding) */
  1530. if(request->len > len) {
  1531. printk(KERN_DEBUG "%s (WE.r) : RtNetlink request len invalid (%d-%d)\n",
  1532. dev->name, request->len, len);
  1533. return -EINVAL;
  1534. }
  1535. /* Only accept GET requests in here */
  1536. if(!IW_IS_GET(request->cmd))
  1537. return -EOPNOTSUPP;
  1538. /* If command is `get the encoding parameters', check if
  1539. * the user has the right to do it */
  1540. if (request->cmd == SIOCGIWENCODE ||
  1541. request->cmd == SIOCGIWENCODEEXT) {
  1542. if (!capable(CAP_NET_ADMIN))
  1543. return -EPERM;
  1544. }
  1545. /* Special cases */
  1546. if(request->cmd == SIOCGIWSTATS)
  1547. /* Get Wireless Stats */
  1548. return rtnetlink_standard_get(dev,
  1549. request,
  1550. request->len,
  1551. &iw_handler_get_iwstats,
  1552. p_buf, p_len);
  1553. if(request->cmd == SIOCGIWPRIV) {
  1554. /* Check if we have some wireless handlers defined */
  1555. if(dev->wireless_handlers == NULL)
  1556. return -EOPNOTSUPP;
  1557. /* Get Wireless Stats */
  1558. return rtnetlink_standard_get(dev,
  1559. request,
  1560. request->len,
  1561. &iw_handler_get_private,
  1562. p_buf, p_len);
  1563. }
  1564. /* Basic check */
  1565. if (!netif_device_present(dev))
  1566. return -ENODEV;
  1567. /* Try to find the handler */
  1568. handler = get_handler(dev, request->cmd);
  1569. if(handler != NULL) {
  1570. /* Standard and private are not the same */
  1571. if(request->cmd < SIOCIWFIRSTPRIV)
  1572. return rtnetlink_standard_get(dev,
  1573. request,
  1574. request->len,
  1575. handler,
  1576. p_buf, p_len);
  1577. else
  1578. return rtnetlink_private_get(dev,
  1579. request,
  1580. request->len,
  1581. handler,
  1582. p_buf, p_len);
  1583. }
  1584. return -EOPNOTSUPP;
  1585. }
  1586. /* ---------------------------------------------------------------- */
  1587. /*
  1588. * Main RtNetlink dispatcher. Called from the main networking code
  1589. * (do_setlink() in net/core/rtnetlink.c).
  1590. * Check the type of Request and call the appropriate wrapper...
  1591. */
  1592. int wireless_rtnetlink_set(struct net_device * dev,
  1593. char * data,
  1594. int len)
  1595. {
  1596. struct iw_event * request = (struct iw_event *) data;
  1597. iw_handler handler;
  1598. /* Check length */
  1599. if(len < IW_EV_LCP_LEN) {
  1600. printk(KERN_DEBUG "%s (WE.r) : RtNetlink request too short (%d)\n",
  1601. dev->name, len);
  1602. return -EINVAL;
  1603. }
  1604. /* ReCheck length (len may have padding) */
  1605. if(request->len > len) {
  1606. printk(KERN_DEBUG "%s (WE.r) : RtNetlink request len invalid (%d-%d)\n",
  1607. dev->name, request->len, len);
  1608. return -EINVAL;
  1609. }
  1610. /* Only accept SET requests in here */
  1611. if(!IW_IS_SET(request->cmd))
  1612. return -EOPNOTSUPP;
  1613. /* Basic check */
  1614. if (!netif_device_present(dev))
  1615. return -ENODEV;
  1616. /* New driver API : try to find the handler */
  1617. handler = get_handler(dev, request->cmd);
  1618. if(handler != NULL) {
  1619. /* Standard and private are not the same */
  1620. if(request->cmd < SIOCIWFIRSTPRIV)
  1621. return rtnetlink_standard_set(dev,
  1622. request,
  1623. request->len,
  1624. handler);
  1625. else
  1626. return rtnetlink_private_set(dev,
  1627. request,
  1628. request->len,
  1629. handler);
  1630. }
  1631. return -EOPNOTSUPP;
  1632. }
  1633. #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
  1634. /************************* EVENT PROCESSING *************************/
  1635. /*
  1636. * Process events generated by the wireless layer or the driver.
  1637. * Most often, the event will be propagated through rtnetlink
  1638. */
  1639. #ifdef WE_EVENT_RTNETLINK
  1640. /* ---------------------------------------------------------------- */
  1641. /*
  1642. * Locking...
  1643. * ----------
  1644. *
  1645. * Thanks to Herbert Xu <herbert@gondor.apana.org.au> for fixing
  1646. * the locking issue in here and implementing this code !
  1647. *
  1648. * The issue : wireless_send_event() is often called in interrupt context,
  1649. * while the Netlink layer can never be called in interrupt context.
  1650. * The fully formed RtNetlink events are queued, and then a tasklet is run
  1651. * to feed those to Netlink.
  1652. * The skb_queue is interrupt safe, and its lock is not held while calling
  1653. * Netlink, so there is no possibility of dealock.
  1654. * Jean II
  1655. */
  1656. static struct sk_buff_head wireless_nlevent_queue;
  1657. static int __init wireless_nlevent_init(void)
  1658. {
  1659. skb_queue_head_init(&wireless_nlevent_queue);
  1660. return 0;
  1661. }
  1662. subsys_initcall(wireless_nlevent_init);
  1663. static void wireless_nlevent_process(unsigned long data)
  1664. {
  1665. struct sk_buff *skb;
  1666. while ((skb = skb_dequeue(&wireless_nlevent_queue)))
  1667. rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
  1668. }
  1669. static DECLARE_TASKLET(wireless_nlevent_tasklet, wireless_nlevent_process, 0);
  1670. /* ---------------------------------------------------------------- */
  1671. /*
  1672. * Fill a rtnetlink message with our event data.
  1673. * Note that we propage only the specified event and don't dump the
  1674. * current wireless config. Dumping the wireless config is far too
  1675. * expensive (for each parameter, the driver need to query the hardware).
  1676. */
  1677. static inline int rtnetlink_fill_iwinfo(struct sk_buff * skb,
  1678. struct net_device * dev,
  1679. int type,
  1680. char * event,
  1681. int event_len)
  1682. {
  1683. struct ifinfomsg *r;
  1684. struct nlmsghdr *nlh;
  1685. unsigned char *b = skb->tail;
  1686. nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(*r));
  1687. r = NLMSG_DATA(nlh);
  1688. r->ifi_family = AF_UNSPEC;
  1689. r->__ifi_pad = 0;
  1690. r->ifi_type = dev->type;
  1691. r->ifi_index = dev->ifindex;
  1692. r->ifi_flags = dev_get_flags(dev);
  1693. r->ifi_change = 0; /* Wireless changes don't affect those flags */
  1694. /* Add the wireless events in the netlink packet */
  1695. RTA_PUT(skb, IFLA_WIRELESS, event_len, event);
  1696. nlh->nlmsg_len = skb->tail - b;
  1697. return skb->len;
  1698. nlmsg_failure:
  1699. rtattr_failure:
  1700. skb_trim(skb, b - skb->data);
  1701. return -1;
  1702. }
  1703. /* ---------------------------------------------------------------- */
  1704. /*
  1705. * Create and broadcast and send it on the standard rtnetlink socket
  1706. * This is a pure clone rtmsg_ifinfo() in net/core/rtnetlink.c
  1707. * Andrzej Krzysztofowicz mandated that I used a IFLA_XXX field
  1708. * within a RTM_NEWLINK event.
  1709. */
  1710. static inline void rtmsg_iwinfo(struct net_device * dev,
  1711. char * event,
  1712. int event_len)
  1713. {
  1714. struct sk_buff *skb;
  1715. int size = NLMSG_GOODSIZE;
  1716. skb = alloc_skb(size, GFP_ATOMIC);
  1717. if (!skb)
  1718. return;
  1719. if (rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK,
  1720. event, event_len) < 0) {
  1721. kfree_skb(skb);
  1722. return;
  1723. }
  1724. NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
  1725. skb_queue_tail(&wireless_nlevent_queue, skb);
  1726. tasklet_schedule(&wireless_nlevent_tasklet);
  1727. }
  1728. #endif /* WE_EVENT_RTNETLINK */
  1729. /* ---------------------------------------------------------------- */
  1730. /*
  1731. * Main event dispatcher. Called from other parts and drivers.
  1732. * Send the event on the appropriate channels.
  1733. * May be called from interrupt context.
  1734. */
  1735. void wireless_send_event(struct net_device * dev,
  1736. unsigned int cmd,
  1737. union iwreq_data * wrqu,
  1738. char * extra)
  1739. {
  1740. const struct iw_ioctl_description * descr = NULL;
  1741. int extra_len = 0;
  1742. struct iw_event *event; /* Mallocated whole event */
  1743. int event_len; /* Its size */
  1744. int hdr_len; /* Size of the event header */
  1745. int wrqu_off = 0; /* Offset in wrqu */
  1746. /* Don't "optimise" the following variable, it will crash */
  1747. unsigned cmd_index; /* *MUST* be unsigned */
  1748. /* Get the description of the Event */
  1749. if(cmd <= SIOCIWLAST) {
  1750. cmd_index = cmd - SIOCIWFIRST;
  1751. if(cmd_index < standard_ioctl_num)
  1752. descr = &(standard_ioctl[cmd_index]);
  1753. } else {
  1754. cmd_index = cmd - IWEVFIRST;
  1755. if(cmd_index < standard_event_num)
  1756. descr = &(standard_event[cmd_index]);
  1757. }
  1758. /* Don't accept unknown events */
  1759. if(descr == NULL) {
  1760. /* Note : we don't return an error to the driver, because
  1761. * the driver would not know what to do about it. It can't
  1762. * return an error to the user, because the event is not
  1763. * initiated by a user request.
  1764. * The best the driver could do is to log an error message.
  1765. * We will do it ourselves instead...
  1766. */
  1767. printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
  1768. dev->name, cmd);
  1769. return;
  1770. }
  1771. #ifdef WE_EVENT_DEBUG
  1772. printk(KERN_DEBUG "%s (WE) : Got event 0x%04X\n",
  1773. dev->name, cmd);
  1774. 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);
  1775. #endif /* WE_EVENT_DEBUG */
  1776. /* Check extra parameters and set extra_len */
  1777. if(descr->header_type == IW_HEADER_TYPE_POINT) {
  1778. /* Check if number of token fits within bounds */
  1779. if(wrqu->data.length > descr->max_tokens) {
  1780. printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length);
  1781. return;
  1782. }
  1783. if(wrqu->data.length < descr->min_tokens) {
  1784. printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length);
  1785. return;
  1786. }
  1787. /* Calculate extra_len - extra is NULL for restricted events */
  1788. if(extra != NULL)
  1789. extra_len = wrqu->data.length * descr->token_size;
  1790. /* Always at an offset in wrqu */
  1791. wrqu_off = IW_EV_POINT_OFF;
  1792. #ifdef WE_EVENT_DEBUG
  1793. printk(KERN_DEBUG "%s (WE) : Event 0x%04X, tokens %d, extra_len %d\n", dev->name, cmd, wrqu->data.length, extra_len);
  1794. #endif /* WE_EVENT_DEBUG */
  1795. }
  1796. /* Total length of the event */
  1797. hdr_len = event_type_size[descr->header_type];
  1798. event_len = hdr_len + extra_len;
  1799. #ifdef WE_EVENT_DEBUG
  1800. 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);
  1801. #endif /* WE_EVENT_DEBUG */
  1802. /* Create temporary buffer to hold the event */
  1803. event = kmalloc(event_len, GFP_ATOMIC);
  1804. if(event == NULL)
  1805. return;
  1806. /* Fill event */
  1807. event->len = event_len;
  1808. event->cmd = cmd;
  1809. memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
  1810. if(extra != NULL)
  1811. memcpy(((char *) event) + hdr_len, extra, extra_len);
  1812. #ifdef WE_EVENT_RTNETLINK
  1813. /* Send via the RtNetlink event channel */
  1814. rtmsg_iwinfo(dev, (char *) event, event_len);
  1815. #endif /* WE_EVENT_RTNETLINK */
  1816. /* Cleanup */
  1817. kfree(event);
  1818. return; /* Always success, I guess ;-) */
  1819. }
  1820. /********************** ENHANCED IWSPY SUPPORT **********************/
  1821. /*
  1822. * In the old days, the driver was handling spy support all by itself.
  1823. * Now, the driver can delegate this task to Wireless Extensions.
  1824. * It needs to use those standard spy iw_handler in struct iw_handler_def,
  1825. * push data to us via wireless_spy_update() and include struct iw_spy_data
  1826. * in its private part (and export it in net_device->wireless_data->spy_data).
  1827. * One of the main advantage of centralising spy support here is that
  1828. * it becomes much easier to improve and extend it without having to touch
  1829. * the drivers. One example is the addition of the Spy-Threshold events.
  1830. */
  1831. /* ---------------------------------------------------------------- */
  1832. /*
  1833. * Return the pointer to the spy data in the driver.
  1834. * Because this is called on the Rx path via wireless_spy_update(),
  1835. * we want it to be efficient...
  1836. */
  1837. static inline struct iw_spy_data * get_spydata(struct net_device *dev)
  1838. {
  1839. /* This is the new way */
  1840. if(dev->wireless_data)
  1841. return(dev->wireless_data->spy_data);
  1842. return NULL;
  1843. }
  1844. /*------------------------------------------------------------------*/
  1845. /*
  1846. * Standard Wireless Handler : set Spy List
  1847. */
  1848. int iw_handler_set_spy(struct net_device * dev,
  1849. struct iw_request_info * info,
  1850. union iwreq_data * wrqu,
  1851. char * extra)
  1852. {
  1853. struct iw_spy_data * spydata = get_spydata(dev);
  1854. struct sockaddr * address = (struct sockaddr *) extra;
  1855. /* Make sure driver is not buggy or using the old API */
  1856. if(!spydata)
  1857. return -EOPNOTSUPP;
  1858. /* Disable spy collection while we copy the addresses.
  1859. * While we copy addresses, any call to wireless_spy_update()
  1860. * will NOP. This is OK, as anyway the addresses are changing. */
  1861. spydata->spy_number = 0;
  1862. /* We want to operate without locking, because wireless_spy_update()
  1863. * most likely will happen in the interrupt handler, and therefore
  1864. * have its own locking constraints and needs performance.
  1865. * The rtnl_lock() make sure we don't race with the other iw_handlers.
  1866. * This make sure wireless_spy_update() "see" that the spy list
  1867. * is temporarily disabled. */
  1868. wmb();
  1869. /* Are there are addresses to copy? */
  1870. if(wrqu->data.length > 0) {
  1871. int i;
  1872. /* Copy addresses */
  1873. for(i = 0; i < wrqu->data.length; i++)
  1874. memcpy(spydata->spy_address[i], address[i].sa_data,
  1875. ETH_ALEN);
  1876. /* Reset stats */
  1877. memset(spydata->spy_stat, 0,
  1878. sizeof(struct iw_quality) * IW_MAX_SPY);
  1879. #ifdef WE_SPY_DEBUG
  1880. printk(KERN_DEBUG "iw_handler_set_spy() : wireless_data %p, spydata %p, num %d\n", dev->wireless_data, spydata, wrqu->data.length);
  1881. for (i = 0; i < wrqu->data.length; i++)
  1882. printk(KERN_DEBUG
  1883. "%02X:%02X:%02X:%02X:%02X:%02X \n",
  1884. spydata->spy_address[i][0],
  1885. spydata->spy_address[i][1],
  1886. spydata->spy_address[i][2],
  1887. spydata->spy_address[i][3],
  1888. spydata->spy_address[i][4],
  1889. spydata->spy_address[i][5]);
  1890. #endif /* WE_SPY_DEBUG */
  1891. }
  1892. /* Make sure above is updated before re-enabling */
  1893. wmb();
  1894. /* Enable addresses */
  1895. spydata->spy_number = wrqu->data.length;
  1896. return 0;
  1897. }
  1898. /*------------------------------------------------------------------*/
  1899. /*
  1900. * Standard Wireless Handler : get Spy List
  1901. */
  1902. int iw_handler_get_spy(struct net_device * dev,
  1903. struct iw_request_info * info,
  1904. union iwreq_data * wrqu,
  1905. char * extra)
  1906. {
  1907. struct iw_spy_data * spydata = get_spydata(dev);
  1908. struct sockaddr * address = (struct sockaddr *) extra;
  1909. int i;
  1910. /* Make sure driver is not buggy or using the old API */
  1911. if(!spydata)
  1912. return -EOPNOTSUPP;
  1913. wrqu->data.length = spydata->spy_number;
  1914. /* Copy addresses. */
  1915. for(i = 0; i < spydata->spy_number; i++) {
  1916. memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
  1917. address[i].sa_family = AF_UNIX;
  1918. }
  1919. /* Copy stats to the user buffer (just after). */
  1920. if(spydata->spy_number > 0)
  1921. memcpy(extra + (sizeof(struct sockaddr) *spydata->spy_number),
  1922. spydata->spy_stat,
  1923. sizeof(struct iw_quality) * spydata->spy_number);
  1924. /* Reset updated flags. */
  1925. for(i = 0; i < spydata->spy_number; i++)
  1926. spydata->spy_stat[i].updated &= ~IW_QUAL_ALL_UPDATED;
  1927. return 0;
  1928. }
  1929. /*------------------------------------------------------------------*/
  1930. /*
  1931. * Standard Wireless Handler : set spy threshold
  1932. */
  1933. int iw_handler_set_thrspy(struct net_device * dev,
  1934. struct iw_request_info *info,
  1935. union iwreq_data * wrqu,
  1936. char * extra)
  1937. {
  1938. struct iw_spy_data * spydata = get_spydata(dev);
  1939. struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
  1940. /* Make sure driver is not buggy or using the old API */
  1941. if(!spydata)
  1942. return -EOPNOTSUPP;
  1943. /* Just do it */
  1944. memcpy(&(spydata->spy_thr_low), &(threshold->low),
  1945. 2 * sizeof(struct iw_quality));
  1946. /* Clear flag */
  1947. memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
  1948. #ifdef WE_SPY_DEBUG
  1949. printk(KERN_DEBUG "iw_handler_set_thrspy() : low %d ; high %d\n", spydata->spy_thr_low.level, spydata->spy_thr_high.level);
  1950. #endif /* WE_SPY_DEBUG */
  1951. return 0;
  1952. }
  1953. /*------------------------------------------------------------------*/
  1954. /*
  1955. * Standard Wireless Handler : get spy threshold
  1956. */
  1957. int iw_handler_get_thrspy(struct net_device * dev,
  1958. struct iw_request_info *info,
  1959. union iwreq_data * wrqu,
  1960. char * extra)
  1961. {
  1962. struct iw_spy_data * spydata = get_spydata(dev);
  1963. struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
  1964. /* Make sure driver is not buggy or using the old API */
  1965. if(!spydata)
  1966. return -EOPNOTSUPP;
  1967. /* Just do it */
  1968. memcpy(&(threshold->low), &(spydata->spy_thr_low),
  1969. 2 * sizeof(struct iw_quality));
  1970. return 0;
  1971. }
  1972. /*------------------------------------------------------------------*/
  1973. /*
  1974. * Prepare and send a Spy Threshold event
  1975. */
  1976. static void iw_send_thrspy_event(struct net_device * dev,
  1977. struct iw_spy_data * spydata,
  1978. unsigned char * address,
  1979. struct iw_quality * wstats)
  1980. {
  1981. union iwreq_data wrqu;
  1982. struct iw_thrspy threshold;
  1983. /* Init */
  1984. wrqu.data.length = 1;
  1985. wrqu.data.flags = 0;
  1986. /* Copy address */
  1987. memcpy(threshold.addr.sa_data, address, ETH_ALEN);
  1988. threshold.addr.sa_family = ARPHRD_ETHER;
  1989. /* Copy stats */
  1990. memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality));
  1991. /* Copy also thresholds */
  1992. memcpy(&(threshold.low), &(spydata->spy_thr_low),
  1993. 2 * sizeof(struct iw_quality));
  1994. #ifdef WE_SPY_DEBUG
  1995. printk(KERN_DEBUG "iw_send_thrspy_event() : address %02X:%02X:%02X:%02X:%02X:%02X, level %d, up = %d\n",
  1996. threshold.addr.sa_data[0],
  1997. threshold.addr.sa_data[1],
  1998. threshold.addr.sa_data[2],
  1999. threshold.addr.sa_data[3],
  2000. threshold.addr.sa_data[4],
  2001. threshold.addr.sa_data[5], threshold.qual.level);
  2002. #endif /* WE_SPY_DEBUG */
  2003. /* Send event to user space */
  2004. wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
  2005. }
  2006. /* ---------------------------------------------------------------- */
  2007. /*
  2008. * Call for the driver to update the spy data.
  2009. * For now, the spy data is a simple array. As the size of the array is
  2010. * small, this is good enough. If we wanted to support larger number of
  2011. * spy addresses, we should use something more efficient...
  2012. */
  2013. void wireless_spy_update(struct net_device * dev,
  2014. unsigned char * address,
  2015. struct iw_quality * wstats)
  2016. {
  2017. struct iw_spy_data * spydata = get_spydata(dev);
  2018. int i;
  2019. int match = -1;
  2020. /* Make sure driver is not buggy or using the old API */
  2021. if(!spydata)
  2022. return;
  2023. #ifdef WE_SPY_DEBUG
  2024. 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]);
  2025. #endif /* WE_SPY_DEBUG */
  2026. /* Update all records that match */
  2027. for(i = 0; i < spydata->spy_number; i++)
  2028. if(!compare_ether_addr(address, spydata->spy_address[i])) {
  2029. memcpy(&(spydata->spy_stat[i]), wstats,
  2030. sizeof(struct iw_quality));
  2031. match = i;
  2032. }
  2033. /* Generate an event if we cross the spy threshold.
  2034. * To avoid event storms, we have a simple hysteresis : we generate
  2035. * event only when we go under the low threshold or above the
  2036. * high threshold. */
  2037. if(match >= 0) {
  2038. if(spydata->spy_thr_under[match]) {
  2039. if(wstats->level > spydata->spy_thr_high.level) {
  2040. spydata->spy_thr_under[match] = 0;
  2041. iw_send_thrspy_event(dev, spydata,
  2042. address, wstats);
  2043. }
  2044. } else {
  2045. if(wstats->level < spydata->spy_thr_low.level) {
  2046. spydata->spy_thr_under[match] = 1;
  2047. iw_send_thrspy_event(dev, spydata,
  2048. address, wstats);
  2049. }
  2050. }
  2051. }
  2052. }
  2053. EXPORT_SYMBOL(iw_handler_get_spy);
  2054. EXPORT_SYMBOL(iw_handler_get_thrspy);
  2055. EXPORT_SYMBOL(iw_handler_set_spy);
  2056. EXPORT_SYMBOL(iw_handler_set_thrspy);
  2057. EXPORT_SYMBOL(wireless_send_event);
  2058. EXPORT_SYMBOL(wireless_spy_update);