2 * Host AP - driver interaction with BSD net80211 layer
3 * Copyright (c) 2004, Sam Leffler <sam@errno.com>
4 * Copyright (c) 2004, 2Wire, Inc
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
21 #include <sys/ioctl.h>
24 #include <sys/socket.h>
26 #include <netinet/in.h>
28 #include <net80211/ieee80211.h>
29 #include <net80211/ieee80211_crypto.h>
30 #include <net80211/ieee80211_ioctl.h>
34 #include "ieee802_1x.h"
37 #include "l2_packet.h"
42 #include "ieee802_11.h"
44 #include "hostap_common.h"
46 struct bsd_driver_data {
47 struct driver_ops ops; /* base class */
48 struct hostapd_data *hapd; /* back pointer */
50 char iface[IFNAMSIZ + 1];
51 struct l2_packet_data *sock_xmit; /* raw packet xmit socket */
52 int ioctl_sock; /* socket for ioctl() use */
53 int wext_sock; /* socket for wireless events */
56 static const struct driver_ops bsd_driver_ops;
58 static int bsd_sta_deauth(void *priv, const u8 *addr, int reason_code);
61 set80211var(struct bsd_driver_data *drv, int op, const void *arg, int arg_len)
63 struct ieee80211req ireq;
65 memset(&ireq, 0, sizeof(ireq));
66 strncpy(ireq.i_name, drv->iface, IFNAMSIZ);
69 ireq.i_data = (void *) arg;
71 if (ioctl(drv->ioctl_sock, SIOCS80211, &ireq) < 0) {
72 perror("ioctl[SIOCS80211]");
79 get80211var(struct bsd_driver_data *drv, int op, void *arg, int arg_len)
81 struct ieee80211req ireq;
83 memset(&ireq, 0, sizeof(ireq));
84 strncpy(ireq.i_name, drv->iface, IFNAMSIZ);
89 if (ioctl(drv->ioctl_sock, SIOCG80211, &ireq) < 0) {
90 perror("ioctl[SIOCG80211]");
97 set80211param(struct bsd_driver_data *drv, int op, int arg)
99 struct ieee80211req ireq;
101 memset(&ireq, 0, sizeof(ireq));
102 strncpy(ireq.i_name, drv->iface, IFNAMSIZ);
106 if (ioctl(drv->ioctl_sock, SIOCS80211, &ireq) < 0) {
107 perror("ioctl[SIOCS80211]");
114 ether_sprintf(const u8 *addr)
116 static char buf[sizeof(MACSTR)];
119 snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
121 snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0);
126 * Configure WPA parameters.
129 bsd_configure_wpa(struct bsd_driver_data *drv)
131 static const char *ciphernames[] =
132 { "WEP", "TKIP", "AES-OCB", "AES-CCM", "*BAD*", "CKIP", "NONE" };
133 struct hostapd_data *hapd = drv->hapd;
134 struct hostapd_bss_config *conf = hapd->conf;
137 switch (conf->wpa_group) {
138 case WPA_CIPHER_CCMP:
139 v = IEEE80211_CIPHER_AES_CCM;
141 case WPA_CIPHER_TKIP:
142 v = IEEE80211_CIPHER_TKIP;
144 case WPA_CIPHER_WEP104:
145 v = IEEE80211_CIPHER_WEP;
147 case WPA_CIPHER_WEP40:
148 v = IEEE80211_CIPHER_WEP;
150 case WPA_CIPHER_NONE:
151 v = IEEE80211_CIPHER_NONE;
154 printf("Unknown group key cipher %u\n",
158 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
159 "%s: group key cipher=%s (%u)\n", __func__, ciphernames[v], v);
160 if (set80211param(drv, IEEE80211_IOC_MCASTCIPHER, v)) {
161 printf("Unable to set group key cipher to %u (%s)\n",
165 if (v == IEEE80211_CIPHER_WEP) {
166 /* key length is done only for specific ciphers */
167 v = (conf->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5);
168 if (set80211param(drv, IEEE80211_IOC_MCASTKEYLEN, v)) {
169 printf("Unable to set group key length to %u\n", v);
175 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
176 v |= 1<<IEEE80211_CIPHER_AES_CCM;
177 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
178 v |= 1<<IEEE80211_CIPHER_TKIP;
179 if (conf->wpa_pairwise & WPA_CIPHER_NONE)
180 v |= 1<<IEEE80211_CIPHER_NONE;
181 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
182 "%s: pairwise key ciphers=0x%x\n", __func__, v);
183 if (set80211param(drv, IEEE80211_IOC_UCASTCIPHERS, v)) {
184 printf("Unable to set pairwise key ciphers to 0x%x\n", v);
188 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
189 "%s: key management algorithms=0x%x\n",
190 __func__, conf->wpa_key_mgmt);
191 if (set80211param(drv, IEEE80211_IOC_KEYMGTALGS, conf->wpa_key_mgmt)) {
192 printf("Unable to set key management algorithms to 0x%x\n",
198 if (conf->rsn_preauth)
200 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
201 "%s: rsn capabilities=0x%x\n", __func__, conf->rsn_preauth);
202 if (set80211param(drv, IEEE80211_IOC_RSNCAPS, v)) {
203 printf("Unable to set RSN capabilities to 0x%x\n", v);
207 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
208 "%s: enable WPA= 0x%x\n", __func__, conf->wpa);
209 if (set80211param(drv, IEEE80211_IOC_WPA, conf->wpa)) {
210 printf("Unable to set WPA to %u\n", conf->wpa);
218 bsd_set_iface_flags(void *priv, int dev_up)
220 struct bsd_driver_data *drv = priv;
221 struct hostapd_data *hapd = drv->hapd;
224 HOSTAPD_DEBUG(HOSTAPD_DEBUG_VERBOSE,
225 "%s: dev_up=%d\n", __func__, dev_up);
227 if (drv->ioctl_sock < 0)
230 memset(&ifr, 0, sizeof(ifr));
231 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", drv->iface);
233 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
234 perror("ioctl[SIOCGIFFLAGS]");
239 ifr.ifr_flags |= IFF_UP;
241 ifr.ifr_flags &= ~IFF_UP;
243 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
244 perror("ioctl[SIOCSIFFLAGS]");
249 memset(&ifr, 0, sizeof(ifr));
250 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", drv->iface);
251 ifr.ifr_mtu = HOSTAPD_MTU;
252 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
253 perror("ioctl[SIOCSIFMTU]");
254 printf("Setting MTU failed - trying to survive with "
263 bsd_set_ieee8021x(const char *ifname, void *priv, int enabled)
265 struct bsd_driver_data *drv = priv;
266 struct hostapd_data *hapd = drv->hapd;
267 struct hostapd_bss_config *conf = hapd->conf;
269 HOSTAPD_DEBUG(HOSTAPD_DEBUG_VERBOSE,
270 "%s: enabled=%d\n", __func__, enabled);
273 /* XXX restore state */
274 return set80211param(priv, IEEE80211_IOC_AUTHMODE,
275 IEEE80211_AUTH_AUTO);
277 if (!conf->wpa && !conf->ieee802_1x) {
278 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
279 HOSTAPD_LEVEL_WARNING, "No 802.1X or WPA enabled!");
282 if (conf->wpa && bsd_configure_wpa(drv) != 0) {
283 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
284 HOSTAPD_LEVEL_WARNING, "Error configuring WPA state!");
287 if (set80211param(priv, IEEE80211_IOC_AUTHMODE,
288 (conf->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) {
289 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
290 HOSTAPD_LEVEL_WARNING, "Error enabling WPA/802.1X!");
293 return bsd_set_iface_flags(priv, 1);
297 bsd_set_privacy(const char *ifname, void *priv, int enabled)
299 struct bsd_driver_data *drv = priv;
300 struct hostapd_data *hapd = drv->hapd;
302 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
303 "%s: enabled=%d\n", __func__, enabled);
305 return set80211param(priv, IEEE80211_IOC_PRIVACY, enabled);
309 bsd_set_sta_authorized(void *priv, const u8 *addr, int authorized)
311 struct bsd_driver_data *drv = priv;
312 struct hostapd_data *hapd = drv->hapd;
313 struct ieee80211req_mlme mlme;
315 HOSTAPD_DEBUG(HOSTAPD_DEBUG_VERBOSE,
316 "%s: addr=%s authorized=%d\n",
317 __func__, ether_sprintf(addr), authorized);
320 mlme.im_op = IEEE80211_MLME_AUTHORIZE;
322 mlme.im_op = IEEE80211_MLME_UNAUTHORIZE;
324 memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
325 return set80211var(priv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
329 bsd_sta_set_flags(void *priv, const u8 *addr, int flags_or, int flags_and)
331 /* For now, only support setting Authorized flag */
332 if (flags_or & WLAN_STA_AUTHORIZED)
333 return bsd_set_sta_authorized(priv, addr, 1);
334 if (!(flags_and & WLAN_STA_AUTHORIZED))
335 return bsd_set_sta_authorized(priv, addr, 0);
340 bsd_del_key(void *priv, const unsigned char *addr, int key_idx)
342 struct bsd_driver_data *drv = priv;
343 struct hostapd_data *hapd = drv->hapd;
344 struct ieee80211req_del_key wk;
346 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
347 "%s: addr=%s key_idx=%d\n",
348 __func__, ether_sprintf(addr), key_idx);
350 memset(&wk, 0, sizeof(wk));
352 memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
353 wk.idk_keyix = (u_int8_t) IEEE80211_KEYIX_NONE; /* XXX */
355 wk.idk_keyix = key_idx;
358 return set80211var(priv, IEEE80211_IOC_DELKEY, &wk, sizeof(wk));
362 bsd_set_key(const char *ifname, void *priv, const char *alg,
363 const u8 *addr, int key_idx,
364 const u8 *key, size_t key_len, int txkey)
366 struct bsd_driver_data *drv = priv;
367 struct hostapd_data *hapd = drv->hapd;
368 struct ieee80211req_key wk;
371 if (strcmp(alg, "none") == 0)
372 return bsd_del_key(priv, addr, key_idx);
374 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
375 "%s: alg=%s addr=%s key_idx=%d\n",
376 __func__, alg, ether_sprintf(addr), key_idx);
378 if (strcmp(alg, "WEP") == 0)
379 cipher = IEEE80211_CIPHER_WEP;
380 else if (strcmp(alg, "TKIP") == 0)
381 cipher = IEEE80211_CIPHER_TKIP;
382 else if (strcmp(alg, "CCMP") == 0)
383 cipher = IEEE80211_CIPHER_AES_CCM;
385 printf("%s: unknown/unsupported algorithm %s\n",
390 if (key_len > sizeof(wk.ik_keydata)) {
391 printf("%s: key length %d too big\n", __func__, key_len);
395 memset(&wk, 0, sizeof(wk));
397 wk.ik_flags = IEEE80211_KEY_RECV | IEEE80211_KEY_XMIT;
399 memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
400 wk.ik_keyix = key_idx;
401 wk.ik_flags |= IEEE80211_KEY_DEFAULT | IEEE80211_KEY_GROUP;
403 memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
404 wk.ik_keyix = IEEE80211_KEYIX_NONE;
406 wk.ik_keylen = key_len;
407 memcpy(wk.ik_keydata, key, key_len);
409 return set80211var(priv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk));
414 bsd_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx,
417 struct bsd_driver_data *drv = priv;
418 struct hostapd_data *hapd = drv->hapd;
419 struct ieee80211req_key wk;
421 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
422 "%s: addr=%s idx=%d\n", __func__, ether_sprintf(addr), idx);
424 memset(&wk, 0, sizeof(wk));
426 memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
428 memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
431 if (get80211var(drv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)) < 0) {
432 printf("Failed to get encryption.\n");
435 /* NB: upper layer expects tsc in network order */
436 wk.ik_keytsc = htole64(wk.ik_keytsc);
437 memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
444 bsd_flush(void *priv)
446 u8 allsta[IEEE80211_ADDR_LEN];
448 memset(allsta, 0xff, IEEE80211_ADDR_LEN);
449 return bsd_sta_deauth(priv, allsta, IEEE80211_REASON_AUTH_LEAVE);
454 bsd_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data,
457 struct bsd_driver_data *drv = priv;
458 struct ieee80211req_sta_stats stats;
460 memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN);
461 if (get80211var(drv, IEEE80211_IOC_STA_STATS, &stats, sizeof(stats)) > 0) {
462 /* XXX? do packets counts include non-data frames? */
463 data->rx_packets = stats.is_stats.ns_rx_data;
464 data->rx_bytes = stats.is_stats.ns_rx_bytes;
465 data->tx_packets = stats.is_stats.ns_tx_data;
466 data->tx_bytes = stats.is_stats.ns_tx_bytes;
472 bsd_sta_clear_stats(void *priv, const u8 *addr)
474 struct bsd_driver_data *drv = priv;
475 struct hostapd_data *hapd = drv->hapd;
476 struct ieee80211req_sta_stats stats;
478 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "%s: addr=%s\n",
479 __func__, ether_sprintf(addr));
481 /* zero station statistics */
482 memset(&stats, 0, sizeof(stats));
483 memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN);
484 return set80211var(drv, IEEE80211_IOC_STA_STATS, &stats, sizeof(stats));
488 bsd_set_opt_ie(const char *ifname, void *priv, const u8 *ie, size_t ie_len)
491 * Do nothing; we setup parameters at startup that define the
492 * contents of the beacon information element.
498 bsd_sta_deauth(void *priv, const u8 *addr, int reason_code)
500 struct bsd_driver_data *drv = priv;
501 struct hostapd_data *hapd = drv->hapd;
502 struct ieee80211req_mlme mlme;
504 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
505 "%s: addr=%s reason_code=%d\n",
506 __func__, ether_sprintf(addr), reason_code);
508 mlme.im_op = IEEE80211_MLME_DEAUTH;
509 mlme.im_reason = reason_code;
510 memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
511 return set80211var(priv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
515 bsd_sta_disassoc(void *priv, const u8 *addr, int reason_code)
517 struct bsd_driver_data *drv = priv;
518 struct hostapd_data *hapd = drv->hapd;
519 struct ieee80211req_mlme mlme;
521 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
522 "%s: addr=%s reason_code=%d\n",
523 __func__, ether_sprintf(addr), reason_code);
525 mlme.im_reason = reason_code;
526 memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
527 return set80211var(priv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
531 bsd_del_sta(struct bsd_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
533 struct hostapd_data *hapd = drv->hapd;
534 struct hostapd_bss_config *conf = hapd->conf;
535 struct sta_info *sta;
537 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
538 HOSTAPD_LEVEL_INFO, "deassociated");
540 sta = ap_get_sta(hapd, addr);
542 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
544 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
545 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
546 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
547 ap_free_sta(hapd, sta);
553 bsd_new_sta(struct bsd_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
555 struct hostapd_data *hapd = drv->hapd;
556 struct hostapd_bss_config *conf = hapd->conf;
557 struct sta_info *sta;
558 struct ieee80211req_wpaie ie;
559 int new_assoc, ielen, res;
561 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
562 HOSTAPD_LEVEL_INFO, "associated");
564 sta = ap_sta_add(hapd, addr);
568 * Fetch and validate any negotiated WPA/RSN parameters.
571 memset(&ie, 0, sizeof(ie));
572 memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
573 if (get80211var(drv, IEEE80211_IOC_WPAIE, &ie, sizeof(ie)) < 0) {
574 printf("Failed to get WPA/RSN information element.\n");
575 return -1; /* XXX not right */
577 if (ie.wpa_ie[1] == 0) {
578 printf("No WPA/RSN information element for station!\n");
579 return -1; /* XXX not right */
581 if (sta->wpa_sm == NULL)
582 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
584 if (sta->wpa_sm == NULL) {
585 printf("Failed to initialize WPA state machine\n");
588 ielen = 2 + ie.wpa_ie[1];
589 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
591 if (res != WPA_IE_OK) {
592 printf("WPA/RSN information element rejected? "
599 * Now that the internal station state is setup
600 * kick the authenticator into action.
602 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
603 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
604 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
605 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
606 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
611 #include <net/route.h>
612 #include <net80211/ieee80211_freebsd.h>
615 bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx)
617 struct bsd_driver_data *drv = ctx;
618 struct hostapd_data *hapd = drv->hapd;
620 struct if_announcemsghdr *ifan;
621 struct rt_msghdr *rtm;
622 struct ieee80211_michael_event *mic;
623 struct ieee80211_join_event *join;
624 struct ieee80211_leave_event *leave;
627 n = read(sock, buf, sizeof(buf));
629 if (errno != EINTR && errno != EAGAIN)
630 perror("read(PF_ROUTE)");
634 rtm = (struct rt_msghdr *) buf;
635 if (rtm->rtm_version != RTM_VERSION) {
636 wpa_printf(MSG_DEBUG, "Routing message version %d not "
637 "understood\n", rtm->rtm_version);
640 ifan = (struct if_announcemsghdr *) rtm;
641 switch (rtm->rtm_type) {
643 switch (ifan->ifan_what) {
644 case RTM_IEEE80211_ASSOC:
645 case RTM_IEEE80211_REASSOC:
646 case RTM_IEEE80211_DISASSOC:
647 case RTM_IEEE80211_SCAN:
649 case RTM_IEEE80211_LEAVE:
650 leave = (struct ieee80211_leave_event *) &ifan[1];
651 bsd_del_sta(drv, leave->iev_addr);
653 case RTM_IEEE80211_JOIN:
654 #ifdef RTM_IEEE80211_REJOIN
655 case RTM_IEEE80211_REJOIN:
657 join = (struct ieee80211_join_event *) &ifan[1];
658 bsd_new_sta(drv, join->iev_addr);
660 case RTM_IEEE80211_REPLAY:
663 case RTM_IEEE80211_MICHAEL:
664 mic = (struct ieee80211_michael_event *) &ifan[1];
665 wpa_printf(MSG_DEBUG,
666 "Michael MIC failure wireless event: "
667 "keyix=%u src_addr=" MACSTR, mic->iev_keyix,
668 MAC2STR(mic->iev_src));
669 ieee80211_michael_mic_failure(hapd, mic->iev_src, 1);
677 bsd_wireless_event_init(void *priv)
679 struct bsd_driver_data *drv = priv;
684 s = socket(PF_ROUTE, SOCK_RAW, 0);
686 perror("socket(PF_ROUTE,SOCK_RAW)");
689 eloop_register_read_sock(s, bsd_wireless_event_receive, drv, NULL);
696 bsd_wireless_event_deinit(void *priv)
698 struct bsd_driver_data *drv = priv;
701 if (drv->wext_sock < 0)
703 eloop_unregister_read_sock(drv->wext_sock);
704 close(drv->wext_sock);
710 bsd_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
711 int encrypt, const u8 *own_addr)
713 struct bsd_driver_data *drv = priv;
714 struct hostapd_data *hapd = drv->hapd;
715 unsigned char buf[3000];
716 unsigned char *bp = buf;
717 struct l2_ethhdr *eth;
722 * Prepend the Etherent header. If the caller left us
723 * space at the front we could just insert it but since
724 * we don't know we copy to a local buffer. Given the frequency
725 * and size of frames this probably doesn't matter.
727 len = data_len + sizeof(struct l2_ethhdr);
728 if (len > sizeof(buf)) {
731 printf("EAPOL frame discarded, cannot malloc temp "
732 "buffer of size %u!\n", len);
736 eth = (struct l2_ethhdr *) bp;
737 memcpy(eth->h_dest, addr, ETH_ALEN);
738 memcpy(eth->h_source, own_addr, ETH_ALEN);
739 eth->h_proto = htons(ETH_P_EAPOL);
740 memcpy(eth+1, data, data_len);
742 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", bp, len);
744 status = l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, bp, len);
752 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
754 struct bsd_driver_data *drv = ctx;
755 struct hostapd_data *hapd = drv->hapd;
756 struct sta_info *sta;
758 sta = ap_get_sta(hapd, src_addr);
759 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
760 printf("Data frame from not associated STA %s\n",
761 ether_sprintf(src_addr));
762 /* XXX cannot happen */
765 ieee802_1x_receive(hapd, src_addr, buf + sizeof(struct l2_ethhdr),
766 len - sizeof(struct l2_ethhdr));
770 bsd_get_ssid(const char *ifname, void *priv, u8 *buf, int len)
772 struct bsd_driver_data *drv = priv;
773 struct hostapd_data *hapd = drv->hapd;
774 int ssid_len = get80211var(priv, IEEE80211_IOC_SSID, buf, len);
776 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "%s: ssid=\"%.*s\"\n",
777 __func__, ssid_len, buf);
783 bsd_set_ssid(const char *ifname, void *priv, const u8 *buf, int len)
785 struct bsd_driver_data *drv = priv;
786 struct hostapd_data *hapd = drv->hapd;
788 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "%s: ssid=\"%.*s\"\n",
791 return set80211var(priv, IEEE80211_IOC_SSID, buf, len);
795 bsd_set_countermeasures(void *priv, int enabled)
797 struct bsd_driver_data *drv = priv;
799 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
800 return set80211param(drv, IEEE80211_IOC_COUNTERMEASURES, enabled);
804 bsd_init(struct hostapd_data *hapd)
806 struct bsd_driver_data *drv;
808 drv = malloc(sizeof(struct bsd_driver_data));
810 printf("Could not allocate memory for bsd driver data\n");
814 memset(drv, 0, sizeof(*drv));
815 drv->ops = bsd_driver_ops;
817 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
818 if (drv->ioctl_sock < 0) {
819 perror("socket[PF_INET,SOCK_DGRAM]");
822 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
824 drv->sock_xmit = l2_packet_init(drv->iface, NULL, ETH_P_EAPOL,
825 handle_read, drv, 1);
826 if (drv->sock_xmit == NULL)
828 if (l2_packet_get_own_addr(drv->sock_xmit, hapd->own_addr))
831 bsd_set_iface_flags(drv, 0); /* mark down during setup */
833 hapd->driver = &drv->ops;
837 if (drv->sock_xmit != NULL)
838 l2_packet_deinit(drv->sock_xmit);
839 if (drv->ioctl_sock >= 0)
840 close(drv->ioctl_sock);
848 bsd_deinit(void *priv)
850 struct bsd_driver_data *drv = priv;
852 drv->hapd->driver = NULL;
854 (void) bsd_set_iface_flags(drv, 0);
855 if (drv->ioctl_sock >= 0)
856 close(drv->ioctl_sock);
857 if (drv->sock_xmit != NULL)
858 l2_packet_deinit(drv->sock_xmit);
862 static const struct driver_ops bsd_driver_ops = {
865 .deinit = bsd_deinit,
866 .set_ieee8021x = bsd_set_ieee8021x,
867 .set_privacy = bsd_set_privacy,
868 .set_encryption = bsd_set_key,
869 .get_seqnum = bsd_get_seqnum,
871 .set_generic_elem = bsd_set_opt_ie,
872 .wireless_event_init = bsd_wireless_event_init,
873 .wireless_event_deinit = bsd_wireless_event_deinit,
874 .sta_set_flags = bsd_sta_set_flags,
875 .read_sta_data = bsd_read_sta_driver_data,
876 .send_eapol = bsd_send_eapol,
877 .sta_disassoc = bsd_sta_disassoc,
878 .sta_deauth = bsd_sta_deauth,
879 .set_ssid = bsd_set_ssid,
880 .get_ssid = bsd_get_ssid,
881 .set_countermeasures = bsd_set_countermeasures,
882 .sta_clear_stats = bsd_sta_clear_stats,
885 void bsd_driver_register(void)
887 driver_register(bsd_driver_ops.name, &bsd_driver_ops);