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
)
43 return (int)(wext_freq2float(in
) / 1000000);
47 static inline int wext_ioctl(const char *ifname
, int cmd
, struct iwreq
*wrq
)
49 if( !strncmp(ifname
, "mon.", 4) )
50 strncpy(wrq
->ifr_name
, &ifname
[4], IFNAMSIZ
);
52 strncpy(wrq
->ifr_name
, ifname
, IFNAMSIZ
);
54 return iwinfo_ioctl(cmd
, wrq
);
58 static int wext_probe(const char *ifname
)
62 if(wext_ioctl(ifname
, SIOCGIWNAME
, &wrq
) >= 0)
68 static void wext_close(void)
73 static int wext_get_mode(const char *ifname
, int *buf
)
77 if(wext_ioctl(ifname
, SIOCGIWMODE
, &wrq
) >= 0)
82 *buf
= IWINFO_OPMODE_ADHOC
;
86 *buf
= IWINFO_OPMODE_CLIENT
;
90 *buf
= IWINFO_OPMODE_MASTER
;
94 *buf
= IWINFO_OPMODE_MONITOR
;
98 *buf
= IWINFO_OPMODE_UNKNOWN
;
108 static int wext_get_ssid(const char *ifname
, char *buf
)
112 wrq
.u
.essid
.pointer
= (caddr_t
) buf
;
113 wrq
.u
.essid
.length
= IW_ESSID_MAX_SIZE
+ 1;
114 wrq
.u
.essid
.flags
= 0;
116 if(wext_ioctl(ifname
, SIOCGIWESSID
, &wrq
) >= 0)
122 static int wext_get_bssid(const char *ifname
, char *buf
)
126 if(wext_ioctl(ifname
, SIOCGIWAP
, &wrq
) >= 0)
128 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
129 (uint8_t)wrq
.u
.ap_addr
.sa_data
[0], (uint8_t)wrq
.u
.ap_addr
.sa_data
[1],
130 (uint8_t)wrq
.u
.ap_addr
.sa_data
[2], (uint8_t)wrq
.u
.ap_addr
.sa_data
[3],
131 (uint8_t)wrq
.u
.ap_addr
.sa_data
[4], (uint8_t)wrq
.u
.ap_addr
.sa_data
[5]);
139 static int wext_get_bitrate(const char *ifname
, int *buf
)
143 if(wext_ioctl(ifname
, SIOCGIWRATE
, &wrq
) >= 0)
145 *buf
= (wrq
.u
.bitrate
.value
/ 1000);
152 static int wext_get_channel(const char *ifname
, int *buf
)
155 struct iw_range range
;
159 if(wext_ioctl(ifname
, SIOCGIWFREQ
, &wrq
) >= 0)
161 if( wrq
.u
.freq
.m
>= 1000 )
163 freq
= wext_freq2float(&wrq
.u
.freq
);
164 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
165 wrq
.u
.data
.length
= sizeof(struct iw_range
);
166 wrq
.u
.data
.flags
= 0;
168 if(wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0)
170 for(i
= 0; i
< range
.num_frequency
; i
++)
172 if( wext_freq2float(&range
.freq
[i
]) == freq
)
174 *buf
= range
.freq
[i
].i
;
190 static int wext_get_frequency(const char *ifname
, int *buf
)
193 struct iw_range range
;
196 if(wext_ioctl(ifname
, SIOCGIWFREQ
, &wrq
) >= 0)
198 /* We got a channel number instead ... */
199 if( wrq
.u
.freq
.m
< 1000 )
201 channel
= wrq
.u
.freq
.m
;
202 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
203 wrq
.u
.data
.length
= sizeof(struct iw_range
);
204 wrq
.u
.data
.flags
= 0;
206 if(wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0)
208 for(i
= 0; i
< range
.num_frequency
; i
++)
210 if( range
.freq
[i
].i
== channel
)
212 *buf
= wext_freq2mhz(&range
.freq
[i
]);
220 *buf
= wext_freq2mhz(&wrq
.u
.freq
);
228 static int wext_get_txpower(const char *ifname
, int *buf
)
232 wrq
.u
.txpower
.flags
= 0;
234 if(wext_ioctl(ifname
, SIOCGIWTXPOW
, &wrq
) >= 0)
236 if(wrq
.u
.txpower
.flags
& IW_TXPOW_MWATT
)
237 *buf
= iwinfo_mw2dbm(wrq
.u
.txpower
.value
);
239 *buf
= wrq
.u
.txpower
.value
;
247 static int wext_get_signal(const char *ifname
, int *buf
)
250 struct iw_statistics stats
;
252 wrq
.u
.data
.pointer
= (caddr_t
) &stats
;
253 wrq
.u
.data
.length
= sizeof(struct iw_statistics
);
254 wrq
.u
.data
.flags
= 1;
256 if(wext_ioctl(ifname
, SIOCGIWSTATS
, &wrq
) >= 0)
258 *buf
= (stats
.qual
.updated
& IW_QUAL_DBM
)
259 ? (stats
.qual
.level
- 0x100) : stats
.qual
.level
;
267 static int wext_get_noise(const char *ifname
, int *buf
)
270 struct iw_statistics stats
;
272 wrq
.u
.data
.pointer
= (caddr_t
) &stats
;
273 wrq
.u
.data
.length
= sizeof(struct iw_statistics
);
274 wrq
.u
.data
.flags
= 1;
276 if(wext_ioctl(ifname
, SIOCGIWSTATS
, &wrq
) >= 0)
278 *buf
= (stats
.qual
.updated
& IW_QUAL_DBM
)
279 ? (stats
.qual
.noise
- 0x100) : stats
.qual
.noise
;
287 static int wext_get_quality(const char *ifname
, int *buf
)
290 struct iw_statistics stats
;
292 wrq
.u
.data
.pointer
= (caddr_t
) &stats
;
293 wrq
.u
.data
.length
= sizeof(struct iw_statistics
);
294 wrq
.u
.data
.flags
= 1;
296 if(wext_ioctl(ifname
, SIOCGIWSTATS
, &wrq
) >= 0)
298 *buf
= stats
.qual
.qual
;
305 static int wext_get_quality_max(const char *ifname
, int *buf
)
308 struct iw_range range
;
310 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
311 wrq
.u
.data
.length
= sizeof(struct iw_range
);
312 wrq
.u
.data
.flags
= 0;
314 if(wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0)
316 *buf
= range
.max_qual
.qual
;
323 static int wext_get_assoclist(const char *ifname
, char *buf
, int *len
)
329 static int wext_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
332 struct iw_range range
;
333 struct iwinfo_txpwrlist_entry entry
;
336 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
337 wrq
.u
.data
.length
= sizeof(struct iw_range
);
338 wrq
.u
.data
.flags
= 0;
340 if( (wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0) &&
341 (range
.num_txpower
> 0) && (range
.num_txpower
<= IW_MAX_TXPOWER
) &&
342 !(range
.txpower_capa
& IW_TXPOW_RELATIVE
)
344 for( i
= 0; i
< range
.num_txpower
; i
++ )
346 if( range
.txpower_capa
& IW_TXPOW_MWATT
)
348 entry
.dbm
= iwinfo_mw2dbm(range
.txpower
[i
]);
349 entry
.mw
= range
.txpower
[i
];
352 /* Madwifi does neither set mW not dBm caps, also iwlist assumes
353 * dBm if mW is not set, so don't check here... */
354 else /* if( range.txpower_capa & IW_TXPOW_DBM ) */
356 entry
.dbm
= range
.txpower
[i
];
357 entry
.mw
= iwinfo_dbm2mw(range
.txpower
[i
]);
360 memcpy(&buf
[i
*sizeof(entry
)], &entry
, sizeof(entry
));
363 *len
= i
* sizeof(entry
);
370 static int wext_get_freqlist(const char *ifname
, char *buf
, int *len
)
373 struct iw_range range
;
374 struct iwinfo_freqlist_entry entry
;
377 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
378 wrq
.u
.data
.length
= sizeof(struct iw_range
);
379 wrq
.u
.data
.flags
= 0;
381 if(wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0)
385 for(i
= 0; i
< range
.num_frequency
; i
++)
387 entry
.mhz
= wext_freq2mhz(&range
.freq
[i
]);
388 entry
.channel
= range
.freq
[i
].i
;
389 entry
.restricted
= 0;
391 memcpy(&buf
[bl
], &entry
, sizeof(struct iwinfo_freqlist_entry
));
392 bl
+= sizeof(struct iwinfo_freqlist_entry
);
402 static int wext_get_country(const char *ifname
, char *buf
)
408 static int wext_get_countrylist(const char *ifname
, char *buf
, int *len
)
414 static int wext_get_hwmodelist(const char *ifname
, int *buf
)
416 char chans
[IWINFO_BUFSIZE
] = { 0 };
417 struct iwinfo_freqlist_entry
*e
= NULL
;
422 if( !wext_get_freqlist(ifname
, chans
, &len
) )
424 for( e
= (struct iwinfo_freqlist_entry
*)chans
; e
->channel
; e
++ )
426 if( e
->channel
<= 14 )
428 *buf
|= IWINFO_80211_B
;
429 *buf
|= IWINFO_80211_G
;
433 *buf
|= IWINFO_80211_A
;
443 static int wext_get_encryption(const char *ifname
, char *buf
)
445 /* No reliable crypto info in wext */
449 static int wext_get_phyname(const char *ifname
, char *buf
)
451 /* No suitable api in wext */
456 static int wext_get_mbssid_support(const char *ifname
, int *buf
)
458 /* No multi bssid support atm */
462 static char * wext_sysfs_ifname_file(const char *ifname
, const char *path
)
465 static char buf
[128];
468 snprintf(buf
, sizeof(buf
), "/sys/class/net/%s/%s", ifname
, path
);
470 if ((f
= fopen(buf
, "r")) != NULL
)
472 memset(buf
, 0, sizeof(buf
));
474 if (fread(buf
, 1, sizeof(buf
), f
))
483 static int wext_get_hardware_id(const char *ifname
, char *buf
)
486 struct iwinfo_hardware_id
*id
= (struct iwinfo_hardware_id
*)buf
;
488 memset(id
, 0, sizeof(struct iwinfo_hardware_id
));
490 data
= wext_sysfs_ifname_file(ifname
, "device/vendor");
492 id
->vendor_id
= strtoul(data
, NULL
, 16);
494 data
= wext_sysfs_ifname_file(ifname
, "device/device");
496 id
->device_id
= strtoul(data
, NULL
, 16);
498 data
= wext_sysfs_ifname_file(ifname
, "device/subsystem_device");
500 id
->subsystem_device_id
= strtoul(data
, NULL
, 16);
502 data
= wext_sysfs_ifname_file(ifname
, "device/subsystem_vendor");
504 id
->subsystem_vendor_id
= strtoul(data
, NULL
, 16);
506 return (id
->vendor_id
> 0 && id
->device_id
> 0) ? 0 : -1;
509 static int wext_get_hardware_name(const char *ifname
, char *buf
)
511 sprintf(buf
, "Generic WEXT");
515 static int wext_get_txpower_offset(const char *ifname
, int *buf
)
522 static int wext_get_frequency_offset(const char *ifname
, int *buf
)
529 const struct iwinfo_ops wext_ops
= {
532 .channel
= wext_get_channel
,
533 .frequency
= wext_get_frequency
,
534 .frequency_offset
= wext_get_frequency_offset
,
535 .txpower
= wext_get_txpower
,
536 .txpower_offset
= wext_get_txpower_offset
,
537 .bitrate
= wext_get_bitrate
,
538 .signal
= wext_get_signal
,
539 .noise
= wext_get_noise
,
540 .quality
= wext_get_quality
,
541 .quality_max
= wext_get_quality_max
,
542 .mbssid_support
= wext_get_mbssid_support
,
543 .hwmodelist
= wext_get_hwmodelist
,
544 .mode
= wext_get_mode
,
545 .ssid
= wext_get_ssid
,
546 .bssid
= wext_get_bssid
,
547 .country
= wext_get_country
,
548 .hardware_id
= wext_get_hardware_id
,
549 .hardware_name
= wext_get_hardware_name
,
550 .encryption
= wext_get_encryption
,
551 .phyname
= wext_get_phyname
,
552 .assoclist
= wext_get_assoclist
,
553 .txpwrlist
= wext_get_txpwrlist
,
554 .scanlist
= wext_get_scanlist
,
555 .freqlist
= wext_get_freqlist
,
556 .countrylist
= wext_get_countrylist
,