Fix UNIX domain socket address handling to be more portable
[libeap.git] / hostapd / ctrl_iface.c
1 /*
2  * hostapd / UNIX domain socket -based control interface
3  * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #ifndef CONFIG_NATIVE_WINDOWS
18
19 #include <sys/un.h>
20 #include <sys/stat.h>
21 #include <stddef.h>
22
23 #include "hostapd.h"
24 #include "eloop.h"
25 #include "config.h"
26 #include "ieee802_1x.h"
27 #include "wpa.h"
28 #include "radius/radius_client.h"
29 #include "ieee802_11.h"
30 #include "ctrl_iface.h"
31 #include "sta_info.h"
32 #include "accounting.h"
33 #include "wps_hostapd.h"
34 #include "driver.h"
35
36
37 struct wpa_ctrl_dst {
38         struct wpa_ctrl_dst *next;
39         struct sockaddr_un addr;
40         socklen_t addrlen;
41         int debug_level;
42         int errors;
43 };
44
45
46 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
47                                     const char *buf, size_t len);
48
49
50 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
51                                      struct sockaddr_un *from,
52                                      socklen_t fromlen)
53 {
54         struct wpa_ctrl_dst *dst;
55
56         dst = os_zalloc(sizeof(*dst));
57         if (dst == NULL)
58                 return -1;
59         os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
60         dst->addrlen = fromlen;
61         dst->debug_level = MSG_INFO;
62         dst->next = hapd->ctrl_dst;
63         hapd->ctrl_dst = dst;
64         wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
65                     (u8 *) from->sun_path,
66                     fromlen - offsetof(struct sockaddr_un, sun_path));
67         return 0;
68 }
69
70
71 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
72                                      struct sockaddr_un *from,
73                                      socklen_t fromlen)
74 {
75         struct wpa_ctrl_dst *dst, *prev = NULL;
76
77         dst = hapd->ctrl_dst;
78         while (dst) {
79                 if (fromlen == dst->addrlen &&
80                     os_memcmp(from->sun_path, dst->addr.sun_path,
81                               fromlen - offsetof(struct sockaddr_un, sun_path))
82                     == 0) {
83                         if (prev == NULL)
84                                 hapd->ctrl_dst = dst->next;
85                         else
86                                 prev->next = dst->next;
87                         os_free(dst);
88                         wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
89                                     (u8 *) from->sun_path,
90                                     fromlen -
91                                     offsetof(struct sockaddr_un, sun_path));
92                         return 0;
93                 }
94                 prev = dst;
95                 dst = dst->next;
96         }
97         return -1;
98 }
99
100
101 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
102                                     struct sockaddr_un *from,
103                                     socklen_t fromlen,
104                                     char *level)
105 {
106         struct wpa_ctrl_dst *dst;
107
108         wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
109
110         dst = hapd->ctrl_dst;
111         while (dst) {
112                 if (fromlen == dst->addrlen &&
113                     os_memcmp(from->sun_path, dst->addr.sun_path,
114                               fromlen - offsetof(struct sockaddr_un, sun_path))
115                     == 0) {
116                         wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
117                                     "level", (u8 *) from->sun_path, fromlen -
118                                     offsetof(struct sockaddr_un, sun_path));
119                         dst->debug_level = atoi(level);
120                         return 0;
121                 }
122                 dst = dst->next;
123         }
124
125         return -1;
126 }
127
128
129 static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
130                                       struct sta_info *sta,
131                                       char *buf, size_t buflen)
132 {
133         int len, res, ret;
134
135         if (sta == NULL) {
136                 ret = os_snprintf(buf, buflen, "FAIL\n");
137                 if (ret < 0 || (size_t) ret >= buflen)
138                         return 0;
139                 return ret;
140         }
141
142         len = 0;
143         ret = os_snprintf(buf + len, buflen - len, MACSTR "\n",
144                           MAC2STR(sta->addr));
145         if (ret < 0 || (size_t) ret >= buflen - len)
146                 return len;
147         len += ret;
148
149         res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
150         if (res >= 0)
151                 len += res;
152         res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
153         if (res >= 0)
154                 len += res;
155         res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
156         if (res >= 0)
157                 len += res;
158
159         return len;
160 }
161
162
163 static int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
164                                         char *buf, size_t buflen)
165 {
166         return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
167 }
168
169
170 static int hostapd_ctrl_iface_sta(struct hostapd_data *hapd,
171                                   const char *txtaddr,
172                                   char *buf, size_t buflen)
173 {
174         u8 addr[ETH_ALEN];
175         int ret;
176
177         if (hwaddr_aton(txtaddr, addr)) {
178                 ret = os_snprintf(buf, buflen, "FAIL\n");
179                 if (ret < 0 || (size_t) ret >= buflen)
180                         return 0;
181                 return ret;
182         }
183         return hostapd_ctrl_iface_sta_mib(hapd, ap_get_sta(hapd, addr),
184                                           buf, buflen);
185 }
186
187
188 static int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd,
189                                        const char *txtaddr,
190                                        char *buf, size_t buflen)
191 {
192         u8 addr[ETH_ALEN];
193         struct sta_info *sta;
194         int ret;
195
196         if (hwaddr_aton(txtaddr, addr) ||
197             (sta = ap_get_sta(hapd, addr)) == NULL) {
198                 ret = os_snprintf(buf, buflen, "FAIL\n");
199                 if (ret < 0 || (size_t) ret >= buflen)
200                         return 0;
201                 return ret;
202         }               
203         return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
204 }
205
206
207 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
208                                       const char *txtaddr)
209 {
210         u8 addr[ETH_ALEN];
211         struct sta_info *sta;
212
213         wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
214
215         if (hwaddr_aton(txtaddr, addr))
216                 return -1;
217
218         sta = ap_get_sta(hapd, addr);
219         if (sta)
220                 return 0;
221
222         wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
223                    "notification", MAC2STR(addr));
224         sta = ap_sta_add(hapd, addr);
225         if (sta == NULL)
226                 return -1;
227
228         hostapd_new_assoc_sta(hapd, sta, 0);
229         return 0;
230 }
231
232
233 #ifdef CONFIG_IEEE80211W
234 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
235                                        const char *txtaddr)
236 {
237         u8 addr[ETH_ALEN];
238         u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
239
240         wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
241
242         if (hwaddr_aton(txtaddr, addr))
243                 return -1;
244
245         os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
246         ieee802_11_send_sa_query_req(hapd, addr, trans_id);
247
248         return 0;
249 }
250 #endif /* CONFIG_IEEE80211W */
251
252
253 #ifdef CONFIG_WPS
254 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
255 {
256         char *pin = os_strchr(txt, ' ');
257         if (pin == NULL)
258                 return -1;
259         *pin++ = '\0';
260         return hostapd_wps_add_pin(hapd, txt, pin);
261 }
262
263
264 #ifdef CONFIG_WPS_OOB
265 static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
266 {
267         char *path, *method, *name;
268
269         path = os_strchr(txt, ' ');
270         if (path == NULL)
271                 return -1;
272         *path++ = '\0';
273
274         method = os_strchr(path, ' ');
275         if (method == NULL)
276                 return -1;
277         *method++ = '\0';
278
279         name = os_strchr(method, ' ');
280         if (name != NULL)
281                 *name++ = '\0';
282
283         return hostapd_wps_start_oob(hapd, txt, path, method, name);
284 }
285 #endif /* CONFIG_WPS_OOB */
286 #endif /* CONFIG_WPS */
287
288
289 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
290                                        void *sock_ctx)
291 {
292         struct hostapd_data *hapd = eloop_ctx;
293         char buf[256];
294         int res;
295         struct sockaddr_un from;
296         socklen_t fromlen = sizeof(from);
297         char *reply;
298         const int reply_size = 4096;
299         int reply_len;
300
301         res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
302                        (struct sockaddr *) &from, &fromlen);
303         if (res < 0) {
304                 perror("recvfrom(ctrl_iface)");
305                 return;
306         }
307         buf[res] = '\0';
308         wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
309
310         reply = os_malloc(reply_size);
311         if (reply == NULL) {
312                 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
313                        fromlen);
314                 return;
315         }
316
317         os_memcpy(reply, "OK\n", 3);
318         reply_len = 3;
319
320         if (os_strcmp(buf, "PING") == 0) {
321                 os_memcpy(reply, "PONG\n", 5);
322                 reply_len = 5;
323         } else if (os_strcmp(buf, "MIB") == 0) {
324                 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
325                 if (reply_len >= 0) {
326                         res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
327                                           reply_size - reply_len);
328                         if (res < 0)
329                                 reply_len = -1;
330                         else
331                                 reply_len += res;
332                 }
333                 if (reply_len >= 0) {
334                         res = ieee802_1x_get_mib(hapd, reply + reply_len,
335                                                  reply_size - reply_len);
336                         if (res < 0)
337                                 reply_len = -1;
338                         else
339                                 reply_len += res;
340                 }
341                 if (reply_len >= 0) {
342                         res = radius_client_get_mib(hapd->radius,
343                                                     reply + reply_len,
344                                                     reply_size - reply_len);
345                         if (res < 0)
346                                 reply_len = -1;
347                         else
348                                 reply_len += res;
349                 }
350         } else if (os_strcmp(buf, "STA-FIRST") == 0) {
351                 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
352                                                          reply_size);
353         } else if (os_strncmp(buf, "STA ", 4) == 0) {
354                 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
355                                                    reply_size);
356         } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
357                 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
358                                                         reply_size);
359         } else if (os_strcmp(buf, "ATTACH") == 0) {
360                 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
361                         reply_len = -1;
362         } else if (os_strcmp(buf, "DETACH") == 0) {
363                 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
364                         reply_len = -1;
365         } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
366                 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
367                                                     buf + 6))
368                         reply_len = -1;
369         } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
370                 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
371                         reply_len = -1;
372 #ifdef CONFIG_IEEE80211W
373         } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
374                 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
375                         reply_len = -1;
376 #endif /* CONFIG_IEEE80211W */
377 #ifdef CONFIG_WPS
378         } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
379                 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
380                         reply_len = -1;
381         } else if (os_strcmp(buf, "WPS_PBC") == 0) {
382                 if (hostapd_wps_button_pushed(hapd))
383                         reply_len = -1;
384 #ifdef CONFIG_WPS_OOB
385         } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
386                 if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
387                         reply_len = -1;
388 #endif /* CONFIG_WPS_OOB */
389 #endif /* CONFIG_WPS */
390         } else {
391                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
392                 reply_len = 16;
393         }
394
395         if (reply_len < 0) {
396                 os_memcpy(reply, "FAIL\n", 5);
397                 reply_len = 5;
398         }
399         sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
400         os_free(reply);
401 }
402
403
404 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
405 {
406         char *buf;
407         size_t len;
408
409         if (hapd->conf->ctrl_interface == NULL)
410                 return NULL;
411
412         len = os_strlen(hapd->conf->ctrl_interface) +
413                 os_strlen(hapd->conf->iface) + 2;
414         buf = os_malloc(len);
415         if (buf == NULL)
416                 return NULL;
417
418         os_snprintf(buf, len, "%s/%s",
419                     hapd->conf->ctrl_interface, hapd->conf->iface);
420         buf[len - 1] = '\0';
421         return buf;
422 }
423
424
425 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
426                                       const char *txt, size_t len)
427 {
428         struct hostapd_data *hapd = ctx;
429         if (hapd == NULL)
430                 return;
431         hostapd_ctrl_iface_send(hapd, level, txt, len);
432 }
433
434
435 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
436 {
437         struct sockaddr_un addr;
438         int s = -1;
439         char *fname = NULL;
440
441         hapd->ctrl_sock = -1;
442
443         if (hapd->conf->ctrl_interface == NULL)
444                 return 0;
445
446         if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
447                 if (errno == EEXIST) {
448                         wpa_printf(MSG_DEBUG, "Using existing control "
449                                    "interface directory.");
450                 } else {
451                         perror("mkdir[ctrl_interface]");
452                         goto fail;
453                 }
454         }
455
456         if (hapd->conf->ctrl_interface_gid_set &&
457             chown(hapd->conf->ctrl_interface, 0,
458                   hapd->conf->ctrl_interface_gid) < 0) {
459                 perror("chown[ctrl_interface]");
460                 return -1;
461         }
462
463         if (os_strlen(hapd->conf->ctrl_interface) + 1 +
464             os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
465                 goto fail;
466
467         s = socket(PF_UNIX, SOCK_DGRAM, 0);
468         if (s < 0) {
469                 perror("socket(PF_UNIX)");
470                 goto fail;
471         }
472
473         os_memset(&addr, 0, sizeof(addr));
474 #ifdef __FreeBSD__
475         addr.sun_len = sizeof(addr);
476 #endif /* __FreeBSD__ */
477         addr.sun_family = AF_UNIX;
478         fname = hostapd_ctrl_iface_path(hapd);
479         if (fname == NULL)
480                 goto fail;
481         os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
482         if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
483                 perror("bind(PF_UNIX)");
484                 goto fail;
485         }
486
487         if (hapd->conf->ctrl_interface_gid_set &&
488             chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
489                 perror("chown[ctrl_interface/ifname]");
490                 goto fail;
491         }
492
493         if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
494                 perror("chmod[ctrl_interface/ifname]");
495                 goto fail;
496         }
497         os_free(fname);
498
499         hapd->ctrl_sock = s;
500         eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
501                                  NULL);
502         wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
503
504         return 0;
505
506 fail:
507         if (s >= 0)
508                 close(s);
509         if (fname) {
510                 unlink(fname);
511                 os_free(fname);
512         }
513         return -1;
514 }
515
516
517 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
518 {
519         struct wpa_ctrl_dst *dst, *prev;
520
521         if (hapd->ctrl_sock > -1) {
522                 char *fname;
523                 eloop_unregister_read_sock(hapd->ctrl_sock);
524                 close(hapd->ctrl_sock);
525                 hapd->ctrl_sock = -1;
526                 fname = hostapd_ctrl_iface_path(hapd);
527                 if (fname)
528                         unlink(fname);
529                 os_free(fname);
530
531                 if (hapd->conf->ctrl_interface &&
532                     rmdir(hapd->conf->ctrl_interface) < 0) {
533                         if (errno == ENOTEMPTY) {
534                                 wpa_printf(MSG_DEBUG, "Control interface "
535                                            "directory not empty - leaving it "
536                                            "behind");
537                         } else {
538                                 perror("rmdir[ctrl_interface]");
539                         }
540                 }
541         }
542
543         dst = hapd->ctrl_dst;
544         while (dst) {
545                 prev = dst;
546                 dst = dst->next;
547                 os_free(prev);
548         }
549 }
550
551
552 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
553                                     const char *buf, size_t len)
554 {
555         struct wpa_ctrl_dst *dst, *next;
556         struct msghdr msg;
557         int idx;
558         struct iovec io[2];
559         char levelstr[10];
560
561         dst = hapd->ctrl_dst;
562         if (hapd->ctrl_sock < 0 || dst == NULL)
563                 return;
564
565         os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
566         io[0].iov_base = levelstr;
567         io[0].iov_len = os_strlen(levelstr);
568         io[1].iov_base = (char *) buf;
569         io[1].iov_len = len;
570         os_memset(&msg, 0, sizeof(msg));
571         msg.msg_iov = io;
572         msg.msg_iovlen = 2;
573
574         idx = 0;
575         while (dst) {
576                 next = dst->next;
577                 if (level >= dst->debug_level) {
578                         wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
579                                     (u8 *) dst->addr.sun_path, dst->addrlen -
580                                     offsetof(struct sockaddr_un, sun_path));
581                         msg.msg_name = &dst->addr;
582                         msg.msg_namelen = dst->addrlen;
583                         if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
584                                 int _errno = errno;
585                                 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
586                                            "%d - %s",
587                                            idx, errno, strerror(errno));
588                                 dst->errors++;
589                                 if (dst->errors > 10 || _errno == ENOENT) {
590                                         hostapd_ctrl_iface_detach(
591                                                 hapd, &dst->addr,
592                                                 dst->addrlen);
593                                 }
594                         } else
595                                 dst->errors = 0;
596                 }
597                 idx++;
598                 dst = next;
599         }
600 }
601
602 #endif /* CONFIG_NATIVE_WINDOWS */