wireless.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  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. extern void *dev_seq_start(struct seq_file *seq, loff_t *pos);
  542. extern void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos);
  543. extern void dev_seq_stop(struct seq_file *seq, void *v);
  544. static struct seq_operations wireless_seq_ops = {
  545. .start = dev_seq_start,
  546. .next = dev_seq_next,
  547. .stop = dev_seq_stop,
  548. .show = wireless_seq_show,
  549. };
  550. static int wireless_seq_open(struct inode *inode, struct file *file)
  551. {
  552. return seq_open(file, &wireless_seq_ops);
  553. }
  554. static struct file_operations wireless_seq_fops = {
  555. .owner = THIS_MODULE,
  556. .open = wireless_seq_open,
  557. .read = seq_read,
  558. .llseek = seq_lseek,
  559. .release = seq_release,
  560. };
  561. int __init wireless_proc_init(void)
  562. {
  563. if (!proc_net_fops_create("wireless", S_IRUGO, &wireless_seq_fops))
  564. return -ENOMEM;
  565. return 0;
  566. }
  567. #endif /* CONFIG_PROC_FS */
  568. /************************** IOCTL SUPPORT **************************/
  569. /*
  570. * The original user space API to configure all those Wireless Extensions
  571. * is through IOCTLs.
  572. * In there, we check if we need to call the new driver API (iw_handler)
  573. * or just call the driver ioctl handler.
  574. */
  575. /* ---------------------------------------------------------------- */
  576. /*
  577. * Allow programatic access to /proc/net/wireless even if /proc
  578. * doesn't exist... Also more efficient...
  579. */
  580. static inline int dev_iwstats(struct net_device *dev, struct ifreq *ifr)
  581. {
  582. /* Get stats from the driver */
  583. struct iw_statistics *stats;
  584. stats = get_wireless_stats(dev);
  585. if (stats != (struct iw_statistics *) NULL) {
  586. struct iwreq * wrq = (struct iwreq *)ifr;
  587. /* Copy statistics to the user buffer */
  588. if(copy_to_user(wrq->u.data.pointer, stats,
  589. sizeof(struct iw_statistics)))
  590. return -EFAULT;
  591. /* Check if we need to clear the update flag */
  592. if(wrq->u.data.flags != 0)
  593. stats->qual.updated = 0;
  594. return 0;
  595. } else
  596. return -EOPNOTSUPP;
  597. }
  598. /* ---------------------------------------------------------------- */
  599. /*
  600. * Export the driver private handler definition
  601. * They will be picked up by tools like iwpriv...
  602. */
  603. static inline int ioctl_export_private(struct net_device * dev,
  604. struct ifreq * ifr)
  605. {
  606. struct iwreq * iwr = (struct iwreq *) ifr;
  607. /* Check if the driver has something to export */
  608. if((dev->wireless_handlers->num_private_args == 0) ||
  609. (dev->wireless_handlers->private_args == NULL))
  610. return -EOPNOTSUPP;
  611. /* Check NULL pointer */
  612. if(iwr->u.data.pointer == NULL)
  613. return -EFAULT;
  614. /* Check if there is enough buffer up there */
  615. if(iwr->u.data.length < dev->wireless_handlers->num_private_args) {
  616. /* User space can't know in advance how large the buffer
  617. * needs to be. Give it a hint, so that we can support
  618. * any size buffer we want somewhat efficiently... */
  619. iwr->u.data.length = dev->wireless_handlers->num_private_args;
  620. return -E2BIG;
  621. }
  622. /* Set the number of available ioctls. */
  623. iwr->u.data.length = dev->wireless_handlers->num_private_args;
  624. /* Copy structure to the user buffer. */
  625. if (copy_to_user(iwr->u.data.pointer,
  626. dev->wireless_handlers->private_args,
  627. sizeof(struct iw_priv_args) * iwr->u.data.length))
  628. return -EFAULT;
  629. return 0;
  630. }
  631. /* ---------------------------------------------------------------- */
  632. /*
  633. * Wrapper to call a standard Wireless Extension handler.
  634. * We do various checks and also take care of moving data between
  635. * user space and kernel space.
  636. */
  637. static inline int ioctl_standard_call(struct net_device * dev,
  638. struct ifreq * ifr,
  639. unsigned int cmd,
  640. iw_handler handler)
  641. {
  642. struct iwreq * iwr = (struct iwreq *) ifr;
  643. const struct iw_ioctl_description * descr;
  644. struct iw_request_info info;
  645. int ret = -EINVAL;
  646. /* Get the description of the IOCTL */
  647. if((cmd - SIOCIWFIRST) >= standard_ioctl_num)
  648. return -EOPNOTSUPP;
  649. descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
  650. #ifdef WE_IOCTL_DEBUG
  651. printk(KERN_DEBUG "%s (WE) : Found standard handler for 0x%04X\n",
  652. ifr->ifr_name, cmd);
  653. 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);
  654. #endif /* WE_IOCTL_DEBUG */
  655. /* Prepare the call */
  656. info.cmd = cmd;
  657. info.flags = 0;
  658. /* Check if we have a pointer to user space data or not */
  659. if(descr->header_type != IW_HEADER_TYPE_POINT) {
  660. /* No extra arguments. Trivial to handle */
  661. ret = handler(dev, &info, &(iwr->u), NULL);
  662. #ifdef WE_SET_EVENT
  663. /* Generate an event to notify listeners of the change */
  664. if((descr->flags & IW_DESCR_FLAG_EVENT) &&
  665. ((ret == 0) || (ret == -EIWCOMMIT)))
  666. wireless_send_event(dev, cmd, &(iwr->u), NULL);
  667. #endif /* WE_SET_EVENT */
  668. } else {
  669. char * extra;
  670. int extra_size;
  671. int user_length = 0;
  672. int err;
  673. /* Calculate space needed by arguments. Always allocate
  674. * for max space. Easier, and won't last long... */
  675. extra_size = descr->max_tokens * descr->token_size;
  676. /* Check what user space is giving us */
  677. if(IW_IS_SET(cmd)) {
  678. /* Check NULL pointer */
  679. if((iwr->u.data.pointer == NULL) &&
  680. (iwr->u.data.length != 0))
  681. return -EFAULT;
  682. /* Check if number of token fits within bounds */
  683. if(iwr->u.data.length > descr->max_tokens)
  684. return -E2BIG;
  685. if(iwr->u.data.length < descr->min_tokens)
  686. return -EINVAL;
  687. } else {
  688. /* Check NULL pointer */
  689. if(iwr->u.data.pointer == NULL)
  690. return -EFAULT;
  691. /* Save user space buffer size for checking */
  692. user_length = iwr->u.data.length;
  693. /* Don't check if user_length > max to allow forward
  694. * compatibility. The test user_length < min is
  695. * implied by the test at the end. */
  696. /* Support for very large requests */
  697. if((descr->flags & IW_DESCR_FLAG_NOMAX) &&
  698. (user_length > descr->max_tokens)) {
  699. /* Allow userspace to GET more than max so
  700. * we can support any size GET requests.
  701. * There is still a limit : -ENOMEM. */
  702. extra_size = user_length * descr->token_size;
  703. /* Note : user_length is originally a __u16,
  704. * and token_size is controlled by us,
  705. * so extra_size won't get negative and
  706. * won't overflow... */
  707. }
  708. }
  709. #ifdef WE_IOCTL_DEBUG
  710. printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
  711. dev->name, extra_size);
  712. #endif /* WE_IOCTL_DEBUG */
  713. /* Create the kernel buffer */
  714. extra = kmalloc(extra_size, GFP_KERNEL);
  715. if (extra == NULL) {
  716. return -ENOMEM;
  717. }
  718. /* If it is a SET, get all the extra data in here */
  719. if(IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
  720. err = copy_from_user(extra, iwr->u.data.pointer,
  721. iwr->u.data.length *
  722. descr->token_size);
  723. if (err) {
  724. kfree(extra);
  725. return -EFAULT;
  726. }
  727. #ifdef WE_IOCTL_DEBUG
  728. printk(KERN_DEBUG "%s (WE) : Got %d bytes\n",
  729. dev->name,
  730. iwr->u.data.length * descr->token_size);
  731. #endif /* WE_IOCTL_DEBUG */
  732. }
  733. /* Call the handler */
  734. ret = handler(dev, &info, &(iwr->u), extra);
  735. /* If we have something to return to the user */
  736. if (!ret && IW_IS_GET(cmd)) {
  737. /* Check if there is enough buffer up there */
  738. if(user_length < iwr->u.data.length) {
  739. kfree(extra);
  740. return -E2BIG;
  741. }
  742. err = copy_to_user(iwr->u.data.pointer, extra,
  743. iwr->u.data.length *
  744. descr->token_size);
  745. if (err)
  746. ret = -EFAULT;
  747. #ifdef WE_IOCTL_DEBUG
  748. printk(KERN_DEBUG "%s (WE) : Wrote %d bytes\n",
  749. dev->name,
  750. iwr->u.data.length * descr->token_size);
  751. #endif /* WE_IOCTL_DEBUG */
  752. }
  753. #ifdef WE_SET_EVENT
  754. /* Generate an event to notify listeners of the change */
  755. if((descr->flags & IW_DESCR_FLAG_EVENT) &&
  756. ((ret == 0) || (ret == -EIWCOMMIT))) {
  757. if(descr->flags & IW_DESCR_FLAG_RESTRICT)
  758. /* If the event is restricted, don't
  759. * export the payload */
  760. wireless_send_event(dev, cmd, &(iwr->u), NULL);
  761. else
  762. wireless_send_event(dev, cmd, &(iwr->u),
  763. extra);
  764. }
  765. #endif /* WE_SET_EVENT */
  766. /* Cleanup - I told you it wasn't that long ;-) */
  767. kfree(extra);
  768. }
  769. /* Call commit handler if needed and defined */
  770. if(ret == -EIWCOMMIT)
  771. ret = call_commit_handler(dev);
  772. /* Here, we will generate the appropriate event if needed */
  773. return ret;
  774. }
  775. /* ---------------------------------------------------------------- */
  776. /*
  777. * Wrapper to call a private Wireless Extension handler.
  778. * We do various checks and also take care of moving data between
  779. * user space and kernel space.
  780. * It's not as nice and slimline as the standard wrapper. The cause
  781. * is struct iw_priv_args, which was not really designed for the
  782. * job we are going here.
  783. *
  784. * IMPORTANT : This function prevent to set and get data on the same
  785. * IOCTL and enforce the SET/GET convention. Not doing it would be
  786. * far too hairy...
  787. * If you need to set and get data at the same time, please don't use
  788. * a iw_handler but process it in your ioctl handler (i.e. use the
  789. * old driver API).
  790. */
  791. static inline int ioctl_private_call(struct net_device * dev,
  792. struct ifreq * ifr,
  793. unsigned int cmd,
  794. iw_handler handler)
  795. {
  796. struct iwreq * iwr = (struct iwreq *) ifr;
  797. const struct iw_priv_args * descr = NULL;
  798. struct iw_request_info info;
  799. int extra_size = 0;
  800. int i;
  801. int ret = -EINVAL;
  802. /* Get the description of the IOCTL */
  803. for(i = 0; i < dev->wireless_handlers->num_private_args; i++)
  804. if(cmd == dev->wireless_handlers->private_args[i].cmd) {
  805. descr = &(dev->wireless_handlers->private_args[i]);
  806. break;
  807. }
  808. #ifdef WE_IOCTL_DEBUG
  809. printk(KERN_DEBUG "%s (WE) : Found private handler for 0x%04X\n",
  810. ifr->ifr_name, cmd);
  811. if(descr) {
  812. printk(KERN_DEBUG "%s (WE) : Name %s, set %X, get %X\n",
  813. dev->name, descr->name,
  814. descr->set_args, descr->get_args);
  815. }
  816. #endif /* WE_IOCTL_DEBUG */
  817. /* Compute the size of the set/get arguments */
  818. if(descr != NULL) {
  819. if(IW_IS_SET(cmd)) {
  820. int offset = 0; /* For sub-ioctls */
  821. /* Check for sub-ioctl handler */
  822. if(descr->name[0] == '\0')
  823. /* Reserve one int for sub-ioctl index */
  824. offset = sizeof(__u32);
  825. /* Size of set arguments */
  826. extra_size = get_priv_size(descr->set_args);
  827. /* Does it fits in iwr ? */
  828. if((descr->set_args & IW_PRIV_SIZE_FIXED) &&
  829. ((extra_size + offset) <= IFNAMSIZ))
  830. extra_size = 0;
  831. } else {
  832. /* Size of get arguments */
  833. extra_size = get_priv_size(descr->get_args);
  834. /* Does it fits in iwr ? */
  835. if((descr->get_args & IW_PRIV_SIZE_FIXED) &&
  836. (extra_size <= IFNAMSIZ))
  837. extra_size = 0;
  838. }
  839. }
  840. /* Prepare the call */
  841. info.cmd = cmd;
  842. info.flags = 0;
  843. /* Check if we have a pointer to user space data or not. */
  844. if(extra_size == 0) {
  845. /* No extra arguments. Trivial to handle */
  846. ret = handler(dev, &info, &(iwr->u), (char *) &(iwr->u));
  847. } else {
  848. char * extra;
  849. int err;
  850. /* Check what user space is giving us */
  851. if(IW_IS_SET(cmd)) {
  852. /* Check NULL pointer */
  853. if((iwr->u.data.pointer == NULL) &&
  854. (iwr->u.data.length != 0))
  855. return -EFAULT;
  856. /* Does it fits within bounds ? */
  857. if(iwr->u.data.length > (descr->set_args &
  858. IW_PRIV_SIZE_MASK))
  859. return -E2BIG;
  860. } else {
  861. /* Check NULL pointer */
  862. if(iwr->u.data.pointer == NULL)
  863. return -EFAULT;
  864. }
  865. #ifdef WE_IOCTL_DEBUG
  866. printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
  867. dev->name, extra_size);
  868. #endif /* WE_IOCTL_DEBUG */
  869. /* Always allocate for max space. Easier, and won't last
  870. * long... */
  871. extra = kmalloc(extra_size, GFP_KERNEL);
  872. if (extra == NULL) {
  873. return -ENOMEM;
  874. }
  875. /* If it is a SET, get all the extra data in here */
  876. if(IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
  877. err = copy_from_user(extra, iwr->u.data.pointer,
  878. extra_size);
  879. if (err) {
  880. kfree(extra);
  881. return -EFAULT;
  882. }
  883. #ifdef WE_IOCTL_DEBUG
  884. printk(KERN_DEBUG "%s (WE) : Got %d elem\n",
  885. dev->name, iwr->u.data.length);
  886. #endif /* WE_IOCTL_DEBUG */
  887. }
  888. /* Call the handler */
  889. ret = handler(dev, &info, &(iwr->u), extra);
  890. /* If we have something to return to the user */
  891. if (!ret && IW_IS_GET(cmd)) {
  892. /* Adjust for the actual length if it's variable,
  893. * avoid leaking kernel bits outside. */
  894. if (!(descr->get_args & IW_PRIV_SIZE_FIXED)) {
  895. extra_size = adjust_priv_size(descr->get_args,
  896. &(iwr->u));
  897. }
  898. err = copy_to_user(iwr->u.data.pointer, extra,
  899. extra_size);
  900. if (err)
  901. ret = -EFAULT;
  902. #ifdef WE_IOCTL_DEBUG
  903. printk(KERN_DEBUG "%s (WE) : Wrote %d elem\n",
  904. dev->name, iwr->u.data.length);
  905. #endif /* WE_IOCTL_DEBUG */
  906. }
  907. /* Cleanup - I told you it wasn't that long ;-) */
  908. kfree(extra);
  909. }
  910. /* Call commit handler if needed and defined */
  911. if(ret == -EIWCOMMIT)
  912. ret = call_commit_handler(dev);
  913. return ret;
  914. }
  915. /* ---------------------------------------------------------------- */
  916. /*
  917. * Main IOCTl dispatcher. Called from the main networking code
  918. * (dev_ioctl() in net/core/dev.c).
  919. * Check the type of IOCTL and call the appropriate wrapper...
  920. */
  921. int wireless_process_ioctl(struct ifreq *ifr, unsigned int cmd)
  922. {
  923. struct net_device *dev;
  924. iw_handler handler;
  925. /* Permissions are already checked in dev_ioctl() before calling us.
  926. * The copy_to/from_user() of ifr is also dealt with in there */
  927. /* Make sure the device exist */
  928. if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL)
  929. return -ENODEV;
  930. /* A bunch of special cases, then the generic case...
  931. * Note that 'cmd' is already filtered in dev_ioctl() with
  932. * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
  933. switch(cmd)
  934. {
  935. case SIOCGIWSTATS:
  936. /* Get Wireless Stats */
  937. return dev_iwstats(dev, ifr);
  938. case SIOCGIWPRIV:
  939. /* Check if we have some wireless handlers defined */
  940. if(dev->wireless_handlers != NULL) {
  941. /* We export to user space the definition of
  942. * the private handler ourselves */
  943. return ioctl_export_private(dev, ifr);
  944. }
  945. // ## Fall-through for old API ##
  946. default:
  947. /* Generic IOCTL */
  948. /* Basic check */
  949. if (!netif_device_present(dev))
  950. return -ENODEV;
  951. /* New driver API : try to find the handler */
  952. handler = get_handler(dev, cmd);
  953. if(handler != NULL) {
  954. /* Standard and private are not the same */
  955. if(cmd < SIOCIWFIRSTPRIV)
  956. return ioctl_standard_call(dev,
  957. ifr,
  958. cmd,
  959. handler);
  960. else
  961. return ioctl_private_call(dev,
  962. ifr,
  963. cmd,
  964. handler);
  965. }
  966. /* Old driver API : call driver ioctl handler */
  967. if (dev->do_ioctl) {
  968. return dev->do_ioctl(dev, ifr, cmd);
  969. }
  970. return -EOPNOTSUPP;
  971. }
  972. /* Not reached */
  973. return -EINVAL;
  974. }
  975. /************************* EVENT PROCESSING *************************/
  976. /*
  977. * Process events generated by the wireless layer or the driver.
  978. * Most often, the event will be propagated through rtnetlink
  979. */
  980. #ifdef WE_EVENT_NETLINK
  981. /* "rtnl" is defined in net/core/rtnetlink.c, but we need it here.
  982. * It is declared in <linux/rtnetlink.h> */
  983. /* ---------------------------------------------------------------- */
  984. /*
  985. * Fill a rtnetlink message with our event data.
  986. * Note that we propage only the specified event and don't dump the
  987. * current wireless config. Dumping the wireless config is far too
  988. * expensive (for each parameter, the driver need to query the hardware).
  989. */
  990. static inline int rtnetlink_fill_iwinfo(struct sk_buff * skb,
  991. struct net_device * dev,
  992. int type,
  993. char * event,
  994. int event_len)
  995. {
  996. struct ifinfomsg *r;
  997. struct nlmsghdr *nlh;
  998. unsigned char *b = skb->tail;
  999. nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(*r));
  1000. r = NLMSG_DATA(nlh);
  1001. r->ifi_family = AF_UNSPEC;
  1002. r->ifi_type = dev->type;
  1003. r->ifi_index = dev->ifindex;
  1004. r->ifi_flags = dev->flags;
  1005. r->ifi_change = 0; /* Wireless changes don't affect those flags */
  1006. /* Add the wireless events in the netlink packet */
  1007. RTA_PUT(skb, IFLA_WIRELESS,
  1008. event_len, event);
  1009. nlh->nlmsg_len = skb->tail - b;
  1010. return skb->len;
  1011. nlmsg_failure:
  1012. rtattr_failure:
  1013. skb_trim(skb, b - skb->data);
  1014. return -1;
  1015. }
  1016. /* ---------------------------------------------------------------- */
  1017. /*
  1018. * Create and broadcast and send it on the standard rtnetlink socket
  1019. * This is a pure clone rtmsg_ifinfo() in net/core/rtnetlink.c
  1020. * Andrzej Krzysztofowicz mandated that I used a IFLA_XXX field
  1021. * within a RTM_NEWLINK event.
  1022. */
  1023. static inline void rtmsg_iwinfo(struct net_device * dev,
  1024. char * event,
  1025. int event_len)
  1026. {
  1027. struct sk_buff *skb;
  1028. int size = NLMSG_GOODSIZE;
  1029. skb = alloc_skb(size, GFP_ATOMIC);
  1030. if (!skb)
  1031. return;
  1032. if (rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK,
  1033. event, event_len) < 0) {
  1034. kfree_skb(skb);
  1035. return;
  1036. }
  1037. NETLINK_CB(skb).dst_groups = RTMGRP_LINK;
  1038. netlink_broadcast(rtnl, skb, 0, RTMGRP_LINK, GFP_ATOMIC);
  1039. }
  1040. #endif /* WE_EVENT_NETLINK */
  1041. /* ---------------------------------------------------------------- */
  1042. /*
  1043. * Main event dispatcher. Called from other parts and drivers.
  1044. * Send the event on the appropriate channels.
  1045. * May be called from interrupt context.
  1046. */
  1047. void wireless_send_event(struct net_device * dev,
  1048. unsigned int cmd,
  1049. union iwreq_data * wrqu,
  1050. char * extra)
  1051. {
  1052. const struct iw_ioctl_description * descr = NULL;
  1053. int extra_len = 0;
  1054. struct iw_event *event; /* Mallocated whole event */
  1055. int event_len; /* Its size */
  1056. int hdr_len; /* Size of the event header */
  1057. /* Don't "optimise" the following variable, it will crash */
  1058. unsigned cmd_index; /* *MUST* be unsigned */
  1059. /* Get the description of the IOCTL */
  1060. if(cmd <= SIOCIWLAST) {
  1061. cmd_index = cmd - SIOCIWFIRST;
  1062. if(cmd_index < standard_ioctl_num)
  1063. descr = &(standard_ioctl[cmd_index]);
  1064. } else {
  1065. cmd_index = cmd - IWEVFIRST;
  1066. if(cmd_index < standard_event_num)
  1067. descr = &(standard_event[cmd_index]);
  1068. }
  1069. /* Don't accept unknown events */
  1070. if(descr == NULL) {
  1071. /* Note : we don't return an error to the driver, because
  1072. * the driver would not know what to do about it. It can't
  1073. * return an error to the user, because the event is not
  1074. * initiated by a user request.
  1075. * The best the driver could do is to log an error message.
  1076. * We will do it ourselves instead...
  1077. */
  1078. printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
  1079. dev->name, cmd);
  1080. return;
  1081. }
  1082. #ifdef WE_EVENT_DEBUG
  1083. printk(KERN_DEBUG "%s (WE) : Got event 0x%04X\n",
  1084. dev->name, cmd);
  1085. 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);
  1086. #endif /* WE_EVENT_DEBUG */
  1087. /* Check extra parameters and set extra_len */
  1088. if(descr->header_type == IW_HEADER_TYPE_POINT) {
  1089. /* Check if number of token fits within bounds */
  1090. if(wrqu->data.length > descr->max_tokens) {
  1091. printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length);
  1092. return;
  1093. }
  1094. if(wrqu->data.length < descr->min_tokens) {
  1095. printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length);
  1096. return;
  1097. }
  1098. /* Calculate extra_len - extra is NULL for restricted events */
  1099. if(extra != NULL)
  1100. extra_len = wrqu->data.length * descr->token_size;
  1101. #ifdef WE_EVENT_DEBUG
  1102. printk(KERN_DEBUG "%s (WE) : Event 0x%04X, tokens %d, extra_len %d\n", dev->name, cmd, wrqu->data.length, extra_len);
  1103. #endif /* WE_EVENT_DEBUG */
  1104. }
  1105. /* Total length of the event */
  1106. hdr_len = event_type_size[descr->header_type];
  1107. event_len = hdr_len + extra_len;
  1108. #ifdef WE_EVENT_DEBUG
  1109. printk(KERN_DEBUG "%s (WE) : Event 0x%04X, hdr_len %d, event_len %d\n", dev->name, cmd, hdr_len, event_len);
  1110. #endif /* WE_EVENT_DEBUG */
  1111. /* Create temporary buffer to hold the event */
  1112. event = kmalloc(event_len, GFP_ATOMIC);
  1113. if(event == NULL)
  1114. return;
  1115. /* Fill event */
  1116. event->len = event_len;
  1117. event->cmd = cmd;
  1118. memcpy(&event->u, wrqu, hdr_len - IW_EV_LCP_LEN);
  1119. if(extra != NULL)
  1120. memcpy(((char *) event) + hdr_len, extra, extra_len);
  1121. #ifdef WE_EVENT_NETLINK
  1122. /* rtnetlink event channel */
  1123. rtmsg_iwinfo(dev, (char *) event, event_len);
  1124. #endif /* WE_EVENT_NETLINK */
  1125. /* Cleanup */
  1126. kfree(event);
  1127. return; /* Always success, I guess ;-) */
  1128. }
  1129. /********************** ENHANCED IWSPY SUPPORT **********************/
  1130. /*
  1131. * In the old days, the driver was handling spy support all by itself.
  1132. * Now, the driver can delegate this task to Wireless Extensions.
  1133. * It needs to use those standard spy iw_handler in struct iw_handler_def,
  1134. * push data to us via wireless_spy_update() and include struct iw_spy_data
  1135. * in its private part (and advertise it in iw_handler_def->spy_offset).
  1136. * One of the main advantage of centralising spy support here is that
  1137. * it becomes much easier to improve and extend it without having to touch
  1138. * the drivers. One example is the addition of the Spy-Threshold events.
  1139. */
  1140. /* ---------------------------------------------------------------- */
  1141. /*
  1142. * Return the pointer to the spy data in the driver.
  1143. * Because this is called on the Rx path via wireless_spy_update(),
  1144. * we want it to be efficient...
  1145. */
  1146. static inline struct iw_spy_data * get_spydata(struct net_device *dev)
  1147. {
  1148. /* This is the new way */
  1149. if(dev->wireless_data)
  1150. return(dev->wireless_data->spy_data);
  1151. /* This is the old way. Doesn't work for multi-headed drivers.
  1152. * It will be removed in the next version of WE. */
  1153. return (dev->priv + dev->wireless_handlers->spy_offset);
  1154. }
  1155. /*------------------------------------------------------------------*/
  1156. /*
  1157. * Standard Wireless Handler : set Spy List
  1158. */
  1159. int iw_handler_set_spy(struct net_device * dev,
  1160. struct iw_request_info * info,
  1161. union iwreq_data * wrqu,
  1162. char * extra)
  1163. {
  1164. struct iw_spy_data * spydata = get_spydata(dev);
  1165. struct sockaddr * address = (struct sockaddr *) extra;
  1166. if(!dev->wireless_data)
  1167. /* Help user know that driver needs updating */
  1168. printk(KERN_DEBUG "%s (WE) : Driver using old/buggy spy support, please fix driver !\n",
  1169. dev->name);
  1170. /* Make sure driver is not buggy or using the old API */
  1171. if(!spydata)
  1172. return -EOPNOTSUPP;
  1173. /* Disable spy collection while we copy the addresses.
  1174. * While we copy addresses, any call to wireless_spy_update()
  1175. * will NOP. This is OK, as anyway the addresses are changing. */
  1176. spydata->spy_number = 0;
  1177. /* We want to operate without locking, because wireless_spy_update()
  1178. * most likely will happen in the interrupt handler, and therefore
  1179. * have its own locking constraints and needs performance.
  1180. * The rtnl_lock() make sure we don't race with the other iw_handlers.
  1181. * This make sure wireless_spy_update() "see" that the spy list
  1182. * is temporarily disabled. */
  1183. wmb();
  1184. /* Are there are addresses to copy? */
  1185. if(wrqu->data.length > 0) {
  1186. int i;
  1187. /* Copy addresses */
  1188. for(i = 0; i < wrqu->data.length; i++)
  1189. memcpy(spydata->spy_address[i], address[i].sa_data,
  1190. ETH_ALEN);
  1191. /* Reset stats */
  1192. memset(spydata->spy_stat, 0,
  1193. sizeof(struct iw_quality) * IW_MAX_SPY);
  1194. #ifdef WE_SPY_DEBUG
  1195. printk(KERN_DEBUG "iw_handler_set_spy() : offset %ld, spydata %p, num %d\n", dev->wireless_handlers->spy_offset, spydata, wrqu->data.length);
  1196. for (i = 0; i < wrqu->data.length; i++)
  1197. printk(KERN_DEBUG
  1198. "%02X:%02X:%02X:%02X:%02X:%02X \n",
  1199. spydata->spy_address[i][0],
  1200. spydata->spy_address[i][1],
  1201. spydata->spy_address[i][2],
  1202. spydata->spy_address[i][3],
  1203. spydata->spy_address[i][4],
  1204. spydata->spy_address[i][5]);
  1205. #endif /* WE_SPY_DEBUG */
  1206. }
  1207. /* Make sure above is updated before re-enabling */
  1208. wmb();
  1209. /* Enable addresses */
  1210. spydata->spy_number = wrqu->data.length;
  1211. return 0;
  1212. }
  1213. /*------------------------------------------------------------------*/
  1214. /*
  1215. * Standard Wireless Handler : get Spy List
  1216. */
  1217. int iw_handler_get_spy(struct net_device * dev,
  1218. struct iw_request_info * info,
  1219. union iwreq_data * wrqu,
  1220. char * extra)
  1221. {
  1222. struct iw_spy_data * spydata = get_spydata(dev);
  1223. struct sockaddr * address = (struct sockaddr *) extra;
  1224. int i;
  1225. /* Make sure driver is not buggy or using the old API */
  1226. if(!spydata)
  1227. return -EOPNOTSUPP;
  1228. wrqu->data.length = spydata->spy_number;
  1229. /* Copy addresses. */
  1230. for(i = 0; i < spydata->spy_number; i++) {
  1231. memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
  1232. address[i].sa_family = AF_UNIX;
  1233. }
  1234. /* Copy stats to the user buffer (just after). */
  1235. if(spydata->spy_number > 0)
  1236. memcpy(extra + (sizeof(struct sockaddr) *spydata->spy_number),
  1237. spydata->spy_stat,
  1238. sizeof(struct iw_quality) * spydata->spy_number);
  1239. /* Reset updated flags. */
  1240. for(i = 0; i < spydata->spy_number; i++)
  1241. spydata->spy_stat[i].updated = 0;
  1242. return 0;
  1243. }
  1244. /*------------------------------------------------------------------*/
  1245. /*
  1246. * Standard Wireless Handler : set spy threshold
  1247. */
  1248. int iw_handler_set_thrspy(struct net_device * dev,
  1249. struct iw_request_info *info,
  1250. union iwreq_data * wrqu,
  1251. char * extra)
  1252. {
  1253. struct iw_spy_data * spydata = get_spydata(dev);
  1254. struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
  1255. /* Make sure driver is not buggy or using the old API */
  1256. if(!spydata)
  1257. return -EOPNOTSUPP;
  1258. /* Just do it */
  1259. memcpy(&(spydata->spy_thr_low), &(threshold->low),
  1260. 2 * sizeof(struct iw_quality));
  1261. /* Clear flag */
  1262. memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
  1263. #ifdef WE_SPY_DEBUG
  1264. printk(KERN_DEBUG "iw_handler_set_thrspy() : low %d ; high %d\n", spydata->spy_thr_low.level, spydata->spy_thr_high.level);
  1265. #endif /* WE_SPY_DEBUG */
  1266. return 0;
  1267. }
  1268. /*------------------------------------------------------------------*/
  1269. /*
  1270. * Standard Wireless Handler : get spy threshold
  1271. */
  1272. int iw_handler_get_thrspy(struct net_device * dev,
  1273. struct iw_request_info *info,
  1274. union iwreq_data * wrqu,
  1275. char * extra)
  1276. {
  1277. struct iw_spy_data * spydata = get_spydata(dev);
  1278. struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
  1279. /* Make sure driver is not buggy or using the old API */
  1280. if(!spydata)
  1281. return -EOPNOTSUPP;
  1282. /* Just do it */
  1283. memcpy(&(threshold->low), &(spydata->spy_thr_low),
  1284. 2 * sizeof(struct iw_quality));
  1285. return 0;
  1286. }
  1287. /*------------------------------------------------------------------*/
  1288. /*
  1289. * Prepare and send a Spy Threshold event
  1290. */
  1291. static void iw_send_thrspy_event(struct net_device * dev,
  1292. struct iw_spy_data * spydata,
  1293. unsigned char * address,
  1294. struct iw_quality * wstats)
  1295. {
  1296. union iwreq_data wrqu;
  1297. struct iw_thrspy threshold;
  1298. /* Init */
  1299. wrqu.data.length = 1;
  1300. wrqu.data.flags = 0;
  1301. /* Copy address */
  1302. memcpy(threshold.addr.sa_data, address, ETH_ALEN);
  1303. threshold.addr.sa_family = ARPHRD_ETHER;
  1304. /* Copy stats */
  1305. memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality));
  1306. /* Copy also thresholds */
  1307. memcpy(&(threshold.low), &(spydata->spy_thr_low),
  1308. 2 * sizeof(struct iw_quality));
  1309. #ifdef WE_SPY_DEBUG
  1310. printk(KERN_DEBUG "iw_send_thrspy_event() : address %02X:%02X:%02X:%02X:%02X:%02X, level %d, up = %d\n",
  1311. threshold.addr.sa_data[0],
  1312. threshold.addr.sa_data[1],
  1313. threshold.addr.sa_data[2],
  1314. threshold.addr.sa_data[3],
  1315. threshold.addr.sa_data[4],
  1316. threshold.addr.sa_data[5], threshold.qual.level);
  1317. #endif /* WE_SPY_DEBUG */
  1318. /* Send event to user space */
  1319. wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
  1320. }
  1321. /* ---------------------------------------------------------------- */
  1322. /*
  1323. * Call for the driver to update the spy data.
  1324. * For now, the spy data is a simple array. As the size of the array is
  1325. * small, this is good enough. If we wanted to support larger number of
  1326. * spy addresses, we should use something more efficient...
  1327. */
  1328. void wireless_spy_update(struct net_device * dev,
  1329. unsigned char * address,
  1330. struct iw_quality * wstats)
  1331. {
  1332. struct iw_spy_data * spydata = get_spydata(dev);
  1333. int i;
  1334. int match = -1;
  1335. /* Make sure driver is not buggy or using the old API */
  1336. if(!spydata)
  1337. return;
  1338. #ifdef WE_SPY_DEBUG
  1339. 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]);
  1340. #endif /* WE_SPY_DEBUG */
  1341. /* Update all records that match */
  1342. for(i = 0; i < spydata->spy_number; i++)
  1343. if(!memcmp(address, spydata->spy_address[i], ETH_ALEN)) {
  1344. memcpy(&(spydata->spy_stat[i]), wstats,
  1345. sizeof(struct iw_quality));
  1346. match = i;
  1347. }
  1348. /* Generate an event if we cross the spy threshold.
  1349. * To avoid event storms, we have a simple hysteresis : we generate
  1350. * event only when we go under the low threshold or above the
  1351. * high threshold. */
  1352. if(match >= 0) {
  1353. if(spydata->spy_thr_under[match]) {
  1354. if(wstats->level > spydata->spy_thr_high.level) {
  1355. spydata->spy_thr_under[match] = 0;
  1356. iw_send_thrspy_event(dev, spydata,
  1357. address, wstats);
  1358. }
  1359. } else {
  1360. if(wstats->level < spydata->spy_thr_low.level) {
  1361. spydata->spy_thr_under[match] = 1;
  1362. iw_send_thrspy_event(dev, spydata,
  1363. address, wstats);
  1364. }
  1365. }
  1366. }
  1367. }
  1368. EXPORT_SYMBOL(iw_handler_get_spy);
  1369. EXPORT_SYMBOL(iw_handler_get_thrspy);
  1370. EXPORT_SYMBOL(iw_handler_set_spy);
  1371. EXPORT_SYMBOL(iw_handler_set_thrspy);
  1372. EXPORT_SYMBOL(wireless_send_event);
  1373. EXPORT_SYMBOL(wireless_spy_update);