2 * hostapd / Kernel driver communication via nl80211
3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * Alternatively, this software may be distributed under the terms of BSD
15 * See README and COPYING for more details.
20 #include <sys/ioctl.h>
21 #include <netlink/genl/genl.h>
22 #include <netlink/genl/family.h>
23 #include <netlink/genl/ctrl.h>
24 #include <netlink/msg.h>
25 #include <netlink/attr.h>
26 #include "nl80211_copy.h"
28 #include <netpacket/packet.h>
29 #include "wireless_copy.h"
30 #include <linux/filter.h>
31 #include <net/if_arp.h>
35 #include "ieee802_1x.h"
37 #include "ieee802_11.h"
39 #include "hw_features.h"
42 #include "radiotap_iter.h"
45 /* libnl 2.0 compatibility code */
46 #define nl_handle_alloc_cb nl_socket_alloc_cb
47 #define nl_handle_destroy nl_socket_free
48 #endif /* CONFIG_LIBNL20 */
50 enum ieee80211_msg_type
{
51 ieee80211_msg_normal
= 0,
52 ieee80211_msg_tx_callback_ack
= 1,
53 ieee80211_msg_tx_callback_fail
= 2,
56 struct i802_driver_data
{
57 struct hostapd_data
*hapd
;
59 char iface
[IFNAMSIZ
+ 1];
61 int ioctl_sock
; /* socket for ioctl() use */
62 int wext_sock
; /* socket for wireless events */
63 int eapol_sock
; /* socket for EAPOL frames */
64 int monitor_sock
; /* socket for monitor */
67 int default_if_indices
[16];
72 struct nl_handle
*nl_handle
;
73 struct nl_cache
*nl_cache
;
75 struct genl_family
*nl80211
;
76 int dtim_period
, beacon_int
;
77 unsigned int beacon_set
:1;
78 unsigned int ieee802_1x_active
:1;
85 static void add_ifidx(struct i802_driver_data
*drv
, int ifidx
)
90 for (i
= 0; i
< drv
->num_if_indices
; i
++) {
91 if (drv
->if_indices
[i
] == 0) {
92 drv
->if_indices
[i
] = ifidx
;
97 if (drv
->if_indices
!= drv
->default_if_indices
)
98 old
= drv
->if_indices
;
102 drv
->if_indices
= realloc(old
,
103 sizeof(int) * (drv
->num_if_indices
+ 1));
104 if (!drv
->if_indices
) {
106 drv
->if_indices
= drv
->default_if_indices
;
108 drv
->if_indices
= old
;
109 wpa_printf(MSG_ERROR
, "Failed to reallocate memory for "
111 wpa_printf(MSG_ERROR
, "Ignoring EAPOL on interface %d", ifidx
);
114 drv
->if_indices
[drv
->num_if_indices
] = ifidx
;
115 drv
->num_if_indices
++;
119 static void del_ifidx(struct i802_driver_data
*drv
, int ifidx
)
123 for (i
= 0; i
< drv
->num_if_indices
; i
++) {
124 if (drv
->if_indices
[i
] == ifidx
) {
125 drv
->if_indices
[i
] = 0;
132 static int have_ifidx(struct i802_driver_data
*drv
, int ifidx
)
136 if (ifidx
== drv
->bridge
)
139 for (i
= 0; i
< drv
->num_if_indices
; i
++)
140 if (drv
->if_indices
[i
] == ifidx
)
148 static int ack_handler(struct nl_msg
*msg
, void *arg
)
155 static int finish_handler(struct nl_msg
*msg
, void *arg
)
162 static int error_handler(struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
170 static int send_and_recv_msgs(struct i802_driver_data
*drv
,
172 int (*valid_handler
)(struct nl_msg
*, void *),
178 cb
= nl_cb_clone(drv
->nl_cb
);
182 err
= nl_send_auto_complete(drv
->nl_handle
, msg
);
188 nl_cb_err(cb
, NL_CB_CUSTOM
, error_handler
, &err
);
189 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, finish_handler
, &err
);
190 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, ack_handler
, &err
);
193 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
,
194 valid_handler
, valid_data
);
197 nl_recvmsgs(drv
->nl_handle
, cb
);
204 static int hostapd_set_iface_flags(struct i802_driver_data
*drv
,
205 const char *ifname
, int dev_up
)
209 if (drv
->ioctl_sock
< 0)
212 memset(&ifr
, 0, sizeof(ifr
));
213 os_strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
215 if (ioctl(drv
->ioctl_sock
, SIOCGIFFLAGS
, &ifr
) != 0) {
216 perror("ioctl[SIOCGIFFLAGS]");
217 wpa_printf(MSG_DEBUG
, "Could not read interface flags (%s)",
223 ifr
.ifr_flags
|= IFF_UP
;
225 ifr
.ifr_flags
&= ~IFF_UP
;
227 if (ioctl(drv
->ioctl_sock
, SIOCSIFFLAGS
, &ifr
) != 0) {
228 perror("ioctl[SIOCSIFFLAGS]");
236 static int nl_set_encr(int ifindex
, struct i802_driver_data
*drv
,
237 const char *alg
, const u8
*addr
, int idx
, const u8
*key
,
238 size_t key_len
, int txkey
)
247 if (strcmp(alg
, "none") == 0) {
248 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
249 0, NL80211_CMD_DEL_KEY
, 0);
251 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
252 0, NL80211_CMD_NEW_KEY
, 0);
253 NLA_PUT(msg
, NL80211_ATTR_KEY_DATA
, key_len
, key
);
254 if (strcmp(alg
, "WEP") == 0) {
256 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
259 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
261 } else if (strcmp(alg
, "TKIP") == 0)
262 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
, 0x000FAC02);
263 else if (strcmp(alg
, "CCMP") == 0)
264 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
, 0x000FAC04);
265 else if (strcmp(alg
, "IGTK") == 0)
266 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
, 0x000FAC06);
268 wpa_printf(MSG_ERROR
, "%s: Unsupported encryption "
269 "algorithm '%s'", __func__
, alg
);
276 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
277 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, idx
);
278 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
280 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
285 * If we failed or don't need to set the default TX key (below),
288 if (ret
|| !txkey
|| addr
)
295 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
296 0, NL80211_CMD_SET_KEY
, 0);
297 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, idx
);
298 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
299 if (strcmp(alg
, "IGTK") == 0)
300 NLA_PUT_FLAG(msg
, NL80211_ATTR_KEY_DEFAULT_MGMT
);
302 NLA_PUT_FLAG(msg
, NL80211_ATTR_KEY_DEFAULT
);
304 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
313 static int i802_set_encryption(const char *iface
, void *priv
, const char *alg
,
314 const u8
*addr
, int idx
, const u8
*key
,
315 size_t key_len
, int txkey
)
317 struct i802_driver_data
*drv
= priv
;
320 ret
= nl_set_encr(if_nametoindex(iface
), drv
, alg
, addr
, idx
, key
,
329 static inline int min_int(int a
, int b
)
337 static int get_key_handler(struct nl_msg
*msg
, void *arg
)
339 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
340 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
342 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
343 genlmsg_attrlen(gnlh
, 0), NULL
);
346 * TODO: validate the key index and mac address!
347 * Otherwise, there's a race condition as soon as
348 * the kernel starts sending key notifications.
351 if (tb
[NL80211_ATTR_KEY_SEQ
])
352 memcpy(arg
, nla_data(tb
[NL80211_ATTR_KEY_SEQ
]),
353 min_int(nla_len(tb
[NL80211_ATTR_KEY_SEQ
]), 6));
358 static int i802_get_seqnum(const char *iface
, void *priv
, const u8
*addr
,
361 struct i802_driver_data
*drv
= priv
;
368 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
369 0, NL80211_CMD_GET_KEY
, 0);
372 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
373 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, idx
);
374 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(iface
));
378 return send_and_recv_msgs(drv
, msg
, get_key_handler
, seq
);
384 static int i802_set_rate_sets(void *priv
, int *supp_rates
, int *basic_rates
,
387 struct i802_driver_data
*drv
= priv
;
389 u8 rates
[NL80211_MAX_SUPP_RATES
];
397 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
398 NL80211_CMD_SET_BSS
, 0);
400 for (i
= 0; i
< NL80211_MAX_SUPP_RATES
&& basic_rates
[i
] >= 0; i
++)
401 rates
[rates_len
++] = basic_rates
[i
] / 5;
403 NLA_PUT(msg
, NL80211_ATTR_BSS_BASIC_RATES
, rates_len
, rates
);
405 /* TODO: multi-BSS support */
406 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->iface
));
408 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
414 static int i802_send_frame(void *priv
, const void *data
, size_t len
,
415 int encrypt
, int flags
)
418 0x00, 0x00, /* radiotap version */
419 0x0e, 0x00, /* radiotap length */
420 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
421 IEEE80211_RADIOTAP_F_FRAG
, /* F_FRAG (fragment if required) */
423 0x00, 0x00, /* RX and TX flags to indicate that */
424 0x00, 0x00, /* this is the injected frame directly */
426 struct i802_driver_data
*drv
= priv
;
427 struct iovec iov
[2] = {
429 .iov_base
= &rtap_hdr
,
430 .iov_len
= sizeof(rtap_hdr
),
433 .iov_base
= (void*)data
,
437 struct msghdr msg
= {
448 rtap_hdr
[8] |= IEEE80211_RADIOTAP_F_WEP
;
450 return sendmsg(drv
->monitor_sock
, &msg
, flags
);
453 static int i802_send_mgmt_frame(void *priv
, const void *data
, size_t len
,
456 struct ieee80211_mgmt
*mgmt
;
457 int do_not_encrypt
= 0;
460 mgmt
= (struct ieee80211_mgmt
*) data
;
461 fc
= le_to_host16(mgmt
->frame_control
);
463 if (WLAN_FC_GET_TYPE(fc
) == WLAN_FC_TYPE_MGMT
&&
464 WLAN_FC_GET_STYPE(fc
) == WLAN_FC_STYPE_AUTH
) {
466 * Only one of the authentication frame types is encrypted.
467 * In order for static WEP encryption to work properly (i.e.,
468 * to not encrypt the frame), we need to tell mac80211 about
469 * the frames that must not be encrypted.
471 u16 auth_alg
= le_to_host16(mgmt
->u
.auth
.auth_alg
);
472 u16 auth_trans
= le_to_host16(mgmt
->u
.auth
.auth_transaction
);
473 if (auth_alg
== WLAN_AUTH_OPEN
||
474 (auth_alg
== WLAN_AUTH_SHARED_KEY
&& auth_trans
!= 3))
478 return i802_send_frame(priv
, data
, len
, !do_not_encrypt
, flags
);
481 /* Set kernel driver on given frequency (MHz) */
482 static int i802_set_freq2(void *priv
, struct hostapd_freq_params
*freq
)
484 struct i802_driver_data
*drv
= priv
;
491 drv
->last_freq
= freq
->freq
;
492 drv
->last_freq_ht
= freq
->ht_enabled
;
494 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
495 NL80211_CMD_SET_WIPHY
, 0);
497 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->iface
));
498 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, freq
->freq
);
499 if (freq
->ht_enabled
) {
500 switch (freq
->sec_channel_offset
) {
502 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
503 NL80211_CHAN_HT40MINUS
);
506 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
507 NL80211_CHAN_HT40PLUS
);
510 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
516 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
) == 0)
523 static int i802_set_rts(void *priv
, int rts
)
525 struct i802_driver_data
*drv
= priv
;
528 memset(&iwr
, 0, sizeof(iwr
));
529 os_strlcpy(iwr
.ifr_name
, drv
->hapd
->conf
->iface
, IFNAMSIZ
);
530 iwr
.u
.rts
.value
= rts
;
533 if (ioctl(drv
->ioctl_sock
, SIOCSIWRTS
, &iwr
) < 0) {
534 perror("ioctl[SIOCSIWRTS]");
542 static int i802_get_rts(void *priv
, int *rts
)
544 struct i802_driver_data
*drv
= priv
;
547 memset(&iwr
, 0, sizeof(iwr
));
548 os_strlcpy(iwr
.ifr_name
, drv
->hapd
->conf
->iface
, IFNAMSIZ
);
550 if (ioctl(drv
->ioctl_sock
, SIOCGIWRTS
, &iwr
) < 0) {
551 perror("ioctl[SIOCGIWRTS]");
555 *rts
= iwr
.u
.rts
.value
;
561 static int i802_set_frag(void *priv
, int frag
)
563 struct i802_driver_data
*drv
= priv
;
566 memset(&iwr
, 0, sizeof(iwr
));
567 os_strlcpy(iwr
.ifr_name
, drv
->hapd
->conf
->iface
, IFNAMSIZ
);
568 iwr
.u
.frag
.value
= frag
;
569 iwr
.u
.frag
.fixed
= 1;
571 if (ioctl(drv
->ioctl_sock
, SIOCSIWFRAG
, &iwr
) < 0) {
572 perror("ioctl[SIOCSIWFRAG]");
580 static int i802_get_frag(void *priv
, int *frag
)
582 struct i802_driver_data
*drv
= priv
;
585 memset(&iwr
, 0, sizeof(iwr
));
586 os_strlcpy(iwr
.ifr_name
, drv
->hapd
->conf
->iface
, IFNAMSIZ
);
588 if (ioctl(drv
->ioctl_sock
, SIOCGIWFRAG
, &iwr
) < 0) {
589 perror("ioctl[SIOCGIWFRAG]");
593 *frag
= iwr
.u
.frag
.value
;
599 static int i802_set_retry(void *priv
, int short_retry
, int long_retry
)
601 struct i802_driver_data
*drv
= priv
;
604 memset(&iwr
, 0, sizeof(iwr
));
605 os_strlcpy(iwr
.ifr_name
, drv
->hapd
->conf
->iface
, IFNAMSIZ
);
607 iwr
.u
.retry
.value
= short_retry
;
608 iwr
.u
.retry
.flags
= IW_RETRY_LIMIT
| IW_RETRY_MIN
;
609 if (ioctl(drv
->ioctl_sock
, SIOCSIWRETRY
, &iwr
) < 0) {
610 perror("ioctl[SIOCSIWRETRY(short)]");
614 iwr
.u
.retry
.value
= long_retry
;
615 iwr
.u
.retry
.flags
= IW_RETRY_LIMIT
| IW_RETRY_MAX
;
616 if (ioctl(drv
->ioctl_sock
, SIOCSIWRETRY
, &iwr
) < 0) {
617 perror("ioctl[SIOCSIWRETRY(long)]");
625 static int i802_get_retry(void *priv
, int *short_retry
, int *long_retry
)
627 struct i802_driver_data
*drv
= priv
;
630 memset(&iwr
, 0, sizeof(iwr
));
631 os_strlcpy(iwr
.ifr_name
, drv
->hapd
->conf
->iface
, IFNAMSIZ
);
633 iwr
.u
.retry
.flags
= IW_RETRY_LIMIT
| IW_RETRY_MIN
;
634 if (ioctl(drv
->ioctl_sock
, SIOCGIWRETRY
, &iwr
) < 0) {
635 perror("ioctl[SIOCGIWFRAG(short)]");
638 *short_retry
= iwr
.u
.retry
.value
;
640 iwr
.u
.retry
.flags
= IW_RETRY_LIMIT
| IW_RETRY_MAX
;
641 if (ioctl(drv
->ioctl_sock
, SIOCGIWRETRY
, &iwr
) < 0) {
642 perror("ioctl[SIOCGIWFRAG(long)]");
645 *long_retry
= iwr
.u
.retry
.value
;
651 static int i802_flush(void *priv
)
653 struct i802_driver_data
*drv
= priv
;
660 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
661 0, NL80211_CMD_DEL_STATION
, 0);
664 * XXX: FIX! this needs to flush all VLANs too
666 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
667 if_nametoindex(drv
->iface
));
669 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
675 static int get_sta_handler(struct nl_msg
*msg
, void *arg
)
677 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
678 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
679 struct hostap_sta_driver_data
*data
= arg
;
680 struct nlattr
*stats
[NL80211_STA_INFO_MAX
+ 1];
681 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
682 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
683 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
684 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
685 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
686 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
689 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
690 genlmsg_attrlen(gnlh
, 0), NULL
);
693 * TODO: validate the interface and mac address!
694 * Otherwise, there's a race condition as soon as
695 * the kernel starts sending station notifications.
698 if (!tb
[NL80211_ATTR_STA_INFO
]) {
699 wpa_printf(MSG_DEBUG
, "sta stats missing!");
702 if (nla_parse_nested(stats
, NL80211_STA_INFO_MAX
,
703 tb
[NL80211_ATTR_STA_INFO
],
705 wpa_printf(MSG_DEBUG
, "failed to parse nested attributes!");
709 if (stats
[NL80211_STA_INFO_INACTIVE_TIME
])
710 data
->inactive_msec
=
711 nla_get_u32(stats
[NL80211_STA_INFO_INACTIVE_TIME
]);
712 if (stats
[NL80211_STA_INFO_RX_BYTES
])
713 data
->rx_bytes
= nla_get_u32(stats
[NL80211_STA_INFO_RX_BYTES
]);
714 if (stats
[NL80211_STA_INFO_TX_BYTES
])
715 data
->tx_bytes
= nla_get_u32(stats
[NL80211_STA_INFO_TX_BYTES
]);
716 if (stats
[NL80211_STA_INFO_RX_PACKETS
])
718 nla_get_u32(stats
[NL80211_STA_INFO_RX_PACKETS
]);
719 if (stats
[NL80211_STA_INFO_TX_PACKETS
])
721 nla_get_u32(stats
[NL80211_STA_INFO_TX_PACKETS
]);
726 static int i802_read_sta_data(void *priv
, struct hostap_sta_driver_data
*data
,
729 struct i802_driver_data
*drv
= priv
;
732 os_memset(data
, 0, sizeof(*data
));
737 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
738 0, NL80211_CMD_GET_STATION
, 0);
740 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
741 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->iface
));
743 return send_and_recv_msgs(drv
, msg
, get_sta_handler
, data
);
749 static int i802_send_eapol(void *priv
, const u8
*addr
, const u8
*data
,
750 size_t data_len
, int encrypt
, const u8
*own_addr
)
752 struct i802_driver_data
*drv
= priv
;
753 struct ieee80211_hdr
*hdr
;
758 int qos
= sta
->flags
& WLAN_STA_WME
;
763 len
= sizeof(*hdr
) + (qos
? 2 : 0) + sizeof(rfc1042_header
) + 2 +
765 hdr
= os_zalloc(len
);
767 printf("malloc() failed for i802_send_data(len=%lu)\n",
768 (unsigned long) len
);
773 IEEE80211_FC(WLAN_FC_TYPE_DATA
, WLAN_FC_STYPE_DATA
);
774 hdr
->frame_control
|= host_to_le16(WLAN_FC_FROMDS
);
776 hdr
->frame_control
|= host_to_le16(WLAN_FC_ISWEP
);
777 #if 0 /* To be enabled if qos determination is added above */
779 hdr
->frame_control
|=
780 host_to_le16(WLAN_FC_STYPE_QOS_DATA
<< 4);
784 memcpy(hdr
->IEEE80211_DA_FROMDS
, addr
, ETH_ALEN
);
785 memcpy(hdr
->IEEE80211_BSSID_FROMDS
, own_addr
, ETH_ALEN
);
786 memcpy(hdr
->IEEE80211_SA_FROMDS
, own_addr
, ETH_ALEN
);
787 pos
= (u8
*) (hdr
+ 1);
789 #if 0 /* To be enabled if qos determination is added above */
791 /* add an empty QoS header if needed */
798 memcpy(pos
, rfc1042_header
, sizeof(rfc1042_header
));
799 pos
+= sizeof(rfc1042_header
);
800 WPA_PUT_BE16(pos
, ETH_P_PAE
);
802 memcpy(pos
, data
, data_len
);
804 res
= i802_send_frame(drv
, (u8
*) hdr
, len
, encrypt
, 0);
808 perror("i802_send_eapol: send");
809 printf("i802_send_eapol - packet len: %lu - failed\n",
810 (unsigned long) len
);
817 static int i802_sta_add2(const char *ifname
, void *priv
,
818 struct hostapd_sta_add_params
*params
)
820 struct i802_driver_data
*drv
= priv
;
828 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
829 0, NL80211_CMD_NEW_STATION
, 0);
831 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
832 if_nametoindex(drv
->iface
));
833 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, params
->addr
);
834 NLA_PUT_U16(msg
, NL80211_ATTR_STA_AID
, params
->aid
);
835 NLA_PUT(msg
, NL80211_ATTR_STA_SUPPORTED_RATES
, params
->supp_rates_len
,
837 NLA_PUT_U16(msg
, NL80211_ATTR_STA_LISTEN_INTERVAL
,
838 params
->listen_interval
);
840 #ifdef CONFIG_IEEE80211N
841 if (params
->ht_capabilities
) {
842 NLA_PUT(msg
, NL80211_ATTR_HT_CAPABILITY
,
843 params
->ht_capabilities
->length
,
844 ¶ms
->ht_capabilities
->data
);
846 #endif /* CONFIG_IEEE80211N */
848 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
850 wpa_printf(MSG_DEBUG
, "nl80211: NL80211_CMD_NEW_STATION "
851 "result: %d (%s)", ret
, strerror(-ret
));
859 static int i802_sta_remove(void *priv
, const u8
*addr
)
861 struct i802_driver_data
*drv
= priv
;
869 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
870 0, NL80211_CMD_DEL_STATION
, 0);
872 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
873 if_nametoindex(drv
->iface
));
874 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
876 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
885 static int i802_sta_set_flags(void *priv
, const u8
*addr
,
886 int total_flags
, int flags_or
, int flags_and
)
888 struct i802_driver_data
*drv
= priv
;
889 struct nl_msg
*msg
, *flags
= NULL
;
895 flags
= nlmsg_alloc();
901 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
902 0, NL80211_CMD_SET_STATION
, 0);
904 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
905 if_nametoindex(drv
->iface
));
906 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
908 if (total_flags
& WLAN_STA_AUTHORIZED
|| !drv
->ieee802_1x_active
)
909 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_AUTHORIZED
);
911 if (total_flags
& WLAN_STA_WMM
)
912 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_WME
);
914 if (total_flags
& WLAN_STA_SHORT_PREAMBLE
)
915 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_SHORT_PREAMBLE
);
917 if (total_flags
& WLAN_STA_MFP
)
918 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_MFP
);
920 if (nla_put_nested(msg
, NL80211_ATTR_STA_FLAGS
, flags
))
921 goto nla_put_failure
;
925 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
932 static int i802_set_regulatory_domain(void *priv
, unsigned int rd
)
938 static int i802_set_tx_queue_params(void *priv
, int queue
, int aifs
,
939 int cw_min
, int cw_max
, int burst_time
)
941 struct i802_driver_data
*drv
= priv
;
943 struct nlattr
*txq
, *params
;
949 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
950 0, NL80211_CMD_SET_WIPHY
, 0);
952 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->iface
));
954 txq
= nla_nest_start(msg
, NL80211_ATTR_WIPHY_TXQ_PARAMS
);
956 goto nla_put_failure
;
958 /* We are only sending parameters for a single TXQ at a time */
959 params
= nla_nest_start(msg
, 1);
961 goto nla_put_failure
;
963 NLA_PUT_U8(msg
, NL80211_TXQ_ATTR_QUEUE
, queue
);
964 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
965 * 32 usec, so need to convert the value here. */
966 NLA_PUT_U16(msg
, NL80211_TXQ_ATTR_TXOP
, (burst_time
* 100 + 16) / 32);
967 NLA_PUT_U16(msg
, NL80211_TXQ_ATTR_CWMIN
, cw_min
);
968 NLA_PUT_U16(msg
, NL80211_TXQ_ATTR_CWMAX
, cw_max
);
969 NLA_PUT_U8(msg
, NL80211_TXQ_ATTR_AIFS
, aifs
);
971 nla_nest_end(msg
, params
);
973 nla_nest_end(msg
, txq
);
975 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
) == 0)
982 static void nl80211_remove_iface(struct i802_driver_data
*drv
, int ifidx
)
986 /* stop listening for EAPOL on this interface */
987 del_ifidx(drv
, ifidx
);
991 goto nla_put_failure
;
993 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
994 0, NL80211_CMD_DEL_INTERFACE
, 0);
995 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifidx
);
997 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
) == 0)
1000 printf("Failed to remove interface.\n");
1004 static int nl80211_create_iface(struct i802_driver_data
*drv
,
1006 enum nl80211_iftype iftype
,
1009 struct nl_msg
*msg
, *flags
= NULL
;
1015 msg
= nlmsg_alloc();
1019 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1020 0, NL80211_CMD_NEW_INTERFACE
, 0);
1021 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
1022 if_nametoindex(drv
->hapd
->conf
->iface
));
1023 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, ifname
);
1024 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, iftype
);
1026 if (iftype
== NL80211_IFTYPE_MONITOR
) {
1029 flags
= nlmsg_alloc();
1031 goto nla_put_failure
;
1033 NLA_PUT_FLAG(flags
, NL80211_MNTR_FLAG_COOK_FRAMES
);
1035 err
= nla_put_nested(msg
, NL80211_ATTR_MNTR_FLAGS
, flags
);
1040 goto nla_put_failure
;
1043 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1046 printf("Failed to create interface %s.\n", ifname
);
1050 ifidx
= if_nametoindex(ifname
);
1055 /* start listening for EAPOL on this interface */
1056 add_ifidx(drv
, ifidx
);
1060 case NL80211_IFTYPE_AP
:
1061 os_strlcpy(ifreq
.ifr_name
, ifname
, IFNAMSIZ
);
1062 memcpy(ifreq
.ifr_hwaddr
.sa_data
, addr
, ETH_ALEN
);
1063 ifreq
.ifr_hwaddr
.sa_family
= ARPHRD_ETHER
;
1065 if (ioctl(drv
->ioctl_sock
, SIOCSIFHWADDR
, &ifreq
)) {
1066 nl80211_remove_iface(drv
, ifidx
);
1070 case NL80211_IFTYPE_WDS
:
1071 memset(&iwr
, 0, sizeof(iwr
));
1072 os_strlcpy(iwr
.ifr_name
, ifname
, IFNAMSIZ
);
1073 iwr
.u
.addr
.sa_family
= ARPHRD_ETHER
;
1074 memcpy(iwr
.u
.addr
.sa_data
, addr
, ETH_ALEN
);
1075 if (ioctl(drv
->ioctl_sock
, SIOCSIWAP
, &iwr
))
1088 static int i802_bss_add(void *priv
, const char *ifname
, const u8
*bssid
)
1093 * The kernel supports that when the low-level driver does,
1094 * but we currently don't because we need per-BSS data that
1095 * currently we can't handle easily.
1099 ifidx
= nl80211_create_iface(priv
, ifname
, NL80211_IFTYPE_AP
, bssid
);
1102 if (hostapd_set_iface_flags(priv
, ifname
, 1)) {
1103 nl80211_remove_iface(priv
, ifidx
);
1110 static int i802_bss_remove(void *priv
, const char *ifname
)
1112 nl80211_remove_iface(priv
, if_nametoindex(ifname
));
1117 static int i802_set_beacon(const char *iface
, void *priv
,
1118 u8
*head
, size_t head_len
,
1119 u8
*tail
, size_t tail_len
)
1121 struct i802_driver_data
*drv
= priv
;
1123 u8 cmd
= NL80211_CMD_NEW_BEACON
;
1126 msg
= nlmsg_alloc();
1130 if (drv
->beacon_set
)
1131 cmd
= NL80211_CMD_SET_BEACON
;
1133 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1135 NLA_PUT(msg
, NL80211_ATTR_BEACON_HEAD
, head_len
, head
);
1136 NLA_PUT(msg
, NL80211_ATTR_BEACON_TAIL
, tail_len
, tail
);
1137 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(iface
));
1138 NLA_PUT_U32(msg
, NL80211_ATTR_BEACON_INTERVAL
, drv
->beacon_int
);
1140 if (!drv
->dtim_period
)
1141 drv
->dtim_period
= 2;
1142 NLA_PUT_U32(msg
, NL80211_ATTR_DTIM_PERIOD
, drv
->dtim_period
);
1144 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1146 drv
->beacon_set
= 1;
1153 static int i802_del_beacon(struct i802_driver_data
*drv
)
1157 msg
= nlmsg_alloc();
1161 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1162 0, NL80211_CMD_DEL_BEACON
, 0);
1163 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->iface
));
1165 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1171 static int i802_set_ieee8021x(const char *ifname
, void *priv
, int enabled
)
1173 struct i802_driver_data
*drv
= priv
;
1176 * FIXME: This needs to be per interface (BSS)
1178 drv
->ieee802_1x_active
= enabled
;
1183 static int i802_set_privacy(const char *ifname
, void *priv
, int enabled
)
1185 struct i802_driver_data
*drv
= priv
;
1188 memset(&iwr
, 0, sizeof(iwr
));
1190 os_strlcpy(iwr
.ifr_name
, ifname
, IFNAMSIZ
);
1191 iwr
.u
.param
.flags
= IW_AUTH_PRIVACY_INVOKED
;
1192 iwr
.u
.param
.value
= enabled
;
1194 ioctl(drv
->ioctl_sock
, SIOCSIWAUTH
, &iwr
);
1196 /* ignore errors, the kernel/driver might not care */
1201 static int i802_set_internal_bridge(void *priv
, int value
)
1207 static int i802_set_beacon_int(void *priv
, int value
)
1209 struct i802_driver_data
*drv
= priv
;
1212 drv
->beacon_int
= value
;
1214 if (!drv
->beacon_set
)
1217 msg
= nlmsg_alloc();
1221 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1222 0, NL80211_CMD_SET_BEACON
, 0);
1223 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->iface
));
1225 NLA_PUT_U32(msg
, NL80211_ATTR_BEACON_INTERVAL
, value
);
1227 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1233 static int i802_set_dtim_period(const char *iface
, void *priv
, int value
)
1235 struct i802_driver_data
*drv
= priv
;
1238 msg
= nlmsg_alloc();
1242 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1243 0, NL80211_CMD_SET_BEACON
, 0);
1244 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(iface
));
1246 drv
->dtim_period
= value
;
1247 NLA_PUT_U32(msg
, NL80211_ATTR_DTIM_PERIOD
, drv
->dtim_period
);
1249 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1255 static int i802_set_bss(void *priv
, int cts
, int preamble
, int slot
)
1257 struct i802_driver_data
*drv
= priv
;
1260 msg
= nlmsg_alloc();
1264 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
1265 NL80211_CMD_SET_BSS
, 0);
1268 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_CTS_PROT
, cts
);
1270 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_SHORT_PREAMBLE
, preamble
);
1272 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_SHORT_SLOT_TIME
, slot
);
1274 /* TODO: multi-BSS support */
1275 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->iface
));
1277 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1283 static int i802_set_cts_protect(void *priv
, int value
)
1285 return i802_set_bss(priv
, value
, -1, -1);
1289 static int i802_set_preamble(void *priv
, int value
)
1291 return i802_set_bss(priv
, -1, value
, -1);
1295 static int i802_set_short_slot_time(void *priv
, int value
)
1297 return i802_set_bss(priv
, -1, -1, value
);
1301 static enum nl80211_iftype
i802_if_type(enum hostapd_driver_if_type type
)
1304 case HOSTAPD_IF_VLAN
:
1305 return NL80211_IFTYPE_AP_VLAN
;
1306 case HOSTAPD_IF_WDS
:
1307 return NL80211_IFTYPE_WDS
;
1313 static int i802_if_add(const char *iface
, void *priv
,
1314 enum hostapd_driver_if_type type
, char *ifname
,
1317 if (nl80211_create_iface(priv
, ifname
, i802_if_type(type
), addr
) < 0)
1323 static int i802_if_update(void *priv
, enum hostapd_driver_if_type type
,
1324 char *ifname
, const u8
*addr
)
1326 /* unused at the moment */
1331 static int i802_if_remove(void *priv
, enum hostapd_driver_if_type type
,
1332 const char *ifname
, const u8
*addr
)
1334 nl80211_remove_iface(priv
, if_nametoindex(ifname
));
1339 struct phy_info_arg
{
1341 struct hostapd_hw_modes
*modes
;
1344 static int phy_info_handler(struct nl_msg
*msg
, void *arg
)
1346 struct nlattr
*tb_msg
[NL80211_ATTR_MAX
+ 1];
1347 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
1348 struct phy_info_arg
*phy_info
= arg
;
1350 struct nlattr
*tb_band
[NL80211_BAND_ATTR_MAX
+ 1];
1352 struct nlattr
*tb_freq
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
1353 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
1354 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
1355 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
1356 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
1357 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
1358 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
1359 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
1362 struct nlattr
*tb_rate
[NL80211_BITRATE_ATTR_MAX
+ 1];
1363 static struct nla_policy rate_policy
[NL80211_BITRATE_ATTR_MAX
+ 1] = {
1364 [NL80211_BITRATE_ATTR_RATE
] = { .type
= NLA_U32
},
1365 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE
] = { .type
= NLA_FLAG
},
1368 struct nlattr
*nl_band
;
1369 struct nlattr
*nl_freq
;
1370 struct nlattr
*nl_rate
;
1371 int rem_band
, rem_freq
, rem_rate
;
1372 struct hostapd_hw_modes
*mode
;
1373 int idx
, mode_is_set
;
1375 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
1376 genlmsg_attrlen(gnlh
, 0), NULL
);
1378 if (!tb_msg
[NL80211_ATTR_WIPHY_BANDS
])
1381 nla_for_each_nested(nl_band
, tb_msg
[NL80211_ATTR_WIPHY_BANDS
], rem_band
) {
1382 mode
= realloc(phy_info
->modes
, (*phy_info
->num_modes
+ 1) * sizeof(*mode
));
1385 phy_info
->modes
= mode
;
1389 mode
= &phy_info
->modes
[*(phy_info
->num_modes
)];
1390 memset(mode
, 0, sizeof(*mode
));
1391 *(phy_info
->num_modes
) += 1;
1393 nla_parse(tb_band
, NL80211_BAND_ATTR_MAX
, nla_data(nl_band
),
1394 nla_len(nl_band
), NULL
);
1396 if (tb_band
[NL80211_BAND_ATTR_HT_CAPA
]) {
1397 mode
->ht_capab
= nla_get_u16(
1398 tb_band
[NL80211_BAND_ATTR_HT_CAPA
]);
1401 nla_for_each_nested(nl_freq
, tb_band
[NL80211_BAND_ATTR_FREQS
], rem_freq
) {
1402 nla_parse(tb_freq
, NL80211_FREQUENCY_ATTR_MAX
, nla_data(nl_freq
),
1403 nla_len(nl_freq
), freq_policy
);
1404 if (!tb_freq
[NL80211_FREQUENCY_ATTR_FREQ
])
1406 mode
->num_channels
++;
1409 mode
->channels
= calloc(mode
->num_channels
, sizeof(struct hostapd_channel_data
));
1410 if (!mode
->channels
)
1415 nla_for_each_nested(nl_freq
, tb_band
[NL80211_BAND_ATTR_FREQS
], rem_freq
) {
1416 nla_parse(tb_freq
, NL80211_FREQUENCY_ATTR_MAX
, nla_data(nl_freq
),
1417 nla_len(nl_freq
), freq_policy
);
1418 if (!tb_freq
[NL80211_FREQUENCY_ATTR_FREQ
])
1421 mode
->channels
[idx
].freq
= nla_get_u32(tb_freq
[NL80211_FREQUENCY_ATTR_FREQ
]);
1422 mode
->channels
[idx
].flag
= 0;
1425 /* crude heuristic */
1426 if (mode
->channels
[idx
].freq
< 4000)
1427 mode
->mode
= HOSTAPD_MODE_IEEE80211B
;
1429 mode
->mode
= HOSTAPD_MODE_IEEE80211A
;
1433 /* crude heuristic */
1434 if (mode
->channels
[idx
].freq
< 4000)
1435 if (mode
->channels
[idx
].freq
== 2484)
1436 mode
->channels
[idx
].chan
= 14;
1438 mode
->channels
[idx
].chan
= (mode
->channels
[idx
].freq
- 2407) / 5;
1440 mode
->channels
[idx
].chan
= mode
->channels
[idx
].freq
/5 - 1000;
1442 if (tb_freq
[NL80211_FREQUENCY_ATTR_DISABLED
])
1443 mode
->channels
[idx
].flag
|=
1444 HOSTAPD_CHAN_DISABLED
;
1445 if (tb_freq
[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
])
1446 mode
->channels
[idx
].flag
|=
1447 HOSTAPD_CHAN_PASSIVE_SCAN
;
1448 if (tb_freq
[NL80211_FREQUENCY_ATTR_NO_IBSS
])
1449 mode
->channels
[idx
].flag
|=
1450 HOSTAPD_CHAN_NO_IBSS
;
1451 if (tb_freq
[NL80211_FREQUENCY_ATTR_RADAR
])
1452 mode
->channels
[idx
].flag
|=
1455 if (tb_freq
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] &&
1456 !tb_freq
[NL80211_FREQUENCY_ATTR_DISABLED
])
1457 mode
->channels
[idx
].max_tx_power
=
1458 nla_get_u32(tb_freq
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]) / 100;
1463 nla_for_each_nested(nl_rate
, tb_band
[NL80211_BAND_ATTR_RATES
], rem_rate
) {
1464 nla_parse(tb_rate
, NL80211_BITRATE_ATTR_MAX
, nla_data(nl_rate
),
1465 nla_len(nl_rate
), rate_policy
);
1466 if (!tb_rate
[NL80211_BITRATE_ATTR_RATE
])
1471 mode
->rates
= calloc(mode
->num_rates
, sizeof(struct hostapd_rate_data
));
1477 nla_for_each_nested(nl_rate
, tb_band
[NL80211_BAND_ATTR_RATES
], rem_rate
) {
1478 nla_parse(tb_rate
, NL80211_BITRATE_ATTR_MAX
, nla_data(nl_rate
),
1479 nla_len(nl_rate
), rate_policy
);
1480 if (!tb_rate
[NL80211_BITRATE_ATTR_RATE
])
1482 mode
->rates
[idx
].rate
= nla_get_u32(tb_rate
[NL80211_BITRATE_ATTR_RATE
]);
1484 /* crude heuristic */
1485 if (mode
->mode
== HOSTAPD_MODE_IEEE80211B
&&
1486 mode
->rates
[idx
].rate
> 200)
1487 mode
->mode
= HOSTAPD_MODE_IEEE80211G
;
1489 if (tb_rate
[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE
])
1490 mode
->rates
[idx
].flags
|= HOSTAPD_RATE_PREAMBLE2
;
1499 static struct hostapd_hw_modes
*i802_add_11b(struct hostapd_hw_modes
*modes
,
1503 struct hostapd_hw_modes
*mode11g
= NULL
, *nmodes
, *mode
;
1504 int i
, mode11g_idx
= -1;
1506 /* If only 802.11g mode is included, use it to construct matching
1507 * 802.11b mode data. */
1509 for (m
= 0; m
< *num_modes
; m
++) {
1510 if (modes
[m
].mode
== HOSTAPD_MODE_IEEE80211B
)
1511 return modes
; /* 802.11b already included */
1512 if (modes
[m
].mode
== HOSTAPD_MODE_IEEE80211G
)
1516 if (mode11g_idx
< 0)
1517 return modes
; /* 2.4 GHz band not supported at all */
1519 nmodes
= os_realloc(modes
, (*num_modes
+ 1) * sizeof(*nmodes
));
1521 return modes
; /* Could not add 802.11b mode */
1523 mode
= &nmodes
[*num_modes
];
1524 os_memset(mode
, 0, sizeof(*mode
));
1528 mode
->mode
= HOSTAPD_MODE_IEEE80211B
;
1530 mode11g
= &modes
[mode11g_idx
];
1531 mode
->num_channels
= mode11g
->num_channels
;
1532 mode
->channels
= os_malloc(mode11g
->num_channels
*
1533 sizeof(struct hostapd_channel_data
));
1534 if (mode
->channels
== NULL
) {
1536 return modes
; /* Could not add 802.11b mode */
1538 os_memcpy(mode
->channels
, mode11g
->channels
,
1539 mode11g
->num_channels
* sizeof(struct hostapd_channel_data
));
1541 mode
->num_rates
= 0;
1542 mode
->rates
= os_malloc(4 * sizeof(struct hostapd_rate_data
));
1543 if (mode
->rates
== NULL
) {
1544 os_free(mode
->channels
);
1546 return modes
; /* Could not add 802.11b mode */
1549 for (i
= 0; i
< mode11g
->num_rates
; i
++) {
1550 if (mode11g
->rates
[i
].rate
> 110 ||
1551 mode11g
->rates
[i
].flags
&
1552 (HOSTAPD_RATE_ERP
| HOSTAPD_RATE_OFDM
))
1554 mode
->rates
[mode
->num_rates
] = mode11g
->rates
[i
];
1556 if (mode
->num_rates
== 4)
1560 if (mode
->num_rates
== 0) {
1561 os_free(mode
->channels
);
1562 os_free(mode
->rates
);
1564 return modes
; /* No 802.11b rates */
1567 wpa_printf(MSG_DEBUG
, "nl80211: Added 802.11b mode based on 802.11g "
1573 static struct hostapd_hw_modes
*i802_get_hw_feature_data(void *priv
,
1577 struct i802_driver_data
*drv
= priv
;
1579 struct phy_info_arg result
= {
1580 .num_modes
= num_modes
,
1587 msg
= nlmsg_alloc();
1591 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1592 0, NL80211_CMD_GET_WIPHY
, 0);
1594 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->iface
));
1596 if (send_and_recv_msgs(drv
, msg
, phy_info_handler
, &result
) == 0)
1597 return i802_add_11b(result
.modes
, num_modes
);
1603 static int i802_set_sta_vlan(void *priv
, const u8
*addr
,
1604 const char *ifname
, int vlan_id
)
1606 struct i802_driver_data
*drv
= priv
;
1609 msg
= nlmsg_alloc();
1613 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1614 0, NL80211_CMD_SET_STATION
, 0);
1616 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
1617 if_nametoindex(drv
->iface
));
1618 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
1619 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
1620 if_nametoindex(ifname
));
1622 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1628 static int i802_set_country(void *priv
, const char *country
)
1630 struct i802_driver_data
*drv
= priv
;
1634 msg
= nlmsg_alloc();
1638 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1639 0, NL80211_CMD_REQ_SET_REG
, 0);
1641 alpha2
[0] = country
[0];
1642 alpha2
[1] = country
[1];
1644 NLA_PUT_STRING(msg
, NL80211_ATTR_REG_ALPHA2
, alpha2
);
1646 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1652 static void handle_unknown_sta(struct hostapd_data
*hapd
, u8
*ta
)
1654 struct sta_info
*sta
;
1656 sta
= ap_get_sta(hapd
, ta
);
1657 if (!sta
|| !(sta
->flags
& WLAN_STA_ASSOC
)) {
1658 printf("Data/PS-poll frame from not associated STA "
1659 MACSTR
"\n", MAC2STR(ta
));
1660 if (sta
&& (sta
->flags
& WLAN_STA_AUTH
))
1661 hostapd_sta_disassoc(
1663 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA
);
1667 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA
);
1672 static void handle_tx_callback(struct hostapd_data
*hapd
, u8
*buf
, size_t len
,
1675 struct ieee80211_hdr
*hdr
;
1676 u16 fc
, type
, stype
;
1677 struct sta_info
*sta
;
1679 hdr
= (struct ieee80211_hdr
*) buf
;
1680 fc
= le_to_host16(hdr
->frame_control
);
1682 type
= WLAN_FC_GET_TYPE(fc
);
1683 stype
= WLAN_FC_GET_STYPE(fc
);
1686 case WLAN_FC_TYPE_MGMT
:
1687 wpa_printf(MSG_DEBUG
, "MGMT (TX callback) %s",
1688 ok
? "ACK" : "fail");
1689 ieee802_11_mgmt_cb(hapd
, buf
, len
, stype
, ok
);
1691 case WLAN_FC_TYPE_CTRL
:
1692 wpa_printf(MSG_DEBUG
, "CTRL (TX callback) %s",
1693 ok
? "ACK" : "fail");
1695 case WLAN_FC_TYPE_DATA
:
1696 sta
= ap_get_sta(hapd
, hdr
->addr1
);
1697 if (sta
&& sta
->flags
& WLAN_STA_PENDING_POLL
) {
1698 wpa_printf(MSG_DEBUG
, "STA " MACSTR
" %s pending "
1699 "activity poll", MAC2STR(sta
->addr
),
1700 ok
? "ACKed" : "did not ACK");
1702 sta
->flags
&= ~WLAN_STA_PENDING_POLL
;
1705 ieee802_1x_tx_status(hapd
, sta
, buf
, len
, ok
);
1708 printf("unknown TX callback frame type %d\n", type
);
1714 static void handle_frame(struct hostapd_iface
*iface
, u8
*buf
, size_t len
,
1715 struct hostapd_frame_info
*hfi
,
1716 enum ieee80211_msg_type msg_type
)
1718 struct ieee80211_hdr
*hdr
;
1719 u16 fc
, type
, stype
;
1720 size_t data_len
= len
;
1721 struct hostapd_data
*hapd
= NULL
;
1722 int broadcast_bssid
= 0;
1727 * PS-Poll frames are 16 bytes. All other frames are
1728 * 24 bytes or longer.
1733 hdr
= (struct ieee80211_hdr
*) buf
;
1734 fc
= le_to_host16(hdr
->frame_control
);
1736 type
= WLAN_FC_GET_TYPE(fc
);
1737 stype
= WLAN_FC_GET_STYPE(fc
);
1740 case WLAN_FC_TYPE_DATA
:
1743 switch (fc
& (WLAN_FC_FROMDS
| WLAN_FC_TODS
)) {
1747 case WLAN_FC_FROMDS
:
1755 case WLAN_FC_TYPE_CTRL
:
1756 /* discard non-ps-poll frames */
1757 if (stype
!= WLAN_FC_STYPE_PSPOLL
)
1761 case WLAN_FC_TYPE_MGMT
:
1769 /* find interface frame belongs to */
1770 for (i
= 0; i
< iface
->num_bss
; i
++) {
1771 if (memcmp(bssid
, iface
->bss
[i
]->own_addr
, ETH_ALEN
) == 0) {
1772 hapd
= iface
->bss
[i
];
1778 hapd
= iface
->bss
[0];
1780 if (bssid
[0] != 0xff || bssid
[1] != 0xff ||
1781 bssid
[2] != 0xff || bssid
[3] != 0xff ||
1782 bssid
[4] != 0xff || bssid
[5] != 0xff) {
1784 * Unknown BSSID - drop frame if this is not from
1785 * passive scanning or a beacon (at least ProbeReq
1786 * frames to other APs may be allowed through RX
1787 * filtering in the wlan hw/driver)
1789 if ((type
!= WLAN_FC_TYPE_MGMT
||
1790 stype
!= WLAN_FC_STYPE_BEACON
))
1793 broadcast_bssid
= 1;
1797 case ieee80211_msg_normal
:
1798 /* continue processing */
1800 case ieee80211_msg_tx_callback_ack
:
1801 handle_tx_callback(hapd
, buf
, data_len
, 1);
1803 case ieee80211_msg_tx_callback_fail
:
1804 handle_tx_callback(hapd
, buf
, data_len
, 0);
1809 case WLAN_FC_TYPE_MGMT
:
1810 if (stype
!= WLAN_FC_STYPE_BEACON
&&
1811 stype
!= WLAN_FC_STYPE_PROBE_REQ
)
1812 wpa_printf(MSG_MSGDUMP
, "MGMT");
1813 if (broadcast_bssid
) {
1814 for (i
= 0; i
< iface
->num_bss
; i
++)
1815 ieee802_11_mgmt(iface
->bss
[i
], buf
, data_len
,
1818 ieee802_11_mgmt(hapd
, buf
, data_len
, stype
, hfi
);
1820 case WLAN_FC_TYPE_CTRL
:
1821 /* can only get here with PS-Poll frames */
1822 wpa_printf(MSG_DEBUG
, "CTRL");
1823 handle_unknown_sta(hapd
, hdr
->addr2
);
1825 case WLAN_FC_TYPE_DATA
:
1826 handle_unknown_sta(hapd
, hdr
->addr2
);
1832 static void handle_eapol(int sock
, void *eloop_ctx
, void *sock_ctx
)
1834 struct i802_driver_data
*drv
= eloop_ctx
;
1835 struct hostapd_data
*hapd
= drv
->hapd
;
1836 struct sockaddr_ll lladdr
;
1837 unsigned char buf
[3000];
1839 socklen_t fromlen
= sizeof(lladdr
);
1841 len
= recvfrom(sock
, buf
, sizeof(buf
), 0,
1842 (struct sockaddr
*)&lladdr
, &fromlen
);
1848 if (have_ifidx(drv
, lladdr
.sll_ifindex
))
1849 ieee802_1x_receive(hapd
, lladdr
.sll_addr
, buf
, len
);
1853 static void handle_monitor_read(int sock
, void *eloop_ctx
, void *sock_ctx
)
1855 struct i802_driver_data
*drv
= eloop_ctx
;
1857 unsigned char buf
[3000];
1858 struct hostapd_data
*hapd
= drv
->hapd
;
1859 struct ieee80211_radiotap_iterator iter
;
1861 struct hostapd_frame_info hfi
;
1862 int injected
= 0, failed
= 0, msg_type
, rxflags
= 0;
1864 len
= recv(sock
, buf
, sizeof(buf
), 0);
1870 if (ieee80211_radiotap_iterator_init(&iter
, (void*)buf
, len
)) {
1871 printf("received invalid radiotap frame\n");
1875 memset(&hfi
, 0, sizeof(hfi
));
1878 ret
= ieee80211_radiotap_iterator_next(&iter
);
1882 printf("received invalid radiotap frame (%d)\n", ret
);
1885 switch (iter
.this_arg_index
) {
1886 case IEEE80211_RADIOTAP_FLAGS
:
1887 if (*iter
.this_arg
& IEEE80211_RADIOTAP_F_FCS
)
1890 case IEEE80211_RADIOTAP_RX_FLAGS
:
1893 case IEEE80211_RADIOTAP_TX_FLAGS
:
1895 failed
= le_to_host16((*(uint16_t *) iter
.this_arg
)) &
1896 IEEE80211_RADIOTAP_F_TX_FAIL
;
1898 case IEEE80211_RADIOTAP_DATA_RETRIES
:
1900 case IEEE80211_RADIOTAP_CHANNEL
:
1901 /* TODO convert from freq/flags to channel number
1906 case IEEE80211_RADIOTAP_RATE
:
1907 hfi
.datarate
= *iter
.this_arg
* 5;
1909 case IEEE80211_RADIOTAP_DB_ANTSIGNAL
:
1910 hfi
.ssi_signal
= *iter
.this_arg
;
1915 if (rxflags
&& injected
)
1919 msg_type
= ieee80211_msg_normal
;
1921 msg_type
= ieee80211_msg_tx_callback_fail
;
1923 msg_type
= ieee80211_msg_tx_callback_ack
;
1925 handle_frame(hapd
->iface
, buf
+ iter
.max_length
,
1926 len
- iter
.max_length
, &hfi
, msg_type
);
1931 * we post-process the filter code later and rewrite
1932 * this to the offset to the last instruction
1937 static struct sock_filter msock_filter_insns
[] = {
1939 * do a little-endian load of the radiotap length field
1941 /* load lower byte into A */
1942 BPF_STMT(BPF_LD
| BPF_B
| BPF_ABS
, 2),
1943 /* put it into X (== index register) */
1944 BPF_STMT(BPF_MISC
| BPF_TAX
, 0),
1945 /* load upper byte into A */
1946 BPF_STMT(BPF_LD
| BPF_B
| BPF_ABS
, 3),
1947 /* left-shift it by 8 */
1948 BPF_STMT(BPF_ALU
| BPF_LSH
| BPF_K
, 8),
1950 BPF_STMT(BPF_ALU
| BPF_OR
| BPF_X
, 0),
1951 /* put result into X */
1952 BPF_STMT(BPF_MISC
| BPF_TAX
, 0),
1955 * Allow management frames through, this also gives us those
1956 * management frames that we sent ourselves with status
1958 /* load the lower byte of the IEEE 802.11 frame control field */
1959 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
1960 /* mask off frame type and version */
1961 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0xF),
1962 /* accept frame if it's both 0, fall through otherwise */
1963 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 0, PASS
, 0),
1966 * TODO: add a bit to radiotap RX flags that indicates
1967 * that the sending station is not associated, then
1968 * add a filter here that filters on our DA and that flag
1969 * to allow us to deauth frames to that bad station.
1971 * Not a regression -- we didn't do it before either.
1976 * drop non-data frames, WDS frames
1978 /* load the lower byte of the frame control field */
1979 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
1980 /* mask off QoS bit */
1981 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0x0c),
1982 /* drop non-data frames */
1983 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 8, 0, FAIL
),
1984 /* load the upper byte of the frame control field */
1985 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
1986 /* mask off toDS/fromDS */
1987 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0x03),
1988 /* drop WDS frames */
1989 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 3, FAIL
, 0),
1993 * add header length to index
1995 /* load the lower byte of the frame control field */
1996 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
1997 /* mask off QoS bit */
1998 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0x80),
1999 /* right shift it by 6 to give 0 or 2 */
2000 BPF_STMT(BPF_ALU
| BPF_RSH
| BPF_K
, 6),
2001 /* add data frame header length */
2002 BPF_STMT(BPF_ALU
| BPF_ADD
| BPF_K
, 24),
2003 /* add index, was start of 802.11 header */
2004 BPF_STMT(BPF_ALU
| BPF_ADD
| BPF_X
, 0),
2005 /* move to index, now start of LL header */
2006 BPF_STMT(BPF_MISC
| BPF_TAX
, 0),
2009 * Accept empty data frames, we use those for
2012 BPF_STMT(BPF_LD
| BPF_W
| BPF_LEN
, 0),
2013 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_X
, 0, PASS
, 0),
2016 * Accept EAPOL frames
2018 BPF_STMT(BPF_LD
| BPF_W
| BPF_IND
, 0),
2019 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 0xAAAA0300, 0, FAIL
),
2020 BPF_STMT(BPF_LD
| BPF_W
| BPF_IND
, 4),
2021 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 0x0000888E, PASS
, FAIL
),
2023 /* keep these last two statements or change the code below */
2024 /* return 0 == "DROP" */
2025 BPF_STMT(BPF_RET
| BPF_K
, 0),
2026 /* return ~0 == "keep all" */
2027 BPF_STMT(BPF_RET
| BPF_K
, ~0),
2030 static struct sock_fprog msock_filter
= {
2031 .len
= sizeof(msock_filter_insns
)/sizeof(msock_filter_insns
[0]),
2032 .filter
= msock_filter_insns
,
2036 static int add_monitor_filter(int s
)
2040 /* rewrite all PASS/FAIL jump offsets */
2041 for (idx
= 0; idx
< msock_filter
.len
; idx
++) {
2042 struct sock_filter
*insn
= &msock_filter_insns
[idx
];
2044 if (BPF_CLASS(insn
->code
) == BPF_JMP
) {
2045 if (insn
->code
== (BPF_JMP
|BPF_JA
)) {
2046 if (insn
->k
== PASS
)
2047 insn
->k
= msock_filter
.len
- idx
- 2;
2048 else if (insn
->k
== FAIL
)
2049 insn
->k
= msock_filter
.len
- idx
- 3;
2052 if (insn
->jt
== PASS
)
2053 insn
->jt
= msock_filter
.len
- idx
- 2;
2054 else if (insn
->jt
== FAIL
)
2055 insn
->jt
= msock_filter
.len
- idx
- 3;
2057 if (insn
->jf
== PASS
)
2058 insn
->jf
= msock_filter
.len
- idx
- 2;
2059 else if (insn
->jf
== FAIL
)
2060 insn
->jf
= msock_filter
.len
- idx
- 3;
2064 if (setsockopt(s
, SOL_SOCKET
, SO_ATTACH_FILTER
,
2065 &msock_filter
, sizeof(msock_filter
))) {
2066 perror("SO_ATTACH_FILTER");
2074 static int nl80211_create_monitor_interface(struct i802_driver_data
*drv
)
2077 struct sockaddr_ll ll
;
2081 snprintf(buf
, IFNAMSIZ
, "mon.%s", drv
->iface
);
2082 buf
[IFNAMSIZ
- 1] = '\0';
2084 drv
->monitor_ifidx
=
2085 nl80211_create_iface(drv
, buf
, NL80211_IFTYPE_MONITOR
, NULL
);
2087 if (drv
->monitor_ifidx
< 0)
2090 if (hostapd_set_iface_flags(drv
, buf
, 1))
2093 memset(&ll
, 0, sizeof(ll
));
2094 ll
.sll_family
= AF_PACKET
;
2095 ll
.sll_ifindex
= drv
->monitor_ifidx
;
2096 drv
->monitor_sock
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
));
2097 if (drv
->monitor_sock
< 0) {
2098 perror("socket[PF_PACKET,SOCK_RAW]");
2102 if (add_monitor_filter(drv
->monitor_sock
)) {
2103 wpa_printf(MSG_INFO
, "Failed to set socket filter for monitor "
2104 "interface; do filtering in user space");
2105 /* This works, but will cost in performance. */
2108 if (bind(drv
->monitor_sock
, (struct sockaddr
*) &ll
,
2110 perror("monitor socket bind");
2114 optlen
= sizeof(optval
);
2117 (drv
->monitor_sock
, SOL_SOCKET
, SO_PRIORITY
, &optval
, optlen
)) {
2118 perror("Failed to set socket priority");
2122 if (eloop_register_read_sock(drv
->monitor_sock
, handle_monitor_read
,
2124 printf("Could not register monitor read socket\n");
2130 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
2135 static int nl80211_set_master_mode(struct i802_driver_data
*drv
,
2141 msg
= nlmsg_alloc();
2145 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2146 0, NL80211_CMD_SET_INTERFACE
, 0);
2147 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
2148 if_nametoindex(ifname
));
2149 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_AP
);
2151 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2155 wpa_printf(MSG_ERROR
, "Failed to set interface %s to master "
2161 static int i802_init_sockets(struct i802_driver_data
*drv
, const u8
*bssid
)
2164 struct sockaddr_ll addr
;
2166 drv
->ioctl_sock
= socket(PF_INET
, SOCK_DGRAM
, 0);
2167 if (drv
->ioctl_sock
< 0) {
2168 perror("socket[PF_INET,SOCK_DGRAM]");
2172 /* start listening for EAPOL on the default AP interface */
2173 add_ifidx(drv
, if_nametoindex(drv
->iface
));
2175 if (hostapd_set_iface_flags(drv
, drv
->iface
, 0))
2179 os_strlcpy(ifr
.ifr_name
, drv
->iface
, IFNAMSIZ
);
2180 memcpy(ifr
.ifr_hwaddr
.sa_data
, bssid
, ETH_ALEN
);
2181 ifr
.ifr_hwaddr
.sa_family
= ARPHRD_ETHER
;
2183 if (ioctl(drv
->ioctl_sock
, SIOCSIFHWADDR
, &ifr
)) {
2184 perror("ioctl(SIOCSIFHWADDR)");
2190 * initialise generic netlink and nl80211
2192 drv
->nl_cb
= nl_cb_alloc(NL_CB_DEFAULT
);
2194 printf("Failed to allocate netlink callbacks.\n");
2198 drv
->nl_handle
= nl_handle_alloc_cb(drv
->nl_cb
);
2199 if (!drv
->nl_handle
) {
2200 printf("Failed to allocate netlink handle.\n");
2204 if (genl_connect(drv
->nl_handle
)) {
2205 printf("Failed to connect to generic netlink.\n");
2209 #ifdef CONFIG_LIBNL20
2210 if (genl_ctrl_alloc_cache(drv
->nl_handle
, &drv
->nl_cache
) < 0) {
2211 printf("Failed to allocate generic netlink cache.\n");
2214 #else /* CONFIG_LIBNL20 */
2215 drv
->nl_cache
= genl_ctrl_alloc_cache(drv
->nl_handle
);
2216 if (!drv
->nl_cache
) {
2217 printf("Failed to allocate generic netlink cache.\n");
2220 #endif /* CONFIG_LIBNL20 */
2222 drv
->nl80211
= genl_ctrl_search_by_name(drv
->nl_cache
, "nl80211");
2223 if (!drv
->nl80211
) {
2224 printf("nl80211 not found.\n");
2228 /* Initialise a monitor interface */
2229 if (nl80211_create_monitor_interface(drv
))
2232 if (nl80211_set_master_mode(drv
, drv
->iface
))
2235 if (hostapd_set_iface_flags(drv
, drv
->iface
, 1))
2238 memset(&addr
, 0, sizeof(addr
));
2239 addr
.sll_family
= AF_PACKET
;
2240 addr
.sll_ifindex
= ifr
.ifr_ifindex
;
2241 wpa_printf(MSG_DEBUG
, "Opening raw packet socket for ifindex %d",
2244 drv
->eapol_sock
= socket(PF_PACKET
, SOCK_DGRAM
, htons(ETH_P_PAE
));
2245 if (drv
->eapol_sock
< 0) {
2246 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
2250 if (eloop_register_read_sock(drv
->eapol_sock
, handle_eapol
, drv
, NULL
))
2252 printf("Could not register read socket for eapol\n");
2256 memset(&ifr
, 0, sizeof(ifr
));
2257 os_strlcpy(ifr
.ifr_name
, drv
->iface
, sizeof(ifr
.ifr_name
));
2258 if (ioctl(drv
->ioctl_sock
, SIOCGIFHWADDR
, &ifr
) != 0) {
2259 perror("ioctl(SIOCGIFHWADDR)");
2263 if (ifr
.ifr_hwaddr
.sa_family
!= ARPHRD_ETHER
) {
2264 printf("Invalid HW-addr family 0x%04x\n",
2265 ifr
.ifr_hwaddr
.sa_family
);
2268 memcpy(drv
->hapd
->own_addr
, ifr
.ifr_hwaddr
.sa_data
, ETH_ALEN
);
2273 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
2278 static int i802_get_inact_sec(void *priv
, const u8
*addr
)
2280 struct hostap_sta_driver_data data
;
2283 data
.inactive_msec
= (unsigned long) -1;
2284 ret
= i802_read_sta_data(priv
, &data
, addr
);
2285 if (ret
|| data
.inactive_msec
== (unsigned long) -1)
2287 return data
.inactive_msec
/ 1000;
2291 static int i802_sta_clear_stats(void *priv
, const u8
*addr
)
2301 hostapd_wireless_event_wireless_custom(struct i802_driver_data
*drv
,
2304 wpa_printf(MSG_DEBUG
, "Custom wireless event: '%s'", custom
);
2306 if (strncmp(custom
, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
2309 pos
= strstr(custom
, "addr=");
2311 wpa_printf(MSG_DEBUG
,
2312 "MLME-MICHAELMICFAILURE.indication "
2313 "without sender address ignored");
2317 if (hwaddr_aton(pos
, addr
) == 0) {
2318 ieee80211_michael_mic_failure(drv
->hapd
, addr
, 1);
2320 wpa_printf(MSG_DEBUG
,
2321 "MLME-MICHAELMICFAILURE.indication "
2322 "with invalid MAC address");
2328 static void hostapd_wireless_event_wireless(struct i802_driver_data
*drv
,
2329 char *data
, int len
)
2331 struct iw_event iwe_buf
, *iwe
= &iwe_buf
;
2332 char *pos
, *end
, *custom
, *buf
;
2337 while (pos
+ IW_EV_LCP_LEN
<= end
) {
2338 /* Event data may be unaligned, so make a local, aligned copy
2339 * before processing. */
2340 memcpy(&iwe_buf
, pos
, IW_EV_LCP_LEN
);
2341 wpa_printf(MSG_DEBUG
, "Wireless event: cmd=0x%x len=%d",
2342 iwe
->cmd
, iwe
->len
);
2343 if (iwe
->len
<= IW_EV_LCP_LEN
)
2346 custom
= pos
+ IW_EV_POINT_LEN
;
2347 if (drv
->we_version
> 18 &&
2348 (iwe
->cmd
== IWEVMICHAELMICFAILURE
||
2349 iwe
->cmd
== IWEVCUSTOM
)) {
2350 /* WE-19 removed the pointer from struct iw_point */
2351 char *dpos
= (char *) &iwe_buf
.u
.data
.length
;
2352 int dlen
= dpos
- (char *) &iwe_buf
;
2353 memcpy(dpos
, pos
+ IW_EV_LCP_LEN
,
2354 sizeof(struct iw_event
) - dlen
);
2356 memcpy(&iwe_buf
, pos
, sizeof(struct iw_event
));
2357 custom
+= IW_EV_POINT_OFF
;
2362 if (custom
+ iwe
->u
.data
.length
> end
)
2364 buf
= malloc(iwe
->u
.data
.length
+ 1);
2367 memcpy(buf
, custom
, iwe
->u
.data
.length
);
2368 buf
[iwe
->u
.data
.length
] = '\0';
2369 hostapd_wireless_event_wireless_custom(drv
, buf
);
2379 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data
*drv
,
2380 struct nlmsghdr
*h
, int len
)
2382 struct ifinfomsg
*ifi
;
2383 int attrlen
, _nlmsg_len
, rta_len
;
2384 struct rtattr
*attr
;
2386 if (len
< (int) sizeof(*ifi
))
2389 ifi
= NLMSG_DATA(h
);
2391 /* TODO: use ifi->ifi_index to filter out wireless events from other
2394 _nlmsg_len
= NLMSG_ALIGN(sizeof(struct ifinfomsg
));
2396 attrlen
= h
->nlmsg_len
- _nlmsg_len
;
2400 attr
= (struct rtattr
*) (((char *) ifi
) + _nlmsg_len
);
2402 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
2403 while (RTA_OK(attr
, attrlen
)) {
2404 if (attr
->rta_type
== IFLA_WIRELESS
) {
2405 hostapd_wireless_event_wireless(
2406 drv
, ((char *) attr
) + rta_len
,
2407 attr
->rta_len
- rta_len
);
2409 attr
= RTA_NEXT(attr
, attrlen
);
2414 static void hostapd_wireless_event_receive(int sock
, void *eloop_ctx
,
2419 struct sockaddr_nl from
;
2422 struct i802_driver_data
*drv
= eloop_ctx
;
2424 fromlen
= sizeof(from
);
2425 left
= recvfrom(sock
, buf
, sizeof(buf
), MSG_DONTWAIT
,
2426 (struct sockaddr
*) &from
, &fromlen
);
2428 if (errno
!= EINTR
&& errno
!= EAGAIN
)
2429 perror("recvfrom(netlink)");
2433 h
= (struct nlmsghdr
*) buf
;
2434 while (left
>= (int) sizeof(*h
)) {
2438 plen
= len
- sizeof(*h
);
2439 if (len
> left
|| plen
< 0) {
2440 printf("Malformed netlink message: "
2441 "len=%d left=%d plen=%d\n",
2446 switch (h
->nlmsg_type
) {
2448 hostapd_wireless_event_rtm_newlink(drv
, h
, plen
);
2452 len
= NLMSG_ALIGN(len
);
2454 h
= (struct nlmsghdr
*) ((char *) h
+ len
);
2458 printf("%d extra bytes in the end of netlink message\n", left
);
2463 static int hostap_get_we_version(struct i802_driver_data
*drv
)
2465 struct iw_range
*range
;
2470 drv
->we_version
= 0;
2473 * Use larger buffer than struct iw_range in order to allow the
2474 * structure to grow in the future.
2476 buflen
= sizeof(struct iw_range
) + 500;
2477 range
= os_zalloc(buflen
);
2481 memset(&iwr
, 0, sizeof(iwr
));
2482 os_strlcpy(iwr
.ifr_name
, drv
->iface
, IFNAMSIZ
);
2483 iwr
.u
.data
.pointer
= (caddr_t
) range
;
2484 iwr
.u
.data
.length
= buflen
;
2486 minlen
= ((char *) &range
->enc_capa
) - (char *) range
+
2487 sizeof(range
->enc_capa
);
2489 if (ioctl(drv
->ioctl_sock
, SIOCGIWRANGE
, &iwr
) < 0) {
2490 perror("ioctl[SIOCGIWRANGE]");
2493 } else if (iwr
.u
.data
.length
>= minlen
&&
2494 range
->we_version_compiled
>= 18) {
2495 wpa_printf(MSG_DEBUG
, "SIOCGIWRANGE: WE(compiled)=%d "
2496 "WE(source)=%d enc_capa=0x%x",
2497 range
->we_version_compiled
,
2498 range
->we_version_source
,
2500 drv
->we_version
= range
->we_version_compiled
;
2508 static int i802_wireless_event_init(void *priv
)
2510 struct i802_driver_data
*drv
= priv
;
2512 struct sockaddr_nl local
;
2514 hostap_get_we_version(drv
);
2516 drv
->wext_sock
= -1;
2518 s
= socket(PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
2520 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2524 memset(&local
, 0, sizeof(local
));
2525 local
.nl_family
= AF_NETLINK
;
2526 local
.nl_groups
= RTMGRP_LINK
;
2527 if (bind(s
, (struct sockaddr
*) &local
, sizeof(local
)) < 0) {
2528 perror("bind(netlink)");
2533 eloop_register_read_sock(s
, hostapd_wireless_event_receive
, drv
,
2541 static void i802_wireless_event_deinit(void *priv
)
2543 struct i802_driver_data
*drv
= priv
;
2544 if (drv
->wext_sock
< 0)
2546 eloop_unregister_read_sock(drv
->wext_sock
);
2547 close(drv
->wext_sock
);
2551 static int i802_sta_deauth(void *priv
, const u8
*addr
, int reason
)
2553 struct i802_driver_data
*drv
= priv
;
2554 struct ieee80211_mgmt mgmt
;
2556 memset(&mgmt
, 0, sizeof(mgmt
));
2557 mgmt
.frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
2558 WLAN_FC_STYPE_DEAUTH
);
2559 memcpy(mgmt
.da
, addr
, ETH_ALEN
);
2560 memcpy(mgmt
.sa
, drv
->hapd
->own_addr
, ETH_ALEN
);
2561 memcpy(mgmt
.bssid
, drv
->hapd
->own_addr
, ETH_ALEN
);
2562 mgmt
.u
.deauth
.reason_code
= host_to_le16(reason
);
2563 return i802_send_mgmt_frame(drv
, &mgmt
, IEEE80211_HDRLEN
+
2564 sizeof(mgmt
.u
.deauth
), 0);
2568 static int i802_sta_disassoc(void *priv
, const u8
*addr
, int reason
)
2570 struct i802_driver_data
*drv
= priv
;
2571 struct ieee80211_mgmt mgmt
;
2573 memset(&mgmt
, 0, sizeof(mgmt
));
2574 mgmt
.frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
2575 WLAN_FC_STYPE_DISASSOC
);
2576 memcpy(mgmt
.da
, addr
, ETH_ALEN
);
2577 memcpy(mgmt
.sa
, drv
->hapd
->own_addr
, ETH_ALEN
);
2578 memcpy(mgmt
.bssid
, drv
->hapd
->own_addr
, ETH_ALEN
);
2579 mgmt
.u
.disassoc
.reason_code
= host_to_le16(reason
);
2580 return i802_send_mgmt_frame(drv
, &mgmt
, IEEE80211_HDRLEN
+
2581 sizeof(mgmt
.u
.disassoc
), 0);
2585 static void *i802_init_bssid(struct hostapd_data
*hapd
, const u8
*bssid
)
2587 struct i802_driver_data
*drv
;
2589 drv
= os_zalloc(sizeof(struct i802_driver_data
));
2591 printf("Could not allocate memory for i802 driver data\n");
2596 memcpy(drv
->iface
, hapd
->conf
->iface
, sizeof(drv
->iface
));
2598 drv
->num_if_indices
= sizeof(drv
->default_if_indices
) / sizeof(int);
2599 drv
->if_indices
= drv
->default_if_indices
;
2600 drv
->bridge
= if_nametoindex(hapd
->conf
->bridge
);
2602 if (i802_init_sockets(drv
, bssid
))
2613 static void *i802_init(struct hostapd_data
*hapd
)
2615 return i802_init_bssid(hapd
, NULL
);
2619 static void i802_deinit(void *priv
)
2621 struct i802_driver_data
*drv
= priv
;
2623 if (drv
->last_freq_ht
) {
2624 /* Clear HT flags from the driver */
2625 struct hostapd_freq_params freq
;
2626 os_memset(&freq
, 0, sizeof(freq
));
2627 freq
.freq
= drv
->last_freq
;
2628 i802_set_freq2(priv
, &freq
);
2631 i802_del_beacon(drv
);
2633 /* remove monitor interface */
2634 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
2636 (void) hostapd_set_iface_flags(drv
, drv
->iface
, 0);
2638 if (drv
->monitor_sock
>= 0) {
2639 eloop_unregister_read_sock(drv
->monitor_sock
);
2640 close(drv
->monitor_sock
);
2642 if (drv
->ioctl_sock
>= 0)
2643 close(drv
->ioctl_sock
);
2644 if (drv
->eapol_sock
>= 0) {
2645 eloop_unregister_read_sock(drv
->eapol_sock
);
2646 close(drv
->eapol_sock
);
2649 genl_family_put(drv
->nl80211
);
2650 nl_cache_free(drv
->nl_cache
);
2651 nl_handle_destroy(drv
->nl_handle
);
2652 nl_cb_put(drv
->nl_cb
);
2654 if (drv
->if_indices
!= drv
->default_if_indices
)
2655 free(drv
->if_indices
);
2661 const struct wpa_driver_ops wpa_driver_nl80211_ops
= {
2664 .init_bssid
= i802_init_bssid
,
2665 .deinit
= i802_deinit
,
2666 .wireless_event_init
= i802_wireless_event_init
,
2667 .wireless_event_deinit
= i802_wireless_event_deinit
,
2668 .set_ieee8021x
= i802_set_ieee8021x
,
2669 .set_privacy
= i802_set_privacy
,
2670 .set_encryption
= i802_set_encryption
,
2671 .get_seqnum
= i802_get_seqnum
,
2672 .flush
= i802_flush
,
2673 .read_sta_data
= i802_read_sta_data
,
2674 .send_eapol
= i802_send_eapol
,
2675 .sta_set_flags
= i802_sta_set_flags
,
2676 .sta_deauth
= i802_sta_deauth
,
2677 .sta_disassoc
= i802_sta_disassoc
,
2678 .sta_remove
= i802_sta_remove
,
2679 .send_mgmt_frame
= i802_send_mgmt_frame
,
2680 .sta_add2
= i802_sta_add2
,
2681 .get_inact_sec
= i802_get_inact_sec
,
2682 .sta_clear_stats
= i802_sta_clear_stats
,
2683 .set_freq2
= i802_set_freq2
,
2684 .set_rts
= i802_set_rts
,
2685 .get_rts
= i802_get_rts
,
2686 .set_frag
= i802_set_frag
,
2687 .get_frag
= i802_get_frag
,
2688 .set_retry
= i802_set_retry
,
2689 .get_retry
= i802_get_retry
,
2690 .set_rate_sets
= i802_set_rate_sets
,
2691 .set_regulatory_domain
= i802_set_regulatory_domain
,
2692 .set_beacon
= i802_set_beacon
,
2693 .set_internal_bridge
= i802_set_internal_bridge
,
2694 .set_beacon_int
= i802_set_beacon_int
,
2695 .set_dtim_period
= i802_set_dtim_period
,
2696 .set_cts_protect
= i802_set_cts_protect
,
2697 .set_preamble
= i802_set_preamble
,
2698 .set_short_slot_time
= i802_set_short_slot_time
,
2699 .set_tx_queue_params
= i802_set_tx_queue_params
,
2700 .bss_add
= i802_bss_add
,
2701 .bss_remove
= i802_bss_remove
,
2702 .if_add
= i802_if_add
,
2703 .if_update
= i802_if_update
,
2704 .if_remove
= i802_if_remove
,
2705 .get_hw_feature_data
= i802_get_hw_feature_data
,
2706 .set_sta_vlan
= i802_set_sta_vlan
,
2707 .set_country
= i802_set_country
,