iwinfo: detect HT PHY for broadcom-wl
[project/iwinfo.git] / iwinfo_utils.c
1 /*
2  * iwinfo - Wireless Information Library - Shared utility routines
3  *
4  *   Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
5  *
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.
9  *
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.
14  *
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/.
17  *
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.
21  */
22
23 #include "iwinfo/utils.h"
24
25
26 static int ioctl_socket = -1;
27 struct uci_context *uci_ctx = NULL;
28
29 static int iwinfo_ioctl_socket(void)
30 {
31         /* Prepare socket */
32         if (ioctl_socket == -1)
33         {
34                 ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
35                 fcntl(ioctl_socket, F_SETFD, fcntl(ioctl_socket, F_GETFD) | FD_CLOEXEC);
36         }
37
38         return ioctl_socket;
39 }
40
41 int iwinfo_ioctl(int cmd, void *ifr)
42 {
43         int s = iwinfo_ioctl_socket();
44         return ioctl(s, cmd, ifr);
45 }
46
47 int iwinfo_dbm2mw(int in)
48 {
49         double res = 1.0;
50         int ip = in / 10;
51         int fp = in % 10;
52         int k;
53
54         for(k = 0; k < ip; k++) res *= 10;
55         for(k = 0; k < fp; k++) res *= LOG10_MAGIC;
56
57         return (int)res;
58 }
59
60 int iwinfo_mw2dbm(int in)
61 {
62         double fin = (double) in;
63         int res = 0;
64
65         while(fin > 10.0)
66         {
67                 res += 10;
68                 fin /= 10.0;
69         }
70
71         while(fin > 1.000001)
72         {
73                 res += 1;
74                 fin /= LOG10_MAGIC;
75         }
76
77         return (int)res;
78 }
79
80 int iwinfo_ifup(const char *ifname)
81 {
82         struct ifreq ifr;
83
84         strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
85
86         if (iwinfo_ioctl(SIOCGIFFLAGS, &ifr))
87                 return 0;
88
89         ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
90
91         return !iwinfo_ioctl(SIOCSIFFLAGS, &ifr);
92 }
93
94 int iwinfo_ifdown(const char *ifname)
95 {
96         struct ifreq ifr;
97
98         strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
99
100         if (iwinfo_ioctl(SIOCGIFFLAGS, &ifr))
101                 return 0;
102
103         ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
104
105         return !iwinfo_ioctl(SIOCSIFFLAGS, &ifr);
106 }
107
108 int iwinfo_ifmac(const char *ifname)
109 {
110         struct ifreq ifr;
111
112         strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
113
114         if (iwinfo_ioctl(SIOCGIFHWADDR, &ifr))
115                 return 0;
116
117         ifr.ifr_hwaddr.sa_data[0] |= 0x02;
118         ifr.ifr_hwaddr.sa_data[1]++;
119         ifr.ifr_hwaddr.sa_data[2]++;
120
121         return !iwinfo_ioctl(SIOCSIFHWADDR, &ifr);
122 }
123
124 void iwinfo_close(void)
125 {
126         if (ioctl_socket > -1)
127                 close(ioctl_socket);
128
129         ioctl_socket = -1;
130 }
131
132 struct iwinfo_hardware_entry * iwinfo_hardware(struct iwinfo_hardware_id *id)
133 {
134         FILE *db;
135         char buf[256] = { 0 };
136         static struct iwinfo_hardware_entry e;
137         struct iwinfo_hardware_entry *rv = NULL;
138
139         if (!(db = fopen(IWINFO_HARDWARE_FILE, "r")))
140                 return NULL;
141
142         while (fgets(buf, sizeof(buf) - 1, db) != NULL)
143         {
144                 memset(&e, 0, sizeof(e));
145
146                 if (sscanf(buf, "%hx %hx %hx %hx %hd %hd \"%63[^\"]\" \"%63[^\"]\"",
147                                &e.vendor_id, &e.device_id,
148                                &e.subsystem_vendor_id, &e.subsystem_device_id,
149                                &e.txpower_offset, &e.frequency_offset,
150                                e.vendor_name, e.device_name) < 8)
151                         continue;
152
153                 if ((e.vendor_id != 0xffff) && (e.vendor_id != id->vendor_id))
154                         continue;
155
156                 if ((e.device_id != 0xffff) && (e.device_id != id->device_id))
157                         continue;
158
159                 if ((e.subsystem_vendor_id != 0xffff) &&
160                         (e.subsystem_vendor_id != id->subsystem_vendor_id))
161                         continue;
162
163                 if ((e.subsystem_device_id != 0xffff) &&
164                         (e.subsystem_device_id != id->subsystem_device_id))
165                         continue;
166
167                 rv = &e;
168                 break;
169         }
170
171         fclose(db);
172         return rv;
173 }
174
175 int iwinfo_hardware_id_from_mtd(struct iwinfo_hardware_id *id)
176 {
177         FILE *mtd;
178         uint16_t *bc;
179
180         int fd, len, off;
181         char buf[128];
182
183         if (!(mtd = fopen("/proc/mtd", "r")))
184                 return -1;
185
186         while (fgets(buf, sizeof(buf), mtd) > 0)
187         {
188                 if (fscanf(mtd, "mtd%d: %x %*x %127s", &off, &len, buf) < 3 ||
189                     (strcmp(buf, "\"boardconfig\"") && strcmp(buf, "\"EEPROM\"") &&
190                      strcmp(buf, "\"factory\"")))
191                 {
192                         off = -1;
193                         continue;
194                 }
195
196                 break;
197         }
198
199         fclose(mtd);
200
201         if (off < 0)
202                 return -1;
203
204         snprintf(buf, sizeof(buf), "/dev/mtdblock%d", off);
205
206         if ((fd = open(buf, O_RDONLY)) < 0)
207                 return -1;
208
209         bc = mmap(NULL, len, PROT_READ, MAP_PRIVATE|MAP_LOCKED, fd, 0);
210
211         if ((void *)bc != MAP_FAILED)
212         {
213                 id->vendor_id = 0;
214                 id->device_id = 0;
215
216                 for (off = len / 2 - 0x800; off >= 0; off -= 0x800)
217                 {
218                         /* AR531X board data magic */
219                         if ((bc[off] == 0x3533) && (bc[off + 1] == 0x3131))
220                         {
221                                 id->vendor_id = bc[off + 0x7d];
222                                 id->device_id = bc[off + 0x7c];
223                                 id->subsystem_vendor_id = bc[off + 0x84];
224                                 id->subsystem_device_id = bc[off + 0x83];
225                                 break;
226                         }
227
228                         /* AR5416 EEPROM magic */
229                         else if ((bc[off] == 0xA55A) || (bc[off] == 0x5AA5))
230                         {
231                                 id->vendor_id = bc[off + 0x0D];
232                                 id->device_id = bc[off + 0x0E];
233                                 id->subsystem_vendor_id = bc[off + 0x13];
234                                 id->subsystem_device_id = bc[off + 0x14];
235                                 break;
236                         }
237
238                         /* Rt3xxx SoC */
239                         else if ((bc[off] == 0x3352) || (bc[off] == 0x5233) ||
240                                  (bc[off] == 0x3350) || (bc[off] == 0x5033) ||
241                                  (bc[off] == 0x3050) || (bc[off] == 0x5030) ||
242                                  (bc[off] == 0x3052) || (bc[off] == 0x5230))
243                         {
244                                 /* vendor: RaLink */
245                                 id->vendor_id = 0x1814;
246                                 id->subsystem_vendor_id = 0x1814;
247
248                                 /* device */
249                                 if (bc[off] & 0xf0 == 0x30)
250                                         id->device_id = (bc[off] >> 8) | (bc[off] & 0x00ff) << 8;
251                                 else
252                                         id->device_id = bc[off];
253
254                                 /* subsystem from EEPROM_NIC_CONF0_RF_TYPE */
255                                 id->subsystem_device_id = (bc[off + 0x1a] & 0x0f00) >> 8;
256                         }
257                 }
258
259                 munmap(bc, len);
260         }
261
262         close(fd);
263
264         return (id->vendor_id && id->device_id) ? 0 : -1;
265 }
266
267 void iwinfo_parse_rsn(struct iwinfo_crypto_entry *c, uint8_t *data, uint8_t len,
268                                           uint8_t defcipher, uint8_t defauth)
269 {
270         uint16_t i, count;
271
272         static unsigned char ms_oui[3]        = { 0x00, 0x50, 0xf2 };
273         static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
274
275         data += 2;
276         len -= 2;
277
278         if (!memcmp(data, ms_oui, 3))
279                 c->wpa_version += 1;
280         else if (!memcmp(data, ieee80211_oui, 3))
281                 c->wpa_version += 2;
282
283         if (len < 4)
284         {
285                 c->group_ciphers |= defcipher;
286                 c->pair_ciphers  |= defcipher;
287                 c->auth_suites   |= defauth;
288                 return;
289         }
290
291         if (!memcmp(data, ms_oui, 3) || !memcmp(data, ieee80211_oui, 3))
292         {
293                 switch (data[3])
294                 {
295                         case 1: c->group_ciphers |= IWINFO_CIPHER_WEP40;  break;
296                         case 2: c->group_ciphers |= IWINFO_CIPHER_TKIP;   break;
297                         case 4: c->group_ciphers |= IWINFO_CIPHER_CCMP;   break;
298                         case 5: c->group_ciphers |= IWINFO_CIPHER_WEP104; break;
299                         case 6:  /* AES-128-CMAC */ break;
300                         default: /* proprietary */  break;
301                 }
302         }
303
304         data += 4;
305         len -= 4;
306
307         if (len < 2)
308         {
309                 c->pair_ciphers |= defcipher;
310                 c->auth_suites  |= defauth;
311                 return;
312         }
313
314         count = data[0] | (data[1] << 8);
315         if (2 + (count * 4) > len)
316                 return;
317
318         for (i = 0; i < count; i++)
319         {
320                 if (!memcmp(data + 2 + (i * 4), ms_oui, 3) ||
321                         !memcmp(data + 2 + (i * 4), ieee80211_oui, 3))
322                 {
323                         switch (data[2 + (i * 4) + 3])
324                         {
325                                 case 1: c->pair_ciphers |= IWINFO_CIPHER_WEP40;  break;
326                                 case 2: c->pair_ciphers |= IWINFO_CIPHER_TKIP;   break;
327                                 case 4: c->pair_ciphers |= IWINFO_CIPHER_CCMP;   break;
328                                 case 5: c->pair_ciphers |= IWINFO_CIPHER_WEP104; break;
329                                 case 6:  /* AES-128-CMAC */ break;
330                                 default: /* proprietary */  break;
331                         }
332                 }
333         }
334
335         data += 2 + (count * 4);
336         len -= 2 + (count * 4);
337
338         if (len < 2)
339         {
340                 c->auth_suites |= defauth;
341                 return;
342         }
343
344         count = data[0] | (data[1] << 8);
345         if (2 + (count * 4) > len)
346                 return;
347
348         for (i = 0; i < count; i++)
349         {
350                 if (!memcmp(data + 2 + (i * 4), ms_oui, 3) ||
351                         !memcmp(data + 2 + (i * 4), ieee80211_oui, 3))
352                 {
353                         switch (data[2 + (i * 4) + 3])
354                         {
355                                 case 1: c->auth_suites |= IWINFO_KMGMT_8021x; break;
356                                 case 2: c->auth_suites |= IWINFO_KMGMT_PSK;   break;
357                                 case 3:  /* FT/IEEE 802.1X */                 break;
358                                 case 4:  /* FT/PSK */                         break;
359                                 case 5:  /* IEEE 802.1X/SHA-256 */            break;
360                                 case 6:  /* PSK/SHA-256 */                    break;
361                                 default: /* proprietary */                    break;
362                         }
363                 }
364         }
365
366         data += 2 + (count * 4);
367         len -= 2 + (count * 4);
368 }
369
370 struct uci_section *iwinfo_uci_get_radio(const char *name, const char *type)
371 {
372         struct uci_ptr ptr = {
373                 .package = "wireless",
374                 .section = name,
375         };
376         const char *opt;
377
378         if (!uci_ctx) {
379                 uci_ctx = uci_alloc_context();
380                 if (!uci_ctx)
381                         return NULL;
382         }
383
384         memset(&ptr, 0, sizeof(ptr));
385         ptr.package = "wireless";
386         ptr.section = name;
387
388         if (uci_lookup_ptr(uci_ctx, &ptr, NULL, false))
389                 return NULL;
390
391         if (!ptr.s || strcmp(ptr.s->type, "wifi-device") != 0)
392                 return NULL;
393
394         opt = uci_lookup_option_string(uci_ctx, ptr.s, "type");
395         if (!opt || strcmp(opt, type) != 0)
396                 return NULL;
397
398         return ptr.s;
399 }
400
401 void iwinfo_uci_free(void)
402 {
403         if (!uci_ctx)
404                 return;
405
406         uci_free_context(uci_ctx);
407         uci_ctx = NULL;
408 }