add ubus iwinfo api (lacks scanning, assoclist, countrylist and txpowerlist yet)
[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 <fcntl.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <limits.h>
25 #include <dirent.h>
26 #include <sys/stat.h>
27 #include <sys/wait.h>
28
29 #include "iwinfo.h"
30
31 static struct blob_buf buf;
32 static const struct iwinfo_ops *iw;
33 static const char *ifname;
34
35 enum {
36 RPC_D_DEVICE,
37 __RPC_D_MAX,
38 };
39
40 static const struct blobmsg_policy rpc_device_policy[__RPC_D_MAX] = {
41 [RPC_D_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
42 };
43
44
45 static int
46 rpc_iwinfo_open(struct blob_attr *msg)
47 {
48 static struct blob_attr *tb[__RPC_D_MAX];
49
50 blobmsg_parse(rpc_device_policy, __RPC_D_MAX, tb,
51 blob_data(msg), blob_len(msg));
52
53 if (!tb[RPC_D_DEVICE])
54 return UBUS_STATUS_INVALID_ARGUMENT;
55
56 ifname = blobmsg_data(tb[RPC_D_DEVICE]);
57 iw = iwinfo_backend(ifname);
58
59 return iw ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
60 }
61
62 static void
63 rpc_iwinfo_call_int(const char *name, int (*func)(const char *, int *),
64 const char **map)
65 {
66 int rv;
67
68 if (!func(ifname, &rv))
69 {
70 if (!map)
71 blobmsg_add_u32(&buf, name, rv);
72 else
73 blobmsg_add_string(&buf, name, map[rv]);
74 }
75 }
76
77 static void
78 rpc_iwinfo_call_hardware_id(const char *name)
79 {
80 struct iwinfo_hardware_id ids;
81 void *c;
82
83 if (!iw->hardware_id(ifname, (char *)&ids))
84 {
85 c = blobmsg_open_array(&buf, name);
86
87 blobmsg_add_u32(&buf, NULL, ids.vendor_id);
88 blobmsg_add_u32(&buf, NULL, ids.device_id);
89 blobmsg_add_u32(&buf, NULL, ids.subsystem_vendor_id);
90 blobmsg_add_u32(&buf, NULL, ids.subsystem_device_id);
91
92 blobmsg_close_array(&buf, c);
93 }
94 }
95
96 static void
97 rpc_iwinfo_call_encryption(const char *name)
98 {
99 struct iwinfo_crypto_entry crypto = { 0 };
100 void *c, *d;
101 int ciph;
102
103 if (!iw->encryption(ifname, (char *)&crypto))
104 {
105 c = blobmsg_open_table(&buf, name);
106
107 blobmsg_add_u8(&buf, "enabled", crypto.enabled);
108
109 if (crypto.enabled)
110 {
111 if (!crypto.wpa_version)
112 {
113 d = blobmsg_open_array(&buf, "wep");
114
115 if (crypto.auth_algs & IWINFO_AUTH_OPEN)
116 blobmsg_add_string(&buf, NULL, "open");
117
118 if (crypto.auth_algs & IWINFO_AUTH_SHARED)
119 blobmsg_add_string(&buf, NULL, "shared");
120
121 blobmsg_close_array(&buf, d);
122 }
123 else
124 {
125 d = blobmsg_open_array(&buf, "wpa");
126
127 if (crypto.wpa_version > 2)
128 {
129 blobmsg_add_u32(&buf, NULL, 1);
130 blobmsg_add_u32(&buf, NULL, 2);
131 }
132 else
133 {
134 blobmsg_add_u32(&buf, NULL, crypto.wpa_version);
135 }
136
137 blobmsg_close_array(&buf, d);
138
139
140 d = blobmsg_open_array(&buf, "authentication");
141
142 if (crypto.auth_suites & IWINFO_KMGMT_PSK)
143 blobmsg_add_string(&buf, NULL, "psk");
144
145 if (crypto.auth_suites & IWINFO_KMGMT_8021x)
146 blobmsg_add_string(&buf, NULL, "802.1x");
147
148 if (!crypto.auth_suites ||
149 (crypto.auth_suites & IWINFO_KMGMT_NONE))
150 blobmsg_add_string(&buf, NULL, "none");
151
152 blobmsg_close_array(&buf, d);
153 }
154
155 d = blobmsg_open_array(&buf, "ciphers");
156 ciph = crypto.pair_ciphers | crypto.group_ciphers;
157
158 if (ciph & IWINFO_CIPHER_WEP40)
159 blobmsg_add_string(&buf, NULL, "wep-40");
160
161 if (ciph & IWINFO_CIPHER_WEP104)
162 blobmsg_add_string(&buf, NULL, "wep-104");
163
164 if (ciph & IWINFO_CIPHER_TKIP)
165 blobmsg_add_string(&buf, NULL, "tkip");
166
167 if (ciph & IWINFO_CIPHER_CCMP)
168 blobmsg_add_string(&buf, NULL, "ccmp");
169
170 if (ciph & IWINFO_CIPHER_WRAP)
171 blobmsg_add_string(&buf, NULL, "wrap");
172
173 if (ciph & IWINFO_CIPHER_AESOCB)
174 blobmsg_add_string(&buf, NULL, "aes-ocb");
175
176 if (ciph & IWINFO_CIPHER_CKIP)
177 blobmsg_add_string(&buf, NULL, "ckip");
178
179 if (!ciph || (ciph & IWINFO_CIPHER_NONE))
180 blobmsg_add_string(&buf, NULL, "none");
181
182 blobmsg_close_array(&buf, d);
183 }
184
185 blobmsg_close_table(&buf, c);
186 }
187 }
188
189 static void
190 rpc_iwinfo_call_hwmodes(const char *name)
191 {
192 int modes;
193 void *c;
194
195 if (!iw->hwmodelist(ifname, &modes))
196 {
197 c = blobmsg_open_array(&buf, name);
198
199 if (modes & IWINFO_80211_A);
200 blobmsg_add_string(&buf, NULL, "a");
201
202 if (modes & IWINFO_80211_B);
203 blobmsg_add_string(&buf, NULL, "b");
204
205 if (modes & IWINFO_80211_G);
206 blobmsg_add_string(&buf, NULL, "g");
207
208 if (modes & IWINFO_80211_N);
209 blobmsg_add_string(&buf, NULL, "n");
210
211 blobmsg_close_array(&buf, c);
212 }
213 }
214
215 static void
216 rpc_iwinfo_call_str(const char *name, int (*func)(const char *, char *))
217 {
218 char rv[IWINFO_BUFSIZE] = { 0 };
219
220 if (!func(ifname, rv))
221 blobmsg_add_string(&buf, name, rv);
222 }
223
224 static int
225 rpc_iwinfo_info(struct ubus_context *ctx, struct ubus_object *obj,
226 struct ubus_request_data *req, const char *method,
227 struct blob_attr *msg)
228 {
229 int rv;
230 void *c;
231
232 rv = rpc_iwinfo_open(msg);
233
234 if (rv)
235 return rv;
236
237 blob_buf_init(&buf, 0);
238
239 rpc_iwinfo_call_str("ssid", iw->ssid);
240 rpc_iwinfo_call_str("bssid", iw->bssid);
241 rpc_iwinfo_call_str("country", iw->country);
242
243 rpc_iwinfo_call_int("mode", iw->mode, IWINFO_OPMODE_NAMES);
244 rpc_iwinfo_call_int("channel", iw->channel, NULL);
245
246 rpc_iwinfo_call_int("frequency", iw->frequency, NULL);
247 rpc_iwinfo_call_int("frequency_offset", iw->frequency_offset, NULL);
248
249 rpc_iwinfo_call_int("txpower", iw->txpower, NULL);
250 rpc_iwinfo_call_int("txpower_offset", iw->txpower_offset, NULL);
251
252 rpc_iwinfo_call_int("quality", iw->quality, NULL);
253 rpc_iwinfo_call_int("quality_max", iw->quality_max, NULL);
254
255 rpc_iwinfo_call_int("signal", iw->signal, NULL);
256 rpc_iwinfo_call_int("noise", iw->noise, NULL);
257
258 rpc_iwinfo_call_int("bitrate", iw->bitrate, NULL);
259
260 rpc_iwinfo_call_encryption("encryption");
261 rpc_iwinfo_call_hwmodes("hwmodes");
262
263 c = blobmsg_open_table(&buf, "hardware");
264 rpc_iwinfo_call_hardware_id("id");
265 rpc_iwinfo_call_str("name", iw->hardware_name);
266 blobmsg_close_table(&buf, c);
267
268 ubus_send_reply(ctx, req, buf.head);
269
270 iw->close();
271
272 return UBUS_STATUS_OK;
273 }
274
275
276 int rpc_iwinfo_api_init(struct ubus_context *ctx)
277 {
278 static const struct ubus_method iwinfo_methods[] = {
279 UBUS_METHOD("info", rpc_iwinfo_info, rpc_device_policy),
280 };
281
282 static struct ubus_object_type iwinfo_type =
283 UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods);
284
285 static struct ubus_object obj = {
286 .name = "iwinfo",
287 .type = &iwinfo_type,
288 .methods = iwinfo_methods,
289 .n_methods = ARRAY_SIZE(iwinfo_methods),
290 };
291
292 return ubus_add_object(ctx, &obj);
293 }