wireless.c 70 KB

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