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