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