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