]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * WPA Supplicant - driver interaction with BSD net80211 layer | |
3 | * Copyright (c) 2004, Sam Leffler <sam@errno.com> | |
c5121837 | 4 | * Copyright (c) 2004, 2Wire, Inc |
6fc6879b JM |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * Alternatively, this software may be distributed under the terms of BSD | |
11 | * license. | |
12 | * | |
13 | * See README and COPYING for more details. | |
14 | */ | |
15 | ||
16 | #include "includes.h" | |
17 | #include <sys/ioctl.h> | |
18 | ||
19 | #include "common.h" | |
20 | #include "driver.h" | |
21 | #include "eloop.h" | |
90973fb2 | 22 | #include "common/ieee802_11_defs.h" |
6fc6879b JM |
23 | |
24 | #include <net/if.h> | |
5197244a | 25 | #include <net/if_media.h> |
898d6921 JR |
26 | |
27 | #ifdef __NetBSD__ | |
28 | #include <net/if_ether.h> | |
898d6921 | 29 | #else |
6fc6879b | 30 | #include <net/ethernet.h> |
898d6921 | 31 | #endif |
131289d8 | 32 | #include <net/route.h> |
6fc6879b | 33 | |
9b336bce MH |
34 | #ifdef __DragonFly__ |
35 | #include <netproto/802_11/ieee80211_ioctl.h> | |
36 | #include <netproto/802_11/ieee80211_dragonfly.h> | |
37 | #else /* __DragonFly__ */ | |
6fc6879b | 38 | #include <net80211/ieee80211.h> |
6fc6879b | 39 | #include <net80211/ieee80211_ioctl.h> |
c610dba1 | 40 | #include <net80211/ieee80211_crypto.h> |
9b336bce | 41 | #endif /* __DragonFly__ */ |
131289d8 MH |
42 | #if __FreeBSD__ |
43 | #include <net80211/ieee80211_freebsd.h> | |
44 | #endif | |
45 | #if __NetBSD__ | |
46 | #include <net80211/ieee80211_netbsd.h> | |
47 | #endif | |
48 | ||
66d4085f RM |
49 | /* Generic functions for hostapd and wpa_supplicant */ |
50 | ||
51 | static int | |
52 | bsd_set80211var(int s, const char *ifname, int op, const void *arg, int arg_len) | |
53 | { | |
54 | struct ieee80211req ireq; | |
55 | ||
56 | os_memset(&ireq, 0, sizeof(ireq)); | |
57 | os_strlcpy(ireq.i_name, ifname, sizeof(ireq.i_name)); | |
58 | ireq.i_type = op; | |
59 | ireq.i_len = arg_len; | |
60 | ireq.i_data = (void *) arg; | |
61 | ||
62 | if (ioctl(s, SIOCS80211, &ireq) < 0) { | |
63 | fprintf(stderr, "ioctl[SIOCS80211, op %u, len %u]: %s\n", | |
64 | op, arg_len, strerror(errno)); | |
65 | return -1; | |
66 | } | |
67 | return 0; | |
68 | } | |
69 | ||
70 | static int | |
71 | bsd_get80211var(int s, const char *ifname, int op, void *arg, int arg_len) | |
72 | { | |
73 | struct ieee80211req ireq; | |
74 | ||
75 | os_memset(&ireq, 0, sizeof(ireq)); | |
76 | os_strlcpy(ireq.i_name, ifname, sizeof(ireq.i_name)); | |
77 | ireq.i_type = op; | |
78 | ireq.i_len = arg_len; | |
79 | ireq.i_data = arg; | |
80 | ||
81 | if (ioctl(s, SIOCG80211, &ireq) < 0) { | |
82 | fprintf(stderr, "ioctl[SIOCG80211, op %u, len %u]: %s\n", | |
83 | op, arg_len, strerror(errno)); | |
84 | return -1; | |
85 | } | |
86 | return ireq.i_len; | |
87 | } | |
88 | ||
89 | static int | |
90 | bsd_set80211param(int s, const char *ifname, int op, int arg) | |
91 | { | |
92 | struct ieee80211req ireq; | |
93 | ||
94 | os_memset(&ireq, 0, sizeof(ireq)); | |
95 | os_strlcpy(ireq.i_name, ifname, sizeof(ireq.i_name)); | |
96 | ireq.i_type = op; | |
97 | ireq.i_val = arg; | |
98 | ||
99 | if (ioctl(s, SIOCS80211, &ireq) < 0) { | |
100 | fprintf(stderr, "ioctl[SIOCS80211, op %u, arg 0x%x]: %s\n", | |
101 | op, arg, strerror(errno)); | |
102 | return -1; | |
103 | } | |
104 | return 0; | |
105 | } | |
106 | ||
107 | static int | |
108 | bsd_get_ssid(int s, const char *ifname, u8 *ssid) | |
109 | { | |
110 | #ifdef SIOCG80211NWID | |
111 | struct ieee80211_nwid nwid; | |
112 | struct ifreq ifr; | |
113 | ||
114 | os_memset(&ifr, 0, sizeof(ifr)); | |
115 | os_strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); | |
116 | ifr.ifr_data = (void *)&nwid; | |
117 | if (ioctl(s, SIOCG80211NWID, &ifr) < 0 || | |
118 | nwid.i_len > IEEE80211_NWID_LEN) | |
119 | return -1; | |
120 | os_memcpy(ssid, nwid.i_nwid, nwid.i_len); | |
121 | return nwid.i_len; | |
122 | #else | |
123 | return bsd_get80211var(s, ifname, IEEE80211_IOC_SSID, | |
124 | ssid, IEEE80211_NWID_LEN); | |
125 | #endif | |
126 | } | |
127 | ||
128 | static int | |
129 | bsd_set_ssid(int s, const char *ifname, const u8 *ssid, size_t ssid_len) | |
130 | { | |
131 | #ifdef SIOCS80211NWID | |
132 | struct ieee80211_nwid nwid; | |
133 | struct ifreq ifr; | |
134 | ||
135 | os_memcpy(nwid.i_nwid, ssid, ssid_len); | |
136 | nwid.i_len = ssid_len; | |
137 | os_memset(&ifr, 0, sizeof(ifr)); | |
138 | os_strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); | |
139 | ifr.ifr_data = (void *)&nwid; | |
140 | return ioctl(s, SIOCS80211NWID, &ifr); | |
141 | #else | |
142 | return bsd_set80211var(s, ifname, IEEE80211_IOC_SSID, ssid, ssid_len); | |
143 | #endif | |
144 | } | |
145 | ||
5197244a MH |
146 | static int |
147 | bsd_get_if_media(int s, const char *ifname) | |
148 | { | |
149 | struct ifmediareq ifmr; | |
150 | ||
151 | os_memset(&ifmr, 0, sizeof(ifmr)); | |
152 | os_strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name)); | |
153 | ||
154 | if (ioctl(s, SIOCGIFMEDIA, &ifmr) < 0) { | |
155 | wpa_printf(MSG_ERROR, "%s: SIOCGIFMEDIA %s", __func__, | |
156 | strerror(errno)); | |
157 | return -1; | |
158 | } | |
159 | ||
160 | return ifmr.ifm_current; | |
161 | } | |
162 | ||
163 | static int | |
164 | bsd_set_if_media(int s, const char *ifname, int media) | |
165 | { | |
166 | struct ifreq ifr; | |
167 | ||
168 | os_memset(&ifr, 0, sizeof(ifr)); | |
169 | os_strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); | |
170 | ifr.ifr_media = media; | |
171 | ||
172 | if (ioctl(s, SIOCSIFMEDIA, &ifr) < 0) { | |
173 | wpa_printf(MSG_ERROR, "%s: SIOCSIFMEDIA %s", __func__, | |
174 | strerror(errno)); | |
175 | return -1; | |
176 | } | |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
181 | static int | |
182 | bsd_set_mediaopt(int s, const char *ifname, uint32_t mask, uint32_t mode) | |
183 | { | |
184 | int media = bsd_get_if_media(s, ifname); | |
185 | ||
186 | if (media < 0) | |
187 | return -1; | |
188 | media &= ~mask; | |
189 | media |= mode; | |
190 | if (bsd_set_if_media(s, ifname, media) < 0) | |
191 | return -1; | |
192 | return 0; | |
193 | } | |
194 | ||
cbdecd2b MH |
195 | static int |
196 | bsd_del_key(int s, const char *ifname, const u8 *addr, int key_idx) | |
197 | { | |
198 | struct ieee80211req_del_key wk; | |
199 | ||
200 | os_memset(&wk, 0, sizeof(wk)); | |
201 | if (addr == NULL) { | |
202 | wpa_printf(MSG_DEBUG, "%s: key_idx=%d", __func__, key_idx); | |
203 | wk.idk_keyix = key_idx; | |
204 | } else { | |
205 | wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__, | |
206 | MAC2STR(addr)); | |
207 | os_memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN); | |
208 | wk.idk_keyix = (u_int8_t) IEEE80211_KEYIX_NONE; /* XXX */ | |
209 | } | |
210 | ||
211 | return bsd_set80211var(s, ifname, IEEE80211_IOC_DELKEY, &wk, | |
212 | sizeof(wk)); | |
213 | } | |
214 | ||
66d4085f | 215 | |
131289d8 | 216 | #ifdef HOSTAPD |
6fc6879b | 217 | |
c5121837 JM |
218 | /* |
219 | * Avoid conflicts with hostapd definitions by undefining couple of defines | |
220 | * from net80211 header files. | |
221 | */ | |
222 | #undef RSN_VERSION | |
223 | #undef WPA_VERSION | |
224 | #undef WPA_OUI_TYPE | |
225 | ||
c5121837 | 226 | #include "l2_packet/l2_packet.h" |
c5121837 JM |
227 | |
228 | struct bsd_driver_data { | |
229 | struct hostapd_data *hapd; /* back pointer */ | |
230 | ||
231 | char iface[IFNAMSIZ + 1]; | |
232 | struct l2_packet_data *sock_xmit; /* raw packet xmit socket */ | |
233 | int ioctl_sock; /* socket for ioctl() use */ | |
234 | int wext_sock; /* socket for wireless events */ | |
235 | }; | |
236 | ||
731723a5 JM |
237 | static int bsd_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, |
238 | int reason_code); | |
c5121837 JM |
239 | |
240 | static int | |
241 | set80211var(struct bsd_driver_data *drv, int op, const void *arg, int arg_len) | |
242 | { | |
66d4085f | 243 | return bsd_set80211var(drv->ioctl_sock, drv->iface, op, arg, arg_len); |
c5121837 JM |
244 | } |
245 | ||
246 | static int | |
247 | get80211var(struct bsd_driver_data *drv, int op, void *arg, int arg_len) | |
248 | { | |
66d4085f | 249 | return bsd_get80211var(drv->ioctl_sock, drv->iface, op, arg, arg_len); |
c5121837 JM |
250 | } |
251 | ||
252 | static int | |
253 | set80211param(struct bsd_driver_data *drv, int op, int arg) | |
254 | { | |
66d4085f | 255 | return bsd_set80211param(drv->ioctl_sock, drv->iface, op, arg); |
c5121837 JM |
256 | } |
257 | ||
258 | static const char * | |
259 | ether_sprintf(const u8 *addr) | |
260 | { | |
261 | static char buf[sizeof(MACSTR)]; | |
262 | ||
263 | if (addr != NULL) | |
264 | snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr)); | |
265 | else | |
266 | snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0); | |
267 | return buf; | |
268 | } | |
269 | ||
270 | /* | |
271 | * Configure WPA parameters. | |
272 | */ | |
273 | static int | |
af586419 | 274 | bsd_configure_wpa(struct bsd_driver_data *drv, struct wpa_bss_params *params) |
c5121837 JM |
275 | { |
276 | static const char *ciphernames[] = | |
277 | { "WEP", "TKIP", "AES-OCB", "AES-CCM", "CKIP", "NONE" }; | |
c5121837 JM |
278 | int v; |
279 | ||
af586419 | 280 | switch (params->wpa_group) { |
c5121837 JM |
281 | case WPA_CIPHER_CCMP: |
282 | v = IEEE80211_CIPHER_AES_CCM; | |
283 | break; | |
284 | case WPA_CIPHER_TKIP: | |
285 | v = IEEE80211_CIPHER_TKIP; | |
286 | break; | |
287 | case WPA_CIPHER_WEP104: | |
288 | v = IEEE80211_CIPHER_WEP; | |
289 | break; | |
290 | case WPA_CIPHER_WEP40: | |
291 | v = IEEE80211_CIPHER_WEP; | |
292 | break; | |
293 | case WPA_CIPHER_NONE: | |
294 | v = IEEE80211_CIPHER_NONE; | |
295 | break; | |
296 | default: | |
297 | printf("Unknown group key cipher %u\n", | |
af586419 | 298 | params->wpa_group); |
c5121837 JM |
299 | return -1; |
300 | } | |
301 | wpa_printf(MSG_DEBUG, "%s: group key cipher=%s (%u)", | |
302 | __func__, ciphernames[v], v); | |
303 | if (set80211param(drv, IEEE80211_IOC_MCASTCIPHER, v)) { | |
304 | printf("Unable to set group key cipher to %u (%s)\n", | |
305 | v, ciphernames[v]); | |
306 | return -1; | |
307 | } | |
308 | if (v == IEEE80211_CIPHER_WEP) { | |
309 | /* key length is done only for specific ciphers */ | |
af586419 | 310 | v = (params->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5); |
c5121837 JM |
311 | if (set80211param(drv, IEEE80211_IOC_MCASTKEYLEN, v)) { |
312 | printf("Unable to set group key length to %u\n", v); | |
313 | return -1; | |
314 | } | |
315 | } | |
316 | ||
317 | v = 0; | |
af586419 | 318 | if (params->wpa_pairwise & WPA_CIPHER_CCMP) |
c5121837 | 319 | v |= 1<<IEEE80211_CIPHER_AES_CCM; |
af586419 | 320 | if (params->wpa_pairwise & WPA_CIPHER_TKIP) |
c5121837 | 321 | v |= 1<<IEEE80211_CIPHER_TKIP; |
af586419 | 322 | if (params->wpa_pairwise & WPA_CIPHER_NONE) |
c5121837 JM |
323 | v |= 1<<IEEE80211_CIPHER_NONE; |
324 | wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v); | |
325 | if (set80211param(drv, IEEE80211_IOC_UCASTCIPHERS, v)) { | |
326 | printf("Unable to set pairwise key ciphers to 0x%x\n", v); | |
327 | return -1; | |
328 | } | |
329 | ||
330 | wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x", | |
af586419 JM |
331 | __func__, params->wpa_key_mgmt); |
332 | if (set80211param(drv, IEEE80211_IOC_KEYMGTALGS, params->wpa_key_mgmt)) | |
333 | { | |
c5121837 | 334 | printf("Unable to set key management algorithms to 0x%x\n", |
af586419 | 335 | params->wpa_key_mgmt); |
c5121837 JM |
336 | return -1; |
337 | } | |
338 | ||
339 | v = 0; | |
af586419 | 340 | if (params->rsn_preauth) |
c5121837 JM |
341 | v |= BIT(0); |
342 | wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x", | |
af586419 | 343 | __func__, params->rsn_preauth); |
c5121837 JM |
344 | if (set80211param(drv, IEEE80211_IOC_RSNCAPS, v)) { |
345 | printf("Unable to set RSN capabilities to 0x%x\n", v); | |
346 | return -1; | |
347 | } | |
348 | ||
af586419 JM |
349 | wpa_printf(MSG_DEBUG, "%s: enable WPA= 0x%x", __func__, params->wpa); |
350 | if (set80211param(drv, IEEE80211_IOC_WPA, params->wpa)) { | |
351 | printf("Unable to set WPA to %u\n", params->wpa); | |
c5121837 JM |
352 | return -1; |
353 | } | |
354 | return 0; | |
355 | } | |
356 | ||
357 | ||
358 | static int | |
359 | bsd_set_iface_flags(void *priv, int dev_up) | |
360 | { | |
361 | struct bsd_driver_data *drv = priv; | |
362 | struct ifreq ifr; | |
363 | ||
364 | wpa_printf(MSG_DEBUG, "%s: dev_up=%d", __func__, dev_up); | |
365 | ||
366 | if (drv->ioctl_sock < 0) | |
367 | return -1; | |
368 | ||
369 | memset(&ifr, 0, sizeof(ifr)); | |
66d4085f | 370 | os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name)); |
c5121837 JM |
371 | |
372 | if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) { | |
373 | perror("ioctl[SIOCGIFFLAGS]"); | |
374 | return -1; | |
375 | } | |
376 | ||
377 | if (dev_up) | |
378 | ifr.ifr_flags |= IFF_UP; | |
379 | else | |
380 | ifr.ifr_flags &= ~IFF_UP; | |
381 | ||
382 | if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) { | |
383 | perror("ioctl[SIOCSIFFLAGS]"); | |
384 | return -1; | |
385 | } | |
386 | ||
c5121837 JM |
387 | return 0; |
388 | } | |
389 | ||
390 | static int | |
e3bd3912 | 391 | bsd_set_ieee8021x(void *priv, struct wpa_bss_params *params) |
c5121837 JM |
392 | { |
393 | struct bsd_driver_data *drv = priv; | |
c5121837 | 394 | |
af586419 | 395 | wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, params->enabled); |
c5121837 | 396 | |
af586419 | 397 | if (!params->enabled) { |
c5121837 JM |
398 | /* XXX restore state */ |
399 | return set80211param(priv, IEEE80211_IOC_AUTHMODE, | |
400 | IEEE80211_AUTH_AUTO); | |
401 | } | |
af586419 JM |
402 | if (!params->wpa && !params->ieee802_1x) { |
403 | hostapd_logger(drv->hapd, NULL, HOSTAPD_MODULE_DRIVER, | |
c5121837 JM |
404 | HOSTAPD_LEVEL_WARNING, "No 802.1X or WPA enabled!"); |
405 | return -1; | |
406 | } | |
c9b9e494 | 407 | if (params->wpa && bsd_configure_wpa(drv, params) != 0) { |
af586419 | 408 | hostapd_logger(drv->hapd, NULL, HOSTAPD_MODULE_DRIVER, |
c5121837 JM |
409 | HOSTAPD_LEVEL_WARNING, "Error configuring WPA state!"); |
410 | return -1; | |
411 | } | |
412 | if (set80211param(priv, IEEE80211_IOC_AUTHMODE, | |
af586419 JM |
413 | (params->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) { |
414 | hostapd_logger(drv->hapd, NULL, HOSTAPD_MODULE_DRIVER, | |
c5121837 JM |
415 | HOSTAPD_LEVEL_WARNING, "Error enabling WPA/802.1X!"); |
416 | return -1; | |
417 | } | |
418 | return bsd_set_iface_flags(priv, 1); | |
419 | } | |
420 | ||
421 | static int | |
422 | bsd_set_privacy(const char *ifname, void *priv, int enabled) | |
423 | { | |
424 | struct bsd_driver_data *drv = priv; | |
425 | ||
426 | wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled); | |
427 | ||
428 | return set80211param(drv, IEEE80211_IOC_PRIVACY, enabled); | |
429 | } | |
430 | ||
431 | static int | |
432 | bsd_set_sta_authorized(void *priv, const u8 *addr, int authorized) | |
433 | { | |
434 | struct bsd_driver_data *drv = priv; | |
435 | struct ieee80211req_mlme mlme; | |
436 | ||
437 | wpa_printf(MSG_DEBUG, "%s: addr=%s authorized=%d", | |
438 | __func__, ether_sprintf(addr), authorized); | |
439 | ||
440 | if (authorized) | |
441 | mlme.im_op = IEEE80211_MLME_AUTHORIZE; | |
442 | else | |
443 | mlme.im_op = IEEE80211_MLME_UNAUTHORIZE; | |
444 | mlme.im_reason = 0; | |
445 | memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN); | |
446 | return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme)); | |
447 | } | |
448 | ||
449 | static int | |
450 | bsd_sta_set_flags(void *priv, const u8 *addr, int total_flags, int flags_or, | |
451 | int flags_and) | |
452 | { | |
453 | /* For now, only support setting Authorized flag */ | |
0de39516 | 454 | if (flags_or & WPA_STA_AUTHORIZED) |
c5121837 | 455 | return bsd_set_sta_authorized(priv, addr, 1); |
0de39516 | 456 | if (!(flags_and & WPA_STA_AUTHORIZED)) |
c5121837 JM |
457 | return bsd_set_sta_authorized(priv, addr, 0); |
458 | return 0; | |
459 | } | |
460 | ||
c5121837 | 461 | static int |
71934751 | 462 | bsd_set_key(const char *ifname, void *priv, enum wpa_alg alg, |
c5121837 JM |
463 | const u8 *addr, int key_idx, int set_tx, const u8 *seq, |
464 | size_t seq_len, const u8 *key, size_t key_len) | |
465 | { | |
466 | struct bsd_driver_data *drv = priv; | |
467 | struct ieee80211req_key wk; | |
468 | u_int8_t cipher; | |
469 | ||
470 | if (alg == WPA_ALG_NONE) | |
cbdecd2b | 471 | return bsd_del_key(drv->ioctl_sock, drv->iface, addr, key_idx); |
c5121837 JM |
472 | |
473 | wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%s key_idx=%d", | |
474 | __func__, alg, ether_sprintf(addr), key_idx); | |
475 | ||
476 | if (alg == WPA_ALG_WEP) | |
477 | cipher = IEEE80211_CIPHER_WEP; | |
478 | else if (alg == WPA_ALG_TKIP) | |
479 | cipher = IEEE80211_CIPHER_TKIP; | |
480 | else if (alg == WPA_ALG_CCMP) | |
481 | cipher = IEEE80211_CIPHER_AES_CCM; | |
482 | else { | |
483 | printf("%s: unknown/unsupported algorithm %d\n", | |
484 | __func__, alg); | |
485 | return -1; | |
486 | } | |
487 | ||
488 | if (key_len > sizeof(wk.ik_keydata)) { | |
92e63aaf | 489 | printf("%s: key length %d too big\n", __func__, (int) key_len); |
c5121837 JM |
490 | return -3; |
491 | } | |
492 | ||
493 | memset(&wk, 0, sizeof(wk)); | |
494 | wk.ik_type = cipher; | |
495 | wk.ik_flags = IEEE80211_KEY_RECV | IEEE80211_KEY_XMIT; | |
496 | if (addr == NULL) { | |
497 | memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN); | |
498 | wk.ik_keyix = key_idx; | |
499 | wk.ik_flags |= IEEE80211_KEY_DEFAULT; | |
500 | } else { | |
501 | memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN); | |
502 | wk.ik_keyix = IEEE80211_KEYIX_NONE; | |
503 | } | |
504 | wk.ik_keylen = key_len; | |
505 | memcpy(wk.ik_keydata, key, key_len); | |
506 | ||
507 | return set80211var(drv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)); | |
508 | } | |
509 | ||
510 | ||
511 | static int | |
512 | bsd_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx, | |
513 | u8 *seq) | |
514 | { | |
515 | struct bsd_driver_data *drv = priv; | |
516 | struct ieee80211req_key wk; | |
517 | ||
518 | wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d", | |
519 | __func__, ether_sprintf(addr), idx); | |
520 | ||
521 | memset(&wk, 0, sizeof(wk)); | |
522 | if (addr == NULL) | |
523 | memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN); | |
524 | else | |
525 | memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN); | |
526 | wk.ik_keyix = idx; | |
527 | ||
528 | if (get80211var(drv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)) < 0) { | |
529 | printf("Failed to get encryption.\n"); | |
530 | return -1; | |
531 | } | |
532 | ||
533 | #ifdef WORDS_BIGENDIAN | |
534 | { | |
535 | /* | |
536 | * wk.ik_keytsc is in host byte order (big endian), need to | |
537 | * swap it to match with the byte order used in WPA. | |
538 | */ | |
539 | int i; | |
540 | u8 tmp[WPA_KEY_RSC_LEN]; | |
541 | memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc)); | |
542 | for (i = 0; i < WPA_KEY_RSC_LEN; i++) { | |
543 | seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1]; | |
544 | } | |
545 | } | |
546 | #else /* WORDS_BIGENDIAN */ | |
547 | memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc)); | |
548 | #endif /* WORDS_BIGENDIAN */ | |
549 | return 0; | |
550 | } | |
551 | ||
552 | ||
553 | static int | |
554 | bsd_flush(void *priv) | |
555 | { | |
556 | u8 allsta[IEEE80211_ADDR_LEN]; | |
557 | ||
558 | memset(allsta, 0xff, IEEE80211_ADDR_LEN); | |
731723a5 | 559 | return bsd_sta_deauth(priv, NULL, allsta, IEEE80211_REASON_AUTH_LEAVE); |
c5121837 JM |
560 | } |
561 | ||
562 | ||
563 | static int | |
564 | bsd_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data, | |
565 | const u8 *addr) | |
566 | { | |
567 | struct bsd_driver_data *drv = priv; | |
568 | struct ieee80211req_sta_stats stats; | |
569 | ||
570 | memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN); | |
571 | if (get80211var(drv, IEEE80211_IOC_STA_STATS, &stats, sizeof(stats)) > 0) { | |
572 | /* XXX? do packets counts include non-data frames? */ | |
573 | data->rx_packets = stats.is_stats.ns_rx_data; | |
574 | data->rx_bytes = stats.is_stats.ns_rx_bytes; | |
575 | data->tx_packets = stats.is_stats.ns_tx_data; | |
576 | data->tx_bytes = stats.is_stats.ns_tx_bytes; | |
577 | } | |
578 | return 0; | |
579 | } | |
580 | ||
581 | static int | |
582 | bsd_set_opt_ie(const char *ifname, void *priv, const u8 *ie, size_t ie_len) | |
583 | { | |
584 | /* | |
585 | * Do nothing; we setup parameters at startup that define the | |
586 | * contents of the beacon information element. | |
587 | */ | |
588 | return 0; | |
589 | } | |
590 | ||
591 | static int | |
731723a5 | 592 | bsd_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, int reason_code) |
c5121837 JM |
593 | { |
594 | struct bsd_driver_data *drv = priv; | |
595 | struct ieee80211req_mlme mlme; | |
596 | ||
597 | wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d", | |
598 | __func__, ether_sprintf(addr), reason_code); | |
599 | ||
600 | mlme.im_op = IEEE80211_MLME_DEAUTH; | |
601 | mlme.im_reason = reason_code; | |
602 | memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN); | |
603 | return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme)); | |
604 | } | |
605 | ||
606 | static int | |
731723a5 JM |
607 | bsd_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr, |
608 | int reason_code) | |
c5121837 JM |
609 | { |
610 | struct bsd_driver_data *drv = priv; | |
611 | struct ieee80211req_mlme mlme; | |
612 | ||
613 | wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d", | |
614 | __func__, ether_sprintf(addr), reason_code); | |
615 | ||
616 | mlme.im_op = IEEE80211_MLME_DISASSOC; | |
617 | mlme.im_reason = reason_code; | |
618 | memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN); | |
619 | return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme)); | |
620 | } | |
621 | ||
1d041bec | 622 | static void |
c5121837 JM |
623 | bsd_new_sta(struct bsd_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN]) |
624 | { | |
625 | struct hostapd_data *hapd = drv->hapd; | |
626 | struct ieee80211req_wpaie ie; | |
627 | int ielen = 0; | |
628 | u8 *iebuf = NULL; | |
629 | ||
630 | /* | |
631 | * Fetch and validate any negotiated WPA/RSN parameters. | |
632 | */ | |
633 | memset(&ie, 0, sizeof(ie)); | |
634 | memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN); | |
635 | if (get80211var(drv, IEEE80211_IOC_WPAIE, &ie, sizeof(ie)) < 0) { | |
636 | printf("Failed to get WPA/RSN information element.\n"); | |
637 | goto no_ie; | |
638 | } | |
639 | iebuf = ie.wpa_ie; | |
640 | ielen = ie.wpa_ie[1]; | |
641 | if (ielen == 0) | |
642 | iebuf = NULL; | |
643 | else | |
644 | ielen += 2; | |
645 | ||
646 | no_ie: | |
1d041bec | 647 | drv_event_assoc(hapd, addr, iebuf, ielen); |
c5121837 JM |
648 | } |
649 | ||
c5121837 JM |
650 | static void |
651 | bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx) | |
652 | { | |
653 | struct bsd_driver_data *drv = ctx; | |
c5121837 JM |
654 | char buf[2048]; |
655 | struct if_announcemsghdr *ifan; | |
656 | struct rt_msghdr *rtm; | |
657 | struct ieee80211_michael_event *mic; | |
658 | struct ieee80211_join_event *join; | |
659 | struct ieee80211_leave_event *leave; | |
660 | int n; | |
1cd973d5 | 661 | union wpa_event_data data; |
c5121837 JM |
662 | |
663 | n = read(sock, buf, sizeof(buf)); | |
664 | if (n < 0) { | |
665 | if (errno != EINTR && errno != EAGAIN) | |
666 | perror("read(PF_ROUTE)"); | |
667 | return; | |
668 | } | |
669 | ||
670 | rtm = (struct rt_msghdr *) buf; | |
671 | if (rtm->rtm_version != RTM_VERSION) { | |
672 | wpa_printf(MSG_DEBUG, "Routing message version %d not " | |
673 | "understood\n", rtm->rtm_version); | |
674 | return; | |
675 | } | |
676 | ifan = (struct if_announcemsghdr *) rtm; | |
677 | switch (rtm->rtm_type) { | |
678 | case RTM_IEEE80211: | |
679 | switch (ifan->ifan_what) { | |
680 | case RTM_IEEE80211_ASSOC: | |
681 | case RTM_IEEE80211_REASSOC: | |
682 | case RTM_IEEE80211_DISASSOC: | |
683 | case RTM_IEEE80211_SCAN: | |
684 | break; | |
685 | case RTM_IEEE80211_LEAVE: | |
686 | leave = (struct ieee80211_leave_event *) &ifan[1]; | |
69378b79 | 687 | drv_event_disassoc(drv->hapd, leave->iev_addr); |
c5121837 JM |
688 | break; |
689 | case RTM_IEEE80211_JOIN: | |
690 | #ifdef RTM_IEEE80211_REJOIN | |
691 | case RTM_IEEE80211_REJOIN: | |
692 | #endif | |
693 | join = (struct ieee80211_join_event *) &ifan[1]; | |
694 | bsd_new_sta(drv, join->iev_addr); | |
695 | break; | |
696 | case RTM_IEEE80211_REPLAY: | |
697 | /* ignore */ | |
698 | break; | |
699 | case RTM_IEEE80211_MICHAEL: | |
700 | mic = (struct ieee80211_michael_event *) &ifan[1]; | |
701 | wpa_printf(MSG_DEBUG, | |
702 | "Michael MIC failure wireless event: " | |
703 | "keyix=%u src_addr=" MACSTR, mic->iev_keyix, | |
704 | MAC2STR(mic->iev_src)); | |
1cd973d5 JM |
705 | os_memset(&data, 0, sizeof(data)); |
706 | data.michael_mic_failure.unicast = 1; | |
707 | data.michael_mic_failure.src = mic->iev_src; | |
708 | wpa_supplicant_event(drv->hapd, | |
709 | EVENT_MICHAEL_MIC_FAILURE, &data); | |
c5121837 JM |
710 | break; |
711 | } | |
712 | break; | |
713 | } | |
714 | } | |
715 | ||
716 | static int | |
717 | bsd_wireless_event_init(struct bsd_driver_data *drv) | |
718 | { | |
719 | int s; | |
720 | ||
721 | drv->wext_sock = -1; | |
722 | ||
723 | s = socket(PF_ROUTE, SOCK_RAW, 0); | |
724 | if (s < 0) { | |
725 | perror("socket(PF_ROUTE,SOCK_RAW)"); | |
726 | return -1; | |
727 | } | |
728 | eloop_register_read_sock(s, bsd_wireless_event_receive, drv, NULL); | |
729 | drv->wext_sock = s; | |
730 | ||
731 | return 0; | |
732 | } | |
733 | ||
734 | static void | |
735 | bsd_wireless_event_deinit(struct bsd_driver_data *drv) | |
736 | { | |
737 | if (drv->wext_sock < 0) | |
738 | return; | |
739 | eloop_unregister_read_sock(drv->wext_sock); | |
740 | close(drv->wext_sock); | |
741 | } | |
742 | ||
743 | ||
744 | static int | |
745 | bsd_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len, | |
746 | int encrypt, const u8 *own_addr) | |
747 | { | |
748 | struct bsd_driver_data *drv = priv; | |
11386396 | 749 | unsigned char *bp; |
c5121837 JM |
750 | struct l2_ethhdr *eth; |
751 | size_t len; | |
752 | int status; | |
753 | ||
754 | /* | |
11386396 | 755 | * Prepend the Ethernet header. If the caller left us |
c5121837 JM |
756 | * space at the front we could just insert it but since |
757 | * we don't know we copy to a local buffer. Given the frequency | |
758 | * and size of frames this probably doesn't matter. | |
759 | */ | |
760 | len = data_len + sizeof(struct l2_ethhdr); | |
11386396 MH |
761 | bp = os_zalloc(len); |
762 | if (bp == NULL) { | |
763 | wpa_printf(MSG_ERROR, "malloc() failed for bsd_send_eapol" | |
764 | "(len=%lu)", (unsigned long) len); | |
765 | return -1; | |
c5121837 JM |
766 | } |
767 | eth = (struct l2_ethhdr *) bp; | |
11386396 MH |
768 | os_memcpy(eth->h_dest, addr, ETH_ALEN); |
769 | os_memcpy(eth->h_source, own_addr, ETH_ALEN); | |
c5121837 | 770 | eth->h_proto = htons(ETH_P_EAPOL); |
11386396 | 771 | os_memcpy(eth + 1, data, data_len); |
c5121837 JM |
772 | |
773 | wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", bp, len); | |
774 | ||
775 | status = l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, bp, len); | |
776 | ||
11386396 | 777 | os_free(bp); |
c5121837 JM |
778 | return status; |
779 | } | |
780 | ||
781 | static void | |
782 | handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len) | |
783 | { | |
784 | struct bsd_driver_data *drv = ctx; | |
baac6490 JM |
785 | drv_event_eapol_rx(drv->hapd, src_addr, buf + sizeof(struct l2_ethhdr), |
786 | len - sizeof(struct l2_ethhdr)); | |
c5121837 JM |
787 | } |
788 | ||
789 | static int | |
66d4085f | 790 | hostapd_bsd_get_ssid(const char *ifname, void *priv, u8 *buf, int len) |
c5121837 JM |
791 | { |
792 | struct bsd_driver_data *drv = priv; | |
66d4085f | 793 | int ssid_len; |
c5121837 | 794 | |
66d4085f | 795 | ssid_len = bsd_get_ssid(drv->ioctl_sock, drv->iface, buf); |
c5121837 JM |
796 | wpa_printf(MSG_DEBUG, "%s: ssid=\"%.*s\"", __func__, ssid_len, buf); |
797 | ||
798 | return ssid_len; | |
799 | } | |
800 | ||
801 | static int | |
66d4085f | 802 | hostapd_bsd_set_ssid(const char *ifname, void *priv, const u8 *buf, int len) |
c5121837 JM |
803 | { |
804 | struct bsd_driver_data *drv = priv; | |
805 | ||
806 | wpa_printf(MSG_DEBUG, "%s: ssid=\"%.*s\"", __func__, len, buf); | |
807 | ||
66d4085f | 808 | return bsd_set_ssid(drv->ioctl_sock, drv->iface, buf, len); |
c5121837 JM |
809 | } |
810 | ||
5197244a MH |
811 | static int |
812 | bsd_set_freq(void *priv, struct hostapd_freq_params *freq) | |
813 | { | |
814 | struct bsd_driver_data *drv = priv; | |
815 | struct ieee80211chanreq creq; | |
816 | uint32_t mode; | |
817 | ||
818 | if (freq->channel < 14) | |
819 | mode = IFM_IEEE80211_11G; | |
820 | else if (freq->channel == 14) | |
821 | mode = IFM_IEEE80211_11B; | |
822 | else | |
823 | mode = IFM_IEEE80211_11A; | |
824 | if (bsd_set_mediaopt(drv->ioctl_sock, drv->iface, IFM_MMASK, | |
825 | mode) < 0) { | |
826 | wpa_printf(MSG_ERROR, "%s: failed to set modulation mode", | |
827 | __func__); | |
828 | return -1; | |
829 | } | |
830 | ||
831 | os_memset(&creq, 0, sizeof(creq)); | |
832 | os_strlcpy(creq.i_name, drv->iface, sizeof(creq.i_name)); | |
833 | creq.i_channel = freq->channel; | |
834 | return ioctl(drv->ioctl_sock, SIOCS80211CHANNEL, &creq); | |
835 | } | |
836 | ||
c5121837 | 837 | static void * |
92f475b4 | 838 | bsd_init(struct hostapd_data *hapd, struct wpa_init_params *params) |
c5121837 JM |
839 | { |
840 | struct bsd_driver_data *drv; | |
841 | ||
842 | drv = os_zalloc(sizeof(struct bsd_driver_data)); | |
843 | if (drv == NULL) { | |
844 | printf("Could not allocate memory for bsd driver data\n"); | |
845 | goto bad; | |
846 | } | |
847 | ||
848 | drv->hapd = hapd; | |
849 | drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0); | |
850 | if (drv->ioctl_sock < 0) { | |
851 | perror("socket[PF_INET,SOCK_DGRAM]"); | |
852 | goto bad; | |
853 | } | |
412036f5 | 854 | memcpy(drv->iface, params->ifname, sizeof(drv->iface)); |
c5121837 JM |
855 | |
856 | drv->sock_xmit = l2_packet_init(drv->iface, NULL, ETH_P_EAPOL, | |
857 | handle_read, drv, 1); | |
858 | if (drv->sock_xmit == NULL) | |
859 | goto bad; | |
412036f5 | 860 | if (l2_packet_get_own_addr(drv->sock_xmit, params->own_addr)) |
c5121837 JM |
861 | goto bad; |
862 | ||
863 | bsd_set_iface_flags(drv, 0); /* mark down during setup */ | |
864 | if (bsd_wireless_event_init(drv)) | |
865 | goto bad; | |
866 | ||
5197244a MH |
867 | if (bsd_set_mediaopt(drv->ioctl_sock, drv->iface, IFM_OMASK, |
868 | IFM_IEEE80211_HOSTAP) < 0) { | |
869 | wpa_printf(MSG_ERROR, "%s: failed to set operation mode", | |
870 | __func__); | |
871 | goto bad; | |
872 | } | |
873 | ||
c5121837 JM |
874 | return drv; |
875 | bad: | |
876 | if (drv->sock_xmit != NULL) | |
877 | l2_packet_deinit(drv->sock_xmit); | |
878 | if (drv->ioctl_sock >= 0) | |
879 | close(drv->ioctl_sock); | |
880 | if (drv != NULL) | |
82f36163 | 881 | os_free(drv); |
c5121837 JM |
882 | return NULL; |
883 | } | |
884 | ||
885 | ||
886 | static void | |
887 | bsd_deinit(void *priv) | |
888 | { | |
889 | struct bsd_driver_data *drv = priv; | |
890 | ||
891 | bsd_wireless_event_deinit(drv); | |
892 | (void) bsd_set_iface_flags(drv, 0); | |
893 | if (drv->ioctl_sock >= 0) | |
894 | close(drv->ioctl_sock); | |
895 | if (drv->sock_xmit != NULL) | |
896 | l2_packet_deinit(drv->sock_xmit); | |
82f36163 | 897 | os_free(drv); |
c5121837 JM |
898 | } |
899 | ||
900 | const struct wpa_driver_ops wpa_driver_bsd_ops = { | |
901 | .name = "bsd", | |
902 | .hapd_init = bsd_init, | |
903 | .hapd_deinit = bsd_deinit, | |
904 | .set_ieee8021x = bsd_set_ieee8021x, | |
905 | .set_privacy = bsd_set_privacy, | |
642187d6 | 906 | .set_key = bsd_set_key, |
c5121837 JM |
907 | .get_seqnum = bsd_get_seqnum, |
908 | .flush = bsd_flush, | |
909 | .set_generic_elem = bsd_set_opt_ie, | |
910 | .sta_set_flags = bsd_sta_set_flags, | |
911 | .read_sta_data = bsd_read_sta_driver_data, | |
912 | .hapd_send_eapol = bsd_send_eapol, | |
913 | .sta_disassoc = bsd_sta_disassoc, | |
914 | .sta_deauth = bsd_sta_deauth, | |
66d4085f RM |
915 | .hapd_set_ssid = hostapd_bsd_set_ssid, |
916 | .hapd_get_ssid = hostapd_bsd_get_ssid, | |
5197244a | 917 | .set_freq = bsd_set_freq, |
c5121837 JM |
918 | }; |
919 | ||
920 | #else /* HOSTAPD */ | |
921 | ||
6fc6879b JM |
922 | struct wpa_driver_bsd_data { |
923 | int sock; /* open socket for 802.11 ioctls */ | |
924 | int route; /* routing socket for events */ | |
925 | char ifname[IFNAMSIZ+1]; /* interface name */ | |
926 | unsigned int ifindex; /* interface index */ | |
927 | void *ctx; | |
928 | int prev_roaming; /* roaming state to restore on deinit */ | |
929 | int prev_privacy; /* privacy state to restore on deinit */ | |
930 | int prev_wpa; /* wpa state to restore on deinit */ | |
931 | }; | |
932 | ||
933 | static int | |
934 | set80211var(struct wpa_driver_bsd_data *drv, int op, const void *arg, int arg_len) | |
935 | { | |
66d4085f | 936 | return bsd_set80211var(drv->sock, drv->ifname, op, arg, arg_len); |
6fc6879b JM |
937 | } |
938 | ||
939 | static int | |
940 | get80211var(struct wpa_driver_bsd_data *drv, int op, void *arg, int arg_len) | |
941 | { | |
66d4085f | 942 | return bsd_get80211var(drv->sock, drv->ifname, op, arg, arg_len); |
6fc6879b JM |
943 | } |
944 | ||
945 | static int | |
946 | set80211param(struct wpa_driver_bsd_data *drv, int op, int arg) | |
947 | { | |
66d4085f | 948 | return bsd_set80211param(drv->sock, drv->ifname, op, arg); |
6fc6879b JM |
949 | } |
950 | ||
951 | static int | |
952 | get80211param(struct wpa_driver_bsd_data *drv, int op) | |
953 | { | |
954 | struct ieee80211req ireq; | |
955 | ||
956 | os_memset(&ireq, 0, sizeof(ireq)); | |
66d4085f | 957 | os_strlcpy(ireq.i_name, drv->ifname, sizeof(ireq.i_name)); |
6fc6879b JM |
958 | ireq.i_type = op; |
959 | ||
960 | if (ioctl(drv->sock, SIOCG80211, &ireq) < 0) { | |
961 | fprintf(stderr, "ioctl[SIOCG80211, op %u]: %s\n", | |
962 | op, strerror(errno)); | |
963 | return -1; | |
964 | } | |
965 | return ireq.i_val; | |
966 | } | |
967 | ||
968 | static int | |
969 | getifflags(struct wpa_driver_bsd_data *drv, int *flags) | |
970 | { | |
971 | struct ifreq ifr; | |
972 | ||
973 | os_memset(&ifr, 0, sizeof(ifr)); | |
974 | os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); | |
975 | if (ioctl(drv->sock, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) { | |
976 | perror("SIOCGIFFLAGS"); | |
977 | return errno; | |
978 | } | |
979 | *flags = ifr.ifr_flags & 0xffff; | |
980 | return 0; | |
981 | } | |
982 | ||
983 | static int | |
984 | setifflags(struct wpa_driver_bsd_data *drv, int flags) | |
985 | { | |
986 | struct ifreq ifr; | |
987 | ||
988 | os_memset(&ifr, 0, sizeof(ifr)); | |
989 | os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); | |
990 | ifr.ifr_flags = flags & 0xffff; | |
991 | if (ioctl(drv->sock, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) { | |
992 | perror("SIOCSIFFLAGS"); | |
993 | return errno; | |
994 | } | |
995 | return 0; | |
996 | } | |
997 | ||
998 | static int | |
999 | wpa_driver_bsd_get_bssid(void *priv, u8 *bssid) | |
1000 | { | |
1001 | struct wpa_driver_bsd_data *drv = priv; | |
66d4085f | 1002 | #ifdef SIOCG80211BSSID |
a65f4a57 | 1003 | struct ieee80211_bssid bs; |
6fc6879b | 1004 | |
a65f4a57 MH |
1005 | os_strncpy(bs.i_name, drv->ifname, sizeof(bs.i_name)); |
1006 | if (ioctl(drv->sock, SIOCG80211BSSID, &bs) < 0) | |
1007 | return -1; | |
1008 | os_memcpy(bssid, bs.i_bssid, sizeof(bs.i_bssid)); | |
1009 | return 0; | |
1010 | #else | |
6fc6879b JM |
1011 | return get80211var(drv, IEEE80211_IOC_BSSID, |
1012 | bssid, IEEE80211_ADDR_LEN) < 0 ? -1 : 0; | |
a65f4a57 | 1013 | #endif |
6fc6879b JM |
1014 | } |
1015 | ||
1016 | #if 0 | |
1017 | static int | |
1018 | wpa_driver_bsd_set_bssid(void *priv, const char *bssid) | |
1019 | { | |
1020 | struct wpa_driver_bsd_data *drv = priv; | |
1021 | ||
1022 | return set80211var(drv, IEEE80211_IOC_BSSID, | |
1023 | bssid, IEEE80211_ADDR_LEN); | |
1024 | } | |
1025 | #endif | |
1026 | ||
1027 | static int | |
1028 | wpa_driver_bsd_get_ssid(void *priv, u8 *ssid) | |
1029 | { | |
1030 | struct wpa_driver_bsd_data *drv = priv; | |
1031 | ||
66d4085f | 1032 | return bsd_get_ssid(drv->sock, drv->ifname, ssid); |
6fc6879b JM |
1033 | } |
1034 | ||
1035 | static int | |
4df91b3f | 1036 | wpa_driver_bsd_set_ssid(void *priv, const u8 *ssid, |
6fc6879b JM |
1037 | size_t ssid_len) |
1038 | { | |
1039 | struct wpa_driver_bsd_data *drv = priv; | |
1040 | ||
66d4085f | 1041 | return bsd_set_ssid(drv->sock, drv->ifname, ssid, ssid_len); |
6fc6879b JM |
1042 | } |
1043 | ||
1044 | static int | |
1045 | wpa_driver_bsd_set_wpa_ie(struct wpa_driver_bsd_data *drv, | |
4df91b3f | 1046 | const u8 *wpa_ie, size_t wpa_ie_len) |
6fc6879b | 1047 | { |
c610dba1 MH |
1048 | #ifdef IEEE80211_IOC_APPIE |
1049 | return set80211var(drv, IEEE80211_IOC_APPIE, wpa_ie, wpa_ie_len); | |
1050 | #else /* IEEE80211_IOC_APPIE */ | |
6fc6879b | 1051 | return set80211var(drv, IEEE80211_IOC_OPTIE, wpa_ie, wpa_ie_len); |
c610dba1 | 1052 | #endif /* IEEE80211_IOC_APPIE */ |
6fc6879b JM |
1053 | } |
1054 | ||
1055 | static int | |
1056 | wpa_driver_bsd_set_wpa_internal(void *priv, int wpa, int privacy) | |
1057 | { | |
1058 | struct wpa_driver_bsd_data *drv = priv; | |
1059 | int ret = 0; | |
1060 | ||
1061 | wpa_printf(MSG_DEBUG, "%s: wpa=%d privacy=%d", | |
1062 | __FUNCTION__, wpa, privacy); | |
1063 | ||
1064 | if (!wpa && wpa_driver_bsd_set_wpa_ie(drv, NULL, 0) < 0) | |
1065 | ret = -1; | |
1066 | if (set80211param(drv, IEEE80211_IOC_PRIVACY, privacy) < 0) | |
1067 | ret = -1; | |
1068 | if (set80211param(drv, IEEE80211_IOC_WPA, wpa) < 0) | |
1069 | ret = -1; | |
1070 | ||
1071 | return ret; | |
1072 | } | |
1073 | ||
1074 | static int | |
1075 | wpa_driver_bsd_set_wpa(void *priv, int enabled) | |
1076 | { | |
1077 | wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled); | |
1078 | ||
1079 | return wpa_driver_bsd_set_wpa_internal(priv, enabled ? 3 : 0, enabled); | |
1080 | } | |
1081 | ||
6fc6879b | 1082 | static int |
71934751 | 1083 | wpa_driver_bsd_set_key(const char *ifname, void *priv, enum wpa_alg alg, |
6fc6879b JM |
1084 | const unsigned char *addr, int key_idx, int set_tx, |
1085 | const u8 *seq, size_t seq_len, | |
1086 | const u8 *key, size_t key_len) | |
1087 | { | |
1088 | struct wpa_driver_bsd_data *drv = priv; | |
1089 | struct ieee80211req_key wk; | |
1090 | struct ether_addr ea; | |
1091 | char *alg_name; | |
1092 | u_int8_t cipher; | |
1093 | ||
1094 | if (alg == WPA_ALG_NONE) | |
cbdecd2b MH |
1095 | return bsd_del_key(drv->sock, drv->ifname, |
1096 | os_memcmp(addr, "\xff\xff\xff\xff\xff\xff", | |
1097 | IEEE80211_ADDR_LEN) == 0 ? NULL : addr, | |
1098 | key_idx); | |
6fc6879b JM |
1099 | |
1100 | switch (alg) { | |
1101 | case WPA_ALG_WEP: | |
1102 | alg_name = "WEP"; | |
1103 | cipher = IEEE80211_CIPHER_WEP; | |
1104 | break; | |
1105 | case WPA_ALG_TKIP: | |
1106 | alg_name = "TKIP"; | |
1107 | cipher = IEEE80211_CIPHER_TKIP; | |
1108 | break; | |
1109 | case WPA_ALG_CCMP: | |
1110 | alg_name = "CCMP"; | |
1111 | cipher = IEEE80211_CIPHER_AES_CCM; | |
1112 | break; | |
1113 | default: | |
1114 | wpa_printf(MSG_DEBUG, "%s: unknown/unsupported algorithm %d", | |
1115 | __func__, alg); | |
1116 | return -1; | |
1117 | } | |
1118 | ||
1119 | os_memcpy(&ea, addr, IEEE80211_ADDR_LEN); | |
1120 | wpa_printf(MSG_DEBUG, | |
1121 | "%s: alg=%s addr=%s key_idx=%d set_tx=%d seq_len=%zu key_len=%zu", | |
1122 | __func__, alg_name, ether_ntoa(&ea), key_idx, set_tx, | |
1123 | seq_len, key_len); | |
1124 | ||
1125 | if (seq_len > sizeof(u_int64_t)) { | |
1126 | wpa_printf(MSG_DEBUG, "%s: seq_len %zu too big", | |
1127 | __func__, seq_len); | |
1128 | return -2; | |
1129 | } | |
1130 | if (key_len > sizeof(wk.ik_keydata)) { | |
1131 | wpa_printf(MSG_DEBUG, "%s: key length %zu too big", | |
1132 | __func__, key_len); | |
1133 | return -3; | |
1134 | } | |
1135 | ||
1136 | os_memset(&wk, 0, sizeof(wk)); | |
1137 | wk.ik_type = cipher; | |
1138 | wk.ik_flags = IEEE80211_KEY_RECV; | |
1139 | if (set_tx) | |
1140 | wk.ik_flags |= IEEE80211_KEY_XMIT; | |
1141 | os_memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN); | |
1142 | /* | |
1143 | * Deduce whether group/global or unicast key by checking | |
1144 | * the address (yech). Note also that we can only mark global | |
1145 | * keys default; doing this for a unicast key is an error. | |
1146 | */ | |
1147 | if (bcmp(addr, "\xff\xff\xff\xff\xff\xff", IEEE80211_ADDR_LEN) == 0) { | |
1148 | wk.ik_flags |= IEEE80211_KEY_GROUP; | |
1149 | wk.ik_keyix = key_idx; | |
1150 | } else { | |
1151 | wk.ik_keyix = (key_idx == 0 ? IEEE80211_KEYIX_NONE : key_idx); | |
1152 | } | |
1153 | if (wk.ik_keyix != IEEE80211_KEYIX_NONE && set_tx) | |
1154 | wk.ik_flags |= IEEE80211_KEY_DEFAULT; | |
1155 | wk.ik_keylen = key_len; | |
1156 | os_memcpy(&wk.ik_keyrsc, seq, seq_len); | |
1157 | os_memcpy(wk.ik_keydata, key, key_len); | |
1158 | ||
1159 | return set80211var(drv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)); | |
1160 | } | |
1161 | ||
1162 | static int | |
1163 | wpa_driver_bsd_set_countermeasures(void *priv, int enabled) | |
1164 | { | |
1165 | struct wpa_driver_bsd_data *drv = priv; | |
1166 | ||
1167 | wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled); | |
1168 | return set80211param(drv, IEEE80211_IOC_COUNTERMEASURES, enabled); | |
1169 | } | |
1170 | ||
1171 | ||
1172 | static int | |
1173 | wpa_driver_bsd_set_drop_unencrypted(void *priv, int enabled) | |
1174 | { | |
1175 | struct wpa_driver_bsd_data *drv = priv; | |
1176 | ||
1177 | wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled); | |
1178 | return set80211param(drv, IEEE80211_IOC_DROPUNENCRYPTED, enabled); | |
1179 | } | |
1180 | ||
1181 | static int | |
1182 | wpa_driver_bsd_deauthenticate(void *priv, const u8 *addr, int reason_code) | |
1183 | { | |
1184 | struct wpa_driver_bsd_data *drv = priv; | |
1185 | struct ieee80211req_mlme mlme; | |
1186 | ||
1187 | wpa_printf(MSG_DEBUG, "%s", __func__); | |
1188 | os_memset(&mlme, 0, sizeof(mlme)); | |
1189 | mlme.im_op = IEEE80211_MLME_DEAUTH; | |
1190 | mlme.im_reason = reason_code; | |
1191 | os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN); | |
1192 | return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme)); | |
1193 | } | |
1194 | ||
1195 | static int | |
1196 | wpa_driver_bsd_disassociate(void *priv, const u8 *addr, int reason_code) | |
1197 | { | |
1198 | struct wpa_driver_bsd_data *drv = priv; | |
1199 | struct ieee80211req_mlme mlme; | |
1200 | ||
1201 | wpa_printf(MSG_DEBUG, "%s", __func__); | |
1202 | os_memset(&mlme, 0, sizeof(mlme)); | |
1203 | mlme.im_op = IEEE80211_MLME_DISASSOC; | |
1204 | mlme.im_reason = reason_code; | |
1205 | os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN); | |
1206 | return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme)); | |
1207 | } | |
1208 | ||
2a91091e MH |
1209 | static int |
1210 | wpa_driver_bsd_set_auth_alg(void *priv, int auth_alg) | |
1211 | { | |
1212 | struct wpa_driver_bsd_data *drv = priv; | |
1213 | int authmode; | |
1214 | ||
abd9fafa JM |
1215 | if ((auth_alg & WPA_AUTH_ALG_OPEN) && |
1216 | (auth_alg & WPA_AUTH_ALG_SHARED)) | |
2a91091e | 1217 | authmode = IEEE80211_AUTH_AUTO; |
abd9fafa | 1218 | else if (auth_alg & WPA_AUTH_ALG_SHARED) |
2a91091e MH |
1219 | authmode = IEEE80211_AUTH_SHARED; |
1220 | else | |
1221 | authmode = IEEE80211_AUTH_OPEN; | |
1222 | ||
1223 | return set80211param(drv, IEEE80211_IOC_AUTHMODE, authmode); | |
1224 | } | |
1225 | ||
6fc6879b JM |
1226 | static int |
1227 | wpa_driver_bsd_associate(void *priv, struct wpa_driver_associate_params *params) | |
1228 | { | |
1229 | struct wpa_driver_bsd_data *drv = priv; | |
1230 | struct ieee80211req_mlme mlme; | |
1231 | int privacy; | |
4a867032 | 1232 | int ret = 0; |
6fc6879b JM |
1233 | |
1234 | wpa_printf(MSG_DEBUG, | |
1235 | "%s: ssid '%.*s' wpa ie len %u pairwise %u group %u key mgmt %u" | |
1236 | , __func__ | |
92e63aaf JM |
1237 | , (unsigned int) params->ssid_len, params->ssid |
1238 | , (unsigned int) params->wpa_ie_len | |
6fc6879b JM |
1239 | , params->pairwise_suite |
1240 | , params->group_suite | |
1241 | , params->key_mgmt_suite | |
1242 | ); | |
1243 | ||
4a867032 JM |
1244 | if (wpa_driver_bsd_set_drop_unencrypted(drv, params->drop_unencrypted) |
1245 | < 0) | |
1246 | ret = -1; | |
1247 | if (wpa_driver_bsd_set_auth_alg(drv, params->auth_alg) < 0) | |
1248 | ret = -1; | |
6fc6879b JM |
1249 | /* XXX error handling is wrong but unclear what to do... */ |
1250 | if (wpa_driver_bsd_set_wpa_ie(drv, params->wpa_ie, params->wpa_ie_len) < 0) | |
1251 | return -1; | |
6fc6879b JM |
1252 | |
1253 | privacy = !(params->pairwise_suite == CIPHER_NONE && | |
1254 | params->group_suite == CIPHER_NONE && | |
1255 | params->key_mgmt_suite == KEY_MGMT_NONE && | |
1256 | params->wpa_ie_len == 0); | |
1257 | wpa_printf(MSG_DEBUG, "%s: set PRIVACY %u", __func__, privacy); | |
1258 | ||
1259 | if (set80211param(drv, IEEE80211_IOC_PRIVACY, privacy) < 0) | |
1260 | return -1; | |
1261 | ||
1262 | if (params->wpa_ie_len && | |
1263 | set80211param(drv, IEEE80211_IOC_WPA, | |
1264 | params->wpa_ie[0] == WLAN_EID_RSN ? 2 : 1) < 0) | |
1265 | return -1; | |
1266 | ||
1267 | os_memset(&mlme, 0, sizeof(mlme)); | |
1268 | mlme.im_op = IEEE80211_MLME_ASSOC; | |
6fc6879b JM |
1269 | if (params->ssid != NULL) |
1270 | os_memcpy(mlme.im_ssid, params->ssid, params->ssid_len); | |
1271 | mlme.im_ssid_len = params->ssid_len; | |
6fc6879b JM |
1272 | if (params->bssid != NULL) |
1273 | os_memcpy(mlme.im_macaddr, params->bssid, IEEE80211_ADDR_LEN); | |
1274 | if (set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme)) < 0) | |
1275 | return -1; | |
4a867032 | 1276 | return ret; |
6fc6879b JM |
1277 | } |
1278 | ||
6fc6879b | 1279 | static int |
c2e8d0a0 | 1280 | wpa_driver_bsd_scan(void *priv, struct wpa_driver_scan_params *params) |
6fc6879b JM |
1281 | { |
1282 | struct wpa_driver_bsd_data *drv = priv; | |
1283 | int flags; | |
c2e8d0a0 JM |
1284 | const u8 *ssid = params->ssids[0].ssid; |
1285 | size_t ssid_len = params->ssids[0].ssid_len; | |
6fc6879b JM |
1286 | |
1287 | /* NB: interface must be marked UP to do a scan */ | |
1288 | if (getifflags(drv, &flags) != 0 || setifflags(drv, flags | IFF_UP) != 0) | |
1289 | return -1; | |
1290 | ||
1291 | /* set desired ssid before scan */ | |
1292 | if (wpa_driver_bsd_set_ssid(drv, ssid, ssid_len) < 0) | |
1293 | return -1; | |
1294 | ||
1295 | /* NB: net80211 delivers a scan complete event so no need to poll */ | |
1296 | return set80211param(drv, IEEE80211_IOC_SCAN_REQ, 0); | |
1297 | } | |
1298 | ||
6fc6879b JM |
1299 | static void |
1300 | wpa_driver_bsd_event_receive(int sock, void *ctx, void *sock_ctx) | |
1301 | { | |
1302 | struct wpa_driver_bsd_data *drv = sock_ctx; | |
1303 | char buf[2048]; | |
1304 | struct if_announcemsghdr *ifan; | |
1305 | struct if_msghdr *ifm; | |
1306 | struct rt_msghdr *rtm; | |
1307 | union wpa_event_data event; | |
1308 | struct ieee80211_michael_event *mic; | |
1309 | int n; | |
1310 | ||
1311 | n = read(sock, buf, sizeof(buf)); | |
1312 | if (n < 0) { | |
1313 | if (errno != EINTR && errno != EAGAIN) | |
1314 | perror("read(PF_ROUTE)"); | |
1315 | return; | |
1316 | } | |
1317 | ||
1318 | rtm = (struct rt_msghdr *) buf; | |
1319 | if (rtm->rtm_version != RTM_VERSION) { | |
1320 | wpa_printf(MSG_DEBUG, "Routing message version %d not " | |
1321 | "understood\n", rtm->rtm_version); | |
1322 | return; | |
1323 | } | |
1324 | os_memset(&event, 0, sizeof(event)); | |
1325 | switch (rtm->rtm_type) { | |
1326 | case RTM_IFANNOUNCE: | |
1327 | ifan = (struct if_announcemsghdr *) rtm; | |
1328 | if (ifan->ifan_index != drv->ifindex) | |
1329 | break; | |
1330 | strlcpy(event.interface_status.ifname, drv->ifname, | |
1331 | sizeof(event.interface_status.ifname)); | |
1332 | switch (ifan->ifan_what) { | |
1333 | case IFAN_DEPARTURE: | |
1334 | event.interface_status.ievent = EVENT_INTERFACE_REMOVED; | |
1335 | default: | |
1336 | return; | |
1337 | } | |
1338 | wpa_printf(MSG_DEBUG, "RTM_IFANNOUNCE: Interface '%s' %s", | |
1339 | event.interface_status.ifname, | |
1340 | ifan->ifan_what == IFAN_DEPARTURE ? | |
1341 | "removed" : "added"); | |
1342 | wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &event); | |
1343 | break; | |
1344 | case RTM_IEEE80211: | |
1345 | ifan = (struct if_announcemsghdr *) rtm; | |
1346 | if (ifan->ifan_index != drv->ifindex) | |
1347 | break; | |
1348 | switch (ifan->ifan_what) { | |
1349 | case RTM_IEEE80211_ASSOC: | |
1350 | case RTM_IEEE80211_REASSOC: | |
1351 | wpa_supplicant_event(ctx, EVENT_ASSOC, NULL); | |
1352 | break; | |
1353 | case RTM_IEEE80211_DISASSOC: | |
1354 | wpa_supplicant_event(ctx, EVENT_DISASSOC, NULL); | |
1355 | break; | |
1356 | case RTM_IEEE80211_SCAN: | |
1357 | wpa_supplicant_event(ctx, EVENT_SCAN_RESULTS, NULL); | |
1358 | break; | |
1359 | case RTM_IEEE80211_REPLAY: | |
1360 | /* ignore */ | |
1361 | break; | |
1362 | case RTM_IEEE80211_MICHAEL: | |
1363 | mic = (struct ieee80211_michael_event *) &ifan[1]; | |
1364 | wpa_printf(MSG_DEBUG, | |
1365 | "Michael MIC failure wireless event: " | |
1366 | "keyix=%u src_addr=" MACSTR, mic->iev_keyix, | |
1367 | MAC2STR(mic->iev_src)); | |
1368 | ||
1369 | os_memset(&event, 0, sizeof(event)); | |
1370 | event.michael_mic_failure.unicast = | |
1371 | !IEEE80211_IS_MULTICAST(mic->iev_dst); | |
1372 | wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE, | |
1373 | &event); | |
1374 | break; | |
1375 | } | |
1376 | break; | |
1377 | case RTM_IFINFO: | |
1378 | ifm = (struct if_msghdr *) rtm; | |
1379 | if (ifm->ifm_index != drv->ifindex) | |
1380 | break; | |
1381 | if ((rtm->rtm_flags & RTF_UP) == 0) { | |
1382 | strlcpy(event.interface_status.ifname, drv->ifname, | |
1383 | sizeof(event.interface_status.ifname)); | |
1384 | event.interface_status.ievent = EVENT_INTERFACE_REMOVED; | |
1385 | wpa_printf(MSG_DEBUG, "RTM_IFINFO: Interface '%s' DOWN", | |
1386 | event.interface_status.ifname); | |
1387 | wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &event); | |
1388 | } | |
1389 | break; | |
1390 | } | |
1391 | } | |
1392 | ||
a65f4a57 MH |
1393 | static void |
1394 | wpa_driver_bsd_add_scan_entry(struct wpa_scan_results *res, | |
1395 | struct ieee80211req_scan_result *sr) | |
6fc6879b | 1396 | { |
a65f4a57 MH |
1397 | struct wpa_scan_res *result, **tmp; |
1398 | size_t extra_len; | |
1399 | u8 *pos; | |
6fc6879b | 1400 | |
a65f4a57 MH |
1401 | extra_len = 2 + sr->isr_ssid_len; |
1402 | extra_len += 2 + sr->isr_nrates; | |
1403 | extra_len += 3; /* ERP IE */ | |
1404 | extra_len += sr->isr_ie_len; | |
6fc6879b | 1405 | |
a65f4a57 MH |
1406 | result = os_zalloc(sizeof(*result) + extra_len); |
1407 | if (result == NULL) | |
1408 | return; | |
1409 | os_memcpy(result->bssid, sr->isr_bssid, ETH_ALEN); | |
1410 | result->freq = sr->isr_freq; | |
1411 | result->beacon_int = sr->isr_intval; | |
1412 | result->caps = sr->isr_capinfo; | |
1413 | result->qual = sr->isr_rssi; | |
1414 | result->noise = sr->isr_noise; | |
6fc6879b | 1415 | |
a65f4a57 | 1416 | pos = (u8 *)(result + 1); |
6fc6879b | 1417 | |
a65f4a57 MH |
1418 | *pos++ = WLAN_EID_SSID; |
1419 | *pos++ = sr->isr_ssid_len; | |
1420 | os_memcpy(pos, sr + 1, sr->isr_ssid_len); | |
1421 | pos += sr->isr_ssid_len; | |
6fc6879b | 1422 | |
a65f4a57 MH |
1423 | /* |
1424 | * Deal all rates as supported rate. | |
1425 | * Because net80211 doesn't report extended supported rate or not. | |
1426 | */ | |
1427 | *pos++ = WLAN_EID_SUPP_RATES; | |
1428 | *pos++ = sr->isr_nrates; | |
1429 | os_memcpy(pos, sr->isr_rates, sr->isr_nrates); | |
1430 | pos += sr->isr_nrates; | |
6fc6879b | 1431 | |
a65f4a57 MH |
1432 | *pos++ = WLAN_EID_ERP_INFO; |
1433 | *pos++ = 1; | |
1434 | *pos++ = sr->isr_erp; | |
6fc6879b | 1435 | |
a65f4a57 MH |
1436 | os_memcpy(pos, (u8 *)(sr + 1) + sr->isr_ssid_len, sr->isr_ie_len); |
1437 | pos += sr->isr_ie_len; | |
6fc6879b | 1438 | |
a65f4a57 MH |
1439 | result->ie_len = pos - (u8 *)(result + 1); |
1440 | ||
1441 | tmp = os_realloc(res->res, | |
1442 | (res->num + 1) * sizeof(struct wpa_scan_res *)); | |
1443 | if (tmp == NULL) { | |
1444 | os_free(result); | |
1445 | return; | |
1446 | } | |
1447 | tmp[res->num++] = result; | |
1448 | res->res = tmp; | |
6fc6879b JM |
1449 | } |
1450 | ||
a65f4a57 MH |
1451 | struct wpa_scan_results * |
1452 | wpa_driver_bsd_get_scan_results2(void *priv) | |
6fc6879b | 1453 | { |
6fc6879b | 1454 | struct wpa_driver_bsd_data *drv = priv; |
6fc6879b | 1455 | struct ieee80211req_scan_result *sr; |
a65f4a57 MH |
1456 | struct wpa_scan_results *res; |
1457 | int len, rest; | |
1458 | uint8_t buf[24*1024], *pos; | |
6fc6879b | 1459 | |
a65f4a57 | 1460 | len = get80211var(drv, IEEE80211_IOC_SCAN_RESULTS, buf, 24*1024); |
6fc6879b | 1461 | if (len < 0) |
a65f4a57 MH |
1462 | return NULL; |
1463 | ||
1464 | res = os_zalloc(sizeof(*res)); | |
1465 | if (res == NULL) | |
1466 | return NULL; | |
6fc6879b | 1467 | |
a65f4a57 MH |
1468 | pos = buf; |
1469 | rest = len; | |
1470 | while (rest >= sizeof(struct ieee80211req_scan_result)) { | |
1471 | sr = (struct ieee80211req_scan_result *)pos; | |
1472 | wpa_driver_bsd_add_scan_entry(res, sr); | |
1473 | pos += sr->isr_len; | |
1474 | rest -= sr->isr_len; | |
6fc6879b | 1475 | } |
6fc6879b | 1476 | |
a65f4a57 MH |
1477 | wpa_printf(MSG_DEBUG, "Received %d bytes of scan results (%lu BSSes)", |
1478 | len, (unsigned long)res->num); | |
6fc6879b | 1479 | |
a65f4a57 | 1480 | return res; |
6fc6879b JM |
1481 | } |
1482 | ||
1483 | static void * | |
1484 | wpa_driver_bsd_init(void *ctx, const char *ifname) | |
1485 | { | |
1486 | #define GETPARAM(drv, param, v) \ | |
1487 | (((v) = get80211param(drv, param)) != -1) | |
1488 | struct wpa_driver_bsd_data *drv; | |
1489 | ||
1490 | drv = os_zalloc(sizeof(*drv)); | |
1491 | if (drv == NULL) | |
1492 | return NULL; | |
1493 | /* | |
1494 | * NB: We require the interface name be mappable to an index. | |
1495 | * This implies we do not support having wpa_supplicant | |
1496 | * wait for an interface to appear. This seems ok; that | |
1497 | * doesn't belong here; it's really the job of devd. | |
1498 | */ | |
1499 | drv->ifindex = if_nametoindex(ifname); | |
1500 | if (drv->ifindex == 0) { | |
1501 | wpa_printf(MSG_DEBUG, "%s: interface %s does not exist", | |
1502 | __func__, ifname); | |
1503 | goto fail1; | |
1504 | } | |
1505 | drv->sock = socket(PF_INET, SOCK_DGRAM, 0); | |
1506 | if (drv->sock < 0) | |
1507 | goto fail1; | |
1508 | drv->route = socket(PF_ROUTE, SOCK_RAW, 0); | |
1509 | if (drv->route < 0) | |
1510 | goto fail; | |
1511 | eloop_register_read_sock(drv->route, | |
1512 | wpa_driver_bsd_event_receive, ctx, drv); | |
1513 | ||
1514 | drv->ctx = ctx; | |
1515 | os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname)); | |
1516 | ||
1517 | if (!GETPARAM(drv, IEEE80211_IOC_ROAMING, drv->prev_roaming)) { | |
1518 | wpa_printf(MSG_DEBUG, "%s: failed to get roaming state: %s", | |
1519 | __func__, strerror(errno)); | |
1520 | goto fail; | |
1521 | } | |
1522 | if (!GETPARAM(drv, IEEE80211_IOC_PRIVACY, drv->prev_privacy)) { | |
1523 | wpa_printf(MSG_DEBUG, "%s: failed to get privacy state: %s", | |
1524 | __func__, strerror(errno)); | |
1525 | goto fail; | |
1526 | } | |
1527 | if (!GETPARAM(drv, IEEE80211_IOC_WPA, drv->prev_wpa)) { | |
1528 | wpa_printf(MSG_DEBUG, "%s: failed to get wpa state: %s", | |
1529 | __func__, strerror(errno)); | |
1530 | goto fail; | |
1531 | } | |
5197244a MH |
1532 | |
1533 | if (bsd_set_mediaopt(drv->sock, drv->ifname, IFM_OMASK, | |
1534 | 0 /* STA */) < 0) { | |
1535 | wpa_printf(MSG_ERROR, "%s: failed to set operation mode", | |
1536 | __func__); | |
1537 | goto fail; | |
1538 | } | |
6fc6879b JM |
1539 | if (set80211param(drv, IEEE80211_IOC_ROAMING, IEEE80211_ROAMING_MANUAL) < 0) { |
1540 | wpa_printf(MSG_DEBUG, "%s: failed to set wpa_supplicant-based " | |
1541 | "roaming: %s", __func__, strerror(errno)); | |
1542 | goto fail; | |
1543 | } | |
1544 | ||
1545 | if (set80211param(drv, IEEE80211_IOC_WPA, 1+2) < 0) { | |
1546 | wpa_printf(MSG_DEBUG, "%s: failed to enable WPA support %s", | |
1547 | __func__, strerror(errno)); | |
1548 | goto fail; | |
1549 | } | |
1550 | ||
4a867032 JM |
1551 | wpa_driver_bsd_set_wpa(drv, 1); |
1552 | ||
6fc6879b JM |
1553 | return drv; |
1554 | fail: | |
1555 | close(drv->sock); | |
1556 | fail1: | |
1557 | os_free(drv); | |
1558 | return NULL; | |
1559 | #undef GETPARAM | |
1560 | } | |
1561 | ||
1562 | static void | |
1563 | wpa_driver_bsd_deinit(void *priv) | |
1564 | { | |
1565 | struct wpa_driver_bsd_data *drv = priv; | |
1566 | int flags; | |
1567 | ||
4a867032 | 1568 | wpa_driver_bsd_set_wpa(drv, 0); |
6fc6879b JM |
1569 | eloop_unregister_read_sock(drv->route); |
1570 | ||
1571 | /* NB: mark interface down */ | |
1572 | if (getifflags(drv, &flags) == 0) | |
1573 | (void) setifflags(drv, flags &~ IFF_UP); | |
1574 | ||
1575 | wpa_driver_bsd_set_wpa_internal(drv, drv->prev_wpa, drv->prev_privacy); | |
1576 | if (set80211param(drv, IEEE80211_IOC_ROAMING, drv->prev_roaming) < 0) | |
1577 | wpa_printf(MSG_DEBUG, "%s: failed to restore roaming state", | |
1578 | __func__); | |
1579 | ||
1580 | (void) close(drv->route); /* ioctl socket */ | |
1581 | (void) close(drv->sock); /* event socket */ | |
1582 | os_free(drv); | |
1583 | } | |
1584 | ||
1585 | ||
1586 | const struct wpa_driver_ops wpa_driver_bsd_ops = { | |
1587 | .name = "bsd", | |
66d4085f | 1588 | .desc = "BSD 802.11 support", |
6fc6879b JM |
1589 | .init = wpa_driver_bsd_init, |
1590 | .deinit = wpa_driver_bsd_deinit, | |
1591 | .get_bssid = wpa_driver_bsd_get_bssid, | |
1592 | .get_ssid = wpa_driver_bsd_get_ssid, | |
6fc6879b JM |
1593 | .set_key = wpa_driver_bsd_set_key, |
1594 | .set_countermeasures = wpa_driver_bsd_set_countermeasures, | |
c2e8d0a0 | 1595 | .scan2 = wpa_driver_bsd_scan, |
a65f4a57 | 1596 | .get_scan_results2 = wpa_driver_bsd_get_scan_results2, |
6fc6879b JM |
1597 | .deauthenticate = wpa_driver_bsd_deauthenticate, |
1598 | .disassociate = wpa_driver_bsd_disassociate, | |
1599 | .associate = wpa_driver_bsd_associate, | |
6fc6879b | 1600 | }; |
c5121837 JM |
1601 | |
1602 | #endif /* HOSTAPD */ |