4133dc46ed14de733f4b66db0b5bc0a22f55a260
[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_GCMP)
203 blobmsg_add_string(&buf, NULL, "gcmp");
204
205 if (ciph & IWINFO_CIPHER_WRAP)
206 blobmsg_add_string(&buf, NULL, "wrap");
207
208 if (ciph & IWINFO_CIPHER_AESOCB)
209 blobmsg_add_string(&buf, NULL, "aes-ocb");
210
211 if (ciph & IWINFO_CIPHER_CKIP)
212 blobmsg_add_string(&buf, NULL, "ckip");
213
214 if (!ciph || (ciph & IWINFO_CIPHER_NONE))
215 blobmsg_add_string(&buf, NULL, "none");
216
217 blobmsg_close_array(&buf, d);
218 }
219
220 blobmsg_close_table(&buf, c);
221 }
222
223 static void
224 rpc_iwinfo_call_encryption(const char *name)
225 {
226 struct iwinfo_crypto_entry crypto = { 0 };
227
228 if (!iw->encryption(ifname, (char *)&crypto))
229 rpc_iwinfo_add_encryption(name, &crypto);
230 }
231
232 static void
233 rpc_iwinfo_call_htmodes(const char *name)
234 {
235 int modes;
236 void *c;
237
238 if (!iw->htmodelist(ifname, &modes))
239 {
240 c = blobmsg_open_array(&buf, name);
241
242 if (modes & IWINFO_HTMODE_HT20)
243 blobmsg_add_string(&buf, NULL, "HT20");
244
245 if (modes & IWINFO_HTMODE_HT40)
246 blobmsg_add_string(&buf, NULL, "HT40");
247
248 if (modes & IWINFO_HTMODE_VHT20)
249 blobmsg_add_string(&buf, NULL, "VHT20");
250
251 if (modes & IWINFO_HTMODE_VHT40)
252 blobmsg_add_string(&buf, NULL, "VHT40");
253
254 if (modes & IWINFO_HTMODE_VHT80)
255 blobmsg_add_string(&buf, NULL, "VHT80");
256
257 if (modes & IWINFO_HTMODE_VHT80_80)
258 blobmsg_add_string(&buf, NULL, "VHT80+80");
259
260 if (modes & IWINFO_HTMODE_VHT160)
261 blobmsg_add_string(&buf, NULL, "VHT160");
262
263 blobmsg_close_array(&buf, c);
264 }
265 }
266
267 static void
268 rpc_iwinfo_call_hwmodes(const char *name)
269 {
270 int modes;
271 void *c;
272
273 if (!iw->hwmodelist(ifname, &modes))
274 {
275 c = blobmsg_open_array(&buf, name);
276
277 if (modes & IWINFO_80211_AD)
278 blobmsg_add_string(&buf, NULL, "ad");
279
280 if (modes & IWINFO_80211_AC)
281 blobmsg_add_string(&buf, NULL, "ac");
282
283 if (modes & IWINFO_80211_A)
284 blobmsg_add_string(&buf, NULL, "a");
285
286 if (modes & IWINFO_80211_B)
287 blobmsg_add_string(&buf, NULL, "b");
288
289 if (modes & IWINFO_80211_G)
290 blobmsg_add_string(&buf, NULL, "g");
291
292 if (modes & IWINFO_80211_N)
293 blobmsg_add_string(&buf, NULL, "n");
294
295 blobmsg_close_array(&buf, c);
296 }
297 }
298
299 static void rpc_iwinfo_call_hw_ht_mode()
300 {
301 const char *hwmode_str;
302 const char *htmode_str;
303 int32_t htmode = 0;
304
305 if (iw->htmode(ifname, &htmode))
306 return;
307
308 switch (htmode) {
309 case IWINFO_HTMODE_HT20:
310 htmode_str = "HT20";
311 hwmode_str = "n";
312 break;
313 case IWINFO_HTMODE_HT40:
314 htmode_str = "HT40";
315 hwmode_str = "n";
316 break;
317 case IWINFO_HTMODE_VHT80:
318 htmode_str = "VHT80";
319 hwmode_str = "ac";
320 break;
321 case IWINFO_HTMODE_VHT80_80:
322 htmode_str = "VHT80+80";
323 hwmode_str = "ac";
324 break;
325 case IWINFO_HTMODE_VHT160:
326 htmode_str = "VHT160";
327 hwmode_str = "ac";
328 break;
329 case IWINFO_HTMODE_NOHT:
330 htmode_str = "20";
331 hwmode_str = "a/g";
332 break;
333 default:
334 htmode_str = hwmode_str = "unknown";
335 break;
336 }
337 blobmsg_add_string(&buf, "hwmode", hwmode_str);
338 blobmsg_add_string(&buf, "htmode", htmode_str);
339 }
340
341 static void
342 rpc_iwinfo_call_str(const char *name, int (*func)(const char *, char *))
343 {
344 char rv[IWINFO_BUFSIZE] = { 0 };
345
346 if (!func(ifname, rv))
347 blobmsg_add_string(&buf, name, rv);
348 }
349
350 static int
351 rpc_iwinfo_info(struct ubus_context *ctx, struct ubus_object *obj,
352 struct ubus_request_data *req, const char *method,
353 struct blob_attr *msg)
354 {
355 int rv;
356 void *c;
357
358 rv = rpc_iwinfo_open(msg);
359
360 if (rv)
361 return rv;
362
363 blob_buf_init(&buf, 0);
364
365 rpc_iwinfo_call_str("phy", iw->phyname);
366
367 rpc_iwinfo_call_str("ssid", iw->ssid);
368 rpc_iwinfo_call_str("bssid", iw->bssid);
369 rpc_iwinfo_call_str("country", iw->country);
370
371 rpc_iwinfo_call_int("mode", iw->mode, IWINFO_OPMODE_NAMES);
372 rpc_iwinfo_call_int("channel", iw->channel, NULL);
373 rpc_iwinfo_call_int("center_chan1", iw->center_chan1, NULL);
374 rpc_iwinfo_call_int("center_chan2", iw->center_chan2, NULL);
375
376 rpc_iwinfo_call_int("frequency", iw->frequency, NULL);
377 rpc_iwinfo_call_int("frequency_offset", iw->frequency_offset, NULL);
378
379 rpc_iwinfo_call_int("txpower", iw->txpower, NULL);
380 rpc_iwinfo_call_int("txpower_offset", iw->txpower_offset, NULL);
381
382 rpc_iwinfo_call_int("quality", iw->quality, NULL);
383 rpc_iwinfo_call_int("quality_max", iw->quality_max, NULL);
384
385 rpc_iwinfo_call_int("signal", iw->signal, NULL);
386 rpc_iwinfo_call_int("noise", iw->noise, NULL);
387
388 rpc_iwinfo_call_int("bitrate", iw->bitrate, NULL);
389
390 rpc_iwinfo_call_encryption("encryption");
391 rpc_iwinfo_call_htmodes("htmodes");
392 rpc_iwinfo_call_hwmodes("hwmodes");
393
394 rpc_iwinfo_call_hw_ht_mode();
395
396 c = blobmsg_open_table(&buf, "hardware");
397 rpc_iwinfo_call_hardware_id("id");
398 rpc_iwinfo_call_str("name", iw->hardware_name);
399 blobmsg_close_table(&buf, c);
400
401 ubus_send_reply(ctx, req, buf.head);
402
403 rpc_iwinfo_close();
404
405 return UBUS_STATUS_OK;
406 }
407
408 static int
409 rpc_iwinfo_scan(struct ubus_context *ctx, struct ubus_object *obj,
410 struct ubus_request_data *req, const char *method,
411 struct blob_attr *msg)
412 {
413 int i, rv, len;
414 void *c, *d, *t;
415 char mac[18];
416 char res[IWINFO_BUFSIZE];
417 struct iwinfo_scanlist_entry *e;
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->scanlist(ifname, res, &len) && (len > 0))
429 {
430 for (i = 0; i < len; i += sizeof(struct iwinfo_scanlist_entry))
431 {
432 e = (struct iwinfo_scanlist_entry *)&res[i];
433 d = blobmsg_open_table(&buf, NULL);
434
435 if (e->ssid[0])
436 blobmsg_add_string(&buf, "ssid", (const char *)e->ssid);
437
438 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
439 e->mac[0], e->mac[1], e->mac[2],
440 e->mac[3], e->mac[4], e->mac[5]);
441
442 blobmsg_add_string(&buf, "bssid", mac);
443
444 blobmsg_add_string(&buf, "mode", IWINFO_OPMODE_NAMES[e->mode]);
445
446 blobmsg_add_u32(&buf, "channel", e->channel);
447 blobmsg_add_u32(&buf, "signal", (uint32_t)(e->signal - 0x100));
448
449 blobmsg_add_u32(&buf, "quality", e->quality);
450 blobmsg_add_u32(&buf, "quality_max", e->quality_max);
451
452 t = blobmsg_open_table(&buf, "ht_operation");
453 blobmsg_add_u32(&buf, "primary_channel", e->ht_chan_info.primary_chan);
454 blobmsg_add_string(&buf, "secondary_channel_offset", ht_secondary_offset[e->ht_chan_info.secondary_chan_off]);
455 blobmsg_add_u32(&buf, "channel_width", ht_chan_width[e->ht_chan_info.chan_width]);
456 blobmsg_close_table(&buf, t);
457
458 if (e->vht_chan_info.center_chan_1) {
459 t = blobmsg_open_table(&buf, "vht_operation");
460 blobmsg_add_u32(&buf, "channel_width", vht_chan_width[e->vht_chan_info.chan_width]);
461 blobmsg_add_u32(&buf, "center_freq_1", e->vht_chan_info.center_chan_1);
462 blobmsg_add_u32(&buf, "center_freq_2", e->vht_chan_info.center_chan_2);
463 blobmsg_close_table(&buf, t);
464 }
465
466 rpc_iwinfo_add_encryption("encryption", &e->crypto);
467
468 blobmsg_close_table(&buf, d);
469 }
470 }
471
472 blobmsg_close_array(&buf, c);
473
474 ubus_send_reply(ctx, req, buf.head);
475
476 rpc_iwinfo_close();
477
478 return UBUS_STATUS_OK;
479 }
480
481 static void
482 rpc_iwinfo_add_rateinfo(struct iwinfo_rate_entry *r)
483 {
484 blobmsg_add_u8(&buf, "ht", r->is_ht);
485 blobmsg_add_u8(&buf, "vht", r->is_vht);
486 blobmsg_add_u32(&buf, "mhz", r->mhz);
487 blobmsg_add_u32(&buf, "rate", r->rate);
488
489 if (r->is_ht) {
490 blobmsg_add_u32(&buf, "mcs", r->mcs);
491 blobmsg_add_u8(&buf, "40mhz", r->is_40mhz);
492 blobmsg_add_u8(&buf, "short_gi", r->is_short_gi);
493 }
494 else if (r->is_vht) {
495 blobmsg_add_u32(&buf, "mcs", r->mcs);
496 blobmsg_add_u32(&buf, "nss", r->nss);
497 blobmsg_add_u8(&buf, "short_gi", r->is_short_gi);
498 }
499 }
500
501 static int
502 rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
503 struct ubus_request_data *req, const char *method,
504 struct blob_attr *msg)
505 {
506 int i, rv, len;
507 char mac[18];
508 char res[IWINFO_BUFSIZE];
509 struct iwinfo_assoclist_entry *a;
510 struct ether_addr *macaddr = NULL;
511 void *c, *d, *e;
512 struct blob_attr *tb[__RPC_A_MAX];
513 bool found = false;
514
515 blobmsg_parse(rpc_assoclist_policy, __RPC_A_MAX, tb,
516 blob_data(msg), blob_len(msg));
517
518 rv = __rpc_iwinfo_open(tb[RPC_A_DEVICE]);
519 if (rv)
520 return rv;
521
522 if (tb[RPC_A_MACADDR])
523 macaddr = ether_aton(blobmsg_data(tb[RPC_A_MACADDR]));
524
525 blob_buf_init(&buf, 0);
526
527 if (!macaddr)
528 c = blobmsg_open_array(&buf, "results");
529
530 if (!iw->assoclist(ifname, res, &len) && (len > 0))
531 {
532 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
533 {
534 a = (struct iwinfo_assoclist_entry *)&res[i];
535
536 if (!macaddr)
537 d = blobmsg_open_table(&buf, NULL);
538 else if (memcmp(macaddr, a->mac, ETH_ALEN) != 0)
539 continue;
540
541 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
542 a->mac[0], a->mac[1], a->mac[2],
543 a->mac[3], a->mac[4], a->mac[5]);
544
545 blobmsg_add_string(&buf, "mac", mac);
546 blobmsg_add_u32(&buf, "signal", a->signal);
547 blobmsg_add_u32(&buf, "signal_avg", a->signal_avg);
548 blobmsg_add_u32(&buf, "noise", a->noise);
549 blobmsg_add_u32(&buf, "inactive", a->inactive);
550 blobmsg_add_u32(&buf, "connected_time", a->connected_time);
551 blobmsg_add_u32(&buf, "thr", a->thr);
552 blobmsg_add_u8(&buf, "authorized", a->is_authorized);
553 blobmsg_add_u8(&buf, "authenticated", a->is_authenticated);
554 blobmsg_add_string(&buf, "preamble", a->is_preamble_short ? "short" : "long");
555 blobmsg_add_u8(&buf, "wme", a->is_wme);
556 blobmsg_add_u8(&buf, "mfp", a->is_mfp);
557 blobmsg_add_u8(&buf, "tdls", a->is_tdls);
558
559 blobmsg_add_u16(&buf, "mesh llid", a->llid);
560 blobmsg_add_u16(&buf, "mesh plid", a->plid);
561 blobmsg_add_string(&buf, "mesh plink", a->plink_state);
562 blobmsg_add_string(&buf, "mesh local PS", a->local_ps);
563 blobmsg_add_string(&buf, "mesh peer PS", a->peer_ps);
564 blobmsg_add_string(&buf, "mesh non-peer PS", a->nonpeer_ps);
565
566 e = blobmsg_open_table(&buf, "rx");
567 blobmsg_add_u64(&buf, "drop_misc", a->rx_drop_misc);
568 blobmsg_add_u32(&buf, "packets", a->rx_packets);
569 blobmsg_add_u32(&buf, "bytes", a->rx_bytes);
570 rpc_iwinfo_add_rateinfo(&a->rx_rate);
571 blobmsg_close_table(&buf, e);
572
573 e = blobmsg_open_table(&buf, "tx");
574 blobmsg_add_u32(&buf, "failed", a->tx_failed);
575 blobmsg_add_u32(&buf, "retries", a->tx_retries);
576 blobmsg_add_u32(&buf, "packets", a->tx_packets);
577 blobmsg_add_u32(&buf, "bytes", a->tx_bytes);
578 rpc_iwinfo_add_rateinfo(&a->tx_rate);
579 blobmsg_close_table(&buf, e);
580
581 found = true;
582 if (!macaddr)
583 blobmsg_close_table(&buf, d);
584 else
585 break;
586 }
587 }
588
589 if (!macaddr)
590 blobmsg_close_array(&buf, c);
591 else if (!found)
592 return UBUS_STATUS_NOT_FOUND;
593
594 ubus_send_reply(ctx, req, buf.head);
595
596 rpc_iwinfo_close();
597
598 return UBUS_STATUS_OK;
599 }
600
601 static int
602 rpc_iwinfo_survey(struct ubus_context *ctx, struct ubus_object *obj,
603 struct ubus_request_data *req, const char *method,
604 struct blob_attr *msg)
605 {
606 char res[IWINFO_BUFSIZE];
607 struct iwinfo_survey_entry *e;
608 void *c, *d;
609 int i, rv, len;
610
611 blob_buf_init(&buf, 0);
612
613 rv = rpc_iwinfo_open(msg);
614
615 c = blobmsg_open_array(&buf, "results");
616
617 if (rv || iw->survey(ifname, res, &len) || len < 0)
618 return UBUS_STATUS_OK;
619
620 for (i = 0; i < len; i += sizeof(struct iwinfo_survey_entry)) {
621 e = (struct iwinfo_survey_entry *)&res[i];
622
623 d = blobmsg_open_table(&buf, NULL);
624 blobmsg_add_u32(&buf, "mhz", e->mhz);
625 blobmsg_add_u32(&buf, "noise", e->noise);
626 blobmsg_add_u64(&buf, "active_time", e->active_time);
627 blobmsg_add_u64(&buf, "busy_time", e->busy_time);
628 blobmsg_add_u64(&buf, "busy_time_ext", e->busy_time_ext);
629 blobmsg_add_u64(&buf, "rx_time", e->rxtime);
630 blobmsg_add_u64(&buf, "tx_time", e->txtime);
631 blobmsg_close_table(&buf, d);
632 }
633
634 blobmsg_close_array(&buf, c);
635 ubus_send_reply(ctx, req, buf.head);
636 rpc_iwinfo_close();
637 return UBUS_STATUS_OK;
638 }
639
640 static int
641 rpc_iwinfo_freqlist(struct ubus_context *ctx, struct ubus_object *obj,
642 struct ubus_request_data *req, const char *method,
643 struct blob_attr *msg)
644 {
645 int i, rv, len, ch;
646 char res[IWINFO_BUFSIZE];
647 struct iwinfo_freqlist_entry *f;
648 void *c, *d;
649
650 rv = rpc_iwinfo_open(msg);
651
652 if (rv)
653 return rv;
654
655 blob_buf_init(&buf, 0);
656
657 c = blobmsg_open_array(&buf, "results");
658
659 if (!iw->freqlist(ifname, res, &len) && (len > 0))
660 {
661 if (iw->channel(ifname, &ch))
662 ch = -1;
663
664 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
665 {
666 f = (struct iwinfo_freqlist_entry *)&res[i];
667 d = blobmsg_open_table(&buf, NULL);
668
669 blobmsg_add_u32(&buf, "channel", f->channel);
670 blobmsg_add_u32(&buf, "mhz", f->mhz);
671 blobmsg_add_u8(&buf, "restricted", f->restricted);
672
673 if (ch > -1)
674 blobmsg_add_u8(&buf, "active", f->channel == ch);
675
676 blobmsg_close_table(&buf, d);
677 }
678 }
679
680 blobmsg_close_array(&buf, c);
681
682 ubus_send_reply(ctx, req, buf.head);
683
684 rpc_iwinfo_close();
685
686 return UBUS_STATUS_OK;
687 }
688
689 static int
690 rpc_iwinfo_txpowerlist(struct ubus_context *ctx, struct ubus_object *obj,
691 struct ubus_request_data *req, const char *method,
692 struct blob_attr *msg)
693 {
694 int i, rv, len, pwr, off;
695 char res[IWINFO_BUFSIZE];
696 struct iwinfo_txpwrlist_entry *t;
697 void *c, *d;
698
699 rv = rpc_iwinfo_open(msg);
700
701 if (rv)
702 return rv;
703
704 blob_buf_init(&buf, 0);
705
706 c = blobmsg_open_array(&buf, "results");
707
708 if (!iw->txpwrlist(ifname, res, &len) && (len > 0))
709 {
710 if (iw->txpower(ifname, &pwr))
711 pwr = -1;
712
713 if (iw->txpower_offset(ifname, &off))
714 off = 0;
715
716 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
717 {
718 t = (struct iwinfo_txpwrlist_entry *)&res[i];
719 d = blobmsg_open_table(&buf, NULL);
720
721 blobmsg_add_u32(&buf, "dbm", t->dbm + off);
722 blobmsg_add_u32(&buf, "mw", iwinfo_dbm2mw(t->dbm + off));
723
724 if (pwr > -1)
725 blobmsg_add_u8(&buf, "active", t->dbm == pwr);
726
727 blobmsg_close_table(&buf, d);
728 }
729 }
730
731 blobmsg_close_array(&buf, c);
732
733 ubus_send_reply(ctx, req, buf.head);
734
735 rpc_iwinfo_close();
736
737 return UBUS_STATUS_OK;
738 }
739
740 static const char *
741 rpc_iwinfo_lookup_country(char *buf, int len, int iso3166)
742 {
743 int i;
744 static char ccode[5];
745 struct iwinfo_country_entry *c;
746
747 for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
748 {
749 c = (struct iwinfo_country_entry *)&buf[i];
750
751 if (c->iso3166 == iso3166)
752 {
753 snprintf(ccode, sizeof(ccode), "%s", c->ccode);
754 return ccode;
755 }
756 }
757
758 return NULL;
759 }
760
761 static int
762 rpc_iwinfo_countrylist(struct ubus_context *ctx, struct ubus_object *obj,
763 struct ubus_request_data *req, const char *method,
764 struct blob_attr *msg)
765 {
766 int rv, len;
767 char cur[3];
768 char iso3166[3];
769 char res[IWINFO_BUFSIZE] = {0};
770 const char *ccode;
771 const struct iwinfo_iso3166_label *l;
772 void *c, *d;
773
774 rv = rpc_iwinfo_open(msg);
775
776 if (rv)
777 return rv;
778
779 blob_buf_init(&buf, 0);
780
781 c = blobmsg_open_array(&buf, "results");
782
783 if (!iw->countrylist(ifname, res, &len) && (len > 0))
784 {
785 if (iw->country(ifname, cur))
786 memset(cur, 0, sizeof(cur));
787
788 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
789 {
790 ccode = rpc_iwinfo_lookup_country(res, len, l->iso3166);
791
792 if (!ccode)
793 continue;
794
795 d = blobmsg_open_table(&buf, NULL);
796
797 blobmsg_add_string(&buf, "code", ccode);
798 blobmsg_add_string(&buf, "country", (const char *)l->name);
799
800 snprintf(iso3166, sizeof(iso3166), "%c%c",
801 (l->iso3166 / 256), (l->iso3166 % 256));
802
803 blobmsg_add_string(&buf, "iso3166", iso3166);
804
805 if (cur[0])
806 blobmsg_add_u8(&buf, "active", !strncmp(ccode, cur, 2));
807
808 blobmsg_close_table(&buf, d);
809 }
810 }
811
812 blobmsg_close_array(&buf, c);
813
814 ubus_send_reply(ctx, req, buf.head);
815
816 rpc_iwinfo_close();
817
818 return UBUS_STATUS_OK;
819 }
820
821 static int
822 rpc_iwinfo_phyname(struct ubus_context *ctx, struct ubus_object *obj,
823 struct ubus_request_data *req, const char *method,
824 struct blob_attr *msg)
825 {
826 int i;
827 bool found = false;
828 char res[IWINFO_BUFSIZE];
829 const struct iwinfo_ops *ops;
830 struct blob_attr *tb[__RPC_U_MAX];
831 const char *backends[] = {
832 "nl80211",
833 "madwifi",
834 "wl"
835 };
836
837 blobmsg_parse(rpc_uci_policy, __RPC_U_MAX, tb,
838 blob_data(msg), blob_len(msg));
839
840 if (!tb[RPC_U_SECTION])
841 return UBUS_STATUS_INVALID_ARGUMENT;
842
843 for (i = 0; i < ARRAY_SIZE(backends); i++)
844 {
845 ops = iwinfo_backend_by_name(backends[i]);
846
847 if (!ops || !ops->lookup_phy)
848 continue;
849
850 if (!ops->lookup_phy(blobmsg_get_string(tb[RPC_U_SECTION]), res))
851 {
852 found = true;
853 break;
854 }
855 }
856
857 if (found)
858 {
859 blob_buf_init(&buf, 0);
860 blobmsg_add_string(&buf, "phyname", res);
861
862 ubus_send_reply(ctx, req, buf.head);
863 }
864
865 rpc_iwinfo_close();
866
867 return found ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
868 }
869
870 static int
871 rpc_iwinfo_devices(struct ubus_context *ctx, struct ubus_object *obj,
872 struct ubus_request_data *req, const char *method,
873 struct blob_attr *msg)
874 {
875 void *c;
876 struct dirent *e;
877 DIR *d;
878
879 d = opendir("/sys/class/net");
880
881 if (!d)
882 return UBUS_STATUS_UNKNOWN_ERROR;
883
884 blob_buf_init(&buf, 0);
885
886 c = blobmsg_open_array(&buf, "devices");
887
888 while ((e = readdir(d)) != NULL)
889 {
890 if (e->d_type != DT_LNK)
891 continue;
892
893 if (iwinfo_type(e->d_name))
894 blobmsg_add_string(&buf, NULL, e->d_name);
895 }
896
897 blobmsg_close_array(&buf, c);
898
899 closedir(d);
900
901 ubus_send_reply(ctx, req, buf.head);
902
903 rpc_iwinfo_close();
904
905 return UBUS_STATUS_OK;
906 }
907
908
909 static int
910 rpc_iwinfo_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
911 {
912 static const struct ubus_method iwinfo_methods[] = {
913 UBUS_METHOD_NOARG("devices", rpc_iwinfo_devices),
914 UBUS_METHOD("info", rpc_iwinfo_info, rpc_device_policy),
915 UBUS_METHOD("scan", rpc_iwinfo_scan, rpc_device_policy),
916 UBUS_METHOD("assoclist", rpc_iwinfo_assoclist, rpc_assoclist_policy),
917 UBUS_METHOD("freqlist", rpc_iwinfo_freqlist, rpc_device_policy),
918 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist, rpc_device_policy),
919 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist, rpc_device_policy),
920 UBUS_METHOD("survey", rpc_iwinfo_survey, rpc_device_policy),
921 UBUS_METHOD("phyname", rpc_iwinfo_phyname, rpc_uci_policy),
922 };
923
924 static struct ubus_object_type iwinfo_type =
925 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods);
926
927 static struct ubus_object obj = {
928 .name = "iwinfo",
929 .type = &iwinfo_type,
930 .methods = iwinfo_methods,
931 .n_methods = ARRAY_SIZE(iwinfo_methods),
932 };
933
934 return ubus_add_object(ctx, &obj);
935 }
936
937 struct rpc_plugin rpc_plugin = {
938 .init = rpc_iwinfo_api_init
939 };