2 * iwinfo - Wireless Information Library - Shared utility routines
4 * Copyright (C) 2010 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 * The signal handling code is derived from the official madwifi tools,
19 * wlanconfig.c in particular. The encryption property handling was
20 * inspired by the hostapd madwifi driver.
23 #include "iwinfo/utils.h"
26 static int ioctl_socket
= -1;
28 static int iwinfo_ioctl_socket(void)
31 if( ioctl_socket
== -1 )
33 ioctl_socket
= socket(AF_INET
, SOCK_DGRAM
, 0);
34 fcntl(ioctl_socket
, F_SETFD
, fcntl(ioctl_socket
, F_GETFD
) | FD_CLOEXEC
);
40 int iwinfo_ioctl(int cmd
, void *ifr
)
42 int s
= iwinfo_ioctl_socket();
43 return ioctl(s
, cmd
, ifr
);
46 int iwinfo_dbm2mw(int in
)
53 for(k
= 0; k
< ip
; k
++) res
*= 10;
54 for(k
= 0; k
< fp
; k
++) res
*= LOG10_MAGIC
;
59 int iwinfo_mw2dbm(int in
)
61 double fin
= (double) in
;
79 int iwinfo_ifup(const char *ifname
)
83 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
85 if( iwinfo_ioctl(SIOCGIFFLAGS
, &ifr
) )
88 ifr
.ifr_flags
|= (IFF_UP
| IFF_RUNNING
);
90 return !iwinfo_ioctl(SIOCSIFFLAGS
, &ifr
);
93 int iwinfo_ifdown(const char *ifname
)
97 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
99 if( iwinfo_ioctl(SIOCGIFFLAGS
, &ifr
) )
102 ifr
.ifr_flags
&= ~(IFF_UP
| IFF_RUNNING
);
104 return !iwinfo_ioctl(SIOCSIFFLAGS
, &ifr
);
107 int iwinfo_ifmac(const char *ifname
)
111 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
113 if( iwinfo_ioctl(SIOCGIFHWADDR
, &ifr
) )
116 ifr
.ifr_hwaddr
.sa_data
[1]++;
117 ifr
.ifr_hwaddr
.sa_data
[2]++;
119 return !iwinfo_ioctl(SIOCSIFHWADDR
, &ifr
);
122 void iwinfo_close(void)
124 if( ioctl_socket
> -1 )
128 struct iwinfo_hardware_entry
* iwinfo_hardware(struct iwinfo_hardware_id
*id
)
130 const struct iwinfo_hardware_entry
*e
;
132 for (e
= IWINFO_HARDWARE_ENTRIES
; e
->vendor_name
; e
++)
134 if ((e
->vendor_id
!= 0xffff) && (e
->vendor_id
!= id
->vendor_id
))
137 if ((e
->device_id
!= 0xffff) && (e
->device_id
!= id
->device_id
))
140 if ((e
->subsystem_vendor_id
!= 0xffff) &&
141 (e
->subsystem_vendor_id
!= id
->subsystem_vendor_id
))
144 if ((e
->subsystem_device_id
!= 0xffff) &&
145 (e
->subsystem_device_id
!= id
->subsystem_device_id
))
148 return (struct iwinfo_hardware_entry
*)e
;
154 int iwinfo_hardware_id_from_mtd(struct iwinfo_hardware_id
*id
)
162 if (!(mtd
= fopen("/proc/mtd", "r")))
165 while (fgets(buf
, sizeof(buf
), mtd
) > 0)
167 if (fscanf(mtd
, "mtd%d: %*x %x %127s", &off
, &len
, buf
) < 3 ||
168 (strcmp(buf
, "\"boardconfig\"") && strcmp(buf
, "\"EEPROM\"")))
182 snprintf(buf
, sizeof(buf
), "/dev/mtdblock%d", off
);
184 if ((fd
= open(buf
, O_RDONLY
)) < 0)
187 bc
= mmap(NULL
, len
, PROT_READ
, MAP_PRIVATE
|MAP_LOCKED
, fd
, 0);
189 if ((void *)bc
!= MAP_FAILED
)
194 for (off
= len
/ 2 - 0x800; off
>= 0; off
-= 0x800)
196 /* AR531X board data magic */
197 if ((bc
[off
] == 0x3533) && (bc
[off
+ 1] == 0x3131))
199 id
->vendor_id
= bc
[off
+ 0x7d];
200 id
->device_id
= bc
[off
+ 0x7c];
201 id
->subsystem_vendor_id
= bc
[off
+ 0x84];
202 id
->subsystem_device_id
= bc
[off
+ 0x83];
206 /* AR5416 EEPROM magic */
207 else if ((bc
[off
] == 0xA55A) || (bc
[off
] == 0x5AA5))
209 id
->vendor_id
= bc
[off
+ 0x0D];
210 id
->device_id
= bc
[off
+ 0x0E];
211 id
->subsystem_vendor_id
= bc
[off
+ 0x13];
212 id
->subsystem_device_id
= bc
[off
+ 0x14];
222 return (id
->vendor_id
&& id
->device_id
) ? 0 : -1;
225 void iwinfo_parse_rsn(struct iwinfo_crypto_entry
*c
, uint8_t *data
, uint8_t len
,
226 uint8_t defcipher
, uint8_t defauth
)
230 static unsigned char ms_oui
[3] = { 0x00, 0x50, 0xf2 };
231 static unsigned char ieee80211_oui
[3] = { 0x00, 0x0f, 0xac };
236 if (!memcmp(data
, ms_oui
, 3))
238 else if (!memcmp(data
, ieee80211_oui
, 3))
243 c
->group_ciphers
|= defcipher
;
244 c
->pair_ciphers
|= defcipher
;
245 c
->auth_suites
|= defauth
;
249 if (!memcmp(data
, ms_oui
, 3) || !memcmp(data
, ieee80211_oui
, 3))
253 case 1: c
->group_ciphers
|= IWINFO_CIPHER_WEP40
; break;
254 case 2: c
->group_ciphers
|= IWINFO_CIPHER_TKIP
; break;
255 case 4: c
->group_ciphers
|= IWINFO_CIPHER_CCMP
; break;
256 case 5: c
->group_ciphers
|= IWINFO_CIPHER_WEP104
; break;
257 case 6: /* AES-128-CMAC */ break;
258 default: /* proprietary */ break;
267 c
->pair_ciphers
|= defcipher
;
268 c
->auth_suites
|= defauth
;
272 count
= data
[0] | (data
[1] << 8);
273 if (2 + (count
* 4) > len
)
276 for (i
= 0; i
< count
; i
++)
278 if (!memcmp(data
+ 2 + (i
* 4), ms_oui
, 3) ||
279 !memcmp(data
+ 2 + (i
* 4), ieee80211_oui
, 3))
281 switch (data
[2 + (i
* 4) + 3])
283 case 1: c
->pair_ciphers
|= IWINFO_CIPHER_WEP40
; break;
284 case 2: c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
; break;
285 case 4: c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
; break;
286 case 5: c
->pair_ciphers
|= IWINFO_CIPHER_WEP104
; break;
287 case 6: /* AES-128-CMAC */ break;
288 default: /* proprietary */ break;
293 data
+= 2 + (count
* 4);
294 len
-= 2 + (count
* 4);
298 c
->auth_suites
|= defauth
;
302 count
= data
[0] | (data
[1] << 8);
303 if (2 + (count
* 4) > len
)
306 for (i
= 0; i
< count
; i
++)
308 if (!memcmp(data
+ 2 + (i
* 4), ms_oui
, 3) ||
309 !memcmp(data
+ 2 + (i
* 4), ieee80211_oui
, 3))
311 switch (data
[2 + (i
* 4) + 3])
313 case 1: c
->auth_suites
|= IWINFO_KMGMT_8021x
; break;
314 case 2: c
->auth_suites
|= IWINFO_KMGMT_PSK
; break;
315 case 3: /* FT/IEEE 802.1X */ break;
316 case 4: /* FT/PSK */ break;
317 case 5: /* IEEE 802.1X/SHA-256 */ break;
318 case 6: /* PSK/SHA-256 */ break;
319 default: /* proprietary */ break;
324 data
+= 2 + (count
* 4);
325 len
-= 2 + (count
* 4);
This page took 0.056733 seconds and 5 git commands to generate.