~ubuntu-branches/ubuntu/gutsy/wpasupplicant/gutsy

« back to all changes in this revision

Viewing changes to ctrl_iface_unix.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler, Alexander Sack
  • Date: 2007-08-26 16:06:57 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20070826160657-2m8pxoweuxe8f93t
Tags: 0.6.0+0.5.8-0ubuntu1
* New upstream release
* remove patch 11_erroneous_manpage_ref, applied upstream
* remove patch 25_wpas_dbus_unregister_iface_fix, applied upstream

[ Alexander Sack ]
* bumping upstream version to replace development version 0.6.0 with
  this package from stable release branch.
* attempt to fix wierd timeout and high latency issues by going
  back to stable upstream version (0.5.9) (LP: #140763,
  LP: #141233).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * WPA Supplicant / UNIX domain socket -based control interface
 
3
 * Copyright (c) 2004-2005, 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
#include <sys/un.h>
 
17
#include <sys/stat.h>
 
18
#include <grp.h>
 
19
 
 
20
#include "common.h"
 
21
#include "eloop.h"
 
22
#include "config.h"
 
23
#include "eapol_sm.h"
 
24
#include "wpa_supplicant_i.h"
 
25
#include "ctrl_iface.h"
 
26
 
 
27
/* Per-interface ctrl_iface */
 
28
 
 
29
/**
 
30
 * struct wpa_ctrl_dst - Internal data structure of control interface monitors
 
31
 *
 
32
 * This structure is used to store information about registered control
 
33
 * interface monitors into struct wpa_supplicant. This data is private to
 
34
 * ctrl_iface_unix.c and should not be touched directly from other files.
 
35
 */
 
36
struct wpa_ctrl_dst {
 
37
        struct wpa_ctrl_dst *next;
 
38
        struct sockaddr_un addr;
 
39
        socklen_t addrlen;
 
40
        int debug_level;
 
41
        int errors;
 
42
};
 
43
 
 
44
 
 
45
struct ctrl_iface_priv {
 
46
        struct wpa_supplicant *wpa_s;
 
47
        int sock;
 
48
        struct wpa_ctrl_dst *ctrl_dst;
 
49
};
 
50
 
 
51
 
 
52
static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
 
53
                                           int level, const char *buf,
 
54
                                           size_t len);
 
55
 
 
56
 
 
57
static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv *priv,
 
58
                                            struct sockaddr_un *from,
 
59
                                            socklen_t fromlen)
 
60
{
 
61
        struct wpa_ctrl_dst *dst;
 
62
 
 
63
        dst = os_zalloc(sizeof(*dst));
 
64
        if (dst == NULL)
 
65
                return -1;
 
66
        os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
 
67
        dst->addrlen = fromlen;
 
68
        dst->debug_level = MSG_INFO;
 
69
        dst->next = priv->ctrl_dst;
 
70
        priv->ctrl_dst = dst;
 
71
        wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
 
72
                    (u8 *) from->sun_path, fromlen - sizeof(from->sun_family));
 
73
        return 0;
 
74
}
 
75
 
 
76
 
 
77
static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv,
 
78
                                            struct sockaddr_un *from,
 
79
                                            socklen_t fromlen)
 
80
{
 
81
        struct wpa_ctrl_dst *dst, *prev = NULL;
 
82
 
 
83
        dst = priv->ctrl_dst;
 
84
        while (dst) {
 
85
                if (fromlen == dst->addrlen &&
 
86
                    os_memcmp(from->sun_path, dst->addr.sun_path,
 
87
                              fromlen - sizeof(from->sun_family)) == 0) {
 
88
                        if (prev == NULL)
 
89
                                priv->ctrl_dst = dst->next;
 
90
                        else
 
91
                                prev->next = dst->next;
 
92
                        os_free(dst);
 
93
                        wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
 
94
                                    (u8 *) from->sun_path,
 
95
                                    fromlen - sizeof(from->sun_family));
 
96
                        return 0;
 
97
                }
 
98
                prev = dst;
 
99
                dst = dst->next;
 
100
        }
 
