2 * hostapd / 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.
17 #include <sys/ioctl.h>
21 #include <net80211/ieee80211.h>
22 #include <net80211/ieee80211_crypto.h>
23 #include <net80211/ieee80211_ioctl.h>
26 * Avoid conflicts with hostapd definitions by undefining couple of defines
27 * from net80211 header files.
37 #include "l2_packet/l2_packet.h"
42 struct bsd_driver_data {
43 struct hostapd_data *hapd; /* back pointer */
45 char iface[IFNAMSIZ + 1];
46 struct l2_packet_data *sock_xmit; /* raw packet xmit socket */
47 int ioctl_sock; /* socket for ioctl() use */
48 int wext_sock; /* socket for wireless events */
51 static int bsd_sta_deauth(void *priv, const u8 *addr, int reason_code);
54 set80211var(struct bsd_driver_data *drv, int op, const void *arg, int arg_len)
56 struct ieee80211req ireq;
58 memset(&ireq, 0, sizeof(ireq));
59 os_strlcpy(ireq.i_name, drv->iface, IFNAMSIZ);
62 ireq.i_data = (void *) arg;
64 if (ioctl(drv->ioctl_sock, SIOCS80211, &ireq) < 0) {
65 perror("ioctl[SIOCS80211]");
72 get80211var(struct bsd_driver_data *drv, int op, void *arg, int arg_len)
74 struct ieee80211req ireq;
76 memset(&ireq, 0, sizeof(ireq));
77 os_strlcpy(ireq.i_name, drv->iface, IFNAMSIZ);
82 if (ioctl(drv->ioctl_sock, SIOCG80211, &ireq) < 0) {
83 perror("ioctl[SIOCG80211]");
90 set80211param(struct bsd_driver_data *drv, int op, int arg)
92 struct ieee80211req ireq;
94 memset(&ireq, 0, sizeof(ireq));
95 os_strlcpy(ireq.i_name, drv->iface, IFNAMSIZ);
99 if (ioctl(drv->ioctl_sock, SIOCS80211, &ireq) < 0) {
100 perror("ioctl[SIOCS80211]");
107 ether_sprintf(const u8 *addr)
109 static char buf[sizeof(MACSTR)];
112 snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
114 snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0);
119 * Configure WPA parameters.
122 bsd_configure_wpa(struct bsd_driver_data *drv)
124 static const char *ciphernames[] =
125 { "WEP", "TKIP", "AES-OCB", "AES-CCM", "CKIP", "NONE" };
126 struct hostapd_data *hapd = drv->hapd;
127 struct hostapd_bss_config *conf = hapd->conf;
130 switch (conf->wpa_group) {
131 case WPA_CIPHER_CCMP:
132 v = IEEE80211_CIPHER_AES_CCM;
134 case WPA_CIPHER_TKIP:
135 v = IEEE80211_CIPHER_TKIP;
137 case WPA_CIPHER_WEP104:
138 v = IEEE80211_CIPHER_WEP;
140 case WPA_CIPHER_WEP40:
141 v = IEEE80211_CIPHER_WEP;
143 case WPA_CIPHER_NONE:
144 v = IEEE80211_CIPHER_NONE;
147 printf("Unknown group key cipher %u\n",
151 wpa_printf(MSG_DEBUG, "%s: group key cipher=%s (%u)",
152 __func__, ciphernames[v], v);
153 if (set80211param(drv, IEEE80211_IOC_MCASTCIPHER, v)) {
154 printf("Unable to set group key cipher to %u (%s)\n",
158 if (v == IEEE80211_CIPHER_WEP) {
159 /* key length is done only for specific ciphers */
160 v = (conf->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5);
161 if (set80211param(drv, IEEE80211_IOC_MCASTKEYLEN, v)) {
162 printf("Unable to set group key length to %u\n", v);
168 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
169 v |= 1<<IEEE80211_CIPHER_AES_CCM;
170 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
171 v |= 1<<IEEE80211_CIPHER_TKIP;
172 if (conf->wpa_pairwise & WPA_CIPHER_NONE)
173 v |= 1<<IEEE80211_CIPHER_NONE;
174 wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v);
175 if (set80211param(drv, IEEE80211_IOC_UCASTCIPHERS, v)) {
176 printf("Unable to set pairwise key ciphers to 0x%x\n", v);
180 wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x",
181 __func__, conf->wpa_key_mgmt);
182 if (set80211param(drv, IEEE80211_IOC_KEYMGTALGS, conf->wpa_key_mgmt)) {
183 printf("Unable to set key management algorithms to 0x%x\n",
189 if (conf->rsn_preauth)
191 wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x",
192 __func__, conf->rsn_preauth);
193 if (set80211param(drv, IEEE80211_IOC_RSNCAPS, v)) {
194 printf("Unable to set RSN capabilities to 0x%x\n", v);
198 wpa_printf(MSG_DEBUG, "%s: enable WPA= 0x%x", __func__, conf->wpa);
199 if (set80211param(drv, IEEE80211_IOC_WPA, conf->wpa)) {
200 printf("Unable to set WPA to %u\n", conf->wpa);
208 bsd_set_iface_flags(void *priv, int dev_up)
210 struct bsd_driver_data *drv = priv;
213 wpa_printf(MSG_DEBUG, "%s: dev_up=%d", __func__, dev_up);
215 if (drv->ioctl_sock < 0)
218 memset(&ifr, 0, sizeof(ifr));
219 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
221 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
222 perror("ioctl[SIOCGIFFLAGS]");
227 ifr.ifr_flags |= IFF_UP;
229 ifr.ifr_flags &= ~IFF_UP;
231 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
232 perror("ioctl[SIOCSIFFLAGS]");
237 memset(&ifr, 0, sizeof(ifr));
238 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
239 ifr.ifr_mtu = HOSTAPD_MTU;
240 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
241 perror("ioctl[SIOCSIFMTU]");
242 printf("Setting MTU failed - trying to survive with "
251 bsd_set_ieee8021x(const char *ifname, void *priv, int enabled)
253 struct bsd_driver_data *drv = priv;
254 struct hostapd_data *hapd = drv->hapd;
255 struct hostapd_bss_config *conf = hapd->conf;
257 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
260 /* XXX restore state */
261 return set80211param(priv, IEEE80211_IOC_AUTHMODE,
262 IEEE80211_AUTH_AUTO);
264 if (!conf->wpa && !conf->ieee802_1x) {
265 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
266 HOSTAPD_LEVEL_WARNING, "No 802.1X or WPA enabled!");
269 if (conf->wpa && bsd_configure_wpa(drv) != 0) {
270 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
271 HOSTAPD_LEVEL_WARNING, "Error configuring WPA state!");
274 if (set80211param(priv, IEEE80211_IOC_AUTHMODE,
275 (conf->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) {
276 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
277 HOSTAPD_LEVEL_WARNING, "Error enabling WPA/802.1X!");
280 return bsd_set_iface_flags(priv, 1);
284 bsd_set_privacy(const char *ifname, void *priv, int enabled)
286 struct bsd_driver_data *drv = priv;
288 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
290 return set80211param(drv, IEEE80211_IOC_PRIVACY, enabled);
294 bsd_set_sta_authorized(void *priv, const u8 *addr, int authorized)
296 struct bsd_driver_data *drv = priv;
297 struct ieee80211req_mlme mlme;
299 wpa_printf(MSG_DEBUG, "%s: addr=%s authorized=%d",
300 __func__, ether_sprintf(addr), authorized);
303 mlme.im_op = IEEE80211_MLME_AUTHORIZE;
305 mlme.im_op = IEEE80211_MLME_UNAUTHORIZE;
307 memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
308 return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
312 bsd_sta_set_flags(void *priv, const u8 *addr, int total_flags, int flags_or,
315 /* For now, only support setting Authorized flag */
316 if (flags_or & WLAN_STA_AUTHORIZED)
317 return bsd_set_sta_authorized(priv, addr, 1);
318 if (!(flags_and & WLAN_STA_AUTHORIZED))
319 return bsd_set_sta_authorized(priv, addr, 0);
324 bsd_del_key(void *priv, const u8 *addr, int key_idx)
326 struct bsd_driver_data *drv = priv;
327 struct ieee80211req_del_key wk;
329 wpa_printf(MSG_DEBUG, "%s: addr=%s key_idx=%d",
330 __func__, ether_sprintf(addr), key_idx);
332 memset(&wk, 0, sizeof(wk));
334 memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
335 wk.idk_keyix = (u_int8_t) IEEE80211_KEYIX_NONE; /* XXX */
337 wk.idk_keyix = key_idx;
340 return set80211var(drv, IEEE80211_IOC_DELKEY, &wk, sizeof(wk));
344 bsd_set_key(const char *ifname, void *priv, wpa_alg alg,
345 const u8 *addr, int key_idx, int set_tx, const u8 *seq,
346 size_t seq_len, const u8 *key, size_t key_len)
348 struct bsd_driver_data *drv = priv;
349 struct ieee80211req_key wk;
352 if (alg == WPA_ALG_NONE)
353 return bsd_del_key(drv, addr, key_idx);
355 wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%s key_idx=%d",
356 __func__, alg, ether_sprintf(addr), key_idx);
358 if (alg == WPA_ALG_WEP)
359 cipher = IEEE80211_CIPHER_WEP;
360 else if (alg == WPA_ALG_TKIP)
361 cipher = IEEE80211_CIPHER_TKIP;
362 else if (alg == WPA_ALG_CCMP)
363 cipher = IEEE80211_CIPHER_AES_CCM;
365 printf("%s: unknown/unsupported algorithm %d\n",
370 if (key_len > sizeof(wk.ik_keydata)) {
371 printf("%s: key length %d too big\n", __func__, key_len);
375 memset(&wk, 0, sizeof(wk));
377 wk.ik_flags = IEEE80211_KEY_RECV | IEEE80211_KEY_XMIT;
379 memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
380 wk.ik_keyix = key_idx;
381 wk.ik_flags |= IEEE80211_KEY_DEFAULT;
383 memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
384 wk.ik_keyix = IEEE80211_KEYIX_NONE;
386 wk.ik_keylen = key_len;
387 memcpy(wk.ik_keydata, key, key_len);
389 return set80211var(drv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk));
394 bsd_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx,
397 struct bsd_driver_data *drv = priv;
398 struct ieee80211req_key wk;
400 wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d",
401 __func__, ether_sprintf(addr), idx);
403 memset(&wk, 0, sizeof(wk));
405 memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
407 memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
410 if (get80211var(drv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)) < 0) {
411 printf("Failed to get encryption.\n");
415 #ifdef WORDS_BIGENDIAN
418 * wk.ik_keytsc is in host byte order (big endian), need to
419 * swap it to match with the byte order used in WPA.
422 u8 tmp[WPA_KEY_RSC_LEN];
423 memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
424 for (i = 0; i < WPA_KEY_RSC_LEN; i++) {
425 seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1];
428 #else /* WORDS_BIGENDIAN */
429 memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
430 #endif /* WORDS_BIGENDIAN */
436 bsd_flush(void *priv)
438 u8 allsta[IEEE80211_ADDR_LEN];
440 memset(allsta, 0xff, IEEE80211_ADDR_LEN);
441 return bsd_sta_deauth(priv, allsta, IEEE80211_REASON_AUTH_LEAVE);
446 bsd_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data,
449 struct bsd_driver_data *drv = priv;
450 struct ieee80211req_sta_stats stats;
452 memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN);
453 if (get80211var(drv, IEEE80211_IOC_STA_STATS, &stats, sizeof(stats)) > 0) {
454 /* XXX? do packets counts include non-data frames? */
455 data->rx_packets = stats.is_stats.ns_rx_data;
456 data->rx_bytes = stats.is_stats.ns_rx_bytes;
457 data->tx_packets = stats.is_stats.ns_tx_data;
458 data->tx_bytes = stats.is_stats.ns_tx_bytes;
464 bsd_set_opt_ie(const char *ifname, void *priv, const u8 *ie, size_t ie_len)
467 * Do nothing; we setup parameters at startup that define the
468 * contents of the beacon information element.
474 bsd_sta_deauth(void *priv, const u8 *addr, int reason_code)
476 struct bsd_driver_data *drv = priv;
477 struct ieee80211req_mlme mlme;
479 wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
480 __func__, ether_sprintf(addr), reason_code);
482 mlme.im_op = IEEE80211_MLME_DEAUTH;
483 mlme.im_reason = reason_code;
484 memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
485 return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
489 bsd_sta_disassoc(void *priv, const u8 *addr, int reason_code)
491 struct bsd_driver_data *drv = priv;
492 struct ieee80211req_mlme mlme;
494 wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
495 __func__, ether_sprintf(addr), reason_code);
497 mlme.im_op = IEEE80211_MLME_DISASSOC;
498 mlme.im_reason = reason_code;
499 memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
500 return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
504 bsd_new_sta(struct bsd_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
506 struct hostapd_data *hapd = drv->hapd;
507 struct ieee80211req_wpaie ie;
512 * Fetch and validate any negotiated WPA/RSN parameters.
514 memset(&ie, 0, sizeof(ie));
515 memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
516 if (get80211var(drv, IEEE80211_IOC_WPAIE, &ie, sizeof(ie)) < 0) {
517 printf("Failed to get WPA/RSN information element.\n");
521 ielen = ie.wpa_ie[1];
528 return hostapd_notif_assoc(hapd, addr, iebuf, ielen);
531 #include <net/route.h>
532 #include <net80211/ieee80211_freebsd.h>
535 bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx)
537 struct bsd_driver_data *drv = ctx;
538 struct hostapd_data *hapd = drv->hapd;
540 struct if_announcemsghdr *ifan;
541 struct rt_msghdr *rtm;
542 struct ieee80211_michael_event *mic;
543 struct ieee80211_join_event *join;
544 struct ieee80211_leave_event *leave;
547 n = read(sock, buf, sizeof(buf));
549 if (errno != EINTR && errno != EAGAIN)
550 perror("read(PF_ROUTE)");
554 rtm = (struct rt_msghdr *) buf;
555 if (rtm->rtm_version != RTM_VERSION) {
556 wpa_printf(MSG_DEBUG, "Routing message version %d not "
557 "understood\n", rtm->rtm_version);
560 ifan = (struct if_announcemsghdr *) rtm;
561 switch (rtm->rtm_type) {
563 switch (ifan->ifan_what) {
564 case RTM_IEEE80211_ASSOC:
565 case RTM_IEEE80211_REASSOC:
566 case RTM_IEEE80211_DISASSOC:
567 case RTM_IEEE80211_SCAN:
569 case RTM_IEEE80211_LEAVE:
570 leave = (struct ieee80211_leave_event *) &ifan[1];
571 hostapd_notif_disassoc(drv->hapd, leave->iev_addr);
573 case RTM_IEEE80211_JOIN:
574 #ifdef RTM_IEEE80211_REJOIN
575 case RTM_IEEE80211_REJOIN:
577 join = (struct ieee80211_join_event *) &ifan[1];
578 bsd_new_sta(drv, join->iev_addr);
580 case RTM_IEEE80211_REPLAY:
583 case RTM_IEEE80211_MICHAEL:
584 mic = (struct ieee80211_michael_event *) &ifan[1];
585 wpa_printf(MSG_DEBUG,
586 "Michael MIC failure wireless event: "
587 "keyix=%u src_addr=" MACSTR, mic->iev_keyix,
588 MAC2STR(mic->iev_src));
589 hostapd_michael_mic_failure(hapd, mic->iev_src);
597 bsd_wireless_event_init(struct bsd_driver_data *drv)
603 s = socket(PF_ROUTE, SOCK_RAW, 0);
605 perror("socket(PF_ROUTE,SOCK_RAW)");
608 eloop_register_read_sock(s, bsd_wireless_event_receive, drv, NULL);
615 bsd_wireless_event_deinit(struct bsd_driver_data *drv)
617 if (drv->wext_sock < 0)
619 eloop_unregister_read_sock(drv->wext_sock);
620 close(drv->wext_sock);
625 bsd_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
626 int encrypt, const u8 *own_addr)
628 struct bsd_driver_data *drv = priv;
629 unsigned char buf[3000];
630 unsigned char *bp = buf;
631 struct l2_ethhdr *eth;
636 * Prepend the Etherent header. If the caller left us
637 * space at the front we could just insert it but since
638 * we don't know we copy to a local buffer. Given the frequency
639 * and size of frames this probably doesn't matter.
641 len = data_len + sizeof(struct l2_ethhdr);
642 if (len > sizeof(buf)) {
645 printf("EAPOL frame discarded, cannot malloc temp "
646 "buffer of size %u!\n", len);
650 eth = (struct l2_ethhdr *) bp;
651 memcpy(eth->h_dest, addr, ETH_ALEN);
652 memcpy(eth->h_source, own_addr, ETH_ALEN);
653 eth->h_proto = htons(ETH_P_EAPOL);
654 memcpy(eth+1, data, data_len);
656 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", bp, len);
658 status = l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, bp, len);
666 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
668 struct bsd_driver_data *drv = ctx;
669 hostapd_eapol_receive(drv->hapd, src_addr,
670 buf + sizeof(struct l2_ethhdr),
671 len - sizeof(struct l2_ethhdr));
675 bsd_get_ssid(const char *ifname, void *priv, u8 *buf, int len)
677 struct bsd_driver_data *drv = priv;
678 int ssid_len = get80211var(drv, IEEE80211_IOC_SSID, buf, len);
680 wpa_printf(MSG_DEBUG, "%s: ssid=\"%.*s\"", __func__, ssid_len, buf);
686 bsd_set_ssid(const char *ifname, void *priv, const u8 *buf, int len)
688 struct bsd_driver_data *drv = priv;
690 wpa_printf(MSG_DEBUG, "%s: ssid=\"%.*s\"", __func__, len, buf);
692 return set80211var(drv, IEEE80211_IOC_SSID, buf, len);
696 bsd_init(struct hostapd_data *hapd)
698 struct bsd_driver_data *drv;
700 drv = os_zalloc(sizeof(struct bsd_driver_data));
702 printf("Could not allocate memory for bsd driver data\n");
707 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
708 if (drv->ioctl_sock < 0) {
709 perror("socket[PF_INET,SOCK_DGRAM]");
712 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
714 drv->sock_xmit = l2_packet_init(drv->iface, NULL, ETH_P_EAPOL,
715 handle_read, drv, 1);
716 if (drv->sock_xmit == NULL)
718 if (l2_packet_get_own_addr(drv->sock_xmit, hapd->own_addr))
721 bsd_set_iface_flags(drv, 0); /* mark down during setup */
722 if (bsd_wireless_event_init(drv))
727 if (drv->sock_xmit != NULL)
728 l2_packet_deinit(drv->sock_xmit);
729 if (drv->ioctl_sock >= 0)
730 close(drv->ioctl_sock);
738 bsd_deinit(void *priv)
740 struct bsd_driver_data *drv = priv;
742 bsd_wireless_event_deinit(drv);
743 (void) bsd_set_iface_flags(drv, 0);
744 if (drv->ioctl_sock >= 0)
745 close(drv->ioctl_sock);
746 if (drv->sock_xmit != NULL)
747 l2_packet_deinit(drv->sock_xmit);
751 const struct hapd_driver_ops wpa_driver_bsd_ops = {
754 .deinit = bsd_deinit,
755 .set_ieee8021x = bsd_set_ieee8021x,
756 .set_privacy = bsd_set_privacy,
757 .set_key = bsd_set_key,
758 .get_seqnum = bsd_get_seqnum,
760 .set_generic_elem = bsd_set_opt_ie,
761 .sta_set_flags = bsd_sta_set_flags,
762 .read_sta_data = bsd_read_sta_driver_data,
763 .send_eapol = bsd_send_eapol,
764 .sta_disassoc = bsd_sta_disassoc,
765 .sta_deauth = bsd_sta_deauth,
766 .set_ssid = bsd_set_ssid,
767 .get_ssid = bsd_get_ssid,