wireless.c 45 KB

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