2 * rpcd - UBUS RPC server
4 * Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
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.
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.
19 #include <sys/types.h>
23 #include <iwinfo/utils.h>
24 #include <net/ethernet.h>
27 #include <netinet/ether.h>
30 #include <rpcd/plugin.h>
33 static struct blob_buf buf;
34 static const struct iwinfo_ops *iw;
35 static const char *ifname;
42 static const struct blobmsg_policy rpc_device_policy[__RPC_D_MAX] = {
43 [RPC_D_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
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 },
58 __rpc_iwinfo_open(struct blob_attr *device)
61 return UBUS_STATUS_INVALID_ARGUMENT;
63 ifname = blobmsg_data(device);
64 iw = iwinfo_backend(ifname);
66 return iw ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
70 rpc_iwinfo_open(struct blob_attr *msg)
72 static struct blob_attr *tb[__RPC_D_MAX];
74 blobmsg_parse(rpc_device_policy, __RPC_D_MAX, tb,
75 blob_data(msg), blob_len(msg));
77 return __rpc_iwinfo_open(tb[RPC_D_DEVICE]);
81 rpc_iwinfo_close(void)
89 rpc_iwinfo_call_int(const char *name, int (*func)(const char *, int *),
94 if (!func(ifname, &rv))
97 blobmsg_add_u32(&buf, name, rv);
99 blobmsg_add_string(&buf, name, map[rv]);
104 rpc_iwinfo_call_hardware_id(const char *name)
106 struct iwinfo_hardware_id ids;
109 if (!iw->hardware_id(ifname, (char *)&ids))
111 c = blobmsg_open_array(&buf, name);
113 blobmsg_add_u32(&buf, NULL, ids.vendor_id);
114 blobmsg_add_u32(&buf, NULL, ids.device_id);
115 blobmsg_add_u32(&buf, NULL, ids.subsystem_vendor_id);
116 blobmsg_add_u32(&buf, NULL, ids.subsystem_device_id);
118 blobmsg_close_array(&buf, c);
123 rpc_iwinfo_add_encryption(const char *name, struct iwinfo_crypto_entry *e)
128 c = blobmsg_open_table(&buf, name);
130 blobmsg_add_u8(&buf, "enabled", e->enabled);
136 d = blobmsg_open_array(&buf, "wep");
138 if (e->auth_algs & IWINFO_AUTH_OPEN)
139 blobmsg_add_string(&buf, NULL, "open");
141 if (e->auth_algs & IWINFO_AUTH_SHARED)
142 blobmsg_add_string(&buf, NULL, "shared");
144 blobmsg_close_array(&buf, d);
148 d = blobmsg_open_array(&buf, "wpa");
150 if (e->wpa_version > 2)
152 blobmsg_add_u32(&buf, NULL, 1);
153 blobmsg_add_u32(&buf, NULL, 2);
157 blobmsg_add_u32(&buf, NULL, e->wpa_version);
160 blobmsg_close_array(&buf, d);
163 d = blobmsg_open_array(&buf, "authentication");
165 if (e->auth_suites & IWINFO_KMGMT_PSK)
166 blobmsg_add_string(&buf, NULL, "psk");
168 if (e->auth_suites & IWINFO_KMGMT_8021x)
169 blobmsg_add_string(&buf, NULL, "802.1x");
171 if (!e->auth_suites ||
172 (e->auth_suites & IWINFO_KMGMT_NONE))
173 blobmsg_add_string(&buf, NULL, "none");
175 blobmsg_close_array(&buf, d);
178 d = blobmsg_open_array(&buf, "ciphers");
179 ciph = e->pair_ciphers | e->group_ciphers;
181 if (ciph & IWINFO_CIPHER_WEP40)
182 blobmsg_add_string(&buf, NULL, "wep-40");
184 if (ciph & IWINFO_CIPHER_WEP104)
185 blobmsg_add_string(&buf, NULL, "wep-104");
187 if (ciph & IWINFO_CIPHER_TKIP)
188 blobmsg_add_string(&buf, NULL, "tkip");
190 if (ciph & IWINFO_CIPHER_CCMP)
191 blobmsg_add_string(&buf, NULL, "ccmp");
193 if (ciph & IWINFO_CIPHER_WRAP)
194 blobmsg_add_string(&buf, NULL, "wrap");
196 if (ciph & IWINFO_CIPHER_AESOCB)
197 blobmsg_add_string(&buf, NULL, "aes-ocb");
199 if (ciph & IWINFO_CIPHER_CKIP)
200 blobmsg_add_string(&buf, NULL, "ckip");
202 if (!ciph || (ciph & IWINFO_CIPHER_NONE))
203 blobmsg_add_string(&buf, NULL, "none");
205 blobmsg_close_array(&buf, d);
208 blobmsg_close_table(&buf, c);
212 rpc_iwinfo_call_encryption(const char *name)
214 struct iwinfo_crypto_entry crypto = { 0 };
216 if (!iw->encryption(ifname, (char *)&crypto))
217 rpc_iwinfo_add_encryption(name, &crypto);
221 rpc_iwinfo_call_hwmodes(const char *name)
226 if (!iw->hwmodelist(ifname, &modes))
228 c = blobmsg_open_array(&buf, name);
230 if (modes & IWINFO_80211_AC)
231 blobmsg_add_string(&buf, NULL, "ac");
233 if (modes & IWINFO_80211_A)
234 blobmsg_add_string(&buf, NULL, "a");
236 if (modes & IWINFO_80211_B)
237 blobmsg_add_string(&buf, NULL, "b");
239 if (modes & IWINFO_80211_G)
240 blobmsg_add_string(&buf, NULL, "g");
242 if (modes & IWINFO_80211_N)
243 blobmsg_add_string(&buf, NULL, "n");
245 blobmsg_close_array(&buf, c);
250 rpc_iwinfo_call_str(const char *name, int (*func)(const char *, char *))
252 char rv[IWINFO_BUFSIZE] = { 0 };
254 if (!func(ifname, rv))
255 blobmsg_add_string(&buf, name, rv);
259 rpc_iwinfo_info(struct ubus_context *ctx, struct ubus_object *obj,
260 struct ubus_request_data *req, const char *method,
261 struct blob_attr *msg)
266 rv = rpc_iwinfo_open(msg);
271 blob_buf_init(&buf, 0);
273 rpc_iwinfo_call_str("phy", iw->phyname);
275 rpc_iwinfo_call_str("ssid", iw->ssid);
276 rpc_iwinfo_call_str("bssid", iw->bssid);
277 rpc_iwinfo_call_str("country", iw->country);
279 rpc_iwinfo_call_int("mode", iw->mode, IWINFO_OPMODE_NAMES);
280 rpc_iwinfo_call_int("channel", iw->channel, NULL);
282 rpc_iwinfo_call_int("frequency", iw->frequency, NULL);
283 rpc_iwinfo_call_int("frequency_offset", iw->frequency_offset, NULL);
285 rpc_iwinfo_call_int("txpower", iw->txpower, NULL);
286 rpc_iwinfo_call_int("txpower_offset", iw->txpower_offset, NULL);
288 rpc_iwinfo_call_int("quality", iw->quality, NULL);
289 rpc_iwinfo_call_int("quality_max", iw->quality_max, NULL);
291 rpc_iwinfo_call_int("signal", iw->signal, NULL);
292 rpc_iwinfo_call_int("noise", iw->noise, NULL);
294 rpc_iwinfo_call_int("bitrate", iw->bitrate, NULL);
296 rpc_iwinfo_call_encryption("encryption");
297 rpc_iwinfo_call_hwmodes("hwmodes");
299 c = blobmsg_open_table(&buf, "hardware");
300 rpc_iwinfo_call_hardware_id("id");
301 rpc_iwinfo_call_str("name", iw->hardware_name);
302 blobmsg_close_table(&buf, c);
304 ubus_send_reply(ctx, req, buf.head);
308 return UBUS_STATUS_OK;
312 rpc_iwinfo_scan(struct ubus_context *ctx, struct ubus_object *obj,
313 struct ubus_request_data *req, const char *method,
314 struct blob_attr *msg)
319 char res[IWINFO_BUFSIZE];
320 struct iwinfo_scanlist_entry *e;
322 rv = rpc_iwinfo_open(msg);
327 blob_buf_init(&buf, 0);
329 c = blobmsg_open_array(&buf, "results");
331 if (!iw->scanlist(ifname, res, &len) && (len > 0))
333 for (i = 0; i < len; i += sizeof(struct iwinfo_scanlist_entry))
335 e = (struct iwinfo_scanlist_entry *)&res[i];
336 d = blobmsg_open_table(&buf, NULL);
339 blobmsg_add_string(&buf, "ssid", (const char *)e->ssid);
341 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
342 e->mac[0], e->mac[1], e->mac[2],
343 e->mac[3], e->mac[4], e->mac[5]);
345 blobmsg_add_string(&buf, "bssid", mac);
347 blobmsg_add_string(&buf, "mode", IWINFO_OPMODE_NAMES[e->mode]);
349 blobmsg_add_u32(&buf, "channel", e->channel);
350 blobmsg_add_u32(&buf, "signal", (uint32_t)(e->signal - 0x100));
352 blobmsg_add_u32(&buf, "quality", e->quality);
353 blobmsg_add_u32(&buf, "quality_max", e->quality_max);
355 rpc_iwinfo_add_encryption("encryption", &e->crypto);
357 blobmsg_close_table(&buf, d);
361 blobmsg_close_array(&buf, c);
363 ubus_send_reply(ctx, req, buf.head);
367 return UBUS_STATUS_OK;
371 rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
372 struct ubus_request_data *req, const char *method,
373 struct blob_attr *msg)
377 char res[IWINFO_BUFSIZE];
378 struct iwinfo_assoclist_entry *a;
379 struct ether_addr *macaddr = NULL;
381 struct blob_attr *tb[__RPC_A_MAX];
384 blobmsg_parse(rpc_assoclist_policy, __RPC_A_MAX, tb,
385 blob_data(msg), blob_len(msg));
387 rv = __rpc_iwinfo_open(tb[RPC_A_DEVICE]);
391 if (tb[RPC_A_MACADDR])
392 macaddr = ether_aton(blobmsg_data(tb[RPC_A_MACADDR]));
394 blob_buf_init(&buf, 0);
397 c = blobmsg_open_array(&buf, "results");
399 if (!iw->assoclist(ifname, res, &len) && (len > 0))
401 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
403 a = (struct iwinfo_assoclist_entry *)&res[i];
406 d = blobmsg_open_table(&buf, NULL);
407 else if (memcmp(macaddr, a->mac, ETH_ALEN) != 0)
410 snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
411 a->mac[0], a->mac[1], a->mac[2],
412 a->mac[3], a->mac[4], a->mac[5]);
414 blobmsg_add_string(&buf, "mac", mac);
415 blobmsg_add_u32(&buf, "signal", a->signal);
416 blobmsg_add_u32(&buf, "noise", a->noise);
417 blobmsg_add_u32(&buf, "inactive", a->inactive);
419 e = blobmsg_open_table(&buf, "rx");
420 blobmsg_add_u32(&buf, "rate", a->rx_rate.rate);
421 blobmsg_add_u32(&buf, "mcs", a->rx_rate.mcs);
422 blobmsg_add_u8(&buf, "40mhz", a->rx_rate.is_40mhz);
423 blobmsg_add_u8(&buf, "short_gi", a->rx_rate.is_short_gi);
424 blobmsg_close_table(&buf, e);
426 e = blobmsg_open_table(&buf, "tx");
427 blobmsg_add_u32(&buf, "rate", a->tx_rate.rate);
428 blobmsg_add_u32(&buf, "mcs", a->tx_rate.mcs);
429 blobmsg_add_u8(&buf, "40mhz", a->tx_rate.is_40mhz);
430 blobmsg_add_u8(&buf, "short_gi", a->tx_rate.is_short_gi);
431 blobmsg_close_table(&buf, e);
435 blobmsg_close_table(&buf, d);
442 blobmsg_close_array(&buf, c);
444 return UBUS_STATUS_NOT_FOUND;
446 ubus_send_reply(ctx, req, buf.head);
450 return UBUS_STATUS_OK;
454 rpc_iwinfo_freqlist(struct ubus_context *ctx, struct ubus_object *obj,
455 struct ubus_request_data *req, const char *method,
456 struct blob_attr *msg)
459 char res[IWINFO_BUFSIZE];
460 struct iwinfo_freqlist_entry *f;
463 rv = rpc_iwinfo_open(msg);
468 blob_buf_init(&buf, 0);
470 c = blobmsg_open_array(&buf, "results");
472 if (!iw->freqlist(ifname, res, &len) && (len > 0))
474 if (iw->channel(ifname, &ch))
477 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
479 f = (struct iwinfo_freqlist_entry *)&res[i];
480 d = blobmsg_open_table(&buf, NULL);
482 blobmsg_add_u32(&buf, "channel", f->channel);
483 blobmsg_add_u32(&buf, "mhz", f->mhz);
484 blobmsg_add_u8(&buf, "restricted", f->restricted);
487 blobmsg_add_u8(&buf, "active", f->channel == ch);
489 blobmsg_close_table(&buf, d);
493 blobmsg_close_array(&buf, c);
495 ubus_send_reply(ctx, req, buf.head);
499 return UBUS_STATUS_OK;
503 rpc_iwinfo_txpowerlist(struct ubus_context *ctx, struct ubus_object *obj,
504 struct ubus_request_data *req, const char *method,
505 struct blob_attr *msg)
507 int i, rv, len, pwr, off;
508 char res[IWINFO_BUFSIZE];
509 struct iwinfo_txpwrlist_entry *t;
512 rv = rpc_iwinfo_open(msg);
517 blob_buf_init(&buf, 0);
519 c = blobmsg_open_array(&buf, "results");
521 if (!iw->txpwrlist(ifname, res, &len) && (len > 0))
523 if (iw->txpower(ifname, &pwr))
526 if (iw->txpower_offset(ifname, &off))
529 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
531 t = (struct iwinfo_txpwrlist_entry *)&res[i];
532 d = blobmsg_open_table(&buf, NULL);
534 blobmsg_add_u32(&buf, "dbm", t->dbm + off);
535 blobmsg_add_u32(&buf, "mw", iwinfo_dbm2mw(t->dbm + off));
538 blobmsg_add_u8(&buf, "active", t->dbm == pwr);
540 blobmsg_close_table(&buf, d);
544 blobmsg_close_array(&buf, c);
546 ubus_send_reply(ctx, req, buf.head);
550 return UBUS_STATUS_OK;
554 rpc_iwinfo_lookup_country(char *buf, int len, int iso3166)
557 static char ccode[5];
558 struct iwinfo_country_entry *c;
560 for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
562 c = (struct iwinfo_country_entry *)&buf[i];
564 if (c->iso3166 == iso3166)
566 snprintf(ccode, sizeof(ccode), "%s", c->ccode);
575 rpc_iwinfo_countrylist(struct ubus_context *ctx, struct ubus_object *obj,
576 struct ubus_request_data *req, const char *method,
577 struct blob_attr *msg)
582 char res[IWINFO_BUFSIZE];
584 const struct iwinfo_iso3166_label *l;
587 rv = rpc_iwinfo_open(msg);
592 blob_buf_init(&buf, 0);
594 c = blobmsg_open_array(&buf, "results");
596 if (!iw->countrylist(ifname, res, &len) && (len > 0))
598 if (iw->country(ifname, cur))
599 memset(cur, 0, sizeof(cur));
601 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
603 ccode = rpc_iwinfo_lookup_country(res, len, l->iso3166);
608 d = blobmsg_open_table(&buf, NULL);
610 blobmsg_add_string(&buf, "code", ccode);
611 blobmsg_add_string(&buf, "country", (const char *)l->name);
613 snprintf(iso3166, sizeof(iso3166), "%c%c",
614 (l->iso3166 / 256), (l->iso3166 % 256));
616 blobmsg_add_string(&buf, "iso3166", iso3166);
619 blobmsg_add_u8(&buf, "active", !strncmp(ccode, cur, 2));
621 blobmsg_close_table(&buf, d);
625 blobmsg_close_array(&buf, c);
627 ubus_send_reply(ctx, req, buf.head);
631 return UBUS_STATUS_OK;
635 rpc_iwinfo_devices(struct ubus_context *ctx, struct ubus_object *obj,
636 struct ubus_request_data *req, const char *method,
637 struct blob_attr *msg)
643 d = opendir("/sys/class/net");
646 return UBUS_STATUS_UNKNOWN_ERROR;
648 blob_buf_init(&buf, 0);
650 c = blobmsg_open_array(&buf, "devices");
652 while ((e = readdir(d)) != NULL)
654 if (e->d_type != DT_LNK)
657 if (iwinfo_type(e->d_name))
658 blobmsg_add_string(&buf, NULL, e->d_name);
661 blobmsg_close_array(&buf, c);
665 ubus_send_reply(ctx, req, buf.head);
669 return UBUS_STATUS_OK;
674 rpc_iwinfo_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
676 static const struct ubus_method iwinfo_methods[] = {
677 UBUS_METHOD_NOARG("devices", rpc_iwinfo_devices),
678 UBUS_METHOD("info", rpc_iwinfo_info, rpc_device_policy),
679 UBUS_METHOD("scan", rpc_iwinfo_scan, rpc_device_policy),
680 UBUS_METHOD("assoclist", rpc_iwinfo_assoclist, rpc_assoclist_policy),
681 UBUS_METHOD("freqlist", rpc_iwinfo_freqlist, rpc_device_policy),
682 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist, rpc_device_policy),
683 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist, rpc_device_policy),
686 static struct ubus_object_type iwinfo_type =
687 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods);
689 static struct ubus_object obj = {
691 .type = &iwinfo_type,
692 .methods = iwinfo_methods,
693 .n_methods = ARRAY_SIZE(iwinfo_methods),
696 return ubus_add_object(ctx, &obj);
699 struct rpc_plugin rpc_plugin = {
700 .init = rpc_iwinfo_api_init