]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/wpa_supplicant/wpa_ctrl.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / wpa_supplicant / wpa_ctrl.c
1 /*
2  * wpa_supplicant/hostapd control interface library
3  * Copyright (c) 2004-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  * $FreeBSD$
15  */
16
17 #include "includes.h"
18
19 #ifdef CONFIG_CTRL_IFACE
20
21 #ifdef CONFIG_CTRL_IFACE_UNIX
22 #include <sys/un.h>
23 #endif /* CONFIG_CTRL_IFACE_UNIX */
24
25 #include "wpa_ctrl.h"
26 #include "common.h"
27
28
29 #if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP)
30 #define CTRL_IFACE_SOCKET
31 #endif /* CONFIG_CTRL_IFACE_UNIX || CONFIG_CTRL_IFACE_UDP */
32
33
34 /**
35  * struct wpa_ctrl - Internal structure for control interface library
36  *
37  * This structure is used by the wpa_supplicant/hostapd control interface
38  * library to store internal data. Programs using the library should not touch
39  * this data directly. They can only use the pointer to the data structure as
40  * an identifier for the control interface connection and use this as one of
41  * the arguments for most of the control interface library functions.
42  */
43 struct wpa_ctrl {
44 #ifdef CONFIG_CTRL_IFACE_UDP
45         int s;
46         struct sockaddr_in local;
47         struct sockaddr_in dest;
48         char *cookie;
49 #endif /* CONFIG_CTRL_IFACE_UDP */
50 #ifdef CONFIG_CTRL_IFACE_UNIX
51         int s;
52         struct sockaddr_un local;
53         struct sockaddr_un dest;
54 #endif /* CONFIG_CTRL_IFACE_UNIX */
55 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
56         HANDLE pipe;
57 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
58 };
59
60
61 #ifdef CONFIG_CTRL_IFACE_UNIX
62
63 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
64 {
65         struct wpa_ctrl *ctrl;
66         static int counter = 0;
67
68         ctrl = os_malloc(sizeof(*ctrl));
69         if (ctrl == NULL)
70                 return NULL;
71         os_memset(ctrl, 0, sizeof(*ctrl));
72
73         ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0);
74         if (ctrl->s < 0) {
75                 os_free(ctrl);
76                 return NULL;
77         }
78
79         ctrl->local.sun_family = AF_UNIX;
80         os_snprintf(ctrl->local.sun_path, sizeof(ctrl->local.sun_path),
81                     "/tmp/wpa_ctrl_%d-%d", getpid(), counter++);
82         if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
83                     sizeof(ctrl->local)) < 0) {
84                 close(ctrl->s);
85                 os_free(ctrl);
86                 return NULL;
87         }
88
89         ctrl->dest.sun_family = AF_UNIX;
90         os_snprintf(ctrl->dest.sun_path, sizeof(ctrl->dest.sun_path), "%s",
91                     ctrl_path);
92         if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
93                     sizeof(ctrl->dest)) < 0) {
94                 close(ctrl->s);
95                 unlink(ctrl->local.sun_path);
96                 os_free(ctrl);
97                 return NULL;
98         }
99
100         return ctrl;
101 }
102
103
104 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
105 {
106         unlink(ctrl->local.sun_path);
107         close(ctrl->s);
108         os_free(ctrl);
109 }
110
111 #endif /* CONFIG_CTRL_IFACE_UNIX */
112
113
114 #ifdef CONFIG_CTRL_IFACE_UDP
115
116 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
117 {
118         struct wpa_ctrl *ctrl;
119         char buf[128];
120         size_t len;
121
122         ctrl = os_malloc(sizeof(*ctrl));
123         if (ctrl == NULL)
124                 return NULL;
125         os_memset(ctrl, 0, sizeof(*ctrl));
126
127         ctrl->s = socket(PF_INET, SOCK_DGRAM, 0);
128         if (ctrl->s < 0) {
129                 perror("socket");
130                 os_free(ctrl);
131                 return NULL;
132         }
133
134         ctrl->local.sin_family = AF_INET;
135         ctrl->local.sin_addr.s_addr = htonl((127 << 24) | 1);
136         if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
137                  sizeof(ctrl->local)) < 0) {
138                 close(ctrl->s);
139                 os_free(ctrl);
140                 return NULL;
141         }
142
143         ctrl->dest.sin_family = AF_INET;
144         ctrl->dest.sin_addr.s_addr = htonl((127 << 24) | 1);
145         ctrl->dest.sin_port = htons(WPA_CTRL_IFACE_PORT);
146         if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
147                     sizeof(ctrl->dest)) < 0) {
148                 perror("connect");
149                 close(ctrl->s);
150                 os_free(ctrl);
151                 return NULL;
152         }
153
154         len = sizeof(buf) - 1;
155         if (wpa_ctrl_request(ctrl, "GET_COOKIE", 10, buf, &len, NULL) == 0) {
156                 buf[len] = '\0';
157                 ctrl->cookie = strdup(buf);
158         }
159
160         return ctrl;
161 }
162
163
164 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
165 {
166         close(ctrl->s);
167         os_free(ctrl->cookie);
168         os_free(ctrl);
169 }
170
171 #endif /* CONFIG_CTRL_IFACE_UDP */
172
173
174 #ifdef CTRL_IFACE_SOCKET
175 int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
176                      char *reply, size_t *reply_len,
177                      void (*msg_cb)(char *msg, size_t len))
178 {
179         struct timeval tv;
180         int res;
181         fd_set rfds;
182         const char *_cmd;
183         char *cmd_buf = NULL;
184         size_t _cmd_len;
185
186 #ifdef CONFIG_CTRL_IFACE_UDP
187         if (ctrl->cookie) {
188                 char *pos;
189                 _cmd_len = strlen(ctrl->cookie) + 1 + cmd_len;
190                 cmd_buf = os_malloc(_cmd_len );
191                 if (cmd_buf == NULL)
192                         return -1;
193                 _cmd = cmd_buf;
194                 pos = cmd_buf;
195                 strcpy(pos, ctrl->cookie);
196                 pos += strlen(ctrl->cookie);
197                 *pos++ = ' ';
198                 memcpy(pos, cmd, cmd_len);
199         } else
200 #endif /* CONFIG_CTRL_IFACE_UDP */
201         {
202                 _cmd = cmd;
203                 _cmd_len = cmd_len;
204         }
205
206         if (send(ctrl->s, _cmd, _cmd_len, 0) < 0) {
207                 os_free(cmd_buf);
208                 return -1;
209         }
210         os_free(cmd_buf);
211
212         for (;;) {
213                 tv.tv_sec = 2;
214                 tv.tv_usec = 0;
215                 FD_ZERO(&rfds);
216                 FD_SET(ctrl->s, &rfds);
217                 res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
218                 if (FD_ISSET(ctrl->s, &rfds)) {
219                         res = recv(ctrl->s, reply, *reply_len, 0);
220                         if (res < 0)
221                                 return res;
222                         if (res > 0 && reply[0] == '<') {
223                                 /* This is an unsolicited message from
224                                  * wpa_supplicant, not the reply to the
225                                  * request. Use msg_cb to report this to the
226                                  * caller. */
227                                 if (msg_cb) {
228                                         /* Make sure the message is nul
229                                          * terminated. */
230                                         if ((size_t) res == *reply_len)
231                                                 res = (*reply_len) - 1;
232                                         reply[res] = '\0';
233                                         msg_cb(reply, res);
234                                 }
235                                 continue;
236                         }
237                         *reply_len = res;
238                         break;
239                 } else {
240                         return -2;
241                 }
242         }
243         return 0;
244 }
245 #endif /* CTRL_IFACE_SOCKET */
246
247
248 static int wpa_ctrl_attach_helper(struct wpa_ctrl *ctrl, int attach)
249 {
250         char buf[10];
251         int ret;
252         size_t len = 10;
253
254         ret = wpa_ctrl_request(ctrl, attach ? "ATTACH" : "DETACH", 6,
255                                buf, &len, NULL);
256         if (ret < 0)
257                 return ret;
258         if (len == 3 && os_memcmp(buf, "OK\n", 3) == 0)
259                 return 0;
260         return -1;
261 }
262
263
264 int wpa_ctrl_attach(struct wpa_ctrl *ctrl)
265 {
266         return wpa_ctrl_attach_helper(ctrl, 1);
267 }
268
269
270 int wpa_ctrl_detach(struct wpa_ctrl *ctrl)
271 {
272         return wpa_ctrl_attach_helper(ctrl, 0);
273 }
274
275
276 #ifdef CTRL_IFACE_SOCKET
277
278 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
279 {
280         int res;
281
282         res = recv(ctrl->s, reply, *reply_len, 0);
283         if (res < 0)
284                 return res;
285         *reply_len = res;
286         return 0;
287 }
288
289
290 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
291 {
292         struct timeval tv;
293         fd_set rfds;
294         tv.tv_sec = 0;
295         tv.tv_usec = 0;
296         FD_ZERO(&rfds);
297         FD_SET(ctrl->s, &rfds);
298         select(ctrl->s + 1, &rfds, NULL, NULL, &tv);
299         return FD_ISSET(ctrl->s, &rfds);
300 }
301
302
303 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
304 {
305         return ctrl->s;
306 }
307
308 #endif /* CTRL_IFACE_SOCKET */
309
310
311 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
312
313 #ifndef WPA_SUPPLICANT_NAMED_PIPE
314 #define WPA_SUPPLICANT_NAMED_PIPE "WpaSupplicant"
315 #endif
316 #define NAMED_PIPE_PREFIX TEXT("\\\\.\\pipe\\") TEXT(WPA_SUPPLICANT_NAMED_PIPE)
317
318 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
319 {
320         struct wpa_ctrl *ctrl;
321         DWORD mode;
322         TCHAR name[256];
323         int i;
324
325         ctrl = os_malloc(sizeof(*ctrl));
326         if (ctrl == NULL)
327                 return NULL;
328         os_memset(ctrl, 0, sizeof(*ctrl));
329
330 #ifdef UNICODE
331         if (ctrl_path == NULL)
332                 _snwprintf(name, 256, NAMED_PIPE_PREFIX);
333         else
334                 _snwprintf(name, 256, NAMED_PIPE_PREFIX TEXT("-%S"),
335                            ctrl_path);
336 #else /* UNICODE */
337         if (ctrl_path == NULL)
338                 os_snprintf(name, 256, NAMED_PIPE_PREFIX);
339         else
340                 os_snprintf(name, 256, NAMED_PIPE_PREFIX "-%s",
341                             ctrl_path);
342 #endif /* UNICODE */
343
344         for (i = 0; i < 10; i++) {
345                 ctrl->pipe = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0,
346                                         NULL, OPEN_EXISTING, 0, NULL);
347                 /*
348                  * Current named pipe server side in wpa_supplicant is
349                  * re-opening the pipe for new clients only after the previous
350                  * one is taken into use. This leaves a small window for race
351                  * conditions when two connections are being opened at almost
352                  * the same time. Retry if that was the case.
353                  */
354                 if (ctrl->pipe != INVALID_HANDLE_VALUE ||
355                     GetLastError() != ERROR_PIPE_BUSY)
356                         break;
357                 WaitNamedPipe(name, 1000);
358         }
359         if (ctrl->pipe == INVALID_HANDLE_VALUE) {
360                 os_free(ctrl);
361                 return NULL;
362         }
363
364         mode = PIPE_READMODE_MESSAGE;
365         if (!SetNamedPipeHandleState(ctrl->pipe, &mode, NULL, NULL)) {
366                 CloseHandle(ctrl->pipe);
367                 os_free(ctrl);
368                 return NULL;
369         }
370
371         return ctrl;
372 }
373
374
375 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
376 {
377         CloseHandle(ctrl->pipe);
378         os_free(ctrl);
379 }
380
381
382 int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
383                      char *reply, size_t *reply_len,
384                      void (*msg_cb)(char *msg, size_t len))
385 {
386         DWORD written;
387         DWORD readlen = *reply_len;
388
389         if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL))
390                 return -1;
391
392         if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL))
393                 return -1;
394         *reply_len = readlen;
395
396         return 0;
397 }
398
399
400 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)
401 {
402         DWORD len = *reply_len;
403         if (!ReadFile(ctrl->pipe, reply, *reply_len, &len, NULL))
404                 return -1;
405         *reply_len = len;
406         return 0;
407 }
408
409
410 int wpa_ctrl_pending(struct wpa_ctrl *ctrl)
411 {
412         DWORD left;
413
414         if (!PeekNamedPipe(ctrl->pipe, NULL, 0, NULL, &left, NULL))
415                 return -1;
416         return left ? 1 : 0;
417 }
418
419
420 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl)
421 {
422         return -1;
423 }
424
425 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
426
427 #endif /* CONFIG_CTRL_IFACE */