iw_handler.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * This file define the new driver API for Wireless Extensions
  4. *
  5. * Version : 8 16.3.07
  6. *
  7. * Authors : Jean Tourrilhes - HPL - <jt@hpl.hp.com>
  8. * Copyright (c) 2001-2007 Jean Tourrilhes, All Rights Reserved.
  9. */
  10. #ifndef _IW_HANDLER_H
  11. #define _IW_HANDLER_H
  12. /************************** DOCUMENTATION **************************/
  13. /*
  14. * Initial driver API (1996 -> onward) :
  15. * -----------------------------------
  16. * The initial API just sends the IOCTL request received from user space
  17. * to the driver (via the driver ioctl handler). The driver has to
  18. * handle all the rest...
  19. *
  20. * The initial API also defines a specific handler in struct net_device
  21. * to handle wireless statistics.
  22. *
  23. * The initial APIs served us well and has proven a reasonably good design.
  24. * However, there is a few shortcommings :
  25. * o No events, everything is a request to the driver.
  26. * o Large ioctl function in driver with gigantic switch statement
  27. * (i.e. spaghetti code).
  28. * o Driver has to mess up with copy_to/from_user, and in many cases
  29. * does it unproperly. Common mistakes are :
  30. * * buffer overflows (no checks or off by one checks)
  31. * * call copy_to/from_user with irq disabled
  32. * o The user space interface is tied to ioctl because of the use
  33. * copy_to/from_user.
  34. *
  35. * New driver API (2002 -> onward) :
  36. * -------------------------------
  37. * The new driver API is just a bunch of standard functions (handlers),
  38. * each handling a specific Wireless Extension. The driver just export
  39. * the list of handler it supports, and those will be called apropriately.
  40. *
  41. * I tried to keep the main advantage of the previous API (simplicity,
  42. * efficiency and light weight), and also I provide a good dose of backward
  43. * compatibility (most structures are the same, driver can use both API
  44. * simultaneously, ...).
  45. * Hopefully, I've also addressed the shortcomming of the initial API.
  46. *
  47. * The advantage of the new API are :
  48. * o Handling of Extensions in driver broken in small contained functions
  49. * o Tighter checks of ioctl before calling the driver
  50. * o Flexible commit strategy (at least, the start of it)
  51. * o Backward compatibility (can be mixed with old API)
  52. * o Driver doesn't have to worry about memory and user-space issues
  53. * The last point is important for the following reasons :
  54. * o You are now able to call the new driver API from any API you
  55. * want (including from within other parts of the kernel).
  56. * o Common mistakes are avoided (buffer overflow, user space copy
  57. * with irq disabled and so on).
  58. *
  59. * The Drawback of the new API are :
  60. * o bloat (especially kernel)
  61. * o need to migrate existing drivers to new API
  62. * My initial testing shows that the new API adds around 3kB to the kernel
  63. * and save between 0 and 5kB from a typical driver.
  64. * Also, as all structures and data types are unchanged, the migration is
  65. * quite straightforward (but tedious).
  66. *
  67. * ---
  68. *
  69. * The new driver API is defined below in this file. User space should
  70. * not be aware of what's happening down there...
  71. *
  72. * A new kernel wrapper is in charge of validating the IOCTLs and calling
  73. * the appropriate driver handler. This is implemented in :
  74. * # net/core/wireless.c
  75. *
  76. * The driver export the list of handlers in :
  77. * # include/linux/netdevice.h (one place)
  78. *
  79. * The new driver API is available for WIRELESS_EXT >= 13.
  80. * Good luck with migration to the new API ;-)
  81. */
  82. /* ---------------------- THE IMPLEMENTATION ---------------------- */
  83. /*
  84. * Some of the choice I've made are pretty controversials. Defining an
  85. * API is very much weighting compromises. This goes into some of the
  86. * details and the thinking behind the implementation.
  87. *
  88. * Implementation goals :
  89. * --------------------
  90. * The implementation goals were as follow :
  91. * o Obvious : you should not need a PhD to understand what's happening,
  92. * the benefit is easier maintenance.
  93. * o Flexible : it should accommodate a wide variety of driver
  94. * implementations and be as flexible as the old API.
  95. * o Lean : it should be efficient memory wise to minimise the impact
  96. * on kernel footprint.
  97. * o Transparent to user space : the large number of user space
  98. * applications that use Wireless Extensions should not need
  99. * any modifications.
  100. *
  101. * Array of functions versus Struct of functions
  102. * ---------------------------------------------
  103. * 1) Having an array of functions allow the kernel code to access the
  104. * handler in a single lookup, which is much more efficient (think hash
  105. * table here).
  106. * 2) The only drawback is that driver writer may put their handler in
  107. * the wrong slot. This is trivial to test (I set the frequency, the
  108. * bitrate changes). Once the handler is in the proper slot, it will be
  109. * there forever, because the array is only extended at the end.
  110. * 3) Backward/forward compatibility : adding new handler just require
  111. * extending the array, so you can put newer driver in older kernel
  112. * without having to patch the kernel code (and vice versa).
  113. *
  114. * All handler are of the same generic type
  115. * ----------------------------------------
  116. * That's a feature !!!
  117. * 1) Having a generic handler allow to have generic code, which is more
  118. * efficient. If each of the handler was individually typed I would need
  119. * to add a big switch in the kernel (== more bloat). This solution is
  120. * more scalable, adding new Wireless Extensions doesn't add new code.
  121. * 2) You can use the same handler in different slots of the array. For
  122. * hardware, it may be more efficient or logical to handle multiple
  123. * Wireless Extensions with a single function, and the API allow you to
  124. * do that. (An example would be a single record on the card to control
  125. * both bitrate and frequency, the handler would read the old record,
  126. * modify it according to info->cmd and rewrite it).
  127. *
  128. * Functions prototype uses union iwreq_data
  129. * -----------------------------------------
  130. * Some would have preferred functions defined this way :
  131. * static int mydriver_ioctl_setrate(struct net_device *dev,
  132. * long rate, int auto)
  133. * 1) The kernel code doesn't "validate" the content of iwreq_data, and
  134. * can't do it (different hardware may have different notion of what a
  135. * valid frequency is), so we don't pretend that we do it.
  136. * 2) The above form is not extendable. If I want to add a flag (for
  137. * example to distinguish setting max rate and basic rate), I would
  138. * break the prototype. Using iwreq_data is more flexible.
  139. * 3) Also, the above form is not generic (see above).
  140. * 4) I don't expect driver developper using the wrong field of the
  141. * union (Doh !), so static typechecking doesn't add much value.
  142. * 5) Lastly, you can skip the union by doing :
  143. * static int mydriver_ioctl_setrate(struct net_device *dev,
  144. * struct iw_request_info *info,
  145. * struct iw_param *rrq,
  146. * char *extra)
  147. * And then adding the handler in the array like this :
  148. * (iw_handler) mydriver_ioctl_setrate, // SIOCSIWRATE
  149. *
  150. * Using functions and not a registry
  151. * ----------------------------------
  152. * Another implementation option would have been for every instance to
  153. * define a registry (a struct containing all the Wireless Extensions)
  154. * and only have a function to commit the registry to the hardware.
  155. * 1) This approach can be emulated by the current code, but not
  156. * vice versa.
  157. * 2) Some drivers don't keep any configuration in the driver, for them
  158. * adding such a registry would be a significant bloat.
  159. * 3) The code to translate from Wireless Extension to native format is
  160. * needed anyway, so it would not reduce significantely the amount of code.
  161. * 4) The current approach only selectively translate Wireless Extensions
  162. * to native format and only selectively set, whereas the registry approach
  163. * would require to translate all WE and set all parameters for any single
  164. * change.
  165. * 5) For many Wireless Extensions, the GET operation return the current
  166. * dynamic value, not the value that was set.
  167. *
  168. * This header is <net/iw_handler.h>
  169. * ---------------------------------
  170. * 1) This header is kernel space only and should not be exported to
  171. * user space. Headers in "include/linux/" are exported, headers in
  172. * "include/net/" are not.
  173. *
  174. * Mixed 32/64 bit issues
  175. * ----------------------
  176. * The Wireless Extensions are designed to be 64 bit clean, by using only
  177. * datatypes with explicit storage size.
  178. * There are some issues related to kernel and user space using different
  179. * memory model, and in particular 64bit kernel with 32bit user space.
  180. * The problem is related to struct iw_point, that contains a pointer
  181. * that *may* need to be translated.
  182. * This is quite messy. The new API doesn't solve this problem (it can't),
  183. * but is a step in the right direction :
  184. * 1) Meta data about each ioctl is easily available, so we know what type
  185. * of translation is needed.
  186. * 2) The move of data between kernel and user space is only done in a single
  187. * place in the kernel, so adding specific hooks in there is possible.
  188. * 3) In the long term, it allows to move away from using ioctl as the
  189. * user space API.
  190. *
  191. * So many comments and so few code
  192. * --------------------------------
  193. * That's a feature. Comments won't bloat the resulting kernel binary.
  194. */
  195. /***************************** INCLUDES *****************************/
  196. #include <linux/wireless.h> /* IOCTL user space API */
  197. #include <linux/if_ether.h>
  198. /***************************** VERSION *****************************/
  199. /*
  200. * This constant is used to know which version of the driver API is
  201. * available. Hopefully, this will be pretty stable and no changes
  202. * will be needed...
  203. * I just plan to increment with each new version.
  204. */
  205. #define IW_HANDLER_VERSION 8
  206. /*
  207. * Changes :
  208. *
  209. * V2 to V3
  210. * --------
  211. * - Move event definition in <linux/wireless.h>
  212. * - Add Wireless Event support :
  213. * o wireless_send_event() prototype
  214. * o iwe_stream_add_event/point() inline functions
  215. * V3 to V4
  216. * --------
  217. * - Reshuffle IW_HEADER_TYPE_XXX to map IW_PRIV_TYPE_XXX changes
  218. *
  219. * V4 to V5
  220. * --------
  221. * - Add new spy support : struct iw_spy_data & prototypes
  222. *
  223. * V5 to V6
  224. * --------
  225. * - Change the way we get to spy_data method for added safety
  226. * - Remove spy #ifdef, they are always on -> cleaner code
  227. * - Add IW_DESCR_FLAG_NOMAX flag for very large requests
  228. * - Start migrating get_wireless_stats to struct iw_handler_def
  229. *
  230. * V6 to V7
  231. * --------
  232. * - Add struct ieee80211_device pointer in struct iw_public_data
  233. * - Remove (struct iw_point *)->pointer from events and streams
  234. * - Remove spy_offset from struct iw_handler_def
  235. * - Add "check" version of event macros for ieee802.11 stack
  236. *
  237. * V7 to V8
  238. * ----------
  239. * - Prevent leaking of kernel space in stream on 64 bits.
  240. */
  241. /**************************** CONSTANTS ****************************/
  242. /* Enhanced spy support available */
  243. #define IW_WIRELESS_SPY
  244. #define IW_WIRELESS_THRSPY
  245. /* Special error message for the driver to indicate that we
  246. * should do a commit after return from the iw_handler */
  247. #define EIWCOMMIT EINPROGRESS
  248. /* Flags available in struct iw_request_info */
  249. #define IW_REQUEST_FLAG_COMPAT 0x0001 /* Compat ioctl call */
  250. /* Type of headers we know about (basically union iwreq_data) */
  251. #define IW_HEADER_TYPE_NULL 0 /* Not available */
  252. #define IW_HEADER_TYPE_CHAR 2 /* char [IFNAMSIZ] */
  253. #define IW_HEADER_TYPE_UINT 4 /* __u32 */
  254. #define IW_HEADER_TYPE_FREQ 5 /* struct iw_freq */
  255. #define IW_HEADER_TYPE_ADDR 6 /* struct sockaddr */
  256. #define IW_HEADER_TYPE_POINT 8 /* struct iw_point */
  257. #define IW_HEADER_TYPE_PARAM 9 /* struct iw_param */
  258. #define IW_HEADER_TYPE_QUAL 10 /* struct iw_quality */
  259. /* Handling flags */
  260. /* Most are not implemented. I just use them as a reminder of some
  261. * cool features we might need one day ;-) */
  262. #define IW_DESCR_FLAG_NONE 0x0000 /* Obvious */
  263. /* Wrapper level flags */
  264. #define IW_DESCR_FLAG_DUMP 0x0001 /* Not part of the dump command */
  265. #define IW_DESCR_FLAG_EVENT 0x0002 /* Generate an event on SET */
  266. #define IW_DESCR_FLAG_RESTRICT 0x0004 /* GET : request is ROOT only */
  267. /* SET : Omit payload from generated iwevent */
  268. #define IW_DESCR_FLAG_NOMAX 0x0008 /* GET : no limit on request size */
  269. /* Driver level flags */
  270. #define IW_DESCR_FLAG_WAIT 0x0100 /* Wait for driver event */
  271. /****************************** TYPES ******************************/
  272. /* ----------------------- WIRELESS HANDLER ----------------------- */
  273. /*
  274. * A wireless handler is just a standard function, that looks like the
  275. * ioctl handler.
  276. * We also define there how a handler list look like... As the Wireless
  277. * Extension space is quite dense, we use a simple array, which is faster
  278. * (that's the perfect hash table ;-).
  279. */
  280. /*
  281. * Meta data about the request passed to the iw_handler.
  282. * Most handlers can safely ignore what's in there.
  283. * The 'cmd' field might come handy if you want to use the same handler
  284. * for multiple command...
  285. * This struct is also my long term insurance. I can add new fields here
  286. * without breaking the prototype of iw_handler...
  287. */
  288. struct iw_request_info {
  289. __u16 cmd; /* Wireless Extension command */
  290. __u16 flags; /* More to come ;-) */
  291. };
  292. struct net_device;
  293. /*
  294. * This is how a function handling a Wireless Extension should look
  295. * like (both get and set, standard and private).
  296. */
  297. typedef int (*iw_handler)(struct net_device *dev, struct iw_request_info *info,
  298. union iwreq_data *wrqu, char *extra);
  299. /*
  300. * This define all the handler that the driver export.
  301. * As you need only one per driver type, please use a static const
  302. * shared by all driver instances... Same for the members...
  303. * This will be linked from net_device in <linux/netdevice.h>
  304. */
  305. struct iw_handler_def {
  306. /* Array of handlers for standard ioctls
  307. * We will call dev->wireless_handlers->standard[ioctl - SIOCIWFIRST]
  308. */
  309. const iw_handler * standard;
  310. /* Number of handlers defined (more precisely, index of the
  311. * last defined handler + 1) */
  312. __u16 num_standard;
  313. #ifdef CONFIG_WEXT_PRIV
  314. __u16 num_private;
  315. /* Number of private arg description */
  316. __u16 num_private_args;
  317. /* Array of handlers for private ioctls
  318. * Will call dev->wireless_handlers->private[ioctl - SIOCIWFIRSTPRIV]
  319. */
  320. const iw_handler * private;
  321. /* Arguments of private handler. This one is just a list, so you
  322. * can put it in any order you want and should not leave holes...
  323. * We will automatically export that to user space... */
  324. const struct iw_priv_args * private_args;
  325. #endif
  326. /* New location of get_wireless_stats, to de-bloat struct net_device.
  327. * The old pointer in struct net_device will be gradually phased
  328. * out, and drivers are encouraged to use this one... */
  329. struct iw_statistics* (*get_wireless_stats)(struct net_device *dev);
  330. };
  331. /* ---------------------- IOCTL DESCRIPTION ---------------------- */
  332. /*
  333. * One of the main goal of the new interface is to deal entirely with
  334. * user space/kernel space memory move.
  335. * For that, we need to know :
  336. * o if iwreq is a pointer or contain the full data
  337. * o what is the size of the data to copy
  338. *
  339. * For private IOCTLs, we use the same rules as used by iwpriv and
  340. * defined in struct iw_priv_args.
  341. *
  342. * For standard IOCTLs, things are quite different and we need to
  343. * use the structures below. Actually, this struct is also more
  344. * efficient, but that's another story...
  345. */
  346. /*
  347. * Describe how a standard IOCTL looks like.
  348. */
  349. struct iw_ioctl_description {
  350. __u8 header_type; /* NULL, iw_point or other */
  351. __u8 token_type; /* Future */
  352. __u16 token_size; /* Granularity of payload */
  353. __u16 min_tokens; /* Min acceptable token number */
  354. __u16 max_tokens; /* Max acceptable token number */
  355. __u32 flags; /* Special handling of the request */
  356. };
  357. /* Need to think of short header translation table. Later. */
  358. /* --------------------- ENHANCED SPY SUPPORT --------------------- */
  359. /*
  360. * In the old days, the driver was handling spy support all by itself.
  361. * Now, the driver can delegate this task to Wireless Extensions.
  362. * It needs to include this struct in its private part and use the
  363. * standard spy iw_handler.
  364. */
  365. /*
  366. * Instance specific spy data, i.e. addresses spied and quality for them.
  367. */
  368. struct iw_spy_data {
  369. /* --- Standard spy support --- */
  370. int spy_number;
  371. u_char spy_address[IW_MAX_SPY][ETH_ALEN];
  372. struct iw_quality spy_stat[IW_MAX_SPY];
  373. /* --- Enhanced spy support (event) */
  374. struct iw_quality spy_thr_low; /* Low threshold */
  375. struct iw_quality spy_thr_high; /* High threshold */
  376. u_char spy_thr_under[IW_MAX_SPY];
  377. };
  378. /* --------------------- DEVICE WIRELESS DATA --------------------- */
  379. /*
  380. * This is all the wireless data specific to a device instance that
  381. * is managed by the core of Wireless Extensions or the 802.11 layer.
  382. * We only keep pointer to those structures, so that a driver is free
  383. * to share them between instances.
  384. * This structure should be initialised before registering the device.
  385. * Access to this data follow the same rules as any other struct net_device
  386. * data (i.e. valid as long as struct net_device exist, same locking rules).
  387. */
  388. /* Forward declaration */
  389. struct libipw_device;
  390. /* The struct */
  391. struct iw_public_data {
  392. /* Driver enhanced spy support */
  393. struct iw_spy_data * spy_data;
  394. /* Legacy structure managed by the ipw2x00-specific IEEE 802.11 layer */
  395. struct libipw_device * libipw;
  396. };
  397. /**************************** PROTOTYPES ****************************/
  398. /*
  399. * Functions part of the Wireless Extensions (defined in net/core/wireless.c).
  400. * Those may be called only within the kernel.
  401. */
  402. /* First : function strictly used inside the kernel */
  403. /* Handle /proc/net/wireless, called in net/code/dev.c */
  404. int dev_get_wireless_info(char *buffer, char **start, off_t offset, int length);
  405. /* Second : functions that may be called by driver modules */
  406. /* Send a single event to user space */
  407. void wireless_send_event(struct net_device *dev, unsigned int cmd,
  408. union iwreq_data *wrqu, const char *extra);
  409. #ifdef CONFIG_WEXT_CORE
  410. /* flush all previous wext events - if work is done from netdev notifiers */
  411. void wireless_nlevent_flush(void);
  412. #else
  413. static inline void wireless_nlevent_flush(void) {}
  414. #endif
  415. /* We may need a function to send a stream of events to user space.
  416. * More on that later... */
  417. /* Standard handler for SIOCSIWSPY */
  418. int iw_handler_set_spy(struct net_device *dev, struct iw_request_info *info,
  419. union iwreq_data *wrqu, char *extra);
  420. /* Standard handler for SIOCGIWSPY */
  421. int iw_handler_get_spy(struct net_device *dev, struct iw_request_info *info,
  422. union iwreq_data *wrqu, char *extra);
  423. /* Standard handler for SIOCSIWTHRSPY */
  424. int iw_handler_set_thrspy(struct net_device *dev, struct iw_request_info *info,
  425. union iwreq_data *wrqu, char *extra);
  426. /* Standard handler for SIOCGIWTHRSPY */
  427. int iw_handler_get_thrspy(struct net_device *dev, struct iw_request_info *info,
  428. union iwreq_data *wrqu, char *extra);
  429. /* Driver call to update spy records */
  430. void wireless_spy_update(struct net_device *dev, unsigned char *address,
  431. struct iw_quality *wstats);
  432. /************************* INLINE FUNTIONS *************************/
  433. /*
  434. * Function that are so simple that it's more efficient inlining them
  435. */
  436. static inline int iwe_stream_lcp_len(struct iw_request_info *info)
  437. {
  438. #ifdef CONFIG_COMPAT
  439. if (info->flags & IW_REQUEST_FLAG_COMPAT)
  440. return IW_EV_COMPAT_LCP_LEN;
  441. #endif
  442. return IW_EV_LCP_LEN;
  443. }
  444. static inline int iwe_stream_point_len(struct iw_request_info *info)
  445. {
  446. #ifdef CONFIG_COMPAT
  447. if (info->flags & IW_REQUEST_FLAG_COMPAT)
  448. return IW_EV_COMPAT_POINT_LEN;
  449. #endif
  450. return IW_EV_POINT_LEN;
  451. }
  452. static inline int iwe_stream_event_len_adjust(struct iw_request_info *info,
  453. int event_len)
  454. {
  455. #ifdef CONFIG_COMPAT
  456. if (info->flags & IW_REQUEST_FLAG_COMPAT) {
  457. event_len -= IW_EV_LCP_LEN;
  458. event_len += IW_EV_COMPAT_LCP_LEN;
  459. }
  460. #endif
  461. return event_len;
  462. }
  463. /*------------------------------------------------------------------*/
  464. /*
  465. * Wrapper to add an Wireless Event to a stream of events.
  466. */
  467. char *iwe_stream_add_event(struct iw_request_info *info, char *stream,
  468. char *ends, struct iw_event *iwe, int event_len);
  469. static inline char *
  470. iwe_stream_add_event_check(struct iw_request_info *info, char *stream,
  471. char *ends, struct iw_event *iwe, int event_len)
  472. {
  473. char *res = iwe_stream_add_event(info, stream, ends, iwe, event_len);
  474. if (res == stream)
  475. return ERR_PTR(-E2BIG);
  476. return res;
  477. }
  478. /*------------------------------------------------------------------*/
  479. /*
  480. * Wrapper to add an short Wireless Event containing a pointer to a
  481. * stream of events.
  482. */
  483. char *iwe_stream_add_point(struct iw_request_info *info, char *stream,
  484. char *ends, struct iw_event *iwe, char *extra);
  485. static inline char *
  486. iwe_stream_add_point_check(struct iw_request_info *info, char *stream,
  487. char *ends, struct iw_event *iwe, char *extra)
  488. {
  489. char *res = iwe_stream_add_point(info, stream, ends, iwe, extra);
  490. if (res == stream)
  491. return ERR_PTR(-E2BIG);
  492. return res;
  493. }
  494. /*------------------------------------------------------------------*/
  495. /*
  496. * Wrapper to add a value to a Wireless Event in a stream of events.
  497. * Be careful, this one is tricky to use properly :
  498. * At the first run, you need to have (value = event + IW_EV_LCP_LEN).
  499. */
  500. char *iwe_stream_add_value(struct iw_request_info *info, char *event,
  501. char *value, char *ends, struct iw_event *iwe,
  502. int event_len);
  503. #endif /* _IW_HANDLER_H */