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.
36 #include "l2_packet/l2_packet.h"
41 struct bsd_driver_data {
42 struct hostapd_data *hapd; /* back pointer */
44 char iface[IFNAMSIZ + 1];
45 struct l2_packet_data *sock_xmit; /* raw packet xmit socket */
46 int ioctl_sock; /* socket for ioctl() use */
47 int wext_sock; /* socket for wireless events */
50 static int bsd_sta_deauth(void *priv, const u8 *addr, int reason_code);
53 set80211var(struct bsd_driver_data *drv, int op, const void *arg, int arg_len)
55 struct ieee80211req ireq;
57 memset(&ireq, 0, sizeof(ireq));
58 os_strlcpy(ireq.i_name, drv->iface, IFNAMSIZ);
61 ireq.i_data = (void *) arg;
63 if (ioctl(drv->ioctl_sock, SIOCS80211, &ireq) < 0) {
64 perror("ioctl[SIOCS80211]");
71 get80211var(struct bsd_driver_data *drv, int op, void *arg, int arg_len)
73 struct ieee80211req ireq;
75 memset(&ireq, 0, sizeof(ireq));
76 os_strlcpy(ireq.i_name, drv->iface, IFNAMSIZ);
81 if (ioctl(drv->ioctl_sock, SIOCG80211, &ireq) < 0) {
82 perror("ioctl[SIOCG80211]");
89 set80211param(struct bsd_driver_data *drv, int op, int arg)
91 struct ieee80211req ireq;
93 memset(&ireq, 0, sizeof(ireq));
94 os_strlcpy(ireq.i_name, drv->iface, IFNAMSIZ);
98 if (ioctl(drv->ioctl_sock, SIOCS80211, &ireq) < 0) {
99 perror("ioctl[SIOCS80211]");
106 ether_sprintf(const u8 *addr)
108 static char buf[sizeof(MACSTR)];
111 snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
113 snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0);
118 * Configure WPA parameters.
121 bsd_configure_wpa(struct bsd_driver_data *drv)
123 static const char *ciphernames[] =
124 { "WEP", "TKIP", "AES-OCB", "AES-CCM", "CKIP", "NONE" };
125 struct hostapd_data *hapd = drv->hapd;
126 struct hostapd_bss_config *conf = hapd->conf;
129 switch (conf->wpa_group) {
130 case WPA_CIPHER_CCMP:
131 v = IEEE80211_CIPHER_AES_CCM;
133 case WPA_CIPHER_TKIP:
134 v = IEEE80211_CIPHER_TKIP;
136 case WPA_CIPHER_WEP104:
137 v = IEEE80211_CIPHER_WEP;
139 case WPA_CIPHER_WEP40:
140 v = IEEE80211_CIPHER_WEP;
142 case WPA_CIPHER_NONE:
143 v = IEEE80211_CIPHER_NONE;
146 printf("Unknown group key cipher %u\n",
150 wpa_printf(MSG_DEBUG, "%s: group key cipher=%s (%u)",
151 __func__, ciphernames[v], v);
152 if (set80211param(drv, IEEE80211_IOC_MCASTCIPHER, v)) {
153 printf("Unable to set group key cipher to %u (%s)\n",
157 if (v == IEEE80211_CIPHER_WEP) {
158 /* key length is done only for specific ciphers */
159 v = (conf->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5);
160 if (set80211param(drv, IEEE80211_IOC_MCASTKEYLEN, v)) {
161 printf("Unable to set group key length to %u\n", v);
167 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
168 v |= 1<<IEEE80211_CIPHER_AES_CCM;
169 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
170 v |= 1<<IEEE80211_CIPHER_TKIP;
171 if (conf->wpa_pairwise & WPA_CIPHER_NONE)
172 v |= 1<<IEEE80211_CIPHER_NONE;
173 wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v);
174 if (set80211param(drv, IEEE80211_IOC_UCASTCIPHERS, v)) {
175 printf("Unable to set pairwise key ciphers to 0x%x\n", v);
179 wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x",
180 __func__, conf->wpa_key_mgmt);
181 if (set80211param(drv, IEEE80211_IOC_KEYMGTALGS, conf->wpa_key_mgmt)) {
182 printf("Unable to set key management algorithms to 0x%x\n",
188 if (conf->rsn_preauth)
190 wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x",
191 __func__, conf->rsn_preauth);
192 if (set80211param(drv, IEEE80211_IOC_RSNCAPS, v)) {
193 printf("Unable to set RSN capabilities to 0x%x\n", v);
197 wpa_printf(MSG_DEBUG, "%s: enable WPA= 0x%x", __func__, conf->wpa);
198 if (set80211param(drv, IEEE80211_IOC_WPA, conf->wpa)) {
199 printf("Unable to set WPA to %u\n", conf->wpa);
207 bsd_set_iface_flags(void *priv, int dev_up)
209 struct bsd_driver_data *drv = priv;
212 wpa_printf(MSG_DEBUG, "%s: dev_up=%d", __func__, dev_up);
214 if (drv->ioctl_sock < 0)
217 memset(&ifr, 0, sizeof(ifr));
218 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
220 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
221 perror("ioctl[SIOCGIFFLAGS]");
226 ifr.ifr_flags |= IFF_UP;
228 ifr.ifr_flags &= ~IFF_UP;
230 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
231 perror("ioctl[SIOCSIFFLAGS]");
236 memset(&ifr, 0, sizeof(ifr));
237 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
238 ifr.ifr_mtu = HOSTAPD_MTU;
239 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
240 perror("ioctl[SIOCSIFMTU]");
241 printf("Setting MTU failed - trying to survive with "
250 bsd_set_ieee8021x(const char *ifname, void *priv, int enabled)
252 struct bsd_driver_data *drv = priv;
253 struct hostapd_data *hapd = drv->hapd;
254 struct hostapd_bss_config *conf = hapd->conf;
256 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
259 /* XXX restore state */
260 return set80211param(priv, IEEE80211_IOC_AUTHMODE,
261 IEEE80211_AUTH_AUTO);
263 if (!conf->wpa && !conf->ieee802_1x) {
264 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
265 HOSTAPD_LEVEL_WARNING, "No 802.1X or WPA enabled!");
268 if (conf->wpa && bsd_configure_wpa(drv) != 0) {
269 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
270 HOSTAPD_LEVEL_WARNING, "Error configuring WPA state!");
273 if (set80211param(priv, IEEE80211_IOC_AUTHMODE,
274 (conf->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) {
275 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_DRIVER,
276 HOSTAPD_LEVEL_WARNING, "Error enabling WPA/802.1X!");
279 return bsd_set_iface_flags(priv, 1);
283 bsd_set_privacy(const char *ifname, void *priv, int enabled)
285 struct bsd_driver_data *drv = priv;
287 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
289 return set80211param(drv, IEEE80211_IOC_PRIVACY, enabled);
293 bsd_set_sta_authorized(void *priv, const u8 *addr, int authorized)
295 struct bsd_driver_data *drv = priv;
296 struct ieee80211req_mlme mlme;
298 wpa_printf(MSG_DEBUG, "%s: addr=%s authorized=%d",
299 __func__, ether_sprintf(addr), authorized);
302 mlme.im_op = IEEE80211_MLME_AUTHORIZE;
304 mlme.im_op = IEEE80211_MLME_UNAUTHORIZE;
306 memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
307 return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
311 bsd_sta_set_flags(void *priv, const u8 *addr, int total_flags, int flags_or,
314 /* For now, only support setting Authorized flag */
315 if (flags_or & WLAN_STA_AUTHORIZED)
316 return bsd_set_sta_authorized(priv, addr, 1);
317 if (!(flags_and & WLAN_STA_AUTHORIZED))
318 return bsd_set_sta_authorized(priv, addr, 0);
323 bsd_del_key(void *priv, const u8 *addr, int key_idx)
325 struct bsd_driver_data *drv = priv;
326 struct ieee80211req_del_key wk;
328 wpa_printf(MSG_DEBUG, "%s: addr=%s key_idx=%d",
329 __func__, ether_sprintf(addr), key_idx);
331 memset(&wk, 0, sizeof(wk));
333 memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
334 wk.idk_keyix = (u_int8_t) IEEE80211_KEYIX_NONE; /* XXX */
336 wk.idk_keyix = key_idx;
339 return set80211var(drv, IEEE80211_IOC_DELKEY, &wk, sizeof(wk));
343 bsd_set_key(const char *ifname, void *priv, const char *alg,
344 const u8 *addr, int key_idx,
345 const u8 *key, size_t key_len, int txkey)
347 struct bsd_driver_data *drv = priv;
348 struct ieee80211req_key wk;
351 if (strcmp(alg, "none") == 0)
352 return bsd_del_key(drv, addr, key_idx);
354 wpa_printf(MSG_DEBUG, "%s: alg=%s addr=%s key_idx=%d",
355 __func__, alg, ether_sprintf(addr), key_idx);
357 if (strcmp(alg, "WEP") == 0)
358 cipher = IEEE80211_CIPHER_WEP;
359 else if (strcmp(alg, "TKIP") == 0)
360 cipher = IEEE80211_CIPHER_TKIP;
361 else if (strcmp(alg, "CCMP") == 0)
362 cipher = IEEE80211_CIPHER_AES_CCM;
364 printf("%s: unknown/unsupported algorithm %s\n",
369 if (key_len > sizeof(wk.ik_keydata)) {
370 printf("%s: key length %d too big\n", __func__, key_len);
374 memset(&wk, 0, sizeof(wk));
376 wk.ik_flags = IEEE80211_KEY_RECV | IEEE80211_KEY_XMIT;
378 memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
379 wk.ik_keyix = key_idx;
380 wk.ik_flags |= IEEE80211_KEY_DEFAULT;
382 memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
383 wk.ik_keyix = IEEE80211_KEYIX_NONE;
385 wk.ik_keylen = key_len;
386 memcpy(wk.ik_keydata, key, key_len);
388 return set80211var(drv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk));
393 bsd_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx,
396 struct bsd_driver_data *drv = priv;
397 struct ieee80211req_key wk;
399 wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d",
400 __func__, ether_sprintf(addr), idx);
402 memset(&wk, 0, sizeof(wk));
404 memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
406 memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
409 if (get80211var(drv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)) < 0) {
410 printf("Failed to get encryption.\n");
414 #ifdef WORDS_BIGENDIAN
417 * wk.ik_keytsc is in host byte order (big endian), need to
418 * swap it to match with the byte order used in WPA.
421 u8 tmp[WPA_KEY_RSC_LEN];
422 memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
423 for (i = 0; i < WPA_KEY_RSC_LEN; i++) {
424 seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1];
427 #else /* WORDS_BIGENDIAN */
428 memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
429 #endif /* WORDS_BIGENDIAN */
435 bsd_flush(void *priv)
437 u8 allsta[IEEE80211_ADDR_LEN];
439 memset(allsta, 0xff, IEEE80211_ADDR_LEN);
440 return bsd_sta_deauth(priv, allsta, IEEE80211_REASON_AUTH_LEAVE);
445 bsd_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data,
448 struct bsd_driver_data *drv = priv;
449 struct ieee80211req_sta_stats stats;
451 memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN);
452 if (get80211var(drv, IEEE80211_IOC_STA_STATS, &stats, sizeof(stats)) > 0) {
453 /* XXX? do packets counts include non-data frames? */
454 data->rx_packets = stats.is_stats.ns_rx_data;
455 data->rx_bytes = stats.is_stats.ns_rx_bytes;
456 data->tx_packets = stats.is_stats.ns_tx_data;
457 data->tx_bytes = stats.is_stats.ns_tx_bytes;
463 bsd_set_opt_ie(const char *ifname, void *priv, const u8 *ie, size_t ie_len)
466 * Do nothing; we setup parameters at startup that define the
467 * contents of the beacon information element.
473 bsd_sta_deauth(void *priv, const u8 *addr, int reason_code)
475 struct bsd_driver_data *drv = priv;
476 struct ieee80211req_mlme mlme;
478 wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
479 __func__, ether_sprintf(addr), reason_code);
481 mlme.im_op = IEEE80211_MLME_DEAUTH;
482 mlme.im_reason = reason_code;
483 memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
484 return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
488 bsd_sta_disassoc(void *priv, const u8 *addr, int reason_code)
490 struct bsd_driver_data *drv = priv;
491 struct ieee80211req_mlme mlme;
493 wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
494 __func__, ether_sprintf(addr), reason_code);
496 mlme.im_op = IEEE80211_MLME_DISASSOC;
497 mlme.im_reason = reason_code;
498 memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
499 return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
503 bsd_new_sta(struct bsd_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
505 struct hostapd_data *hapd = drv->hapd;
506 struct ieee80211req_wpaie ie;
511 * Fetch and validate any negotiated WPA/RSN parameters.
513 memset(&ie, 0, sizeof(ie));
514 memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
515 if (get80211var(drv, IEEE80211_IOC_WPAIE, &ie, sizeof(ie)) < 0) {
516 printf("Failed to get WPA/RSN information element.\n");
520 ielen = ie.wpa_ie[1];
527 return hostapd_notif_assoc(hapd, addr, iebuf, ielen);
530 #include <net/route.h>
531 #include <net80211/ieee80211_freebsd.h>
534 bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx)
536 struct bsd_driver_data *drv = ctx;
537 struct hostapd_data *hapd = drv->hapd;
539 struct if_announcemsghdr *ifan;
540 struct rt_msghdr *rtm;
541 struct ieee80211_michael_event *mic;
542 struct ieee80211_join_event *join;
543 struct ieee80211_leave_event *leave;
546 n = read(sock, buf, sizeof(buf));
548 if (errno != EINTR && errno != EAGAIN)
549 perror("read(PF_ROUTE)");
553 rtm = (struct rt_msghdr *) buf;
554 if (rtm->rtm_version != RTM_VERSION) {
555 wpa_printf(MSG_DEBUG, "Routing message version %d not "
556 "understood\n", rtm->rtm_version);
559 ifan = (struct if_announcemsghdr *) rtm;
560 switch (rtm->rtm_type) {
562 switch (ifan->ifan_what) {
563 case RTM_IEEE80211_ASSOC:
564 case RTM_IEEE80211_REASSOC:
565 case RTM_IEEE80211_DISASSOC:
566 case RTM_IEEE80211_SCAN:
568 case RTM_IEEE80211_LEAVE:
569 leave = (struct ieee80211_leave_event *) &ifan[1];
570 hostapd_notif_disassoc(drv->hapd, leave->iev_addr);
572 case RTM_IEEE80211_JOIN:
573 #ifdef RTM_IEEE80211_REJOIN
574 case RTM_IEEE80211_REJOIN:
576 join = (struct ieee80211_join_event *) &ifan[1];
577 bsd_new_sta(drv, join->iev_addr);
579 case RTM_IEEE80211_REPLAY:
582 case RTM_IEEE80211_MICHAEL:
583 mic = (struct ieee80211_michael_event *) &ifan[1];
584 wpa_printf(MSG_DEBUG,
585 "Michael MIC failure wireless event: "
586 "keyix=%u src_addr=" MACSTR, mic->iev_keyix,
587 MAC2STR(mic->iev_src));
588 hostapd_michael_mic_failure(hapd, mic->iev_src);
596 bsd_wireless_event_init(void *priv)
598 struct bsd_driver_data *drv = priv;
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(void *priv)
617 struct bsd_driver_data *drv = priv;
620 if (drv->wext_sock < 0)
622 eloop_unregister_read_sock(drv->wext_sock);
623 close(drv->wext_sock);
629 bsd_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
630 int encrypt, const u8 *own_addr)
632 struct bsd_driver_data *drv = priv;
633 unsigned char buf[3000];
634 unsigned char *bp = buf;
635 struct l2_ethhdr *eth;
640 * Prepend the Etherent header. If the caller left us
641 * space at the front we could just insert it but since
642 * we don't know we copy to a local buffer. Given the frequency
643 * and size of frames this probably doesn't matter.
645 len = data_len + sizeof(struct l2_ethhdr);
646 if (len > sizeof(buf)) {
649 printf("EAPOL frame discarded, cannot malloc temp "
650 "buffer of size %u!\n", len);
654 eth = (struct l2_ethhdr *) bp;
655 memcpy(eth->h_dest, addr, ETH_ALEN);
656 memcpy(eth->h_source, own_addr, ETH_ALEN);
657 eth->h_proto = htons(ETH_P_EAPOL);
658 memcpy(eth+1, data, data_len);
660 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", bp, len);
662 status = l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, bp, len);
670 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
672 struct bsd_driver_data *drv = ctx;
673 hostapd_eapol_receive(drv->hapd, src_addr,
674 buf + sizeof(struct l2_ethhdr),
675 len - sizeof(struct l2_ethhdr));
679 bsd_get_ssid(const char *ifname, void *priv, u8 *buf, int len)
681 struct bsd_driver_data *drv = priv;
682 int ssid_len = get80211var(drv, IEEE80211_IOC_SSID, buf, len);
684 wpa_printf(MSG_DEBUG, "%s: ssid=\"%.*s\"", __func__, ssid_len, buf);
690 bsd_set_ssid(const char *ifname, void *priv, const u8 *buf, int len)
692 struct bsd_driver_data *drv = priv;
694 wpa_printf(MSG_DEBUG, "%s: ssid=\"%.*s\"", __func__, len, buf);
696 return set80211var(drv, IEEE80211_IOC_SSID, buf, len);
700 bsd_init(struct hostapd_data *hapd)
702 struct bsd_driver_data *drv;
704 drv = os_zalloc(sizeof(struct bsd_driver_data));
706 printf("Could not allocate memory for bsd driver data\n");
711 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
712 if (drv->ioctl_sock < 0) {
713 perror("socket[PF_INET,SOCK_DGRAM]");
716 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
718 drv->sock_xmit = l2_packet_init(drv->iface, NULL, ETH_P_EAPOL,
719 handle_read, drv, 1);
720 if (drv->sock_xmit == NULL)
722 if (l2_packet_get_own_addr(drv->sock_xmit, hapd->own_addr))
725 bsd_set_iface_flags(drv, 0); /* mark down during setup */
729 if (drv->sock_xmit != NULL)
730 l2_packet_deinit(drv->sock_xmit);
731 if (drv->ioctl_sock >= 0)
732 close(drv->ioctl_sock);
740 bsd_deinit(void *priv)
742 struct bsd_driver_data *drv = priv;
744 (void) bsd_set_iface_flags(drv, 0);
745 if (drv->ioctl_sock >= 0)
746 close(drv->ioctl_sock);
747 if (drv->sock_xmit != NULL)
748 l2_packet_deinit(drv->sock_xmit);
752 const struct wpa_driver_ops wpa_driver_bsd_ops = {
755 .deinit = bsd_deinit,
756 .set_ieee8021x = bsd_set_ieee8021x,
757 .set_privacy = bsd_set_privacy,
758 .set_encryption = bsd_set_key,
759 .get_seqnum = bsd_get_seqnum,
761 .set_generic_elem = bsd_set_opt_ie,
762 .wireless_event_init = bsd_wireless_event_init,
763 .wireless_event_deinit = bsd_wireless_event_deinit,
764 .sta_set_flags = bsd_sta_set_flags,
765 .read_sta_data = bsd_read_sta_driver_data,
766 .send_eapol = bsd_send_eapol,
767 .sta_disassoc = bsd_sta_disassoc,
768 .sta_deauth = bsd_sta_deauth,
769 .set_ssid = bsd_set_ssid,
770 .get_ssid = bsd_get_ssid,