wext.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  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 lose 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 <net/net_namespace.h>
  94. #include <linux/wireless.h> /* Pretty obvious */
  95. #include <net/iw_handler.h> /* New driver API */
  96. #include <net/netlink.h>
  97. #include <net/wext.h>
  98. #include <asm/uaccess.h> /* copy_to_user() */
  99. /************************* GLOBAL VARIABLES *************************/
  100. /*
  101. * You should not use global variables, because of re-entrancy.
  102. * On our case, it's only const, so it's OK...
  103. */
  104. /*
  105. * Meta-data about all the standard Wireless Extension request we
  106. * know about.
  107. */
  108. static const struct iw_ioctl_description standard_ioctl[] = {
  109. [SIOCSIWCOMMIT - SIOCIWFIRST] = {
  110. .header_type = IW_HEADER_TYPE_NULL,
  111. },
  112. [SIOCGIWNAME - SIOCIWFIRST] = {
  113. .header_type = IW_HEADER_TYPE_CHAR,
  114. .flags = IW_DESCR_FLAG_DUMP,
  115. },
  116. [SIOCSIWNWID - SIOCIWFIRST] = {
  117. .header_type = IW_HEADER_TYPE_PARAM,
  118. .flags = IW_DESCR_FLAG_EVENT,
  119. },
  120. [SIOCGIWNWID - SIOCIWFIRST] = {
  121. .header_type = IW_HEADER_TYPE_PARAM,
  122. .flags = IW_DESCR_FLAG_DUMP,
  123. },
  124. [SIOCSIWFREQ - SIOCIWFIRST] = {
  125. .header_type = IW_HEADER_TYPE_FREQ,
  126. .flags = IW_DESCR_FLAG_EVENT,
  127. },
  128. [SIOCGIWFREQ - SIOCIWFIRST] = {
  129. .header_type = IW_HEADER_TYPE_FREQ,
  130. .flags = IW_DESCR_FLAG_DUMP,
  131. },
  132. [SIOCSIWMODE - SIOCIWFIRST] = {
  133. .header_type = IW_HEADER_TYPE_UINT,
  134. .flags = IW_DESCR_FLAG_EVENT,
  135. },
  136. [SIOCGIWMODE - SIOCIWFIRST] = {
  137. .header_type = IW_HEADER_TYPE_UINT,
  138. .flags = IW_DESCR_FLAG_DUMP,
  139. },
  140. [SIOCSIWSENS - SIOCIWFIRST] = {
  141. .header_type = IW_HEADER_TYPE_PARAM,
  142. },
  143. [SIOCGIWSENS - SIOCIWFIRST] = {
  144. .header_type = IW_HEADER_TYPE_PARAM,
  145. },
  146. [SIOCSIWRANGE - SIOCIWFIRST] = {
  147. .header_type = IW_HEADER_TYPE_NULL,
  148. },
  149. [SIOCGIWRANGE - SIOCIWFIRST] = {
  150. .header_type = IW_HEADER_TYPE_POINT,
  151. .token_size = 1,
  152. .max_tokens = sizeof(struct iw_range),
  153. .flags = IW_DESCR_FLAG_DUMP,
  154. },
  155. [SIOCSIWPRIV - SIOCIWFIRST] = {
  156. .header_type = IW_HEADER_TYPE_NULL,
  157. },
  158. [SIOCGIWPRIV - SIOCIWFIRST] = { /* (handled directly by us) */
  159. .header_type = IW_HEADER_TYPE_POINT,
  160. .token_size = sizeof(struct iw_priv_args),
  161. .max_tokens = 16,
  162. .flags = IW_DESCR_FLAG_NOMAX,
  163. },
  164. [SIOCSIWSTATS - SIOCIWFIRST] = {
  165. .header_type = IW_HEADER_TYPE_NULL,
  166. },
  167. [SIOCGIWSTATS - SIOCIWFIRST] = { /* (handled directly by us) */
  168. .header_type = IW_HEADER_TYPE_POINT,
  169. .token_size = 1,
  170. .max_tokens = sizeof(struct iw_statistics),
  171. .flags = IW_DESCR_FLAG_DUMP,
  172. },
  173. [SIOCSIWSPY - SIOCIWFIRST] = {
  174. .header_type = IW_HEADER_TYPE_POINT,
  175. .token_size = sizeof(struct sockaddr),
  176. .max_tokens = IW_MAX_SPY,
  177. },
  178. [SIOCGIWSPY - SIOCIWFIRST] = {
  179. .header_type = IW_HEADER_TYPE_POINT,
  180. .token_size = sizeof(struct sockaddr) +
  181. sizeof(struct iw_quality),
  182. .max_tokens = IW_MAX_SPY,
  183. },
  184. [SIOCSIWTHRSPY - SIOCIWFIRST] = {
  185. .header_type = IW_HEADER_TYPE_POINT,
  186. .token_size = sizeof(struct iw_thrspy),
  187. .min_tokens = 1,
  188. .max_tokens = 1,
  189. },
  190. [SIOCGIWTHRSPY - SIOCIWFIRST] = {
  191. .header_type = IW_HEADER_TYPE_POINT,
  192. .token_size = sizeof(struct iw_thrspy),
  193. .min_tokens = 1,
  194. .max_tokens = 1,
  195. },
  196. [SIOCSIWAP - SIOCIWFIRST] = {
  197. .header_type = IW_HEADER_TYPE_ADDR,
  198. },
  199. [SIOCGIWAP - SIOCIWFIRST] = {
  200. .header_type = IW_HEADER_TYPE_ADDR,
  201. .flags = IW_DESCR_FLAG_DUMP,
  202. },
  203. [SIOCSIWMLME - SIOCIWFIRST] = {
  204. .header_type = IW_HEADER_TYPE_POINT,
  205. .token_size = 1,
  206. .min_tokens = sizeof(struct iw_mlme),
  207. .max_tokens = sizeof(struct iw_mlme),
  208. },
  209. [SIOCGIWAPLIST - SIOCIWFIRST] = {
  210. .header_type = IW_HEADER_TYPE_POINT,
  211. .token_size = sizeof(struct sockaddr) +
  212. sizeof(struct iw_quality),
  213. .max_tokens = IW_MAX_AP,
  214. .flags = IW_DESCR_FLAG_NOMAX,
  215. },
  216. [SIOCSIWSCAN - SIOCIWFIRST] = {
  217. .header_type = IW_HEADER_TYPE_POINT,
  218. .token_size = 1,
  219. .min_tokens = 0,
  220. .max_tokens = sizeof(struct iw_scan_req),
  221. },
  222. [SIOCGIWSCAN - SIOCIWFIRST] = {
  223. .header_type = IW_HEADER_TYPE_POINT,
  224. .token_size = 1,
  225. .max_tokens = IW_SCAN_MAX_DATA,
  226. .flags = IW_DESCR_FLAG_NOMAX,
  227. },
  228. [SIOCSIWESSID - SIOCIWFIRST] = {
  229. .header_type = IW_HEADER_TYPE_POINT,
  230. .token_size = 1,
  231. .max_tokens = IW_ESSID_MAX_SIZE,
  232. .flags = IW_DESCR_FLAG_EVENT,
  233. },
  234. [SIOCGIWESSID - SIOCIWFIRST] = {
  235. .header_type = IW_HEADER_TYPE_POINT,
  236. .token_size = 1,
  237. .max_tokens = IW_ESSID_MAX_SIZE,
  238. .flags = IW_DESCR_FLAG_DUMP,
  239. },
  240. [SIOCSIWNICKN - SIOCIWFIRST] = {
  241. .header_type = IW_HEADER_TYPE_POINT,
  242. .token_size = 1,
  243. .max_tokens = IW_ESSID_MAX_SIZE,
  244. },
  245. [SIOCGIWNICKN - SIOCIWFIRST] = {
  246. .header_type = IW_HEADER_TYPE_POINT,
  247. .token_size = 1,
  248. .max_tokens = IW_ESSID_MAX_SIZE,
  249. },
  250. [SIOCSIWRATE - SIOCIWFIRST] = {
  251. .header_type = IW_HEADER_TYPE_PARAM,
  252. },
  253. [SIOCGIWRATE - SIOCIWFIRST] = {
  254. .header_type = IW_HEADER_TYPE_PARAM,
  255. },
  256. [SIOCSIWRTS - SIOCIWFIRST] = {
  257. .header_type = IW_HEADER_TYPE_PARAM,
  258. },
  259. [SIOCGIWRTS - SIOCIWFIRST] = {
  260. .header_type = IW_HEADER_TYPE_PARAM,
  261. },
  262. [SIOCSIWFRAG - SIOCIWFIRST] = {
  263. .header_type = IW_HEADER_TYPE_PARAM,
  264. },
  265. [SIOCGIWFRAG - SIOCIWFIRST] = {
  266. .header_type = IW_HEADER_TYPE_PARAM,
  267. },
  268. [SIOCSIWTXPOW - SIOCIWFIRST] = {
  269. .header_type = IW_HEADER_TYPE_PARAM,
  270. },
  271. [SIOCGIWTXPOW - SIOCIWFIRST] = {
  272. .header_type = IW_HEADER_TYPE_PARAM,
  273. },
  274. [SIOCSIWRETRY - SIOCIWFIRST] = {
  275. .header_type = IW_HEADER_TYPE_PARAM,
  276. },
  277. [SIOCGIWRETRY - SIOCIWFIRST] = {
  278. .header_type = IW_HEADER_TYPE_PARAM,
  279. },
  280. [SIOCSIWENCODE - SIOCIWFIRST] = {
  281. .header_type = IW_HEADER_TYPE_POINT,
  282. .token_size = 1,
  283. .max_tokens = IW_ENCODING_TOKEN_MAX,
  284. .flags = IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT,
  285. },
  286. [SIOCGIWENCODE - SIOCIWFIRST] = {
  287. .header_type = IW_HEADER_TYPE_POINT,
  288. .token_size = 1,
  289. .max_tokens = IW_ENCODING_TOKEN_MAX,
  290. .flags = IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT,
  291. },
  292. [SIOCSIWPOWER - SIOCIWFIRST] = {
  293. .header_type = IW_HEADER_TYPE_PARAM,
  294. },
  295. [SIOCGIWPOWER - SIOCIWFIRST] = {
  296. .header_type = IW_HEADER_TYPE_PARAM,
  297. },
  298. [SIOCSIWGENIE - SIOCIWFIRST] = {
  299. .header_type = IW_HEADER_TYPE_POINT,
  300. .token_size = 1,
  301. .max_tokens = IW_GENERIC_IE_MAX,
  302. },
  303. [SIOCGIWGENIE - SIOCIWFIRST] = {
  304. .header_type = IW_HEADER_TYPE_POINT,
  305. .token_size = 1,
  306. .max_tokens = IW_GENERIC_IE_MAX,
  307. },
  308. [SIOCSIWAUTH - SIOCIWFIRST] = {
  309. .header_type = IW_HEADER_TYPE_PARAM,
  310. },
  311. [SIOCGIWAUTH - SIOCIWFIRST] = {
  312. .header_type = IW_HEADER_TYPE_PARAM,
  313. },
  314. [SIOCSIWENCODEEXT - SIOCIWFIRST] = {
  315. .header_type = IW_HEADER_TYPE_POINT,
  316. .token_size = 1,
  317. .min_tokens = sizeof(struct iw_encode_ext),
  318. .max_tokens = sizeof(struct iw_encode_ext) +
  319. IW_ENCODING_TOKEN_MAX,
  320. },
  321. [SIOCGIWENCODEEXT - SIOCIWFIRST] = {
  322. .header_type = IW_HEADER_TYPE_POINT,
  323. .token_size = 1,
  324. .min_tokens = sizeof(struct iw_encode_ext),
  325. .max_tokens = sizeof(struct iw_encode_ext) +
  326. IW_ENCODING_TOKEN_MAX,
  327. },
  328. [SIOCSIWPMKSA - SIOCIWFIRST] = {
  329. .header_type = IW_HEADER_TYPE_POINT,
  330. .token_size = 1,
  331. .min_tokens = sizeof(struct iw_pmksa),
  332. .max_tokens = sizeof(struct iw_pmksa),
  333. },
  334. };
  335. static const unsigned standard_ioctl_num = ARRAY_SIZE(standard_ioctl);
  336. /*
  337. * Meta-data about all the additional standard Wireless Extension events
  338. * we know about.
  339. */
  340. static const struct iw_ioctl_description standard_event[] = {
  341. [IWEVTXDROP - IWEVFIRST] = {
  342. .header_type = IW_HEADER_TYPE_ADDR,
  343. },
  344. [IWEVQUAL - IWEVFIRST] = {
  345. .header_type = IW_HEADER_TYPE_QUAL,
  346. },
  347. [IWEVCUSTOM - IWEVFIRST] = {
  348. .header_type = IW_HEADER_TYPE_POINT,
  349. .token_size = 1,
  350. .max_tokens = IW_CUSTOM_MAX,
  351. },
  352. [IWEVREGISTERED - IWEVFIRST] = {
  353. .header_type = IW_HEADER_TYPE_ADDR,
  354. },
  355. [IWEVEXPIRED - IWEVFIRST] = {
  356. .header_type = IW_HEADER_TYPE_ADDR,
  357. },
  358. [IWEVGENIE - IWEVFIRST] = {
  359. .header_type = IW_HEADER_TYPE_POINT,
  360. .token_size = 1,
  361. .max_tokens = IW_GENERIC_IE_MAX,
  362. },
  363. [IWEVMICHAELMICFAILURE - IWEVFIRST] = {
  364. .header_type = IW_HEADER_TYPE_POINT,
  365. .token_size = 1,
  366. .max_tokens = sizeof(struct iw_michaelmicfailure),
  367. },
  368. [IWEVASSOCREQIE - IWEVFIRST] = {
  369. .header_type = IW_HEADER_TYPE_POINT,
  370. .token_size = 1,
  371. .max_tokens = IW_GENERIC_IE_MAX,
  372. },
  373. [IWEVASSOCRESPIE - IWEVFIRST] = {
  374. .header_type = IW_HEADER_TYPE_POINT,
  375. .token_size = 1,
  376. .max_tokens = IW_GENERIC_IE_MAX,
  377. },
  378. [IWEVPMKIDCAND - IWEVFIRST] = {
  379. .header_type = IW_HEADER_TYPE_POINT,
  380. .token_size = 1,
  381. .max_tokens = sizeof(struct iw_pmkid_cand),
  382. },
  383. };
  384. static const unsigned standard_event_num = ARRAY_SIZE(standard_event);
  385. /* Size (in bytes) of the various private data types */
  386. static const char iw_priv_type_size[] = {
  387. 0, /* IW_PRIV_TYPE_NONE */
  388. 1, /* IW_PRIV_TYPE_BYTE */
  389. 1, /* IW_PRIV_TYPE_CHAR */
  390. 0, /* Not defined */
  391. sizeof(__u32), /* IW_PRIV_TYPE_INT */
  392. sizeof(struct iw_freq), /* IW_PRIV_TYPE_FLOAT */
  393. sizeof(struct sockaddr), /* IW_PRIV_TYPE_ADDR */
  394. 0, /* Not defined */
  395. };
  396. /* Size (in bytes) of various events */
  397. static const int event_type_size[] = {
  398. IW_EV_LCP_LEN, /* IW_HEADER_TYPE_NULL */
  399. 0,
  400. IW_EV_CHAR_LEN, /* IW_HEADER_TYPE_CHAR */
  401. 0,
  402. IW_EV_UINT_LEN, /* IW_HEADER_TYPE_UINT */
  403. IW_EV_FREQ_LEN, /* IW_HEADER_TYPE_FREQ */
  404. IW_EV_ADDR_LEN, /* IW_HEADER_TYPE_ADDR */
  405. 0,
  406. IW_EV_POINT_LEN, /* Without variable payload */
  407. IW_EV_PARAM_LEN, /* IW_HEADER_TYPE_PARAM */
  408. IW_EV_QUAL_LEN, /* IW_HEADER_TYPE_QUAL */
  409. };
  410. #ifdef CONFIG_COMPAT
  411. static const int compat_event_type_size[] = {
  412. IW_EV_COMPAT_LCP_LEN, /* IW_HEADER_TYPE_NULL */
  413. 0,
  414. IW_EV_COMPAT_CHAR_LEN, /* IW_HEADER_TYPE_CHAR */
  415. 0,
  416. IW_EV_COMPAT_UINT_LEN, /* IW_HEADER_TYPE_UINT */
  417. IW_EV_COMPAT_FREQ_LEN, /* IW_HEADER_TYPE_FREQ */
  418. IW_EV_COMPAT_ADDR_LEN, /* IW_HEADER_TYPE_ADDR */
  419. 0,
  420. IW_EV_COMPAT_POINT_LEN, /* Without variable payload */
  421. IW_EV_COMPAT_PARAM_LEN, /* IW_HEADER_TYPE_PARAM */
  422. IW_EV_COMPAT_QUAL_LEN, /* IW_HEADER_TYPE_QUAL */
  423. };
  424. #endif
  425. /************************ COMMON SUBROUTINES ************************/
  426. /*
  427. * Stuff that may be used in various place or doesn't fit in one
  428. * of the section below.
  429. */
  430. /* ---------------------------------------------------------------- */
  431. /*
  432. * Return the driver handler associated with a specific Wireless Extension.
  433. */
  434. static iw_handler get_handler(struct net_device *dev, unsigned int cmd)
  435. {
  436. /* Don't "optimise" the following variable, it will crash */
  437. unsigned int index; /* *MUST* be unsigned */
  438. /* Check if we have some wireless handlers defined */
  439. if (dev->wireless_handlers == NULL)
  440. return NULL;
  441. /* Try as a standard command */
  442. index = cmd - SIOCIWFIRST;
  443. if (index < dev->wireless_handlers->num_standard)
  444. return dev->wireless_handlers->standard[index];
  445. /* Try as a private command */
  446. index = cmd - SIOCIWFIRSTPRIV;
  447. if (index < dev->wireless_handlers->num_private)
  448. return dev->wireless_handlers->private[index];
  449. /* Not found */
  450. return NULL;
  451. }
  452. /* ---------------------------------------------------------------- */
  453. /*
  454. * Get statistics out of the driver
  455. */
  456. struct iw_statistics *get_wireless_stats(struct net_device *dev)
  457. {
  458. /* New location */
  459. if ((dev->wireless_handlers != NULL) &&
  460. (dev->wireless_handlers->get_wireless_stats != NULL))
  461. return dev->wireless_handlers->get_wireless_stats(dev);
  462. /* Not found */
  463. return NULL;
  464. }
  465. /* ---------------------------------------------------------------- */
  466. /*
  467. * Call the commit handler in the driver
  468. * (if exist and if conditions are right)
  469. *
  470. * Note : our current commit strategy is currently pretty dumb,
  471. * but we will be able to improve on that...
  472. * The goal is to try to agreagate as many changes as possible
  473. * before doing the commit. Drivers that will define a commit handler
  474. * are usually those that need a reset after changing parameters, so
  475. * we want to minimise the number of reset.
  476. * A cool idea is to use a timer : at each "set" command, we re-set the
  477. * timer, when the timer eventually fires, we call the driver.
  478. * Hopefully, more on that later.
  479. *
  480. * Also, I'm waiting to see how many people will complain about the
  481. * netif_running(dev) test. I'm open on that one...
  482. * Hopefully, the driver will remember to do a commit in "open()" ;-)
  483. */
  484. static int call_commit_handler(struct net_device *dev)
  485. {
  486. if ((netif_running(dev)) &&
  487. (dev->wireless_handlers->standard[0] != NULL))
  488. /* Call the commit handler on the driver */
  489. return dev->wireless_handlers->standard[0](dev, NULL,
  490. NULL, NULL);
  491. else
  492. return 0; /* Command completed successfully */
  493. }
  494. /* ---------------------------------------------------------------- */
  495. /*
  496. * Calculate size of private arguments
  497. */
  498. static int get_priv_size(__u16 args)
  499. {
  500. int num = args & IW_PRIV_SIZE_MASK;
  501. int type = (args & IW_PRIV_TYPE_MASK) >> 12;
  502. return num * iw_priv_type_size[type];
  503. }
  504. /* ---------------------------------------------------------------- */
  505. /*
  506. * Re-calculate the size of private arguments
  507. */
  508. static int adjust_priv_size(__u16 args, struct iw_point *iwp)
  509. {
  510. int num = iwp->length;
  511. int max = args & IW_PRIV_SIZE_MASK;
  512. int type = (args & IW_PRIV_TYPE_MASK) >> 12;
  513. /* Make sure the driver doesn't goof up */
  514. if (max < num)
  515. num = max;
  516. return num * iw_priv_type_size[type];
  517. }
  518. /* ---------------------------------------------------------------- */
  519. /*
  520. * Standard Wireless Handler : get wireless stats
  521. * Allow programatic access to /proc/net/wireless even if /proc
  522. * doesn't exist... Also more efficient...
  523. */
  524. static int iw_handler_get_iwstats(struct net_device * dev,
  525. struct iw_request_info * info,
  526. union iwreq_data * wrqu,
  527. char * extra)
  528. {
  529. /* Get stats from the driver */
  530. struct iw_statistics *stats;
  531. stats = get_wireless_stats(dev);
  532. if (stats) {
  533. /* Copy statistics to extra */
  534. memcpy(extra, stats, sizeof(struct iw_statistics));
  535. wrqu->data.length = sizeof(struct iw_statistics);
  536. /* Check if we need to clear the updated flag */
  537. if (wrqu->data.flags != 0)
  538. stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
  539. return 0;
  540. } else
  541. return -EOPNOTSUPP;
  542. }
  543. /* ---------------------------------------------------------------- */
  544. /*
  545. * Standard Wireless Handler : get iwpriv definitions
  546. * Export the driver private handler definition
  547. * They will be picked up by tools like iwpriv...
  548. */
  549. static int iw_handler_get_private(struct net_device * dev,
  550. struct iw_request_info * info,
  551. union iwreq_data * wrqu,
  552. char * extra)
  553. {
  554. /* Check if the driver has something to export */
  555. if ((dev->wireless_handlers->num_private_args == 0) ||
  556. (dev->wireless_handlers->private_args == NULL))
  557. return -EOPNOTSUPP;
  558. /* Check if there is enough buffer up there */
  559. if (wrqu->data.length < dev->wireless_handlers->num_private_args) {
  560. /* User space can't know in advance how large the buffer
  561. * needs to be. Give it a hint, so that we can support
  562. * any size buffer we want somewhat efficiently... */
  563. wrqu->data.length = dev->wireless_handlers->num_private_args;
  564. return -E2BIG;
  565. }
  566. /* Set the number of available ioctls. */
  567. wrqu->data.length = dev->wireless_handlers->num_private_args;
  568. /* Copy structure to the user buffer. */
  569. memcpy(extra, dev->wireless_handlers->private_args,
  570. sizeof(struct iw_priv_args) * wrqu->data.length);
  571. return 0;
  572. }
  573. /******************** /proc/net/wireless SUPPORT ********************/
  574. /*
  575. * The /proc/net/wireless file is a human readable user-space interface
  576. * exporting various wireless specific statistics from the wireless devices.
  577. * This is the most popular part of the Wireless Extensions ;-)
  578. *
  579. * This interface is a pure clone of /proc/net/dev (in net/core/dev.c).
  580. * The content of the file is basically the content of "struct iw_statistics".
  581. */
  582. #ifdef CONFIG_PROC_FS
  583. /* ---------------------------------------------------------------- */
  584. /*
  585. * Print one entry (line) of /proc/net/wireless
  586. */
  587. static void wireless_seq_printf_stats(struct seq_file *seq,
  588. struct net_device *dev)
  589. {
  590. /* Get stats from the driver */
  591. struct iw_statistics *stats = get_wireless_stats(dev);
  592. static struct iw_statistics nullstats = {};
  593. /* show device if it's wireless regardless of current stats */
  594. if (!stats && dev->wireless_handlers)
  595. stats = &nullstats;
  596. if (stats) {
  597. seq_printf(seq, "%6s: %04x %3d%c %3d%c %3d%c %6d %6d %6d "
  598. "%6d %6d %6d\n",
  599. dev->name, stats->status, stats->qual.qual,
  600. stats->qual.updated & IW_QUAL_QUAL_UPDATED
  601. ? '.' : ' ',
  602. ((__s32) stats->qual.level) -
  603. ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
  604. stats->qual.updated & IW_QUAL_LEVEL_UPDATED
  605. ? '.' : ' ',
  606. ((__s32) stats->qual.noise) -
  607. ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
  608. stats->qual.updated & IW_QUAL_NOISE_UPDATED
  609. ? '.' : ' ',
  610. stats->discard.nwid, stats->discard.code,
  611. stats->discard.fragment, stats->discard.retries,
  612. stats->discard.misc, stats->miss.beacon);
  613. if (stats != &nullstats)
  614. stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
  615. }
  616. }
  617. /* ---------------------------------------------------------------- */
  618. /*
  619. * Print info for /proc/net/wireless (print all entries)
  620. */
  621. static int wireless_dev_seq_show(struct seq_file *seq, void *v)
  622. {
  623. might_sleep();
  624. if (v == SEQ_START_TOKEN)
  625. seq_printf(seq, "Inter-| sta-| Quality | Discarded "
  626. "packets | Missed | WE\n"
  627. " face | tus | link level noise | nwid "
  628. "crypt frag retry misc | beacon | %d\n",
  629. WIRELESS_EXT);
  630. else
  631. wireless_seq_printf_stats(seq, v);
  632. return 0;
  633. }
  634. static void *wireless_dev_seq_start(struct seq_file *seq, loff_t *pos)
  635. {
  636. struct net *net = seq_file_net(seq);
  637. loff_t off;
  638. struct net_device *dev;
  639. rtnl_lock();
  640. if (!*pos)
  641. return SEQ_START_TOKEN;
  642. off = 1;
  643. for_each_netdev(net, dev)
  644. if (off++ == *pos)
  645. return dev;
  646. return NULL;
  647. }
  648. static void *wireless_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  649. {
  650. struct net *net = seq_file_net(seq);
  651. ++*pos;
  652. return v == SEQ_START_TOKEN ?
  653. first_net_device(net) : next_net_device(v);
  654. }
  655. static void wireless_dev_seq_stop(struct seq_file *seq, void *v)
  656. {
  657. rtnl_unlock();
  658. }
  659. static const struct seq_operations wireless_seq_ops = {
  660. .start = wireless_dev_seq_start,
  661. .next = wireless_dev_seq_next,
  662. .stop = wireless_dev_seq_stop,
  663. .show = wireless_dev_seq_show,
  664. };
  665. static int seq_open_wireless(struct inode *inode, struct file *file)
  666. {
  667. return seq_open_net(inode, file, &wireless_seq_ops,
  668. sizeof(struct seq_net_private));
  669. }
  670. static const struct file_operations wireless_seq_fops = {
  671. .owner = THIS_MODULE,
  672. .open = seq_open_wireless,
  673. .read = seq_read,
  674. .llseek = seq_lseek,
  675. .release = seq_release_net,
  676. };
  677. int wext_proc_init(struct net *net)
  678. {
  679. /* Create /proc/net/wireless entry */
  680. if (!proc_net_fops_create(net, "wireless", S_IRUGO, &wireless_seq_fops))
  681. return -ENOMEM;
  682. return 0;
  683. }
  684. void wext_proc_exit(struct net *net)
  685. {
  686. proc_net_remove(net, "wireless");
  687. }
  688. #endif /* CONFIG_PROC_FS */
  689. /************************** IOCTL SUPPORT **************************/
  690. /*
  691. * The original user space API to configure all those Wireless Extensions
  692. * is through IOCTLs.
  693. * In there, we check if we need to call the new driver API (iw_handler)
  694. * or just call the driver ioctl handler.
  695. */
  696. /* ---------------------------------------------------------------- */
  697. static int ioctl_standard_iw_point(struct iw_point *iwp, unsigned int cmd,
  698. const struct iw_ioctl_description *descr,
  699. iw_handler handler, struct net_device *dev,
  700. struct iw_request_info *info)
  701. {
  702. int err, extra_size, user_length = 0, essid_compat = 0;
  703. char *extra;
  704. /* Calculate space needed by arguments. Always allocate
  705. * for max space.
  706. */
  707. extra_size = descr->max_tokens * descr->token_size;
  708. /* Check need for ESSID compatibility for WE < 21 */
  709. switch (cmd) {
  710. case SIOCSIWESSID:
  711. case SIOCGIWESSID:
  712. case SIOCSIWNICKN:
  713. case SIOCGIWNICKN:
  714. if (iwp->length == descr->max_tokens + 1)
  715. essid_compat = 1;
  716. else if (IW_IS_SET(cmd) && (iwp->length != 0)) {
  717. char essid[IW_ESSID_MAX_SIZE + 1];
  718. unsigned int len;
  719. len = iwp->length * descr->token_size;
  720. if (len > IW_ESSID_MAX_SIZE)
  721. return -EFAULT;
  722. err = copy_from_user(essid, iwp->pointer, len);
  723. if (err)
  724. return -EFAULT;
  725. if (essid[iwp->length - 1] == '\0')
  726. essid_compat = 1;
  727. }
  728. break;
  729. default:
  730. break;
  731. }
  732. iwp->length -= essid_compat;
  733. /* Check what user space is giving us */
  734. if (IW_IS_SET(cmd)) {
  735. /* Check NULL pointer */
  736. if (!iwp->pointer && iwp->length != 0)
  737. return -EFAULT;
  738. /* Check if number of token fits within bounds */
  739. if (iwp->length > descr->max_tokens)
  740. return -E2BIG;
  741. if (iwp->length < descr->min_tokens)
  742. return -EINVAL;
  743. } else {
  744. /* Check NULL pointer */
  745. if (!iwp->pointer)
  746. return -EFAULT;
  747. /* Save user space buffer size for checking */
  748. user_length = iwp->length;
  749. /* Don't check if user_length > max to allow forward
  750. * compatibility. The test user_length < min is
  751. * implied by the test at the end.
  752. */
  753. /* Support for very large requests */
  754. if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
  755. (user_length > descr->max_tokens)) {
  756. /* Allow userspace to GET more than max so
  757. * we can support any size GET requests.
  758. * There is still a limit : -ENOMEM.
  759. */
  760. extra_size = user_length * descr->token_size;
  761. /* Note : user_length is originally a __u16,
  762. * and token_size is controlled by us,
  763. * so extra_size won't get negative and
  764. * won't overflow...
  765. */
  766. }
  767. }
  768. /* kzalloc() ensures NULL-termination for essid_compat. */
  769. extra = kzalloc(extra_size, GFP_KERNEL);
  770. if (!extra)
  771. return -ENOMEM;
  772. /* If it is a SET, get all the extra data in here */
  773. if (IW_IS_SET(cmd) && (iwp->length != 0)) {
  774. if (copy_from_user(extra, iwp->pointer,
  775. iwp->length *
  776. descr->token_size)) {
  777. err = -EFAULT;
  778. goto out;
  779. }
  780. if (cmd == SIOCSIWENCODEEXT) {
  781. struct iw_encode_ext *ee = (void *) extra;
  782. if (iwp->length < sizeof(*ee) + ee->key_len)
  783. return -EFAULT;
  784. }
  785. }
  786. err = handler(dev, info, (union iwreq_data *) iwp, extra);
  787. iwp->length += essid_compat;
  788. /* If we have something to return to the user */
  789. if (!err && IW_IS_GET(cmd)) {
  790. /* Check if there is enough buffer up there */
  791. if (user_length < iwp->length) {
  792. err = -E2BIG;
  793. goto out;
  794. }
  795. if (copy_to_user(iwp->pointer, extra,
  796. iwp->length *
  797. descr->token_size)) {
  798. err = -EFAULT;
  799. goto out;
  800. }
  801. }
  802. /* Generate an event to notify listeners of the change */
  803. if ((descr->flags & IW_DESCR_FLAG_EVENT) && err == -EIWCOMMIT) {
  804. union iwreq_data *data = (union iwreq_data *) iwp;
  805. if (descr->flags & IW_DESCR_FLAG_RESTRICT)
  806. /* If the event is restricted, don't
  807. * export the payload.
  808. */
  809. wireless_send_event(dev, cmd, data, NULL);
  810. else
  811. wireless_send_event(dev, cmd, data, extra);
  812. }
  813. out:
  814. kfree(extra);
  815. return err;
  816. }
  817. /*
  818. * Wrapper to call a standard Wireless Extension handler.
  819. * We do various checks and also take care of moving data between
  820. * user space and kernel space.
  821. */
  822. static int ioctl_standard_call(struct net_device * dev,
  823. struct iwreq *iwr,
  824. unsigned int cmd,
  825. struct iw_request_info *info,
  826. iw_handler handler)
  827. {
  828. const struct iw_ioctl_description * descr;
  829. int ret = -EINVAL;
  830. /* Get the description of the IOCTL */
  831. if ((cmd - SIOCIWFIRST) >= standard_ioctl_num)
  832. return -EOPNOTSUPP;
  833. descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
  834. /* Check if we have a pointer to user space data or not */
  835. if (descr->header_type != IW_HEADER_TYPE_POINT) {
  836. /* No extra arguments. Trivial to handle */
  837. ret = handler(dev, info, &(iwr->u), NULL);
  838. /* Generate an event to notify listeners of the change */
  839. if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
  840. ((ret == 0) || (ret == -EIWCOMMIT)))
  841. wireless_send_event(dev, cmd, &(iwr->u), NULL);
  842. } else {
  843. ret = ioctl_standard_iw_point(&iwr->u.data, cmd, descr,
  844. handler, dev, info);
  845. }
  846. /* Call commit handler if needed and defined */
  847. if (ret == -EIWCOMMIT)
  848. ret = call_commit_handler(dev);
  849. /* Here, we will generate the appropriate event if needed */
  850. return ret;
  851. }
  852. /* ---------------------------------------------------------------- */
  853. /*
  854. * Wrapper to call a private Wireless Extension handler.
  855. * We do various checks and also take care of moving data between
  856. * user space and kernel space.
  857. * It's not as nice and slimline as the standard wrapper. The cause
  858. * is struct iw_priv_args, which was not really designed for the
  859. * job we are going here.
  860. *
  861. * IMPORTANT : This function prevent to set and get data on the same
  862. * IOCTL and enforce the SET/GET convention. Not doing it would be
  863. * far too hairy...
  864. * If you need to set and get data at the same time, please don't use
  865. * a iw_handler but process it in your ioctl handler (i.e. use the
  866. * old driver API).
  867. */
  868. static int get_priv_descr_and_size(struct net_device *dev, unsigned int cmd,
  869. const struct iw_priv_args **descrp)
  870. {
  871. const struct iw_priv_args *descr;
  872. int i, extra_size;
  873. descr = NULL;
  874. for (i = 0; i < dev->wireless_handlers->num_private_args; i++) {
  875. if (cmd == dev->wireless_handlers->private_args[i].cmd) {
  876. descr = &dev->wireless_handlers->private_args[i];
  877. break;
  878. }
  879. }
  880. extra_size = 0;
  881. if (descr) {
  882. if (IW_IS_SET(cmd)) {
  883. int offset = 0; /* For sub-ioctls */
  884. /* Check for sub-ioctl handler */
  885. if (descr->name[0] == '\0')
  886. /* Reserve one int for sub-ioctl index */
  887. offset = sizeof(__u32);
  888. /* Size of set arguments */
  889. extra_size = get_priv_size(descr->set_args);
  890. /* Does it fits in iwr ? */
  891. if ((descr->set_args & IW_PRIV_SIZE_FIXED) &&
  892. ((extra_size + offset) <= IFNAMSIZ))
  893. extra_size = 0;
  894. } else {
  895. /* Size of get arguments */
  896. extra_size = get_priv_size(descr->get_args);
  897. /* Does it fits in iwr ? */
  898. if ((descr->get_args & IW_PRIV_SIZE_FIXED) &&
  899. (extra_size <= IFNAMSIZ))
  900. extra_size = 0;
  901. }
  902. }
  903. *descrp = descr;
  904. return extra_size;
  905. }
  906. static int ioctl_private_iw_point(struct iw_point *iwp, unsigned int cmd,
  907. const struct iw_priv_args *descr,
  908. iw_handler handler, struct net_device *dev,
  909. struct iw_request_info *info, int extra_size)
  910. {
  911. char *extra;
  912. int err;
  913. /* Check what user space is giving us */
  914. if (IW_IS_SET(cmd)) {
  915. if (!iwp->pointer && iwp->length != 0)
  916. return -EFAULT;
  917. if (iwp->length > (descr->set_args & IW_PRIV_SIZE_MASK))
  918. return -E2BIG;
  919. } else if (!iwp->pointer)
  920. return -EFAULT;
  921. extra = kmalloc(extra_size, GFP_KERNEL);
  922. if (!extra)
  923. return -ENOMEM;
  924. /* If it is a SET, get all the extra data in here */
  925. if (IW_IS_SET(cmd) && (iwp->length != 0)) {
  926. if (copy_from_user(extra, iwp->pointer, extra_size)) {
  927. err = -EFAULT;
  928. goto out;
  929. }
  930. }
  931. /* Call the handler */
  932. err = handler(dev, info, (union iwreq_data *) iwp, extra);
  933. /* If we have something to return to the user */
  934. if (!err && IW_IS_GET(cmd)) {
  935. /* Adjust for the actual length if it's variable,
  936. * avoid leaking kernel bits outside.
  937. */
  938. if (!(descr->get_args & IW_PRIV_SIZE_FIXED))
  939. extra_size = adjust_priv_size(descr->get_args, iwp);
  940. if (copy_to_user(iwp->pointer, extra, extra_size))
  941. err = -EFAULT;
  942. }
  943. out:
  944. kfree(extra);
  945. return err;
  946. }
  947. static int ioctl_private_call(struct net_device *dev, struct iwreq *iwr,
  948. unsigned int cmd, struct iw_request_info *info,
  949. iw_handler handler)
  950. {
  951. int extra_size = 0, ret = -EINVAL;
  952. const struct iw_priv_args *descr;
  953. extra_size = get_priv_descr_and_size(dev, cmd, &descr);
  954. /* Check if we have a pointer to user space data or not. */
  955. if (extra_size == 0) {
  956. /* No extra arguments. Trivial to handle */
  957. ret = handler(dev, info, &(iwr->u), (char *) &(iwr->u));
  958. } else {
  959. ret = ioctl_private_iw_point(&iwr->u.data, cmd, descr,
  960. handler, dev, info, extra_size);
  961. }
  962. /* Call commit handler if needed and defined */
  963. if (ret == -EIWCOMMIT)
  964. ret = call_commit_handler(dev);
  965. return ret;
  966. }
  967. /* ---------------------------------------------------------------- */
  968. typedef int (*wext_ioctl_func)(struct net_device *, struct iwreq *,
  969. unsigned int, struct iw_request_info *,
  970. iw_handler);
  971. /*
  972. * Main IOCTl dispatcher.
  973. * Check the type of IOCTL and call the appropriate wrapper...
  974. */
  975. static int wireless_process_ioctl(struct net *net, struct ifreq *ifr,
  976. unsigned int cmd,
  977. struct iw_request_info *info,
  978. wext_ioctl_func standard,
  979. wext_ioctl_func private)
  980. {
  981. struct iwreq *iwr = (struct iwreq *) ifr;
  982. struct net_device *dev;
  983. iw_handler handler;
  984. /* Permissions are already checked in dev_ioctl() before calling us.
  985. * The copy_to/from_user() of ifr is also dealt with in there */
  986. /* Make sure the device exist */
  987. if ((dev = __dev_get_by_name(net, ifr->ifr_name)) == NULL)
  988. return -ENODEV;
  989. /* A bunch of special cases, then the generic case...
  990. * Note that 'cmd' is already filtered in dev_ioctl() with
  991. * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
  992. if (cmd == SIOCGIWSTATS)
  993. return standard(dev, iwr, cmd, info,
  994. &iw_handler_get_iwstats);
  995. if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
  996. return standard(dev, iwr, cmd, info,
  997. &iw_handler_get_private);
  998. /* Basic check */
  999. if (!netif_device_present(dev))
  1000. return -ENODEV;
  1001. /* New driver API : try to find the handler */
  1002. handler = get_handler(dev, cmd);
  1003. if (handler) {
  1004. /* Standard and private are not the same */
  1005. if (cmd < SIOCIWFIRSTPRIV)
  1006. return standard(dev, iwr, cmd, info, handler);
  1007. else
  1008. return private(dev, iwr, cmd, info, handler);
  1009. }
  1010. /* Old driver API : call driver ioctl handler */
  1011. if (dev->netdev_ops->ndo_do_ioctl)
  1012. return dev->netdev_ops->ndo_do_ioctl(dev, ifr, cmd);
  1013. return -EOPNOTSUPP;
  1014. }
  1015. /* If command is `set a parameter', or `get the encoding parameters',
  1016. * check if the user has the right to do it.
  1017. */
  1018. static int wext_permission_check(unsigned int cmd)
  1019. {
  1020. if ((IW_IS_SET(cmd) || cmd == SIOCGIWENCODE || cmd == SIOCGIWENCODEEXT)
  1021. && !capable(CAP_NET_ADMIN))
  1022. return -EPERM;
  1023. return 0;
  1024. }
  1025. /* entry point from dev ioctl */
  1026. static int wext_ioctl_dispatch(struct net *net, struct ifreq *ifr,
  1027. unsigned int cmd, struct iw_request_info *info,
  1028. wext_ioctl_func standard,
  1029. wext_ioctl_func private)
  1030. {
  1031. int ret = wext_permission_check(cmd);
  1032. if (ret)
  1033. return ret;
  1034. dev_load(net, ifr->ifr_name);
  1035. rtnl_lock();
  1036. ret = wireless_process_ioctl(net, ifr, cmd, info, standard, private);
  1037. rtnl_unlock();
  1038. return ret;
  1039. }
  1040. int wext_handle_ioctl(struct net *net, struct ifreq *ifr, unsigned int cmd,
  1041. void __user *arg)
  1042. {
  1043. struct iw_request_info info = { .cmd = cmd, .flags = 0 };
  1044. int ret;
  1045. ret = wext_ioctl_dispatch(net, ifr, cmd, &info,
  1046. ioctl_standard_call,
  1047. ioctl_private_call);
  1048. if (ret >= 0 &&
  1049. IW_IS_GET(cmd) &&
  1050. copy_to_user(arg, ifr, sizeof(struct iwreq)))
  1051. return -EFAULT;
  1052. return ret;
  1053. }
  1054. #ifdef CONFIG_COMPAT
  1055. static int compat_standard_call(struct net_device *dev,
  1056. struct iwreq *iwr,
  1057. unsigned int cmd,
  1058. struct iw_request_info *info,
  1059. iw_handler handler)
  1060. {
  1061. const struct iw_ioctl_description *descr;
  1062. struct compat_iw_point *iwp_compat;
  1063. struct iw_point iwp;
  1064. int err;
  1065. descr = standard_ioctl + (cmd - SIOCIWFIRST);
  1066. if (descr->header_type != IW_HEADER_TYPE_POINT)
  1067. return ioctl_standard_call(dev, iwr, cmd, info, handler);
  1068. iwp_compat = (struct compat_iw_point *) &iwr->u.data;
  1069. iwp.pointer = compat_ptr(iwp_compat->pointer);
  1070. iwp.length = iwp_compat->length;
  1071. iwp.flags = iwp_compat->flags;
  1072. err = ioctl_standard_iw_point(&iwp, cmd, descr, handler, dev, info);
  1073. iwp_compat->pointer = ptr_to_compat(iwp.pointer);
  1074. iwp_compat->length = iwp.length;
  1075. iwp_compat->flags = iwp.flags;
  1076. return err;
  1077. }
  1078. static int compat_private_call(struct net_device *dev, struct iwreq *iwr,
  1079. unsigned int cmd, struct iw_request_info *info,
  1080. iw_handler handler)
  1081. {
  1082. const struct iw_priv_args *descr;
  1083. int ret, extra_size;
  1084. extra_size = get_priv_descr_and_size(dev, cmd, &descr);
  1085. /* Check if we have a pointer to user space data or not. */
  1086. if (extra_size == 0) {
  1087. /* No extra arguments. Trivial to handle */
  1088. ret = handler(dev, info, &(iwr->u), (char *) &(iwr->u));
  1089. } else {
  1090. struct compat_iw_point *iwp_compat;
  1091. struct iw_point iwp;
  1092. iwp_compat = (struct compat_iw_point *) &iwr->u.data;
  1093. iwp.pointer = compat_ptr(iwp_compat->pointer);
  1094. iwp.length = iwp_compat->length;
  1095. iwp.flags = iwp_compat->flags;
  1096. ret = ioctl_private_iw_point(&iwp, cmd, descr,
  1097. handler, dev, info, extra_size);
  1098. iwp_compat->pointer = ptr_to_compat(iwp.pointer);
  1099. iwp_compat->length = iwp.length;
  1100. iwp_compat->flags = iwp.flags;
  1101. }
  1102. /* Call commit handler if needed and defined */
  1103. if (ret == -EIWCOMMIT)
  1104. ret = call_commit_handler(dev);
  1105. return ret;
  1106. }
  1107. int compat_wext_handle_ioctl(struct net *net, unsigned int cmd,
  1108. unsigned long arg)
  1109. {
  1110. void __user *argp = (void __user *)arg;
  1111. struct iw_request_info info;
  1112. struct iwreq iwr;
  1113. char *colon;
  1114. int ret;
  1115. if (copy_from_user(&iwr, argp, sizeof(struct iwreq)))
  1116. return -EFAULT;
  1117. iwr.ifr_name[IFNAMSIZ-1] = 0;
  1118. colon = strchr(iwr.ifr_name, ':');
  1119. if (colon)
  1120. *colon = 0;
  1121. info.cmd = cmd;
  1122. info.flags = IW_REQUEST_FLAG_COMPAT;
  1123. ret = wext_ioctl_dispatch(net, (struct ifreq *) &iwr, cmd, &info,
  1124. compat_standard_call,
  1125. compat_private_call);
  1126. if (ret >= 0 &&
  1127. IW_IS_GET(cmd) &&
  1128. copy_to_user(argp, &iwr, sizeof(struct iwreq)))
  1129. return -EFAULT;
  1130. return ret;
  1131. }
  1132. #endif
  1133. static int __net_init wext_pernet_init(struct net *net)
  1134. {
  1135. skb_queue_head_init(&net->wext_nlevents);
  1136. return 0;
  1137. }
  1138. static void __net_exit wext_pernet_exit(struct net *net)
  1139. {
  1140. skb_queue_purge(&net->wext_nlevents);
  1141. }
  1142. static struct pernet_operations wext_pernet_ops = {
  1143. .init = wext_pernet_init,
  1144. .exit = wext_pernet_exit,
  1145. };
  1146. static int __init wireless_nlevent_init(void)
  1147. {
  1148. return register_pernet_subsys(&wext_pernet_ops);
  1149. }
  1150. subsys_initcall(wireless_nlevent_init);
  1151. /* Process events generated by the wireless layer or the driver. */
  1152. static void wireless_nlevent_process(struct work_struct *work)
  1153. {
  1154. struct sk_buff *skb;
  1155. struct net *net;
  1156. rtnl_lock();
  1157. for_each_net(net) {
  1158. while ((skb = skb_dequeue(&net->wext_nlevents)))
  1159. rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL,
  1160. GFP_KERNEL);
  1161. }
  1162. rtnl_unlock();
  1163. }
  1164. static DECLARE_WORK(wireless_nlevent_work, wireless_nlevent_process);
  1165. static struct nlmsghdr *rtnetlink_ifinfo_prep(struct net_device *dev,
  1166. struct sk_buff *skb)
  1167. {
  1168. struct ifinfomsg *r;
  1169. struct nlmsghdr *nlh;
  1170. nlh = nlmsg_put(skb, 0, 0, RTM_NEWLINK, sizeof(*r), 0);
  1171. if (!nlh)
  1172. return NULL;
  1173. r = nlmsg_data(nlh);
  1174. r->ifi_family = AF_UNSPEC;
  1175. r->__ifi_pad = 0;
  1176. r->ifi_type = dev->type;
  1177. r->ifi_index = dev->ifindex;
  1178. r->ifi_flags = dev_get_flags(dev);
  1179. r->ifi_change = 0; /* Wireless changes don't affect those flags */
  1180. NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
  1181. return nlh;
  1182. nla_put_failure:
  1183. nlmsg_cancel(skb, nlh);
  1184. return NULL;
  1185. }
  1186. /*
  1187. * Main event dispatcher. Called from other parts and drivers.
  1188. * Send the event on the appropriate channels.
  1189. * May be called from interrupt context.
  1190. */
  1191. void wireless_send_event(struct net_device * dev,
  1192. unsigned int cmd,
  1193. union iwreq_data * wrqu,
  1194. const char * extra)
  1195. {
  1196. const struct iw_ioctl_description * descr = NULL;
  1197. int extra_len = 0;
  1198. struct iw_event *event; /* Mallocated whole event */
  1199. int event_len; /* Its size */
  1200. int hdr_len; /* Size of the event header */
  1201. int wrqu_off = 0; /* Offset in wrqu */
  1202. /* Don't "optimise" the following variable, it will crash */
  1203. unsigned cmd_index; /* *MUST* be unsigned */
  1204. struct sk_buff *skb;
  1205. struct nlmsghdr *nlh;
  1206. struct nlattr *nla;
  1207. #ifdef CONFIG_COMPAT
  1208. struct __compat_iw_event *compat_event;
  1209. struct compat_iw_point compat_wrqu;
  1210. struct sk_buff *compskb;
  1211. #endif
  1212. /*
  1213. * Nothing in the kernel sends scan events with data, be safe.
  1214. * This is necessary because we cannot fix up scan event data
  1215. * for compat, due to being contained in 'extra', but normally
  1216. * applications are required to retrieve the scan data anyway
  1217. * and no data is included in the event, this codifies that
  1218. * practice.
  1219. */
  1220. if (WARN_ON(cmd == SIOCGIWSCAN && extra))
  1221. extra = NULL;
  1222. /* Get the description of the Event */
  1223. if (cmd <= SIOCIWLAST) {
  1224. cmd_index = cmd - SIOCIWFIRST;
  1225. if (cmd_index < standard_ioctl_num)
  1226. descr = &(standard_ioctl[cmd_index]);
  1227. } else {
  1228. cmd_index = cmd - IWEVFIRST;
  1229. if (cmd_index < standard_event_num)
  1230. descr = &(standard_event[cmd_index]);
  1231. }
  1232. /* Don't accept unknown events */
  1233. if (descr == NULL) {
  1234. /* Note : we don't return an error to the driver, because
  1235. * the driver would not know what to do about it. It can't
  1236. * return an error to the user, because the event is not
  1237. * initiated by a user request.
  1238. * The best the driver could do is to log an error message.
  1239. * We will do it ourselves instead...
  1240. */
  1241. printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
  1242. dev->name, cmd);
  1243. return;
  1244. }
  1245. /* Check extra parameters and set extra_len */
  1246. if (descr->header_type == IW_HEADER_TYPE_POINT) {
  1247. /* Check if number of token fits within bounds */
  1248. if (wrqu->data.length > descr->max_tokens) {
  1249. printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length);
  1250. return;
  1251. }
  1252. if (wrqu->data.length < descr->min_tokens) {
  1253. printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length);
  1254. return;
  1255. }
  1256. /* Calculate extra_len - extra is NULL for restricted events */
  1257. if (extra != NULL)
  1258. extra_len = wrqu->data.length * descr->token_size;
  1259. /* Always at an offset in wrqu */
  1260. wrqu_off = IW_EV_POINT_OFF;
  1261. }
  1262. /* Total length of the event */
  1263. hdr_len = event_type_size[descr->header_type];
  1264. event_len = hdr_len + extra_len;
  1265. /*
  1266. * The problem for 64/32 bit.
  1267. *
  1268. * On 64-bit, a regular event is laid out as follows:
  1269. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  1270. * | event.len | event.cmd | p a d d i n g |
  1271. * | wrqu data ... (with the correct size) |
  1272. *
  1273. * This padding exists because we manipulate event->u,
  1274. * and 'event' is not packed.
  1275. *
  1276. * An iw_point event is laid out like this instead:
  1277. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  1278. * | event.len | event.cmd | p a d d i n g |
  1279. * | iwpnt.len | iwpnt.flg | p a d d i n g |
  1280. * | extra data ...
  1281. *
  1282. * The second padding exists because struct iw_point is extended,
  1283. * but this depends on the platform...
  1284. *
  1285. * On 32-bit, all the padding shouldn't be there.
  1286. */
  1287. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1288. if (!skb)
  1289. return;
  1290. /* Send via the RtNetlink event channel */
  1291. nlh = rtnetlink_ifinfo_prep(dev, skb);
  1292. if (WARN_ON(!nlh)) {
  1293. kfree_skb(skb);
  1294. return;
  1295. }
  1296. /* Add the wireless events in the netlink packet */
  1297. nla = nla_reserve(skb, IFLA_WIRELESS, event_len);
  1298. if (!nla) {
  1299. kfree_skb(skb);
  1300. return;
  1301. }
  1302. event = nla_data(nla);
  1303. /* Fill event - first clear to avoid data leaking */
  1304. memset(event, 0, hdr_len);
  1305. event->len = event_len;
  1306. event->cmd = cmd;
  1307. memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
  1308. if (extra_len)
  1309. memcpy(((char *) event) + hdr_len, extra, extra_len);
  1310. nlmsg_end(skb, nlh);
  1311. #ifdef CONFIG_COMPAT
  1312. hdr_len = compat_event_type_size[descr->header_type];
  1313. event_len = hdr_len + extra_len;
  1314. compskb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1315. if (!compskb) {
  1316. kfree_skb(skb);
  1317. return;
  1318. }
  1319. /* Send via the RtNetlink event channel */
  1320. nlh = rtnetlink_ifinfo_prep(dev, compskb);
  1321. if (WARN_ON(!nlh)) {
  1322. kfree_skb(skb);
  1323. kfree_skb(compskb);
  1324. return;
  1325. }
  1326. /* Add the wireless events in the netlink packet */
  1327. nla = nla_reserve(compskb, IFLA_WIRELESS, event_len);
  1328. if (!nla) {
  1329. kfree_skb(skb);
  1330. kfree_skb(compskb);
  1331. return;
  1332. }
  1333. compat_event = nla_data(nla);
  1334. compat_event->len = event_len;
  1335. compat_event->cmd = cmd;
  1336. if (descr->header_type == IW_HEADER_TYPE_POINT) {
  1337. compat_wrqu.length = wrqu->data.length;
  1338. compat_wrqu.flags = wrqu->data.flags;
  1339. memcpy(&compat_event->pointer,
  1340. ((char *) &compat_wrqu) + IW_EV_COMPAT_POINT_OFF,
  1341. hdr_len - IW_EV_COMPAT_LCP_LEN);
  1342. if (extra_len)
  1343. memcpy(((char *) compat_event) + hdr_len,
  1344. extra, extra_len);
  1345. } else {
  1346. /* extra_len must be zero, so no if (extra) needed */
  1347. memcpy(&compat_event->pointer, wrqu,
  1348. hdr_len - IW_EV_COMPAT_LCP_LEN);
  1349. }
  1350. nlmsg_end(compskb, nlh);
  1351. skb_shinfo(skb)->frag_list = compskb;
  1352. #endif
  1353. skb_queue_tail(&dev_net(dev)->wext_nlevents, skb);
  1354. schedule_work(&wireless_nlevent_work);
  1355. }
  1356. EXPORT_SYMBOL(wireless_send_event);
  1357. /********************** ENHANCED IWSPY SUPPORT **********************/
  1358. /*
  1359. * In the old days, the driver was handling spy support all by itself.
  1360. * Now, the driver can delegate this task to Wireless Extensions.
  1361. * It needs to use those standard spy iw_handler in struct iw_handler_def,
  1362. * push data to us via wireless_spy_update() and include struct iw_spy_data
  1363. * in its private part (and export it in net_device->wireless_data->spy_data).
  1364. * One of the main advantage of centralising spy support here is that
  1365. * it becomes much easier to improve and extend it without having to touch
  1366. * the drivers. One example is the addition of the Spy-Threshold events.
  1367. */
  1368. /* ---------------------------------------------------------------- */
  1369. /*
  1370. * Return the pointer to the spy data in the driver.
  1371. * Because this is called on the Rx path via wireless_spy_update(),
  1372. * we want it to be efficient...
  1373. */
  1374. static inline struct iw_spy_data *get_spydata(struct net_device *dev)
  1375. {
  1376. /* This is the new way */
  1377. if (dev->wireless_data)
  1378. return dev->wireless_data->spy_data;
  1379. return NULL;
  1380. }
  1381. /*------------------------------------------------------------------*/
  1382. /*
  1383. * Standard Wireless Handler : set Spy List
  1384. */
  1385. int iw_handler_set_spy(struct net_device * dev,
  1386. struct iw_request_info * info,
  1387. union iwreq_data * wrqu,
  1388. char * extra)
  1389. {
  1390. struct iw_spy_data * spydata = get_spydata(dev);
  1391. struct sockaddr * address = (struct sockaddr *) extra;
  1392. /* Make sure driver is not buggy or using the old API */
  1393. if (!spydata)
  1394. return -EOPNOTSUPP;
  1395. /* Disable spy collection while we copy the addresses.
  1396. * While we copy addresses, any call to wireless_spy_update()
  1397. * will NOP. This is OK, as anyway the addresses are changing. */
  1398. spydata->spy_number = 0;
  1399. /* We want to operate without locking, because wireless_spy_update()
  1400. * most likely will happen in the interrupt handler, and therefore
  1401. * have its own locking constraints and needs performance.
  1402. * The rtnl_lock() make sure we don't race with the other iw_handlers.
  1403. * This make sure wireless_spy_update() "see" that the spy list
  1404. * is temporarily disabled. */
  1405. smp_wmb();
  1406. /* Are there are addresses to copy? */
  1407. if (wrqu->data.length > 0) {
  1408. int i;
  1409. /* Copy addresses */
  1410. for (i = 0; i < wrqu->data.length; i++)
  1411. memcpy(spydata->spy_address[i], address[i].sa_data,
  1412. ETH_ALEN);
  1413. /* Reset stats */
  1414. memset(spydata->spy_stat, 0,
  1415. sizeof(struct iw_quality) * IW_MAX_SPY);
  1416. }
  1417. /* Make sure above is updated before re-enabling */
  1418. smp_wmb();
  1419. /* Enable addresses */
  1420. spydata->spy_number = wrqu->data.length;
  1421. return 0;
  1422. }
  1423. EXPORT_SYMBOL(iw_handler_set_spy);
  1424. /*------------------------------------------------------------------*/
  1425. /*
  1426. * Standard Wireless Handler : get Spy List
  1427. */
  1428. int iw_handler_get_spy(struct net_device * dev,
  1429. struct iw_request_info * info,
  1430. union iwreq_data * wrqu,
  1431. char * extra)
  1432. {
  1433. struct iw_spy_data * spydata = get_spydata(dev);
  1434. struct sockaddr * address = (struct sockaddr *) extra;
  1435. int i;
  1436. /* Make sure driver is not buggy or using the old API */
  1437. if (!spydata)
  1438. return -EOPNOTSUPP;
  1439. wrqu->data.length = spydata->spy_number;
  1440. /* Copy addresses. */
  1441. for (i = 0; i < spydata->spy_number; i++) {
  1442. memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
  1443. address[i].sa_family = AF_UNIX;
  1444. }
  1445. /* Copy stats to the user buffer (just after). */
  1446. if (spydata->spy_number > 0)
  1447. memcpy(extra + (sizeof(struct sockaddr) *spydata->spy_number),
  1448. spydata->spy_stat,
  1449. sizeof(struct iw_quality) * spydata->spy_number);
  1450. /* Reset updated flags. */
  1451. for (i = 0; i < spydata->spy_number; i++)
  1452. spydata->spy_stat[i].updated &= ~IW_QUAL_ALL_UPDATED;
  1453. return 0;
  1454. }
  1455. EXPORT_SYMBOL(iw_handler_get_spy);
  1456. /*------------------------------------------------------------------*/
  1457. /*
  1458. * Standard Wireless Handler : set spy threshold
  1459. */
  1460. int iw_handler_set_thrspy(struct net_device * dev,
  1461. struct iw_request_info *info,
  1462. union iwreq_data * wrqu,
  1463. char * extra)
  1464. {
  1465. struct iw_spy_data * spydata = get_spydata(dev);
  1466. struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
  1467. /* Make sure driver is not buggy or using the old API */
  1468. if (!spydata)
  1469. return -EOPNOTSUPP;
  1470. /* Just do it */
  1471. memcpy(&(spydata->spy_thr_low), &(threshold->low),
  1472. 2 * sizeof(struct iw_quality));
  1473. /* Clear flag */
  1474. memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
  1475. return 0;
  1476. }
  1477. EXPORT_SYMBOL(iw_handler_set_thrspy);
  1478. /*------------------------------------------------------------------*/
  1479. /*
  1480. * Standard Wireless Handler : get spy threshold
  1481. */
  1482. int iw_handler_get_thrspy(struct net_device * dev,
  1483. struct iw_request_info *info,
  1484. union iwreq_data * wrqu,
  1485. char * extra)
  1486. {
  1487. struct iw_spy_data * spydata = get_spydata(dev);
  1488. struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
  1489. /* Make sure driver is not buggy or using the old API */
  1490. if (!spydata)
  1491. return -EOPNOTSUPP;
  1492. /* Just do it */
  1493. memcpy(&(threshold->low), &(spydata->spy_thr_low),
  1494. 2 * sizeof(struct iw_quality));
  1495. return 0;
  1496. }
  1497. EXPORT_SYMBOL(iw_handler_get_thrspy);
  1498. /*------------------------------------------------------------------*/
  1499. /*
  1500. * Prepare and send a Spy Threshold event
  1501. */
  1502. static void iw_send_thrspy_event(struct net_device * dev,
  1503. struct iw_spy_data * spydata,
  1504. unsigned char * address,
  1505. struct iw_quality * wstats)
  1506. {
  1507. union iwreq_data wrqu;
  1508. struct iw_thrspy threshold;
  1509. /* Init */
  1510. wrqu.data.length = 1;
  1511. wrqu.data.flags = 0;
  1512. /* Copy address */
  1513. memcpy(threshold.addr.sa_data, address, ETH_ALEN);
  1514. threshold.addr.sa_family = ARPHRD_ETHER;
  1515. /* Copy stats */
  1516. memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality));
  1517. /* Copy also thresholds */
  1518. memcpy(&(threshold.low), &(spydata->spy_thr_low),
  1519. 2 * sizeof(struct iw_quality));
  1520. /* Send event to user space */
  1521. wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
  1522. }
  1523. /* ---------------------------------------------------------------- */
  1524. /*
  1525. * Call for the driver to update the spy data.
  1526. * For now, the spy data is a simple array. As the size of the array is
  1527. * small, this is good enough. If we wanted to support larger number of
  1528. * spy addresses, we should use something more efficient...
  1529. */
  1530. void wireless_spy_update(struct net_device * dev,
  1531. unsigned char * address,
  1532. struct iw_quality * wstats)
  1533. {
  1534. struct iw_spy_data * spydata = get_spydata(dev);
  1535. int i;
  1536. int match = -1;
  1537. /* Make sure driver is not buggy or using the old API */
  1538. if (!spydata)
  1539. return;
  1540. /* Update all records that match */
  1541. for (i = 0; i < spydata->spy_number; i++)
  1542. if (!compare_ether_addr(address, spydata->spy_address[i])) {
  1543. memcpy(&(spydata->spy_stat[i]), wstats,
  1544. sizeof(struct iw_quality));
  1545. match = i;
  1546. }
  1547. /* Generate an event if we cross the spy threshold.
  1548. * To avoid event storms, we have a simple hysteresis : we generate
  1549. * event only when we go under the low threshold or above the
  1550. * high threshold. */
  1551. if (match >= 0) {
  1552. if (spydata->spy_thr_under[match]) {
  1553. if (wstats->level > spydata->spy_thr_high.level) {
  1554. spydata->spy_thr_under[match] = 0;
  1555. iw_send_thrspy_event(dev, spydata,
  1556. address, wstats);
  1557. }
  1558. } else {
  1559. if (wstats->level < spydata->spy_thr_low.level) {
  1560. spydata->spy_thr_under[match] = 1;
  1561. iw_send_thrspy_event(dev, spydata,
  1562. address, wstats);
  1563. }
  1564. }
  1565. }
  1566. }
  1567. EXPORT_SYMBOL(wireless_spy_update);