]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * WPA Supplicant / UNIX domain socket -based control interface | |
29257565 | 3 | * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi> |
6fc6879b | 4 | * |
0f3d578e JM |
5 | * This software may be distributed under the terms of the BSD license. |
6 | * See README for more details. | |
6fc6879b JM |
7 | */ |
8 | ||
9 | #include "includes.h" | |
10 | #include <sys/un.h> | |
11 | #include <sys/stat.h> | |
12 | #include <grp.h> | |
19b9436c | 13 | #include <stddef.h> |
4fdc8def BG |
14 | #include <unistd.h> |
15 | #include <fcntl.h> | |
b3f3865e DS |
16 | #ifdef ANDROID |
17 | #include <cutils/sockets.h> | |
18 | #endif /* ANDROID */ | |
6fc6879b | 19 | |
09e47a07 JM |
20 | #include "utils/common.h" |
21 | #include "utils/eloop.h" | |
22 | #include "utils/list.h" | |
6fc6879b | 23 | #include "eapol_supp/eapol_supp_sm.h" |
09e47a07 | 24 | #include "config.h" |
6fc6879b JM |
25 | #include "wpa_supplicant_i.h" |
26 | #include "ctrl_iface.h" | |
27 | ||
28 | /* Per-interface ctrl_iface */ | |
29 | ||
30 | /** | |
31 | * struct wpa_ctrl_dst - Internal data structure of control interface monitors | |
32 | * | |
33 | * This structure is used to store information about registered control | |
34 | * interface monitors into struct wpa_supplicant. This data is private to | |
35 | * ctrl_iface_unix.c and should not be touched directly from other files. | |
36 | */ | |
37 | struct wpa_ctrl_dst { | |
09e47a07 | 38 | struct dl_list list; |
6fc6879b JM |
39 | struct sockaddr_un addr; |
40 | socklen_t addrlen; | |
41 | int debug_level; | |
42 | int errors; | |
43 | }; | |
44 | ||
45 | ||
46 | struct ctrl_iface_priv { | |
47 | struct wpa_supplicant *wpa_s; | |
48 | int sock; | |
09e47a07 | 49 | struct dl_list ctrl_dst; |
6fc6879b JM |
50 | }; |
51 | ||
52 | ||
214e428b JM |
53 | struct ctrl_iface_global_priv { |
54 | struct wpa_global *global; | |
55 | int sock; | |
56 | struct dl_list ctrl_dst; | |
57 | }; | |
58 | ||
59 | ||
60 | static void wpa_supplicant_ctrl_iface_send(const char *ifname, int sock, | |
61 | struct dl_list *ctrl_dst, | |
6fc6879b JM |
62 | int level, const char *buf, |
63 | size_t len); | |
64 | ||
65 | ||
214e428b | 66 | static int wpa_supplicant_ctrl_iface_attach(struct dl_list *ctrl_dst, |
6fc6879b JM |
67 | struct sockaddr_un *from, |
68 | socklen_t fromlen) | |
69 | { | |
70 | struct wpa_ctrl_dst *dst; | |
71 | ||
72 | dst = os_zalloc(sizeof(*dst)); | |
73 | if (dst == NULL) | |
74 | return -1; | |
75 | os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un)); | |
76 | dst->addrlen = fromlen; | |
77 | dst->debug_level = MSG_INFO; | |
214e428b | 78 | dl_list_add(ctrl_dst, &dst->list); |
6fc6879b | 79 | wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached", |
19b9436c SL |
80 | (u8 *) from->sun_path, |
81 | fromlen - offsetof(struct sockaddr_un, sun_path)); | |
6fc6879b JM |
82 | return 0; |
83 | } | |
84 | ||
85 | ||
214e428b | 86 | static int wpa_supplicant_ctrl_iface_detach(struct dl_list *ctrl_dst, |
6fc6879b JM |
87 | struct sockaddr_un *from, |
88 | socklen_t fromlen) | |
89 | { | |
09e47a07 | 90 | struct wpa_ctrl_dst *dst; |
6fc6879b | 91 | |
214e428b | 92 | dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) { |
6fc6879b JM |
93 | if (fromlen == dst->addrlen && |
94 | os_memcmp(from->sun_path, dst->addr.sun_path, | |
19b9436c SL |
95 | fromlen - offsetof(struct sockaddr_un, sun_path)) |
96 | == 0) { | |
6fc6879b JM |
97 | wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached", |
98 | (u8 *) from->sun_path, | |
19b9436c SL |
99 | fromlen - |
100 | offsetof(struct sockaddr_un, sun_path)); | |
a235aca3 JM |
101 | dl_list_del(&dst->list); |
102 | os_free(dst); | |
6fc6879b JM |
103 | return 0; |
104 | } | |
6fc6879b JM |
105 | } |
106 | return -1; | |
107 | } | |
108 | ||
109 | ||
110 | static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv, | |
111 | struct sockaddr_un *from, | |
112 | socklen_t fromlen, | |
113 | char *level) | |
114 | { | |
115 | struct wpa_ctrl_dst *dst; | |
116 | ||
117 | wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level); | |
118 | ||
09e47a07 | 119 | dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) { |
6fc6879b JM |
120 | if (fromlen == dst->addrlen && |
121 | os_memcmp(from->sun_path, dst->addr.sun_path, | |
19b9436c SL |
122 | fromlen - offsetof(struct sockaddr_un, sun_path)) |
123 | == 0) { | |
6fc6879b JM |
124 | wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor " |
125 | "level", (u8 *) from->sun_path, | |
19b9436c SL |
126 | fromlen - |
127 | offsetof(struct sockaddr_un, sun_path)); | |
6fc6879b JM |
128 | dst->debug_level = atoi(level); |
129 | return 0; | |
130 | } | |
6fc6879b JM |
131 | } |
132 | ||
133 | return -1; | |
134 | } | |
135 | ||
136 | ||
137 | static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx, | |
138 | void *sock_ctx) | |
139 | { | |
140 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
141 | struct ctrl_iface_priv *priv = sock_ctx; | |
b563b388 | 142 | char buf[4096]; |
6fc6879b JM |
143 | int res; |
144 | struct sockaddr_un from; | |
145 | socklen_t fromlen = sizeof(from); | |
146 | char *reply = NULL; | |
147 | size_t reply_len = 0; | |
148 | int new_attached = 0; | |
149 | ||
150 | res = recvfrom(sock, buf, sizeof(buf) - 1, 0, | |
151 | (struct sockaddr *) &from, &fromlen); | |
152 | if (res < 0) { | |
153 | perror("recvfrom(ctrl_iface)"); | |
154 | return; | |
155 | } | |
156 | buf[res] = '\0'; | |
157 | ||
158 | if (os_strcmp(buf, "ATTACH") == 0) { | |
214e428b JM |
159 | if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from, |
160 | fromlen)) | |
6fc6879b JM |
161 | reply_len = 1; |
162 | else { | |
163 | new_attached = 1; | |
164 | reply_len = 2; | |
165 | } | |
166 | } else if (os_strcmp(buf, "DETACH") == 0) { | |
214e428b JM |
167 | if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from, |
168 | fromlen)) | |
6fc6879b JM |
169 | reply_len = 1; |
170 | else | |
171 | reply_len = 2; | |
172 | } else if (os_strncmp(buf, "LEVEL ", 6) == 0) { | |
173 | if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen, | |
174 | buf + 6)) | |
175 | reply_len = 1; | |
176 | else | |
177 | reply_len = 2; | |
178 | } else { | |
179 | reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf, | |
180 | &reply_len); | |
181 | } | |
182 | ||
183 | if (reply) { | |
184 | sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, | |
185 | fromlen); | |
186 | os_free(reply); | |
187 | } else if (reply_len == 1) { | |
188 | sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, | |
189 | fromlen); | |
190 | } else if (reply_len == 2) { | |
191 | sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from, | |
192 | fromlen); | |
193 | } | |
194 | ||
195 | if (new_attached) | |
196 | eapol_sm_notify_ctrl_attached(wpa_s->eapol); | |
197 | } | |
198 | ||
199 | ||
200 | static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s) | |
201 | { | |
202 | char *buf; | |
203 | size_t len; | |
204 | char *pbuf, *dir = NULL, *gid_str = NULL; | |
205 | int res; | |
206 | ||
207 | if (wpa_s->conf->ctrl_interface == NULL) | |
208 | return NULL; | |
209 | ||
210 | pbuf = os_strdup(wpa_s->conf->ctrl_interface); | |
211 | if (pbuf == NULL) | |
212 | return NULL; | |
213 | if (os_strncmp(pbuf, "DIR=", 4) == 0) { | |
214 | dir = pbuf + 4; | |
215 | gid_str = os_strstr(dir, " GROUP="); | |
216 | if (gid_str) { | |
217 | *gid_str = '\0'; | |
218 | gid_str += 7; | |
219 | } | |
220 | } else | |
221 | dir = pbuf; | |
222 | ||
223 | len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2; | |
224 | buf = os_malloc(len); | |
225 | if (buf == NULL) { | |
226 | os_free(pbuf); | |
227 | return NULL; | |
228 | } | |
229 | ||
230 | res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname); | |
231 | if (res < 0 || (size_t) res >= len) { | |
232 | os_free(pbuf); | |
233 | os_free(buf); | |
234 | return NULL; | |
235 | } | |
236 | #ifdef __CYGWIN__ | |
237 | { | |
238 | /* Windows/WinPcap uses interface names that are not suitable | |
239 | * as a file name - convert invalid chars to underscores */ | |
240 | char *pos = buf; | |
241 | while (*pos) { | |
242 | if (*pos == '\\') | |
243 | *pos = '_'; | |
244 | pos++; | |
245 | } | |
246 | } | |
247 | #endif /* __CYGWIN__ */ | |
248 | os_free(pbuf); | |
249 | return buf; | |
250 | } | |
251 | ||
252 | ||
47bfe49c | 253 | static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level, int global, |
6fc6879b JM |
254 | const char *txt, size_t len) |
255 | { | |
256 | struct wpa_supplicant *wpa_s = ctx; | |
214e428b JM |
257 | |
258 | if (wpa_s == NULL) | |
6fc6879b | 259 | return; |
214e428b | 260 | |
c4bf83a7 | 261 | if (global != 2 && wpa_s->global->ctrl_iface) { |
214e428b JM |
262 | struct ctrl_iface_global_priv *priv = wpa_s->global->ctrl_iface; |
263 | if (!dl_list_empty(&priv->ctrl_dst)) { | |
47bfe49c JM |
264 | wpa_supplicant_ctrl_iface_send(global ? NULL : |
265 | wpa_s->ifname, | |
214e428b JM |
266 | priv->sock, |
267 | &priv->ctrl_dst, | |
268 | level, txt, len); | |
269 | } | |
270 | } | |
271 | ||
272 | if (wpa_s->ctrl_iface == NULL) | |
273 | return; | |
274 | wpa_supplicant_ctrl_iface_send(NULL, wpa_s->ctrl_iface->sock, | |
275 | &wpa_s->ctrl_iface->ctrl_dst, | |
276 | level, txt, len); | |
6fc6879b JM |
277 | } |
278 | ||
279 | ||
280 | struct ctrl_iface_priv * | |
281 | wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s) | |
282 | { | |
283 | struct ctrl_iface_priv *priv; | |
284 | struct sockaddr_un addr; | |
285 | char *fname = NULL; | |
286 | gid_t gid = 0; | |
287 | int gid_set = 0; | |
288 | char *buf, *dir = NULL, *gid_str = NULL; | |
289 | struct group *grp; | |
290 | char *endp; | |
4fdc8def | 291 | int flags; |
6fc6879b JM |
292 | |
293 | priv = os_zalloc(sizeof(*priv)); | |
294 | if (priv == NULL) | |
295 | return NULL; | |
09e47a07 | 296 | dl_list_init(&priv->ctrl_dst); |
6fc6879b JM |
297 | priv->wpa_s = wpa_s; |
298 | priv->sock = -1; | |
299 | ||
300 | if (wpa_s->conf->ctrl_interface == NULL) | |
301 | return priv; | |
302 | ||
303 | buf = os_strdup(wpa_s->conf->ctrl_interface); | |
304 | if (buf == NULL) | |
305 | goto fail; | |
b3f3865e DS |
306 | #ifdef ANDROID |
307 | os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s", | |
308 | wpa_s->conf->ctrl_interface); | |
309 | priv->sock = android_get_control_socket(addr.sun_path); | |
310 | if (priv->sock >= 0) | |
311 | goto havesock; | |
312 | #endif /* ANDROID */ | |
6fc6879b JM |
313 | if (os_strncmp(buf, "DIR=", 4) == 0) { |
314 | dir = buf + 4; | |
315 | gid_str = os_strstr(dir, " GROUP="); | |
316 | if (gid_str) { | |
317 | *gid_str = '\0'; | |
318 | gid_str += 7; | |
319 | } | |
320 | } else { | |
321 | dir = buf; | |
322 | gid_str = wpa_s->conf->ctrl_interface_group; | |
323 | } | |
324 | ||
325 | if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) { | |
326 | if (errno == EEXIST) { | |
327 | wpa_printf(MSG_DEBUG, "Using existing control " | |
328 | "interface directory."); | |
329 | } else { | |
330 | perror("mkdir[ctrl_interface]"); | |
331 | goto fail; | |
332 | } | |
333 | } | |
334 | ||
d49ea682 JM |
335 | #ifdef ANDROID |
336 | /* | |
337 | * wpa_supplicant is started from /init.*.rc on Android and that seems | |
338 | * to be using umask 0077 which would leave the control interface | |
339 | * directory without group access. This breaks things since Wi-Fi | |
340 | * framework assumes that this directory can be accessed by other | |
341 | * applications in the wifi group. Fix this by adding group access even | |
342 | * if umask value would prevent this. | |
343 | */ | |
344 | if (chmod(dir, S_IRWXU | S_IRWXG) < 0) { | |
345 | wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s", | |
346 | strerror(errno)); | |
347 | /* Try to continue anyway */ | |
348 | } | |
349 | #endif /* ANDROID */ | |
350 | ||
6fc6879b JM |
351 | if (gid_str) { |
352 | grp = getgrnam(gid_str); | |
353 | if (grp) { | |
354 | gid = grp->gr_gid; | |
355 | gid_set = 1; | |
356 | wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d" | |
357 | " (from group name '%s')", | |
358 | (int) gid, gid_str); | |
359 | } else { | |
360 | /* Group name not found - try to parse this as gid */ | |
361 | gid = strtol(gid_str, &endp, 10); | |
362 | if (*gid_str == '\0' || *endp != '\0') { | |
8e888179 | 363 | wpa_printf(MSG_ERROR, "CTRL: Invalid group " |
6fc6879b JM |
364 | "'%s'", gid_str); |
365 | goto fail; | |
366 | } | |
367 | gid_set = 1; | |
368 | wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d", | |
369 | (int) gid); | |
370 | } | |
371 | } | |
372 | ||
373 | if (gid_set && chown(dir, -1, gid) < 0) { | |
374 | perror("chown[ctrl_interface]"); | |
375 | goto fail; | |
376 | } | |
377 | ||
3fd2a226 AAS |
378 | /* Make sure the group can enter and read the directory */ |
379 | if (gid_set && | |
380 | chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) { | |
381 | wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s", | |
382 | strerror(errno)); | |
383 | goto fail; | |
384 | } | |
385 | ||
6fc6879b JM |
386 | if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >= |
387 | sizeof(addr.sun_path)) { | |
388 | wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded"); | |
389 | goto fail; | |
390 | } | |
391 | ||
392 | priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0); | |
393 | if (priv->sock < 0) { | |
394 | perror("socket(PF_UNIX)"); | |
395 | goto fail; | |
396 | } | |
397 | ||
398 | os_memset(&addr, 0, sizeof(addr)); | |
09bd6e8c | 399 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
19b9436c SL |
400 | addr.sun_len = sizeof(addr); |
401 | #endif /* __FreeBSD__ */ | |
6fc6879b JM |
402 | addr.sun_family = AF_UNIX; |
403 | fname = wpa_supplicant_ctrl_iface_path(wpa_s); | |
404 | if (fname == NULL) | |
405 | goto fail; | |
406 | os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path)); | |
407 | if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) { | |
408 | wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s", | |
409 | strerror(errno)); | |
410 | if (connect(priv->sock, (struct sockaddr *) &addr, | |
411 | sizeof(addr)) < 0) { | |
412 | wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not" | |
413 | " allow connections - assuming it was left" | |
414 | "over from forced program termination"); | |
415 | if (unlink(fname) < 0) { | |
416 | perror("unlink[ctrl_iface]"); | |
417 | wpa_printf(MSG_ERROR, "Could not unlink " | |
418 | "existing ctrl_iface socket '%s'", | |
419 | fname); | |
420 | goto fail; | |
421 | } | |
422 | if (bind(priv->sock, (struct sockaddr *) &addr, | |
423 | sizeof(addr)) < 0) { | |
9d053747 | 424 | perror("supp-ctrl-iface-init: bind(PF_UNIX)"); |
6fc6879b JM |
425 | goto fail; |
426 | } | |
427 | wpa_printf(MSG_DEBUG, "Successfully replaced leftover " | |
428 | "ctrl_iface socket '%s'", fname); | |
429 | } else { | |
430 | wpa_printf(MSG_INFO, "ctrl_iface exists and seems to " | |
431 | "be in use - cannot override it"); | |
432 | wpa_printf(MSG_INFO, "Delete '%s' manually if it is " | |
433 | "not used anymore", fname); | |
434 | os_free(fname); | |
435 | fname = NULL; | |
436 | goto fail; | |
437 | } | |
438 | } | |
439 | ||
440 | if (gid_set && chown(fname, -1, gid) < 0) { | |
441 | perror("chown[ctrl_interface/ifname]"); | |
442 | goto fail; | |
443 | } | |
444 | ||
445 | if (chmod(fname, S_IRWXU | S_IRWXG) < 0) { | |
446 | perror("chmod[ctrl_interface/ifname]"); | |
447 | goto fail; | |
448 | } | |
449 | os_free(fname); | |
450 | ||
b3f3865e DS |
451 | #ifdef ANDROID |
452 | havesock: | |
453 | #endif /* ANDROID */ | |
4fdc8def BG |
454 | |
455 | /* | |
456 | * Make socket non-blocking so that we don't hang forever if | |
457 | * target dies unexpectedly. | |
458 | */ | |
459 | flags = fcntl(priv->sock, F_GETFL); | |
460 | if (flags >= 0) { | |
461 | flags |= O_NONBLOCK; | |
462 | if (fcntl(priv->sock, F_SETFL, flags) < 0) { | |
463 | perror("fcntl(ctrl, O_NONBLOCK)"); | |
464 | /* Not fatal, continue on.*/ | |
465 | } | |
466 | } | |
467 | ||
6fc6879b JM |
468 | eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive, |
469 | wpa_s, priv); | |
470 | wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb); | |
471 | ||
472 | os_free(buf); | |
473 | return priv; | |
474 | ||
475 | fail: | |
476 | if (priv->sock >= 0) | |
477 | close(priv->sock); | |
478 | os_free(priv); | |
479 | if (fname) { | |
480 | unlink(fname); | |
481 | os_free(fname); | |
482 | } | |
483 | os_free(buf); | |
484 | return NULL; | |
485 | } | |
486 | ||
487 | ||
488 | void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv) | |
489 | { | |
490 | struct wpa_ctrl_dst *dst, *prev; | |
491 | ||
492 | if (priv->sock > -1) { | |
493 | char *fname; | |
494 | char *buf, *dir = NULL, *gid_str = NULL; | |
495 | eloop_unregister_read_sock(priv->sock); | |
09e47a07 | 496 | if (!dl_list_empty(&priv->ctrl_dst)) { |
6fc6879b JM |
497 | /* |
498 | * Wait a second before closing the control socket if | |
499 | * there are any attached monitors in order to allow | |
500 | * them to receive any pending messages. | |
501 | */ | |
502 | wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached " | |
503 | "monitors to receive messages"); | |
504 | os_sleep(1, 0); | |
505 | } | |
506 | close(priv->sock); | |
507 | priv->sock = -1; | |
508 | fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s); | |
509 | if (fname) { | |
510 | unlink(fname); | |
511 | os_free(fname); | |
512 | } | |
513 | ||
514 | buf = os_strdup(priv->wpa_s->conf->ctrl_interface); | |
515 | if (buf == NULL) | |
516 | goto free_dst; | |
517 | if (os_strncmp(buf, "DIR=", 4) == 0) { | |
518 | dir = buf + 4; | |
519 | gid_str = os_strstr(dir, " GROUP="); | |
520 | if (gid_str) { | |
521 | *gid_str = '\0'; | |
522 | gid_str += 7; | |
523 | } | |
524 | } else | |
525 | dir = buf; | |
526 | ||
527 | if (rmdir(dir) < 0) { | |
528 | if (errno == ENOTEMPTY) { | |
529 | wpa_printf(MSG_DEBUG, "Control interface " | |
530 | "directory not empty - leaving it " | |
531 | "behind"); | |
532 | } else { | |
533 | perror("rmdir[ctrl_interface]"); | |
534 | } | |
535 | } | |
536 | os_free(buf); | |
537 | } | |
538 | ||
539 | free_dst: | |
09e47a07 JM |
540 | dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst, |
541 | list) | |
542 | os_free(dst); | |
6fc6879b JM |
543 | os_free(priv); |
544 | } | |
545 | ||
546 | ||
547 | /** | |
548 | * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors | |
214e428b JM |
549 | * @ifname: Interface name for global control socket or %NULL |
550 | * @sock: Local socket fd | |
551 | * @ctrl_dst: List of attached listeners | |
6fc6879b JM |
552 | * @level: Priority level of the message |
553 | * @buf: Message data | |
554 | * @len: Message length | |
555 | * | |
556 | * Send a packet to all monitor programs attached to the control interface. | |
557 | */ | |
214e428b JM |
558 | static void wpa_supplicant_ctrl_iface_send(const char *ifname, int sock, |
559 | struct dl_list *ctrl_dst, | |
6fc6879b JM |
560 | int level, const char *buf, |
561 | size_t len) | |
562 | { | |
563 | struct wpa_ctrl_dst *dst, *next; | |
564 | char levelstr[10]; | |
565 | int idx, res; | |
566 | struct msghdr msg; | |
214e428b | 567 | struct iovec io[5]; |
6fc6879b | 568 | |
214e428b | 569 | if (sock < 0 || dl_list_empty(ctrl_dst)) |
6fc6879b JM |
570 | return; |
571 | ||
572 | res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level); | |
573 | if (res < 0 || (size_t) res >= sizeof(levelstr)) | |
574 | return; | |
214e428b JM |
575 | idx = 0; |
576 | if (ifname) { | |
577 | io[idx].iov_base = "IFNAME="; | |
578 | io[idx].iov_len = 7; | |
579 | idx++; | |
580 | io[idx].iov_base = (char *) ifname; | |
581 | io[idx].iov_len = os_strlen(ifname); | |
582 | idx++; | |
583 | io[idx].iov_base = " "; | |
584 | io[idx].iov_len = 1; | |
585 | idx++; | |
586 | } | |
587 | io[idx].iov_base = levelstr; | |
588 | io[idx].iov_len = os_strlen(levelstr); | |
589 | idx++; | |
590 | io[idx].iov_base = (char *) buf; | |
591 | io[idx].iov_len = len; | |
592 | idx++; | |
6fc6879b JM |
593 | os_memset(&msg, 0, sizeof(msg)); |
594 | msg.msg_iov = io; | |
214e428b | 595 | msg.msg_iovlen = idx; |
6fc6879b JM |
596 | |
597 | idx = 0; | |
214e428b | 598 | dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) { |
6fc6879b JM |
599 | if (level >= dst->debug_level) { |
600 | wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send", | |
601 | (u8 *) dst->addr.sun_path, dst->addrlen - | |
19b9436c | 602 | offsetof(struct sockaddr_un, sun_path)); |
6fc6879b JM |
603 | msg.msg_name = (void *) &dst->addr; |
604 | msg.msg_namelen = dst->addrlen; | |
b62b29ea | 605 | if (sendmsg(sock, &msg, MSG_DONTWAIT) < 0) { |
c5aaa015 JM |
606 | int _errno = errno; |
607 | wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: " | |
608 | "%d - %s", | |
609 | idx, errno, strerror(errno)); | |
6fc6879b | 610 | dst->errors++; |
6700a277 JM |
611 | if (dst->errors > 1000 || |
612 | (_errno != ENOBUFS && dst->errors > 10) || | |
613 | _errno == ENOENT) { | |
6fc6879b | 614 | wpa_supplicant_ctrl_iface_detach( |
214e428b | 615 | ctrl_dst, &dst->addr, |
6fc6879b JM |
616 | dst->addrlen); |
617 | } | |
618 | } else | |
619 | dst->errors = 0; | |
620 | } | |
621 | idx++; | |
6fc6879b JM |
622 | } |
623 | } | |
624 | ||
625 | ||
626 | void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv) | |
627 | { | |
628 | char buf[256]; | |
629 | int res; | |
630 | struct sockaddr_un from; | |
631 | socklen_t fromlen = sizeof(from); | |
632 | ||
633 | for (;;) { | |
634 | wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to " | |
635 | "attach", priv->wpa_s->ifname); | |
636 | eloop_wait_for_read_sock(priv->sock); | |
637 | ||
638 | res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0, | |
639 | (struct sockaddr *) &from, &fromlen); | |
640 | if (res < 0) { | |
641 | perror("recvfrom(ctrl_iface)"); | |
642 | continue; | |
643 | } | |
644 | buf[res] = '\0'; | |
645 | ||
646 | if (os_strcmp(buf, "ATTACH") == 0) { | |
647 | /* handle ATTACH signal of first monitor interface */ | |
214e428b JM |
648 | if (!wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, |
649 | &from, fromlen)) { | |
6fc6879b JM |
650 | sendto(priv->sock, "OK\n", 3, 0, |
651 | (struct sockaddr *) &from, fromlen); | |
652 | /* OK to continue */ | |
653 | return; | |
654 | } else { | |
655 | sendto(priv->sock, "FAIL\n", 5, 0, | |
656 | (struct sockaddr *) &from, fromlen); | |
657 | } | |
658 | } else { | |
659 | /* return FAIL for all other signals */ | |
660 | sendto(priv->sock, "FAIL\n", 5, 0, | |
661 | (struct sockaddr *) &from, fromlen); | |
662 | } | |
663 | } | |
664 | } | |
665 | ||
666 | ||
667 | /* Global ctrl_iface */ | |
668 | ||
6fc6879b JM |
669 | static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx, |
670 | void *sock_ctx) | |
671 | { | |
672 | struct wpa_global *global = eloop_ctx; | |
214e428b | 673 | struct ctrl_iface_global_priv *priv = sock_ctx; |
6fc6879b JM |
674 | char buf[256]; |
675 | int res; | |
676 | struct sockaddr_un from; | |
677 | socklen_t fromlen = sizeof(from); | |
214e428b | 678 | char *reply = NULL; |
6fc6879b JM |
679 | size_t reply_len; |
680 | ||
681 | res = recvfrom(sock, buf, sizeof(buf) - 1, 0, | |
682 | (struct sockaddr *) &from, &fromlen); | |
683 | if (res < 0) { | |
684 | perror("recvfrom(ctrl_iface)"); | |
685 | return; | |
686 | } | |
687 | buf[res] = '\0'; | |
688 | ||
214e428b JM |
689 | if (os_strcmp(buf, "ATTACH") == 0) { |
690 | if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from, | |
691 | fromlen)) | |
692 | reply_len = 1; | |
693 | else | |
694 | reply_len = 2; | |
695 | } else if (os_strcmp(buf, "DETACH") == 0) { | |
696 | if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from, | |
697 | fromlen)) | |
698 | reply_len = 1; | |
699 | else | |
700 | reply_len = 2; | |
701 | } else { | |
702 | reply = wpa_supplicant_global_ctrl_iface_process(global, buf, | |
703 | &reply_len); | |
704 | } | |
6fc6879b JM |
705 | |
706 | if (reply) { | |
707 | sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, | |
708 | fromlen); | |
709 | os_free(reply); | |
214e428b | 710 | } else if (reply_len == 1) { |
6fc6879b JM |
711 | sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, |
712 | fromlen); | |
214e428b JM |
713 | } else if (reply_len == 2) { |
714 | sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from, fromlen); | |
6fc6879b JM |
715 | } |
716 | } | |
717 | ||
718 | ||
719 | struct ctrl_iface_global_priv * | |
720 | wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global) | |
721 | { | |
722 | struct ctrl_iface_global_priv *priv; | |
723 | struct sockaddr_un addr; | |
d2a9e2c7 | 724 | const char *ctrl = global->params.ctrl_interface; |
8d6e0350 | 725 | int flags; |
6fc6879b JM |
726 | |
727 | priv = os_zalloc(sizeof(*priv)); | |
728 | if (priv == NULL) | |
729 | return NULL; | |
214e428b | 730 | dl_list_init(&priv->ctrl_dst); |
6fc6879b JM |
731 | priv->global = global; |
732 | priv->sock = -1; | |
733 | ||
d2a9e2c7 | 734 | if (ctrl == NULL) |
6fc6879b JM |
735 | return priv; |
736 | ||
d2a9e2c7 JM |
737 | wpa_printf(MSG_DEBUG, "Global control interface '%s'", ctrl); |
738 | ||
b3f3865e | 739 | #ifdef ANDROID |
d2a9e2c7 JM |
740 | if (os_strncmp(ctrl, "@android:", 9) == 0) { |
741 | priv->sock = android_get_control_socket(ctrl + 9); | |
742 | if (priv->sock < 0) { | |
743 | wpa_printf(MSG_ERROR, "Failed to open Android control " | |
744 | "socket '%s'", ctrl + 9); | |
745 | goto fail; | |
746 | } | |
747 | wpa_printf(MSG_DEBUG, "Using Android control socket '%s'", | |
748 | ctrl + 9); | |
b3f3865e | 749 | goto havesock; |
d2a9e2c7 | 750 | } |
b3f3865e | 751 | |
d2a9e2c7 JM |
752 | if (os_strncmp(ctrl, "@abstract:", 10) != 0) { |
753 | /* | |
754 | * Backwards compatibility - try to open an Android control | |
755 | * socket and if that fails, assume this was a UNIX domain | |
756 | * socket instead. | |
757 | */ | |
758 | priv->sock = android_get_control_socket(ctrl); | |
759 | if (priv->sock >= 0) { | |
760 | wpa_printf(MSG_DEBUG, | |
761 | "Using Android control socket '%s'", | |
762 | ctrl); | |
763 | goto havesock; | |
764 | } | |
765 | } | |
766 | #endif /* ANDROID */ | |
6fc6879b JM |
767 | |
768 | priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0); | |
769 | if (priv->sock < 0) { | |
770 | perror("socket(PF_UNIX)"); | |
771 | goto fail; | |
772 | } | |
773 | ||
774 | os_memset(&addr, 0, sizeof(addr)); | |
09bd6e8c | 775 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
19b9436c SL |
776 | addr.sun_len = sizeof(addr); |
777 | #endif /* __FreeBSD__ */ | |
6fc6879b | 778 | addr.sun_family = AF_UNIX; |
d2a9e2c7 JM |
779 | |
780 | if (os_strncmp(ctrl, "@abstract:", 10) == 0) { | |
781 | addr.sun_path[0] = '\0'; | |
782 | os_strlcpy(addr.sun_path + 1, ctrl + 10, | |
783 | sizeof(addr.sun_path) - 1); | |
784 | if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < | |
785 | 0) { | |
786 | wpa_printf(MSG_ERROR, "supp-global-ctrl-iface-init: " | |
787 | "bind(PF_UNIX) failed: %s", strerror(errno)); | |
788 | goto fail; | |
789 | } | |
790 | wpa_printf(MSG_DEBUG, "Using Abstract control socket '%s'", | |
791 | ctrl + 10); | |
792 | goto havesock; | |
793 | } | |
794 | ||
795 | os_strlcpy(addr.sun_path, ctrl, sizeof(addr.sun_path)); | |
6fc6879b | 796 | if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
9d053747 BG |
797 | perror("supp-global-ctrl-iface-init (will try fixup): " |
798 | "bind(PF_UNIX)"); | |
6fc6879b JM |
799 | if (connect(priv->sock, (struct sockaddr *) &addr, |
800 | sizeof(addr)) < 0) { | |
801 | wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not" | |
802 | " allow connections - assuming it was left" | |
803 | "over from forced program termination"); | |
d2a9e2c7 | 804 | if (unlink(ctrl) < 0) { |
6fc6879b JM |
805 | perror("unlink[ctrl_iface]"); |
806 | wpa_printf(MSG_ERROR, "Could not unlink " | |
807 | "existing ctrl_iface socket '%s'", | |
d2a9e2c7 | 808 | ctrl); |
6fc6879b JM |
809 | goto fail; |
810 | } | |
811 | if (bind(priv->sock, (struct sockaddr *) &addr, | |
812 | sizeof(addr)) < 0) { | |
9d053747 | 813 | perror("supp-glb-iface-init: bind(PF_UNIX)"); |
6fc6879b JM |
814 | goto fail; |
815 | } | |
816 | wpa_printf(MSG_DEBUG, "Successfully replaced leftover " | |
817 | "ctrl_iface socket '%s'", | |
d2a9e2c7 | 818 | ctrl); |
6fc6879b JM |
819 | } else { |
820 | wpa_printf(MSG_INFO, "ctrl_iface exists and seems to " | |
821 | "be in use - cannot override it"); | |
822 | wpa_printf(MSG_INFO, "Delete '%s' manually if it is " | |
823 | "not used anymore", | |
d2a9e2c7 | 824 | ctrl); |
6fc6879b JM |
825 | goto fail; |
826 | } | |
827 | } | |
828 | ||
d2a9e2c7 JM |
829 | wpa_printf(MSG_DEBUG, "Using UNIX control socket '%s'", ctrl); |
830 | ||
29257565 JM |
831 | if (global->params.ctrl_interface_group) { |
832 | char *gid_str = global->params.ctrl_interface_group; | |
833 | gid_t gid = 0; | |
834 | struct group *grp; | |
835 | char *endp; | |
836 | ||
837 | grp = getgrnam(gid_str); | |
838 | if (grp) { | |
839 | gid = grp->gr_gid; | |
840 | wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d" | |
841 | " (from group name '%s')", | |
842 | (int) gid, gid_str); | |
843 | } else { | |
844 | /* Group name not found - try to parse this as gid */ | |
845 | gid = strtol(gid_str, &endp, 10); | |
846 | if (*gid_str == '\0' || *endp != '\0') { | |
847 | wpa_printf(MSG_ERROR, "CTRL: Invalid group " | |
848 | "'%s'", gid_str); | |
849 | goto fail; | |
850 | } | |
851 | wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d", | |
852 | (int) gid); | |
853 | } | |
d2a9e2c7 | 854 | if (chown(ctrl, -1, gid) < 0) { |
29257565 JM |
855 | perror("chown[global_ctrl_interface/ifname]"); |
856 | goto fail; | |
857 | } | |
858 | ||
d2a9e2c7 | 859 | if (chmod(ctrl, S_IRWXU | S_IRWXG) < 0) { |
29257565 JM |
860 | perror("chmod[global_ctrl_interface/ifname]"); |
861 | goto fail; | |
862 | } | |
d2a9e2c7 JM |
863 | } else { |
864 | chmod(ctrl, S_IRWXU); | |
29257565 JM |
865 | } |
866 | ||
b3f3865e | 867 | havesock: |
8d6e0350 JM |
868 | |
869 | /* | |
870 | * Make socket non-blocking so that we don't hang forever if | |
871 | * target dies unexpectedly. | |
872 | */ | |
873 | flags = fcntl(priv->sock, F_GETFL); | |
874 | if (flags >= 0) { | |
875 | flags |= O_NONBLOCK; | |
876 | if (fcntl(priv->sock, F_SETFL, flags) < 0) { | |
877 | perror("fcntl(ctrl, O_NONBLOCK)"); | |
878 | /* Not fatal, continue on.*/ | |
879 | } | |
880 | } | |
881 | ||
6fc6879b JM |
882 | eloop_register_read_sock(priv->sock, |
883 | wpa_supplicant_global_ctrl_iface_receive, | |
214e428b | 884 | global, priv); |
6fc6879b JM |
885 | |
886 | return priv; | |
887 | ||
888 | fail: | |
889 | if (priv->sock >= 0) | |
890 | close(priv->sock); | |
891 | os_free(priv); | |
892 | return NULL; | |
893 | } | |
894 | ||
895 | ||
896 | void | |
897 | wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv) | |
898 | { | |
214e428b JM |
899 | struct wpa_ctrl_dst *dst, *prev; |
900 | ||
6fc6879b JM |
901 | if (priv->sock >= 0) { |
902 | eloop_unregister_read_sock(priv->sock); | |
903 | close(priv->sock); | |
904 | } | |
905 | if (priv->global->params.ctrl_interface) | |
906 | unlink(priv->global->params.ctrl_interface); | |
214e428b JM |
907 | dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst, |
908 | list) | |
909 | os_free(dst); | |
6fc6879b JM |
910 | os_free(priv); |
911 | } |