wext.c 47 KB

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