101
        return -1;
 
102
}
 
103
 
 
104
 
 
105
static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
 
106
                                           struct sockaddr_un *from,
 
107
                                           socklen_t fromlen,
 
108
                                           char *level)
 
109
{
 
110
        struct wpa_ctrl_dst *dst;
 
111
 
 
112
        wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
 
113
 
 
114
        dst = priv->ctrl_dst;
 
115
        while (dst) {
 
116
                if (fromlen == dst->addrlen &&
 
117
                    os_memcmp(from->sun_path, dst->addr.sun_path,
 
118
                              fromlen - sizeof(from->sun_family)) == 0) {
 
119
                        wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
 
120
                                    "level", (u8 *) from->sun_path,
 
121
                                    fromlen - sizeof(from->sun_family));
 
122
                        dst->debug_level = atoi(level);
 
123
                        return 0;
 
124
                }
 
125
                dst = dst->next;
 
126
        }
 
127
 
 
128
        return -1;
 
129
}
 
130
 
 
131
 
 
132
static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
 
133
                                              void *sock_ctx)
 
134
{
 
135
        struct wpa_supplicant *wpa_s = eloop_ctx;
 
136
        struct ctrl_iface_priv *priv = sock_ctx;
 
137
        char buf[256];
 
138
        int res;
 
139
        struct sockaddr_un from;
 
140
        socklen_t fromlen = sizeof(from);
 
141
        char *reply = NULL;
 
142
        size_t reply_len = 0;
 
143
        int new_attached = 0;
 
144
 
 
145
        res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
 
146
                       (struct sockaddr *) &from, &fromlen);
 
147
        if (res < 0) {
 
148
                perror("recvfrom(ctrl_iface)");
 
149
                return;
 
150
        }
 
151
        buf[res] = '\0';
 
152
 
 
153
        if (os_strcmp(buf, "ATTACH") == 0) {
 
154
                if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen))
 
155
                        reply_len = 1;
 
156
                else {
 
157
                        new_attached = 1;
 
158
                        reply_len = 2;
 
159
                }
 
160
        } else if (os_strcmp(buf, "DETACH") == 0) {
 
161
                if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen))
 
162
                        reply_len = 1;
 
163
                else
 
164
                        reply_len = 2;
 
165
        } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
 
166
                if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
 
167
                                                    buf + 6))
 
168
                        reply_len = 1;
 
169
                else
 
170
                        reply_len = 2;
 
171
        } else {
 
172
                reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
 
173
                                                          &reply_len);
 
174
        }
 
175
 
 
176
        if (reply) {
 
177
                sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
 
178
                       fromlen);
 
179
                os_free(reply);
 
180
        } else if (reply_len == 1) {
 
181
                sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
 
182
                       fromlen);
 
183
        } else if (reply_len == 2) {
 
184
                sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
 
185
                       fromlen);
 
186
        }
 
187
 
 
188
        if (new_attached)
 
189
                eapol_sm_notify_ctrl_attached(wpa_s->eapol);
 
190
}
 
191
 
 
192
 
 
193
static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
 
