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