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