194
{
 
195
        char *buf;
 
196
        size_t len;
 
197
        char *pbuf, *dir = NULL, *gid_str = NULL;
 
198
 
 
199
        if (wpa_s->conf->ctrl_interface == NULL)
 
200
                return NULL;
 
201
 
 
202
        pbuf = os_strdup(wpa_s->conf->ctrl_interface);
 
203
        if (pbuf == NULL)
 
204
                return NULL;
 
205
        if (os_strncmp(pbuf, "DIR=", 4) == 0) {
 
206
                dir = pbuf + 4;
 
207
                gid_str = os_strstr(dir, " GROUP=");
 
208
                if (gid_str) {
 
209
                        *gid_str = '\0';
 
210
                        gid_str += 7;
 
211
                }
 
212
        } else
 
213
                dir = pbuf;
 
214
 
 
215
        len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
 
216
        buf = os_malloc(len);
 
217
        if (buf == NULL) {
 
218
                os_free(pbuf);
 
219
                return NULL;
 
220
        }
 
221
 
 
222
        os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
 
223
#ifdef __CYGWIN__
 
224
        {
 
225
                /* Windows/WinPcap uses interface names that are not suitable
 
226
                 * as a file name - convert invalid chars to underscores */
 
227
                char *pos = buf;
 
228
                while (*pos) {
 
229
                        if (*pos == '\\')
 
230
                                *pos = '_';
 
231
                        pos++;
 
232
                }
 
233
        }
 
234
#endif /* __CYGWIN__ */
 
235
        os_free(pbuf);
 
236
        return buf;
 
237
}
 
238
 
 
239
 
 
240
static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
 
241
                                             const char *txt, size_t len)
 
242
{
 
243
        struct wpa_supplicant *wpa_s = ctx;
 
244
        if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
 
245
                return;
 
246
        wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
 
247
}
 
248
 
 
249
 
 
250
struct ctrl_iface_priv *
 
251
wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
 
252
{
 
253
        struct ctrl_iface_priv *priv;
 
254
        struct sockaddr_un addr;
 
255
        char *fname = NULL;
 
256
        gid_t gid = 0;
 
257
        int gid_set = 0;
 
258
        char *buf, *dir = NULL, *gid_str = NULL;
 
259
        struct group *grp;
 
260
        char *endp;
 
261
 
 
262
        priv = os_zalloc(sizeof(*priv));
 
263
        if (priv == NULL)
 
264
                return NULL;
 
265
        priv->wpa_s = wpa_s;
 
266
        priv->sock = -1;
 
267
 
 
268
        if (wpa_s->conf->ctrl_interface == NULL)
 
269
                return priv;
 
270
 
 
271
        buf = os_strdup(wpa_s->conf->ctrl_interface);
 
272
        if (buf == NULL)
 
273
                goto fail;
 
274
        if (os_strncmp(buf, "DIR=", 4) == 0) {
 
275
                dir = buf + 4;
 
276
                gid_str = os_strstr(dir, " GROUP=");
 
277
                if (gid_str) {
 
278
                        *gid_str = '\0';
 
279
                        gid_str += 7;
 
280
                }
 
281
        } else {
 
282
                dir = buf;
 
283
                gid_str = wpa_s->conf->ctrl_interface_group;
 
284
        }
 
285
 
 
286
        if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
 
287
                if (errno == EEXIST) {
 
288
                        wpa_printf(MSG_DEBUG, "Using existing control "
 
289
                                   "interface directory.");
 
290
                } else {
 
291
                        perror("mkdir[ctrl_interface]");
 
292
                        goto fail;
 
293
                }
 
294
        }
 
295
 
 
296
        if (gid_str) {
 
297
                grp = getgrnam(gid_str);
 
298
                if (grp) {
 
299
                        gid = grp->gr_gid;
 
300
                        gid_set = 1;
 
301
                        wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
 
302
                                   " (from group name '%s')",
 
303
                                   (int) gid, gid_str);
 
304
                } else {
 
305
                        /* Group name not found - try to parse this as gid */
 
306
                        gid = strtol(gid_str, &endp, 10);
 
307
                        if (*gid_str == '\0' || *endp != '\0') {
 
308
                                wpa_printf(MSG_DEBUG, "CTRL: Invalid group "
 
309
                                           "'%s'", gid_str);
 
310
                                goto fail;
 
311
                        }
 
312
                        gid_set = 1;
 
313
                        wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
 
314
                                   (int) gid);
 
315
                }
 
316
        }
 
317
 
 
318
        if (gid_set && chown(dir, -1, gid) < 0) {
 
319
                perror("chown[ctrl_interface]");
 
320
                goto fail;
 
321
        }
 
322
 
 
323
        if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
 
324
            sizeof(addr.sun_path))
 
