wext.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684
  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_dev_seq_show(struct seq_file *seq, void *v)
  602. {
  603. might_sleep();
  604. if (v == SEQ_START_TOKEN)
  605. seq_printf(seq, "Inter-| sta-| Quality | Discarded "
  606. "packets | Missed | WE\n"
  607. " face | tus | link level noise | nwid "
  608. "crypt frag retry misc | beacon | %d\n",
  609. WIRELESS_EXT);
  610. else
  611. wireless_seq_printf_stats(seq, v);
  612. return 0;
  613. }
  614. static void *wireless_dev_seq_start(struct seq_file *seq, loff_t *pos)
  615. {
  616. struct net *net = seq_file_net(seq);
  617. loff_t off;
  618. struct net_device *dev;
  619. rtnl_lock();
  620. if (!*pos)
  621. return SEQ_START_TOKEN;
  622. off = 1;
  623. for_each_netdev(net, dev)
  624. if (off++ == *pos)
  625. return dev;
  626. return NULL;
  627. }
  628. static void *wireless_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  629. {
  630. struct net *net = seq_file_net(seq);
  631. ++*pos;
  632. return v == SEQ_START_TOKEN ?
  633. first_net_device(net) : next_net_device(v);
  634. }
  635. static void wireless_dev_seq_stop(struct seq_file *seq, void *v)
  636. {
  637. rtnl_unlock();
  638. }
  639. static const struct seq_operations wireless_seq_ops = {
  640. .start = wireless_dev_seq_start,
  641. .next = wireless_dev_seq_next,
  642. .stop = wireless_dev_seq_stop,
  643. .show = wireless_dev_seq_show,
  644. };
  645. static int seq_open_wireless(struct inode *inode, struct file *file)
  646. {
  647. return seq_open_net(inode, file, &wireless_seq_ops,
  648. sizeof(struct seq_net_private));
  649. }
  650. static const struct file_operations wireless_seq_fops = {
  651. .owner = THIS_MODULE,
  652. .open = seq_open_wireless,
  653. .read = seq_read,
  654. .llseek = seq_lseek,
  655. .release = seq_release_net,
  656. };
  657. int wext_proc_init(struct net *net)
  658. {
  659. /* Create /proc/net/wireless entry */
  660. if (!proc_net_fops_create(net, "wireless", S_IRUGO, &wireless_seq_fops))
  661. return -ENOMEM;
  662. return 0;
  663. }
  664. void wext_proc_exit(struct net *net)
  665. {
  666. proc_net_remove(net, "wireless");
  667. }
  668. #endif /* CONFIG_PROC_FS */
  669. /************************** IOCTL SUPPORT **************************/
  670. /*
  671. * The original user space API to configure all those Wireless Extensions
  672. * is through IOCTLs.
  673. * In there, we check if we need to call the new driver API (iw_handler)
  674. * or just call the driver ioctl handler.
  675. */
  676. /* ---------------------------------------------------------------- */
  677. static int ioctl_standard_iw_point(struct iw_point *iwp, unsigned int cmd,
  678. const struct iw_ioctl_description *descr,
  679. iw_handler handler, struct net_device *dev,
  680. struct iw_request_info *info)
  681. {
  682. int err, extra_size, user_length = 0, essid_compat = 0;
  683. char *extra;
  684. /* Calculate space needed by arguments. Always allocate
  685. * for max space.
  686. */
  687. extra_size = descr->max_tokens * descr->token_size;
  688. /* Check need for ESSID compatibility for WE < 21 */
  689. switch (cmd) {
  690. case SIOCSIWESSID:
  691. case SIOCGIWESSID:
  692. case SIOCSIWNICKN:
  693. case SIOCGIWNICKN:
  694. if (iwp->length == descr->max_tokens + 1)
  695. essid_compat = 1;
  696. else if (IW_IS_SET(cmd) && (iwp->length != 0)) {
  697. char essid[IW_ESSID_MAX_SIZE + 1];
  698. err = copy_from_user(essid, iwp->pointer,
  699. iwp->length *
  700. descr->token_size);
  701. if (err)
  702. return -EFAULT;
  703. if (essid[iwp->length - 1] == '\0')
  704. essid_compat = 1;
  705. }
  706. break;
  707. default:
  708. break;
  709. }
  710. iwp->length -= essid_compat;
  711. /* Check what user space is giving us */
  712. if (IW_IS_SET(cmd)) {
  713. /* Check NULL pointer */
  714. if (!iwp->pointer && iwp->length != 0)
  715. return -EFAULT;
  716. /* Check if number of token fits within bounds */
  717. if (iwp->length > descr->max_tokens)
  718. return -E2BIG;
  719. if (iwp->length < descr->min_tokens)
  720. return -EINVAL;
  721. } else {
  722. /* Check NULL pointer */
  723. if (!iwp->pointer)
  724. return -EFAULT;
  725. /* Save user space buffer size for checking */
  726. user_length = iwp->length;
  727. /* Don't check if user_length > max to allow forward
  728. * compatibility. The test user_length < min is
  729. * implied by the test at the end.
  730. */
  731. /* Support for very large requests */
  732. if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
  733. (user_length > descr->max_tokens)) {
  734. /* Allow userspace to GET more than max so
  735. * we can support any size GET requests.
  736. * There is still a limit : -ENOMEM.
  737. */
  738. extra_size = user_length * descr->token_size;
  739. /* Note : user_length is originally a __u16,
  740. * and token_size is controlled by us,
  741. * so extra_size won't get negative and
  742. * won't overflow...
  743. */
  744. }
  745. }
  746. /* kzalloc() ensures NULL-termination for essid_compat. */
  747. extra = kzalloc(extra_size, GFP_KERNEL);
  748. if (!extra)
  749. return -ENOMEM;
  750. /* If it is a SET, get all the extra data in here */
  751. if (IW_IS_SET(cmd) && (iwp->length != 0)) {
  752. if (copy_from_user(extra, iwp->pointer,
  753. iwp->length *
  754. descr->token_size)) {
  755. err = -EFAULT;
  756. goto out;
  757. }
  758. }
  759. err = handler(dev, info, (union iwreq_data *) iwp, extra);
  760. iwp->length += essid_compat;
  761. /* If we have something to return to the user */
  762. if (!err && IW_IS_GET(cmd)) {
  763. /* Check if there is enough buffer up there */
  764. if (user_length < iwp->length) {
  765. err = -E2BIG;
  766. goto out;
  767. }
  768. if (copy_to_user(iwp->pointer, extra,
  769. iwp->length *
  770. descr->token_size)) {
  771. err = -EFAULT;
  772. goto out;
  773. }
  774. }
  775. /* Generate an event to notify listeners of the change */
  776. if ((descr->flags & IW_DESCR_FLAG_EVENT) && err == -EIWCOMMIT) {
  777. union iwreq_data *data = (union iwreq_data *) iwp;
  778. if (descr->flags & IW_DESCR_FLAG_RESTRICT)
  779. /* If the event is restricted, don't
  780. * export the payload.
  781. */
  782. wireless_send_event(dev, cmd, data, NULL);
  783. else
  784. wireless_send_event(dev, cmd, data, extra);
  785. }
  786. out:
  787. kfree(extra);
  788. return err;
  789. }
  790. /*
  791. * Wrapper to call a standard Wireless Extension handler.
  792. * We do various checks and also take care of moving data between
  793. * user space and kernel space.
  794. */
  795. static int ioctl_standard_call(struct net_device * dev,
  796. struct iwreq *iwr,
  797. unsigned int cmd,
  798. struct iw_request_info *info,
  799. iw_handler handler)
  800. {
  801. const struct iw_ioctl_description * descr;
  802. int ret = -EINVAL;
  803. /* Get the description of the IOCTL */
  804. if ((cmd - SIOCIWFIRST) >= standard_ioctl_num)
  805. return -EOPNOTSUPP;
  806. descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
  807. /* Check if we have a pointer to user space data or not */
  808. if (descr->header_type != IW_HEADER_TYPE_POINT) {
  809. /* No extra arguments. Trivial to handle */
  810. ret = handler(dev, info, &(iwr->u), NULL);
  811. /* Generate an event to notify listeners of the change */
  812. if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
  813. ((ret == 0) || (ret == -EIWCOMMIT)))
  814. wireless_send_event(dev, cmd, &(iwr->u), NULL);
  815. } else {
  816. ret = ioctl_standard_iw_point(&iwr->u.data, cmd, descr,
  817. handler, dev, info);
  818. }
  819. /* Call commit handler if needed and defined */
  820. if (ret == -EIWCOMMIT)
  821. ret = call_commit_handler(dev);
  822. /* Here, we will generate the appropriate event if needed */
  823. return ret;
  824. }
  825. /* ---------------------------------------------------------------- */
  826. /*
  827. * Wrapper to call a private Wireless Extension handler.
  828. * We do various checks and also take care of moving data between
  829. * user space and kernel space.
  830. * It's not as nice and slimline as the standard wrapper. The cause
  831. * is struct iw_priv_args, which was not really designed for the
  832. * job we are going here.
  833. *
  834. * IMPORTANT : This function prevent to set and get data on the same
  835. * IOCTL and enforce the SET/GET convention. Not doing it would be
  836. * far too hairy...
  837. * If you need to set and get data at the same time, please don't use
  838. * a iw_handler but process it in your ioctl handler (i.e. use the
  839. * old driver API).
  840. */
  841. static int get_priv_descr_and_size(struct net_device *dev, unsigned int cmd,
  842. const struct iw_priv_args **descrp)
  843. {
  844. const struct iw_priv_args *descr;
  845. int i, extra_size;
  846. descr = NULL;
  847. for (i = 0; i < dev->wireless_handlers->num_private_args; i++) {
  848. if (cmd == dev->wireless_handlers->private_args[i].cmd) {
  849. descr = &dev->wireless_handlers->private_args[i];
  850. break;
  851. }
  852. }
  853. extra_size = 0;
  854. if (descr) {
  855. if (IW_IS_SET(cmd)) {
  856. int offset = 0; /* For sub-ioctls */
  857. /* Check for sub-ioctl handler */
  858. if (descr->name[0] == '\0')
  859. /* Reserve one int for sub-ioctl index */
  860. offset = sizeof(__u32);
  861. /* Size of set arguments */
  862. extra_size = get_priv_size(descr->set_args);
  863. /* Does it fits in iwr ? */
  864. if ((descr->set_args & IW_PRIV_SIZE_FIXED) &&
  865. ((extra_size + offset) <= IFNAMSIZ))
  866. extra_size = 0;
  867. } else {
  868. /* Size of get arguments */
  869. extra_size = get_priv_size(descr->get_args);
  870. /* Does it fits in iwr ? */
  871. if ((descr->get_args & IW_PRIV_SIZE_FIXED) &&
  872. (extra_size <= IFNAMSIZ))
  873. extra_size = 0;
  874. }
  875. }
  876. *descrp = descr;
  877. return extra_size;
  878. }
  879. static int ioctl_private_iw_point(struct iw_point *iwp, unsigned int cmd,
  880. const struct iw_priv_args *descr,
  881. iw_handler handler, struct net_device *dev,
  882. struct iw_request_info *info, int extra_size)
  883. {
  884. char *extra;
  885. int err;
  886. /* Check what user space is giving us */
  887. if (IW_IS_SET(cmd)) {
  888. if (!iwp->pointer && iwp->length != 0)
  889. return -EFAULT;
  890. if (iwp->length > (descr->set_args & IW_PRIV_SIZE_MASK))
  891. return -E2BIG;
  892. } else if (!iwp->pointer)
  893. return -EFAULT;
  894. extra = kmalloc(extra_size, GFP_KERNEL);
  895. if (!extra)
  896. return -ENOMEM;
  897. /* If it is a SET, get all the extra data in here */
  898. if (IW_IS_SET(cmd) && (iwp->length != 0)) {
  899. if (copy_from_user(extra, iwp->pointer, extra_size)) {
  900. err = -EFAULT;
  901. goto out;
  902. }
  903. }
  904. /* Call the handler */
  905. err = handler(dev, info, (union iwreq_data *) iwp, extra);
  906. /* If we have something to return to the user */
  907. if (!err && IW_IS_GET(cmd)) {
  908. /* Adjust for the actual length if it's variable,
  909. * avoid leaking kernel bits outside.
  910. */
  911. if (!(descr->get_args & IW_PRIV_SIZE_FIXED))
  912. extra_size = adjust_priv_size(descr->get_args, iwp);
  913. if (copy_to_user(iwp->pointer, extra, extra_size))
  914. err = -EFAULT;
  915. }
  916. out:
  917. kfree(extra);
  918. return err;
  919. }
  920. static int ioctl_private_call(struct net_device *dev, struct iwreq *iwr,
  921. unsigned int cmd, struct iw_request_info *info,
  922. iw_handler handler)
  923. {
  924. int extra_size = 0, ret = -EINVAL;
  925. const struct iw_priv_args *descr;
  926. extra_size = get_priv_descr_and_size(dev, cmd, &descr);
  927. /* Check if we have a pointer to user space data or not. */
  928. if (extra_size == 0) {
  929. /* No extra arguments. Trivial to handle */
  930. ret = handler(dev, info, &(iwr->u), (char *) &(iwr->u));
  931. } else {
  932. ret = ioctl_private_iw_point(&iwr->u.data, cmd, descr,
  933. handler, dev, info, extra_size);
  934. }
  935. /* Call commit handler if needed and defined */
  936. if (ret == -EIWCOMMIT)
  937. ret = call_commit_handler(dev);
  938. return ret;
  939. }
  940. /* ---------------------------------------------------------------- */
  941. typedef int (*wext_ioctl_func)(struct net_device *, struct iwreq *,
  942. unsigned int, struct iw_request_info *,
  943. iw_handler);
  944. /*
  945. * Main IOCTl dispatcher.
  946. * Check the type of IOCTL and call the appropriate wrapper...
  947. */
  948. static int wireless_process_ioctl(struct net *net, struct ifreq *ifr,
  949. unsigned int cmd,
  950. struct iw_request_info *info,
  951. wext_ioctl_func standard,
  952. wext_ioctl_func private)
  953. {
  954. struct iwreq *iwr = (struct iwreq *) ifr;
  955. struct net_device *dev;
  956. iw_handler handler;
  957. /* Permissions are already checked in dev_ioctl() before calling us.
  958. * The copy_to/from_user() of ifr is also dealt with in there */
  959. /* Make sure the device exist */
  960. if ((dev = __dev_get_by_name(net, ifr->ifr_name)) == NULL)
  961. return -ENODEV;
  962. /* A bunch of special cases, then the generic case...
  963. * Note that 'cmd' is already filtered in dev_ioctl() with
  964. * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
  965. if (cmd == SIOCGIWSTATS)
  966. return standard(dev, iwr, cmd, info,
  967. &iw_handler_get_iwstats);
  968. if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
  969. return standard(dev, iwr, cmd, info,
  970. &iw_handler_get_private);
  971. /* Basic check */
  972. if (!netif_device_present(dev))
  973. return -ENODEV;
  974. /* New driver API : try to find the handler */
  975. handler = get_handler(dev, cmd);
  976. if (handler) {
  977. /* Standard and private are not the same */
  978. if (cmd < SIOCIWFIRSTPRIV)
  979. return standard(dev, iwr, cmd, info, handler);
  980. else
  981. return private(dev, iwr, cmd, info, handler);
  982. }
  983. /* Old driver API : call driver ioctl handler */
  984. if (dev->netdev_ops->ndo_do_ioctl)
  985. return dev->netdev_ops->ndo_do_ioctl(dev, ifr, cmd);
  986. return -EOPNOTSUPP;
  987. }
  988. /* If command is `set a parameter', or `get the encoding parameters',
  989. * check if the user has the right to do it.
  990. */
  991. static int wext_permission_check(unsigned int cmd)
  992. {
  993. if ((IW_IS_SET(cmd) || cmd == SIOCGIWENCODE || cmd == SIOCGIWENCODEEXT)
  994. && !capable(CAP_NET_ADMIN))
  995. return -EPERM;
  996. return 0;
  997. }
  998. /* entry point from dev ioctl */
  999. static int wext_ioctl_dispatch(struct net *net, struct ifreq *ifr,
  1000. unsigned int cmd, struct iw_request_info *info,
  1001. wext_ioctl_func standard,
  1002. wext_ioctl_func private)
  1003. {
  1004. int ret = wext_permission_check(cmd);
  1005. if (ret)
  1006. return ret;
  1007. dev_load(net, ifr->ifr_name);
  1008. rtnl_lock();
  1009. ret = wireless_process_ioctl(net, ifr, cmd, info, standard, private);
  1010. rtnl_unlock();
  1011. return ret;
  1012. }
  1013. int wext_handle_ioctl(struct net *net, struct ifreq *ifr, unsigned int cmd,
  1014. void __user *arg)
  1015. {
  1016. struct iw_request_info info = { .cmd = cmd, .flags = 0 };
  1017. int ret;
  1018. ret = wext_ioctl_dispatch(net, ifr, cmd, &info,
  1019. ioctl_standard_call,
  1020. ioctl_private_call);
  1021. if (ret >= 0 &&
  1022. IW_IS_GET(cmd) &&
  1023. copy_to_user(arg, ifr, sizeof(struct iwreq)))
  1024. return -EFAULT;
  1025. return ret;
  1026. }
  1027. #ifdef CONFIG_COMPAT
  1028. static int compat_standard_call(struct net_device *dev,
  1029. struct iwreq *iwr,
  1030. unsigned int cmd,
  1031. struct iw_request_info *info,
  1032. iw_handler handler)
  1033. {
  1034. const struct iw_ioctl_description *descr;
  1035. struct compat_iw_point *iwp_compat;
  1036. struct iw_point iwp;
  1037. int err;
  1038. descr = standard_ioctl + (cmd - SIOCIWFIRST);
  1039. if (descr->header_type != IW_HEADER_TYPE_POINT)
  1040. return ioctl_standard_call(dev, iwr, cmd, info, handler);
  1041. iwp_compat = (struct compat_iw_point *) &iwr->u.data;
  1042. iwp.pointer = compat_ptr(iwp_compat->pointer);
  1043. iwp.length = iwp_compat->length;
  1044. iwp.flags = iwp_compat->flags;
  1045. err = ioctl_standard_iw_point(&iwp, cmd, descr, handler, dev, info);
  1046. iwp_compat->pointer = ptr_to_compat(iwp.pointer);
  1047. iwp_compat->length = iwp.length;
  1048. iwp_compat->flags = iwp.flags;
  1049. return err;
  1050. }
  1051. static int compat_private_call(struct net_device *dev, struct iwreq *iwr,
  1052. unsigned int cmd, struct iw_request_info *info,
  1053. iw_handler handler)
  1054. {
  1055. const struct iw_priv_args *descr;
  1056. int ret, extra_size;
  1057. extra_size = get_priv_descr_and_size(dev, cmd, &descr);
  1058. /* Check if we have a pointer to user space data or not. */
  1059. if (extra_size == 0) {
  1060. /* No extra arguments. Trivial to handle */
  1061. ret = handler(dev, info, &(iwr->u), (char *) &(iwr->u));
  1062. } else {
  1063. struct compat_iw_point *iwp_compat;
  1064. struct iw_point iwp;
  1065. iwp_compat = (struct compat_iw_point *) &iwr->u.data;
  1066. iwp.pointer = compat_ptr(iwp_compat->pointer);
  1067. iwp.length = iwp_compat->length;
  1068. iwp.flags = iwp_compat->flags;
  1069. ret = ioctl_private_iw_point(&iwp, cmd, descr,
  1070. handler, dev, info, extra_size);
  1071. iwp_compat->pointer = ptr_to_compat(iwp.pointer);
  1072. iwp_compat->length = iwp.length;
  1073. iwp_compat->flags = iwp.flags;
  1074. }
  1075. /* Call commit handler if needed and defined */
  1076. if (ret == -EIWCOMMIT)
  1077. ret = call_commit_handler(dev);
  1078. return ret;
  1079. }
  1080. int compat_wext_handle_ioctl(struct net *net, unsigned int cmd,
  1081. unsigned long arg)
  1082. {
  1083. void __user *argp = (void __user *)arg;
  1084. struct iw_request_info info;
  1085. struct iwreq iwr;
  1086. char *colon;
  1087. int ret;
  1088. if (copy_from_user(&iwr, argp, sizeof(struct iwreq)))
  1089. return -EFAULT;
  1090. iwr.ifr_name[IFNAMSIZ-1] = 0;
  1091. colon = strchr(iwr.ifr_name, ':');
  1092. if (colon)
  1093. *colon = 0;
  1094. info.cmd = cmd;
  1095. info.flags = IW_REQUEST_FLAG_COMPAT;
  1096. ret = wext_ioctl_dispatch(net, (struct ifreq *) &iwr, cmd, &info,
  1097. compat_standard_call,
  1098. compat_private_call);
  1099. if (ret >= 0 &&
  1100. IW_IS_GET(cmd) &&
  1101. copy_to_user(argp, &iwr, sizeof(struct iwreq)))
  1102. return -EFAULT;
  1103. return ret;
  1104. }
  1105. #endif
  1106. /************************* EVENT PROCESSING *************************/
  1107. /*
  1108. * Process events generated by the wireless layer or the driver.
  1109. * Most often, the event will be propagated through rtnetlink
  1110. */
  1111. /* ---------------------------------------------------------------- */
  1112. /*
  1113. * Locking...
  1114. * ----------
  1115. *
  1116. * Thanks to Herbert Xu <herbert@gondor.apana.org.au> for fixing
  1117. * the locking issue in here and implementing this code !
  1118. *
  1119. * The issue : wireless_send_event() is often called in interrupt context,
  1120. * while the Netlink layer can never be called in interrupt context.
  1121. * The fully formed RtNetlink events are queued, and then a tasklet is run
  1122. * to feed those to Netlink.
  1123. * The skb_queue is interrupt safe, and its lock is not held while calling
  1124. * Netlink, so there is no possibility of dealock.
  1125. * Jean II
  1126. */
  1127. static struct sk_buff_head wireless_nlevent_queue;
  1128. static int __init wireless_nlevent_init(void)
  1129. {
  1130. skb_queue_head_init(&wireless_nlevent_queue);
  1131. return 0;
  1132. }
  1133. subsys_initcall(wireless_nlevent_init);
  1134. static void wireless_nlevent_process(unsigned long data)
  1135. {
  1136. struct sk_buff *skb;
  1137. while ((skb = skb_dequeue(&wireless_nlevent_queue)))
  1138. rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
  1139. }
  1140. static DECLARE_TASKLET(wireless_nlevent_tasklet, wireless_nlevent_process, 0);
  1141. /* ---------------------------------------------------------------- */
  1142. /*
  1143. * Fill a rtnetlink message with our event data.
  1144. * Note that we propage only the specified event and don't dump the
  1145. * current wireless config. Dumping the wireless config is far too
  1146. * expensive (for each parameter, the driver need to query the hardware).
  1147. */
  1148. static int rtnetlink_fill_iwinfo(struct sk_buff *skb, struct net_device *dev,
  1149. int type, char *event, int event_len)
  1150. {
  1151. struct ifinfomsg *r;
  1152. struct nlmsghdr *nlh;
  1153. nlh = nlmsg_put(skb, 0, 0, type, sizeof(*r), 0);
  1154. if (nlh == NULL)
  1155. return -EMSGSIZE;
  1156. r = nlmsg_data(nlh);
  1157. r->ifi_family = AF_UNSPEC;
  1158. r->__ifi_pad = 0;
  1159. r->ifi_type = dev->type;
  1160. r->ifi_index = dev->ifindex;
  1161. r->ifi_flags = dev_get_flags(dev);
  1162. r->ifi_change = 0; /* Wireless changes don't affect those flags */
  1163. NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
  1164. /* Add the wireless events in the netlink packet */
  1165. NLA_PUT(skb, IFLA_WIRELESS, event_len, event);
  1166. return nlmsg_end(skb, nlh);
  1167. nla_put_failure:
  1168. nlmsg_cancel(skb, nlh);
  1169. return -EMSGSIZE;
  1170. }
  1171. /* ---------------------------------------------------------------- */
  1172. /*
  1173. * Create and broadcast and send it on the standard rtnetlink socket
  1174. * This is a pure clone rtmsg_ifinfo() in net/core/rtnetlink.c
  1175. * Andrzej Krzysztofowicz mandated that I used a IFLA_XXX field
  1176. * within a RTM_NEWLINK event.
  1177. */
  1178. static void rtmsg_iwinfo(struct net_device *dev, char *event, int event_len)
  1179. {
  1180. struct sk_buff *skb;
  1181. int err;
  1182. if (!net_eq(dev_net(dev), &init_net))
  1183. return;
  1184. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1185. if (!skb)
  1186. return;
  1187. err = rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK, event, event_len);
  1188. if (err < 0) {
  1189. WARN_ON(err == -EMSGSIZE);
  1190. kfree_skb(skb);
  1191. return;
  1192. }
  1193. NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
  1194. skb_queue_tail(&wireless_nlevent_queue, skb);
  1195. tasklet_schedule(&wireless_nlevent_tasklet);
  1196. }
  1197. /* ---------------------------------------------------------------- */
  1198. /*
  1199. * Main event dispatcher. Called from other parts and drivers.
  1200. * Send the event on the appropriate channels.
  1201. * May be called from interrupt context.
  1202. */
  1203. void wireless_send_event(struct net_device * dev,
  1204. unsigned int cmd,
  1205. union iwreq_data * wrqu,
  1206. char * extra)
  1207. {
  1208. const struct iw_ioctl_description * descr = NULL;
  1209. int extra_len = 0;
  1210. struct iw_event *event; /* Mallocated whole event */
  1211. int event_len; /* Its size */
  1212. int hdr_len; /* Size of the event header */
  1213. int wrqu_off = 0; /* Offset in wrqu */
  1214. /* Don't "optimise" the following variable, it will crash */
  1215. unsigned cmd_index; /* *MUST* be unsigned */
  1216. /* Get the description of the Event */
  1217. if (cmd <= SIOCIWLAST) {
  1218. cmd_index = cmd - SIOCIWFIRST;
  1219. if (cmd_index < standard_ioctl_num)
  1220. descr = &(standard_ioctl[cmd_index]);
  1221. } else {
  1222. cmd_index = cmd - IWEVFIRST;
  1223. if (cmd_index < standard_event_num)
  1224. descr = &(standard_event[cmd_index]);
  1225. }
  1226. /* Don't accept unknown events */
  1227. if (descr == NULL) {
  1228. /* Note : we don't return an error to the driver, because
  1229. * the driver would not know what to do about it. It can't
  1230. * return an error to the user, because the event is not
  1231. * initiated by a user request.
  1232. * The best the driver could do is to log an error message.
  1233. * We will do it ourselves instead...
  1234. */
  1235. printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
  1236. dev->name, cmd);
  1237. return;
  1238. }
  1239. /* Check extra parameters and set extra_len */
  1240. if (descr->header_type == IW_HEADER_TYPE_POINT) {
  1241. /* Check if number of token fits within bounds */
  1242. if (wrqu->data.length > descr->max_tokens) {
  1243. printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length);
  1244. return;
  1245. }
  1246. if (wrqu->data.length < descr->min_tokens) {
  1247. printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length);
  1248. return;
  1249. }
  1250. /* Calculate extra_len - extra is NULL for restricted events */
  1251. if (extra != NULL)
  1252. extra_len = wrqu->data.length * descr->token_size;
  1253. /* Always at an offset in wrqu */
  1254. wrqu_off = IW_EV_POINT_OFF;
  1255. }
  1256. /* Total length of the event */
  1257. hdr_len = event_type_size[descr->header_type];
  1258. event_len = hdr_len + extra_len;
  1259. /* Create temporary buffer to hold the event */
  1260. event = kmalloc(event_len, GFP_ATOMIC);
  1261. if (event == NULL)
  1262. return;
  1263. /* Fill event */
  1264. event->len = event_len;
  1265. event->cmd = cmd;
  1266. memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
  1267. if (extra)
  1268. memcpy(((char *) event) + hdr_len, extra, extra_len);
  1269. /* Send via the RtNetlink event channel */
  1270. rtmsg_iwinfo(dev, (char *) event, event_len);
  1271. /* Cleanup */
  1272. kfree(event);
  1273. return; /* Always success, I guess ;-) */
  1274. }
  1275. EXPORT_SYMBOL(wireless_send_event);
  1276. /********************** ENHANCED IWSPY SUPPORT **********************/
  1277. /*
  1278. * In the old days, the driver was handling spy support all by itself.
  1279. * Now, the driver can delegate this task to Wireless Extensions.
  1280. * It needs to use those standard spy iw_handler in struct iw_handler_def,
  1281. * push data to us via wireless_spy_update() and include struct iw_spy_data
  1282. * in its private part (and export it in net_device->wireless_data->spy_data).
  1283. * One of the main advantage of centralising spy support here is that
  1284. * it becomes much easier to improve and extend it without having to touch
  1285. * the drivers. One example is the addition of the Spy-Threshold events.
  1286. */
  1287. /* ---------------------------------------------------------------- */
  1288. /*
  1289. * Return the pointer to the spy data in the driver.
  1290. * Because this is called on the Rx path via wireless_spy_update(),
  1291. * we want it to be efficient...
  1292. */
  1293. static inline struct iw_spy_data *get_spydata(struct net_device *dev)
  1294. {
  1295. /* This is the new way */
  1296. if (dev->wireless_data)
  1297. return dev->wireless_data->spy_data;
  1298. return NULL;
  1299. }
  1300. /*------------------------------------------------------------------*/
  1301. /*
  1302. * Standard Wireless Handler : set Spy List
  1303. */
  1304. int iw_handler_set_spy(struct net_device * dev,
  1305. struct iw_request_info * info,
  1306. union iwreq_data * wrqu,
  1307. char * extra)
  1308. {
  1309. struct iw_spy_data * spydata = get_spydata(dev);
  1310. struct sockaddr * address = (struct sockaddr *) extra;
  1311. /* Make sure driver is not buggy or using the old API */
  1312. if (!spydata)
  1313. return -EOPNOTSUPP;
  1314. /* Disable spy collection while we copy the addresses.
  1315. * While we copy addresses, any call to wireless_spy_update()
  1316. * will NOP. This is OK, as anyway the addresses are changing. */
  1317. spydata->spy_number = 0;
  1318. /* We want to operate without locking, because wireless_spy_update()
  1319. * most likely will happen in the interrupt handler, and therefore
  1320. * have its own locking constraints and needs performance.
  1321. * The rtnl_lock() make sure we don't race with the other iw_handlers.
  1322. * This make sure wireless_spy_update() "see" that the spy list
  1323. * is temporarily disabled. */
  1324. smp_wmb();
  1325. /* Are there are addresses to copy? */
  1326. if (wrqu->data.length > 0) {
  1327. int i;
  1328. /* Copy addresses */
  1329. for (i = 0; i < wrqu->data.length; i++)
  1330. memcpy(spydata->spy_address[i], address[i].sa_data,
  1331. ETH_ALEN);
  1332. /* Reset stats */
  1333. memset(spydata->spy_stat, 0,
  1334. sizeof(struct iw_quality) * IW_MAX_SPY);
  1335. }
  1336. /* Make sure above is updated before re-enabling */
  1337. smp_wmb();
  1338. /* Enable addresses */
  1339. spydata->spy_number = wrqu->data.length;
  1340. return 0;
  1341. }
  1342. EXPORT_SYMBOL(iw_handler_set_spy);
  1343. /*------------------------------------------------------------------*/
  1344. /*
  1345. * Standard Wireless Handler : get Spy List
  1346. */
  1347. int iw_handler_get_spy(struct net_device * dev,
  1348. struct iw_request_info * info,
  1349. union iwreq_data * wrqu,
  1350. char * extra)
  1351. {
  1352. struct iw_spy_data * spydata = get_spydata(dev);
  1353. struct sockaddr * address = (struct sockaddr *) extra;
  1354. int i;
  1355. /* Make sure driver is not buggy or using the old API */
  1356. if (!spydata)
  1357. return -EOPNOTSUPP;
  1358. wrqu->data.length = spydata->spy_number;
  1359. /* Copy addresses. */
  1360. for (i = 0; i < spydata->spy_number; i++) {
  1361. memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
  1362. address[i].sa_family = AF_UNIX;
  1363. }
  1364. /* Copy stats to the user buffer (just after). */
  1365. if (spydata->spy_number > 0)
  1366. memcpy(extra + (sizeof(struct sockaddr) *spydata->spy_number),
  1367. spydata->spy_stat,
  1368. sizeof(struct iw_quality) * spydata->spy_number);
  1369. /* Reset updated flags. */
  1370. for (i = 0; i < spydata->spy_number; i++)
  1371. spydata->spy_stat[i].updated &= ~IW_QUAL_ALL_UPDATED;
  1372. return 0;
  1373. }
  1374. EXPORT_SYMBOL(iw_handler_get_spy);
  1375. /*------------------------------------------------------------------*/
  1376. /*
  1377. * Standard Wireless Handler : set spy threshold
  1378. */
  1379. int iw_handler_set_thrspy(struct net_device * dev,
  1380. struct iw_request_info *info,
  1381. union iwreq_data * wrqu,
  1382. char * extra)
  1383. {
  1384. struct iw_spy_data * spydata = get_spydata(dev);
  1385. struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
  1386. /* Make sure driver is not buggy or using the old API */
  1387. if (!spydata)
  1388. return -EOPNOTSUPP;
  1389. /* Just do it */
  1390. memcpy(&(spydata->spy_thr_low), &(threshold->low),
  1391. 2 * sizeof(struct iw_quality));
  1392. /* Clear flag */
  1393. memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
  1394. return 0;
  1395. }
  1396. EXPORT_SYMBOL(iw_handler_set_thrspy);
  1397. /*------------------------------------------------------------------*/
  1398. /*
  1399. * Standard Wireless Handler : get spy threshold
  1400. */
  1401. int iw_handler_get_thrspy(struct net_device * dev,
  1402. struct iw_request_info *info,
  1403. union iwreq_data * wrqu,
  1404. char * extra)
  1405. {
  1406. struct iw_spy_data * spydata = get_spydata(dev);
  1407. struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
  1408. /* Make sure driver is not buggy or using the old API */
  1409. if (!spydata)
  1410. return -EOPNOTSUPP;
  1411. /* Just do it */
  1412. memcpy(&(threshold->low), &(spydata->spy_thr_low),
  1413. 2 * sizeof(struct iw_quality));
  1414. return 0;
  1415. }
  1416. EXPORT_SYMBOL(iw_handler_get_thrspy);
  1417. /*------------------------------------------------------------------*/
  1418. /*
  1419. * Prepare and send a Spy Threshold event
  1420. */
  1421. static void iw_send_thrspy_event(struct net_device * dev,
  1422. struct iw_spy_data * spydata,
  1423. unsigned char * address,
  1424. struct iw_quality * wstats)
  1425. {
  1426. union iwreq_data wrqu;
  1427. struct iw_thrspy threshold;
  1428. /* Init */
  1429. wrqu.data.length = 1;
  1430. wrqu.data.flags = 0;
  1431. /* Copy address */
  1432. memcpy(threshold.addr.sa_data, address, ETH_ALEN);
  1433. threshold.addr.sa_family = ARPHRD_ETHER;
  1434. /* Copy stats */
  1435. memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality));
  1436. /* Copy also thresholds */
  1437. memcpy(&(threshold.low), &(spydata->spy_thr_low),
  1438. 2 * sizeof(struct iw_quality));
  1439. /* Send event to user space */
  1440. wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
  1441. }
  1442. /* ---------------------------------------------------------------- */
  1443. /*
  1444. * Call for the driver to update the spy data.
  1445. * For now, the spy data is a simple array. As the size of the array is
  1446. * small, this is good enough. If we wanted to support larger number of
  1447. * spy addresses, we should use something more efficient...
  1448. */
  1449. void wireless_spy_update(struct net_device * dev,
  1450. unsigned char * address,
  1451. struct iw_quality * wstats)
  1452. {
  1453. struct iw_spy_data * spydata = get_spydata(dev);
  1454. int i;
  1455. int match = -1;
  1456. /* Make sure driver is not buggy or using the old API */
  1457. if (!spydata)
  1458. return;
  1459. /* Update all records that match */
  1460. for (i = 0; i < spydata->spy_number; i++)
  1461. if (!compare_ether_addr(address, spydata->spy_address[i])) {
  1462. memcpy(&(spydata->spy_stat[i]), wstats,
  1463. sizeof(struct iw_quality));
  1464. match = i;
  1465. }
  1466. /* Generate an event if we cross the spy threshold.
  1467. * To avoid event storms, we have a simple hysteresis : we generate
  1468. * event only when we go under the low threshold or above the
  1469. * high threshold. */
  1470. if (match >= 0) {
  1471. if (spydata->spy_thr_under[match]) {
  1472. if (wstats->level > spydata->spy_thr_high.level) {
  1473. spydata->spy_thr_under[match] = 0;
  1474. iw_send_thrspy_event(dev, spydata,
  1475. address, wstats);
  1476. }
  1477. } else {
  1478. if (wstats->level < spydata->spy_thr_low.level) {
  1479. spydata->spy_thr_under[match] = 1;
  1480. iw_send_thrspy_event(dev, spydata,
  1481. address, wstats);
  1482. }
  1483. }
  1484. }
  1485. }
  1486. EXPORT_SYMBOL(wireless_spy_update);