wext.c 47 KB

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