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