wireless.c 46 KB

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