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