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