iwlib.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * Wireless Tools
  3. *
  4. * Jean II - HPLB 97->99 - HPL 99->07
  5. *
  6. * Common header for the Wireless Extension library...
  7. *
  8. * This file is released under the GPL license.
  9. * Copyright (c) 1997-2007 Jean Tourrilhes <jt@hpl.hp.com>
  10. */
  11. #ifndef IWLIB_H
  12. #define IWLIB_H
  13. /*#include "CHANGELOG.h"*/
  14. /***************************** INCLUDES *****************************/
  15. /* Standard headers */
  16. #include <sys/types.h>
  17. #include <sys/ioctl.h>
  18. #include <stdio.h>
  19. #include <math.h>
  20. #include <errno.h>
  21. #include <fcntl.h>
  22. #include <ctype.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <netdb.h> /* gethostbyname, getnetbyname */
  27. #include <net/ethernet.h> /* struct ether_addr */
  28. #include <sys/time.h> /* struct timeval */
  29. #include <unistd.h>
  30. /* This is our header selection. Try to hide the mess and the misery :-(
  31. * Don't look, you would go blind ;-)
  32. * Note : compatibility with *old* distributions has been removed,
  33. * you will need Glibc 2.2 and older to compile (which means
  34. * Mandrake 8.0, Debian 2.3, RH 7.1 or older).
  35. */
  36. /* Set of headers proposed by Dr. Michael Rietz <rietz@mail.amps.de>, 27.3.2 */
  37. #include <net/if_arp.h> /* For ARPHRD_ETHER */
  38. #include <sys/socket.h> /* For AF_INET & struct sockaddr */
  39. #include <netinet/in.h> /* For struct sockaddr_in */
  40. #include <netinet/if_ether.h>
  41. /* Fixup to be able to include kernel includes in userspace.
  42. * Basically, kill the sparse annotations... Jean II */
  43. #ifndef __user
  44. #define __user
  45. #endif
  46. #include <linux/types.h> /* for "caddr_t" et al */
  47. /* Glibc systems headers are supposedly less problematic than kernel ones */
  48. #include <sys/socket.h> /* for "struct sockaddr" et al */
  49. #include <net/if.h> /* for IFNAMSIZ and co... */
  50. /* Private copy of Wireless extensions (in this directoty) */
  51. #include "wireless.h"
  52. /* Make gcc understant that when we say inline, we mean it.
  53. * I really hate when the compiler is trying to be more clever than me,
  54. * because in this case gcc is not able to figure out functions with a
  55. * single call site, so not only I have to tag those functions inline
  56. * by hand, but then it refuse to inline them properly.
  57. * Total saving for iwevent : 150B = 0.7%.
  58. * Fortunately, in gcc 3.4, they now automatically inline static functions
  59. * with a single call site. Hurrah !
  60. * Jean II */
  61. #undef IW_GCC_HAS_BROKEN_INLINE
  62. #if __GNUC__ == 3
  63. #if __GNUC_MINOR__ >= 1 && __GNUC_MINOR__ < 4
  64. #define IW_GCC_HAS_BROKEN_INLINE 1
  65. #endif /* __GNUC_MINOR__ */
  66. #endif /* __GNUC__ */
  67. /* However, gcc 4.0 has introduce a new "feature", when compiling with
  68. * '-Os', it does not want to inline iw_ether_cmp() and friends.
  69. * So, we need to fix inline again !
  70. * Jean II */
  71. #if __GNUC__ == 4
  72. #define IW_GCC_HAS_BROKEN_INLINE 1
  73. #endif /* __GNUC__ */
  74. /* Now, really fix the inline */
  75. #ifdef IW_GCC_HAS_BROKEN_INLINE
  76. #ifdef inline
  77. #undef inline
  78. #endif /* inline */
  79. #define inline inline __attribute__((always_inline))
  80. #endif /* IW_GCC_HAS_BROKEN_INLINE */
  81. #ifdef __cplusplus
  82. extern "C" {
  83. #endif
  84. /****************************** DEBUG ******************************/
  85. //#define DEBUG 1
  86. /************************ CONSTANTS & MACROS ************************/
  87. /* Various versions information */
  88. /* Recommended Wireless Extension version */
  89. #define WE_VERSION 21
  90. /* Maximum forward compatibility built in this version of WT */
  91. #define WE_MAX_VERSION 22
  92. /* Version of Wireless Tools */
  93. #define WT_VERSION 29
  94. /* Paths */
  95. #define PROC_NET_WIRELESS "/proc/net/wireless"
  96. #define PROC_NET_DEV "/proc/net/dev"
  97. /* Some usefull constants */
  98. #define KILO 1e3
  99. #define MEGA 1e6
  100. #define GIGA 1e9
  101. /* For doing log10/exp10 without libm */
  102. #define LOG10_MAGIC 1.25892541179
  103. /* Backward compatibility for network headers */
  104. #ifndef ARPHRD_IEEE80211
  105. #define ARPHRD_IEEE80211 801 /* IEEE 802.11 */
  106. #endif /* ARPHRD_IEEE80211 */
  107. #ifndef IW_EV_LCP_PK_LEN
  108. /* Size of the Event prefix when packed in stream */
  109. #define IW_EV_LCP_PK_LEN (4)
  110. /* Size of the various events when packed in stream */
  111. #define IW_EV_CHAR_PK_LEN (IW_EV_LCP_PK_LEN + IFNAMSIZ)
  112. #define IW_EV_UINT_PK_LEN (IW_EV_LCP_PK_LEN + sizeof(__u32))
  113. #define IW_EV_FREQ_PK_LEN (IW_EV_LCP_PK_LEN + sizeof(struct iw_freq))
  114. #define IW_EV_PARAM_PK_LEN (IW_EV_LCP_PK_LEN + sizeof(struct iw_param))
  115. #define IW_EV_ADDR_PK_LEN (IW_EV_LCP_PK_LEN + sizeof(struct sockaddr))
  116. #define IW_EV_QUAL_PK_LEN (IW_EV_LCP_PK_LEN + sizeof(struct iw_quality))
  117. #define IW_EV_POINT_PK_LEN (IW_EV_LCP_PK_LEN + 4)
  118. struct iw_pk_event
  119. {
  120. __u16 len; /* Real lenght of this stuff */
  121. __u16 cmd; /* Wireless IOCTL */
  122. union iwreq_data u; /* IOCTL fixed payload */
  123. } __attribute__ ((packed));
  124. struct iw_pk_point
  125. {
  126. void __user *pointer; /* Pointer to the data (in user space) */
  127. __u16 length; /* number of fields or size in bytes */
  128. __u16 flags; /* Optional params */
  129. } __attribute__ ((packed));
  130. #define IW_EV_LCP_PK2_LEN (sizeof(struct iw_pk_event) - sizeof(union iwreq_data))
  131. #define IW_EV_POINT_PK2_LEN (IW_EV_LCP_PK2_LEN + sizeof(struct iw_pk_point) - IW_EV_POINT_OFF)
  132. #endif /* IW_EV_LCP_PK_LEN */
  133. /****************************** TYPES ******************************/
  134. /* Shortcuts */
  135. typedef struct iw_statistics iwstats;
  136. typedef struct iw_range iwrange;
  137. typedef struct iw_param iwparam;
  138. typedef struct iw_freq iwfreq;
  139. typedef struct iw_quality iwqual;
  140. typedef struct iw_priv_args iwprivargs;
  141. typedef struct sockaddr sockaddr;
  142. /* Structure for storing all wireless information for each device
  143. * This is a cut down version of the one above, containing only
  144. * the things *truly* needed to configure a card.
  145. * Don't add other junk, I'll remove it... */
  146. typedef struct wireless_config
  147. {
  148. char name[IFNAMSIZ + 1]; /* Wireless/protocol name */
  149. int has_nwid;
  150. iwparam nwid; /* Network ID */
  151. int has_freq;
  152. double freq; /* Frequency/channel */
  153. int freq_flags;
  154. int has_key;
  155. unsigned char key[IW_ENCODING_TOKEN_MAX]; /* Encoding key used */
  156. int key_size; /* Number of bytes */
  157. int key_flags; /* Various flags */
  158. int has_essid;
  159. int essid_on;
  160. char essid[IW_ESSID_MAX_SIZE + 1]; /* ESSID (extended network) */
  161. int has_mode;
  162. int mode; /* Operation mode */
  163. } wireless_config;
  164. /* Structure for storing all wireless information for each device
  165. * This is pretty exhaustive... */
  166. typedef struct wireless_info
  167. {
  168. struct wireless_config b; /* Basic information */
  169. int has_sens;
  170. iwparam sens; /* sensitivity */
  171. int has_nickname;
  172. char nickname[IW_ESSID_MAX_SIZE + 1]; /* NickName */
  173. int has_ap_addr;
  174. sockaddr ap_addr; /* Access point address */
  175. int has_bitrate;
  176. iwparam bitrate; /* Bit rate in bps */
  177. int has_rts;
  178. iwparam rts; /* RTS threshold in bytes */
  179. int has_frag;
  180. iwparam frag; /* Fragmentation threshold in bytes */
  181. int has_power;
  182. iwparam power; /* Power management parameters */
  183. int has_txpower;
  184. iwparam txpower; /* Transmit Power in dBm */
  185. int has_retry;
  186. iwparam retry; /* Retry limit or lifetime */
  187. /* Stats */
  188. iwstats stats;
  189. int has_stats;
  190. iwrange range;
  191. int has_range;
  192. /* Auth params for WPA/802.1x/802.11i */
  193. int auth_key_mgmt;
  194. int has_auth_key_mgmt;
  195. int auth_cipher_pairwise;
  196. int has_auth_cipher_pairwise;
  197. int auth_cipher_group;
  198. int has_auth_cipher_group;
  199. } wireless_info;
  200. /* Structure for storing an entry of a wireless scan.
  201. * This is only a subset of all possible information, the flexible
  202. * structure of scan results make it impossible to capture all
  203. * information in such a static structure. */
  204. typedef struct wireless_scan
  205. {
  206. /* Linked list */
  207. struct wireless_scan * next;
  208. /* Cell identifiaction */
  209. int has_ap_addr;
  210. sockaddr ap_addr; /* Access point address */
  211. /* Other information */
  212. struct wireless_config b; /* Basic information */
  213. iwstats stats; /* Signal strength */
  214. int has_stats;
  215. iwparam maxbitrate; /* Max bit rate in bps */
  216. int has_maxbitrate;
  217. } wireless_scan;
  218. /*
  219. * Context used for non-blocking scan.
  220. */
  221. typedef struct wireless_scan_head
  222. {
  223. wireless_scan * result; /* Result of the scan */
  224. int retry; /* Retry level */
  225. } wireless_scan_head;
  226. /* Structure used for parsing event streams, such as Wireless Events
  227. * and scan results */
  228. typedef struct stream_descr
  229. {
  230. char * end; /* End of the stream */
  231. char * current; /* Current event in stream of events */
  232. char * value; /* Current value in event */
  233. } stream_descr;
  234. /* Prototype for handling display of each single interface on the
  235. * system - see iw_enum_devices() */
  236. typedef int (*iw_enum_handler)(int skfd,
  237. char * ifname,
  238. char * args[],
  239. int count);
  240. /* Describe a modulation */
  241. typedef struct iw_modul_descr
  242. {
  243. unsigned int mask; /* Modulation bitmask */
  244. char cmd[8]; /* Short name */
  245. char * verbose; /* Verbose description */
  246. } iw_modul_descr;
  247. /**************************** PROTOTYPES ****************************/
  248. /*
  249. * All the functions in iwcommon.c
  250. */
  251. /* ---------------------- SOCKET SUBROUTINES -----------------------*/
  252. int
  253. iw_sockets_open(void);
  254. void
  255. iw_enum_devices(int skfd,
  256. iw_enum_handler fn,
  257. char * args[],
  258. int count);
  259. /* --------------------- WIRELESS SUBROUTINES ----------------------*/
  260. int
  261. iw_get_kernel_we_version(void);
  262. int
  263. iw_print_version_info(const char * toolname);
  264. int
  265. iw_get_range_info(int skfd,
  266. const char * ifname,
  267. iwrange * range);
  268. int
  269. iw_get_priv_info(int skfd,
  270. const char * ifname,
  271. iwprivargs ** ppriv);
  272. int
  273. iw_get_basic_config(int skfd,
  274. const char * ifname,
  275. wireless_config * info);
  276. int
  277. iw_set_basic_config(int skfd,
  278. const char * ifname,
  279. wireless_config * info);
  280. /* --------------------- PROTOCOL SUBROUTINES --------------------- */
  281. int
  282. iw_protocol_compare(const char * protocol1,
  283. const char * protocol2);
  284. /* -------------------- FREQUENCY SUBROUTINES --------------------- */
  285. void
  286. iw_float2freq(double in,
  287. iwfreq * out);
  288. double
  289. iw_freq2float(const iwfreq * in);
  290. void
  291. iw_print_freq_value(char * buffer,
  292. int buflen,
  293. double freq);
  294. void
  295. iw_print_freq(char * buffer,
  296. int buflen,
  297. double freq,
  298. int channel,
  299. int freq_flags);
  300. int
  301. iw_freq_to_channel(double freq,
  302. const struct iw_range * range);
  303. int
  304. iw_channel_to_freq(int channel,
  305. double * pfreq,
  306. const struct iw_range * range);
  307. void
  308. iw_print_bitrate(char * buffer,
  309. int buflen,
  310. int bitrate);
  311. /* ---------------------- POWER SUBROUTINES ----------------------- */
  312. int
  313. iw_dbm2mwatt(int in);
  314. int
  315. iw_mwatt2dbm(int in);
  316. void
  317. iw_print_txpower(char * buffer,
  318. int buflen,
  319. struct iw_param * txpower);
  320. /* -------------------- STATISTICS SUBROUTINES -------------------- */
  321. int
  322. iw_get_stats(int skfd,
  323. const char * ifname,
  324. iwstats * stats,
  325. const iwrange * range,
  326. int has_range);
  327. void
  328. iw_print_stats(char * buffer,
  329. int buflen,
  330. const iwqual * qual,
  331. const iwrange * range,
  332. int has_range);
  333. /* --------------------- ENCODING SUBROUTINES --------------------- */
  334. void
  335. iw_print_key(char * buffer,
  336. int buflen,
  337. const unsigned char * key,
  338. int key_size,
  339. int key_flags);
  340. int
  341. iw_in_key(const char * input,
  342. unsigned char * key);
  343. int
  344. iw_in_key_full(int skfd,
  345. const char * ifname,
  346. const char * input,
  347. unsigned char * key,
  348. __u16 * flags);
  349. /* ----------------- POWER MANAGEMENT SUBROUTINES ----------------- */
  350. void
  351. iw_print_pm_value(char * buffer,
  352. int buflen,
  353. int value,
  354. int flags,
  355. int we_version);
  356. void
  357. iw_print_pm_mode(char * buffer,
  358. int buflen,
  359. int flags);
  360. /* --------------- RETRY LIMIT/LIFETIME SUBROUTINES --------------- */
  361. void
  362. iw_print_retry_value(char * buffer,
  363. int buflen,
  364. int value,
  365. int flags,
  366. int we_version);
  367. /* ----------------------- TIME SUBROUTINES ----------------------- */
  368. void
  369. iw_print_timeval(char * buffer,
  370. int buflen,
  371. const struct timeval * time,
  372. const struct timezone * tz);
  373. /* --------------------- ADDRESS SUBROUTINES ---------------------- */
  374. int
  375. iw_check_mac_addr_type(int skfd,
  376. const char * ifname);
  377. int
  378. iw_check_if_addr_type(int skfd,
  379. const char * ifname);
  380. #if 0
  381. int
  382. iw_check_addr_type(int skfd,
  383. const char * ifname);
  384. #endif
  385. #if 0
  386. int
  387. iw_get_mac_addr(int skfd,
  388. const char * name,
  389. struct ether_addr * eth,
  390. unsigned short * ptype);
  391. #endif
  392. char *
  393. iw_mac_ntop(const unsigned char * mac,
  394. int maclen,
  395. char * buf,
  396. int buflen);
  397. void
  398. iw_ether_ntop(const struct ether_addr * eth,
  399. char * buf);
  400. char *
  401. iw_sawap_ntop(const struct sockaddr * sap,
  402. char * buf);
  403. int
  404. iw_mac_aton(const char * orig,
  405. unsigned char * mac,
  406. int macmax);
  407. int
  408. iw_ether_aton(const char* bufp, struct ether_addr* eth);
  409. int
  410. iw_in_inet(char *bufp, struct sockaddr *sap);
  411. int
  412. iw_in_addr(int skfd,
  413. const char * ifname,
  414. char * bufp,
  415. struct sockaddr * sap);
  416. /* ----------------------- MISC SUBROUTINES ------------------------ */
  417. int
  418. iw_get_priv_size(int args);
  419. /* ---------------------- EVENT SUBROUTINES ---------------------- */
  420. void
  421. iw_init_event_stream(struct stream_descr * stream,
  422. char * data,
  423. int len);
  424. int
  425. iw_extract_event_stream(struct stream_descr * stream,
  426. struct iw_event * iwe,
  427. int we_version);
  428. /* --------------------- SCANNING SUBROUTINES --------------------- */
  429. int
  430. iw_process_scan(int skfd,
  431. char * ifname,
  432. int we_version,
  433. wireless_scan_head * context);
  434. int
  435. iw_scan(int skfd,
  436. char * ifname,
  437. int we_version,
  438. wireless_scan_head * context);
  439. /**************************** VARIABLES ****************************/
  440. /* Modes as human readable strings */
  441. extern const char * const iw_operation_mode[];
  442. #define IW_NUM_OPER_MODE 7
  443. #define IW_NUM_OPER_MODE_EXT 8
  444. /* Modulations as human readable strings */
  445. extern const struct iw_modul_descr iw_modul_list[];
  446. #define IW_SIZE_MODUL_LIST 16
  447. /************************* INLINE FUNTIONS *************************/
  448. /*
  449. * Functions that are so simple that it's more efficient inlining them
  450. */
  451. /*
  452. * Note : I've defined wrapper for the ioctl request so that
  453. * it will be easier to migrate to other kernel API if needed
  454. */
  455. /*------------------------------------------------------------------*/
  456. /*
  457. * Wrapper to push some Wireless Parameter in the driver
  458. */
  459. static inline int
  460. iw_set_ext(int skfd, /* Socket to the kernel */
  461. const char * ifname, /* Device name */
  462. int request, /* WE ID */
  463. struct iwreq * pwrq) /* Fixed part of the request */
  464. {
  465. /* Set device name */
  466. strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
  467. /* Do the request */
  468. return(ioctl(skfd, request, pwrq));
  469. }
  470. /*------------------------------------------------------------------*/
  471. /*
  472. * Wrapper to extract some Wireless Parameter out of the driver
  473. */
  474. static inline int
  475. iw_get_ext(int skfd, /* Socket to the kernel */
  476. const char * ifname, /* Device name */
  477. int request, /* WE ID */
  478. struct iwreq * pwrq) /* Fixed part of the request */
  479. {
  480. /* Set device name */
  481. strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
  482. /* Do the request */
  483. return(ioctl(skfd, request, pwrq));
  484. }
  485. /*------------------------------------------------------------------*/
  486. /*
  487. * Close the socket used for ioctl.
  488. */
  489. static inline void
  490. iw_sockets_close(int skfd)
  491. {
  492. close(skfd);
  493. }
  494. /*------------------------------------------------------------------*/
  495. /*
  496. * Display an Ethernet Socket Address in readable format.
  497. */
  498. static inline char *
  499. iw_saether_ntop(const struct sockaddr *sap, char* bufp)
  500. {
  501. iw_ether_ntop((const struct ether_addr *) sap->sa_data, bufp);
  502. return bufp;
  503. }
  504. /*------------------------------------------------------------------*/
  505. /*
  506. * Input an Ethernet Socket Address and convert to binary.
  507. */
  508. static inline int
  509. iw_saether_aton(const char *bufp, struct sockaddr *sap)
  510. {
  511. sap->sa_family = ARPHRD_ETHER;
  512. return iw_ether_aton(bufp, (struct ether_addr *) sap->sa_data);
  513. }
  514. /*------------------------------------------------------------------*/
  515. /*
  516. * Create an Ethernet broadcast address
  517. */
  518. static inline void
  519. iw_broad_ether(struct sockaddr *sap)
  520. {
  521. sap->sa_family = ARPHRD_ETHER;
  522. memset((char *) sap->sa_data, 0xFF, ETH_ALEN);
  523. }
  524. /*------------------------------------------------------------------*/
  525. /*
  526. * Create an Ethernet NULL address
  527. */
  528. static inline void
  529. iw_null_ether(struct sockaddr *sap)
  530. {
  531. sap->sa_family = ARPHRD_ETHER;
  532. memset((char *) sap->sa_data, 0x00, ETH_ALEN);
  533. }
  534. /*------------------------------------------------------------------*/
  535. /*
  536. * Compare two ethernet addresses
  537. */
  538. static inline int
  539. iw_ether_cmp(const struct ether_addr* eth1, const struct ether_addr* eth2)
  540. {
  541. return memcmp(eth1, eth2, sizeof(*eth1));
  542. }
  543. #ifdef __cplusplus
  544. }
  545. #endif
  546. #endif /* IWLIB_H */