wpa_priv: allow l2_packet to be opened for ethertype 0x890d
[mech_eap.git] / wpa_supplicant / wpa_priv.c
1 /*
2  * WPA Supplicant / privileged helper program
3  * Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "includes.h"
10 #ifdef __linux__
11 #include <fcntl.h>
12 #endif /* __linux__ */
13 #include <sys/un.h>
14 #include <sys/stat.h>
15
16 #include "common.h"
17 #include "eloop.h"
18 #include "common/version.h"
19 #include "drivers/driver.h"
20 #include "l2_packet/l2_packet.h"
21 #include "common/privsep_commands.h"
22 #include "common/ieee802_11_defs.h"
23
24
25 struct wpa_priv_interface {
26         struct wpa_priv_interface *next;
27         char *driver_name;
28         char *ifname;
29         char *sock_name;
30         int fd;
31
32         const struct wpa_driver_ops *driver;
33         void *drv_priv;
34         struct sockaddr_un drv_addr;
35         int wpas_registered;
36
37         /* TODO: add support for multiple l2 connections */
38         struct l2_packet_data *l2;
39         struct sockaddr_un l2_addr;
40 };
41
42
43 static void wpa_priv_cmd_register(struct wpa_priv_interface *iface,
44                                   struct sockaddr_un *from)
45 {
46         if (iface->drv_priv) {
47                 wpa_printf(MSG_DEBUG, "Cleaning up forgotten driver instance");
48                 if (iface->driver->deinit)
49                         iface->driver->deinit(iface->drv_priv);
50                 iface->drv_priv = NULL;
51                 iface->wpas_registered = 0;
52         }
53
54         if (iface->l2) {
55                 wpa_printf(MSG_DEBUG, "Cleaning up forgotten l2_packet "
56                            "instance");
57                 l2_packet_deinit(iface->l2);
58                 iface->l2 = NULL;
59         }
60
61         if (iface->driver->init == NULL)
62                 return;
63
64         iface->drv_priv = iface->driver->init(iface, iface->ifname);
65         if (iface->drv_priv == NULL) {
66                 wpa_printf(MSG_DEBUG, "Failed to initialize driver wrapper");
67                 return;
68         }
69
70         wpa_printf(MSG_DEBUG, "Driver wrapper '%s' initialized for interface "
71                    "'%s'", iface->driver_name, iface->ifname);
72
73         os_memcpy(&iface->drv_addr, from, sizeof(iface->drv_addr));
74         iface->wpas_registered = 1;
75
76         if (iface->driver->set_param &&
77             iface->driver->set_param(iface->drv_priv, NULL) < 0) {
78                 wpa_printf(MSG_ERROR, "Driver interface rejected param");
79         }
80 }
81
82
83 static void wpa_priv_cmd_unregister(struct wpa_priv_interface *iface,
84                                     struct sockaddr_un *from)
85 {
86         if (iface->drv_priv) {
87                 if (iface->driver->deinit)
88                         iface->driver->deinit(iface->drv_priv);
89                 iface->drv_priv = NULL;
90                 iface->wpas_registered = 0;
91         }
92 }
93
94
95 static void wpa_priv_cmd_scan(struct wpa_priv_interface *iface,
96                               char *buf, size_t len)
97 {
98         struct wpa_driver_scan_params params;
99
100         if (iface->drv_priv == NULL)
101                 return;
102
103         os_memset(&params, 0, sizeof(params));
104         if (len) {
105                 params.ssids[0].ssid = (u8 *) buf;
106                 params.ssids[0].ssid_len = len;
107                 params.num_ssids = 1;
108         }
109
110         if (iface->driver->scan2)
111                 iface->driver->scan2(iface->drv_priv, &params);
112 }
113
114
115 static void wpa_priv_get_scan_results2(struct wpa_priv_interface *iface,
116                                        struct sockaddr_un *from)
117 {
118         struct wpa_scan_results *res;
119         u8 *buf = NULL, *pos, *end;
120         int val;
121         size_t i;
122
123         res = iface->driver->get_scan_results2(iface->drv_priv);
124         if (res == NULL)
125                 goto fail;
126
127         buf = os_malloc(60000);
128         if (buf == NULL)
129                 goto fail;
130         pos = buf;
131         end = buf + 60000;
132         val = res->num;
133         os_memcpy(pos, &val, sizeof(int));
134         pos += sizeof(int);
135
136         for (i = 0; i < res->num; i++) {
137                 struct wpa_scan_res *r = res->res[i];
138                 val = sizeof(*r) + r->ie_len;
139                 if (end - pos < (int) sizeof(int) + val)
140                         break;
141                 os_memcpy(pos, &val, sizeof(int));
142                 pos += sizeof(int);
143                 os_memcpy(pos, r, val);
144                 pos += val;
145         }
146
147         sendto(iface->fd, buf, pos - buf, 0, (struct sockaddr *) from,
148                sizeof(*from));
149
150         os_free(buf);
151         wpa_scan_results_free(res);
152         return;
153
154 fail:
155         os_free(buf);
156         wpa_scan_results_free(res);
157         sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from));
158 }
159
160
161 static void wpa_priv_cmd_get_scan_results(struct wpa_priv_interface *iface,
162                                           struct sockaddr_un *from)
163 {
164         if (iface->drv_priv == NULL)
165                 return;
166
167         if (iface->driver->get_scan_results2)
168                 wpa_priv_get_scan_results2(iface, from);
169         else
170                 sendto(iface->fd, "", 0, 0, (struct sockaddr *) from,
171                        sizeof(*from));
172 }
173
174
175 static void wpa_priv_cmd_associate(struct wpa_priv_interface *iface,
176                                    void *buf, size_t len)
177 {
178         struct wpa_driver_associate_params params;
179         struct privsep_cmd_associate *assoc;
180         u8 *bssid;
181         int res;
182
183         if (iface->drv_priv == NULL || iface->driver->associate == NULL)
184                 return;
185
186         if (len < sizeof(*assoc)) {
187                 wpa_printf(MSG_DEBUG, "Invalid association request");
188                 return;
189         }
190
191         assoc = buf;
192         if (sizeof(*assoc) + assoc->wpa_ie_len > len) {
193                 wpa_printf(MSG_DEBUG, "Association request overflow");
194                 return;
195         }
196
197         os_memset(&params, 0, sizeof(params));
198         bssid = assoc->bssid;
199         if (bssid[0] | bssid[1] | bssid[2] | bssid[3] | bssid[4] | bssid[5])
200                 params.bssid = bssid;
201         params.ssid = assoc->ssid;
202         if (assoc->ssid_len > SSID_MAX_LEN)
203                 return;
204         params.ssid_len = assoc->ssid_len;
205         params.freq.mode = assoc->hwmode;
206         params.freq.freq = assoc->freq;
207         params.freq.channel = assoc->channel;
208         if (assoc->wpa_ie_len) {
209                 params.wpa_ie = (u8 *) (assoc + 1);
210                 params.wpa_ie_len = assoc->wpa_ie_len;
211         }
212         params.pairwise_suite = assoc->pairwise_suite;
213         params.group_suite = assoc->group_suite;
214         params.key_mgmt_suite = assoc->key_mgmt_suite;
215         params.auth_alg = assoc->auth_alg;
216         params.mode = assoc->mode;
217
218         res = iface->driver->associate(iface->drv_priv, &params);
219         wpa_printf(MSG_DEBUG, "drv->associate: res=%d", res);
220 }
221
222
223 static void wpa_priv_cmd_get_bssid(struct wpa_priv_interface *iface,
224                                    struct sockaddr_un *from)
225 {
226         u8 bssid[ETH_ALEN];
227
228         if (iface->drv_priv == NULL)
229                 goto fail;
230
231         if (iface->driver->get_bssid == NULL ||
232             iface->driver->get_bssid(iface->drv_priv, bssid) < 0)
233                 goto fail;
234
235         sendto(iface->fd, bssid, ETH_ALEN, 0, (struct sockaddr *) from,
236                sizeof(*from));
237         return;
238
239 fail:
240         sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from));
241 }
242
243
244 static void wpa_priv_cmd_get_ssid(struct wpa_priv_interface *iface,
245                                   struct sockaddr_un *from)
246 {
247         u8 ssid[sizeof(int) + SSID_MAX_LEN];
248         int res;
249
250         if (iface->drv_priv == NULL)
251                 goto fail;
252
253         if (iface->driver->get_ssid == NULL)
254                 goto fail;
255
256         res = iface->driver->get_ssid(iface->drv_priv, &ssid[sizeof(int)]);
257         if (res < 0 || res > SSID_MAX_LEN)
258                 goto fail;
259         os_memcpy(ssid, &res, sizeof(int));
260
261         sendto(iface->fd, ssid, sizeof(ssid), 0, (struct sockaddr *) from,
262                sizeof(*from));
263         return;
264
265 fail:
266         sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from));
267 }
268
269
270 static void wpa_priv_cmd_set_key(struct wpa_priv_interface *iface,
271                                  void *buf, size_t len)
272 {
273         struct privsep_cmd_set_key *params;
274         int res;
275
276         if (iface->drv_priv == NULL || iface->driver->set_key == NULL)
277                 return;
278
279         if (len != sizeof(*params)) {
280                 wpa_printf(MSG_DEBUG, "Invalid set_key request");
281                 return;
282         }
283
284         params = buf;
285
286         res = iface->driver->set_key(iface->ifname, iface->drv_priv,
287                                      params->alg,
288                                      params->addr, params->key_idx,
289                                      params->set_tx,
290                                      params->seq_len ? params->seq : NULL,
291                                      params->seq_len,
292                                      params->key_len ? params->key : NULL,
293                                      params->key_len);
294         wpa_printf(MSG_DEBUG, "drv->set_key: res=%d", res);
295 }
296
297
298 static void wpa_priv_cmd_get_capa(struct wpa_priv_interface *iface,
299                                   struct sockaddr_un *from)
300 {
301         struct wpa_driver_capa capa;
302
303         if (iface->drv_priv == NULL)
304                 goto fail;
305
306         if (iface->driver->get_capa == NULL ||
307             iface->driver->get_capa(iface->drv_priv, &capa) < 0)
308                 goto fail;
309
310         sendto(iface->fd, &capa, sizeof(capa), 0, (struct sockaddr *) from,
311                sizeof(*from));
312         return;
313
314 fail:
315         sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from));
316 }
317
318
319 static void wpa_priv_l2_rx(void *ctx, const u8 *src_addr, const u8 *buf,
320                            size_t len)
321 {
322         struct wpa_priv_interface *iface = ctx;
323         struct msghdr msg;
324         struct iovec io[2];
325
326         io[0].iov_base = (u8 *) src_addr;
327         io[0].iov_len = ETH_ALEN;
328         io[1].iov_base = (u8 *) buf;
329         io[1].iov_len = len;
330
331         os_memset(&msg, 0, sizeof(msg));
332         msg.msg_iov = io;
333         msg.msg_iovlen = 2;
334         msg.msg_name = &iface->l2_addr;
335         msg.msg_namelen = sizeof(iface->l2_addr);
336
337         if (sendmsg(iface->fd, &msg, 0) < 0) {
338                 wpa_printf(MSG_ERROR, "sendmsg(l2 rx): %s", strerror(errno));
339         }
340 }
341
342
343 static void wpa_priv_cmd_l2_register(struct wpa_priv_interface *iface,
344                                      struct sockaddr_un *from,
345                                      void *buf, size_t len)
346 {
347         int *reg_cmd = buf;
348         u8 own_addr[ETH_ALEN];
349         int res;
350         u16 proto;
351
352         if (len != 2 * sizeof(int)) {
353                 wpa_printf(MSG_DEBUG, "Invalid l2_register length %lu",
354                            (unsigned long) len);
355                 return;
356         }
357
358         proto = reg_cmd[0];
359         if (proto != ETH_P_EAPOL && proto != ETH_P_RSN_PREAUTH &&
360             proto != ETH_P_80211_ENCAP) {
361                 wpa_printf(MSG_DEBUG, "Refused l2_packet connection for "
362                            "ethertype 0x%x", proto);
363                 return;
364         }
365
366         if (iface->l2) {
367                 wpa_printf(MSG_DEBUG, "Cleaning up forgotten l2_packet "
368                            "instance");
369                 l2_packet_deinit(iface->l2);
370                 iface->l2 = NULL;
371         }
372
373         os_memcpy(&iface->l2_addr, from, sizeof(iface->l2_addr));
374
375         iface->l2 = l2_packet_init(iface->ifname, NULL, proto,
376                                    wpa_priv_l2_rx, iface, reg_cmd[1]);
377         if (iface->l2 == NULL) {
378                 wpa_printf(MSG_DEBUG, "Failed to initialize l2_packet "
379                            "instance for protocol %d", proto);
380                 return;
381         }
382
383         if (l2_packet_get_own_addr(iface->l2, own_addr) < 0) {
384                 wpa_printf(MSG_DEBUG, "Failed to get own address from "
385                            "l2_packet");
386                 l2_packet_deinit(iface->l2);
387                 iface->l2 = NULL;
388                 return;
389         }
390
391         res = sendto(iface->fd, own_addr, ETH_ALEN, 0,
392                      (struct sockaddr *) from, sizeof(*from));
393         wpa_printf(MSG_DEBUG, "L2 registration: res=%d", res);
394 }
395
396
397 static void wpa_priv_cmd_l2_unregister(struct wpa_priv_interface *iface,
398                                        struct sockaddr_un *from)
399 {
400         if (iface->l2) {
401                 l2_packet_deinit(iface->l2);
402                 iface->l2 = NULL;
403         }
404 }
405
406
407 static void wpa_priv_cmd_l2_notify_auth_start(struct wpa_priv_interface *iface,
408                                               struct sockaddr_un *from)
409 {
410         if (iface->l2)
411                 l2_packet_notify_auth_start(iface->l2);
412 }
413
414
415 static void wpa_priv_cmd_l2_send(struct wpa_priv_interface *iface,
416                                  struct sockaddr_un *from,
417                                  void *buf, size_t len)
418 {
419         u8 *dst_addr;
420         u16 proto;
421         int res;
422
423         if (iface->l2 == NULL)
424                 return;
425
426         if (len < ETH_ALEN + 2) {
427                 wpa_printf(MSG_DEBUG, "Too short L2 send packet (len=%lu)",
428                            (unsigned long) len);
429                 return;
430         }
431
432         dst_addr = buf;
433         os_memcpy(&proto, buf + ETH_ALEN, 2);
434
435         if (proto != ETH_P_EAPOL && proto != ETH_P_RSN_PREAUTH) {
436                 wpa_printf(MSG_DEBUG, "Refused l2_packet send for ethertype "
437                            "0x%x", proto);
438                 return;
439         }
440
441         res = l2_packet_send(iface->l2, dst_addr, proto, buf + ETH_ALEN + 2,
442                              len - ETH_ALEN - 2);
443         wpa_printf(MSG_DEBUG, "L2 send: res=%d", res);
444 }
445
446
447 static void wpa_priv_cmd_set_country(struct wpa_priv_interface *iface,
448                                      char *buf)
449 {
450         if (iface->drv_priv == NULL || iface->driver->set_country == NULL ||
451             *buf == '\0')
452                 return;
453
454         iface->driver->set_country(iface->drv_priv, buf);
455 }
456
457
458 static void wpa_priv_receive(int sock, void *eloop_ctx, void *sock_ctx)
459 {
460         struct wpa_priv_interface *iface = eloop_ctx;
461         char buf[2000], *pos;
462         void *cmd_buf;
463         size_t cmd_len;
464         int res, cmd;
465         struct sockaddr_un from;
466         socklen_t fromlen = sizeof(from);
467
468         res = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &from,
469                        &fromlen);
470         if (res < 0) {
471                 wpa_printf(MSG_ERROR, "recvfrom: %s", strerror(errno));
472                 return;
473         }
474
475         if (res < (int) sizeof(int)) {
476                 wpa_printf(MSG_DEBUG, "Too short command (len=%d)", res);
477                 return;
478         }
479
480         os_memcpy(&cmd, buf, sizeof(int));
481         wpa_printf(MSG_DEBUG, "Command %d for interface %s",
482                    cmd, iface->ifname);
483         cmd_buf = &buf[sizeof(int)];
484         cmd_len = res - sizeof(int);
485
486         switch (cmd) {
487         case PRIVSEP_CMD_REGISTER:
488                 wpa_priv_cmd_register(iface, &from);
489                 break;
490         case PRIVSEP_CMD_UNREGISTER:
491                 wpa_priv_cmd_unregister(iface, &from);
492                 break;
493         case PRIVSEP_CMD_SCAN:
494                 wpa_priv_cmd_scan(iface, cmd_buf, cmd_len);
495                 break;
496         case PRIVSEP_CMD_GET_SCAN_RESULTS:
497                 wpa_priv_cmd_get_scan_results(iface, &from);
498                 break;
499         case PRIVSEP_CMD_ASSOCIATE:
500                 wpa_priv_cmd_associate(iface, cmd_buf, cmd_len);
501                 break;
502         case PRIVSEP_CMD_GET_BSSID:
503                 wpa_priv_cmd_get_bssid(iface, &from);
504                 break;
505         case PRIVSEP_CMD_GET_SSID:
506                 wpa_priv_cmd_get_ssid(iface, &from);
507                 break;
508         case PRIVSEP_CMD_SET_KEY:
509                 wpa_priv_cmd_set_key(iface, cmd_buf, cmd_len);
510                 break;
511         case PRIVSEP_CMD_GET_CAPA:
512                 wpa_priv_cmd_get_capa(iface, &from);
513                 break;
514         case PRIVSEP_CMD_L2_REGISTER:
515                 wpa_priv_cmd_l2_register(iface, &from, cmd_buf, cmd_len);
516                 break;
517         case PRIVSEP_CMD_L2_UNREGISTER:
518                 wpa_priv_cmd_l2_unregister(iface, &from);
519                 break;
520         case PRIVSEP_CMD_L2_NOTIFY_AUTH_START:
521                 wpa_priv_cmd_l2_notify_auth_start(iface, &from);
522                 break;
523         case PRIVSEP_CMD_L2_SEND:
524                 wpa_priv_cmd_l2_send(iface, &from, cmd_buf, cmd_len);
525                 break;
526         case PRIVSEP_CMD_SET_COUNTRY:
527                 pos = cmd_buf;
528                 if (pos + cmd_len >= buf + sizeof(buf))
529                         break;
530                 pos[cmd_len] = '\0';
531                 wpa_priv_cmd_set_country(iface, pos);
532                 break;
533         }
534 }
535
536
537 static void wpa_priv_interface_deinit(struct wpa_priv_interface *iface)
538 {
539         if (iface->drv_priv && iface->driver->deinit)
540                 iface->driver->deinit(iface->drv_priv);
541
542         if (iface->fd >= 0) {
543                 eloop_unregister_read_sock(iface->fd);
544                 close(iface->fd);
545                 unlink(iface->sock_name);
546         }
547
548         if (iface->l2)
549                 l2_packet_deinit(iface->l2);
550
551         os_free(iface->ifname);
552         os_free(iface->driver_name);
553         os_free(iface->sock_name);
554         os_free(iface);
555 }
556
557
558 static struct wpa_priv_interface *
559 wpa_priv_interface_init(const char *dir, const char *params)
560 {
561         struct wpa_priv_interface *iface;
562         char *pos;
563         size_t len;
564         struct sockaddr_un addr;
565         int i;
566
567         pos = os_strchr(params, ':');
568         if (pos == NULL)
569                 return NULL;
570
571         iface = os_zalloc(sizeof(*iface));
572         if (iface == NULL)
573                 return NULL;
574         iface->fd = -1;
575
576         len = pos - params;
577         iface->driver_name = dup_binstr(params, len);
578         if (iface->driver_name == NULL) {
579                 wpa_priv_interface_deinit(iface);
580                 return NULL;
581         }
582
583         for (i = 0; wpa_drivers[i]; i++) {
584                 if (os_strcmp(iface->driver_name,
585                               wpa_drivers[i]->name) == 0) {
586                         iface->driver = wpa_drivers[i];
587                         break;
588                 }
589         }
590         if (iface->driver == NULL) {
591                 wpa_printf(MSG_ERROR, "Unsupported driver '%s'",
592                            iface->driver_name);
593                 wpa_priv_interface_deinit(iface);
594                 return NULL;
595         }
596
597         pos++;
598         iface->ifname = os_strdup(pos);
599         if (iface->ifname == NULL) {
600                 wpa_priv_interface_deinit(iface);
601                 return NULL;
602         }
603
604         len = os_strlen(dir) + 1 + os_strlen(iface->ifname);
605         iface->sock_name = os_malloc(len + 1);
606         if (iface->sock_name == NULL) {
607                 wpa_priv_interface_deinit(iface);
608                 return NULL;
609         }
610
611         os_snprintf(iface->sock_name, len + 1, "%s/%s", dir, iface->ifname);
612         if (os_strlen(iface->sock_name) >= sizeof(addr.sun_path)) {
613                 wpa_priv_interface_deinit(iface);
614                 return NULL;
615         }
616
617         iface->fd = socket(PF_UNIX, SOCK_DGRAM, 0);
618         if (iface->fd < 0) {
619                 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
620                 wpa_priv_interface_deinit(iface);
621                 return NULL;
622         }
623
624         os_memset(&addr, 0, sizeof(addr));
625         addr.sun_family = AF_UNIX;
626         os_strlcpy(addr.sun_path, iface->sock_name, sizeof(addr.sun_path));
627
628         if (bind(iface->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
629                 wpa_printf(MSG_DEBUG, "bind(PF_UNIX) failed: %s",
630                            strerror(errno));
631                 if (connect(iface->fd, (struct sockaddr *) &addr,
632                             sizeof(addr)) < 0) {
633                         wpa_printf(MSG_DEBUG, "Socket exists, but does not "
634                                    "allow connections - assuming it was "
635                                    "leftover from forced program termination");
636                         if (unlink(iface->sock_name) < 0) {
637                                 wpa_printf(MSG_ERROR,
638                                            "Could not unlink existing ctrl_iface socket '%s': %s",
639                                            iface->sock_name, strerror(errno));
640                                 goto fail;
641                         }
642                         if (bind(iface->fd, (struct sockaddr *) &addr,
643                                  sizeof(addr)) < 0) {
644                                 wpa_printf(MSG_ERROR,
645                                            "wpa-priv-iface-init: bind(PF_UNIX): %s",
646                                            strerror(errno));
647                                 goto fail;
648                         }
649                         wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
650                                    "socket '%s'", iface->sock_name);
651                 } else {
652                         wpa_printf(MSG_INFO, "Socket exists and seems to be "
653                                    "in use - cannot override it");
654                         wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
655                                    "not used anymore", iface->sock_name);
656                         goto fail;
657                 }
658         }
659
660         if (chmod(iface->sock_name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
661                 wpa_printf(MSG_ERROR, "chmod: %s", strerror(errno));
662                 goto fail;
663         }
664
665         eloop_register_read_sock(iface->fd, wpa_priv_receive, iface, NULL);
666
667         return iface;
668
669 fail:
670         wpa_priv_interface_deinit(iface);
671         return NULL;
672 }
673
674
675 static int wpa_priv_send_event(struct wpa_priv_interface *iface, int event,
676                                const void *data, size_t data_len)
677 {
678         struct msghdr msg;
679         struct iovec io[2];
680
681         io[0].iov_base = &event;
682         io[0].iov_len = sizeof(event);
683         io[1].iov_base = (u8 *) data;
684         io[1].iov_len = data_len;
685
686         os_memset(&msg, 0, sizeof(msg));
687         msg.msg_iov = io;
688         msg.msg_iovlen = data ? 2 : 1;
689         msg.msg_name = &iface->drv_addr;
690         msg.msg_namelen = sizeof(iface->drv_addr);
691
692         if (sendmsg(iface->fd, &msg, 0) < 0) {
693                 wpa_printf(MSG_ERROR, "sendmsg(wpas_socket): %s",
694                            strerror(errno));
695                 return -1;
696         }
697
698         return 0;
699 }
700
701
702 static void wpa_priv_send_assoc(struct wpa_priv_interface *iface, int event,
703                                 union wpa_event_data *data)
704 {
705         size_t buflen = 3 * sizeof(int);
706         u8 *buf, *pos;
707         int len;
708
709         if (data) {
710                 buflen += data->assoc_info.req_ies_len +
711                         data->assoc_info.resp_ies_len +
712                         data->assoc_info.beacon_ies_len;
713         }
714
715         buf = os_malloc(buflen);
716         if (buf == NULL)
717                 return;
718
719         pos = buf;
720
721         if (data && data->assoc_info.req_ies) {
722                 len = data->assoc_info.req_ies_len;
723                 os_memcpy(pos, &len, sizeof(int));
724                 pos += sizeof(int);
725                 os_memcpy(pos, data->assoc_info.req_ies, len);
726                 pos += len;
727         } else {
728                 len = 0;
729                 os_memcpy(pos, &len, sizeof(int));
730                 pos += sizeof(int);
731         }
732
733         if (data && data->assoc_info.resp_ies) {
734                 len = data->assoc_info.resp_ies_len;
735                 os_memcpy(pos, &len, sizeof(int));
736                 pos += sizeof(int);
737                 os_memcpy(pos, data->assoc_info.resp_ies, len);
738                 pos += len;
739         } else {
740                 len = 0;
741                 os_memcpy(pos, &len, sizeof(int));
742                 pos += sizeof(int);
743         }
744
745         if (data && data->assoc_info.beacon_ies) {
746                 len = data->assoc_info.beacon_ies_len;
747                 os_memcpy(pos, &len, sizeof(int));
748                 pos += sizeof(int);
749                 os_memcpy(pos, data->assoc_info.beacon_ies, len);
750                 pos += len;
751         } else {
752                 len = 0;
753                 os_memcpy(pos, &len, sizeof(int));
754                 pos += sizeof(int);
755         }
756
757         wpa_priv_send_event(iface, event, buf, buflen);
758
759         os_free(buf);
760 }
761
762
763 static void wpa_priv_send_interface_status(struct wpa_priv_interface *iface,
764                                            union wpa_event_data *data)
765 {
766         int ievent;
767         size_t len, maxlen;
768         u8 *buf;
769         char *ifname;
770
771         if (data == NULL)
772                 return;
773
774         ievent = data->interface_status.ievent;
775         maxlen = sizeof(data->interface_status.ifname);
776         ifname = data->interface_status.ifname;
777         for (len = 0; len < maxlen && ifname[len]; len++)
778                 ;
779
780         buf = os_malloc(sizeof(int) + len);
781         if (buf == NULL)
782                 return;
783
784         os_memcpy(buf, &ievent, sizeof(int));
785         os_memcpy(buf + sizeof(int), ifname, len);
786
787         wpa_priv_send_event(iface, PRIVSEP_EVENT_INTERFACE_STATUS,
788                             buf, sizeof(int) + len);
789
790         os_free(buf);
791
792 }
793
794
795 static void wpa_priv_send_ft_response(struct wpa_priv_interface *iface,
796                                       union wpa_event_data *data)
797 {
798         size_t len;
799         u8 *buf, *pos;
800
801         if (data == NULL || data->ft_ies.ies == NULL)
802                 return;
803
804         len = sizeof(int) + ETH_ALEN + data->ft_ies.ies_len;
805         buf = os_malloc(len);
806         if (buf == NULL)
807                 return;
808
809         pos = buf;
810         os_memcpy(pos, &data->ft_ies.ft_action, sizeof(int));
811         pos += sizeof(int);
812         os_memcpy(pos, data->ft_ies.target_ap, ETH_ALEN);
813         pos += ETH_ALEN;
814         os_memcpy(pos, data->ft_ies.ies, data->ft_ies.ies_len);
815
816         wpa_priv_send_event(iface, PRIVSEP_EVENT_FT_RESPONSE, buf, len);
817
818         os_free(buf);
819
820 }
821
822
823 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
824                           union wpa_event_data *data)
825 {
826         struct wpa_priv_interface *iface = ctx;
827
828         wpa_printf(MSG_DEBUG, "%s - event=%d", __func__, event);
829
830         if (!iface->wpas_registered) {
831                 wpa_printf(MSG_DEBUG, "Driver event received, but "
832                            "wpa_supplicant not registered");
833                 return;
834         }
835
836         switch (event) {
837         case EVENT_ASSOC:
838                 wpa_priv_send_assoc(iface, PRIVSEP_EVENT_ASSOC, data);
839                 break;
840         case EVENT_DISASSOC:
841                 wpa_priv_send_event(iface, PRIVSEP_EVENT_DISASSOC, NULL, 0);
842                 break;
843         case EVENT_ASSOCINFO:
844                 if (data == NULL)
845                         return;
846                 wpa_priv_send_assoc(iface, PRIVSEP_EVENT_ASSOCINFO, data);
847                 break;
848         case EVENT_MICHAEL_MIC_FAILURE:
849                 if (data == NULL)
850                         return;
851                 wpa_priv_send_event(iface, PRIVSEP_EVENT_MICHAEL_MIC_FAILURE,
852                                     &data->michael_mic_failure.unicast,
853                                     sizeof(int));
854                 break;
855         case EVENT_SCAN_RESULTS:
856                 wpa_priv_send_event(iface, PRIVSEP_EVENT_SCAN_RESULTS, NULL,
857                                     0);
858                 break;
859         case EVENT_INTERFACE_STATUS:
860                 wpa_priv_send_interface_status(iface, data);
861                 break;
862         case EVENT_PMKID_CANDIDATE:
863                 if (data == NULL)
864                         return;
865                 wpa_priv_send_event(iface, PRIVSEP_EVENT_PMKID_CANDIDATE,
866                                     &data->pmkid_candidate,
867                                     sizeof(struct pmkid_candidate));
868                 break;
869         case EVENT_STKSTART:
870                 if (data == NULL)
871                         return;
872                 wpa_priv_send_event(iface, PRIVSEP_EVENT_STKSTART,
873                                     &data->stkstart.peer, ETH_ALEN);
874                 break;
875         case EVENT_FT_RESPONSE:
876                 wpa_priv_send_ft_response(iface, data);
877                 break;
878         default:
879                 wpa_printf(MSG_DEBUG, "Unsupported driver event %d - TODO",
880                            event);
881                 break;
882         }
883 }
884
885
886 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
887                              const u8 *buf, size_t len)
888 {
889         struct wpa_priv_interface *iface = ctx;
890         struct msghdr msg;
891         struct iovec io[3];
892         int event = PRIVSEP_EVENT_RX_EAPOL;
893
894         wpa_printf(MSG_DEBUG, "RX EAPOL from driver");
895         io[0].iov_base = &event;
896         io[0].iov_len = sizeof(event);
897         io[1].iov_base = (u8 *) src_addr;
898         io[1].iov_len = ETH_ALEN;
899         io[2].iov_base = (u8 *) buf;
900         io[2].iov_len = len;
901
902         os_memset(&msg, 0, sizeof(msg));
903         msg.msg_iov = io;
904         msg.msg_iovlen = 3;
905         msg.msg_name = &iface->drv_addr;
906         msg.msg_namelen = sizeof(iface->drv_addr);
907
908         if (sendmsg(iface->fd, &msg, 0) < 0)
909                 wpa_printf(MSG_ERROR, "sendmsg(wpas_socket): %s",
910                            strerror(errno));
911 }
912
913
914 static void wpa_priv_terminate(int sig, void *signal_ctx)
915 {
916         wpa_printf(MSG_DEBUG, "wpa_priv termination requested");
917         eloop_terminate();
918 }
919
920
921 static void wpa_priv_fd_workaround(void)
922 {
923 #ifdef __linux__
924         int s, i;
925         /* When started from pcmcia-cs scripts, wpa_supplicant might start with
926          * fd 0, 1, and 2 closed. This will cause some issues because many
927          * places in wpa_supplicant are still printing out to stdout. As a
928          * workaround, make sure that fd's 0, 1, and 2 are not used for other
929          * sockets. */
930         for (i = 0; i < 3; i++) {
931                 s = open("/dev/null", O_RDWR);
932                 if (s > 2) {
933                         close(s);
934                         break;
935                 }
936         }
937 #endif /* __linux__ */
938 }
939
940
941 static void usage(void)
942 {
943         printf("wpa_priv v" VERSION_STR "\n"
944                "Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi> and "
945                "contributors\n"
946                "\n"
947                "usage:\n"
948                "  wpa_priv [-Bdd] [-c<ctrl dir>] [-P<pid file>] "
949                "<driver:ifname> \\\n"
950                "           [driver:ifname ...]\n");
951 }
952
953
954 int main(int argc, char *argv[])
955 {
956         int c, i;
957         int ret = -1;
958         char *pid_file = NULL;
959         int daemonize = 0;
960         char *ctrl_dir = "/var/run/wpa_priv";
961         struct wpa_priv_interface *interfaces = NULL, *iface;
962
963         if (os_program_init())
964                 return -1;
965
966         wpa_priv_fd_workaround();
967
968         for (;;) {
969                 c = getopt(argc, argv, "Bc:dP:");
970                 if (c < 0)
971                         break;
972                 switch (c) {
973                 case 'B':
974                         daemonize++;
975                         break;
976                 case 'c':
977                         ctrl_dir = optarg;
978                         break;
979                 case 'd':
980                         wpa_debug_level--;
981                         break;
982                 case 'P':
983                         pid_file = os_rel2abs_path(optarg);
984                         break;
985                 default:
986                         usage();
987                         goto out2;
988                 }
989         }
990
991         if (optind >= argc) {
992                 usage();
993                 goto out2;
994         }
995
996         wpa_printf(MSG_DEBUG, "wpa_priv control directory: '%s'", ctrl_dir);
997
998         if (eloop_init()) {
999                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1000                 goto out2;
1001         }
1002
1003         for (i = optind; i < argc; i++) {
1004                 wpa_printf(MSG_DEBUG, "Adding driver:interface %s", argv[i]);
1005                 iface = wpa_priv_interface_init(ctrl_dir, argv[i]);
1006                 if (iface == NULL)
1007                         goto out;
1008                 iface->next = interfaces;
1009                 interfaces = iface;
1010         }
1011
1012         if (daemonize && os_daemonize(pid_file))
1013                 goto out;
1014
1015         eloop_register_signal_terminate(wpa_priv_terminate, NULL);
1016         eloop_run();
1017
1018         ret = 0;
1019
1020 out:
1021         iface = interfaces;
1022         while (iface) {
1023                 struct wpa_priv_interface *prev = iface;
1024                 iface = iface->next;
1025                 wpa_priv_interface_deinit(prev);
1026         }
1027
1028         eloop_destroy();
1029
1030 out2:
1031         if (daemonize)
1032                 os_daemonize_terminate(pid_file);
1033         os_free(pid_file);
1034         os_program_deinit();
1035
1036         return ret;
1037 }