325
                goto fail;
 
326
 
 
327
        priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
 
328
        if (priv->sock < 0) {
 
329
                perror("socket(PF_UNIX)");
 
330
                goto fail;
 
331
        }
 
332
 
 
333
        os_memset(&addr, 0, sizeof(addr));
 
334
        addr.sun_family = AF_UNIX;
 
335
        fname = wpa_supplicant_ctrl_iface_path(wpa_s);
 
336
        if (fname == NULL)
 
337
                goto fail;
 
338
        os_strncpy(addr.sun_path, fname, sizeof(addr.sun_path));
 
339
        if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
 
340
                wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
 
341
                           strerror(errno));
 
342
                if (connect(priv->sock, (struct sockaddr *) &addr,
 
343
                            sizeof(addr)) < 0) {
 
344
                        wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
 
345
                                   " allow connections - assuming it was left"
 
346
                                   "over from forced program termination");
 
347
                        if (unlink(fname) < 0) {
 
348
                                perror("unlink[ctrl_iface]");
 
349
                                wpa_printf(MSG_ERROR, "Could not unlink "
 
350
                                           "existing ctrl_iface socket '%s'",
 
351
                                           fname);
 
352
                                goto fail;
 
353
                        }
 
354
                        if (bind(priv->sock, (struct sockaddr *) &addr,
 
355
                                 sizeof(addr)) < 0) {
 
356
                                perror("bind(PF_UNIX)");
 
357
                                goto fail;
 
358
                        }
 
359
                        wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
 
360
                                   "ctrl_iface socket '%s'", fname);
 
361
                } else {
 
362
                        wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
 
363
                                   "be in use - cannot override it");
 
364
                        wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
 
365
                                   "not used anymore", fname);
 
366
                        os_free(fname);
 
367
                        fname = NULL;
 
368
                        goto fail;
 
369
                }
 
370
        }
 
371
 
 
372
        if (gid_set && chown(fname, -1, gid) < 0) {
 
373
                perror("chown[ctrl_interface/ifname]");
 
374
                goto fail;
 
375
        }
 
376
 
 
377
        if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
 
378
                perror("chmod[ctrl_interface/ifname]");
 
379
                goto fail;
 
380
        }
 
381
        os_free(fname);
 
382
 
 
383
        eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
 
384
                                 wpa_s, priv);
 
385
        wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
 
386
 
 
387
        os_free(buf);
 
388
        return priv;
 
389
 
 
390
fail:
 
391
        if (priv->sock >= 0)
 
392
                close(priv->sock);
 
393
        os_free(priv);
 
394
        if (fname) {
 
395
                unlink(fname);
 
396
                os_free(fname);
 
397
        }
 
398
        os_free(buf);
 
399
        return NULL;
 
400
}
 
401
 
 
402
 
 
403
void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
 
404
{
 
405
        struct wpa_ctrl_dst *dst, *prev;
 
406
 
 
407
        if (priv->sock > -1) {
 
408
                char *fname;
 
409
                char *buf, *dir = NULL, *gid_str = NULL;
 
410
                eloop_unregister_read_sock(priv->sock);
 
411
                if (priv->ctrl_dst) {
 
412
                        /*
 
413
                         * Wait a second before closing the control socket if
 
414
                         * there are any attached monitors in order to allow
 
415
                         * them to receive any pending messages.
 
416
                         */
 
417
                        wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
 
418
                                   "monitors to receive messages");
 
419
                        os_sleep(1, 0);
 
420
                }
 
421
                close(priv->sock);
 
422
                priv->sock = -1;
 
423
                fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
 
424
                if (fname) {
 
425
                        unlink(fname);
 
426
                        os_free(fname);
 
427
                }
 
428
 
 
429
                buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
 
430
                if (buf == NULL)
 
431
                        goto free_dst;
 
432
                if (os_strncmp(buf, "DIR=", 4) == 0) {
 
433
                        dir = buf + 4;
 
434
                        gid_str = os_strstr(dir, " GROUP=");
 
435
                        if (gid_str) {
 
436
                                *gid_str = '\0';
 
437
                                gid_str += 7;
 
438
                        }
 
439
                } else
 
440
                        dir = buf;
 
441
 
 
442
                if (rmdir(dir) < 0) {
 
443
                        if (errno == ENOTEMPTY) {
 
444
                                wpa_printf(MSG_DEBUG, "Control interface "
 
445
                                           "directory not empty - leaving it "
 
446
                                           "behind");
 
447
                        } else {
 
448
                                perror("rmdir[ctrl_interface]");
 
449
                        }
 
450
                }
 
451
                os_free(buf);
 
452
        }
 
