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