4 * Jean II - HPLB 97->99 - HPL 99->09
6 * Common header for the Wireless Extension library...
8 * This file is released under the GPL license.
9 * Copyright (c) 1997-2009 Jean Tourrilhes <jt@hpl.hp.com>
15 /***************************** INCLUDES *****************************/
17 /* Standard headers */
18 #include <sys/types.h>
19 #include <sys/ioctl.h>
28 #include <netdb.h> /* gethostbyname, getnetbyname */
29 #include <net/ethernet.h> /* struct ether_addr */
30 #include <sys/time.h> /* struct timeval */
33 /* This is our header selection. Try to hide the mess and the misery :-(
34 * Don't look, you would go blind ;-)
35 * Note : compatibility with *old* distributions has been removed,
36 * you will need Glibc 2.2 and older to compile (which means
37 * Mandrake 8.0, Debian 2.3, RH 7.1 or older).
40 /* Set of headers proposed by Dr. Michael Rietz <rietz@mail.amps.de>, 27.3.2 */
41 #include <net/if_arp.h> /* For ARPHRD_ETHER */
42 #include <sys/socket.h> /* For AF_INET & struct sockaddr */
43 #include <netinet/in.h> /* For struct sockaddr_in */
44 #include <netinet/if_ether.h>
46 /* Fixup to be able to include kernel includes in userspace.
47 * Basically, kill the sparse annotations... Jean II */
52 #include <linux/types.h> /* for "caddr_t" et al */
54 /* Glibc systems headers are supposedly less problematic than kernel ones */
55 #include <sys/socket.h> /* for "struct sockaddr" et al */
56 #include <net/if.h> /* for IFNAMSIZ and co... */
58 /* Private copy of Wireless extensions (in this directoty) */
65 /************************ CONSTANTS & MACROS ************************/
67 /* Various versions information */
68 /* Recommended Wireless Extension version */
70 /* Maximum forward compatibility built in this version of WT */
71 #define WE_MAX_VERSION 22
72 /* Version of Wireless Tools */
75 /****************************** TYPES ******************************/
78 typedef struct iw_statistics iwstats;
79 typedef struct iw_range iwrange;
80 typedef struct iw_param iwparam;
81 typedef struct iw_freq iwfreq;
82 typedef struct iw_quality iwqual;
83 typedef struct iw_priv_args iwprivargs;
84 typedef struct sockaddr sockaddr;
86 /* Structure for storing all wireless information for each device
87 * This is a cut down version of the one above, containing only
88 * the things *truly* needed to configure a card.
89 * Don't add other junk, I'll remove it... */
90 typedef struct wireless_config
92 char name[IFNAMSIZ + 1]; /* Wireless/protocol name */
94 iwparam nwid; /* Network ID */
96 double freq; /* Frequency/channel */
99 unsigned char key[IW_ENCODING_TOKEN_MAX]; /* Encoding key used */
100 int key_size; /* Number of bytes */
101 int key_flags; /* Various flags */
104 char essid[IW_ESSID_MAX_SIZE + 2]; /* ESSID (extended network) */
107 int mode; /* Operation mode */
110 /* Structure for storing all wireless information for each device
111 * This is pretty exhaustive... */
112 typedef struct wireless_info
114 struct wireless_config b; /* Basic information */
117 iwparam sens; /* sensitivity */
119 char nickname[IW_ESSID_MAX_SIZE + 2]; /* NickName */
121 sockaddr ap_addr; /* Access point address */
123 iwparam bitrate; /* Bit rate in bps */
125 iwparam rts; /* RTS threshold in bytes */
127 iwparam frag; /* Fragmentation threshold in bytes */
129 iwparam power; /* Power management parameters */
131 iwparam txpower; /* Transmit Power in dBm */
133 iwparam retry; /* Retry limit or lifetime */
141 /* Auth params for WPA/802.1x/802.11i */
143 int has_auth_key_mgmt;
144 int auth_cipher_pairwise;
145 int has_auth_cipher_pairwise;
146 int auth_cipher_group;
147 int has_auth_cipher_group;
150 /* Structure for storing an entry of a wireless scan.
151 * This is only a subset of all possible information, the flexible
152 * structure of scan results make it impossible to capture all
153 * information in such a static structure. */
154 typedef struct wireless_scan
157 struct wireless_scan * next;
159 /* Cell identifiaction */
161 sockaddr ap_addr; /* Access point address */
163 /* Other information */
164 struct wireless_config b; /* Basic information */
165 iwstats stats; /* Signal strength */
167 iwparam maxbitrate; /* Max bit rate in bps */
172 * Context used for non-blocking scan.
174 typedef struct wireless_scan_head
176 wireless_scan * result; /* Result of the scan */
177 int retry; /* Retry level */
178 } wireless_scan_head;
180 /* Structure used for parsing event streams, such as Wireless Events
181 * and scan results */
182 typedef struct stream_descr
184 char * end; /* End of the stream */
185 char * current; /* Current event in stream of events */
186 char * value; /* Current value in event */
189 /* Prototype for handling display of each single interface on the
190 * system - see iw_enum_devices() */
191 typedef int (*iw_enum_handler)(int skfd,
196 /* Describe a modulation */
197 typedef struct iw_modul_descr
199 unsigned int mask; /* Modulation bitmask */
200 char cmd[8]; /* Short name */
201 char * verbose; /* Verbose description */
204 /**************************** PROTOTYPES ****************************/
206 * All the functions in iwlib.c
209 /* ---------------------- SOCKET SUBROUTINES -----------------------*/
211 iw_sockets_open(void);
213 iw_enum_devices(int skfd,
217 /* --------------------- WIRELESS SUBROUTINES ----------------------*/
219 iw_get_kernel_we_version(void);
221 iw_print_version_info(const char * toolname);
223 iw_get_range_info(int skfd,
227 iw_get_priv_info(int skfd,
229 iwprivargs ** ppriv);
231 iw_get_basic_config(int skfd,
233 wireless_config * info);
235 iw_set_basic_config(int skfd,
237 wireless_config * info);
238 /* --------------------- PROTOCOL SUBROUTINES --------------------- */
240 iw_protocol_compare(const char * protocol1,
241 const char * protocol2);
242 /* ---------------------- ESSID SUBROUTINES ---------------------- */
244 iw_essid_escape(char * dest,
248 iw_essid_unescape(char * dest,
250 /* -------------------- FREQUENCY SUBROUTINES --------------------- */
252 iw_float2freq(double in,
255 iw_freq2float(const iwfreq * in);
257 iw_print_freq_value(char * buffer,
261 iw_print_freq(char * buffer,
267 iw_freq_to_channel(double freq,
268 const struct iw_range * range);
270 iw_channel_to_freq(int channel,
272 const struct iw_range * range);
274 iw_print_bitrate(char * buffer,
277 /* ---------------------- POWER SUBROUTINES ----------------------- */
279 iw_dbm2mwatt(int in);
281 iw_mwatt2dbm(int in);
283 iw_print_txpower(char * buffer,
285 struct iw_param * txpower);
286 /* -------------------- STATISTICS SUBROUTINES -------------------- */
288 iw_get_stats(int skfd,
291 const iwrange * range,
294 iw_print_stats(char * buffer,
297 const iwrange * range,
299 /* --------------------- ENCODING SUBROUTINES --------------------- */
301 iw_print_key(char * buffer,
303 const unsigned char * key,
307 iw_in_key(const char * input,
308 unsigned char * key);
310 iw_in_key_full(int skfd,
315 /* ----------------- POWER MANAGEMENT SUBROUTINES ----------------- */
317 iw_print_pm_value(char * buffer,
323 iw_print_pm_mode(char * buffer,
326 /* --------------- RETRY LIMIT/LIFETIME SUBROUTINES --------------- */
328 iw_print_retry_value(char * buffer,
333 /* ----------------------- TIME SUBROUTINES ----------------------- */
335 iw_print_timeval(char * buffer,
337 const struct timeval * time,
338 const struct timezone * tz);
339 /* --------------------- ADDRESS SUBROUTINES ---------------------- */
341 iw_check_mac_addr_type(int skfd,
342 const char * ifname);
344 iw_check_if_addr_type(int skfd,
345 const char * ifname);
347 iw_mac_ntop(const unsigned char * mac,
352 iw_ether_ntop(const struct ether_addr * eth,
355 iw_sawap_ntop(const struct sockaddr * sap,
358 iw_mac_aton(const char * orig,
362 iw_ether_aton(const char* bufp, struct ether_addr* eth);
364 iw_in_inet(char *bufp, struct sockaddr *sap);
369 struct sockaddr * sap);
370 /* ----------------------- MISC SUBROUTINES ------------------------ */
372 iw_get_priv_size(int args);
374 /* ---------------------- EVENT SUBROUTINES ---------------------- */
376 iw_init_event_stream(struct stream_descr * stream,
380 iw_extract_event_stream(struct stream_descr * stream,
381 struct iw_event * iwe,
383 /* --------------------- SCANNING SUBROUTINES --------------------- */
385 iw_process_scan(int skfd,
388 wireless_scan_head * context);
393 wireless_scan_head * context);
395 /**************************** VARIABLES ****************************/
397 /* Modes as human readable strings */
398 extern const char * const iw_operation_mode[];
399 #define IW_NUM_OPER_MODE 7
400 #define IW_NUM_OPER_MODE_EXT 8
402 /* Modulations as human readable strings */
403 extern const struct iw_modul_descr iw_modul_list[];
404 #define IW_SIZE_MODUL_LIST 16
406 /************************* INLINE FUNTIONS *************************/
408 * Functions that are so simple that it's more efficient inlining them
409 * Most inline are private because gcc is fussy about inline...
413 * Note : I've defined wrapper for the ioctl request so that
414 * it will be easier to migrate to other kernel API if needed
417 /*------------------------------------------------------------------*/
419 * Wrapper to push some Wireless Parameter in the driver
421 static inline __attribute__((always_inline)) int
422 iw_set_ext(int skfd, /* Socket to the kernel */
423 const char * ifname, /* Device name */
424 int request, /* WE ID */
425 struct iwreq * pwrq) /* Fixed part of the request */
427 /* Set device name */
428 strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
430 return(ioctl(skfd, request, pwrq));
433 /*------------------------------------------------------------------*/
435 * Wrapper to extract some Wireless Parameter out of the driver
437 static inline __attribute__((always_inline)) int
438 iw_get_ext(int skfd, /* Socket to the kernel */
439 const char * ifname, /* Device name */
440 int request, /* WE ID */
441 struct iwreq * pwrq) /* Fixed part of the request */
443 /* Set device name */
444 strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
446 return(ioctl(skfd, request, pwrq));
449 /*------------------------------------------------------------------*/
451 * Close the socket used for ioctl.
454 iw_sockets_close(int skfd)