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 #ifdef PRIVATE_WE_HEADER
126 /* Private copy of Wireless extensions */
127 #include "wireless.h"
128 #else /* PRIVATE_WE_HEADER */
129 /* System wide Wireless extensions */
130 #include <linux/wireless.h>
131 #endif /* PRIVATE_WE_HEADER */
134 #error "Wireless Extension v9 or newer required :-( - \
135 Use Wireless Tools v19 or update your kernel headers"
137 #if WIRELESS_EXT < 14
138 #warning "Wireless Extension v14 recommended (but not mandatory)... - \
139 You may update your kernel and/or system headers to get the new features, \
140 or you may just ignore this message"
143 /****************************** DEBUG ******************************/
146 /************************ CONSTANTS & MACROS ************************/
149 #define PROC_NET_WIRELESS "/proc/net/wireless"
151 /* Some usefull constants */
156 /* Backward compatibility for Wireless Extension 9 */
157 #ifndef IW_POWER_MODIFIER
158 #define IW_POWER_MODIFIER 0x000F /* Modify a parameter */
159 #define IW_POWER_MIN 0x0001 /* Value is a minimum */
160 #define IW_POWER_MAX 0x0002 /* Value is a maximum */
161 #define IW_POWER_RELATIVE 0x0004 /* Value is not in seconds/ms/us */
162 #endif /* IW_POWER_MODIFIER */
164 #ifndef IW_ENCODE_NOKEY
165 #define IW_ENCODE_NOKEY 0x0800 /* Key is write only, so not here */
166 #define IW_ENCODE_MODE 0xF000 /* Modes defined below */
167 #endif /* IW_ENCODE_NOKEY */
169 /* More backward compatibility */
170 #ifndef SIOCSIWCOMMIT
171 #define SIOCSIWCOMMIT SIOCSIWNAME
172 #endif /* SIOCSIWCOMMIT */
174 /****************************** TYPES ******************************/
177 typedef struct iw_statistics iwstats;
178 typedef struct iw_range iwrange;
179 typedef struct iw_param iwparam;
180 typedef struct iw_freq iwfreq;
181 typedef struct iw_quality iwqual;
182 typedef struct iw_priv_args iwprivargs;
183 typedef struct sockaddr sockaddr;
185 /* Structure for storing all wireless information for each device
186 * This is pretty exhaustive... */
187 typedef struct wireless_info
189 char name[IFNAMSIZ]; /* Wireless/protocol name */
191 iwparam nwid; /* Network ID */
193 float freq; /* Frequency/channel */
195 iwparam sens; /* sensitivity */
197 unsigned char key[IW_ENCODING_TOKEN_MAX]; /* Encoding key used */
198 int key_size; /* Number of bytes */
199 int key_flags; /* Various flags */
202 char essid[IW_ESSID_MAX_SIZE + 1]; /* ESSID (extended network) */
204 char nickname[IW_ESSID_MAX_SIZE + 1]; /* NickName */
206 sockaddr ap_addr; /* Access point address */
208 iwparam bitrate; /* Bit rate in bps */
210 iwparam rts; /* RTS threshold in bytes */
212 iwparam frag; /* Fragmentation threshold in bytes */
214 int mode; /* Operation mode */
216 iwparam power; /* Power management parameters */
218 iwparam txpower; /* Transmit Power in dBm */
220 iwparam retry; /* Retry limit or lifetime */
229 /* Structure for storing all wireless information for each device
230 * This is a cut down version of the one above, containing only
231 * the things *truly* needed to configure a card.
232 * Don't add other junk, I'll remove it... */
233 typedef struct wireless_config
235 char name[IFNAMSIZ]; /* Wireless/protocol name */
237 iwparam nwid; /* Network ID */
239 float freq; /* Frequency/channel */
241 unsigned char key[IW_ENCODING_TOKEN_MAX]; /* Encoding key used */
242 int key_size; /* Number of bytes */
243 int key_flags; /* Various flags */
246 char essid[IW_ESSID_MAX_SIZE + 1]; /* ESSID (extended network) */
248 int mode; /* Operation mode */
251 typedef struct stream_descr
253 char * end; /* End of the stream */
254 char * current; /* Current event in stream of events */
255 char * value; /* Current value in event */
258 /* Prototype for handling display of each single interface on the
259 * system - see iw_enum_devices() */
260 typedef int (*iw_enum_handler)(int skfd,
265 /**************************** PROTOTYPES ****************************/
267 * All the functions in iwcommon.c
269 /* ---------------------- SOCKET SUBROUTINES -----------------------*/
271 iw_sockets_open(void);
273 iw_enum_devices(int skfd,
277 /* --------------------- WIRELESS SUBROUTINES ----------------------*/
279 iw_get_range_info(int skfd,
283 iw_get_priv_info(int skfd,
287 iw_get_basic_config(int skfd,
289 wireless_config * info);
291 iw_set_basic_config(int skfd,
293 wireless_config * info);
294 /* -------------------- FREQUENCY SUBROUTINES --------------------- */
296 iw_float2freq(double in,
299 iw_freq2float(iwfreq * in);
301 iw_print_freq(char * buffer,
304 iw_print_bitrate(char * buffer,
306 /* ---------------------- POWER SUBROUTINES ----------------------- */
308 iw_dbm2mwatt(int in);
310 iw_mwatt2dbm(int in);
311 /* -------------------- STATISTICS SUBROUTINES -------------------- */
313 iw_get_stats(int skfd,
317 iw_print_stats(char * buffer,
321 /* --------------------- ENCODING SUBROUTINES --------------------- */
323 iw_print_key(char * buffer,
328 iw_in_key(char * input,
329 unsigned char * key);
330 /* ----------------- POWER MANAGEMENT SUBROUTINES ----------------- */
332 iw_print_pm_value(char * buffer,
336 iw_print_pm_mode(char * buffer,
338 /* --------------- RETRY LIMIT/LIFETIME SUBROUTINES --------------- */
339 #if WIRELESS_EXT > 10
341 iw_print_retry_value(char * buffer,
345 /* ----------------------- TIME SUBROUTINES ----------------------- */
347 iw_print_timeval(char * buffer,
348 const struct timeval * time);
349 /* --------------------- ADDRESS SUBROUTINES ---------------------- */
351 iw_check_mac_addr_type(int skfd,
354 iw_check_if_addr_type(int skfd,
358 iw_check_addr_type(int skfd,
362 iw_ether_ntop(const struct ether_addr* eth, char* buf);
364 iw_ether_ntoa(const struct ether_addr* eth);
366 iw_ether_aton(const char* bufp, struct ether_addr* eth);
368 iw_in_inet(char *bufp, struct sockaddr *sap);
373 struct sockaddr * sap);
374 /* ----------------------- MISC SUBROUTINES ------------------------ */
376 iw_byte_size(int args);
378 #if WIRELESS_EXT > 13
379 /* ---------------------- EVENT SUBROUTINES ---------------------- */
381 iw_init_event_stream(struct stream_descr * stream,
385 iw_extract_event_stream(struct stream_descr * stream,
386 struct iw_event * iwe);
387 #endif /* WIRELESS_EXT > 13 */
389 /**************************** VARIABLES ****************************/
391 extern const char * const iw_operation_mode[];
392 #define IW_NUM_OPER_MODE 6
394 /************************* INLINE FUNTIONS *************************/
396 * Functions that are so simple that it's more efficient inlining them
400 * Note : I've defined wrapper for the ioctl request so that
401 * it will be easier to migrate to other kernel API if needed
404 /*------------------------------------------------------------------*/
406 * Wrapper to push some Wireless Parameter in the driver
409 iw_set_ext(int skfd, /* Socket to the kernel */
410 char * ifname, /* Device name */
411 int request, /* WE ID */
412 struct iwreq * pwrq) /* Fixed part of the request */
414 /* Set device name */
415 strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
417 return(ioctl(skfd, request, pwrq));
420 /*------------------------------------------------------------------*/
422 * Wrapper to extract some Wireless Parameter out of the driver
425 iw_get_ext(int skfd, /* Socket to the kernel */
426 char * ifname, /* Device name */
427 int request, /* WE ID */
428 struct iwreq * pwrq) /* Fixed part of the request */
430 /* Set device name */
431 strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
433 return(ioctl(skfd, request, pwrq));
436 /*------------------------------------------------------------------*/
437 /* Backwards compatability
438 * Actually, those form are much easier to use when dealing with
439 * struct sockaddr... */
441 iw_pr_ether(char* bufp, const unsigned char* addr)
443 iw_ether_ntop((const struct ether_addr *) addr, bufp);
446 /* Backwards compatability */
448 iw_in_ether(const char *bufp, struct sockaddr *sap)
450 sap->sa_family = ARPHRD_ETHER;
451 return iw_ether_aton(bufp, (struct ether_addr *) sap->sa_data) ? 0 : -1;
454 /*------------------------------------------------------------------*/
456 * Create an Ethernet broadcast address
459 iw_broad_ether(struct sockaddr *sap)
461 sap->sa_family = ARPHRD_ETHER;
462 memset((char *) sap->sa_data, 0xFF, ETH_ALEN);
465 /*------------------------------------------------------------------*/
467 * Create an Ethernet NULL address
470 iw_null_ether(struct sockaddr *sap)
472 sap->sa_family = ARPHRD_ETHER;
473 memset((char *) sap->sa_data, 0x00, ETH_ALEN);