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