wext.c 49 KB

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