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_center_chan1(const char *ifname
, int *buf
)
194 static int wext_get_center_chan2(const char *ifname
, int *buf
)
200 static int wext_get_frequency(const char *ifname
, int *buf
)
203 struct iw_range range
;
206 if(wext_ioctl(ifname
, SIOCGIWFREQ
, &wrq
) >= 0)
208 /* We got a channel number instead ... */
209 if( wrq
.u
.freq
.m
< 1000 )
211 channel
= wrq
.u
.freq
.m
;
212 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
213 wrq
.u
.data
.length
= sizeof(struct iw_range
);
214 wrq
.u
.data
.flags
= 0;
216 if(wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0)
218 for(i
= 0; i
< range
.num_frequency
; i
++)
220 if( range
.freq
[i
].i
== channel
)
222 *buf
= wext_freq2mhz(&range
.freq
[i
]);
230 *buf
= wext_freq2mhz(&wrq
.u
.freq
);
238 static int wext_get_txpower(const char *ifname
, int *buf
)
242 wrq
.u
.txpower
.flags
= 0;
244 if(wext_ioctl(ifname
, SIOCGIWTXPOW
, &wrq
) >= 0)
246 if(wrq
.u
.txpower
.flags
& IW_TXPOW_MWATT
)
247 *buf
= iwinfo_mw2dbm(wrq
.u
.txpower
.value
);
249 *buf
= wrq
.u
.txpower
.value
;
257 static int wext_get_signal(const char *ifname
, int *buf
)
260 struct iw_statistics stats
;
262 wrq
.u
.data
.pointer
= (caddr_t
) &stats
;
263 wrq
.u
.data
.length
= sizeof(struct iw_statistics
);
264 wrq
.u
.data
.flags
= 1;
266 if(wext_ioctl(ifname
, SIOCGIWSTATS
, &wrq
) >= 0)
268 *buf
= (stats
.qual
.updated
& IW_QUAL_DBM
)
269 ? (stats
.qual
.level
- 0x100) : stats
.qual
.level
;
277 static int wext_get_noise(const char *ifname
, int *buf
)
280 struct iw_statistics stats
;
282 wrq
.u
.data
.pointer
= (caddr_t
) &stats
;
283 wrq
.u
.data
.length
= sizeof(struct iw_statistics
);
284 wrq
.u
.data
.flags
= 1;
286 if(wext_ioctl(ifname
, SIOCGIWSTATS
, &wrq
) >= 0)
288 *buf
= (stats
.qual
.updated
& IW_QUAL_DBM
)
289 ? (stats
.qual
.noise
- 0x100) : stats
.qual
.noise
;
297 static int wext_get_quality(const char *ifname
, int *buf
)
300 struct iw_statistics stats
;
302 wrq
.u
.data
.pointer
= (caddr_t
) &stats
;
303 wrq
.u
.data
.length
= sizeof(struct iw_statistics
);
304 wrq
.u
.data
.flags
= 1;
306 if(wext_ioctl(ifname
, SIOCGIWSTATS
, &wrq
) >= 0)
308 *buf
= stats
.qual
.qual
;
315 static int wext_get_quality_max(const char *ifname
, int *buf
)
318 struct iw_range range
;
320 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
321 wrq
.u
.data
.length
= sizeof(struct iw_range
);
322 wrq
.u
.data
.flags
= 0;
324 if(wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0)
326 *buf
= range
.max_qual
.qual
;
333 static int wext_get_assoclist(const char *ifname
, char *buf
, int *len
)
339 static int wext_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
342 struct iw_range range
;
343 struct iwinfo_txpwrlist_entry entry
;
346 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
347 wrq
.u
.data
.length
= sizeof(struct iw_range
);
348 wrq
.u
.data
.flags
= 0;
350 if( (wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0) &&
351 (range
.num_txpower
> 0) && (range
.num_txpower
<= IW_MAX_TXPOWER
) &&
352 !(range
.txpower_capa
& IW_TXPOW_RELATIVE
)
354 for( i
= 0; i
< range
.num_txpower
; i
++ )
356 if( range
.txpower_capa
& IW_TXPOW_MWATT
)
358 entry
.dbm
= iwinfo_mw2dbm(range
.txpower
[i
]);
359 entry
.mw
= range
.txpower
[i
];
362 /* Madwifi does neither set mW not dBm caps, also iwlist assumes
363 * dBm if mW is not set, so don't check here... */
364 else /* if( range.txpower_capa & IW_TXPOW_DBM ) */
366 entry
.dbm
= range
.txpower
[i
];
367 entry
.mw
= iwinfo_dbm2mw(range
.txpower
[i
]);
370 memcpy(&buf
[i
*sizeof(entry
)], &entry
, sizeof(entry
));
373 *len
= i
* sizeof(entry
);
380 static int wext_get_freqlist(const char *ifname
, char *buf
, int *len
)
383 struct iw_range range
;
384 struct iwinfo_freqlist_entry entry
;
387 wrq
.u
.data
.pointer
= (caddr_t
) &range
;
388 wrq
.u
.data
.length
= sizeof(struct iw_range
);
389 wrq
.u
.data
.flags
= 0;
391 if(wext_ioctl(ifname
, SIOCGIWRANGE
, &wrq
) >= 0)
395 for(i
= 0; i
< range
.num_frequency
; i
++)
397 entry
.mhz
= wext_freq2mhz(&range
.freq
[i
]);
398 entry
.channel
= range
.freq
[i
].i
;
399 entry
.restricted
= 0;
401 memcpy(&buf
[bl
], &entry
, sizeof(struct iwinfo_freqlist_entry
));
402 bl
+= sizeof(struct iwinfo_freqlist_entry
);
412 static int wext_get_country(const char *ifname
, char *buf
)
418 static int wext_get_countrylist(const char *ifname
, char *buf
, int *len
)
424 static int wext_get_hwmodelist(const char *ifname
, int *buf
)
426 char chans
[IWINFO_BUFSIZE
] = { 0 };
427 struct iwinfo_freqlist_entry
*e
= NULL
;
432 if( !wext_get_freqlist(ifname
, chans
, &len
) )
434 for( e
= (struct iwinfo_freqlist_entry
*)chans
; e
->channel
; e
++ )
436 if( e
->channel
<= 14 )
438 *buf
|= IWINFO_80211_B
;
439 *buf
|= IWINFO_80211_G
;
443 *buf
|= IWINFO_80211_A
;
453 static int wext_get_htmodelist(const char *ifname
, int *buf
)
459 static int wext_get_encryption(const char *ifname
, char *buf
)
461 /* No reliable crypto info in wext */
465 static int wext_get_phyname(const char *ifname
, char *buf
)
467 /* No suitable api in wext */
472 static int wext_get_mbssid_support(const char *ifname
, int *buf
)
474 /* No multi bssid support atm */
478 static char * wext_sysfs_ifname_file(const char *ifname
, const char *path
)
481 static char buf
[128];
484 snprintf(buf
, sizeof(buf
), "/sys/class/net/%s/%s", ifname
, path
);
486 if ((f
= fopen(buf
, "r")) != NULL
)
488 memset(buf
, 0, sizeof(buf
));
490 if (fread(buf
, 1, sizeof(buf
), f
))
499 static int wext_get_hardware_id(const char *ifname
, char *buf
)
502 struct iwinfo_hardware_id
*id
= (struct iwinfo_hardware_id
*)buf
;
504 memset(id
, 0, sizeof(struct iwinfo_hardware_id
));
506 data
= wext_sysfs_ifname_file(ifname
, "device/vendor");
508 id
->vendor_id
= strtoul(data
, NULL
, 16);
510 data
= wext_sysfs_ifname_file(ifname
, "device/device");
512 id
->device_id
= strtoul(data
, NULL
, 16);
514 data
= wext_sysfs_ifname_file(ifname
, "device/subsystem_device");
516 id
->subsystem_device_id
= strtoul(data
, NULL
, 16);
518 data
= wext_sysfs_ifname_file(ifname
, "device/subsystem_vendor");
520 id
->subsystem_vendor_id
= strtoul(data
, NULL
, 16);
522 return (id
->vendor_id
> 0 && id
->device_id
> 0) ? 0 : -1;
525 static int wext_get_hardware_name(const char *ifname
, char *buf
)
527 sprintf(buf
, "Generic WEXT");
531 static int wext_get_txpower_offset(const char *ifname
, int *buf
)
538 static int wext_get_frequency_offset(const char *ifname
, int *buf
)
545 const struct iwinfo_ops wext_ops
= {
548 .channel
= wext_get_channel
,
549 .center_chan1
= wext_get_center_chan1
,
550 .center_chan2
= wext_get_center_chan2
,
551 .frequency
= wext_get_frequency
,
552 .frequency_offset
= wext_get_frequency_offset
,
553 .txpower
= wext_get_txpower
,
554 .txpower_offset
= wext_get_txpower_offset
,
555 .bitrate
= wext_get_bitrate
,
556 .signal
= wext_get_signal
,
557 .noise
= wext_get_noise
,
558 .quality
= wext_get_quality
,
559 .quality_max
= wext_get_quality_max
,
560 .mbssid_support
= wext_get_mbssid_support
,
561 .hwmodelist
= wext_get_hwmodelist
,
562 .htmodelist
= wext_get_htmodelist
,
563 .mode
= wext_get_mode
,
564 .ssid
= wext_get_ssid
,
565 .bssid
= wext_get_bssid
,
566 .country
= wext_get_country
,
567 .hardware_id
= wext_get_hardware_id
,
568 .hardware_name
= wext_get_hardware_name
,
569 .encryption
= wext_get_encryption
,
570 .phyname
= wext_get_phyname
,
571 .assoclist
= wext_get_assoclist
,
572 .txpwrlist
= wext_get_txpwrlist
,
573 .scanlist
= wext_get_scanlist
,
574 .freqlist
= wext_get_freqlist
,
575 .countrylist
= wext_get_countrylist
,