d4ed8e222ae3210d6bb464072779577fce1b948e
[openwrt/openwrt.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 static int
100 wpas_bss_wps_start(struct ubus_context *ctx, struct ubus_object *obj,
101 struct ubus_request_data *req, const char *method,
102 struct blob_attr *msg)
103 {
104 int rc;
105 struct wpa_supplicant *wpa_s = get_wpas_from_object(obj);
106
107 rc = wpas_wps_start_pbc(wpa_s, NULL, 0);
108
109 if (rc != 0)
110 return UBUS_STATUS_NOT_SUPPORTED;
111
112 return 0;
113 }
114
115 static int
116 wpas_bss_wps_cancel(struct ubus_context *ctx, struct ubus_object *obj,
117 struct ubus_request_data *req, const char *method,
118 struct blob_attr *msg)
119 {
120 int rc;
121 struct wpa_supplicant *wpa_s = get_wpas_from_object(obj);
122
123 rc = wpas_wps_cancel(wpa_s);
124
125 if (rc != 0)
126 return UBUS_STATUS_NOT_SUPPORTED;
127
128 return 0;
129 }
130 #endif
131
132 static const struct ubus_method bss_methods[] = {
133 #ifdef CONFIG_WPS
134 UBUS_METHOD_NOARG("wps_start", wpas_bss_wps_start),
135 UBUS_METHOD_NOARG("wps_cancel", wpas_bss_wps_cancel),
136 #endif
137 UBUS_METHOD_NOARG("get_features", wpas_bss_get_features),
138 };
139
140 static struct ubus_object_type bss_object_type =
141 UBUS_OBJECT_TYPE("wpas_bss", bss_methods);
142
143 void wpas_ubus_add_bss(struct wpa_supplicant *wpa_s)
144 {
145 struct ubus_object *obj = &wpa_s->ubus.obj;
146 char *name;
147 int ret;
148
149 if (!wpas_ubus_init())
150 return;
151
152 if (asprintf(&name, "wpa_supplicant.%s", wpa_s->ifname) < 0)
153 return;
154
155 obj->name = name;
156 obj->type = &bss_object_type;
157 obj->methods = bss_object_type.methods;
158 obj->n_methods = bss_object_type.n_methods;
159 ret = ubus_add_object(ctx, obj);
160 wpas_ubus_ref_inc();
161 }
162
163 void wpas_ubus_free_bss(struct wpa_supplicant *wpa_s)
164 {
165 struct ubus_object *obj = &wpa_s->ubus.obj;
166 char *name = (char *) obj->name;
167
168 if (!ctx)
169 return;
170
171 if (obj->id) {
172 ubus_remove_object(ctx, obj);
173 wpas_ubus_ref_dec();
174 }
175
176 free(name);
177 }
178
179 #ifdef CONFIG_WPS
180 void wpas_ubus_notify(struct wpa_supplicant *wpa_s, const struct wps_credential *cred)
181 {
182 u16 auth_type;
183 char *ifname, *encryption, *ssid, *key;
184 size_t ifname_len;
185
186 if (!cred)
187 return;
188
189 auth_type = cred->auth_type;
190
191 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK))
192 auth_type = WPS_AUTH_WPA2PSK;
193
194 if (auth_type != WPS_AUTH_OPEN &&
195 auth_type != WPS_AUTH_WPAPSK &&
196 auth_type != WPS_AUTH_WPA2PSK) {
197 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
198 "unsupported authentication type 0x%x",
199 auth_type);
200 return;
201 }
202
203 if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
204 if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
205 wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
206 "invalid Network Key length %lu",
207 (unsigned long) cred->key_len);
208 return;
209 }
210 }
211
212 blob_buf_init(&b, 0);
213
214 ifname_len = strlen(wpa_s->ifname);
215 ifname = blobmsg_alloc_string_buffer(&b, "ifname", ifname_len + 1);
216 memcpy(ifname, wpa_s->ifname, ifname_len + 1);
217 ifname[ifname_len] = '\0';
218 blobmsg_add_string_buffer(&b);
219
220 switch (auth_type) {
221 case WPS_AUTH_WPA2PSK:
222 encryption = "psk2";
223 break;
224 case WPS_AUTH_WPAPSK:
225 encryption = "psk";
226 break;
227 default:
228 encryption = "none";
229 break;
230 }
231
232 blobmsg_add_string(&b, "encryption", encryption);
233
234 ssid = blobmsg_alloc_string_buffer(&b, "ssid", cred->ssid_len + 1);
235 memcpy(ssid, cred->ssid, cred->ssid_len);
236 ssid[cred->ssid_len] = '\0';
237 blobmsg_add_string_buffer(&b);
238
239 if (cred->key_len > 0) {
240 key = blobmsg_alloc_string_buffer(&b, "key", cred->key_len + 1);
241 memcpy(key, cred->key, cred->key_len);
242 key[cred->key_len] = '\0';
243 blobmsg_add_string_buffer(&b);
244 }
245
246 // ubus_notify(ctx, &wpa_s->ubus.obj, "wps_credentials", b.head, -1);
247 ubus_send_event(ctx, "wps_credentials", b.head);
248 }
249 #endif /* CONFIG_WPS */