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 */
125 /* Those 3 headers were previously included in wireless.h */
126 #include <linux/types.h> /* for "caddr_t" et al */
127 #include <linux/socket.h> /* for "struct sockaddr" et al */
128 #include <linux/if.h> /* for IFNAMSIZ and co... */
131 /* Private copy of Wireless extensions */
133 #else /* !WEXT_HEADER */
134 /* System wide Wireless extensions */
135 #include <linux/wireless.h>
136 #endif /* !WEXT_HEADER */
142 /****************************** DEBUG ******************************/
145 /************************ CONSTANTS & MACROS ************************/
148 #define PROC_NET_WIRELESS "/proc/net/wireless"
149 #define PROC_NET_DEV "/proc/net/dev"
151 /* Some usefull constants */
155 /* For doing log10/exp10 without libm */
156 #define LOG10_MAGIC 1.25892541179
158 /* Backward compatibility for Wireless Extension 9 */
159 #ifndef IW_POWER_MODIFIER
160 #define IW_POWER_MODIFIER 0x000F /* Modify a parameter */
161 #define IW_POWER_MIN 0x0001 /* Value is a minimum */
162 #define IW_POWER_MAX 0x0002 /* Value is a maximum */
163 #define IW_POWER_RELATIVE 0x0004 /* Value is not in seconds/ms/us */
164 #endif /* IW_POWER_MODIFIER */
166 #ifndef IW_ENCODE_NOKEY
167 #define IW_ENCODE_NOKEY 0x0800 /* Key is write only, so not here */
168 #define IW_ENCODE_MODE 0xF000 /* Modes defined below */
169 #endif /* IW_ENCODE_NOKEY */
170 #ifndef IW_ENCODE_TEMP
171 #define IW_ENCODE_TEMP 0x0400 /* Temporary key */
172 #endif /* IW_ENCODE_TEMP */
174 /* More backward compatibility */
175 #ifndef SIOCSIWCOMMIT
176 #define SIOCSIWCOMMIT SIOCSIWNAME
177 #endif /* SIOCSIWCOMMIT */
179 /****************************** TYPES ******************************/
182 typedef struct iw_statistics iwstats;
183 typedef struct iw_range iwrange;
184 typedef struct iw_param iwparam;
185 typedef struct iw_freq iwfreq;
186 typedef struct iw_quality iwqual;
187 typedef struct iw_priv_args iwprivargs;
188 typedef struct sockaddr sockaddr;
190 /* Structure for storing all wireless information for each device
191 * This is pretty exhaustive... */
192 typedef struct wireless_info
194 char name[IFNAMSIZ + 1]; /* Wireless/protocol name */
196 iwparam nwid; /* Network ID */
198 double freq; /* Frequency/channel */
200 iwparam sens; /* sensitivity */
202 unsigned char key[IW_ENCODING_TOKEN_MAX]; /* Encoding key used */
203 int key_size; /* Number of bytes */
204 int key_flags; /* Various flags */
207 char essid[IW_ESSID_MAX_SIZE + 1]; /* ESSID (extended network) */
209 char nickname[IW_ESSID_MAX_SIZE + 1]; /* NickName */
211 sockaddr ap_addr; /* Access point address */
213 iwparam bitrate; /* Bit rate in bps */
215 iwparam rts; /* RTS threshold in bytes */
217 iwparam frag; /* Fragmentation threshold in bytes */
219 int mode; /* Operation mode */
221 iwparam power; /* Power management parameters */
223 iwparam txpower; /* Transmit Power in dBm */
225 iwparam retry; /* Retry limit or lifetime */
234 /* Structure for storing all wireless information for each device
235 * This is a cut down version of the one above, containing only
236 * the things *truly* needed to configure a card.
237 * Don't add other junk, I'll remove it... */
238 typedef struct wireless_config
240 char name[IFNAMSIZ + 1]; /* Wireless/protocol name */
242 iwparam nwid; /* Network ID */
244 double freq; /* Frequency/channel */
246 unsigned char key[IW_ENCODING_TOKEN_MAX]; /* Encoding key used */
247 int key_size; /* Number of bytes */
248 int key_flags; /* Various flags */
251 char essid[IW_ESSID_MAX_SIZE + 1]; /* ESSID (extended network) */
253 int mode; /* Operation mode */
256 typedef struct stream_descr
258 char * end; /* End of the stream */
259 char * current; /* Current event in stream of events */
260 char * value; /* Current value in event */
263 /* Prototype for handling display of each single interface on the
264 * system - see iw_enum_devices() */
265 typedef int (*iw_enum_handler)(int skfd,
270 /**************************** PROTOTYPES ****************************/
272 * All the functions in iwcommon.c
275 /* ---------------------- SOCKET SUBROUTINES -----------------------*/
277 iw_sockets_open(void);
279 iw_enum_devices(int skfd,
283 /* --------------------- WIRELESS SUBROUTINES ----------------------*/
285 iw_get_range_info(int skfd,
289 iw_print_version_info(char * toolname);
291 iw_get_priv_info(int skfd,
296 iw_get_basic_config(int skfd,
298 wireless_config * info);
300 iw_set_basic_config(int skfd,
302 wireless_config * info);
303 /* --------------------- PROTOCOL SUBROUTINES --------------------- */
305 iw_protocol_compare(char * protocol1,
307 /* -------------------- FREQUENCY SUBROUTINES --------------------- */
309 iw_float2freq(double in,
312 iw_freq2float(iwfreq * in);
314 iw_print_freq(char * buffer,
317 iw_freq_to_channel(double freq,
318 struct iw_range * range);
320 iw_print_bitrate(char * buffer,
322 /* ---------------------- POWER SUBROUTINES ----------------------- */
324 iw_dbm2mwatt(int in);
326 iw_mwatt2dbm(int in);
327 /* -------------------- STATISTICS SUBROUTINES -------------------- */
329 iw_get_stats(int skfd,
333 iw_print_stats(char * buffer,
337 /* --------------------- ENCODING SUBROUTINES --------------------- */
339 iw_print_key(char * buffer,
344 iw_in_key(char * input,
345 unsigned char * key);
347 iw_in_key_full(int skfd,
352 /* ----------------- POWER MANAGEMENT SUBROUTINES ----------------- */
354 iw_print_pm_value(char * buffer,
358 iw_print_pm_mode(char * buffer,
360 /* --------------- RETRY LIMIT/LIFETIME SUBROUTINES --------------- */
361 #if WIRELESS_EXT > 10
363 iw_print_retry_value(char * buffer,
367 /* ----------------------- TIME SUBROUTINES ----------------------- */
369 iw_print_timeval(char * buffer,
370 const struct timeval * time);
371 /* --------------------- ADDRESS SUBROUTINES ---------------------- */
373 iw_check_mac_addr_type(int skfd,
376 iw_check_if_addr_type(int skfd,
380 iw_check_addr_type(int skfd,
384 iw_ether_ntop(const struct ether_addr* eth, char* buf);
386 iw_ether_ntoa(const struct ether_addr* eth);
388 iw_ether_aton(const char* bufp, struct ether_addr* eth);
390 iw_in_inet(char *bufp, struct sockaddr *sap);
395 struct sockaddr * sap);
396 /* ----------------------- MISC SUBROUTINES ------------------------ */
398 iw_get_priv_size(int args);
400 #if WIRELESS_EXT > 13
401 /* ---------------------- EVENT SUBROUTINES ---------------------- */
403 iw_init_event_stream(struct stream_descr * stream,
407 iw_extract_event_stream(struct stream_descr * stream,
408 struct iw_event * iwe);
409 #endif /* WIRELESS_EXT > 13 */
411 /**************************** VARIABLES ****************************/
413 extern const char * const iw_operation_mode[];
414 #define IW_NUM_OPER_MODE 7
416 /************************* INLINE FUNTIONS *************************/
418 * Functions that are so simple that it's more efficient inlining them
422 * Note : I've defined wrapper for the ioctl request so that
423 * it will be easier to migrate to other kernel API if needed
426 /*------------------------------------------------------------------*/
428 * Wrapper to push some Wireless Parameter in the driver
431 iw_set_ext(int skfd, /* Socket to the kernel */
432 char * ifname, /* Device name */
433 int request, /* WE ID */
434 struct iwreq * pwrq) /* Fixed part of the request */
436 /* Set device name */
437 strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
439 return(ioctl(skfd, request, pwrq));
442 /*------------------------------------------------------------------*/
444 * Wrapper to extract some Wireless Parameter out of the driver
447 iw_get_ext(int skfd, /* Socket to the kernel */
448 char * ifname, /* Device name */
449 int request, /* WE ID */
450 struct iwreq * pwrq) /* Fixed part of the request */
452 /* Set device name */
453 strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
455 return(ioctl(skfd, request, pwrq));
458 /*------------------------------------------------------------------*/
459 /* Backwards compatability
460 * Actually, those form are much easier to use when dealing with
461 * struct sockaddr... */
463 iw_pr_ether(char* bufp, const unsigned char* addr)
465 iw_ether_ntop((const struct ether_addr *) addr, bufp);
468 /* Backwards compatability */
470 iw_in_ether(const char *bufp, struct sockaddr *sap)
472 sap->sa_family = ARPHRD_ETHER;
473 return iw_ether_aton(bufp, (struct ether_addr *) sap->sa_data) ? 0 : -1;
476 /*------------------------------------------------------------------*/
478 * Create an Ethernet broadcast address
481 iw_broad_ether(struct sockaddr *sap)
483 sap->sa_family = ARPHRD_ETHER;
484 memset((char *) sap->sa_data, 0xFF, ETH_ALEN);
487 /*------------------------------------------------------------------*/
489 * Create an Ethernet NULL address
492 iw_null_ether(struct sockaddr *sap)
494 sap->sa_family = ARPHRD_ETHER;
495 memset((char *) sap->sa_data, 0x00, ETH_ALEN);