453
 
 
454
free_dst:
 
455
        dst = priv->ctrl_dst;
 
456
        while (dst) {
 
457
                prev = dst;
 
458
                dst = dst->next;
 
459
                os_free(prev);
 
460
        }
 
461
        os_free(priv);
 
462
}
 
463
 
 
464
 
 
465
/**
 
466
 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
 
467
 * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
 
468
 * @level: Priority level of the message
 
469
 * @buf: Message data
 
470
 * @len: Message length
 
471
 *
 
472
 * Send a packet to all monitor programs attached to the control interface.
 
473
 */
 
474
static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
 
475
                                           int level, const char *buf,
 
476
                                           size_t len)
 
477
{
 
478
        struct wpa_ctrl_dst *dst, *next;
 
479
        char levelstr[10];
 
480
        int idx;
 
481
        struct msghdr msg;
 
482
        struct iovec io[2];
 
483
 
 
484
        dst = priv->ctrl_dst;
 
485
        if (priv->sock < 0 || dst == NULL)
 
486
                return;
 
487
 
 
488
        os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
 
489
        io[0].iov_base = levelstr;
 
490
        io[0].iov_len = os_strlen(levelstr);
 
491
        io[1].iov_base = (char *) buf;
 
492
        io[1].iov_len = len;
 
493
        os_memset(&msg, 0, sizeof(msg));
 
494
        msg.msg_iov = io;
 
495
        msg.msg_iovlen = 2;
 
496
 
 
497
        idx = 0;
 
498
        while (dst) {
 
499
                next = dst->next;
 
500
                if (level >= dst->debug_level) {
 
501
                        wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
 
502
                                    (u8 *) dst->addr.sun_path, dst->addrlen -
 
503
                                    sizeof(dst->addr.sun_family));
 
504
                        msg.msg_name = (void *) &dst->addr;
 
505
                        msg.msg_namelen = dst->addrlen;
 
506
                        if (sendmsg(priv->sock, &msg, 0) < 0) {
 
507
                                perror("sendmsg(CTRL_IFACE monitor)");
 
508
                                dst->errors++;
 
509
                                if (dst->errors > 10) {
 
510
                                        wpa_supplicant_ctrl_iface_detach(
 
511
                                                priv, &dst->addr,
 
512
                                                dst->addrlen);
 
513
                                }
 
514
                        } else
 
515
                                dst->errors = 0;
 
516
                }
 
517
                idx++;
 
518
                dst = next;
 
519
        }
 
520
}
 
521
 
 
522
 
 
523
void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
 
524
{
 
525
        wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor",
 
526
                   priv->wpa_s->ifname);
 
527
        eloop_wait_for_read_sock(priv->sock);
 
528
}
 
529
 
 
530
 
 
531
/* Global ctrl_iface */
 
532
 
 
533
struct ctrl_iface_global_priv {
 
534
        struct wpa_global *global;
 
535
        int sock;
 
536
};
 
537
 
 
538
 
 
539
static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
 
540
                                                     void *sock_ctx)
 
541
{
 
542
        struct wpa_global *global = eloop_ctx;
 
543
        char buf[256];
 
544
        int res;
 
545
        struct sockaddr_un from;
 
546
        socklen_t fromlen = sizeof(from);
 
547
        char *reply;
 
548
        size_t reply_len;
 
549
 
 
550
        res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
 
551
                       (struct sockaddr *) &from, &fromlen);
 
