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