wireless.c 44 KB

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