Extend copyright
[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_A)
210 blobmsg_add_string(&buf, NULL, "a");
211
212 if (modes & IWINFO_80211_B)
213 blobmsg_add_string(&buf, NULL, "b");
214
215 if (modes & IWINFO_80211_G)
216 blobmsg_add_string(&buf, NULL, "g");
217
218 if (modes & IWINFO_80211_N)
219 blobmsg_add_string(&buf, NULL, "n");
220
221 blobmsg_close_array(&buf, c);
222 }
223 }
224
225 static void
226 rpc_iwinfo_call_str(const char *name, int (*func)(const char *, char *))
227 {
228 char rv[IWINFO_BUFSIZE] = { 0 };
229
230 if (!func(ifname, rv))
231 blobmsg_add_string(&buf, name, rv);
232 }
233
234 static int
235 rpc_iwinfo_info(struct ubus_context *ctx, struct ubus_object *obj,
236 struct ubus_request_data *req, const char *method,
237 struct blob_attr *msg)
238 {
239 int rv;
240 void *c;
241
242 rv = rpc_iwinfo_open(msg);
243
244 if (rv)
245 return rv;
246
247 blob_buf_init(&buf, 0);
248
249 rpc_iwinfo_call_str("phy", iw->phyname);
250
251 rpc_iwinfo_call_str("ssid", iw->ssid);
252 rpc_iwinfo_call_str("bssid", iw->bssid);
253 rpc_iwinfo_call_str("country", iw->country);
254
255 rpc_iwinfo_call_int("mode", iw->mode, IWINFO_OPMODE_NAMES);
256 rpc_iwinfo_call_int("channel", iw->channel, NULL);
257
258 rpc_iwinfo_call_int("frequency", iw->frequency, NULL);
259 rpc_iwinfo_call_int("frequency_offset", iw->frequency_offset, NULL);
260
261 rpc_iwinfo_call_int("txpower", iw->txpower, NULL);
262 rpc_iwinfo_call_int("txpower_offset", iw->txpower_offset, NULL);
263
264 rpc_iwinfo_call_int("quality", iw->quality, NULL);
265 rpc_iwinfo_call_int("quality_max", iw->quality_max, NULL);
266
267 rpc_iwinfo_call_int("signal", iw->signal, NULL);
268 rpc_iwinfo_call_int("noise", iw->noise, NULL);
269
270 rpc_iwinfo_call_int("bitrate", iw->bitrate, NULL);
271
272 rpc_iwinfo_call_encryption("encryption");
273 rpc_iwinfo_call_hwmodes("hwmodes");
274
275 c = blobmsg_open_table(&buf, "hardware");
276 rpc_iwinfo_call_hardware_id("id");
277 rpc_iwinfo_call_str("name", iw->hardware_name);
278 blobmsg_close_table(&buf, c);
279
280 ubus_send_reply(ctx, req, buf.head);
281
282 rpc_iwinfo_close();
283
284 return UBUS_STATUS_OK;
285 }
286
287 static int
288 rpc_iwinfo_scan(struct ubus_context *ctx, struct ubus_object *obj,
289 struct ubus_request_data *req, const char *method,
290 struct blob_attr *msg)
291 {
292 int i, rv, len;
293 void *c, *d;
294 char mac[18];
295 char res[IWINFO_BUFSIZE];
296 struct iwinfo_scanlist_entry *e;
297
298 rv = rpc_iwinfo_open(msg);
299
300 if (rv)
301 return rv;
302
303 blob_buf_init(&buf, 0);
304
305 c = blobmsg_open_array(&buf, "results");
306
307 if (!iw->scanlist(ifname, res, &len) && (len > 0))
308 {
309 for (i = 0; i < len; i += sizeof(struct iwinfo_scanlist_entry))
310 {
311 e = (struct iwinfo_scanlist_entry *)&res[i];
312 d = blobmsg_open_table(&buf, NULL);
313
314 if (e->ssid[0])
315 blobmsg_add_string(&buf, "ssid", (const char *)e->ssid);
316
317 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
318 e->mac[0], e->mac[1], e->mac[2],
319 e->mac[3], e->mac[4], e->mac[5]);
320
321 blobmsg_add_string(&buf, "bssid", mac);
322
323 blobmsg_add_string(&buf, "mode", IWINFO_OPMODE_NAMES[e->mode]);
324
325 blobmsg_add_u32(&buf, "channel", e->channel);
326 blobmsg_add_u32(&buf, "signal", (uint32_t)(e->signal - 0x100));
327
328 blobmsg_add_u32(&buf, "quality", e->quality);
329 blobmsg_add_u32(&buf, "quality_max", e->quality_max);
330
331 rpc_iwinfo_add_encryption("encryption", &e->crypto);
332
333 blobmsg_close_table(&buf, d);
334 }
335 }
336
337 blobmsg_close_array(&buf, c);
338
339 ubus_send_reply(ctx, req, buf.head);
340
341 rpc_iwinfo_close();
342
343 return UBUS_STATUS_OK;
344 }
345
346 static int
347 rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
348 struct ubus_request_data *req, const char *method,
349 struct blob_attr *msg)
350 {
351 int i, rv, len;
352 char mac[18];
353 char res[IWINFO_BUFSIZE];
354 struct iwinfo_assoclist_entry *a;
355 void *c, *d, *e;
356
357 rv = rpc_iwinfo_open(msg);
358
359 if (rv)
360 return rv;
361
362 blob_buf_init(&buf, 0);
363
364 c = blobmsg_open_array(&buf, "results");
365
366 if (!iw->assoclist(ifname, res, &len) && (len > 0))
367 {
368 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
369 {
370 a = (struct iwinfo_assoclist_entry *)&res[i];
371 d = blobmsg_open_table(&buf, NULL);
372
373 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
374 a->mac[0], a->mac[1], a->mac[2],
375 a->mac[3], a->mac[4], a->mac[5]);
376
377 blobmsg_add_string(&buf, "mac", mac);
378 blobmsg_add_u32(&buf, "signal", a->signal);
379 blobmsg_add_u32(&buf, "noise", a->noise);
380 blobmsg_add_u32(&buf, "inactive", a->inactive);
381
382 e = blobmsg_open_table(&buf, "rx");
383 blobmsg_add_u32(&buf, "rate", a->rx_rate.rate);
384 blobmsg_add_u32(&buf, "mcs", a->rx_rate.mcs);
385 blobmsg_add_u8(&buf, "40mhz", a->rx_rate.is_40mhz);
386 blobmsg_add_u8(&buf, "short_gi", a->rx_rate.is_short_gi);
387 blobmsg_close_table(&buf, e);
388
389 e = blobmsg_open_table(&buf, "tx");
390 blobmsg_add_u32(&buf, "rate", a->tx_rate.rate);
391 blobmsg_add_u32(&buf, "mcs", a->tx_rate.mcs);
392 blobmsg_add_u8(&buf, "40mhz", a->tx_rate.is_40mhz);
393 blobmsg_add_u8(&buf, "short_gi", a->tx_rate.is_short_gi);
394 blobmsg_close_table(&buf, e);
395
396 blobmsg_close_table(&buf, d);
397 }
398 }
399
400 blobmsg_close_array(&buf, c);
401
402 ubus_send_reply(ctx, req, buf.head);
403
404 rpc_iwinfo_close();
405
406 return UBUS_STATUS_OK;
407 }
408
409 static int
410 rpc_iwinfo_freqlist(struct ubus_context *ctx, struct ubus_object *obj,
411 struct ubus_request_data *req, const char *method,
412 struct blob_attr *msg)
413 {
414 int i, rv, len, ch;
415 char res[IWINFO_BUFSIZE];
416 struct iwinfo_freqlist_entry *f;
417 void *c, *d;
418
419 rv = rpc_iwinfo_open(msg);
420
421 if (rv)
422 return rv;
423
424 blob_buf_init(&buf, 0);
425
426 c = blobmsg_open_array(&buf, "results");
427
428 if (!iw->freqlist(ifname, res, &len) && (len > 0))
429 {
430 if (iw->channel(ifname, &ch))
431 ch = -1;
432
433 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
434 {
435 f = (struct iwinfo_freqlist_entry *)&res[i];
436 d = blobmsg_open_table(&buf, NULL);
437
438 blobmsg_add_u32(&buf, "channel", f->channel);
439 blobmsg_add_u32(&buf, "mhz", f->mhz);
440 blobmsg_add_u8(&buf, "restricted", f->restricted);
441
442 if (ch > -1)
443 blobmsg_add_u8(&buf, "active", f->channel == ch);
444
445 blobmsg_close_table(&buf, d);
446 }
447 }
448
449 blobmsg_close_array(&buf, c);
450
451 ubus_send_reply(ctx, req, buf.head);
452
453 rpc_iwinfo_close();
454
455 return UBUS_STATUS_OK;
456 }
457
458 static int
459 rpc_iwinfo_txpowerlist(struct ubus_context *ctx, struct ubus_object *obj,
460 struct ubus_request_data *req, const char *method,
461 struct blob_attr *msg)
462 {
463 int i, rv, len, pwr, off;
464 char res[IWINFO_BUFSIZE];
465 struct iwinfo_txpwrlist_entry *t;
466 void *c, *d;
467
468 rv = rpc_iwinfo_open(msg);
469
470 if (rv)
471 return rv;
472
473 blob_buf_init(&buf, 0);
474
475 c = blobmsg_open_array(&buf, "results");
476
477 if (!iw->txpwrlist(ifname, res, &len) && (len > 0))
478 {
479 if (iw->txpower(ifname, &pwr))
480 pwr = -1;
481
482 if (iw->txpower_offset(ifname, &off))
483 off = 0;
484
485 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
486 {
487 t = (struct iwinfo_txpwrlist_entry *)&res[i];
488 d = blobmsg_open_table(&buf, NULL);
489
490 blobmsg_add_u32(&buf, "dbm", t->dbm + off);
491 blobmsg_add_u32(&buf, "mw", iwinfo_dbm2mw(t->dbm + off));
492
493 if (pwr > -1)
494 blobmsg_add_u8(&buf, "active", t->dbm == pwr);
495
496 blobmsg_close_table(&buf, d);
497 }
498 }
499
500 blobmsg_close_array(&buf, c);
501
502 ubus_send_reply(ctx, req, buf.head);
503
504 rpc_iwinfo_close();
505
506 return UBUS_STATUS_OK;
507 }
508
509 static const char *
510 rpc_iwinfo_lookup_country(char *buf, int len, int iso3166)
511 {
512 int i;
513 static char ccode[5];
514 struct iwinfo_country_entry *c;
515
516 for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
517 {
518 c = (struct iwinfo_country_entry *)&buf[i];
519
520 if (c->iso3166 == iso3166)
521 {
522 snprintf(ccode, sizeof(ccode), "%s", c->ccode);
523 return ccode;
524 }
525 }
526
527 return NULL;
528 }
529
530 static int
531 rpc_iwinfo_countrylist(struct ubus_context *ctx, struct ubus_object *obj,
532 struct ubus_request_data *req, const char *method,
533 struct blob_attr *msg)
534 {
535 int rv, len;
536 char cur[3];
537 char iso3166[3];
538 char res[IWINFO_BUFSIZE];
539 const char *ccode;
540 const struct iwinfo_iso3166_label *l;
541 void *c, *d;
542
543 rv = rpc_iwinfo_open(msg);
544
545 if (rv)
546 return rv;
547
548 blob_buf_init(&buf, 0);
549
550 c = blobmsg_open_array(&buf, "results");
551
552 if (!iw->countrylist(ifname, res, &len) && (len > 0))
553 {
554 if (iw->country(ifname, cur))
555 memset(cur, 0, sizeof(cur));
556
557 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
558 {
559 ccode = rpc_iwinfo_lookup_country(res, len, l->iso3166);
560
561 if (!ccode)
562 continue;
563
564 d = blobmsg_open_table(&buf, NULL);
565
566 blobmsg_add_string(&buf, "code", ccode);
567 blobmsg_add_string(&buf, "country", (const char *)l->name);
568
569 snprintf(iso3166, sizeof(iso3166), "%c%c",
570 (l->iso3166 / 256), (l->iso3166 % 256));
571
572 blobmsg_add_string(&buf, "iso3166", iso3166);
573
574 if (cur[0])
575 blobmsg_add_u8(&buf, "active", !strncmp(ccode, cur, 2));
576
577 blobmsg_close_table(&buf, d);
578 }
579 }
580
581 blobmsg_close_array(&buf, c);
582
583 ubus_send_reply(ctx, req, buf.head);
584
585 rpc_iwinfo_close();
586
587 return UBUS_STATUS_OK;
588 }
589
590 static int
591 rpc_iwinfo_devices(struct ubus_context *ctx, struct ubus_object *obj,
592 struct ubus_request_data *req, const char *method,
593 struct blob_attr *msg)
594 {
595 void *c;
596 struct dirent *e;
597 DIR *d;
598
599 d = opendir("/sys/class/net");
600
601 if (!d)
602 return UBUS_STATUS_UNKNOWN_ERROR;
603
604 blob_buf_init(&buf, 0);
605
606 c = blobmsg_open_array(&buf, "devices");
607
608 while ((e = readdir(d)) != NULL)
609 {
610 if (e->d_type != DT_LNK)
611 continue;
612
613 if (iwinfo_type(e->d_name))
614 blobmsg_add_string(&buf, NULL, e->d_name);
615 }
616
617 blobmsg_close_array(&buf, c);
618
619 closedir(d);
620
621 ubus_send_reply(ctx, req, buf.head);
622
623 rpc_iwinfo_close();
624
625 return UBUS_STATUS_OK;
626 }
627
628
629 static int
630 rpc_iwinfo_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
631 {
632 static const struct ubus_method iwinfo_methods[] = {
633 { .name = "devices", .handler = rpc_iwinfo_devices },
634 UBUS_METHOD("info", rpc_iwinfo_info, rpc_device_policy),
635 UBUS_METHOD("scan", rpc_iwinfo_scan, rpc_device_policy),
636 UBUS_METHOD("assoclist", rpc_iwinfo_assoclist, rpc_device_policy),
637 UBUS_METHOD("freqlist", rpc_iwinfo_freqlist, rpc_device_policy),
638 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist, rpc_device_policy),
639 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist, rpc_device_policy),
640 };
641
642 static struct ubus_object_type iwinfo_type =
643 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods);
644
645 static struct ubus_object obj = {
646 .name = "iwinfo",
647 .type = &iwinfo_type,
648 .methods = iwinfo_methods,
649 .n_methods = ARRAY_SIZE(iwinfo_methods),
650 };
651
652 return ubus_add_object(ctx, &obj);
653 }
654
655 const struct rpc_plugin rpc_plugin = {
656 .init = rpc_iwinfo_api_init
657 };