uci: add delete method
[project/rpcd.git] / iwinfo.c
1 /*
2  * luci-rpcd - LuCI UBUS RPC server
3  *
4  *   Copyright (C) 2013 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
22 #include "iwinfo.h"
23
24 static struct blob_buf buf;
25 static const struct iwinfo_ops *iw;
26 static const char *ifname;
27
28 enum {
29         RPC_D_DEVICE,
30         __RPC_D_MAX,
31 };
32
33 static const struct blobmsg_policy rpc_device_policy[__RPC_D_MAX] = {
34         [RPC_D_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
35 };
36
37
38 static int
39 rpc_iwinfo_open(struct blob_attr *msg)
40 {
41         static struct blob_attr *tb[__RPC_D_MAX];
42
43         blobmsg_parse(rpc_device_policy, __RPC_D_MAX, tb,
44                       blob_data(msg), blob_len(msg));
45
46         if (!tb[RPC_D_DEVICE])
47                 return UBUS_STATUS_INVALID_ARGUMENT;
48
49         ifname = blobmsg_data(tb[RPC_D_DEVICE]);
50         iw = iwinfo_backend(ifname);
51
52         return iw ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
53 }
54
55 static void
56 rpc_iwinfo_close(void)
57 {
58         iw = NULL;
59         ifname = NULL;
60         iwinfo_finish();
61 }
62
63 static void
64 rpc_iwinfo_call_int(const char *name, int (*func)(const char *, int *),
65                     const char **map)
66 {
67         int rv;
68
69         if (!func(ifname, &rv))
70         {
71                 if (!map)
72                         blobmsg_add_u32(&buf, name, rv);
73                 else
74                         blobmsg_add_string(&buf, name, map[rv]);
75         }
76 }
77
78 static void
79 rpc_iwinfo_call_hardware_id(const char *name)
80 {
81         struct iwinfo_hardware_id ids;
82         void *c;
83
84         if (!iw->hardware_id(ifname, (char *)&ids))
85         {
86                 c = blobmsg_open_array(&buf, name);
87
88                 blobmsg_add_u32(&buf, NULL, ids.vendor_id);
89                 blobmsg_add_u32(&buf, NULL, ids.device_id);
90                 blobmsg_add_u32(&buf, NULL, ids.subsystem_vendor_id);
91                 blobmsg_add_u32(&buf, NULL, ids.subsystem_device_id);
92
93                 blobmsg_close_array(&buf, c);
94         }
95 }
96
97 static void
98 rpc_iwinfo_add_encryption(const char *name, struct iwinfo_crypto_entry *e)
99 {
100         int ciph;
101         void *c, *d;
102
103         c = blobmsg_open_table(&buf, name);
104
105         blobmsg_add_u8(&buf, "enabled", e->enabled);
106
107         if (e->enabled)
108         {
109                 if (!e->wpa_version)
110                 {
111                         d = blobmsg_open_array(&buf, "wep");
112
113                         if (e->auth_algs & IWINFO_AUTH_OPEN)
114                                 blobmsg_add_string(&buf, NULL, "open");
115
116                         if (e->auth_algs & IWINFO_AUTH_SHARED)
117                                 blobmsg_add_string(&buf, NULL, "shared");
118
119                         blobmsg_close_array(&buf, d);
120                 }
121                 else
122                 {
123                         d = blobmsg_open_array(&buf, "wpa");
124
125                         if (e->wpa_version > 2)
126                         {
127                                 blobmsg_add_u32(&buf, NULL, 1);
128                                 blobmsg_add_u32(&buf, NULL, 2);
129                         }
130                         else
131                         {
132                                 blobmsg_add_u32(&buf, NULL, e->wpa_version);
133                         }
134
135                         blobmsg_close_array(&buf, d);
136
137
138                         d = blobmsg_open_array(&buf, "authentication");
139
140                         if (e->auth_suites & IWINFO_KMGMT_PSK)
141                                 blobmsg_add_string(&buf, NULL, "psk");
142
143                         if (e->auth_suites & IWINFO_KMGMT_8021x)
144                                 blobmsg_add_string(&buf, NULL, "802.1x");
145
146                         if (!e->auth_suites ||
147                                 (e->auth_suites & IWINFO_KMGMT_NONE))
148                                 blobmsg_add_string(&buf, NULL, "none");
149
150                         blobmsg_close_array(&buf, d);
151                 }
152
153                 d = blobmsg_open_array(&buf, "ciphers");
154                 ciph = e->pair_ciphers | e->group_ciphers;
155
156                 if (ciph & IWINFO_CIPHER_WEP40)
157                         blobmsg_add_string(&buf, NULL, "wep-40");
158
159                 if (ciph & IWINFO_CIPHER_WEP104)
160                         blobmsg_add_string(&buf, NULL, "wep-104");
161
162                 if (ciph & IWINFO_CIPHER_TKIP)
163                         blobmsg_add_string(&buf, NULL, "tkip");
164
165                 if (ciph & IWINFO_CIPHER_CCMP)
166                         blobmsg_add_string(&buf, NULL, "ccmp");
167
168                 if (ciph & IWINFO_CIPHER_WRAP)
169                         blobmsg_add_string(&buf, NULL, "wrap");
170
171                 if (ciph & IWINFO_CIPHER_AESOCB)
172                         blobmsg_add_string(&buf, NULL, "aes-ocb");
173
174                 if (ciph & IWINFO_CIPHER_CKIP)
175                         blobmsg_add_string(&buf, NULL, "ckip");
176
177                 if (!ciph || (ciph & IWINFO_CIPHER_NONE))
178                         blobmsg_add_string(&buf, NULL, "none");
179
180                 blobmsg_close_array(&buf, d);
181         }
182
183         blobmsg_close_table(&buf, c);
184 }
185
186 static void
187 rpc_iwinfo_call_encryption(const char *name)
188 {
189         struct iwinfo_crypto_entry crypto = { 0 };
190
191         if (!iw->encryption(ifname, (char *)&crypto))
192                 rpc_iwinfo_add_encryption(name, &crypto);
193 }
194
195 static void
196 rpc_iwinfo_call_hwmodes(const char *name)
197 {
198         int modes;
199         void *c;
200
201         if (!iw->hwmodelist(ifname, &modes))
202         {
203                 c = blobmsg_open_array(&buf, name);
204
205                 if (modes & IWINFO_80211_A)
206                         blobmsg_add_string(&buf, NULL, "a");
207
208                 if (modes & IWINFO_80211_B)
209                         blobmsg_add_string(&buf, NULL, "b");
210
211                 if (modes & IWINFO_80211_G)
212                         blobmsg_add_string(&buf, NULL, "g");
213
214                 if (modes & IWINFO_80211_N)
215                         blobmsg_add_string(&buf, NULL, "n");
216
217                 blobmsg_close_array(&buf, c);
218         }
219 }
220
221 static void
222 rpc_iwinfo_call_str(const char *name, int (*func)(const char *, char *))
223 {
224         char rv[IWINFO_BUFSIZE] = { 0 };
225
226         if (!func(ifname, rv))
227                 blobmsg_add_string(&buf, name, rv);
228 }
229
230 static int
231 rpc_iwinfo_info(struct ubus_context *ctx, struct ubus_object *obj,
232                 struct ubus_request_data *req, const char *method,
233                 struct blob_attr *msg)
234 {
235         int rv;
236         void *c;
237
238         rv = rpc_iwinfo_open(msg);
239
240         if (rv)
241                 return rv;
242
243         blob_buf_init(&buf, 0);
244
245         rpc_iwinfo_call_str("phy", iw->phyname);
246
247         rpc_iwinfo_call_str("ssid", iw->ssid);
248         rpc_iwinfo_call_str("bssid", iw->bssid);
249         rpc_iwinfo_call_str("country", iw->country);
250
251         rpc_iwinfo_call_int("mode", iw->mode, IWINFO_OPMODE_NAMES);
252         rpc_iwinfo_call_int("channel", iw->channel, NULL);
253
254         rpc_iwinfo_call_int("frequency", iw->frequency, NULL);
255         rpc_iwinfo_call_int("frequency_offset", iw->frequency_offset, NULL);
256
257         rpc_iwinfo_call_int("txpower", iw->txpower, NULL);
258         rpc_iwinfo_call_int("txpower_offset", iw->txpower_offset, NULL);
259
260         rpc_iwinfo_call_int("quality", iw->quality, NULL);
261         rpc_iwinfo_call_int("quality_max", iw->quality_max, NULL);
262
263         rpc_iwinfo_call_int("signal", iw->signal, NULL);
264         rpc_iwinfo_call_int("noise", iw->noise, NULL);
265
266         rpc_iwinfo_call_int("bitrate", iw->bitrate, NULL);
267
268         rpc_iwinfo_call_encryption("encryption");
269         rpc_iwinfo_call_hwmodes("hwmodes");
270
271         c = blobmsg_open_table(&buf, "hardware");
272         rpc_iwinfo_call_hardware_id("id");
273         rpc_iwinfo_call_str("name", iw->hardware_name);
274         blobmsg_close_table(&buf, c);
275
276         ubus_send_reply(ctx, req, buf.head);
277
278         rpc_iwinfo_close();
279
280         return UBUS_STATUS_OK;
281 }
282
283 static int
284 rpc_iwinfo_scan(struct ubus_context *ctx, struct ubus_object *obj,
285                 struct ubus_request_data *req, const char *method,
286                 struct blob_attr *msg)
287 {
288         int i, rv, len;
289         void *c, *d;
290         char mac[18];
291         char res[IWINFO_BUFSIZE];
292         struct iwinfo_scanlist_entry *e;
293
294         rv = rpc_iwinfo_open(msg);
295
296         if (rv)
297                 return rv;
298
299         blob_buf_init(&buf, 0);
300
301         c = blobmsg_open_array(&buf, "results");
302
303         if (!iw->scanlist(ifname, res, &len) && (len > 0))
304         {
305                 for (i = 0; i < len; i += sizeof(struct iwinfo_scanlist_entry))
306                 {
307                         e = (struct iwinfo_scanlist_entry *)&res[i];
308                         d = blobmsg_open_table(&buf, NULL);
309
310                         if (e->ssid[0])
311                                 blobmsg_add_string(&buf, "ssid", (const char *)e->ssid);
312
313                         snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
314                                          e->mac[0], e->mac[1], e->mac[2],
315                                          e->mac[3], e->mac[4], e->mac[5]);
316
317                         blobmsg_add_string(&buf, "bssid", mac);
318
319                         blobmsg_add_string(&buf, "mode", IWINFO_OPMODE_NAMES[e->mode]);
320
321                         blobmsg_add_u32(&buf, "channel", e->channel);
322                         blobmsg_add_u32(&buf, "signal", (uint32_t)(e->signal - 0x100));
323
324                         blobmsg_add_u32(&buf, "quality", e->quality);
325                         blobmsg_add_u32(&buf, "quality_max", e->quality_max);
326
327                         rpc_iwinfo_add_encryption("encryption", &e->crypto);
328
329                         blobmsg_close_table(&buf, d);
330                 }
331         }
332
333         blobmsg_close_array(&buf, c);
334
335         ubus_send_reply(ctx, req, buf.head);
336
337         rpc_iwinfo_close();
338
339         return UBUS_STATUS_OK;
340 }
341
342 static int
343 rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
344                      struct ubus_request_data *req, const char *method,
345                      struct blob_attr *msg)
346 {
347         int i, rv, len;
348         char mac[18];
349         char res[IWINFO_BUFSIZE];
350         struct iwinfo_assoclist_entry *a;
351         void *c, *d, *e;
352
353         rv = rpc_iwinfo_open(msg);
354
355         if (rv)
356                 return rv;
357
358         blob_buf_init(&buf, 0);
359
360         c = blobmsg_open_array(&buf, "results");
361
362         if (!iw->assoclist(ifname, res, &len) && (len > 0))
363         {
364                 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
365                 {
366                         a = (struct iwinfo_assoclist_entry *)&res[i];
367                         d = blobmsg_open_table(&buf, NULL);
368
369                         snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
370                                          a->mac[0], a->mac[1], a->mac[2],
371                                          a->mac[3], a->mac[4], a->mac[5]);
372
373                         blobmsg_add_string(&buf, "mac", mac);
374                         blobmsg_add_u32(&buf, "signal", a->signal);
375                         blobmsg_add_u32(&buf, "noise", a->noise);
376                         blobmsg_add_u32(&buf, "inactive", a->inactive);
377
378                         e = blobmsg_open_table(&buf, "rx");
379                         blobmsg_add_u32(&buf, "rate", a->rx_rate.rate);
380                         blobmsg_add_u32(&buf, "mcs",  a->rx_rate.mcs);
381                         blobmsg_add_u8(&buf, "40mhz", a->rx_rate.is_40mhz);
382                         blobmsg_add_u8(&buf, "short_gi", a->rx_rate.is_short_gi);
383                         blobmsg_close_table(&buf, e);
384
385                         e = blobmsg_open_table(&buf, "tx");
386                         blobmsg_add_u32(&buf, "rate", a->tx_rate.rate);
387                         blobmsg_add_u32(&buf, "mcs",  a->tx_rate.mcs);
388                         blobmsg_add_u8(&buf, "40mhz", a->tx_rate.is_40mhz);
389                         blobmsg_add_u8(&buf, "short_gi", a->tx_rate.is_short_gi);
390                         blobmsg_close_table(&buf, e);
391
392                         blobmsg_close_table(&buf, d);
393                 }
394         }
395
396         blobmsg_close_array(&buf, c);
397
398         ubus_send_reply(ctx, req, buf.head);
399
400         rpc_iwinfo_close();
401
402         return UBUS_STATUS_OK;
403 }
404
405 static int
406 rpc_iwinfo_freqlist(struct ubus_context *ctx, struct ubus_object *obj,
407                     struct ubus_request_data *req, const char *method,
408                     struct blob_attr *msg)
409 {
410         int i, rv, len, ch;
411         char res[IWINFO_BUFSIZE];
412         struct iwinfo_freqlist_entry *f;
413         void *c, *d;
414
415         rv = rpc_iwinfo_open(msg);
416
417         if (rv)
418                 return rv;
419
420         blob_buf_init(&buf, 0);
421
422         c = blobmsg_open_array(&buf, "results");
423
424         if (!iw->freqlist(ifname, res, &len) && (len > 0))
425         {
426                 if (iw->channel(ifname, &ch))
427                         ch = -1;
428
429                 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
430                 {
431                         f = (struct iwinfo_freqlist_entry *)&res[i];
432                         d = blobmsg_open_table(&buf, NULL);
433
434                         blobmsg_add_u32(&buf, "channel", f->channel);
435                         blobmsg_add_u32(&buf, "mhz", f->mhz);
436                         blobmsg_add_u8(&buf, "restricted", f->restricted);
437
438                         if (ch > -1)
439                                 blobmsg_add_u8(&buf, "active", f->channel == ch);
440
441                         blobmsg_close_table(&buf, d);
442                 }
443         }
444
445         blobmsg_close_array(&buf, c);
446
447         ubus_send_reply(ctx, req, buf.head);
448
449         rpc_iwinfo_close();
450
451         return UBUS_STATUS_OK;
452 }
453
454 static int
455 rpc_iwinfo_txpowerlist(struct ubus_context *ctx, struct ubus_object *obj,
456                        struct ubus_request_data *req, const char *method,
457                        struct blob_attr *msg)
458 {
459         int i, rv, len, pwr, off;
460         char res[IWINFO_BUFSIZE];
461         struct iwinfo_txpwrlist_entry *t;
462         void *c, *d;
463
464         rv = rpc_iwinfo_open(msg);
465
466         if (rv)
467                 return rv;
468
469         blob_buf_init(&buf, 0);
470
471         c = blobmsg_open_array(&buf, "results");
472
473         if (!iw->txpwrlist(ifname, res, &len) && (len > 0))
474         {
475                 if (iw->txpower(ifname, &pwr))
476                         pwr = -1;
477
478                 if (iw->txpower_offset(ifname, &off))
479                         off = 0;
480
481                 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
482                 {
483                         t = (struct iwinfo_txpwrlist_entry *)&res[i];
484                         d = blobmsg_open_table(&buf, NULL);
485
486                         blobmsg_add_u32(&buf, "dbm", t->dbm + off);
487                         blobmsg_add_u32(&buf, "mw", iwinfo_dbm2mw(t->dbm + off));
488
489                         if (pwr > -1)
490                                 blobmsg_add_u8(&buf, "active", t->dbm == pwr);
491
492                         blobmsg_close_table(&buf, d);
493                 }
494         }
495
496         blobmsg_close_array(&buf, c);
497
498         ubus_send_reply(ctx, req, buf.head);
499
500         rpc_iwinfo_close();
501
502         return UBUS_STATUS_OK;
503 }
504
505 static const char *
506 rpc_iwinfo_lookup_country(char *buf, int len, int iso3166)
507 {
508         int i;
509         static char ccode[5];
510         struct iwinfo_country_entry *c;
511
512         for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
513         {
514                 c = (struct iwinfo_country_entry *)&buf[i];
515
516                 if (c->iso3166 == iso3166)
517                 {
518                         snprintf(ccode, sizeof(ccode), "%s", c->ccode);
519                         return ccode;
520                 }
521         }
522
523         return NULL;
524 }
525
526 static int
527 rpc_iwinfo_countrylist(struct ubus_context *ctx, struct ubus_object *obj,
528                        struct ubus_request_data *req, const char *method,
529                        struct blob_attr *msg)
530 {
531         int rv, len;
532         char cur[3];
533         char iso3166[3];
534         char res[IWINFO_BUFSIZE];
535         const char *ccode;
536         const struct iwinfo_iso3166_label *l;
537         void *c, *d;
538
539         rv = rpc_iwinfo_open(msg);
540
541         if (rv)
542                 return rv;
543
544         blob_buf_init(&buf, 0);
545
546         c = blobmsg_open_array(&buf, "results");
547
548         if (!iw->countrylist(ifname, res, &len) && (len > 0))
549         {
550                 if (iw->country(ifname, cur))
551                         memset(cur, 0, sizeof(cur));
552
553                 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
554                 {
555                         ccode = rpc_iwinfo_lookup_country(res, len, l->iso3166);
556
557                         if (!ccode)
558                                 continue;
559
560                         d = blobmsg_open_table(&buf, NULL);
561
562                         blobmsg_add_string(&buf, "code", ccode);
563                         blobmsg_add_string(&buf, "country", (const char *)l->name);
564
565                         snprintf(iso3166, sizeof(iso3166), "%c%c",
566                                  (l->iso3166 / 256), (l->iso3166 % 256));
567
568                         blobmsg_add_string(&buf, "iso3166", iso3166);
569
570                         if (cur[0])
571                                 blobmsg_add_u8(&buf, "active", !strncmp(ccode, cur, 2));
572
573                         blobmsg_close_table(&buf, d);
574                 }
575         }
576
577         blobmsg_close_array(&buf, c);
578
579         ubus_send_reply(ctx, req, buf.head);
580
581         rpc_iwinfo_close();
582
583         return UBUS_STATUS_OK;
584 }
585
586 static int
587 rpc_iwinfo_devices(struct ubus_context *ctx, struct ubus_object *obj,
588                    struct ubus_request_data *req, const char *method,
589                    struct blob_attr *msg)
590 {
591         void *c;
592         struct dirent *e;
593         DIR *d;
594
595         d = opendir("/sys/class/net");
596
597         if (!d)
598                 return UBUS_STATUS_UNKNOWN_ERROR;
599
600         blob_buf_init(&buf, 0);
601
602         c = blobmsg_open_array(&buf, "devices");
603
604         while ((e = readdir(d)) != NULL)
605         {
606                 if (e->d_type != DT_LNK)
607                         continue;
608
609                 if (iwinfo_type(e->d_name))
610                         blobmsg_add_string(&buf, NULL, e->d_name);
611         }
612
613         blobmsg_close_array(&buf, c);
614
615         closedir(d);
616
617         ubus_send_reply(ctx, req, buf.head);
618
619         rpc_iwinfo_close();
620
621         return UBUS_STATUS_OK;
622 }
623
624
625 int rpc_iwinfo_api_init(struct ubus_context *ctx)
626 {
627         static const struct ubus_method iwinfo_methods[] = {
628                 { .name = "devices", .handler = rpc_iwinfo_devices },
629                 UBUS_METHOD("info",        rpc_iwinfo_info,        rpc_device_policy),
630                 UBUS_METHOD("scan",        rpc_iwinfo_scan,        rpc_device_policy),
631                 UBUS_METHOD("assoclist",   rpc_iwinfo_assoclist,   rpc_device_policy),
632                 UBUS_METHOD("freqlist",    rpc_iwinfo_freqlist,    rpc_device_policy),
633                 UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist, rpc_device_policy),
634                 UBUS_METHOD("countrylist", rpc_iwinfo_countrylist, rpc_device_policy),
635         };
636
637         static struct ubus_object_type iwinfo_type =
638                 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods);
639
640         static struct ubus_object obj = {
641                 .name = "iwinfo",
642                 .type = &iwinfo_type,
643                 .methods = iwinfo_methods,
644                 .n_methods = ARRAY_SIZE(iwinfo_methods),
645         };
646
647         return ubus_add_object(ctx, &obj);
648 }