uci: manually clear uci_ptr flags after uci_delete() operations
[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 enum {
58 RPC_U_SECTION,
59 __RPC_U_MAX
60 };
61
62 static const struct blobmsg_policy rpc_uci_policy[__RPC_U_MAX] = {
63 [RPC_U_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
64 };
65
66 static int
67 __rpc_iwinfo_open(struct blob_attr *device)
68 {
69 if (!device)
70 return UBUS_STATUS_INVALID_ARGUMENT;
71
72 ifname = blobmsg_data(device);
73 iw = iwinfo_backend(ifname);
74
75 return iw ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
76 }
77
78 static int
79 rpc_iwinfo_open(struct blob_attr *msg)
80 {
81 static struct blob_attr *tb[__RPC_D_MAX];
82
83 blobmsg_parse(rpc_device_policy, __RPC_D_MAX, tb,
84 blob_data(msg), blob_len(msg));
85
86 return __rpc_iwinfo_open(tb[RPC_D_DEVICE]);
87 }
88
89 static void
90 rpc_iwinfo_close(void)
91 {
92 iw = NULL;
93 ifname = NULL;
94 iwinfo_finish();
95 }
96
97 static void
98 rpc_iwinfo_call_int(const char *name, int (*func)(const char *, int *),
99 const char **map)
100 {
101 int rv;
102
103 if (!func(ifname, &rv))
104 {
105 if (!map)
106 blobmsg_add_u32(&buf, name, rv);
107 else
108 blobmsg_add_string(&buf, name, map[rv]);
109 }
110 }
111
112 static void
113 rpc_iwinfo_call_hardware_id(const char *name)
114 {
115 struct iwinfo_hardware_id ids;
116 void *c;
117
118 if (!iw->hardware_id(ifname, (char *)&ids))
119 {
120 c = blobmsg_open_array(&buf, name);
121
122 blobmsg_add_u32(&buf, NULL, ids.vendor_id);
123 blobmsg_add_u32(&buf, NULL, ids.device_id);
124 blobmsg_add_u32(&buf, NULL, ids.subsystem_vendor_id);
125 blobmsg_add_u32(&buf, NULL, ids.subsystem_device_id);
126
127 blobmsg_close_array(&buf, c);
128 }
129 }
130
131 static void
132 rpc_iwinfo_add_encryption(const char *name, struct iwinfo_crypto_entry *e)
133 {
134 int ciph, wpa_version;
135 void *c, *d;
136
137 c = blobmsg_open_table(&buf, name);
138
139 blobmsg_add_u8(&buf, "enabled", e->enabled);
140
141 if (e->enabled)
142 {
143 if (!e->wpa_version)
144 {
145 d = blobmsg_open_array(&buf, "wep");
146
147 if (e->auth_algs & IWINFO_AUTH_OPEN)
148 blobmsg_add_string(&buf, NULL, "open");
149
150 if (e->auth_algs & IWINFO_AUTH_SHARED)
151 blobmsg_add_string(&buf, NULL, "shared");
152
153 blobmsg_close_array(&buf, d);
154 }
155 else
156 {
157 d = blobmsg_open_array(&buf, "wpa");
158
159 for (wpa_version = 1; wpa_version <= 3; wpa_version++)
160 if (e->wpa_version & (1 << (wpa_version - 1)))
161 blobmsg_add_u32(&buf, NULL, wpa_version);
162
163 blobmsg_close_array(&buf, d);
164
165
166 d = blobmsg_open_array(&buf, "authentication");
167
168 if (e->auth_suites & IWINFO_KMGMT_PSK)
169 blobmsg_add_string(&buf, NULL, "psk");
170
171 if (e->auth_suites & IWINFO_KMGMT_8021x)
172 blobmsg_add_string(&buf, NULL, "802.1x");
173
174 if (e->auth_suites & IWINFO_KMGMT_SAE)
175 blobmsg_add_string(&buf, NULL, "sae");
176
177 if (e->auth_suites & IWINFO_KMGMT_OWE)
178 blobmsg_add_string(&buf, NULL, "owe");
179
180 if (!e->auth_suites ||
181 (e->auth_suites & IWINFO_KMGMT_NONE))
182 blobmsg_add_string(&buf, NULL, "none");
183
184 blobmsg_close_array(&buf, d);
185 }
186
187 d = blobmsg_open_array(&buf, "ciphers");
188 ciph = e->pair_ciphers | e->group_ciphers;
189
190 if (ciph & IWINFO_CIPHER_WEP40)
191 blobmsg_add_string(&buf, NULL, "wep-40");
192
193 if (ciph & IWINFO_CIPHER_WEP104)
194 blobmsg_add_string(&buf, NULL, "wep-104");
195
196 if (ciph & IWINFO_CIPHER_TKIP)
197 blobmsg_add_string(&buf, NULL, "tkip");
198
199 if (ciph & IWINFO_CIPHER_CCMP)
200 blobmsg_add_string(&buf, NULL, "ccmp");
201
202 if (ciph & IWINFO_CIPHER_WRAP)
203 blobmsg_add_string(&buf, NULL, "wrap");
204
205 if (ciph & IWINFO_CIPHER_AESOCB)
206 blobmsg_add_string(&buf, NULL, "aes-ocb");
207
208 if (ciph & IWINFO_CIPHER_CKIP)
209 blobmsg_add_string(&buf, NULL, "ckip");
210
211 if (!ciph || (ciph & IWINFO_CIPHER_NONE))
212 blobmsg_add_string(&buf, NULL, "none");
213
214 blobmsg_close_array(&buf, d);
215 }
216
217 blobmsg_close_table(&buf, c);
218 }
219
220 static void
221 rpc_iwinfo_call_encryption(const char *name)
222 {
223 struct iwinfo_crypto_entry crypto = { 0 };
224
225 if (!iw->encryption(ifname, (char *)&crypto))
226 rpc_iwinfo_add_encryption(name, &crypto);
227 }
228
229 static void
230 rpc_iwinfo_call_htmodes(const char *name)
231 {
232 int modes;
233 void *c;
234
235 if (!iw->htmodelist(ifname, &modes))
236 {
237 c = blobmsg_open_array(&buf, name);
238
239 if (modes & IWINFO_HTMODE_HT20)
240 blobmsg_add_string(&buf, NULL, "HT20");
241
242 if (modes & IWINFO_HTMODE_HT40)
243 blobmsg_add_string(&buf, NULL, "HT40");
244
245 if (modes & IWINFO_HTMODE_VHT20)
246 blobmsg_add_string(&buf, NULL, "VHT20");
247
248 if (modes & IWINFO_HTMODE_VHT40)
249 blobmsg_add_string(&buf, NULL, "VHT40");
250
251 if (modes & IWINFO_HTMODE_VHT80)
252 blobmsg_add_string(&buf, NULL, "VHT80");
253
254 if (modes & IWINFO_HTMODE_VHT80_80)
255 blobmsg_add_string(&buf, NULL, "VHT80+80");
256
257 if (modes & IWINFO_HTMODE_VHT160)
258 blobmsg_add_string(&buf, NULL, "VHT160");
259
260 blobmsg_close_array(&buf, c);
261 }
262 }
263
264 static void
265 rpc_iwinfo_call_hwmodes(const char *name)
266 {
267 int modes;
268 void *c;
269
270 if (!iw->hwmodelist(ifname, &modes))
271 {
272 c = blobmsg_open_array(&buf, name);
273
274 if (modes & IWINFO_80211_AC)
275 blobmsg_add_string(&buf, NULL, "ac");
276
277 if (modes & IWINFO_80211_A)
278 blobmsg_add_string(&buf, NULL, "a");
279
280 if (modes & IWINFO_80211_B)
281 blobmsg_add_string(&buf, NULL, "b");
282
283 if (modes & IWINFO_80211_G)
284 blobmsg_add_string(&buf, NULL, "g");
285
286 if (modes & IWINFO_80211_N)
287 blobmsg_add_string(&buf, NULL, "n");
288
289 blobmsg_close_array(&buf, c);
290 }
291 }
292
293 static void
294 rpc_iwinfo_call_str(const char *name, int (*func)(const char *, char *))
295 {
296 char rv[IWINFO_BUFSIZE] = { 0 };
297
298 if (!func(ifname, rv))
299 blobmsg_add_string(&buf, name, rv);
300 }
301
302 static int
303 rpc_iwinfo_info(struct ubus_context *ctx, struct ubus_object *obj,
304 struct ubus_request_data *req, const char *method,
305 struct blob_attr *msg)
306 {
307 int rv;
308 void *c;
309
310 rv = rpc_iwinfo_open(msg);
311
312 if (rv)
313 return rv;
314
315 blob_buf_init(&buf, 0);
316
317 rpc_iwinfo_call_str("phy", iw->phyname);
318
319 rpc_iwinfo_call_str("ssid", iw->ssid);
320 rpc_iwinfo_call_str("bssid", iw->bssid);
321 rpc_iwinfo_call_str("country", iw->country);
322
323 rpc_iwinfo_call_int("mode", iw->mode, IWINFO_OPMODE_NAMES);
324 rpc_iwinfo_call_int("channel", iw->channel, NULL);
325
326 rpc_iwinfo_call_int("frequency", iw->frequency, NULL);
327 rpc_iwinfo_call_int("frequency_offset", iw->frequency_offset, NULL);
328
329 rpc_iwinfo_call_int("txpower", iw->txpower, NULL);
330 rpc_iwinfo_call_int("txpower_offset", iw->txpower_offset, NULL);
331
332 rpc_iwinfo_call_int("quality", iw->quality, NULL);
333 rpc_iwinfo_call_int("quality_max", iw->quality_max, NULL);
334
335 rpc_iwinfo_call_int("signal", iw->signal, NULL);
336 rpc_iwinfo_call_int("noise", iw->noise, NULL);
337
338 rpc_iwinfo_call_int("bitrate", iw->bitrate, NULL);
339
340 rpc_iwinfo_call_encryption("encryption");
341 rpc_iwinfo_call_htmodes("htmodes");
342 rpc_iwinfo_call_hwmodes("hwmodes");
343
344 c = blobmsg_open_table(&buf, "hardware");
345 rpc_iwinfo_call_hardware_id("id");
346 rpc_iwinfo_call_str("name", iw->hardware_name);
347 blobmsg_close_table(&buf, c);
348
349 ubus_send_reply(ctx, req, buf.head);
350
351 rpc_iwinfo_close();
352
353 return UBUS_STATUS_OK;
354 }
355
356 static int
357 rpc_iwinfo_scan(struct ubus_context *ctx, struct ubus_object *obj,
358 struct ubus_request_data *req, const char *method,
359 struct blob_attr *msg)
360 {
361 int i, rv, len;
362 void *c, *d;
363 char mac[18];
364 char res[IWINFO_BUFSIZE];
365 struct iwinfo_scanlist_entry *e;
366
367 rv = rpc_iwinfo_open(msg);
368
369 if (rv)
370 return rv;
371
372 blob_buf_init(&buf, 0);
373
374 c = blobmsg_open_array(&buf, "results");
375
376 if (!iw->scanlist(ifname, res, &len) && (len > 0))
377 {
378 for (i = 0; i < len; i += sizeof(struct iwinfo_scanlist_entry))
379 {
380 e = (struct iwinfo_scanlist_entry *)&res[i];
381 d = blobmsg_open_table(&buf, NULL);
382
383 if (e->ssid[0])
384 blobmsg_add_string(&buf, "ssid", (const char *)e->ssid);
385
386 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
387 e->mac[0], e->mac[1], e->mac[2],
388 e->mac[3], e->mac[4], e->mac[5]);
389
390 blobmsg_add_string(&buf, "bssid", mac);
391
392 blobmsg_add_string(&buf, "mode", IWINFO_OPMODE_NAMES[e->mode]);
393
394 blobmsg_add_u32(&buf, "channel", e->channel);
395 blobmsg_add_u32(&buf, "signal", (uint32_t)(e->signal - 0x100));
396
397 blobmsg_add_u32(&buf, "quality", e->quality);
398 blobmsg_add_u32(&buf, "quality_max", e->quality_max);
399
400 rpc_iwinfo_add_encryption("encryption", &e->crypto);
401
402 blobmsg_close_table(&buf, d);
403 }
404 }
405
406 blobmsg_close_array(&buf, c);
407
408 ubus_send_reply(ctx, req, buf.head);
409
410 rpc_iwinfo_close();
411
412 return UBUS_STATUS_OK;
413 }
414
415 static void
416 rpc_iwinfo_add_rateinfo(struct iwinfo_rate_entry *r)
417 {
418 blobmsg_add_u8(&buf, "ht", r->is_ht);
419 blobmsg_add_u8(&buf, "vht", r->is_vht);
420 blobmsg_add_u32(&buf, "mhz", r->mhz);
421 blobmsg_add_u32(&buf, "rate", r->rate);
422
423 if (r->is_ht) {
424 blobmsg_add_u32(&buf, "mcs", r->mcs);
425 blobmsg_add_u8(&buf, "40mhz", r->is_40mhz);
426 blobmsg_add_u8(&buf, "short_gi", r->is_short_gi);
427 }
428 else if (r->is_vht) {
429 blobmsg_add_u32(&buf, "mcs", r->mcs);
430 blobmsg_add_u32(&buf, "nss", r->nss);
431 blobmsg_add_u8(&buf, "short_gi", r->is_short_gi);
432 }
433 }
434
435 static int
436 rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
437 struct ubus_request_data *req, const char *method,
438 struct blob_attr *msg)
439 {
440 int i, rv, len;
441 char mac[18];
442 char res[IWINFO_BUFSIZE];
443 struct iwinfo_assoclist_entry *a;
444 struct ether_addr *macaddr = NULL;
445 void *c, *d, *e;
446 struct blob_attr *tb[__RPC_A_MAX];
447 bool found = false;
448
449 blobmsg_parse(rpc_assoclist_policy, __RPC_A_MAX, tb,
450 blob_data(msg), blob_len(msg));
451
452 rv = __rpc_iwinfo_open(tb[RPC_A_DEVICE]);
453 if (rv)
454 return rv;
455
456 if (tb[RPC_A_MACADDR])
457 macaddr = ether_aton(blobmsg_data(tb[RPC_A_MACADDR]));
458
459 blob_buf_init(&buf, 0);
460
461 if (!macaddr)
462 c = blobmsg_open_array(&buf, "results");
463
464 if (!iw->assoclist(ifname, res, &len) && (len > 0))
465 {
466 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
467 {
468 a = (struct iwinfo_assoclist_entry *)&res[i];
469
470 if (!macaddr)
471 d = blobmsg_open_table(&buf, NULL);
472 else if (memcmp(macaddr, a->mac, ETH_ALEN) != 0)
473 continue;
474
475 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
476 a->mac[0], a->mac[1], a->mac[2],
477 a->mac[3], a->mac[4], a->mac[5]);
478
479 blobmsg_add_string(&buf, "mac", mac);
480 blobmsg_add_u32(&buf, "signal", a->signal);
481 blobmsg_add_u32(&buf, "signal_avg", a->signal_avg);
482 blobmsg_add_u32(&buf, "noise", a->noise);
483 blobmsg_add_u32(&buf, "inactive", a->inactive);
484 blobmsg_add_u32(&buf, "connected_time", a->connected_time);
485 blobmsg_add_u32(&buf, "thr", a->thr);
486 blobmsg_add_u8(&buf, "authorized", a->is_authorized);
487 blobmsg_add_u8(&buf, "authenticated", a->is_authenticated);
488 blobmsg_add_string(&buf, "preamble", a->is_preamble_short ? "short" : "long");
489 blobmsg_add_u8(&buf, "wme", a->is_wme);
490 blobmsg_add_u8(&buf, "mfp", a->is_mfp);
491 blobmsg_add_u8(&buf, "tdls", a->is_tdls);
492
493 blobmsg_add_u16(&buf, "mesh llid", a->llid);
494 blobmsg_add_u16(&buf, "mesh plid", a->plid);
495 blobmsg_add_string(&buf, "mesh plink", a->plink_state);
496 blobmsg_add_string(&buf, "mesh local PS", a->local_ps);
497 blobmsg_add_string(&buf, "mesh peer PS", a->peer_ps);
498 blobmsg_add_string(&buf, "mesh non-peer PS", a->nonpeer_ps);
499
500 e = blobmsg_open_table(&buf, "rx");
501 blobmsg_add_u64(&buf, "drop_misc", a->rx_drop_misc);
502 blobmsg_add_u32(&buf, "packets", a->rx_packets);
503 blobmsg_add_u32(&buf, "bytes", a->rx_bytes);
504 rpc_iwinfo_add_rateinfo(&a->rx_rate);
505 blobmsg_close_table(&buf, e);
506
507 e = blobmsg_open_table(&buf, "tx");
508 blobmsg_add_u32(&buf, "failed", a->tx_failed);
509 blobmsg_add_u32(&buf, "retries", a->tx_retries);
510 blobmsg_add_u32(&buf, "packets", a->tx_packets);
511 blobmsg_add_u32(&buf, "bytes", a->tx_bytes);
512 rpc_iwinfo_add_rateinfo(&a->tx_rate);
513 blobmsg_close_table(&buf, e);
514
515 found = true;
516 if (!macaddr)
517 blobmsg_close_table(&buf, d);
518 else
519 break;
520 }
521 }
522
523 if (!macaddr)
524 blobmsg_close_array(&buf, c);
525 else if (!found)
526 return UBUS_STATUS_NOT_FOUND;
527
528 ubus_send_reply(ctx, req, buf.head);
529
530 rpc_iwinfo_close();
531
532 return UBUS_STATUS_OK;
533 }
534
535 static int
536 rpc_iwinfo_survey(struct ubus_context *ctx, struct ubus_object *obj,
537 struct ubus_request_data *req, const char *method,
538 struct blob_attr *msg)
539 {
540 char res[IWINFO_BUFSIZE];
541 struct iwinfo_survey_entry *e;
542 void *c, *d;
543 int i, rv, len;
544
545 blob_buf_init(&buf, 0);
546
547 rv = rpc_iwinfo_open(msg);
548
549 c = blobmsg_open_array(&buf, "results");
550
551 if (rv || iw->survey(ifname, res, &len) || len < 0)
552 return UBUS_STATUS_OK;
553
554 for (i = 0; i < len; i += sizeof(struct iwinfo_survey_entry)) {
555 e = (struct iwinfo_survey_entry *)&res[i];
556
557 d = blobmsg_open_table(&buf, NULL);
558 blobmsg_add_u32(&buf, "mhz", e->mhz);
559 blobmsg_add_u32(&buf, "noise", e->noise);
560 blobmsg_add_u64(&buf, "active_time", e->active_time);
561 blobmsg_add_u64(&buf, "busy_time", e->busy_time);
562 blobmsg_add_u64(&buf, "busy_time_ext", e->busy_time_ext);
563 blobmsg_add_u64(&buf, "rx_time", e->rxtime);
564 blobmsg_add_u64(&buf, "tx_time", e->txtime);
565 blobmsg_close_table(&buf, d);
566 }
567
568 blobmsg_close_array(&buf, c);
569 ubus_send_reply(ctx, req, buf.head);
570 rpc_iwinfo_close();
571 return UBUS_STATUS_OK;
572 }
573
574 static int
575 rpc_iwinfo_freqlist(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 i, rv, len, ch;
580 char res[IWINFO_BUFSIZE];
581 struct iwinfo_freqlist_entry *f;
582 void *c, *d;
583
584 rv = rpc_iwinfo_open(msg);
585
586 if (rv)
587 return rv;
588
589 blob_buf_init(&buf, 0);
590
591 c = blobmsg_open_array(&buf, "results");
592
593 if (!iw->freqlist(ifname, res, &len) && (len > 0))
594 {
595 if (iw->channel(ifname, &ch))
596 ch = -1;
597
598 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
599 {
600 f = (struct iwinfo_freqlist_entry *)&res[i];
601 d = blobmsg_open_table(&buf, NULL);
602
603 blobmsg_add_u32(&buf, "channel", f->channel);
604 blobmsg_add_u32(&buf, "mhz", f->mhz);
605 blobmsg_add_u8(&buf, "restricted", f->restricted);
606
607 if (ch > -1)
608 blobmsg_add_u8(&buf, "active", f->channel == ch);
609
610 blobmsg_close_table(&buf, d);
611 }
612 }
613
614 blobmsg_close_array(&buf, c);
615
616 ubus_send_reply(ctx, req, buf.head);
617
618 rpc_iwinfo_close();
619
620 return UBUS_STATUS_OK;
621 }
622
623 static int
624 rpc_iwinfo_txpowerlist(struct ubus_context *ctx, struct ubus_object *obj,
625 struct ubus_request_data *req, const char *method,
626 struct blob_attr *msg)
627 {
628 int i, rv, len, pwr, off;
629 char res[IWINFO_BUFSIZE];
630 struct iwinfo_txpwrlist_entry *t;
631 void *c, *d;
632
633 rv = rpc_iwinfo_open(msg);
634
635 if (rv)
636 return rv;
637
638 blob_buf_init(&buf, 0);
639
640 c = blobmsg_open_array(&buf, "results");
641
642 if (!iw->txpwrlist(ifname, res, &len) && (len > 0))
643 {
644 if (iw->txpower(ifname, &pwr))
645 pwr = -1;
646
647 if (iw->txpower_offset(ifname, &off))
648 off = 0;
649
650 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
651 {
652 t = (struct iwinfo_txpwrlist_entry *)&res[i];
653 d = blobmsg_open_table(&buf, NULL);
654
655 blobmsg_add_u32(&buf, "dbm", t->dbm + off);
656 blobmsg_add_u32(&buf, "mw", iwinfo_dbm2mw(t->dbm + off));
657
658 if (pwr > -1)
659 blobmsg_add_u8(&buf, "active", t->dbm == pwr);
660
661 blobmsg_close_table(&buf, d);
662 }
663 }
664
665 blobmsg_close_array(&buf, c);
666
667 ubus_send_reply(ctx, req, buf.head);
668
669 rpc_iwinfo_close();
670
671 return UBUS_STATUS_OK;
672 }
673
674 static const char *
675 rpc_iwinfo_lookup_country(char *buf, int len, int iso3166)
676 {
677 int i;
678 static char ccode[5];
679 struct iwinfo_country_entry *c;
680
681 for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
682 {
683 c = (struct iwinfo_country_entry *)&buf[i];
684
685 if (c->iso3166 == iso3166)
686 {
687 snprintf(ccode, sizeof(ccode), "%s", c->ccode);
688 return ccode;
689 }
690 }
691
692 return NULL;
693 }
694
695 static int
696 rpc_iwinfo_countrylist(struct ubus_context *ctx, struct ubus_object *obj,
697 struct ubus_request_data *req, const char *method,
698 struct blob_attr *msg)
699 {
700 int rv, len;
701 char cur[3];
702 char iso3166[3];
703 char res[IWINFO_BUFSIZE] = {0};
704 const char *ccode;
705 const struct iwinfo_iso3166_label *l;
706 void *c, *d;
707
708 rv = rpc_iwinfo_open(msg);
709
710 if (rv)
711 return rv;
712
713 blob_buf_init(&buf, 0);
714
715 c = blobmsg_open_array(&buf, "results");
716
717 if (!iw->countrylist(ifname, res, &len) && (len > 0))
718 {
719 if (iw->country(ifname, cur))
720 memset(cur, 0, sizeof(cur));
721
722 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
723 {
724 ccode = rpc_iwinfo_lookup_country(res, len, l->iso3166);
725
726 if (!ccode)
727 continue;
728
729 d = blobmsg_open_table(&buf, NULL);
730
731 blobmsg_add_string(&buf, "code", ccode);
732 blobmsg_add_string(&buf, "country", (const char *)l->name);
733
734 snprintf(iso3166, sizeof(iso3166), "%c%c",
735 (l->iso3166 / 256), (l->iso3166 % 256));
736
737 blobmsg_add_string(&buf, "iso3166", iso3166);
738
739 if (cur[0])
740 blobmsg_add_u8(&buf, "active", !strncmp(ccode, cur, 2));
741
742 blobmsg_close_table(&buf, d);
743 }
744 }
745
746 blobmsg_close_array(&buf, c);
747
748 ubus_send_reply(ctx, req, buf.head);
749
750 rpc_iwinfo_close();
751
752 return UBUS_STATUS_OK;
753 }
754
755 static int
756 rpc_iwinfo_phyname(struct ubus_context *ctx, struct ubus_object *obj,
757 struct ubus_request_data *req, const char *method,
758 struct blob_attr *msg)
759 {
760 int i;
761 bool found = false;
762 char res[IWINFO_BUFSIZE];
763 const struct iwinfo_ops *ops;
764 struct blob_attr *tb[__RPC_U_MAX];
765 const char *backends[] = {
766 "nl80211",
767 "madwifi",
768 "wl"
769 };
770
771 blobmsg_parse(rpc_uci_policy, __RPC_U_MAX, tb,
772 blob_data(msg), blob_len(msg));
773
774 if (!tb[RPC_U_SECTION])
775 return UBUS_STATUS_INVALID_ARGUMENT;
776
777 for (i = 0; i < ARRAY_SIZE(backends); i++)
778 {
779 ops = iwinfo_backend_by_name(backends[i]);
780
781 if (!ops || !ops->lookup_phy)
782 continue;
783
784 if (!ops->lookup_phy(blobmsg_get_string(tb[RPC_U_SECTION]), res))
785 {
786 found = true;
787 break;
788 }
789 }
790
791 if (found)
792 {
793 blob_buf_init(&buf, 0);
794 blobmsg_add_string(&buf, "phyname", res);
795
796 ubus_send_reply(ctx, req, buf.head);
797 }
798
799 rpc_iwinfo_close();
800
801 return found ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
802 }
803
804 static int
805 rpc_iwinfo_devices(struct ubus_context *ctx, struct ubus_object *obj,
806 struct ubus_request_data *req, const char *method,
807 struct blob_attr *msg)
808 {
809 void *c;
810 struct dirent *e;
811 DIR *d;
812
813 d = opendir("/sys/class/net");
814
815 if (!d)
816 return UBUS_STATUS_UNKNOWN_ERROR;
817
818 blob_buf_init(&buf, 0);
819
820 c = blobmsg_open_array(&buf, "devices");
821
822 while ((e = readdir(d)) != NULL)
823 {
824 if (e->d_type != DT_LNK)
825 continue;
826
827 if (iwinfo_type(e->d_name))
828 blobmsg_add_string(&buf, NULL, e->d_name);
829 }
830
831 blobmsg_close_array(&buf, c);
832
833 closedir(d);
834
835 ubus_send_reply(ctx, req, buf.head);
836
837 rpc_iwinfo_close();
838
839 return UBUS_STATUS_OK;
840 }
841
842
843 static int
844 rpc_iwinfo_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
845 {
846 static const struct ubus_method iwinfo_methods[] = {
847 UBUS_METHOD_NOARG("devices", rpc_iwinfo_devices),
848 UBUS_METHOD("info", rpc_iwinfo_info, rpc_device_policy),
849 UBUS_METHOD("scan", rpc_iwinfo_scan, rpc_device_policy),
850 UBUS_METHOD("assoclist", rpc_iwinfo_assoclist, rpc_assoclist_policy),
851 UBUS_METHOD("freqlist", rpc_iwinfo_freqlist, rpc_device_policy),
852 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist, rpc_device_policy),
853 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist, rpc_device_policy),
854 UBUS_METHOD("survey", rpc_iwinfo_survey, rpc_device_policy),
855 UBUS_METHOD("phyname", rpc_iwinfo_phyname, rpc_uci_policy),
856 };
857
858 static struct ubus_object_type iwinfo_type =
859 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods);
860
861 static struct ubus_object obj = {
862 .name = "iwinfo",
863 .type = &iwinfo_type,
864 .methods = iwinfo_methods,
865 .n_methods = ARRAY_SIZE(iwinfo_methods),
866 };
867
868 return ubus_add_object(ctx, &obj);
869 }
870
871 struct rpc_plugin rpc_plugin = {
872 .init = rpc_iwinfo_api_init
873 };