2 * iwinfo - Wireless Information Library - Linux Wireless Extension Backend
4 * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
6 * The iwinfo library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
10 * The iwinfo library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with the iwinfo library. If not, see http://www.gnu.org/licenses/.
18 * Parts of this code are derived from the Linux wireless tools, iwlib.c,
19 * iwlist.c and iwconfig.c in particular.
23 #include "iwinfo_wext.h"
25 static double wext_freq2float(const struct iw_freq
*in
)
28 double res
= (double) in
->m
;
29 for(i
= 0; i
< in
->e
; i
++) res
*= 10;
33 static inline int wext_freq2mhz(const struct iw_freq
*in
)
41 return (int)(wext_freq2float(in
) / 1000000);
45 static inline int wext_ioctl(const char *ifname
, int cmd
, struct iwreq
*wrq
)
47 if( !strncmp(ifname
, "mon.", 4) )
48 strncpy(wrq
->ifr_name
, &ifname
[4], IFNAMSIZ
);
50 strncpy(wrq
->ifr_name
, ifname
, IFNAMSIZ
);
52 return iwinfo_ioctl(cmd
, wrq
);
56 static int wext_probe(const char *ifname
)
60 if(wext_ioctl(ifname
, SIOCGIWNAME
, &wrq
) >= 0)
66 static void wext_close(void)
71 static int wext_get_mode(const char *ifname
, int *buf
)
75 if(wext_ioctl(ifname
, SIOCGIWMODE
, &wrq
) >= 0)
80 *buf
= IWINFO_OPMODE_ADHOC
;
84 *buf
= IWINFO_OPMODE_CLIENT
;
88 *buf
= IWINFO_OPMODE_MASTER
;
92 *buf
= IWINFO_OPMODE_MONITOR
;
96 *buf
= IWINFO_OPMODE_UNKNOWN
;
106 static int wext_get_ssid(const char *ifname
, char *buf
)
110 wrq
.u
.essid
.pointer
= (caddr_t
) buf
;
111 wrq
.u
.essid
.length
= IW_ESSID_MAX_SIZE
+ 1;
112 wrq
.u
.essid
.flags
= 0;
114 if(wext_ioctl(ifname
, SIOCGIWESSID
, &wrq
) >= 0)
120 static int wext_get_bssid(const char *ifname
, char *buf
)
124 if(wext_ioctl(ifname
, SIOCGIWAP
, &wrq
) >= 0)
126 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
127 (uint8_t)wrq
.u
.ap_addr
.sa_data
[0], (uint8_t)wrq
.u
.ap_addr
.sa_data
[1],
128 (uint8_t)wrq
.u
.ap_addr
.sa_data
[2], (uint8_t)wrq
.u
.ap_addr
.sa_data
[3],
129 (uint8_t)wrq
.u
.ap_addr
.sa_data
[4], (uint8_t)wrq
.u
.ap_addr
.sa_data
[5]);
137 static int wext_get_bitrate(const char *ifname
, int *buf
)
141 if(wext_ioctl(ifname
, SIOCGIWRATE
, &wrq
) >= 0)
143 *buf
= (wrq
.u
.bitrate
.value
/ 1000);
150 static int wext_get_channel(const char *ifname
, int *buf
)
153 struct iw_range range
;
157 if(wext_ioctl(ifname
, SIOCGIWFREQ
, &wrq
) >= 0)
159 if( wrq
.u
.freq
.m
>= 1000 )
161 freq
= wext_freq2float(&wrq
.u
.freq
);
162 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
163 wrq
.u
.data
.length
= sizeof(struct iw_range
);
164 wrq
.u
.data
.flags
= 0;
166 if(wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0)
168 for(i
= 0; i
< range
.num_frequency
; i
++)
170 if( wext_freq2float(&range
.freq
[i
]) == freq
)
172 *buf
= range
.freq
[i
].i
;
188 static int wext_get_frequency(const char *ifname
, int *buf
)
191 struct iw_range range
;
194 if(wext_ioctl(ifname
, SIOCGIWFREQ
, &wrq
) >= 0)
196 /* We got a channel number instead ... */
197 if( wrq
.u
.freq
.m
< 1000 )
199 channel
= wrq
.u
.freq
.m
;
200 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
201 wrq
.u
.data
.length
= sizeof(struct iw_range
);
202 wrq
.u
.data
.flags
= 0;
204 if(wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0)
206 for(i
= 0; i
< range
.num_frequency
; i
++)
208 if( range
.freq
[i
].i
== channel
)
210 *buf
= wext_freq2mhz(&range
.freq
[i
]);
218 *buf
= wext_freq2mhz(&wrq
.u
.freq
);
226 static int wext_get_txpower(const char *ifname
, int *buf
)
230 wrq
.u
.txpower
.flags
= 0;
232 if(wext_ioctl(ifname
, SIOCGIWTXPOW
, &wrq
) >= 0)
234 if(wrq
.u
.txpower
.flags
& IW_TXPOW_MWATT
)
235 *buf
= iwinfo_mw2dbm(wrq
.u
.txpower
.value
);
237 *buf
= wrq
.u
.txpower
.value
;
245 static int wext_get_signal(const char *ifname
, int *buf
)
248 struct iw_statistics stats
;
250 wrq
.u
.data
.pointer
= (caddr_t
) &stats
;
251 wrq
.u
.data
.length
= sizeof(struct iw_statistics
);
252 wrq
.u
.data
.flags
= 1;
254 if(wext_ioctl(ifname
, SIOCGIWSTATS
, &wrq
) >= 0)
256 *buf
= (stats
.qual
.updated
& IW_QUAL_DBM
)
257 ? (stats
.qual
.level
- 0x100) : stats
.qual
.level
;
265 static int wext_get_noise(const char *ifname
, int *buf
)
268 struct iw_statistics stats
;
270 wrq
.u
.data
.pointer
= (caddr_t
) &stats
;
271 wrq
.u
.data
.length
= sizeof(struct iw_statistics
);
272 wrq
.u
.data
.flags
= 1;
274 if(wext_ioctl(ifname
, SIOCGIWSTATS
, &wrq
) >= 0)
276 *buf
= (stats
.qual
.updated
& IW_QUAL_DBM
)
277 ? (stats
.qual
.noise
- 0x100) : stats
.qual
.noise
;
285 static int wext_get_quality(const char *ifname
, int *buf
)
288 struct iw_statistics stats
;
290 wrq
.u
.data
.pointer
= (caddr_t
) &stats
;
291 wrq
.u
.data
.length
= sizeof(struct iw_statistics
);
292 wrq
.u
.data
.flags
= 1;
294 if(wext_ioctl(ifname
, SIOCGIWSTATS
, &wrq
) >= 0)
296 *buf
= stats
.qual
.qual
;
303 static int wext_get_quality_max(const char *ifname
, int *buf
)
306 struct iw_range range
;
308 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
309 wrq
.u
.data
.length
= sizeof(struct iw_range
);
310 wrq
.u
.data
.flags
= 0;
312 if(wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0)
314 *buf
= range
.max_qual
.qual
;
321 static int wext_get_assoclist(const char *ifname
, char *buf
, int *len
)
327 static int wext_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
330 struct iw_range range
;
331 struct iwinfo_txpwrlist_entry entry
;
334 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
335 wrq
.u
.data
.length
= sizeof(struct iw_range
);
336 wrq
.u
.data
.flags
= 0;
338 if( (wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0) &&
339 (range
.num_txpower
> 0) && (range
.num_txpower
<= IW_MAX_TXPOWER
) &&
340 !(range
.txpower_capa
& IW_TXPOW_RELATIVE
)
342 for( i
= 0; i
< range
.num_txpower
; i
++ )
344 if( range
.txpower_capa
& IW_TXPOW_MWATT
)
346 entry
.dbm
= iwinfo_mw2dbm(range
.txpower
[i
]);
347 entry
.mw
= range
.txpower
[i
];
350 /* Madwifi does neither set mW not dBm caps, also iwlist assumes
351 * dBm if mW is not set, so don't check here... */
352 else /* if( range.txpower_capa & IW_TXPOW_DBM ) */
354 entry
.dbm
= range
.txpower
[i
];
355 entry
.mw
= iwinfo_dbm2mw(range
.txpower
[i
]);
358 memcpy(&buf
[i
*sizeof(entry
)], &entry
, sizeof(entry
));
361 *len
= i
* sizeof(entry
);
368 static int wext_get_freqlist(const char *ifname
, char *buf
, int *len
)
371 struct iw_range range
;
372 struct iwinfo_freqlist_entry entry
;
375 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
376 wrq
.u
.data
.length
= sizeof(struct iw_range
);
377 wrq
.u
.data
.flags
= 0;
379 if(wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0)
383 for(i
= 0; i
< range
.num_frequency
; i
++)
385 entry
.mhz
= wext_freq2mhz(&range
.freq
[i
]);
386 entry
.channel
= range
.freq
[i
].i
;
387 entry
.restricted
= 0;
389 memcpy(&buf
[bl
], &entry
, sizeof(struct iwinfo_freqlist_entry
));
390 bl
+= sizeof(struct iwinfo_freqlist_entry
);
400 static int wext_get_country(const char *ifname
, char *buf
)
406 static int wext_get_countrylist(const char *ifname
, char *buf
, int *len
)
412 static int wext_get_hwmodelist(const char *ifname
, int *buf
)
414 char chans
[IWINFO_BUFSIZE
] = { 0 };
415 struct iwinfo_freqlist_entry
*e
= NULL
;
420 if( !wext_get_freqlist(ifname
, chans
, &len
) )
422 for( e
= (struct iwinfo_freqlist_entry
*)chans
; e
->channel
; e
++ )
424 if( e
->channel
<= 14 )
426 *buf
|= IWINFO_80211_B
;
427 *buf
|= IWINFO_80211_G
;
431 *buf
|= IWINFO_80211_A
;
441 static int wext_get_htmodelist(const char *ifname
, int *buf
)
447 static int wext_get_encryption(const char *ifname
, char *buf
)
449 /* No reliable crypto info in wext */
453 static int wext_get_phyname(const char *ifname
, char *buf
)
455 /* No suitable api in wext */
460 static int wext_get_mbssid_support(const char *ifname
, int *buf
)
462 /* No multi bssid support atm */
466 static char * wext_sysfs_ifname_file(const char *ifname
, const char *path
)
469 static char buf
[128];
472 snprintf(buf
, sizeof(buf
), "/sys/class/net/%s/%s", ifname
, path
);
474 if ((f
= fopen(buf
, "r")) != NULL
)
476 memset(buf
, 0, sizeof(buf
));
478 if (fread(buf
, 1, sizeof(buf
), f
))
487 static int wext_get_hardware_id(const char *ifname
, char *buf
)
490 struct iwinfo_hardware_id
*id
= (struct iwinfo_hardware_id
*)buf
;
492 memset(id
, 0, sizeof(struct iwinfo_hardware_id
));
494 data
= wext_sysfs_ifname_file(ifname
, "device/vendor");
496 id
->vendor_id
= strtoul(data
, NULL
, 16);
498 data
= wext_sysfs_ifname_file(ifname
, "device/device");
500 id
->device_id
= strtoul(data
, NULL
, 16);
502 data
= wext_sysfs_ifname_file(ifname
, "device/subsystem_device");
504 id
->subsystem_device_id
= strtoul(data
, NULL
, 16);
506 data
= wext_sysfs_ifname_file(ifname
, "device/subsystem_vendor");
508 id
->subsystem_vendor_id
= strtoul(data
, NULL
, 16);
510 return (id
->vendor_id
> 0 && id
->device_id
> 0) ? 0 : -1;
513 static int wext_get_hardware_name(const char *ifname
, char *buf
)
515 sprintf(buf
, "Generic WEXT");
519 static int wext_get_txpower_offset(const char *ifname
, int *buf
)
526 static int wext_get_frequency_offset(const char *ifname
, int *buf
)
533 const struct iwinfo_ops wext_ops
= {
536 .channel
= wext_get_channel
,
537 .frequency
= wext_get_frequency
,
538 .frequency_offset
= wext_get_frequency_offset
,
539 .txpower
= wext_get_txpower
,
540 .txpower_offset
= wext_get_txpower_offset
,
541 .bitrate
= wext_get_bitrate
,
542 .signal
= wext_get_signal
,
543 .noise
= wext_get_noise
,
544 .quality
= wext_get_quality
,
545 .quality_max
= wext_get_quality_max
,
546 .mbssid_support
= wext_get_mbssid_support
,
547 .hwmodelist
= wext_get_hwmodelist
,
548 .htmodelist
= wext_get_htmodelist
,
549 .mode
= wext_get_mode
,
550 .ssid
= wext_get_ssid
,
551 .bssid
= wext_get_bssid
,
552 .country
= wext_get_country
,
553 .hardware_id
= wext_get_hardware_id
,
554 .hardware_name
= wext_get_hardware_name
,
555 .encryption
= wext_get_encryption
,
556 .phyname
= wext_get_phyname
,
557 .assoclist
= wext_get_assoclist
,
558 .txpwrlist
= wext_get_txpwrlist
,
559 .scanlist
= wext_get_scanlist
,
560 .freqlist
= wext_get_freqlist
,
561 .countrylist
= wext_get_countrylist
,