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