552
        if (res < 0) {
 
553
                perror("recvfrom(ctrl_iface)");
 
554
                return;
 
555
        }
 
556
        buf[res] = '\0';
 
557
 
 
558
        reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
 
559
                                                         &reply_len);
 
560
 
 
561
        if (reply) {
 
562
                sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
 
563
                       fromlen);
 
564
                os_free(reply);
 
565
        } else if (reply_len) {
 
566
                sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
 
567
                       fromlen);
 
568
        }
 
569
}
 
570
 
 
571
 
 
572
struct ctrl_iface_global_priv *
 
573
wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
 
574
{
 
575
        struct ctrl_iface_global_priv *priv;
 
576
        struct sockaddr_un addr;
 
577
 
 
578
        priv = os_zalloc(sizeof(*priv));
 
579
        if (priv == NULL)
 
580
                return NULL;
 
581
        priv->global = global;
 
582
        priv->sock = -1;
 
583
 
 
584
        if (global->params.ctrl_interface == NULL)
 
585
                return priv;
 
586
 
 
587
        wpa_printf(MSG_DEBUG, "Global control interface '%s'",
 
588
                   global->params.ctrl_interface);
 
589
 
 
590
        priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
 
591
        if (priv->sock < 0) {
 
592
                perror("socket(PF_UNIX)");
 
593
                goto fail;
 
594
        }
 
595
 
 
596
        os_memset(&addr, 0, sizeof(addr));
 
597
        addr.sun_family = AF_UNIX;
 
598
        os_strncpy(addr.sun_path, global->params.ctrl_interface,
 
599
                   sizeof(addr.sun_path));
 
600
        if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
 
601
                perror("bind(PF_UNIX)");
 
602
                if (connect(priv->sock, (struct sockaddr *) &addr,
 
603
                            sizeof(addr)) < 0) {
 
604
                        wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
 
605
                                   " allow connections - assuming it was left"
 
606
                                   "over from forced program termination");
 
607
                        if (unlink(global->params.ctrl_interface) < 0) {
 
608
                                perror("unlink[ctrl_iface]");
 
609
                                wpa_printf(MSG_ERROR, "Could not unlink "
 
610
                                           "existing ctrl_iface socket '%s'",
 
611
                                           global->params.ctrl_interface);
 
612
                                goto fail;
 
613
                        }
 
614
                        if (bind(priv->sock, (struct sockaddr *) &addr,
 
615
                                 sizeof(addr)) < 0) {
 
616
                                perror("bind(PF_UNIX)");
 
617
                                goto fail;
 
618
                        }
 
619
                        wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
 
620
                                   "ctrl_iface socket '%s'",
 
621
                                   global->params.ctrl_interface);
 
622
                } else {
 
623
                        wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
 
624
                                   "be in use - cannot override it");
 
625
                        wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
 
626
                                   "not used anymore",
 
627
                                   global->params.ctrl_interface);
 
628
                        goto fail;
 
629
                }
 
630
        }
 
631
 
 
632
        eloop_register_read_sock(priv->sock,
 
633
                                 wpa_supplicant_global_ctrl_iface_receive,
 
634
                                 global, NULL);
 
635
 
 
636
        return priv;
 
637
 
 
638
fail:
 
639
        if (priv->sock >= 0)
 
640
                close(priv->sock);
 
641
        os_free(priv);
 
642
        return NULL;
 
643
}
 
644
 
 
645
 
 
646
void
 
647
wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
 
648
{
 
649
        if (priv->sock >= 0) {
 
650
                eloop_unregister_read_sock(priv->sock);
 
651
                close(priv->sock);
 
652
        }
 
653
        if (priv->global->params.ctrl_interface)
 
654
                unlink(priv->global->params.ctrl_interface);
 
655
        os_free(priv);
 
656
}