4 * Jean II - HPLB 97->99 - HPL 99->01
6 * Common subroutines to all the wireless tools...
8 * This file is released under the GPL license.
11 #include "iwlib.h" /* Header */
13 /**************************** VARIABLES ****************************/
15 const char * iw_operation_mode[] = { "Auto",
22 /************************ SOCKET SUBROUTINES *************************/
24 /*------------------------------------------------------------------*/
27 * Depending on the protocol present, open the right socket. The socket
28 * will allow us to talk to the driver.
33 int ipx_sock = -1; /* IPX socket */
34 int ax25_sock = -1; /* AX.25 socket */
35 int inet_sock = -1; /* INET socket */
36 int ddp_sock = -1; /* Appletalk DDP socket */
39 * Now pick any (exisiting) useful socket family for generic queries
40 * Note : don't open all the socket, only returns when one matches,
41 * all protocols might not be valid.
42 * Workaround by Jim Kaba <jkaba@sarnoff.com>
43 * Note : in 99% of the case, we will just open the inet_sock.
44 * The remaining 1% case are not fully correct...
46 inet_sock=socket(AF_INET, SOCK_DGRAM, 0);
49 ipx_sock=socket(AF_IPX, SOCK_DGRAM, 0);
52 ax25_sock=socket(AF_AX25, SOCK_DGRAM, 0);
55 ddp_sock=socket(AF_APPLETALK, SOCK_DGRAM, 0);
57 * If this is -1 we have no known network layers and its time to jump.
62 /*********************** WIRELESS SUBROUTINES ************************/
64 /*------------------------------------------------------------------*/
66 * Get the range information out of the driver
69 iw_get_range_info(int skfd,
74 char buffer[sizeof(iwrange) * 2]; /* Large enough */
77 memset(buffer, 0, sizeof(range));
79 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
80 wrq.u.data.pointer = (caddr_t) buffer;
81 wrq.u.data.length = 0;
83 if(ioctl(skfd, SIOCGIWRANGE, &wrq) < 0)
86 /* Copy stuff at the right place, ignore extra */
87 memcpy((char *) range, buffer, sizeof(iwrange));
89 /* Lot's of people have driver and tools out of sync as far as Wireless
90 * Extensions are concerned. It's because /usr/include/linux/wireless.h
91 * and /usr/src/linux/include/linux/wireless.h are different.
92 * We try to catch this stuff here... */
94 /* For new versions, we can check the version directly, for old versions
95 * we use magic. 300 bytes is a also magic number, don't touch... */
96 if((WIRELESS_EXT > 10) && (wrq.u.data.length >= 300))
99 /* Version verification - for new versions */
100 if(range->we_version_compiled != WIRELESS_EXT)
102 fprintf(stderr, "Warning : Device %s has been compiled with version %d\n", ifname, range->we_version_compiled);
103 fprintf(stderr, "of Wireless Extension, while we are using version %d.\n", WIRELESS_EXT);
104 fprintf(stderr, "Some things may be broken...\n\n");
106 #endif /* WIRELESS_EXT > 10 */
110 /* Version verification - for old versions */
111 if(wrq.u.data.length != sizeof(iwrange))
113 fprintf(stderr, "Warning : Device %s has been compiled with a different version\n", ifname);
114 fprintf(stderr, "of Wireless Extension than ours (we are using version %d).\n", WIRELESS_EXT);
115 fprintf(stderr, "Some things may be broken...\n\n");
119 /* Note : we are only trying to catch compile difference, not source.
120 * If the driver source has not been updated to the latest, it doesn't
121 * matter because the new fields are set to zero */
126 /*------------------------------------------------------------------*/
128 * Get information about what private ioctls are supported by the driver
131 iw_get_priv_info(int skfd,
138 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
139 wrq.u.data.pointer = (caddr_t) priv;
140 wrq.u.data.length = 0;
141 wrq.u.data.flags = 0;
142 if(ioctl(skfd, SIOCGIWPRIV, &wrq) < 0)
145 /* Return the number of ioctls */
146 return(wrq.u.data.length);
149 /*------------------------------------------------------------------*/
151 * Get essential wireless config from the device driver
152 * We will call all the classical wireless ioctl on the driver through
153 * the socket to know what is supported and to get the settings...
154 * Note : compare to the version in iwconfig, we extract only
155 * what's *really* needed to configure a device...
158 iw_get_basic_config(int skfd,
160 wireless_config * info)
164 memset((char *) info, 0, sizeof(struct wireless_config));
166 /* Get wireless name */
167 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
168 if(ioctl(skfd, SIOCGIWNAME, &wrq) < 0)
169 /* If no wireless name : no wireless extensions */
172 strcpy(info->name, wrq.u.name);
175 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
176 if(ioctl(skfd, SIOCGIWNWID, &wrq) >= 0)
179 memcpy(&(info->nwid), &(wrq.u.nwid), sizeof(iwparam));
182 /* Get frequency / channel */
183 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
184 if(ioctl(skfd, SIOCGIWFREQ, &wrq) >= 0)
187 info->freq = iw_freq2float(&(wrq.u.freq));
190 /* Get encryption information */
191 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
192 wrq.u.data.pointer = (caddr_t) info->key;
193 wrq.u.data.length = 0;
194 wrq.u.data.flags = 0;
195 if(ioctl(skfd, SIOCGIWENCODE, &wrq) >= 0)
198 info->key_size = wrq.u.data.length;
199 info->key_flags = wrq.u.data.flags;
203 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
204 wrq.u.essid.pointer = (caddr_t) info->essid;
205 wrq.u.essid.length = 0;
206 wrq.u.essid.flags = 0;
207 if(ioctl(skfd, SIOCGIWESSID, &wrq) >= 0)
210 info->essid_on = wrq.u.data.flags;
213 /* Get operation mode */
214 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
215 if(ioctl(skfd, SIOCGIWMODE, &wrq) >= 0)
217 if((wrq.u.mode < 6) && (wrq.u.mode >= 0))
219 info->mode = wrq.u.mode;
225 /*------------------------------------------------------------------*/
227 * Set essential wireless config in the device driver
228 * We will call all the classical wireless ioctl on the driver through
229 * the socket to know what is supported and to set the settings...
230 * We support only the restricted set as above...
233 iw_set_basic_config(int skfd,
235 wireless_config * info)
240 /* Get wireless name (check if interface is valid) */
241 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
242 if(ioctl(skfd, SIOCGIWNAME, &wrq) < 0)
243 /* If no wireless name : no wireless extensions */
246 /* Set Network ID, if available (this is for non-802.11 cards) */
249 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
250 memcpy(&(wrq.u.nwid), &(info->nwid), sizeof(iwparam));
251 wrq.u.nwid.fixed = 1; /* Hum... When in Rome... */
253 if(ioctl(skfd, SIOCSIWNWID, &wrq) < 0)
255 fprintf(stderr, "SIOCSIWNWID: %s\n", strerror(errno));
260 /* Set frequency / channel */
263 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
264 iw_float2freq(info->freq, &(wrq.u.freq));
266 if(ioctl(skfd, SIOCSIWFREQ, &wrq) < 0)
268 fprintf(stderr, "SIOCSIWFREQ: %s\n", strerror(errno));
273 /* Set encryption information */
276 int flags = info->key_flags;
278 /* Check if there is a key index */
279 if((flags & IW_ENCODE_INDEX) > 0)
282 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
283 wrq.u.data.pointer = (caddr_t) NULL;
284 wrq.u.data.flags = flags & (IW_ENCODE_INDEX);
285 wrq.u.data.length = 0;
287 if(ioctl(skfd, SIOCSIWENCODE, &wrq) < 0)
289 fprintf(stderr, "SIOCSIWENCODE(%d): %s\n",
290 errno, strerror(errno));
295 /* Mask out index to minimise probability of reject when setting key */
296 flags = flags & (~IW_ENCODE_INDEX);
298 /* Set the key itself (set current key in this case) */
299 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
300 wrq.u.data.pointer = (caddr_t) info->key;
301 wrq.u.data.length = info->key_size;
302 wrq.u.data.flags = flags;
304 if(ioctl(skfd, SIOCSIWENCODE, &wrq) < 0)
306 fprintf(stderr, "SIOCSIWENCODE(%d): %s\n",
307 errno, strerror(errno));
312 /* Set ESSID (extended network), if available */
315 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
316 wrq.u.essid.pointer = (caddr_t) info->essid;
317 wrq.u.essid.length = strlen(info->essid) + 1;
318 wrq.u.data.flags = info->essid_on;
320 if(ioctl(skfd, SIOCSIWESSID, &wrq) < 0)
322 fprintf(stderr, "SIOCSIWESSID: %s\n", strerror(errno));
327 /* Set the current mode of operation */
330 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
331 wrq.u.mode = info->mode;
333 if(ioctl(skfd, SIOCSIWMODE, &wrq) < 0)
335 fprintf(stderr, "SIOCSIWMODE: %s\n", strerror(errno));
343 /********************** FREQUENCY SUBROUTINES ***********************/
345 /*------------------------------------------------------------------*/
347 * Convert a floating point the our internal representation of
349 * The kernel doesn't want to hear about floating point, so we use
350 * this custom format instead.
353 iw_float2freq(double in,
356 out->e = (short) (floor(log10(in)));
359 out->m = ((long) (floor(in / pow(10,out->e - 6)))) * 100;
369 /*------------------------------------------------------------------*/
371 * Convert our internal representation of frequencies to a floating point.
374 iw_freq2float(iwfreq * in)
376 return ((double) in->m) * pow(10,in->e);
379 /************************ POWER SUBROUTINES *************************/
381 /*------------------------------------------------------------------*/
383 * Convert a value in dBm to a value in milliWatt.
388 return((int) (floor(pow(10.0, (((double) in) / 10.0)))));
391 /*------------------------------------------------------------------*/
393 * Convert a value in milliWatt to a value in dBm.
398 return((int) (ceil(10.0 * log10((double) in))));
401 /********************** STATISTICS SUBROUTINES **********************/
403 /*------------------------------------------------------------------*/
405 * Read /proc/net/wireless to get the latest statistics
408 iw_get_stats(int skfd,
412 #if WIRELESS_EXT > 11
414 wrq.u.data.pointer = (caddr_t) stats;
415 wrq.u.data.length = 0;
416 wrq.u.data.flags = 1; /* Clear updated flag */
417 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
418 if(ioctl(skfd, SIOCGIWSTATS, &wrq) < 0)
422 #else /* WIRELESS_EXT > 11 */
423 FILE * f=fopen("/proc/net/wireless","r");
429 /* Loop on all devices */
430 while(fgets(buf,255,f))
433 while(*bp&&isspace(*bp))
435 /* Is it the good device ? */
436 if(strncmp(bp,ifname,strlen(ifname))==0 && bp[strlen(ifname)]==':')
442 bp = strtok(bp, " ");
443 sscanf(bp, "%X", &t);
444 stats->status = (unsigned short) t;
445 /* -- link quality -- */
446 bp = strtok(NULL, " ");
447 if(strchr(bp,'.') != NULL)
448 stats->qual.updated |= 1;
449 sscanf(bp, "%d", &t);
450 stats->qual.qual = (unsigned char) t;
451 /* -- signal level -- */
452 bp = strtok(NULL, " ");
453 if(strchr(bp,'.') != NULL)
454 stats->qual.updated |= 2;
455 sscanf(bp, "%d", &t);
456 stats->qual.level = (unsigned char) t;
457 /* -- noise level -- */
458 bp = strtok(NULL, " ");
459 if(strchr(bp,'.') != NULL)
460 stats->qual.updated += 4;
461 sscanf(bp, "%d", &t);
462 stats->qual.noise = (unsigned char) t;
463 /* -- discarded packets -- */
464 bp = strtok(NULL, " ");
465 sscanf(bp, "%d", &stats->discard.nwid);
466 bp = strtok(NULL, " ");
467 sscanf(bp, "%d", &stats->discard.code);
468 bp = strtok(NULL, " ");
469 sscanf(bp, "%d", &stats->discard.misc);
476 #endif /* WIRELESS_EXT > 11 */
479 /*------------------------------------------------------------------*/
481 * Output the link statistics, taking care of formating
484 iw_print_stats(char * buffer,
490 if(has_range && (qual->level != 0))
492 /* If the statistics are in dBm */
493 if(qual->level > range->max_qual.level)
495 /* Statistics are in dBm (absolute power measurement) */
497 "Quality:%d/%d Signal level:%d dBm Noise level:%d dBm%s",
498 qual->qual, range->max_qual.qual,
499 qual->level - 0x100, qual->noise - 0x100,
500 (qual->updated & 0x7) ? " (updated)" : "");
504 /* Statistics are relative values (0 -> max) */
506 "Quality:%d/%d Signal level:%d/%d Noise level:%d/%d%s",
507 qual->qual, range->max_qual.qual,
508 qual->level, range->max_qual.level,
509 qual->noise, range->max_qual.noise,
510 (qual->updated & 0x7) ? " (updated)" : "");
515 /* We can't read the range, so we don't know... */
516 sprintf(buffer, "Quality:%d Signal level:%d Noise level:%d%s",
517 qual->qual, qual->level, qual->noise,
518 (qual->updated & 0x7) ? " (updated)" : "");
522 /*********************** ENCODING SUBROUTINES ***********************/
524 /*------------------------------------------------------------------*/
526 * Output the encoding key, with a nice formating
529 iw_print_key(char * buffer,
536 /* Is the key present ??? */
537 if(key_flags & IW_ENCODE_NOKEY)
539 /* Nope : print dummy */
540 strcpy(buffer, "**");
542 for(i = 1; i < key_size; i++)
545 strcpy(buffer++, "-");
546 strcpy(buffer, "**");
552 /* Yes : print the key */
553 sprintf(buffer, "%.2X", key[0]);
555 for(i = 1; i < key_size; i++)
558 strcpy(buffer++, "-");
559 sprintf(buffer, "%.2X", key[i]);
566 /******************* POWER MANAGEMENT SUBROUTINES *******************/
568 /*------------------------------------------------------------------*/
570 * Output a power management value with all attributes...
573 iw_print_pm_value(char * buffer,
578 if(flags & IW_POWER_MIN)
580 strcpy(buffer, " min");
583 if(flags & IW_POWER_MAX)
585 strcpy(buffer, " max");
590 if(flags & IW_POWER_TIMEOUT)
592 strcpy(buffer, " timeout:");
597 strcpy(buffer, " period:");
601 /* Display value without units */
602 if(flags & IW_POWER_RELATIVE)
603 sprintf(buffer, "%g ", ((double) value) / MEGA);
606 /* Display value with units */
607 if(value >= (int) MEGA)
608 sprintf(buffer, "%gs ", ((double) value) / MEGA);
610 if(value >= (int) KILO)
611 sprintf(buffer, "%gms ", ((double) value) / KILO);
613 sprintf(buffer, "%dus ", value);
617 /*------------------------------------------------------------------*/
619 * Output a power management mode
622 iw_print_pm_mode(char * buffer,
625 /* Print the proper mode... */
626 switch(flags & IW_POWER_MODE)
628 case IW_POWER_UNICAST_R:
629 strcpy(buffer, " mode:Unicast only received");
631 case IW_POWER_MULTICAST_R:
632 strcpy(buffer, " mode:Multicast only received");
635 strcpy(buffer, " mode:All packets received");
637 case IW_POWER_FORCE_S:
638 strcpy(buffer, " mode:Force sending");
640 case IW_POWER_REPEATER:
641 strcpy(buffer, " mode:Repeat multicasts");
647 /***************** RETRY LIMIT/LIFETIME SUBROUTINES *****************/
649 #if WIRELESS_EXT > 10
650 /*------------------------------------------------------------------*/
652 * Output a retry value with all attributes...
655 iw_print_retry_value(char * buffer,
660 if(flags & IW_RETRY_MIN)
662 strcpy(buffer, " min");
665 if(flags & IW_RETRY_MAX)
667 strcpy(buffer, " max");
671 /* Type lifetime of limit */
672 if(flags & IW_RETRY_LIFETIME)
674 strcpy(buffer, " lifetime:");
677 /* Display value without units */
678 if(flags & IW_POWER_RELATIVE)
679 sprintf(buffer, "%g", ((double) value) / MEGA);
682 /* Display value with units */
683 if(value >= (int) MEGA)
684 sprintf(buffer, "%gs", ((double) value) / MEGA);
686 if(value >= (int) KILO)
687 sprintf(buffer, "%gms", ((double) value) / KILO);
689 sprintf(buffer, "%dus", value);
693 sprintf(buffer, " limit:%d", value);
695 #endif /* WIRELESS_EXT > 10 */
697 /*********************** ADDRESS SUBROUTINES ************************/
699 * This section is mostly a cut & past from net-tools-1.2.0
700 * manage address display and input...
703 /*------------------------------------------------------------------*/
705 * Check if interface support the right address types...
708 iw_check_addr_type(int skfd,
713 /* Get the type of interface address */
714 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
715 if((ioctl(skfd, SIOCGIFADDR, &ifr) < 0) ||
716 (ifr.ifr_addr.sa_family != AF_INET))
718 /* Deep trouble... */
719 fprintf(stderr, "Interface %s doesn't support IP addresses\n", ifname);
724 printf("Interface : %d - 0x%lX\n", ifr.ifr_addr.sa_family,
725 *((unsigned long *) ifr.ifr_addr.sa_data));
728 /* Get the type of hardware address */
729 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
730 if((ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0) ||
731 (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER))
733 /* Deep trouble... */
734 fprintf(stderr, "Interface %s doesn't support MAC addresses\n",
740 printf("Hardware : %d - %s\n", ifr.ifr_hwaddr.sa_family,
741 pr_ether(ifr.ifr_hwaddr.sa_data));
748 /*------------------------------------------------------------------*/
750 * Display an Ethernet address in readable format.
753 iw_pr_ether(char * buffer,
756 sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X",
757 (ptr[0] & 0xFF), (ptr[1] & 0xFF), (ptr[2] & 0xFF),
758 (ptr[3] & 0xFF), (ptr[4] & 0xFF), (ptr[5] & 0xFF)
763 /*------------------------------------------------------------------*/
765 * Input an Ethernet address and convert to binary.
768 iw_in_ether(char *bufp, struct sockaddr *sap)
774 sap->sa_family = ARPHRD_ETHER;
779 while((*bufp != '\0') && (i < ETH_ALEN)) {
782 if (isdigit(c)) val = c - '0';
783 else if (c >= 'a' && c <= 'f') val = c - 'a' + 10;
784 else if (c >= 'A' && c <= 'F') val = c - 'A' + 10;
787 fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
794 if (isdigit(c)) val |= c - '0';
795 else if (c >= 'a' && c <= 'f') val |= c - 'a' + 10;
796 else if (c >= 'A' && c <= 'F') val |= c - 'A' + 10;
799 fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
804 *ptr++ = (unsigned char) (val & 0377);
807 /* We might get a semicolon here - not required. */
811 fprintf(stderr, "in_ether(%s): trailing : ignored!\n",
820 /* That's it. Any trailing junk? */
821 if ((i == ETH_ALEN) && (*bufp != '\0')) {
823 fprintf(stderr, "in_ether(%s): trailing junk!\n", orig);
830 fprintf(stderr, "in_ether(%s): %s\n", orig, pr_ether(sap->sa_data));
836 /*------------------------------------------------------------------*/
838 * Input an Internet address and convert to binary.
841 iw_in_inet(char *bufp, struct sockaddr *sap)
846 struct sockaddr_in *sin = (struct sockaddr_in *) sap;
849 sin->sin_family = AF_INET;
852 /* Default is special, meaning 0.0.0.0. */
853 if (!strcmp(name, "default")) {
854 sin->sin_addr.s_addr = INADDR_ANY;
858 /* Try the NETWORKS database to see if this is a known network. */
859 if ((np = getnetbyname(name)) != (struct netent *)NULL) {
860 sin->sin_addr.s_addr = htonl(np->n_net);
861 strcpy(name, np->n_name);
865 if ((hp = gethostbyname(name)) == (struct hostent *)NULL) {
869 memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0], hp->h_length);
870 strcpy(name, hp->h_name);
874 /*------------------------------------------------------------------*/
876 * Input an address and convert to binary.
882 struct sockaddr *sap)
884 /* Check if it is a hardware or IP address */
885 if(index(bufp, ':') == NULL)
887 struct sockaddr if_address;
888 struct arpreq arp_query;
890 /* Read interface address */
891 if(iw_in_inet(bufp, &if_address) < 0)
893 fprintf(stderr, "Invalid interface address %s\n", bufp);
897 /* Translate IP addresses to MAC addresses */
898 memcpy((char *) &(arp_query.arp_pa),
899 (char *) &if_address,
900 sizeof(struct sockaddr));
901 arp_query.arp_ha.sa_family = 0;
902 arp_query.arp_flags = 0;
903 /* The following restrict the search to the interface only */
904 /* For old kernels which complain, just comment it... */
905 strncpy(arp_query.arp_dev, ifname, IFNAMSIZ);
906 if((ioctl(skfd, SIOCGARP, &arp_query) < 0) ||
907 !(arp_query.arp_flags & ATF_COM))
909 fprintf(stderr, "Arp failed for %s on %s... (%d)\nTry to ping the address before setting it.\n",
910 bufp, ifname, errno);
914 /* Store new MAC address */
916 (char *) &(arp_query.arp_ha),
917 sizeof(struct sockaddr));
920 printf("IP Address %s => Hw Address = %s\n",
921 bufp, pr_ether(sap->sa_data));
924 else /* If it's an hardware address */
925 /* Get the hardware address */
926 if(iw_in_ether(bufp, sap) < 0)
928 fprintf(stderr, "Invalid hardware address %s\n", bufp);
933 printf("Hw Address = %s\n", pr_ether(sap->sa_data));
939 /************************* MISC SUBROUTINES **************************/
941 /*------------------------------------------------------------------*/
945 iw_byte_size(int args)
947 int ret = args & IW_PRIV_SIZE_MASK;
949 if(((args & IW_PRIV_TYPE_MASK) == IW_PRIV_TYPE_INT) ||
950 ((args & IW_PRIV_TYPE_MASK) == IW_PRIV_TYPE_FLOAT))
953 if((args & IW_PRIV_TYPE_MASK) == IW_PRIV_TYPE_NONE)