5fdb57be7a0e3a971ee8cfcde4e9a1d0d489cece
[openwrt/staging/jow.git] / package / network / services / hostapd / src / wpa_supplicant / ubus.c
1 /*
2 * wpa_supplicant / ubus support
3 * Copyright (c) 2018, Daniel Golle <daniel@makrotopia.org>
4 * Copyright (c) 2013, Felix Fietkau <nbd@nbd.name>
5 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/wpabuf.h"
14 #include "common/ieee802_11_defs.h"
15 #include "wpa_supplicant_i.h"
16 #include "wps_supplicant.h"
17 #include "ubus.h"
18
19 static struct ubus_context *ctx;
20 static struct blob_buf b;
21 static int ctx_ref;
22
23 static inline struct wpa_supplicant *get_wpas_from_object(struct ubus_object *obj)
24 {
25 return container_of(obj, struct wpa_supplicant, ubus.obj);
26 }
27
28 static void ubus_receive(int sock, void *eloop_ctx, void *sock_ctx)
29 {
30 struct ubus_context *ctx = eloop_ctx;
31 ubus_handle_event(ctx);
32 }
33
34 static void ubus_reconnect_timeout(void *eloop_data, void *user_ctx)
35 {
36 if (ubus_reconnect(ctx, NULL)) {
37 eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
38 return;
39 }
40
41 eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
42 }
43
44 static void wpas_ubus_connection_lost(struct ubus_context *ctx)
45 {
46 eloop_unregister_read_sock(ctx->sock.fd);
47 eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
48 }
49
50 static bool wpas_ubus_init(void)
51 {
52 if (ctx)
53 return true;
54
55 ctx = ubus_connect(NULL);
56 if (!ctx)
57 return false;
58
59 ctx->connection_lost = wpas_ubus_connection_lost;
60 eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
61 return true;
62 }
63
64 static void wpas_ubus_ref_inc(void)
65 {
66 ctx_ref++;
67 }
68
69 static void wpas_ubus_ref_dec(void)
70 {
71 ctx_ref--;
72 if (!ctx)
73 return;
74
75 if (ctx_ref)
76 return;
77
78 eloop_unregister_read_sock(ctx->sock.fd);
79 ubus_free(ctx);
80 ctx = NULL;
81 }
82
83 static int
84 wpas_bss_get_features(struct ubus_context *ctx, struct ubus_object *obj,
85 struct ubus_request_data *req, const char *method,
86 struct blob_attr *msg)
87 {
88 struct wpa_supplicant *wpa_s = get_wpas_from_object(obj);
89
90 blob_buf_init(&b, 0);
91 blobmsg_add_u8(&b, "ht_supported", ht_supported(wpa_s->hw.modes));
92 blobmsg_add_u8(&b, "vht_supported", vht_supported(wpa_s->hw.modes));
93 ubus_send_reply(ctx, req, b.head);
94
95 return 0;
96 }
97
98 #ifdef CONFIG_WPS
99 enum {
100 WPS_START_MULTI_AP,
101 __WPS_START_MAX
102 };
103
104 static const struct blobmsg_policy wps_start_policy[] = {
105 [WPS_START_MULTI_AP] = { "multi_ap", BLOBMSG_TYPE_BOOL },
106 };
107
108 static int
109 wpas_bss_wps_start(struct ubus_context *ctx, struct ubus_object *obj,
110 struct ubus_request_data *req, const char *method,
111 struct blob_attr *msg)
112 {
113 int rc;
114 struct wpa_supplicant *wpa_s = get_wpas_from_object(obj);
115 struct blob_attr *tb[__WPS_START_MAX], *cur;
116 int multi_ap = 0;
117
118 blobmsg_parse(wps_start_policy, __WPS_START_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
119
120 if (tb[WPS_START_MULTI_AP])
121 multi_ap = blobmsg_get_bool(tb[WPS_START_MULTI_AP]);
122
123 rc = wpas_wps_start_pbc(wpa_s, NULL, 0, multi_ap);
124
125 if (rc != 0)
126 return UBUS_STATUS_NOT_SUPPORTED;
127
128 return 0;
129 }
130
131 static int
132 wpas_bss_wps_cancel(struct ubus_context *ctx, struct ubus_object *obj,
133 struct ubus_request_data *req, const char *method,
134 struct blob_attr *msg)
135 {
136 int rc;
137 struct wpa_supplicant *wpa_s = get_wpas_from_object(obj);
138
139 rc = wpas_wps_cancel(wpa_s);
140
141 if (rc != 0)
142 return UBUS_STATUS_NOT_SUPPORTED;
143
144 return 0;
145 }
146 #endif
147
148 static const struct ubus_method bss_methods[] = {
149 #ifdef CONFIG_WPS
150 UBUS_METHOD_NOARG("wps_start", wpas_bss_wps_start),
151 UBUS_METHOD_NOARG("wps_cancel", wpas_bss_wps_cancel),
152 #endif
153 UBUS_METHOD_NOARG("get_features", wpas_bss_get_features),
154 };
155
156 static struct ubus_object_type bss_object_type =
157 UBUS_OBJECT_TYPE("wpas_bss", bss_methods);
158
159 void wpas_ubus_add_bss(struct wpa_supplicant *wpa_s)
160 {
161 struct ubus_object *obj = &wpa_s->ubus.obj;
162 char *name;
163 int ret;
164
165 if (!wpas_ubus_init())
166 return;
167
168 if (asprintf(&name, "wpa_supplicant.%s", wpa_s->ifname) < 0)
169 return;
170
171 obj->name = name;
172 obj->type = &bss_object_type;
173 obj->methods = bss_object_type.methods;
174 obj->n_methods = bss_object_type.n_methods;
175 ret = ubus_add_object(ctx, obj);
176 wpas_ubus_ref_inc();
177 }
178
179 void wpas_ubus_free_bss(struct wpa_supplicant *wpa_s)
180 {
181 struct ubus_object *obj = &wpa_s->ubus.obj;
182 char *name = (char *) obj->name;
183
184 if (!ctx)
185 return;
186
187 if (obj->id) {
188 ubus_remove_object(ctx, obj);
189 wpas_ubus_ref_dec();
190 }
191
192 free(name);
193 }
194
195 #ifdef CONFIG_WPS
196 void wpas_ubus_notify(struct wpa_supplicant *wpa_s, const struct wps_credential *cred)
197 {
198 u16 auth_type;
199 char *ifname, *encryption, *ssid, *key;
200 size_t ifname_len;
201
202 if (!cred)
203 return;
204
205 auth_type = cred->auth_type;
206
207 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK))
208 auth_type = WPS_AUTH_WPA2PSK;
209
210 if (auth_type != WPS_AUTH_OPEN &&
211 auth_type != WPS_AUTH_WPAPSK &&
212 auth_type != WPS_AUTH_WPA2PSK) {
213 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
214 "unsupported authentication type 0x%x",
215 auth_type);
216 return;
217 }
218
219 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
220 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
221 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
222 "invalid Network Key length %lu",
223 (unsigned long) cred->key_len);
224 return;
225 }
226 }
227
228 blob_buf_init(&b, 0);
229
230 ifname_len = strlen(wpa_s->ifname);
231 ifname = blobmsg_alloc_string_buffer(&b, "ifname", ifname_len + 1);
232 memcpy(ifname, wpa_s->ifname, ifname_len + 1);
233 ifname[ifname_len] = '\0';
234 blobmsg_add_string_buffer(&b);
235
236 switch (auth_type) {
237 case WPS_AUTH_WPA2PSK:
238 encryption = "psk2";
239 break;
240 case WPS_AUTH_WPAPSK:
241 encryption = "psk";
242 break;
243 default:
244 encryption = "none";
245 break;
246 }
247
248 blobmsg_add_string(&b, "encryption", encryption);
249
250 ssid = blobmsg_alloc_string_buffer(&b, "ssid", cred->ssid_len + 1);
251 memcpy(ssid, cred->ssid, cred->ssid_len);
252 ssid[cred->ssid_len] = '\0';
253 blobmsg_add_string_buffer(&b);
254
255 if (cred->key_len > 0) {
256 key = blobmsg_alloc_string_buffer(&b, "key", cred->key_len + 1);
257 memcpy(key, cred->key, cred->key_len);
258 key[cred->key_len] = '\0';
259 blobmsg_add_string_buffer(&b);
260 }
261
262 // ubus_notify(ctx, &wpa_s->ubus.obj, "wps_credentials", b.head, -1);
263 ubus_send_event(ctx, "wps_credentials", b.head);
264 }
265 #endif /* CONFIG_WPS */