iwinfo: add 802.11ac hw mode
[project/rpcd.git] / iwinfo.c
1 /*
2 * rpcd - UBUS RPC server
3 *
4 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/types.h>
20 #include <dirent.h>
21 #include <libubus.h>
22 #include <iwinfo.h>
23 #include <iwinfo/utils.h>
24
25 #include <rpcd/plugin.h>
26
27
28 static struct blob_buf buf;
29 static const struct iwinfo_ops *iw;
30 static const char *ifname;
31
32 enum {
33 RPC_D_DEVICE,
34 __RPC_D_MAX,
35 };
36
37 static const struct blobmsg_policy rpc_device_policy[__RPC_D_MAX] = {
38 [RPC_D_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
39 };
40
41
42 static int
43 rpc_iwinfo_open(struct blob_attr *msg)
44 {
45 static struct blob_attr *tb[__RPC_D_MAX];
46
47 blobmsg_parse(rpc_device_policy, __RPC_D_MAX, tb,
48 blob_data(msg), blob_len(msg));
49
50 if (!tb[RPC_D_DEVICE])
51 return UBUS_STATUS_INVALID_ARGUMENT;
52
53 ifname = blobmsg_data(tb[RPC_D_DEVICE]);
54 iw = iwinfo_backend(ifname);
55
56 return iw ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
57 }
58
59 static void
60 rpc_iwinfo_close(void)
61 {
62 iw = NULL;
63 ifname = NULL;
64 iwinfo_finish();
65 }
66
67 static void
68 rpc_iwinfo_call_int(const char *name, int (*func)(const char *, int *),
69 const char **map)
70 {
71 int rv;
72
73 if (!func(ifname, &rv))
74 {
75 if (!map)
76 blobmsg_add_u32(&buf, name, rv);
77 else
78 blobmsg_add_string(&buf, name, map[rv]);
79 }
80 }
81
82 static void
83 rpc_iwinfo_call_hardware_id(const char *name)
84 {
85 struct iwinfo_hardware_id ids;
86 void *c;
87
88 if (!iw->hardware_id(ifname, (char *)&ids))
89 {
90 c = blobmsg_open_array(&buf, name);
91
92 blobmsg_add_u32(&buf, NULL, ids.vendor_id);
93 blobmsg_add_u32(&buf, NULL, ids.device_id);
94 blobmsg_add_u32(&buf, NULL, ids.subsystem_vendor_id);
95 blobmsg_add_u32(&buf, NULL, ids.subsystem_device_id);
96
97 blobmsg_close_array(&buf, c);
98 }
99 }
100
101 static void
102 rpc_iwinfo_add_encryption(const char *name, struct iwinfo_crypto_entry *e)
103 {
104 int ciph;
105 void *c, *d;
106
107 c = blobmsg_open_table(&buf, name);
108
109 blobmsg_add_u8(&buf, "enabled", e->enabled);
110
111 if (e->enabled)
112 {
113 if (!e->wpa_version)
114 {
115 d = blobmsg_open_array(&buf, "wep");
116
117 if (e->auth_algs & IWINFO_AUTH_OPEN)
118 blobmsg_add_string(&buf, NULL, "open");
119
120 if (e->auth_algs & IWINFO_AUTH_SHARED)
121 blobmsg_add_string(&buf, NULL, "shared");
122
123 blobmsg_close_array(&buf, d);
124 }
125 else
126 {
127 d = blobmsg_open_array(&buf, "wpa");
128
129 if (e->wpa_version > 2)
130 {
131 blobmsg_add_u32(&buf, NULL, 1);
132 blobmsg_add_u32(&buf, NULL, 2);
133 }
134 else
135 {
136 blobmsg_add_u32(&buf, NULL, e->wpa_version);
137 }
138
139 blobmsg_close_array(&buf, d);
140
141
142 d = blobmsg_open_array(&buf, "authentication");
143
144 if (e->auth_suites & IWINFO_KMGMT_PSK)
145 blobmsg_add_string(&buf, NULL, "psk");
146
147 if (e->auth_suites & IWINFO_KMGMT_8021x)
148 blobmsg_add_string(&buf, NULL, "802.1x");
149
150 if (!e->auth_suites ||
151 (e->auth_suites & IWINFO_KMGMT_NONE))
152 blobmsg_add_string(&buf, NULL, "none");
153
154 blobmsg_close_array(&buf, d);
155 }
156
157 d = blobmsg_open_array(&buf, "ciphers");
158 ciph = e->pair_ciphers | e->group_ciphers;
159
160 if (ciph & IWINFO_CIPHER_WEP40)
161 blobmsg_add_string(&buf, NULL, "wep-40");
162
163 if (ciph & IWINFO_CIPHER_WEP104)
164 blobmsg_add_string(&buf, NULL, "wep-104");
165
166 if (ciph & IWINFO_CIPHER_TKIP)
167 blobmsg_add_string(&buf, NULL, "tkip");
168
169 if (ciph & IWINFO_CIPHER_CCMP)
170 blobmsg_add_string(&buf, NULL, "ccmp");
171
172 if (ciph & IWINFO_CIPHER_WRAP)
173 blobmsg_add_string(&buf, NULL, "wrap");
174
175 if (ciph & IWINFO_CIPHER_AESOCB)
176 blobmsg_add_string(&buf, NULL, "aes-ocb");
177
178 if (ciph & IWINFO_CIPHER_CKIP)
179 blobmsg_add_string(&buf, NULL, "ckip");
180
181 if (!ciph || (ciph & IWINFO_CIPHER_NONE))
182 blobmsg_add_string(&buf, NULL, "none");
183
184 blobmsg_close_array(&buf, d);
185 }
186
187 blobmsg_close_table(&buf, c);
188 }
189
190 static void
191 rpc_iwinfo_call_encryption(const char *name)
192 {
193 struct iwinfo_crypto_entry crypto = { 0 };
194
195 if (!iw->encryption(ifname, (char *)&crypto))
196 rpc_iwinfo_add_encryption(name, &crypto);
197 }
198
199 static void
200 rpc_iwinfo_call_hwmodes(const char *name)
201 {
202 int modes;
203 void *c;
204
205 if (!iw->hwmodelist(ifname, &modes))
206 {
207 c = blobmsg_open_array(&buf, name);
208
209 if (modes & IWINFO_80211_AC)
210 blobmsg_add_string(&buf, NULL, "ac");
211
212 if (modes & IWINFO_80211_A)
213 blobmsg_add_string(&buf, NULL, "a");
214
215 if (modes & IWINFO_80211_B)
216 blobmsg_add_string(&buf, NULL, "b");
217
218 if (modes & IWINFO_80211_G)
219 blobmsg_add_string(&buf, NULL, "g");
220
221 if (modes & IWINFO_80211_N)
222 blobmsg_add_string(&buf, NULL, "n");
223
224 blobmsg_close_array(&buf, c);
225 }
226 }
227
228 static void
229 rpc_iwinfo_call_str(const char *name, int (*func)(const char *, char *))
230 {
231 char rv[IWINFO_BUFSIZE] = { 0 };
232
233 if (!func(ifname, rv))
234 blobmsg_add_string(&buf, name, rv);
235 }
236
237 static int
238 rpc_iwinfo_info(struct ubus_context *ctx, struct ubus_object *obj,
239 struct ubus_request_data *req, const char *method,
240 struct blob_attr *msg)
241 {
242 int rv;
243 void *c;
244
245 rv = rpc_iwinfo_open(msg);
246
247 if (rv)
248 return rv;
249
250 blob_buf_init(&buf, 0);
251
252 rpc_iwinfo_call_str("phy", iw->phyname);
253
254 rpc_iwinfo_call_str("ssid", iw->ssid);
255 rpc_iwinfo_call_str("bssid", iw->bssid);
256 rpc_iwinfo_call_str("country", iw->country);
257
258 rpc_iwinfo_call_int("mode", iw->mode, IWINFO_OPMODE_NAMES);
259 rpc_iwinfo_call_int("channel", iw->channel, NULL);
260
261 rpc_iwinfo_call_int("frequency", iw->frequency, NULL);
262 rpc_iwinfo_call_int("frequency_offset", iw->frequency_offset, NULL);
263
264 rpc_iwinfo_call_int("txpower", iw->txpower, NULL);
265 rpc_iwinfo_call_int("txpower_offset", iw->txpower_offset, NULL);
266
267 rpc_iwinfo_call_int("quality", iw->quality, NULL);
268 rpc_iwinfo_call_int("quality_max", iw->quality_max, NULL);
269
270 rpc_iwinfo_call_int("signal", iw->signal, NULL);
271 rpc_iwinfo_call_int("noise", iw->noise, NULL);
272
273 rpc_iwinfo_call_int("bitrate", iw->bitrate, NULL);
274
275 rpc_iwinfo_call_encryption("encryption");
276 rpc_iwinfo_call_hwmodes("hwmodes");
277
278 c = blobmsg_open_table(&buf, "hardware");
279 rpc_iwinfo_call_hardware_id("id");
280 rpc_iwinfo_call_str("name", iw->hardware_name);
281 blobmsg_close_table(&buf, c);
282
283 ubus_send_reply(ctx, req, buf.head);
284
285 rpc_iwinfo_close();
286
287 return UBUS_STATUS_OK;
288 }
289
290 static int
291 rpc_iwinfo_scan(struct ubus_context *ctx, struct ubus_object *obj,
292 struct ubus_request_data *req, const char *method,
293 struct blob_attr *msg)
294 {
295 int i, rv, len;
296 void *c, *d;
297 char mac[18];
298 char res[IWINFO_BUFSIZE];
299 struct iwinfo_scanlist_entry *e;
300
301 rv = rpc_iwinfo_open(msg);
302
303 if (rv)
304 return rv;
305
306 blob_buf_init(&buf, 0);
307
308 c = blobmsg_open_array(&buf, "results");
309
310 if (!iw->scanlist(ifname, res, &len) && (len > 0))
311 {
312 for (i = 0; i < len; i += sizeof(struct iwinfo_scanlist_entry))
313 {
314 e = (struct iwinfo_scanlist_entry *)&res[i];
315 d = blobmsg_open_table(&buf, NULL);
316
317 if (e->ssid[0])
318 blobmsg_add_string(&buf, "ssid", (const char *)e->ssid);
319
320 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
321 e->mac[0], e->mac[1], e->mac[2],
322 e->mac[3], e->mac[4], e->mac[5]);
323
324 blobmsg_add_string(&buf, "bssid", mac);
325
326 blobmsg_add_string(&buf, "mode", IWINFO_OPMODE_NAMES[e->mode]);
327
328 blobmsg_add_u32(&buf, "channel", e->channel);
329 blobmsg_add_u32(&buf, "signal", (uint32_t)(e->signal - 0x100));
330
331 blobmsg_add_u32(&buf, "quality", e->quality);
332 blobmsg_add_u32(&buf, "quality_max", e->quality_max);
333
334 rpc_iwinfo_add_encryption("encryption", &e->crypto);
335
336 blobmsg_close_table(&buf, d);
337 }
338 }
339
340 blobmsg_close_array(&buf, c);
341
342 ubus_send_reply(ctx, req, buf.head);
343
344 rpc_iwinfo_close();
345
346 return UBUS_STATUS_OK;
347 }
348
349 static int
350 rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
351 struct ubus_request_data *req, const char *method,
352 struct blob_attr *msg)
353 {
354 int i, rv, len;
355 char mac[18];
356 char res[IWINFO_BUFSIZE];
357 struct iwinfo_assoclist_entry *a;
358 void *c, *d, *e;
359
360 rv = rpc_iwinfo_open(msg);
361
362 if (rv)
363 return rv;
364
365 blob_buf_init(&buf, 0);
366
367 c = blobmsg_open_array(&buf, "results");
368
369 if (!iw->assoclist(ifname, res, &len) && (len > 0))
370 {
371 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
372 {
373 a = (struct iwinfo_assoclist_entry *)&res[i];
374 d = blobmsg_open_table(&buf, NULL);
375
376 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
377 a->mac[0], a->mac[1], a->mac[2],
378 a->mac[3], a->mac[4], a->mac[5]);
379
380 blobmsg_add_string(&buf, "mac", mac);
381 blobmsg_add_u32(&buf, "signal", a->signal);
382 blobmsg_add_u32(&buf, "noise", a->noise);
383 blobmsg_add_u32(&buf, "inactive", a->inactive);
384
385 e = blobmsg_open_table(&buf, "rx");
386 blobmsg_add_u32(&buf, "rate", a->rx_rate.rate);
387 blobmsg_add_u32(&buf, "mcs", a->rx_rate.mcs);
388 blobmsg_add_u8(&buf, "40mhz", a->rx_rate.is_40mhz);
389 blobmsg_add_u8(&buf, "short_gi", a->rx_rate.is_short_gi);
390 blobmsg_close_table(&buf, e);
391
392 e = blobmsg_open_table(&buf, "tx");
393 blobmsg_add_u32(&buf, "rate", a->tx_rate.rate);
394 blobmsg_add_u32(&buf, "mcs", a->tx_rate.mcs);
395 blobmsg_add_u8(&buf, "40mhz", a->tx_rate.is_40mhz);
396 blobmsg_add_u8(&buf, "short_gi", a->tx_rate.is_short_gi);
397 blobmsg_close_table(&buf, e);
398
399 blobmsg_close_table(&buf, d);
400 }
401 }
402
403 blobmsg_close_array(&buf, c);
404
405 ubus_send_reply(ctx, req, buf.head);
406
407 rpc_iwinfo_close();
408
409 return UBUS_STATUS_OK;
410 }
411
412 static int
413 rpc_iwinfo_freqlist(struct ubus_context *ctx, struct ubus_object *obj,
414 struct ubus_request_data *req, const char *method,
415 struct blob_attr *msg)
416 {
417 int i, rv, len, ch;
418 char res[IWINFO_BUFSIZE];
419 struct iwinfo_freqlist_entry *f;
420 void *c, *d;
421
422 rv = rpc_iwinfo_open(msg);
423
424 if (rv)
425 return rv;
426
427 blob_buf_init(&buf, 0);
428
429 c = blobmsg_open_array(&buf, "results");
430
431 if (!iw->freqlist(ifname, res, &len) && (len > 0))
432 {
433 if (iw->channel(ifname, &ch))
434 ch = -1;
435
436 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
437 {
438 f = (struct iwinfo_freqlist_entry *)&res[i];
439 d = blobmsg_open_table(&buf, NULL);
440
441 blobmsg_add_u32(&buf, "channel", f->channel);
442 blobmsg_add_u32(&buf, "mhz", f->mhz);
443 blobmsg_add_u8(&buf, "restricted", f->restricted);
444
445 if (ch > -1)
446 blobmsg_add_u8(&buf, "active", f->channel == ch);
447
448 blobmsg_close_table(&buf, d);
449 }
450 }
451
452 blobmsg_close_array(&buf, c);
453
454 ubus_send_reply(ctx, req, buf.head);
455
456 rpc_iwinfo_close();
457
458 return UBUS_STATUS_OK;
459 }
460
461 static int
462 rpc_iwinfo_txpowerlist(struct ubus_context *ctx, struct ubus_object *obj,
463 struct ubus_request_data *req, const char *method,
464 struct blob_attr *msg)
465 {
466 int i, rv, len, pwr, off;
467 char res[IWINFO_BUFSIZE];
468 struct iwinfo_txpwrlist_entry *t;
469 void *c, *d;
470
471 rv = rpc_iwinfo_open(msg);
472
473 if (rv)
474 return rv;
475
476 blob_buf_init(&buf, 0);
477
478 c = blobmsg_open_array(&buf, "results");
479
480 if (!iw->txpwrlist(ifname, res, &len) && (len > 0))
481 {
482 if (iw->txpower(ifname, &pwr))
483 pwr = -1;
484
485 if (iw->txpower_offset(ifname, &off))
486 off = 0;
487
488 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
489 {
490 t = (struct iwinfo_txpwrlist_entry *)&res[i];
491 d = blobmsg_open_table(&buf, NULL);
492
493 blobmsg_add_u32(&buf, "dbm", t->dbm + off);
494 blobmsg_add_u32(&buf, "mw", iwinfo_dbm2mw(t->dbm + off));
495
496 if (pwr > -1)
497 blobmsg_add_u8(&buf, "active", t->dbm == pwr);
498
499 blobmsg_close_table(&buf, d);
500 }
501 }
502
503 blobmsg_close_array(&buf, c);
504
505 ubus_send_reply(ctx, req, buf.head);
506
507 rpc_iwinfo_close();
508
509 return UBUS_STATUS_OK;
510 }
511
512 static const char *
513 rpc_iwinfo_lookup_country(char *buf, int len, int iso3166)
514 {
515 int i;
516 static char ccode[5];
517 struct iwinfo_country_entry *c;
518
519 for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
520 {
521 c = (struct iwinfo_country_entry *)&buf[i];
522
523 if (c->iso3166 == iso3166)
524 {
525 snprintf(ccode, sizeof(ccode), "%s", c->ccode);
526 return ccode;
527 }
528 }
529
530 return NULL;
531 }
532
533 static int
534 rpc_iwinfo_countrylist(struct ubus_context *ctx, struct ubus_object *obj,
535 struct ubus_request_data *req, const char *method,
536 struct blob_attr *msg)
537 {
538 int rv, len;
539 char cur[3];
540 char iso3166[3];
541 char res[IWINFO_BUFSIZE];
542 const char *ccode;
543 const struct iwinfo_iso3166_label *l;
544 void *c, *d;
545
546 rv = rpc_iwinfo_open(msg);
547
548 if (rv)
549 return rv;
550
551 blob_buf_init(&buf, 0);
552
553 c = blobmsg_open_array(&buf, "results");
554
555 if (!iw->countrylist(ifname, res, &len) && (len > 0))
556 {
557 if (iw->country(ifname, cur))
558 memset(cur, 0, sizeof(cur));
559
560 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
561 {
562 ccode = rpc_iwinfo_lookup_country(res, len, l->iso3166);
563
564 if (!ccode)
565 continue;
566
567 d = blobmsg_open_table(&buf, NULL);
568
569 blobmsg_add_string(&buf, "code", ccode);
570 blobmsg_add_string(&buf, "country", (const char *)l->name);
571
572 snprintf(iso3166, sizeof(iso3166), "%c%c",
573 (l->iso3166 / 256), (l->iso3166 % 256));
574
575 blobmsg_add_string(&buf, "iso3166", iso3166);
576
577 if (cur[0])
578 blobmsg_add_u8(&buf, "active", !strncmp(ccode, cur, 2));
579
580 blobmsg_close_table(&buf, d);
581 }
582 }
583
584 blobmsg_close_array(&buf, c);
585
586 ubus_send_reply(ctx, req, buf.head);
587
588 rpc_iwinfo_close();
589
590 return UBUS_STATUS_OK;
591 }
592
593 static int
594 rpc_iwinfo_devices(struct ubus_context *ctx, struct ubus_object *obj,
595 struct ubus_request_data *req, const char *method,
596 struct blob_attr *msg)
597 {
598 void *c;
599 struct dirent *e;
600 DIR *d;
601
602 d = opendir("/sys/class/net");
603
604 if (!d)
605 return UBUS_STATUS_UNKNOWN_ERROR;
606
607 blob_buf_init(&buf, 0);
608
609 c = blobmsg_open_array(&buf, "devices");
610
611 while ((e = readdir(d)) != NULL)
612 {
613 if (e->d_type != DT_LNK)
614 continue;
615
616 if (iwinfo_type(e->d_name))
617 blobmsg_add_string(&buf, NULL, e->d_name);
618 }
619
620 blobmsg_close_array(&buf, c);
621
622 closedir(d);
623
624 ubus_send_reply(ctx, req, buf.head);
625
626 rpc_iwinfo_close();
627
628 return UBUS_STATUS_OK;
629 }
630
631
632 static int
633 rpc_iwinfo_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
634 {
635 static const struct ubus_method iwinfo_methods[] = {
636 UBUS_METHOD_NOARG("devices", rpc_iwinfo_devices),
637 UBUS_METHOD("info", rpc_iwinfo_info, rpc_device_policy),
638 UBUS_METHOD("scan", rpc_iwinfo_scan, rpc_device_policy),
639 UBUS_METHOD("assoclist", rpc_iwinfo_assoclist, rpc_device_policy),
640 UBUS_METHOD("freqlist", rpc_iwinfo_freqlist, rpc_device_policy),
641 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist, rpc_device_policy),
642 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist, rpc_device_policy),
643 };
644
645 static struct ubus_object_type iwinfo_type =
646 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods);
647
648 static struct ubus_object obj = {
649 .name = "iwinfo",
650 .type = &iwinfo_type,
651 .methods = iwinfo_methods,
652 .n_methods = ARRAY_SIZE(iwinfo_methods),
653 };
654
655 return ubus_add_object(ctx, &obj);
656 }
657
658 struct rpc_plugin rpc_plugin = {
659 .init = rpc_iwinfo_api_init
660 };