4 * Jean II - HPLB 97->99 - HPL 99->02
6 * Common header for the Wireless Extension library...
8 * This file is released under the GPL license.
9 * Copyright (c) 1997-2002 Jean Tourrilhes <jt@hpl.hp.com>
15 /*#include "CHANGELOG.h"*/
17 /***************************** INCLUDES *****************************/
19 /* Standard headers */
20 #include <sys/types.h>
21 #include <sys/ioctl.h>
30 #include <netdb.h> /* gethostbyname, getnetbyname */
31 #include <net/ethernet.h> /* struct ether_addr */
32 #include <sys/time.h> /* struct timeval */
35 /* This is our header selection. Try to hide the mess and the misery :-(
36 * Don't look, you would go blind ;-) */
38 #ifndef LINUX_VERSION_CODE
39 #include <linux/version.h>
42 /* Kernel headers 2.4.X + Glibc 2.2 - Mandrake 8.0, Debian 2.3, RH 7.1
43 * Kernel headers 2.2.X + Glibc 2.2 - Slackware 8.0 */
44 #if defined(__GLIBC__) \
46 && __GLIBC_MINOR__ >= 2 \
47 && LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
48 //#define GLIBC22_HEADERS
49 #define GENERIC_HEADERS
51 /* Kernel headers 2.4.X + Glibc 2.1 - Debian 2.2 upgraded, RH 7.0
52 * Kernel headers 2.2.X + Glibc 2.1 - Debian 2.2, RH 6.1 */
53 #elif defined(__GLIBC__) \
55 && __GLIBC_MINOR__ == 1 \
56 && LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
57 //#define GLIBC_HEADERS
58 #define GENERIC_HEADERS
60 /* Kernel headers 2.2.X + Glibc 2.0 - Debian 2.1 */
61 #elif defined(__GLIBC__) \
63 && __GLIBC_MINOR__ == 0 \
64 && LINUX_VERSION_CODE >= KERNEL_VERSION(2,0,0) \
65 && LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
67 #define KLUDGE_HEADERS
69 /* Note : is it really worth supporting kernel 2.0.X, knowing that
70 * we require WE v9, which is only available in 2.2.X and higher ?
71 * I guess one could use 2.0.x with an upgraded wireless.h... */
73 /* Kernel headers 2.0.X + Glibc 2.0 - Debian 2.0, RH 5 */
74 #elif defined(__GLIBC__) \
76 && __GLIBC_MINOR__ == 0 \
77 && LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0) \
78 && LINUX_VERSION_CODE >= KERNEL_VERSION(2,0,0)
81 /* Kernel headers 2.0.X + libc5 - old systems */
82 #elif defined(_LINUX_C_LIB_VERSION_MAJOR) \
83 && _LINUX_C_LIB_VERSION_MAJOR == 5 \
84 && LINUX_VERSION_CODE >= KERNEL_VERSION(2,0,0) \
85 && LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
88 /* Unsupported combination */
90 #error "Your kernel/libc combination is not supported"
93 #ifdef GENERIC_HEADERS
94 /* Proposed by Dr. Michael Rietz <rietz@mail.amps.de>, 27.3.2 */
95 /* If this works for all, it might be more stable on the long term - Jean II */
96 #include <net/if_arp.h> /* For ARPHRD_ETHER */
97 #include <sys/socket.h> /* For AF_INET & struct sockaddr */
98 #include <netinet/in.h> /* For struct sockaddr_in */
99 #include <netinet/if_ether.h>
100 #endif /* GENERIC_HEADERS */
102 #ifdef GLIBC22_HEADERS
103 /* Added by Ross G. Miller <Ross_Miller@baylor.edu>, 3/28/01 */
104 #include <linux/if_arp.h> /* For ARPHRD_ETHER */
105 #include <linux/socket.h> /* For AF_INET & struct sockaddr */
106 #include <sys/socket.h>
107 #endif /* GLIBC22_HEADERS */
109 #ifdef KLUDGE_HEADERS
110 #include <socketbits.h>
111 #endif /* KLUDGE_HEADERS */
114 #include <linux/if_arp.h> /* For ARPHRD_ETHER */
115 #include <linux/socket.h> /* For AF_INET & struct sockaddr */
116 #include <linux/in.h> /* For struct sockaddr_in */
117 #endif /* KLUDGE_HEADERS || GLIBC_HEADERS */
120 #include <sys/socket.h> /* For AF_INET & struct sockaddr & socket() */
121 #include <linux/if_arp.h> /* For ARPHRD_ETHER */
122 #include <linux/in.h> /* For struct sockaddr_in */
123 #endif /* LIBC5_HEADERS */
126 /* Private copy of Wireless extensions */
128 #else /* !WEXT_HEADER */
129 /* System wide Wireless extensions */
130 #include <linux/wireless.h>
131 #endif /* !WEXT_HEADER */
133 /****************************** DEBUG ******************************/
136 /************************ CONSTANTS & MACROS ************************/
139 #define PROC_NET_WIRELESS "/proc/net/wireless"
140 #define PROC_NET_DEV "/proc/net/dev"
142 /* Some usefull constants */
147 /* Backward compatibility for Wireless Extension 9 */
148 #ifndef IW_POWER_MODIFIER
149 #define IW_POWER_MODIFIER 0x000F /* Modify a parameter */
150 #define IW_POWER_MIN 0x0001 /* Value is a minimum */
151 #define IW_POWER_MAX 0x0002 /* Value is a maximum */
152 #define IW_POWER_RELATIVE 0x0004 /* Value is not in seconds/ms/us */
153 #endif /* IW_POWER_MODIFIER */
155 #ifndef IW_ENCODE_NOKEY
156 #define IW_ENCODE_NOKEY 0x0800 /* Key is write only, so not here */
157 #define IW_ENCODE_MODE 0xF000 /* Modes defined below */
158 #endif /* IW_ENCODE_NOKEY */
160 /* More backward compatibility */
161 #ifndef SIOCSIWCOMMIT
162 #define SIOCSIWCOMMIT SIOCSIWNAME
163 #endif /* SIOCSIWCOMMIT */
165 /****************************** TYPES ******************************/
168 typedef struct iw_statistics iwstats;
169 typedef struct iw_range iwrange;
170 typedef struct iw_param iwparam;
171 typedef struct iw_freq iwfreq;
172 typedef struct iw_quality iwqual;
173 typedef struct iw_priv_args iwprivargs;
174 typedef struct sockaddr sockaddr;
176 /* Structure for storing all wireless information for each device
177 * This is pretty exhaustive... */
178 typedef struct wireless_info
180 char name[IFNAMSIZ + 1]; /* Wireless/protocol name */
182 iwparam nwid; /* Network ID */
184 double freq; /* Frequency/channel */
186 iwparam sens; /* sensitivity */
188 unsigned char key[IW_ENCODING_TOKEN_MAX]; /* Encoding key used */
189 int key_size; /* Number of bytes */
190 int key_flags; /* Various flags */
193 char essid[IW_ESSID_MAX_SIZE + 1]; /* ESSID (extended network) */
195 char nickname[IW_ESSID_MAX_SIZE + 1]; /* NickName */
197 sockaddr ap_addr; /* Access point address */
199 iwparam bitrate; /* Bit rate in bps */
201 iwparam rts; /* RTS threshold in bytes */
203 iwparam frag; /* Fragmentation threshold in bytes */
205 int mode; /* Operation mode */
207 iwparam power; /* Power management parameters */
209 iwparam txpower; /* Transmit Power in dBm */
211 iwparam retry; /* Retry limit or lifetime */
220 /* Structure for storing all wireless information for each device
221 * This is a cut down version of the one above, containing only
222 * the things *truly* needed to configure a card.
223 * Don't add other junk, I'll remove it... */
224 typedef struct wireless_config
226 char name[IFNAMSIZ + 1]; /* Wireless/protocol name */
228 iwparam nwid; /* Network ID */
230 double freq; /* Frequency/channel */
232 unsigned char key[IW_ENCODING_TOKEN_MAX]; /* Encoding key used */
233 int key_size; /* Number of bytes */
234 int key_flags; /* Various flags */
237 char essid[IW_ESSID_MAX_SIZE + 1]; /* ESSID (extended network) */
239 int mode; /* Operation mode */
242 typedef struct stream_descr
244 char * end; /* End of the stream */
245 char * current; /* Current event in stream of events */
246 char * value; /* Current value in event */
249 /* Prototype for handling display of each single interface on the
250 * system - see iw_enum_devices() */
251 typedef int (*iw_enum_handler)(int skfd,
256 /**************************** PROTOTYPES ****************************/
258 * All the functions in iwcommon.c
260 /* ---------------------- SOCKET SUBROUTINES -----------------------*/
262 iw_sockets_open(void);
264 iw_enum_devices(int skfd,
268 /* --------------------- WIRELESS SUBROUTINES ----------------------*/
270 iw_get_range_info(int skfd,
274 iw_print_version_info(char * toolname);
276 iw_get_priv_info(int skfd,
281 iw_get_basic_config(int skfd,
283 wireless_config * info);
285 iw_set_basic_config(int skfd,
287 wireless_config * info);
288 /* --------------------- PROTOCOL SUBROUTINES --------------------- */
290 iw_protocol_compare(char * protocol1,
292 /* -------------------- FREQUENCY SUBROUTINES --------------------- */
294 iw_float2freq(double in,
297 iw_freq2float(iwfreq * in);
299 iw_print_freq(char * buffer,
302 iw_freq_to_channel(double freq,
303 struct iw_range * range);
305 iw_print_bitrate(char * buffer,
307 /* ---------------------- POWER SUBROUTINES ----------------------- */
309 iw_dbm2mwatt(int in);
311 iw_mwatt2dbm(int in);
312 /* -------------------- STATISTICS SUBROUTINES -------------------- */
314 iw_get_stats(int skfd,
318 iw_print_stats(char * buffer,
322 /* --------------------- ENCODING SUBROUTINES --------------------- */
324 iw_print_key(char * buffer,
329 iw_in_key(char * input,
330 unsigned char * key);
331 /* ----------------- POWER MANAGEMENT SUBROUTINES ----------------- */
333 iw_print_pm_value(char * buffer,
337 iw_print_pm_mode(char * buffer,
339 /* --------------- RETRY LIMIT/LIFETIME SUBROUTINES --------------- */
340 #if WIRELESS_EXT > 10
342 iw_print_retry_value(char * buffer,
346 /* ----------------------- TIME SUBROUTINES ----------------------- */
348 iw_print_timeval(char * buffer,
349 const struct timeval * time);
350 /* --------------------- ADDRESS SUBROUTINES ---------------------- */
352 iw_check_mac_addr_type(int skfd,
355 iw_check_if_addr_type(int skfd,
359 iw_check_addr_type(int skfd,
363 iw_ether_ntop(const struct ether_addr* eth, char* buf);
365 iw_ether_ntoa(const struct ether_addr* eth);
367 iw_ether_aton(const char* bufp, struct ether_addr* eth);
369 iw_in_inet(char *bufp, struct sockaddr *sap);
374 struct sockaddr * sap);
375 /* ----------------------- MISC SUBROUTINES ------------------------ */
377 iw_get_priv_size(int args);
379 #if WIRELESS_EXT > 13
380 /* ---------------------- EVENT SUBROUTINES ---------------------- */
382 iw_init_event_stream(struct stream_descr * stream,
386 iw_extract_event_stream(struct stream_descr * stream,
387 struct iw_event * iwe);
388 #endif /* WIRELESS_EXT > 13 */
390 /**************************** VARIABLES ****************************/
392 extern const char * const iw_operation_mode[];
393 #define IW_NUM_OPER_MODE 7
395 /************************* INLINE FUNTIONS *************************/
397 * Functions that are so simple that it's more efficient inlining them
401 * Note : I've defined wrapper for the ioctl request so that
402 * it will be easier to migrate to other kernel API if needed
405 /*------------------------------------------------------------------*/
407 * Wrapper to push some Wireless Parameter in the driver
410 iw_set_ext(int skfd, /* Socket to the kernel */
411 char * ifname, /* Device name */
412 int request, /* WE ID */
413 struct iwreq * pwrq) /* Fixed part of the request */
415 /* Set device name */
416 strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
418 return(ioctl(skfd, request, pwrq));
421 /*------------------------------------------------------------------*/
423 * Wrapper to extract some Wireless Parameter out of the driver
426 iw_get_ext(int skfd, /* Socket to the kernel */
427 char * ifname, /* Device name */
428 int request, /* WE ID */
429 struct iwreq * pwrq) /* Fixed part of the request */
431 /* Set device name */
432 strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
434 return(ioctl(skfd, request, pwrq));
437 /*------------------------------------------------------------------*/
438 /* Backwards compatability
439 * Actually, those form are much easier to use when dealing with
440 * struct sockaddr... */
442 iw_pr_ether(char* bufp, const unsigned char* addr)
444 iw_ether_ntop((const struct ether_addr *) addr, bufp);
447 /* Backwards compatability */
449 iw_in_ether(const char *bufp, struct sockaddr *sap)
451 sap->sa_family = ARPHRD_ETHER;
452 return iw_ether_aton(bufp, (struct ether_addr *) sap->sa_data) ? 0 : -1;
455 /*------------------------------------------------------------------*/
457 * Create an Ethernet broadcast address
460 iw_broad_ether(struct sockaddr *sap)
462 sap->sa_family = ARPHRD_ETHER;
463 memset((char *) sap->sa_data, 0xFF, ETH_ALEN);
466 /*------------------------------------------------------------------*/
468 * Create an Ethernet NULL address
471 iw_null_ether(struct sockaddr *sap)
473 sap->sa_family = ARPHRD_ETHER;
474 memset((char *) sap->sa_data, 0x00, ETH_ALEN);