2 * Driver interaction with Linux nl80211/cfg80211
3 * Copyright (c) 2002-2010, 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>
7 * Copyright (c) 2009, Atheros Communications
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * Alternatively, this software may be distributed under the terms of BSD
16 * See README and COPYING for more details.
20 #include <sys/ioctl.h>
22 #include <netlink/genl/genl.h>
23 #include <netlink/genl/family.h>
24 #include <netlink/genl/ctrl.h>
25 #include <netpacket/packet.h>
26 #include <linux/filter.h>
27 #include "nl80211_copy.h"
31 #include "common/ieee802_11_defs.h"
33 #include "linux_ioctl.h"
35 #include "radiotap_iter.h"
39 /* libnl 2.0 compatibility code */
40 #define nl_handle nl_sock
41 #define nl_handle_alloc_cb nl_socket_alloc_cb
42 #define nl_handle_destroy nl_socket_free
43 #endif /* CONFIG_LIBNL20 */
47 #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
50 #define IFF_DORMANT 0x20000 /* driver signals dormant */
53 #ifndef IF_OPER_DORMANT
54 #define IF_OPER_DORMANT 5
61 struct i802_bss
*next
;
63 unsigned int beacon_set
:1;
66 struct wpa_driver_nl80211_data
{
68 struct netlink_data
*netlink
;
69 int ioctl_sock
; /* socket for ioctl() use */
70 char ifname
[IFNAMSIZ
+ 1];
73 struct wpa_driver_capa capa
;
78 int scan_complete_events
;
80 struct nl_handle
*nl_handle
;
81 struct nl_handle
*nl_handle_event
;
82 struct nl_cache
*nl_cache
;
83 struct nl_cache
*nl_cache_event
;
85 struct genl_family
*nl80211
;
87 u8 auth_bssid
[ETH_ALEN
];
93 int ap_scan_as_station
;
94 unsigned int assoc_freq
;
100 unsigned int beacon_set
:1;
101 unsigned int pending_remain_on_chan
:1;
103 u64 remain_on_chan_cookie
;
106 int eapol_sock
; /* socket for EAPOL frames */
108 int default_if_indices
[16];
120 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx
,
122 static int wpa_driver_nl80211_set_mode(void *priv
, int mode
);
124 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data
*drv
);
125 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data
*drv
,
126 const u8
*addr
, int cmd
, u16 reason_code
);
127 static void nl80211_remove_monitor_interface(
128 struct wpa_driver_nl80211_data
*drv
);
131 static void add_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
);
132 static void del_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
);
133 static struct i802_bss
* get_bss(struct wpa_driver_nl80211_data
*drv
,
135 static int i802_set_freq(void *priv
, struct hostapd_freq_params
*freq
);
136 static int wpa_driver_nl80211_if_remove(void *priv
,
137 enum wpa_driver_if_type type
,
141 static void wpa_driver_nl80211_probe_req_report_timeout(void *eloop_ctx
,
146 static int ack_handler(struct nl_msg
*msg
, void *arg
)
153 static int finish_handler(struct nl_msg
*msg
, void *arg
)
160 static int error_handler(struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
169 static int no_seq_check(struct nl_msg
*msg
, void *arg
)
175 static int send_and_recv_msgs(struct wpa_driver_nl80211_data
*drv
,
177 int (*valid_handler
)(struct nl_msg
*, void *),
183 cb
= nl_cb_clone(drv
->nl_cb
);
187 err
= nl_send_auto_complete(drv
->nl_handle
, msg
);
193 nl_cb_err(cb
, NL_CB_CUSTOM
, error_handler
, &err
);
194 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, finish_handler
, &err
);
195 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, ack_handler
, &err
);
198 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
,
199 valid_handler
, valid_data
);
202 nl_recvmsgs(drv
->nl_handle
, cb
);
216 static int family_handler(struct nl_msg
*msg
, void *arg
)
218 struct family_data
*res
= arg
;
219 struct nlattr
*tb
[CTRL_ATTR_MAX
+ 1];
220 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
221 struct nlattr
*mcgrp
;
224 nla_parse(tb
, CTRL_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
225 genlmsg_attrlen(gnlh
, 0), NULL
);
226 if (!tb
[CTRL_ATTR_MCAST_GROUPS
])
229 nla_for_each_nested(mcgrp
, tb
[CTRL_ATTR_MCAST_GROUPS
], i
) {
230 struct nlattr
*tb2
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
231 nla_parse(tb2
, CTRL_ATTR_MCAST_GRP_MAX
, nla_data(mcgrp
),
232 nla_len(mcgrp
), NULL
);
233 if (!tb2
[CTRL_ATTR_MCAST_GRP_NAME
] ||
234 !tb2
[CTRL_ATTR_MCAST_GRP_ID
] ||
235 os_strncmp(nla_data(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]),
237 nla_len(tb2
[CTRL_ATTR_MCAST_GRP_NAME
])) != 0)
239 res
->id
= nla_get_u32(tb2
[CTRL_ATTR_MCAST_GRP_ID
]);
247 static int nl_get_multicast_id(struct wpa_driver_nl80211_data
*drv
,
248 const char *family
, const char *group
)
252 struct family_data res
= { group
, -ENOENT
};
257 genlmsg_put(msg
, 0, 0, genl_ctrl_resolve(drv
->nl_handle
, "nlctrl"),
258 0, 0, CTRL_CMD_GETFAMILY
, 0);
259 NLA_PUT_STRING(msg
, CTRL_ATTR_FAMILY_NAME
, family
);
261 ret
= send_and_recv_msgs(drv
, msg
, family_handler
, &res
);
272 static int wpa_driver_nl80211_get_bssid(void *priv
, u8
*bssid
)
274 struct wpa_driver_nl80211_data
*drv
= priv
;
275 if (!drv
->associated
)
277 os_memcpy(bssid
, drv
->bssid
, ETH_ALEN
);
282 static int wpa_driver_nl80211_get_ssid(void *priv
, u8
*ssid
)
284 struct wpa_driver_nl80211_data
*drv
= priv
;
285 if (!drv
->associated
)
287 os_memcpy(ssid
, drv
->ssid
, drv
->ssid_len
);
288 return drv
->ssid_len
;
292 static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data
*drv
,
293 char *buf
, size_t len
, int del
)
295 union wpa_event_data event
;
297 os_memset(&event
, 0, sizeof(event
));
298 if (len
> sizeof(event
.interface_status
.ifname
))
299 len
= sizeof(event
.interface_status
.ifname
) - 1;
300 os_memcpy(event
.interface_status
.ifname
, buf
, len
);
301 event
.interface_status
.ievent
= del
? EVENT_INTERFACE_REMOVED
:
302 EVENT_INTERFACE_ADDED
;
304 wpa_printf(MSG_DEBUG
, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
306 event
.interface_status
.ifname
,
307 del
? "removed" : "added");
309 if (os_strcmp(drv
->ifname
, event
.interface_status
.ifname
) == 0) {
316 wpa_supplicant_event(drv
->ctx
, EVENT_INTERFACE_STATUS
, &event
);
320 static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data
*drv
,
323 int attrlen
, rta_len
;
327 attr
= (struct rtattr
*) buf
;
329 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
330 while (RTA_OK(attr
, attrlen
)) {
331 if (attr
->rta_type
== IFLA_IFNAME
) {
332 if (os_strcmp(((char *) attr
) + rta_len
, drv
->ifname
)
338 attr
= RTA_NEXT(attr
, attrlen
);
345 static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data
*drv
,
346 int ifindex
, u8
*buf
, size_t len
)
348 if (drv
->ifindex
== ifindex
)
351 if (drv
->if_removed
&& wpa_driver_nl80211_own_ifname(drv
, buf
, len
)) {
352 drv
->ifindex
= if_nametoindex(drv
->ifname
);
353 wpa_printf(MSG_DEBUG
, "nl80211: Update ifindex for a removed "
355 wpa_driver_nl80211_finish_drv_init(drv
);
363 static void wpa_driver_nl80211_event_rtm_newlink(void *ctx
,
364 struct ifinfomsg
*ifi
,
367 struct wpa_driver_nl80211_data
*drv
= ctx
;
368 int attrlen
, rta_len
;
371 if (!wpa_driver_nl80211_own_ifindex(drv
, ifi
->ifi_index
, buf
, len
)) {
372 wpa_printf(MSG_DEBUG
, "Ignore event for foreign ifindex %d",
377 wpa_printf(MSG_DEBUG
, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
379 drv
->operstate
, ifi
->ifi_flags
,
380 (ifi
->ifi_flags
& IFF_UP
) ? "[UP]" : "",
381 (ifi
->ifi_flags
& IFF_RUNNING
) ? "[RUNNING]" : "",
382 (ifi
->ifi_flags
& IFF_LOWER_UP
) ? "[LOWER_UP]" : "",
383 (ifi
->ifi_flags
& IFF_DORMANT
) ? "[DORMANT]" : "");
385 * Some drivers send the association event before the operup event--in
386 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
387 * fails. This will hit us when wpa_supplicant does not need to do
388 * IEEE 802.1X authentication
390 if (drv
->operstate
== 1 &&
391 (ifi
->ifi_flags
& (IFF_LOWER_UP
| IFF_DORMANT
)) == IFF_LOWER_UP
&&
392 !(ifi
->ifi_flags
& IFF_RUNNING
))
393 netlink_send_oper_ifla(drv
->netlink
, drv
->ifindex
,
397 attr
= (struct rtattr
*) buf
;
398 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
399 while (RTA_OK(attr
, attrlen
)) {
400 if (attr
->rta_type
== IFLA_IFNAME
) {
401 wpa_driver_nl80211_event_link(
403 ((char *) attr
) + rta_len
,
404 attr
->rta_len
- rta_len
, 0);
406 attr
= RTA_NEXT(attr
, attrlen
);
411 static void wpa_driver_nl80211_event_rtm_dellink(void *ctx
,
412 struct ifinfomsg
*ifi
,
415 struct wpa_driver_nl80211_data
*drv
= ctx
;
416 int attrlen
, rta_len
;
420 attr
= (struct rtattr
*) buf
;
422 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
423 while (RTA_OK(attr
, attrlen
)) {
424 if (attr
->rta_type
== IFLA_IFNAME
) {
425 wpa_driver_nl80211_event_link(
427 ((char *) attr
) + rta_len
,
428 attr
->rta_len
- rta_len
, 1);
430 attr
= RTA_NEXT(attr
, attrlen
);
435 static void mlme_event_auth(struct wpa_driver_nl80211_data
*drv
,
436 const u8
*frame
, size_t len
)
438 const struct ieee80211_mgmt
*mgmt
;
439 union wpa_event_data event
;
441 mgmt
= (const struct ieee80211_mgmt
*) frame
;
442 if (len
< 24 + sizeof(mgmt
->u
.auth
)) {
443 wpa_printf(MSG_DEBUG
, "nl80211: Too short association event "
448 os_memcpy(drv
->auth_bssid
, mgmt
->sa
, ETH_ALEN
);
449 os_memset(&event
, 0, sizeof(event
));
450 os_memcpy(event
.auth
.peer
, mgmt
->sa
, ETH_ALEN
);
451 event
.auth
.auth_type
= le_to_host16(mgmt
->u
.auth
.auth_alg
);
452 event
.auth
.status_code
= le_to_host16(mgmt
->u
.auth
.status_code
);
453 if (len
> 24 + sizeof(mgmt
->u
.auth
)) {
454 event
.auth
.ies
= mgmt
->u
.auth
.variable
;
455 event
.auth
.ies_len
= len
- 24 - sizeof(mgmt
->u
.auth
);
458 wpa_supplicant_event(drv
->ctx
, EVENT_AUTH
, &event
);
462 static void mlme_event_assoc(struct wpa_driver_nl80211_data
*drv
,
463 const u8
*frame
, size_t len
)
465 const struct ieee80211_mgmt
*mgmt
;
466 union wpa_event_data event
;
469 mgmt
= (const struct ieee80211_mgmt
*) frame
;
470 if (len
< 24 + sizeof(mgmt
->u
.assoc_resp
)) {
471 wpa_printf(MSG_DEBUG
, "nl80211: Too short association event "
476 status
= le_to_host16(mgmt
->u
.assoc_resp
.status_code
);
477 if (status
!= WLAN_STATUS_SUCCESS
) {
478 os_memset(&event
, 0, sizeof(event
));
479 if (len
> 24 + sizeof(mgmt
->u
.assoc_resp
)) {
480 event
.assoc_reject
.resp_ies
=
481 (u8
*) mgmt
->u
.assoc_resp
.variable
;
482 event
.assoc_reject
.resp_ies_len
=
483 len
- 24 - sizeof(mgmt
->u
.assoc_resp
);
485 event
.assoc_reject
.status_code
= status
;
487 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC_REJECT
, &event
);
492 os_memcpy(drv
->bssid
, mgmt
->sa
, ETH_ALEN
);
494 os_memset(&event
, 0, sizeof(event
));
495 if (len
> 24 + sizeof(mgmt
->u
.assoc_resp
)) {
496 event
.assoc_info
.resp_ies
= (u8
*) mgmt
->u
.assoc_resp
.variable
;
497 event
.assoc_info
.resp_ies_len
=
498 len
- 24 - sizeof(mgmt
->u
.assoc_resp
);
501 event
.assoc_info
.freq
= drv
->assoc_freq
;
503 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC
, &event
);
507 static void mlme_event_connect(struct wpa_driver_nl80211_data
*drv
,
508 enum nl80211_commands cmd
, struct nlattr
*status
,
509 struct nlattr
*addr
, struct nlattr
*req_ie
,
510 struct nlattr
*resp_ie
)
512 union wpa_event_data event
;
514 if (drv
->capa
.flags
& WPA_DRIVER_FLAGS_SME
) {
516 * Avoid reporting two association events that would confuse
519 wpa_printf(MSG_DEBUG
, "nl80211: Ignore connect event (cmd=%d) "
520 "when using userspace SME", cmd
);
524 os_memset(&event
, 0, sizeof(event
));
525 if (cmd
== NL80211_CMD_CONNECT
&&
526 nla_get_u16(status
) != WLAN_STATUS_SUCCESS
) {
528 event
.assoc_reject
.resp_ies
= nla_data(resp_ie
);
529 event
.assoc_reject
.resp_ies_len
= nla_len(resp_ie
);
531 event
.assoc_reject
.status_code
= nla_get_u16(status
);
532 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC_REJECT
, &event
);
538 os_memcpy(drv
->bssid
, nla_data(addr
), ETH_ALEN
);
541 event
.assoc_info
.req_ies
= nla_data(req_ie
);
542 event
.assoc_info
.req_ies_len
= nla_len(req_ie
);
545 event
.assoc_info
.resp_ies
= nla_data(resp_ie
);
546 event
.assoc_info
.resp_ies_len
= nla_len(resp_ie
);
549 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC
, &event
);
553 static void mlme_timeout_event(struct wpa_driver_nl80211_data
*drv
,
554 enum nl80211_commands cmd
, struct nlattr
*addr
)
556 union wpa_event_data event
;
557 enum wpa_event_type ev
;
559 if (nla_len(addr
) != ETH_ALEN
)
562 wpa_printf(MSG_DEBUG
, "nl80211: MLME event %d; timeout with " MACSTR
,
563 cmd
, MAC2STR((u8
*) nla_data(addr
)));
565 if (cmd
== NL80211_CMD_AUTHENTICATE
)
566 ev
= EVENT_AUTH_TIMED_OUT
;
567 else if (cmd
== NL80211_CMD_ASSOCIATE
)
568 ev
= EVENT_ASSOC_TIMED_OUT
;
572 os_memset(&event
, 0, sizeof(event
));
573 os_memcpy(event
.timeout_event
.addr
, nla_data(addr
), ETH_ALEN
);
574 wpa_supplicant_event(drv
->ctx
, ev
, &event
);
578 static void mlme_event(struct wpa_driver_nl80211_data
*drv
,
579 enum nl80211_commands cmd
, struct nlattr
*frame
,
580 struct nlattr
*addr
, struct nlattr
*timed_out
)
582 if (timed_out
&& addr
) {
583 mlme_timeout_event(drv
, cmd
, addr
);
588 wpa_printf(MSG_DEBUG
, "nl80211: MLME event %d without frame "
593 wpa_printf(MSG_DEBUG
, "nl80211: MLME event %d", cmd
);
594 wpa_hexdump(MSG_MSGDUMP
, "nl80211: MLME event frame",
595 nla_data(frame
), nla_len(frame
));
598 case NL80211_CMD_AUTHENTICATE
:
599 mlme_event_auth(drv
, nla_data(frame
), nla_len(frame
));
601 case NL80211_CMD_ASSOCIATE
:
602 mlme_event_assoc(drv
, nla_data(frame
), nla_len(frame
));
604 case NL80211_CMD_DEAUTHENTICATE
:
606 wpa_supplicant_event(drv
->ctx
, EVENT_DEAUTH
, NULL
);
608 case NL80211_CMD_DISASSOCIATE
:
610 wpa_supplicant_event(drv
->ctx
, EVENT_DISASSOC
, NULL
);
618 static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data
*drv
,
621 union wpa_event_data data
;
623 wpa_printf(MSG_DEBUG
, "nl80211: MLME event Michael MIC failure");
624 os_memset(&data
, 0, sizeof(data
));
625 if (tb
[NL80211_ATTR_MAC
]) {
626 wpa_hexdump(MSG_DEBUG
, "nl80211: Source MAC address",
627 nla_data(tb
[NL80211_ATTR_MAC
]),
628 nla_len(tb
[NL80211_ATTR_MAC
]));
629 data
.michael_mic_failure
.src
= nla_data(tb
[NL80211_ATTR_MAC
]);
631 if (tb
[NL80211_ATTR_KEY_SEQ
]) {
632 wpa_hexdump(MSG_DEBUG
, "nl80211: TSC",
633 nla_data(tb
[NL80211_ATTR_KEY_SEQ
]),
634 nla_len(tb
[NL80211_ATTR_KEY_SEQ
]));
636 if (tb
[NL80211_ATTR_KEY_TYPE
]) {
637 enum nl80211_key_type key_type
=
638 nla_get_u32(tb
[NL80211_ATTR_KEY_TYPE
]);
639 wpa_printf(MSG_DEBUG
, "nl80211: Key Type %d", key_type
);
640 if (key_type
== NL80211_KEYTYPE_PAIRWISE
)
641 data
.michael_mic_failure
.unicast
= 1;
643 data
.michael_mic_failure
.unicast
= 1;
645 if (tb
[NL80211_ATTR_KEY_IDX
]) {
646 u8 key_id
= nla_get_u8(tb
[NL80211_ATTR_KEY_IDX
]);
647 wpa_printf(MSG_DEBUG
, "nl80211: Key Id %d", key_id
);
650 wpa_supplicant_event(drv
->ctx
, EVENT_MICHAEL_MIC_FAILURE
, &data
);
654 static void mlme_event_join_ibss(struct wpa_driver_nl80211_data
*drv
,
657 if (tb
[NL80211_ATTR_MAC
] == NULL
) {
658 wpa_printf(MSG_DEBUG
, "nl80211: No address in IBSS joined "
662 os_memcpy(drv
->bssid
, nla_data(tb
[NL80211_ATTR_MAC
]), ETH_ALEN
);
664 wpa_printf(MSG_DEBUG
, "nl80211: IBSS " MACSTR
" joined",
665 MAC2STR(drv
->bssid
));
667 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC
, NULL
);
671 static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data
*drv
,
672 int cancel_event
, struct nlattr
*tb
[])
674 unsigned int freq
, chan_type
, duration
;
675 union wpa_event_data data
;
678 if (tb
[NL80211_ATTR_WIPHY_FREQ
])
679 freq
= nla_get_u32(tb
[NL80211_ATTR_WIPHY_FREQ
]);
683 if (tb
[NL80211_ATTR_WIPHY_CHANNEL_TYPE
])
684 chan_type
= nla_get_u32(tb
[NL80211_ATTR_WIPHY_CHANNEL_TYPE
]);
688 if (tb
[NL80211_ATTR_DURATION
])
689 duration
= nla_get_u32(tb
[NL80211_ATTR_DURATION
]);
693 if (tb
[NL80211_ATTR_COOKIE
])
694 cookie
= nla_get_u64(tb
[NL80211_ATTR_COOKIE
]);
698 wpa_printf(MSG_DEBUG
, "nl80211: Remain-on-channel event (cancel=%d "
699 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
700 cancel_event
, freq
, chan_type
, duration
,
701 (long long unsigned int) cookie
,
702 cookie
== drv
->remain_on_chan_cookie
? "match" : "unknown");
704 if (cookie
!= drv
->remain_on_chan_cookie
)
705 return; /* not for us */
707 drv
->pending_remain_on_chan
= !cancel_event
;
709 os_memset(&data
, 0, sizeof(data
));
710 data
.remain_on_channel
.freq
= freq
;
711 data
.remain_on_channel
.duration
= duration
;
712 wpa_supplicant_event(drv
->ctx
, cancel_event
?
713 EVENT_CANCEL_REMAIN_ON_CHANNEL
:
714 EVENT_REMAIN_ON_CHANNEL
, &data
);
718 static void send_scan_event(struct wpa_driver_nl80211_data
*drv
, int aborted
,
721 union wpa_event_data event
;
724 struct scan_info
*info
;
725 #define MAX_REPORT_FREQS 50
726 int freqs
[MAX_REPORT_FREQS
];
729 os_memset(&event
, 0, sizeof(event
));
730 info
= &event
.scan_info
;
731 info
->aborted
= aborted
;
733 if (tb
[NL80211_ATTR_SCAN_SSIDS
]) {
734 nla_for_each_nested(nl
, tb
[NL80211_ATTR_SCAN_SSIDS
], rem
) {
735 struct wpa_driver_scan_ssid
*s
=
736 &info
->ssids
[info
->num_ssids
];
737 s
->ssid
= nla_data(nl
);
738 s
->ssid_len
= nla_len(nl
);
740 if (info
->num_ssids
== WPAS_MAX_SCAN_SSIDS
)
744 if (tb
[NL80211_ATTR_SCAN_FREQUENCIES
]) {
745 nla_for_each_nested(nl
, tb
[NL80211_ATTR_SCAN_FREQUENCIES
], rem
)
747 freqs
[num_freqs
] = nla_get_u32(nl
);
749 if (num_freqs
== MAX_REPORT_FREQS
- 1)
753 info
->num_freqs
= num_freqs
;
755 wpa_supplicant_event(drv
->ctx
, EVENT_SCAN_RESULTS
, &event
);
759 static int process_event(struct nl_msg
*msg
, void *arg
)
761 struct wpa_driver_nl80211_data
*drv
= arg
;
762 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
763 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
765 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
766 genlmsg_attrlen(gnlh
, 0), NULL
);
768 if (tb
[NL80211_ATTR_IFINDEX
]) {
769 int ifindex
= nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]);
770 if (ifindex
!= drv
->ifindex
) {
771 wpa_printf(MSG_DEBUG
, "nl80211: Ignored event (cmd=%d)"
772 " for foreign interface (ifindex %d)",
778 if (drv
->ap_scan_as_station
&&
779 (gnlh
->cmd
== NL80211_CMD_NEW_SCAN_RESULTS
||
780 gnlh
->cmd
== NL80211_CMD_SCAN_ABORTED
)) {
781 wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_AP
);
782 drv
->ap_scan_as_station
= 0;
786 case NL80211_CMD_TRIGGER_SCAN
:
787 wpa_printf(MSG_DEBUG
, "nl80211: Scan trigger");
789 case NL80211_CMD_NEW_SCAN_RESULTS
:
790 wpa_printf(MSG_DEBUG
, "nl80211: New scan results available");
791 drv
->scan_complete_events
= 1;
792 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
,
794 send_scan_event(drv
, 0, tb
);
796 case NL80211_CMD_SCAN_ABORTED
:
797 wpa_printf(MSG_DEBUG
, "nl80211: Scan aborted");
799 * Need to indicate that scan results are available in order
800 * not to make wpa_supplicant stop its scanning.
802 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
,
804 send_scan_event(drv
, 1, tb
);
806 case NL80211_CMD_AUTHENTICATE
:
807 case NL80211_CMD_ASSOCIATE
:
808 case NL80211_CMD_DEAUTHENTICATE
:
809 case NL80211_CMD_DISASSOCIATE
:
810 mlme_event(drv
, gnlh
->cmd
, tb
[NL80211_ATTR_FRAME
],
811 tb
[NL80211_ATTR_MAC
], tb
[NL80211_ATTR_TIMED_OUT
]);
813 case NL80211_CMD_CONNECT
:
814 case NL80211_CMD_ROAM
:
815 mlme_event_connect(drv
, gnlh
->cmd
,
816 tb
[NL80211_ATTR_STATUS_CODE
],
817 tb
[NL80211_ATTR_MAC
],
818 tb
[NL80211_ATTR_REQ_IE
],
819 tb
[NL80211_ATTR_RESP_IE
]);
821 case NL80211_CMD_DISCONNECT
:
822 if (drv
->capa
.flags
& WPA_DRIVER_FLAGS_SME
) {
824 * Avoid reporting two disassociation events that could
825 * confuse the core code.
827 wpa_printf(MSG_DEBUG
, "nl80211: Ignore disconnect "
828 "event when using userspace SME");
832 wpa_supplicant_event(drv
->ctx
, EVENT_DISASSOC
, NULL
);
834 case NL80211_CMD_MICHAEL_MIC_FAILURE
:
835 mlme_event_michael_mic_failure(drv
, tb
);
837 case NL80211_CMD_JOIN_IBSS
:
838 mlme_event_join_ibss(drv
, tb
);
840 case NL80211_CMD_REMAIN_ON_CHANNEL
:
841 mlme_event_remain_on_channel(drv
, 0, tb
);
843 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL
:
844 mlme_event_remain_on_channel(drv
, 1, tb
);
847 wpa_printf(MSG_DEBUG
, "nl80211: Ignored unknown event "
848 "(cmd=%d)", gnlh
->cmd
);
856 static void wpa_driver_nl80211_event_receive(int sock
, void *eloop_ctx
,
860 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
862 wpa_printf(MSG_DEBUG
, "nl80211: Event message available");
864 cb
= nl_cb_clone(drv
->nl_cb
);
867 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, no_seq_check
, NULL
);
868 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, process_event
, drv
);
869 nl_recvmsgs(drv
->nl_handle_event
, cb
);
875 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
876 * @priv: driver_nl80211 private data
877 * @alpha2_arg: country to which to switch to
878 * Returns: 0 on success, -1 on failure
880 * This asks nl80211 to set the regulatory domain for given
881 * country ISO / IEC alpha2.
883 static int wpa_driver_nl80211_set_country(void *priv
, const char *alpha2_arg
)
885 struct wpa_driver_nl80211_data
*drv
= priv
;
893 alpha2
[0] = alpha2_arg
[0];
894 alpha2
[1] = alpha2_arg
[1];
897 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
898 0, NL80211_CMD_REQ_SET_REG
, 0);
900 NLA_PUT_STRING(msg
, NL80211_ATTR_REG_ALPHA2
, alpha2
);
901 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
))
910 struct wiphy_info_data
{
914 int connect_supported
;
918 static int wiphy_info_handler(struct nl_msg
*msg
, void *arg
)
920 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
921 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
922 struct wiphy_info_data
*info
= arg
;
924 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
925 genlmsg_attrlen(gnlh
, 0), NULL
);
927 if (tb
[NL80211_ATTR_MAX_NUM_SCAN_SSIDS
])
928 info
->max_scan_ssids
=
929 nla_get_u8(tb
[NL80211_ATTR_MAX_NUM_SCAN_SSIDS
]);
931 if (tb
[NL80211_ATTR_SUPPORTED_IFTYPES
]) {
932 struct nlattr
*nl_mode
;
934 nla_for_each_nested(nl_mode
,
935 tb
[NL80211_ATTR_SUPPORTED_IFTYPES
], i
) {
936 if (nl_mode
->nla_type
== NL80211_IFTYPE_AP
) {
937 info
->ap_supported
= 1;
943 if (tb
[NL80211_ATTR_SUPPORTED_COMMANDS
]) {
944 struct nlattr
*nl_cmd
;
947 nla_for_each_nested(nl_cmd
,
948 tb
[NL80211_ATTR_SUPPORTED_COMMANDS
], i
) {
949 u32 cmd
= nla_get_u32(nl_cmd
);
950 if (cmd
== NL80211_CMD_AUTHENTICATE
)
951 info
->auth_supported
= 1;
952 else if (cmd
== NL80211_CMD_CONNECT
)
953 info
->connect_supported
= 1;
961 static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data
*drv
,
962 struct wiphy_info_data
*info
)
966 os_memset(info
, 0, sizeof(*info
));
971 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
972 0, NL80211_CMD_GET_WIPHY
, 0);
974 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
976 if (send_and_recv_msgs(drv
, msg
, wiphy_info_handler
, info
) == 0)
985 static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data
*drv
)
987 struct wiphy_info_data info
;
988 if (wpa_driver_nl80211_get_info(drv
, &info
))
990 drv
->has_capability
= 1;
991 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
992 drv
->capa
.key_mgmt
= WPA_DRIVER_CAPA_KEY_MGMT_WPA
|
993 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK
|
994 WPA_DRIVER_CAPA_KEY_MGMT_WPA2
|
995 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK
;
996 drv
->capa
.enc
= WPA_DRIVER_CAPA_ENC_WEP40
|
997 WPA_DRIVER_CAPA_ENC_WEP104
|
998 WPA_DRIVER_CAPA_ENC_TKIP
|
999 WPA_DRIVER_CAPA_ENC_CCMP
;
1000 drv
->capa
.auth
= WPA_DRIVER_AUTH_OPEN
|
1001 WPA_DRIVER_AUTH_SHARED
|
1002 WPA_DRIVER_AUTH_LEAP
;
1004 drv
->capa
.max_scan_ssids
= info
.max_scan_ssids
;
1005 if (info
.ap_supported
)
1006 drv
->capa
.flags
|= WPA_DRIVER_FLAGS_AP
;
1008 if (info
.auth_supported
)
1009 drv
->capa
.flags
|= WPA_DRIVER_FLAGS_SME
;
1010 else if (!info
.connect_supported
) {
1011 wpa_printf(MSG_INFO
, "nl80211: Driver does not support "
1012 "authentication/association or connect commands");
1016 drv
->capa
.flags
|= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE
;
1020 #endif /* HOSTAPD */
1023 static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data
*drv
,
1028 /* Initialize generic netlink and nl80211 */
1030 drv
->nl_cb
= nl_cb_alloc(NL_CB_DEFAULT
);
1031 if (drv
->nl_cb
== NULL
) {
1032 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate netlink "
1037 drv
->nl_handle
= nl_handle_alloc_cb(drv
->nl_cb
);
1038 if (drv
->nl_handle
== NULL
) {
1039 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate netlink "
1044 drv
->nl_handle_event
= nl_handle_alloc_cb(drv
->nl_cb
);
1045 if (drv
->nl_handle_event
== NULL
) {
1046 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate netlink "
1047 "callbacks (event)");
1051 if (genl_connect(drv
->nl_handle
)) {
1052 wpa_printf(MSG_ERROR
, "nl80211: Failed to connect to generic "
1057 if (genl_connect(drv
->nl_handle_event
)) {
1058 wpa_printf(MSG_ERROR
, "nl80211: Failed to connect to generic "
1063 #ifdef CONFIG_LIBNL20
1064 if (genl_ctrl_alloc_cache(drv
->nl_handle
, &drv
->nl_cache
) < 0) {
1065 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate generic "
1069 if (genl_ctrl_alloc_cache(drv
->nl_handle_event
, &drv
->nl_cache_event
) <
1071 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate generic "
1072 "netlink cache (event)");
1075 #else /* CONFIG_LIBNL20 */
1076 drv
->nl_cache
= genl_ctrl_alloc_cache(drv
->nl_handle
);
1077 if (drv
->nl_cache
== NULL
) {
1078 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate generic "
1082 drv
->nl_cache_event
= genl_ctrl_alloc_cache(drv
->nl_handle_event
);
1083 if (drv
->nl_cache_event
== NULL
) {
1084 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate generic "
1085 "netlink cache (event)");
1088 #endif /* CONFIG_LIBNL20 */
1090 drv
->nl80211
= genl_ctrl_search_by_name(drv
->nl_cache
, "nl80211");
1091 if (drv
->nl80211
== NULL
) {
1092 wpa_printf(MSG_ERROR
, "nl80211: 'nl80211' generic netlink not "
1097 ret
= nl_get_multicast_id(drv
, "nl80211", "scan");
1099 ret
= nl_socket_add_membership(drv
->nl_handle_event
, ret
);
1101 wpa_printf(MSG_ERROR
, "nl80211: Could not add multicast "
1102 "membership for scan events: %d (%s)",
1103 ret
, strerror(-ret
));
1107 ret
= nl_get_multicast_id(drv
, "nl80211", "mlme");
1109 ret
= nl_socket_add_membership(drv
->nl_handle_event
, ret
);
1111 wpa_printf(MSG_ERROR
, "nl80211: Could not add multicast "
1112 "membership for mlme events: %d (%s)",
1113 ret
, strerror(-ret
));
1117 eloop_register_read_sock(nl_socket_get_fd(drv
->nl_handle_event
),
1118 wpa_driver_nl80211_event_receive
, drv
, ctx
);
1123 nl_cache_free(drv
->nl_cache_event
);
1125 nl_cache_free(drv
->nl_cache
);
1127 nl_handle_destroy(drv
->nl_handle_event
);
1129 nl_handle_destroy(drv
->nl_handle
);
1131 nl_cb_put(drv
->nl_cb
);
1138 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1139 * @ctx: context to be used when calling wpa_supplicant functions,
1140 * e.g., wpa_supplicant_event()
1141 * @ifname: interface name, e.g., wlan0
1142 * Returns: Pointer to private data, %NULL on failure
1144 static void * wpa_driver_nl80211_init(void *ctx
, const char *ifname
)
1146 struct wpa_driver_nl80211_data
*drv
;
1147 struct netlink_config
*cfg
;
1149 drv
= os_zalloc(sizeof(*drv
));
1153 os_strlcpy(drv
->ifname
, ifname
, sizeof(drv
->ifname
));
1154 drv
->monitor_ifidx
= -1;
1155 drv
->monitor_sock
= -1;
1156 drv
->ioctl_sock
= -1;
1158 if (wpa_driver_nl80211_init_nl(drv
, ctx
)) {
1163 drv
->ioctl_sock
= socket(PF_INET
, SOCK_DGRAM
, 0);
1164 if (drv
->ioctl_sock
< 0) {
1165 perror("socket(PF_INET,SOCK_DGRAM)");
1169 cfg
= os_zalloc(sizeof(*cfg
));
1173 cfg
->newlink_cb
= wpa_driver_nl80211_event_rtm_newlink
;
1174 cfg
->dellink_cb
= wpa_driver_nl80211_event_rtm_dellink
;
1175 drv
->netlink
= netlink_init(cfg
);
1176 if (drv
->netlink
== NULL
) {
1180 if (wpa_driver_nl80211_finish_drv_init(drv
))
1186 netlink_deinit(drv
->netlink
);
1187 if (drv
->ioctl_sock
>= 0)
1188 close(drv
->ioctl_sock
);
1190 genl_family_put(drv
->nl80211
);
1191 nl_cache_free(drv
->nl_cache
);
1192 nl_handle_destroy(drv
->nl_handle
);
1193 nl_cb_put(drv
->nl_cb
);
1201 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data
*drv
)
1203 drv
->ifindex
= if_nametoindex(drv
->ifname
);
1206 if (wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_INFRA
) < 0) {
1207 wpa_printf(MSG_DEBUG
, "nl80211: Could not configure driver to "
1208 "use managed mode");
1211 if (linux_set_iface_flags(drv
->ioctl_sock
, drv
->ifname
, 1)) {
1212 wpa_printf(MSG_ERROR
, "Could not set interface '%s' UP",
1217 if (wpa_driver_nl80211_capa(drv
))
1220 netlink_send_oper_ifla(drv
->netlink
, drv
->ifindex
,
1221 1, IF_OPER_DORMANT
);
1222 #endif /* HOSTAPD */
1229 static void wpa_driver_nl80211_free_bss(struct wpa_driver_nl80211_data
*drv
)
1231 struct i802_bss
*bss
, *prev
;
1232 bss
= drv
->bss
.next
;
1239 #endif /* HOSTAPD */
1242 static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data
*drv
)
1246 msg
= nlmsg_alloc();
1250 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1251 0, NL80211_CMD_DEL_BEACON
, 0);
1252 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1254 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1261 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
1262 * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
1264 * Shut down driver interface and processing of driver events. Free
1265 * private data buffer if one was allocated in wpa_driver_nl80211_init().
1267 static void wpa_driver_nl80211_deinit(void *priv
)
1269 struct wpa_driver_nl80211_data
*drv
= priv
;
1271 nl80211_remove_monitor_interface(drv
);
1273 if (drv
->nlmode
== NL80211_IFTYPE_AP
)
1274 wpa_driver_nl80211_del_beacon(drv
);
1277 if (drv
->last_freq_ht
) {
1278 /* Clear HT flags from the driver */
1279 struct hostapd_freq_params freq
;
1280 os_memset(&freq
, 0, sizeof(freq
));
1281 freq
.freq
= drv
->last_freq
;
1282 i802_set_freq(priv
, &freq
);
1285 if (drv
->eapol_sock
>= 0) {
1286 eloop_unregister_read_sock(drv
->eapol_sock
);
1287 close(drv
->eapol_sock
);
1290 if (drv
->if_indices
!= drv
->default_if_indices
)
1291 os_free(drv
->if_indices
);
1293 wpa_driver_nl80211_free_bss(drv
);
1294 #endif /* HOSTAPD */
1296 netlink_send_oper_ifla(drv
->netlink
, drv
->ifindex
, 0, IF_OPER_UP
);
1297 netlink_deinit(drv
->netlink
);
1299 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
, drv
->ctx
);
1301 (void) linux_set_iface_flags(drv
->ioctl_sock
, drv
->ifname
, 0);
1302 wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_INFRA
);
1304 if (drv
->ioctl_sock
>= 0)
1305 close(drv
->ioctl_sock
);
1307 eloop_unregister_read_sock(nl_socket_get_fd(drv
->nl_handle_event
));
1308 genl_family_put(drv
->nl80211
);
1309 nl_cache_free(drv
->nl_cache
);
1310 nl_cache_free(drv
->nl_cache_event
);
1311 nl_handle_destroy(drv
->nl_handle
);
1312 nl_handle_destroy(drv
->nl_handle_event
);
1313 nl_cb_put(drv
->nl_cb
);
1315 eloop_cancel_timeout(wpa_driver_nl80211_probe_req_report_timeout
,
1323 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
1324 * @eloop_ctx: Driver private data
1325 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
1327 * This function can be used as registered timeout when starting a scan to
1328 * generate a scan completed event if the driver does not report this.
1330 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
1332 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
1333 if (drv
->ap_scan_as_station
) {
1334 wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_AP
);
1335 drv
->ap_scan_as_station
= 0;
1337 wpa_printf(MSG_DEBUG
, "Scan timeout - try to get results");
1338 wpa_supplicant_event(timeout_ctx
, EVENT_SCAN_RESULTS
, NULL
);
1343 * wpa_driver_nl80211_scan - Request the driver to initiate scan
1344 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
1345 * @params: Scan parameters
1346 * Returns: 0 on success, -1 on failure
1348 static int wpa_driver_nl80211_scan(void *priv
,
1349 struct wpa_driver_scan_params
*params
)
1351 struct wpa_driver_nl80211_data
*drv
= priv
;
1352 int ret
= 0, timeout
;
1353 struct nl_msg
*msg
, *ssids
, *freqs
;
1356 msg
= nlmsg_alloc();
1357 ssids
= nlmsg_alloc();
1358 freqs
= nlmsg_alloc();
1359 if (!msg
|| !ssids
|| !freqs
) {
1366 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
1367 NL80211_CMD_TRIGGER_SCAN
, 0);
1369 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1371 for (i
= 0; i
< params
->num_ssids
; i
++) {
1372 NLA_PUT(ssids
, i
+ 1, params
->ssids
[i
].ssid_len
,
1373 params
->ssids
[i
].ssid
);
1375 if (params
->num_ssids
)
1376 nla_put_nested(msg
, NL80211_ATTR_SCAN_SSIDS
, ssids
);
1378 if (params
->extra_ies
) {
1379 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->extra_ies_len
,
1383 if (params
->freqs
) {
1384 for (i
= 0; params
->freqs
[i
]; i
++)
1385 NLA_PUT_U32(freqs
, i
+ 1, params
->freqs
[i
]);
1386 nla_put_nested(msg
, NL80211_ATTR_SCAN_FREQUENCIES
, freqs
);
1389 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1392 wpa_printf(MSG_DEBUG
, "nl80211: Scan trigger failed: ret=%d "
1393 "(%s)", ret
, strerror(-ret
));
1395 if (drv
->nlmode
== NL80211_IFTYPE_AP
) {
1397 * mac80211 does not allow scan requests in AP mode, so
1398 * try to do this in station mode.
1400 if (wpa_driver_nl80211_set_mode(drv
,
1401 IEEE80211_MODE_INFRA
))
1402 goto nla_put_failure
;
1404 if (wpa_driver_nl80211_scan(drv
, params
)) {
1405 wpa_driver_nl80211_set_mode(drv
,
1407 goto nla_put_failure
;
1410 /* Restore AP mode when processing scan results */
1411 drv
->ap_scan_as_station
= 1;
1414 goto nla_put_failure
;
1416 goto nla_put_failure
;
1417 #endif /* HOSTAPD */
1420 /* Not all drivers generate "scan completed" wireless event, so try to
1421 * read results after a timeout. */
1423 if (drv
->scan_complete_events
) {
1425 * The driver seems to deliver events to notify when scan is
1426 * complete, so use longer timeout to avoid race conditions
1427 * with scanning and following association request.
1431 wpa_printf(MSG_DEBUG
, "Scan requested (ret=%d) - scan timeout %d "
1432 "seconds", ret
, timeout
);
1433 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
, drv
->ctx
);
1434 eloop_register_timeout(timeout
, 0, wpa_driver_nl80211_scan_timeout
,
1445 static int bss_info_handler(struct nl_msg
*msg
, void *arg
)
1447 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
1448 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
1449 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1450 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1451 [NL80211_BSS_BSSID
] = { .type
= NLA_UNSPEC
},
1452 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1453 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
1454 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
1455 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
1456 [NL80211_BSS_INFORMATION_ELEMENTS
] = { .type
= NLA_UNSPEC
},
1457 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
1458 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
1459 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1460 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
1462 struct wpa_scan_results
*res
= arg
;
1463 struct wpa_scan_res
**tmp
;
1464 struct wpa_scan_res
*r
;
1468 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
1469 genlmsg_attrlen(gnlh
, 0), NULL
);
1470 if (!tb
[NL80211_ATTR_BSS
])
1472 if (nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
1475 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
]) {
1476 ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1477 ie_len
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1483 r
= os_zalloc(sizeof(*r
) + ie_len
);
1486 if (bss
[NL80211_BSS_BSSID
])
1487 os_memcpy(r
->bssid
, nla_data(bss
[NL80211_BSS_BSSID
]),
1489 if (bss
[NL80211_BSS_FREQUENCY
])
1490 r
->freq
= nla_get_u32(bss
[NL80211_BSS_FREQUENCY
]);
1491 if (bss
[NL80211_BSS_BEACON_INTERVAL
])
1492 r
->beacon_int
= nla_get_u16(bss
[NL80211_BSS_BEACON_INTERVAL
]);
1493 if (bss
[NL80211_BSS_CAPABILITY
])
1494 r
->caps
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
1495 r
->flags
|= WPA_SCAN_NOISE_INVALID
;
1496 if (bss
[NL80211_BSS_SIGNAL_MBM
]) {
1497 r
->level
= nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]);
1498 r
->level
/= 100; /* mBm to dBm */
1499 r
->flags
|= WPA_SCAN_LEVEL_DBM
| WPA_SCAN_QUAL_INVALID
;
1500 } else if (bss
[NL80211_BSS_SIGNAL_UNSPEC
]) {
1501 r
->level
= nla_get_u8(bss
[NL80211_BSS_SIGNAL_UNSPEC
]);
1502 r
->flags
|= WPA_SCAN_LEVEL_INVALID
;
1504 r
->flags
|= WPA_SCAN_LEVEL_INVALID
| WPA_SCAN_QUAL_INVALID
;
1505 if (bss
[NL80211_BSS_TSF
])
1506 r
->tsf
= nla_get_u64(bss
[NL80211_BSS_TSF
]);
1507 if (bss
[NL80211_BSS_SEEN_MS_AGO
])
1508 r
->age
= nla_get_u32(bss
[NL80211_BSS_SEEN_MS_AGO
]);
1511 os_memcpy(r
+ 1, ie
, ie_len
);
1513 if (bss
[NL80211_BSS_STATUS
]) {
1514 enum nl80211_bss_status status
;
1515 status
= nla_get_u32(bss
[NL80211_BSS_STATUS
]);
1517 case NL80211_BSS_STATUS_AUTHENTICATED
:
1518 r
->flags
|= WPA_SCAN_AUTHENTICATED
;
1520 case NL80211_BSS_STATUS_ASSOCIATED
:
1521 r
->flags
|= WPA_SCAN_ASSOCIATED
;
1528 tmp
= os_realloc(res
->res
,
1529 (res
->num
+ 1) * sizeof(struct wpa_scan_res
*));
1534 tmp
[res
->num
++] = r
;
1541 static void clear_state_mismatch(struct wpa_driver_nl80211_data
*drv
,
1544 if (drv
->capa
.flags
& WPA_DRIVER_FLAGS_SME
) {
1545 wpa_printf(MSG_DEBUG
, "nl80211: Clear possible state "
1547 wpa_driver_nl80211_mlme(drv
, addr
,
1548 NL80211_CMD_DEAUTHENTICATE
,
1549 WLAN_REASON_PREV_AUTH_NOT_VALID
);
1554 static void wpa_driver_nl80211_check_bss_status(
1555 struct wpa_driver_nl80211_data
*drv
, struct wpa_scan_results
*res
)
1559 for (i
= 0; i
< res
->num
; i
++) {
1560 struct wpa_scan_res
*r
= res
->res
[i
];
1561 if (r
->flags
& WPA_SCAN_AUTHENTICATED
) {
1562 wpa_printf(MSG_DEBUG
, "nl80211: Scan results "
1563 "indicates BSS status with " MACSTR
1564 " as authenticated",
1566 if (drv
->nlmode
== NL80211_IFTYPE_STATION
&&
1567 os_memcmp(r
->bssid
, drv
->bssid
, ETH_ALEN
) != 0 &&
1568 os_memcmp(r
->bssid
, drv
->auth_bssid
, ETH_ALEN
) !=
1570 wpa_printf(MSG_DEBUG
, "nl80211: Unknown BSSID"
1571 " in local state (auth=" MACSTR
1572 " assoc=" MACSTR
")",
1573 MAC2STR(drv
->auth_bssid
),
1574 MAC2STR(drv
->bssid
));
1578 if (r
->flags
& WPA_SCAN_ASSOCIATED
) {
1579 wpa_printf(MSG_DEBUG
, "nl80211: Scan results "
1580 "indicate BSS status with " MACSTR
1583 if (drv
->nlmode
== NL80211_IFTYPE_STATION
&&
1585 wpa_printf(MSG_DEBUG
, "nl80211: Local state "
1586 "(not associated) does not match "
1588 clear_state_mismatch(drv
, r
->bssid
);
1589 } else if (drv
->nlmode
== NL80211_IFTYPE_STATION
&&
1590 os_memcmp(drv
->bssid
, r
->bssid
, ETH_ALEN
) !=
1592 wpa_printf(MSG_DEBUG
, "nl80211: Local state "
1593 "(associated with " MACSTR
") does "
1594 "not match with BSS state",
1595 MAC2STR(drv
->bssid
));
1596 clear_state_mismatch(drv
, r
->bssid
);
1597 clear_state_mismatch(drv
, drv
->bssid
);
1604 static void wpa_scan_results_free(struct wpa_scan_results
*res
)
1611 for (i
= 0; i
< res
->num
; i
++)
1612 os_free(res
->res
[i
]);
1619 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
1620 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
1621 * Returns: Scan results on success, -1 on failure
1623 static struct wpa_scan_results
*
1624 wpa_driver_nl80211_get_scan_results(void *priv
)
1626 struct wpa_driver_nl80211_data
*drv
= priv
;
1628 struct wpa_scan_results
*res
;
1631 res
= os_zalloc(sizeof(*res
));
1634 msg
= nlmsg_alloc();
1636 goto nla_put_failure
;
1638 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, NLM_F_DUMP
,
1639 NL80211_CMD_GET_SCAN
, 0);
1640 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1642 ret
= send_and_recv_msgs(drv
, msg
, bss_info_handler
, res
);
1645 wpa_printf(MSG_DEBUG
, "Received scan results (%lu BSSes)",
1646 (unsigned long) res
->num
);
1647 wpa_driver_nl80211_check_bss_status(drv
, res
);
1650 wpa_printf(MSG_DEBUG
, "nl80211: Scan result fetch failed: ret=%d "
1651 "(%s)", ret
, strerror(-ret
));
1654 wpa_scan_results_free(res
);
1659 static int wpa_driver_nl80211_set_key(const char *ifname
, void *priv
,
1660 enum wpa_alg alg
, const u8
*addr
,
1661 int key_idx
, int set_tx
,
1662 const u8
*seq
, size_t seq_len
,
1663 const u8
*key
, size_t key_len
)
1665 struct wpa_driver_nl80211_data
*drv
= priv
;
1666 int ifindex
= if_nametoindex(ifname
);
1670 wpa_printf(MSG_DEBUG
, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
1671 "set_tx=%d seq_len=%lu key_len=%lu",
1672 __func__
, ifindex
, alg
, addr
, key_idx
, set_tx
,
1673 (unsigned long) seq_len
, (unsigned long) key_len
);
1675 msg
= nlmsg_alloc();
1679 if (alg
== WPA_ALG_NONE
) {
1680 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1681 0, NL80211_CMD_DEL_KEY
, 0);
1683 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1684 0, NL80211_CMD_NEW_KEY
, 0);
1685 NLA_PUT(msg
, NL80211_ATTR_KEY_DATA
, key_len
, key
);
1689 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1690 WLAN_CIPHER_SUITE_WEP40
);
1692 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1693 WLAN_CIPHER_SUITE_WEP104
);
1696 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1697 WLAN_CIPHER_SUITE_TKIP
);
1700 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1701 WLAN_CIPHER_SUITE_CCMP
);
1704 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1705 WLAN_CIPHER_SUITE_AES_CMAC
);
1708 wpa_printf(MSG_ERROR
, "%s: Unsupported encryption "
1709 "algorithm %d", __func__
, alg
);
1716 NLA_PUT(msg
, NL80211_ATTR_KEY_SEQ
, seq_len
, seq
);
1718 if (addr
&& os_memcmp(addr
, "\xff\xff\xff\xff\xff\xff", ETH_ALEN
) != 0)
1720 wpa_printf(MSG_DEBUG
, " addr=" MACSTR
, MAC2STR(addr
));
1721 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
1723 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, key_idx
);
1724 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
1726 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1727 if (ret
== -ENOENT
&& alg
== WPA_ALG_NONE
)
1730 wpa_printf(MSG_DEBUG
, "nl80211: set_key failed; err=%d %s)",
1731 ret
, strerror(-ret
));
1734 * If we failed or don't need to set the default TX key (below),
1737 if (ret
|| !set_tx
|| alg
== WPA_ALG_NONE
)
1739 #ifdef HOSTAPD /* FIX: is this needed? */
1742 #endif /* HOSTAPD */
1744 msg
= nlmsg_alloc();
1748 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1749 0, NL80211_CMD_SET_KEY
, 0);
1750 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, key_idx
);
1751 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
1752 if (alg
== WPA_ALG_IGTK
)
1753 NLA_PUT_FLAG(msg
, NL80211_ATTR_KEY_DEFAULT_MGMT
);
1755 NLA_PUT_FLAG(msg
, NL80211_ATTR_KEY_DEFAULT
);
1757 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1761 wpa_printf(MSG_DEBUG
, "nl80211: set_key default failed; "
1762 "err=%d %s)", ret
, strerror(-ret
));
1770 static int nl_add_key(struct nl_msg
*msg
, enum wpa_alg alg
,
1771 int key_idx
, int defkey
,
1772 const u8
*seq
, size_t seq_len
,
1773 const u8
*key
, size_t key_len
)
1775 struct nlattr
*key_attr
= nla_nest_start(msg
, NL80211_ATTR_KEY
);
1779 if (defkey
&& alg
== WPA_ALG_IGTK
)
1780 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT_MGMT
);
1782 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT
);
1784 NLA_PUT_U8(msg
, NL80211_KEY_IDX
, key_idx
);
1789 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
,
1790 WLAN_CIPHER_SUITE_WEP40
);
1792 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
,
1793 WLAN_CIPHER_SUITE_WEP104
);
1796 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, WLAN_CIPHER_SUITE_TKIP
);
1799 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, WLAN_CIPHER_SUITE_CCMP
);
1802 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
,
1803 WLAN_CIPHER_SUITE_AES_CMAC
);
1806 wpa_printf(MSG_ERROR
, "%s: Unsupported encryption "
1807 "algorithm %d", __func__
, alg
);
1812 NLA_PUT(msg
, NL80211_KEY_SEQ
, seq_len
, seq
);
1814 NLA_PUT(msg
, NL80211_KEY_DATA
, key_len
, key
);
1816 nla_nest_end(msg
, key_attr
);
1824 static int nl80211_set_conn_keys(struct wpa_driver_associate_params
*params
,
1828 struct nlattr
*nl_keys
, *nl_key
;
1830 for (i
= 0; i
< 4; i
++) {
1831 if (!params
->wep_key
[i
])
1839 NLA_PUT_FLAG(msg
, NL80211_ATTR_PRIVACY
);
1841 nl_keys
= nla_nest_start(msg
, NL80211_ATTR_KEYS
);
1843 goto nla_put_failure
;
1845 for (i
= 0; i
< 4; i
++) {
1846 if (!params
->wep_key
[i
])
1849 nl_key
= nla_nest_start(msg
, i
);
1851 goto nla_put_failure
;
1853 NLA_PUT(msg
, NL80211_KEY_DATA
, params
->wep_key_len
[i
],
1854 params
->wep_key
[i
]);
1855 if (params
->wep_key_len
[i
] == 5)
1856 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
,
1857 WLAN_CIPHER_SUITE_WEP40
);
1859 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
,
1860 WLAN_CIPHER_SUITE_WEP104
);
1862 NLA_PUT_U8(msg
, NL80211_KEY_IDX
, i
);
1864 if (i
== params
->wep_tx_keyidx
)
1865 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT
);
1867 nla_nest_end(msg
, nl_key
);
1869 nla_nest_end(msg
, nl_keys
);
1878 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data
*drv
,
1879 const u8
*addr
, int cmd
, u16 reason_code
)
1884 msg
= nlmsg_alloc();
1888 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0, cmd
, 0);
1890 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1891 NLA_PUT_U16(msg
, NL80211_ATTR_REASON_CODE
, reason_code
);
1892 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
1894 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1897 wpa_printf(MSG_DEBUG
, "nl80211: MLME command failed: ret=%d "
1898 "(%s)", ret
, strerror(-ret
));
1899 goto nla_put_failure
;
1909 static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data
*drv
,
1910 const u8
*addr
, int reason_code
)
1912 wpa_printf(MSG_DEBUG
, "%s", __func__
);
1913 drv
->associated
= 0;
1914 return wpa_driver_nl80211_mlme(drv
, addr
, NL80211_CMD_DISCONNECT
,
1919 static int wpa_driver_nl80211_deauthenticate(void *priv
, const u8
*addr
,
1922 struct wpa_driver_nl80211_data
*drv
= priv
;
1923 if (!(drv
->capa
.flags
& WPA_DRIVER_FLAGS_SME
))
1924 return wpa_driver_nl80211_disconnect(drv
, addr
, reason_code
);
1925 wpa_printf(MSG_DEBUG
, "%s", __func__
);
1926 drv
->associated
= 0;
1927 return wpa_driver_nl80211_mlme(drv
, addr
, NL80211_CMD_DEAUTHENTICATE
,
1932 static int wpa_driver_nl80211_disassociate(void *priv
, const u8
*addr
,
1935 struct wpa_driver_nl80211_data
*drv
= priv
;
1936 if (!(drv
->capa
.flags
& WPA_DRIVER_FLAGS_SME
))
1937 return wpa_driver_nl80211_disconnect(drv
, addr
, reason_code
);
1938 wpa_printf(MSG_DEBUG
, "%s", __func__
);
1939 drv
->associated
= 0;
1940 return wpa_driver_nl80211_mlme(drv
, addr
, NL80211_CMD_DISASSOCIATE
,
1945 static int wpa_driver_nl80211_authenticate(
1946 void *priv
, struct wpa_driver_auth_params
*params
)
1948 struct wpa_driver_nl80211_data
*drv
= priv
;
1951 enum nl80211_auth_type type
;
1954 drv
->associated
= 0;
1955 os_memset(drv
->auth_bssid
, 0, ETH_ALEN
);
1957 if (wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_INFRA
) < 0)
1961 msg
= nlmsg_alloc();
1965 wpa_printf(MSG_DEBUG
, "nl80211: Authenticate (ifindex=%d)",
1968 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
1969 NL80211_CMD_AUTHENTICATE
, 0);
1971 for (i
= 0; i
< 4; i
++) {
1972 if (!params
->wep_key
[i
])
1974 wpa_driver_nl80211_set_key(drv
->ifname
, drv
, WPA_ALG_WEP
, NULL
,
1976 i
== params
->wep_tx_keyidx
, NULL
, 0,
1978 params
->wep_key_len
[i
]);
1979 if (params
->wep_tx_keyidx
!= i
)
1981 if (nl_add_key(msg
, WPA_ALG_WEP
, i
, 1, NULL
, 0,
1982 params
->wep_key
[i
], params
->wep_key_len
[i
])) {
1988 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1989 if (params
->bssid
) {
1990 wpa_printf(MSG_DEBUG
, " * bssid=" MACSTR
,
1991 MAC2STR(params
->bssid
));
1992 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, params
->bssid
);
1995 wpa_printf(MSG_DEBUG
, " * freq=%d", params
->freq
);
1996 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, params
->freq
);
1999 wpa_hexdump_ascii(MSG_DEBUG
, " * SSID",
2000 params
->ssid
, params
->ssid_len
);
2001 NLA_PUT(msg
, NL80211_ATTR_SSID
, params
->ssid_len
,
2004 wpa_hexdump(MSG_DEBUG
, " * IEs", params
->ie
, params
->ie_len
);
2006 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->ie_len
, params
->ie
);
2008 * TODO: if multiple auth_alg options enabled, try them one by one if
2009 * the AP rejects authentication due to unknown auth alg
2011 if (params
->auth_alg
& WPA_AUTH_ALG_OPEN
)
2012 type
= NL80211_AUTHTYPE_OPEN_SYSTEM
;
2013 else if (params
->auth_alg
& WPA_AUTH_ALG_SHARED
)
2014 type
= NL80211_AUTHTYPE_SHARED_KEY
;
2015 else if (params
->auth_alg
& WPA_AUTH_ALG_LEAP
)
2016 type
= NL80211_AUTHTYPE_NETWORK_EAP
;
2017 else if (params
->auth_alg
& WPA_AUTH_ALG_FT
)
2018 type
= NL80211_AUTHTYPE_FT
;
2020 goto nla_put_failure
;
2021 wpa_printf(MSG_DEBUG
, " * Auth Type %d", type
);
2022 NLA_PUT_U32(msg
, NL80211_ATTR_AUTH_TYPE
, type
);
2024 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2027 wpa_printf(MSG_DEBUG
, "nl80211: MLME command failed: ret=%d "
2028 "(%s)", ret
, strerror(-ret
));
2030 if (ret
== -EALREADY
&& count
== 1 && params
->bssid
) {
2032 * mac80211 does not currently accept new
2033 * authentication if we are already authenticated. As a
2034 * workaround, force deauthentication and try again.
2036 wpa_printf(MSG_DEBUG
, "nl80211: Retry authentication "
2037 "after forced deauthentication");
2038 wpa_driver_nl80211_deauthenticate(
2040 WLAN_REASON_PREV_AUTH_NOT_VALID
);
2044 goto nla_put_failure
;
2047 wpa_printf(MSG_DEBUG
, "nl80211: Authentication request send "
2056 struct phy_info_arg
{
2058 struct hostapd_hw_modes
*modes
;
2061 static int phy_info_handler(struct nl_msg
*msg
, void *arg
)
2063 struct nlattr
*tb_msg
[NL80211_ATTR_MAX
+ 1];
2064 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
2065 struct phy_info_arg
*phy_info
= arg
;
2067 struct nlattr
*tb_band
[NL80211_BAND_ATTR_MAX
+ 1];
2069 struct nlattr
*tb_freq
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2070 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
2071 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
2072 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
2073 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
2074 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
2075 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
2076 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
2079 struct nlattr
*tb_rate
[NL80211_BITRATE_ATTR_MAX
+ 1];
2080 static struct nla_policy rate_policy
[NL80211_BITRATE_ATTR_MAX
+ 1] = {
2081 [NL80211_BITRATE_ATTR_RATE
] = { .type
= NLA_U32
},
2082 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE
] = { .type
= NLA_FLAG
},
2085 struct nlattr
*nl_band
;
2086 struct nlattr
*nl_freq
;
2087 struct nlattr
*nl_rate
;
2088 int rem_band
, rem_freq
, rem_rate
;
2089 struct hostapd_hw_modes
*mode
;
2090 int idx
, mode_is_set
;
2092 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
2093 genlmsg_attrlen(gnlh
, 0), NULL
);
2095 if (!tb_msg
[NL80211_ATTR_WIPHY_BANDS
])
2098 nla_for_each_nested(nl_band
, tb_msg
[NL80211_ATTR_WIPHY_BANDS
], rem_band
) {
2099 mode
= os_realloc(phy_info
->modes
, (*phy_info
->num_modes
+ 1) * sizeof(*mode
));
2102 phy_info
->modes
= mode
;
2106 mode
= &phy_info
->modes
[*(phy_info
->num_modes
)];
2107 memset(mode
, 0, sizeof(*mode
));
2108 *(phy_info
->num_modes
) += 1;
2110 nla_parse(tb_band
, NL80211_BAND_ATTR_MAX
, nla_data(nl_band
),
2111 nla_len(nl_band
), NULL
);
2113 if (tb_band
[NL80211_BAND_ATTR_HT_CAPA
]) {
2114 mode
->ht_capab
= nla_get_u16(
2115 tb_band
[NL80211_BAND_ATTR_HT_CAPA
]);
2118 if (tb_band
[NL80211_BAND_ATTR_HT_AMPDU_FACTOR
]) {
2119 mode
->a_mpdu_params
|= nla_get_u8(
2120 tb_band
[NL80211_BAND_ATTR_HT_AMPDU_FACTOR
]) &
2124 if (tb_band
[NL80211_BAND_ATTR_HT_AMPDU_DENSITY
]) {
2125 mode
->a_mpdu_params
|= nla_get_u8(
2126 tb_band
[NL80211_BAND_ATTR_HT_AMPDU_DENSITY
]) <<
2130 if (tb_band
[NL80211_BAND_ATTR_HT_MCS_SET
] &&
2131 nla_len(tb_band
[NL80211_BAND_ATTR_HT_MCS_SET
])) {
2133 mcs
= nla_data(tb_band
[NL80211_BAND_ATTR_HT_MCS_SET
]);
2134 os_memcpy(mode
->mcs_set
, mcs
, 16);
2137 nla_for_each_nested(nl_freq
, tb_band
[NL80211_BAND_ATTR_FREQS
], rem_freq
) {
2138 nla_parse(tb_freq
, NL80211_FREQUENCY_ATTR_MAX
, nla_data(nl_freq
),
2139 nla_len(nl_freq
), freq_policy
);
2140 if (!tb_freq
[NL80211_FREQUENCY_ATTR_FREQ
])
2142 mode
->num_channels
++;
2145 mode
->channels
= os_zalloc(mode
->num_channels
* sizeof(struct hostapd_channel_data
));
2146 if (!mode
->channels
)
2151 nla_for_each_nested(nl_freq
, tb_band
[NL80211_BAND_ATTR_FREQS
], rem_freq
) {
2152 nla_parse(tb_freq
, NL80211_FREQUENCY_ATTR_MAX
, nla_data(nl_freq
),
2153 nla_len(nl_freq
), freq_policy
);
2154 if (!tb_freq
[NL80211_FREQUENCY_ATTR_FREQ
])
2157 mode
->channels
[idx
].freq
= nla_get_u32(tb_freq
[NL80211_FREQUENCY_ATTR_FREQ
]);
2158 mode
->channels
[idx
].flag
= 0;
2161 /* crude heuristic */
2162 if (mode
->channels
[idx
].freq
< 4000)
2163 mode
->mode
= HOSTAPD_MODE_IEEE80211B
;
2165 mode
->mode
= HOSTAPD_MODE_IEEE80211A
;
2169 /* crude heuristic */
2170 if (mode
->channels
[idx
].freq
< 4000)
2171 if (mode
->channels
[idx
].freq
== 2484)
2172 mode
->channels
[idx
].chan
= 14;
2174 mode
->channels
[idx
].chan
= (mode
->channels
[idx
].freq
- 2407) / 5;
2176 mode
->channels
[idx
].chan
= mode
->channels
[idx
].freq
/5 - 1000;
2178 if (tb_freq
[NL80211_FREQUENCY_ATTR_DISABLED
])
2179 mode
->channels
[idx
].flag
|=
2180 HOSTAPD_CHAN_DISABLED
;
2181 if (tb_freq
[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
])
2182 mode
->channels
[idx
].flag
|=
2183 HOSTAPD_CHAN_PASSIVE_SCAN
;
2184 if (tb_freq
[NL80211_FREQUENCY_ATTR_NO_IBSS
])
2185 mode
->channels
[idx
].flag
|=
2186 HOSTAPD_CHAN_NO_IBSS
;
2187 if (tb_freq
[NL80211_FREQUENCY_ATTR_RADAR
])
2188 mode
->channels
[idx
].flag
|=
2191 if (tb_freq
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] &&
2192 !tb_freq
[NL80211_FREQUENCY_ATTR_DISABLED
])
2193 mode
->channels
[idx
].max_tx_power
=
2194 nla_get_u32(tb_freq
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]) / 100;
2199 nla_for_each_nested(nl_rate
, tb_band
[NL80211_BAND_ATTR_RATES
], rem_rate
) {
2200 nla_parse(tb_rate
, NL80211_BITRATE_ATTR_MAX
, nla_data(nl_rate
),
2201 nla_len(nl_rate
), rate_policy
);
2202 if (!tb_rate
[NL80211_BITRATE_ATTR_RATE
])
2207 mode
->rates
= os_zalloc(mode
->num_rates
* sizeof(int));
2213 nla_for_each_nested(nl_rate
, tb_band
[NL80211_BAND_ATTR_RATES
], rem_rate
) {
2214 nla_parse(tb_rate
, NL80211_BITRATE_ATTR_MAX
, nla_data(nl_rate
),
2215 nla_len(nl_rate
), rate_policy
);
2216 if (!tb_rate
[NL80211_BITRATE_ATTR_RATE
])
2218 mode
->rates
[idx
] = nla_get_u32(tb_rate
[NL80211_BITRATE_ATTR_RATE
]);
2220 /* crude heuristic */
2221 if (mode
->mode
== HOSTAPD_MODE_IEEE80211B
&&
2222 mode
->rates
[idx
] > 200)
2223 mode
->mode
= HOSTAPD_MODE_IEEE80211G
;
2232 static struct hostapd_hw_modes
*
2233 wpa_driver_nl80211_add_11b(struct hostapd_hw_modes
*modes
, u16
*num_modes
)
2236 struct hostapd_hw_modes
*mode11g
= NULL
, *nmodes
, *mode
;
2237 int i
, mode11g_idx
= -1;
2239 /* If only 802.11g mode is included, use it to construct matching
2240 * 802.11b mode data. */
2242 for (m
= 0; m
< *num_modes
; m
++) {
2243 if (modes
[m
].mode
== HOSTAPD_MODE_IEEE80211B
)
2244 return modes
; /* 802.11b already included */
2245 if (modes
[m
].mode
== HOSTAPD_MODE_IEEE80211G
)
2249 if (mode11g_idx
< 0)
2250 return modes
; /* 2.4 GHz band not supported at all */
2252 nmodes
= os_realloc(modes
, (*num_modes
+ 1) * sizeof(*nmodes
));
2254 return modes
; /* Could not add 802.11b mode */
2256 mode
= &nmodes
[*num_modes
];
2257 os_memset(mode
, 0, sizeof(*mode
));
2261 mode
->mode
= HOSTAPD_MODE_IEEE80211B
;
2263 mode11g
= &modes
[mode11g_idx
];
2264 mode
->num_channels
= mode11g
->num_channels
;
2265 mode
->channels
= os_malloc(mode11g
->num_channels
*
2266 sizeof(struct hostapd_channel_data
));
2267 if (mode
->channels
== NULL
) {
2269 return modes
; /* Could not add 802.11b mode */
2271 os_memcpy(mode
->channels
, mode11g
->channels
,
2272 mode11g
->num_channels
* sizeof(struct hostapd_channel_data
));
2274 mode
->num_rates
= 0;
2275 mode
->rates
= os_malloc(4 * sizeof(int));
2276 if (mode
->rates
== NULL
) {
2277 os_free(mode
->channels
);
2279 return modes
; /* Could not add 802.11b mode */
2282 for (i
= 0; i
< mode11g
->num_rates
; i
++) {
2283 if (mode11g
->rates
[i
] != 10 && mode11g
->rates
[i
] != 20 &&
2284 mode11g
->rates
[i
] != 55 && mode11g
->rates
[i
] != 110)
2286 mode
->rates
[mode
->num_rates
] = mode11g
->rates
[i
];
2288 if (mode
->num_rates
== 4)
2292 if (mode
->num_rates
== 0) {
2293 os_free(mode
->channels
);
2294 os_free(mode
->rates
);
2296 return modes
; /* No 802.11b rates */
2299 wpa_printf(MSG_DEBUG
, "nl80211: Added 802.11b mode based on 802.11g "
2306 static struct hostapd_hw_modes
*
2307 wpa_driver_nl80211_get_hw_feature_data(void *priv
, u16
*num_modes
, u16
*flags
)
2309 struct wpa_driver_nl80211_data
*drv
= priv
;
2311 struct phy_info_arg result
= {
2312 .num_modes
= num_modes
,
2319 msg
= nlmsg_alloc();
2323 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2324 0, NL80211_CMD_GET_WIPHY
, 0);
2326 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
2328 if (send_and_recv_msgs(drv
, msg
, phy_info_handler
, &result
) == 0)
2329 return wpa_driver_nl80211_add_11b(result
.modes
, num_modes
);
2335 static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data
*drv
,
2336 const void *data
, size_t len
,
2340 0x00, 0x00, /* radiotap version */
2341 0x0e, 0x00, /* radiotap length */
2342 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
2343 IEEE80211_RADIOTAP_F_FRAG
, /* F_FRAG (fragment if required) */
2345 0x00, 0x00, /* RX and TX flags to indicate that */
2346 0x00, 0x00, /* this is the injected frame directly */
2348 struct iovec iov
[2] = {
2350 .iov_base
= &rtap_hdr
,
2351 .iov_len
= sizeof(rtap_hdr
),
2354 .iov_base
= (void *) data
,
2358 struct msghdr msg
= {
2363 .msg_control
= NULL
,
2364 .msg_controllen
= 0,
2369 rtap_hdr
[8] |= IEEE80211_RADIOTAP_F_WEP
;
2371 return sendmsg(drv
->monitor_sock
, &msg
, 0);
2375 static int wpa_driver_nl80211_send_mlme(void *priv
, const u8
*data
,
2378 struct wpa_driver_nl80211_data
*drv
= priv
;
2379 struct ieee80211_mgmt
*mgmt
;
2383 mgmt
= (struct ieee80211_mgmt
*) data
;
2384 fc
= le_to_host16(mgmt
->frame_control
);
2386 if (WLAN_FC_GET_TYPE(fc
) == WLAN_FC_TYPE_MGMT
&&
2387 WLAN_FC_GET_STYPE(fc
) == WLAN_FC_STYPE_AUTH
) {
2389 * Only one of the authentication frame types is encrypted.
2390 * In order for static WEP encryption to work properly (i.e.,
2391 * to not encrypt the frame), we need to tell mac80211 about
2392 * the frames that must not be encrypted.
2394 u16 auth_alg
= le_to_host16(mgmt
->u
.auth
.auth_alg
);
2395 u16 auth_trans
= le_to_host16(mgmt
->u
.auth
.auth_transaction
);
2396 if (auth_alg
!= WLAN_AUTH_SHARED_KEY
|| auth_trans
!= 3)
2400 return wpa_driver_nl80211_send_frame(drv
, data
, data_len
, encrypt
);
2404 static int wpa_driver_nl80211_set_beacon(const char *ifname
, void *priv
,
2405 const u8
*head
, size_t head_len
,
2406 const u8
*tail
, size_t tail_len
,
2407 int dtim_period
, int beacon_int
)
2409 struct wpa_driver_nl80211_data
*drv
= priv
;
2411 u8 cmd
= NL80211_CMD_NEW_BEACON
;
2414 int ifindex
= if_nametoindex(ifname
);
2416 struct i802_bss
*bss
;
2418 bss
= get_bss(drv
, ifindex
);
2421 beacon_set
= bss
->beacon_set
;
2423 beacon_set
= drv
->beacon_set
;
2424 #endif /* HOSTAPD */
2426 msg
= nlmsg_alloc();
2430 wpa_printf(MSG_DEBUG
, "nl80211: Set beacon (beacon_set=%d)",
2433 cmd
= NL80211_CMD_SET_BEACON
;
2435 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2437 NLA_PUT(msg
, NL80211_ATTR_BEACON_HEAD
, head_len
, head
);
2438 NLA_PUT(msg
, NL80211_ATTR_BEACON_TAIL
, tail_len
, tail
);
2439 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
2440 NLA_PUT_U32(msg
, NL80211_ATTR_BEACON_INTERVAL
, beacon_int
);
2441 NLA_PUT_U32(msg
, NL80211_ATTR_DTIM_PERIOD
, dtim_period
);
2443 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2445 wpa_printf(MSG_DEBUG
, "nl80211: Beacon set failed: %d (%s)",
2446 ret
, strerror(-ret
));
2449 bss
->beacon_set
= 1;
2451 drv
->beacon_set
= 1;
2452 #endif /* HOSTAPD */
2460 static int wpa_driver_nl80211_set_freq(struct wpa_driver_nl80211_data
*drv
,
2461 int freq
, int ht_enabled
,
2462 int sec_channel_offset
)
2467 msg
= nlmsg_alloc();
2471 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
2472 NL80211_CMD_SET_WIPHY
, 0);
2474 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
2475 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, freq
);
2477 switch (sec_channel_offset
) {
2479 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
2480 NL80211_CHAN_HT40MINUS
);
2483 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
2484 NL80211_CHAN_HT40PLUS
);
2487 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
2493 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2496 wpa_printf(MSG_DEBUG
, "nl80211: Failed to set channel (freq=%d): "
2497 "%d (%s)", freq
, ret
, strerror(-ret
));
2503 static int wpa_driver_nl80211_sta_add(const char *ifname
, void *priv
,
2504 struct hostapd_sta_add_params
*params
)
2506 struct wpa_driver_nl80211_data
*drv
= priv
;
2510 msg
= nlmsg_alloc();
2514 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2515 0, NL80211_CMD_NEW_STATION
, 0);
2517 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(ifname
));
2518 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, params
->addr
);
2519 NLA_PUT_U16(msg
, NL80211_ATTR_STA_AID
, params
->aid
);
2520 NLA_PUT(msg
, NL80211_ATTR_STA_SUPPORTED_RATES
, params
->supp_rates_len
,
2521 params
->supp_rates
);
2522 NLA_PUT_U16(msg
, NL80211_ATTR_STA_LISTEN_INTERVAL
,
2523 params
->listen_interval
);
2524 if (params
->ht_capabilities
) {
2525 NLA_PUT(msg
, NL80211_ATTR_HT_CAPABILITY
,
2526 sizeof(*params
->ht_capabilities
),
2527 params
->ht_capabilities
);
2530 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2532 wpa_printf(MSG_DEBUG
, "nl80211: NL80211_CMD_NEW_STATION "
2533 "result: %d (%s)", ret
, strerror(-ret
));
2541 static int wpa_driver_nl80211_sta_remove(void *priv
, const u8
*addr
)
2543 struct wpa_driver_nl80211_data
*drv
= priv
;
2547 msg
= nlmsg_alloc();
2551 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2552 0, NL80211_CMD_DEL_STATION
, 0);
2554 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
2555 if_nametoindex(drv
->ifname
));
2556 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
2558 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2567 static void nl80211_remove_iface(struct wpa_driver_nl80211_data
*drv
,
2572 wpa_printf(MSG_DEBUG
, "nl80211: Remove interface ifindex=%d", ifidx
);
2575 /* stop listening for EAPOL on this interface */
2576 del_ifidx(drv
, ifidx
);
2577 #endif /* HOSTAPD */
2579 msg
= nlmsg_alloc();
2581 goto nla_put_failure
;
2583 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2584 0, NL80211_CMD_DEL_INTERFACE
, 0);
2585 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifidx
);
2587 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
) == 0)
2590 wpa_printf(MSG_ERROR
, "Failed to remove interface (ifidx=%d).\n",
2595 static int nl80211_create_iface_once(struct wpa_driver_nl80211_data
*drv
,
2597 enum nl80211_iftype iftype
,
2598 const u8
*addr
, int wds
)
2600 struct nl_msg
*msg
, *flags
= NULL
;
2604 msg
= nlmsg_alloc();
2608 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2609 0, NL80211_CMD_NEW_INTERFACE
, 0);
2610 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
2611 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, ifname
);
2612 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, iftype
);
2614 if (iftype
== NL80211_IFTYPE_MONITOR
) {
2617 flags
= nlmsg_alloc();
2619 goto nla_put_failure
;
2621 NLA_PUT_FLAG(flags
, NL80211_MNTR_FLAG_COOK_FRAMES
);
2623 err
= nla_put_nested(msg
, NL80211_ATTR_MNTR_FLAGS
, flags
);
2628 goto nla_put_failure
;
2630 NLA_PUT_U8(msg
, NL80211_ATTR_4ADDR
, wds
);
2633 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2636 wpa_printf(MSG_ERROR
, "Failed to create interface %s: %d (%s)",
2637 ifname
, ret
, strerror(-ret
));
2641 ifidx
= if_nametoindex(ifname
);
2642 wpa_printf(MSG_DEBUG
, "nl80211: New interface %s created: ifindex=%d",
2649 /* start listening for EAPOL on this interface */
2650 add_ifidx(drv
, ifidx
);
2651 #endif /* HOSTAPD */
2653 if (addr
&& iftype
!= NL80211_IFTYPE_MONITOR
&&
2654 linux_set_ifhwaddr(drv
->ioctl_sock
, ifname
, addr
)) {
2655 nl80211_remove_iface(drv
, ifidx
);
2663 static int nl80211_create_iface(struct wpa_driver_nl80211_data
*drv
,
2664 const char *ifname
, enum nl80211_iftype iftype
,
2665 const u8
*addr
, int wds
)
2669 ret
= nl80211_create_iface_once(drv
, ifname
, iftype
, addr
, wds
);
2671 /* if error occured and interface exists already */
2672 if (ret
== -ENFILE
&& if_nametoindex(ifname
)) {
2673 wpa_printf(MSG_INFO
, "Try to remove and re-create %s", ifname
);
2675 /* Try to remove the interface that was already there. */
2676 nl80211_remove_iface(drv
, if_nametoindex(ifname
));
2678 /* Try to create the interface again */
2679 ret
= nl80211_create_iface_once(drv
, ifname
, iftype
, addr
,
2687 static void handle_tx_callback(void *ctx
, u8
*buf
, size_t len
, int ok
)
2689 struct ieee80211_hdr
*hdr
;
2691 union wpa_event_data event
;
2693 hdr
= (struct ieee80211_hdr
*) buf
;
2694 fc
= le_to_host16(hdr
->frame_control
);
2696 os_memset(&event
, 0, sizeof(event
));
2697 event
.tx_status
.type
= WLAN_FC_GET_TYPE(fc
);
2698 event
.tx_status
.stype
= WLAN_FC_GET_STYPE(fc
);
2699 event
.tx_status
.dst
= hdr
->addr1
;
2700 event
.tx_status
.data
= buf
;
2701 event
.tx_status
.data_len
= len
;
2702 event
.tx_status
.ack
= ok
;
2703 wpa_supplicant_event(ctx
, EVENT_TX_STATUS
, &event
);
2707 static void from_unknown_sta(struct wpa_driver_nl80211_data
*drv
,
2708 u8
*buf
, size_t len
)
2710 union wpa_event_data event
;
2711 os_memset(&event
, 0, sizeof(event
));
2712 event
.rx_from_unknown
.frame
= buf
;
2713 event
.rx_from_unknown
.len
= len
;
2714 wpa_supplicant_event(drv
->ctx
, EVENT_RX_FROM_UNKNOWN
, &event
);
2718 static void handle_frame(struct wpa_driver_nl80211_data
*drv
,
2719 u8
*buf
, size_t len
, int datarate
, int ssi_signal
)
2721 struct ieee80211_hdr
*hdr
;
2723 union wpa_event_data event
;
2725 hdr
= (struct ieee80211_hdr
*) buf
;
2726 fc
= le_to_host16(hdr
->frame_control
);
2728 switch (WLAN_FC_GET_TYPE(fc
)) {
2729 case WLAN_FC_TYPE_MGMT
:
2730 os_memset(&event
, 0, sizeof(event
));
2731 event
.rx_mgmt
.frame
= buf
;
2732 event
.rx_mgmt
.frame_len
= len
;
2733 event
.rx_mgmt
.datarate
= datarate
;
2734 event
.rx_mgmt
.ssi_signal
= ssi_signal
;
2735 wpa_supplicant_event(drv
->ctx
, EVENT_RX_MGMT
, &event
);
2737 case WLAN_FC_TYPE_CTRL
:
2738 /* can only get here with PS-Poll frames */
2739 wpa_printf(MSG_DEBUG
, "CTRL");
2740 from_unknown_sta(drv
, buf
, len
);
2742 case WLAN_FC_TYPE_DATA
:
2743 from_unknown_sta(drv
, buf
, len
);
2749 static void handle_monitor_read(int sock
, void *eloop_ctx
, void *sock_ctx
)
2751 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
2753 unsigned char buf
[3000];
2754 struct ieee80211_radiotap_iterator iter
;
2756 int datarate
= 0, ssi_signal
= 0;
2757 int injected
= 0, failed
= 0, rxflags
= 0;
2759 len
= recv(sock
, buf
, sizeof(buf
), 0);
2765 if (drv
->nlmode
== NL80211_IFTYPE_STATION
&& !drv
->probe_req_report
) {
2766 wpa_printf(MSG_DEBUG
, "nl80211: Ignore monitor interface "
2767 "frame since Probe Request reporting is disabled");
2771 if (ieee80211_radiotap_iterator_init(&iter
, (void*)buf
, len
)) {
2772 printf("received invalid radiotap frame\n");
2777 ret
= ieee80211_radiotap_iterator_next(&iter
);
2781 printf("received invalid radiotap frame (%d)\n", ret
);
2784 switch (iter
.this_arg_index
) {
2785 case IEEE80211_RADIOTAP_FLAGS
:
2786 if (*iter
.this_arg
& IEEE80211_RADIOTAP_F_FCS
)
2789 case IEEE80211_RADIOTAP_RX_FLAGS
:
2792 case IEEE80211_RADIOTAP_TX_FLAGS
:
2794 failed
= le_to_host16((*(uint16_t *) iter
.this_arg
)) &
2795 IEEE80211_RADIOTAP_F_TX_FAIL
;
2797 case IEEE80211_RADIOTAP_DATA_RETRIES
:
2799 case IEEE80211_RADIOTAP_CHANNEL
:
2800 /* TODO: convert from freq/flags to channel number */
2802 case IEEE80211_RADIOTAP_RATE
:
2803 datarate
= *iter
.this_arg
* 5;
2805 case IEEE80211_RADIOTAP_DB_ANTSIGNAL
:
2806 ssi_signal
= *iter
.this_arg
;
2811 if (rxflags
&& injected
)
2815 handle_frame(drv
, buf
+ iter
.max_length
,
2816 len
- iter
.max_length
, datarate
, ssi_signal
);
2818 handle_tx_callback(drv
->ctx
, buf
+ iter
.max_length
,
2819 len
- iter
.max_length
, !failed
);
2824 * we post-process the filter code later and rewrite
2825 * this to the offset to the last instruction
2830 static struct sock_filter msock_filter_insns
[] = {
2832 * do a little-endian load of the radiotap length field
2834 /* load lower byte into A */
2835 BPF_STMT(BPF_LD
| BPF_B
| BPF_ABS
, 2),
2836 /* put it into X (== index register) */
2837 BPF_STMT(BPF_MISC
| BPF_TAX
, 0),
2838 /* load upper byte into A */
2839 BPF_STMT(BPF_LD
| BPF_B
| BPF_ABS
, 3),
2840 /* left-shift it by 8 */
2841 BPF_STMT(BPF_ALU
| BPF_LSH
| BPF_K
, 8),
2843 BPF_STMT(BPF_ALU
| BPF_OR
| BPF_X
, 0),
2844 /* put result into X */
2845 BPF_STMT(BPF_MISC
| BPF_TAX
, 0),
2848 * Allow management frames through, this also gives us those
2849 * management frames that we sent ourselves with status
2851 /* load the lower byte of the IEEE 802.11 frame control field */
2852 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
2853 /* mask off frame type and version */
2854 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0xF),
2855 /* accept frame if it's both 0, fall through otherwise */
2856 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 0, PASS
, 0),
2859 * TODO: add a bit to radiotap RX flags that indicates
2860 * that the sending station is not associated, then
2861 * add a filter here that filters on our DA and that flag
2862 * to allow us to deauth frames to that bad station.
2864 * Not a regression -- we didn't do it before either.
2869 * drop non-data frames
2871 /* load the lower byte of the frame control field */
2872 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
2873 /* mask off QoS bit */
2874 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0x0c),
2875 /* drop non-data frames */
2876 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 8, 0, FAIL
),
2878 /* load the upper byte of the frame control field */
2879 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 1),
2880 /* mask off toDS/fromDS */
2881 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0x03),
2882 /* accept WDS frames */
2883 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 3, PASS
, 0),
2886 * add header length to index
2888 /* load the lower byte of the frame control field */
2889 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
2890 /* mask off QoS bit */
2891 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0x80),
2892 /* right shift it by 6 to give 0 or 2 */
2893 BPF_STMT(BPF_ALU
| BPF_RSH
| BPF_K
, 6),
2894 /* add data frame header length */
2895 BPF_STMT(BPF_ALU
| BPF_ADD
| BPF_K
, 24),
2896 /* add index, was start of 802.11 header */
2897 BPF_STMT(BPF_ALU
| BPF_ADD
| BPF_X
, 0),
2898 /* move to index, now start of LL header */
2899 BPF_STMT(BPF_MISC
| BPF_TAX
, 0),
2902 * Accept empty data frames, we use those for
2905 BPF_STMT(BPF_LD
| BPF_W
| BPF_LEN
, 0),
2906 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_X
, 0, PASS
, 0),
2909 * Accept EAPOL frames
2911 BPF_STMT(BPF_LD
| BPF_W
| BPF_IND
, 0),
2912 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 0xAAAA0300, 0, FAIL
),
2913 BPF_STMT(BPF_LD
| BPF_W
| BPF_IND
, 4),
2914 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 0x0000888E, PASS
, FAIL
),
2916 /* keep these last two statements or change the code below */
2917 /* return 0 == "DROP" */
2918 BPF_STMT(BPF_RET
| BPF_K
, 0),
2919 /* return ~0 == "keep all" */
2920 BPF_STMT(BPF_RET
| BPF_K
, ~0),
2923 static struct sock_fprog msock_filter
= {
2924 .len
= sizeof(msock_filter_insns
)/sizeof(msock_filter_insns
[0]),
2925 .filter
= msock_filter_insns
,
2929 static int add_monitor_filter(int s
)
2933 /* rewrite all PASS/FAIL jump offsets */
2934 for (idx
= 0; idx
< msock_filter
.len
; idx
++) {
2935 struct sock_filter
*insn
= &msock_filter_insns
[idx
];
2937 if (BPF_CLASS(insn
->code
) == BPF_JMP
) {
2938 if (insn
->code
== (BPF_JMP
|BPF_JA
)) {
2939 if (insn
->k
== PASS
)
2940 insn
->k
= msock_filter
.len
- idx
- 2;
2941 else if (insn
->k
== FAIL
)
2942 insn
->k
= msock_filter
.len
- idx
- 3;
2945 if (insn
->jt
== PASS
)
2946 insn
->jt
= msock_filter
.len
- idx
- 2;
2947 else if (insn
->jt
== FAIL
)
2948 insn
->jt
= msock_filter
.len
- idx
- 3;
2950 if (insn
->jf
== PASS
)
2951 insn
->jf
= msock_filter
.len
- idx
- 2;
2952 else if (insn
->jf
== FAIL
)
2953 insn
->jf
= msock_filter
.len
- idx
- 3;
2957 if (setsockopt(s
, SOL_SOCKET
, SO_ATTACH_FILTER
,
2958 &msock_filter
, sizeof(msock_filter
))) {
2959 perror("SO_ATTACH_FILTER");
2967 static void nl80211_remove_monitor_interface(
2968 struct wpa_driver_nl80211_data
*drv
)
2970 if (drv
->monitor_ifidx
>= 0) {
2971 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
2972 drv
->monitor_ifidx
= -1;
2974 if (drv
->monitor_sock
>= 0) {
2975 eloop_unregister_read_sock(drv
->monitor_sock
);
2976 close(drv
->monitor_sock
);
2977 drv
->monitor_sock
= -1;
2983 nl80211_create_monitor_interface(struct wpa_driver_nl80211_data
*drv
)
2986 struct sockaddr_ll ll
;
2990 snprintf(buf
, IFNAMSIZ
, "mon.%s", drv
->ifname
);
2991 buf
[IFNAMSIZ
- 1] = '\0';
2993 drv
->monitor_ifidx
=
2994 nl80211_create_iface(drv
, buf
, NL80211_IFTYPE_MONITOR
, NULL
,
2997 if (drv
->monitor_ifidx
< 0)
3000 if (linux_set_iface_flags(drv
->ioctl_sock
, buf
, 1))
3003 memset(&ll
, 0, sizeof(ll
));
3004 ll
.sll_family
= AF_PACKET
;
3005 ll
.sll_ifindex
= drv
->monitor_ifidx
;
3006 drv
->monitor_sock
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
));
3007 if (drv
->monitor_sock
< 0) {
3008 perror("socket[PF_PACKET,SOCK_RAW]");
3012 if (add_monitor_filter(drv
->monitor_sock
)) {
3013 wpa_printf(MSG_INFO
, "Failed to set socket filter for monitor "
3014 "interface; do filtering in user space");
3015 /* This works, but will cost in performance. */
3018 if (bind(drv
->monitor_sock
, (struct sockaddr
*) &ll
, sizeof(ll
)) < 0) {
3019 perror("monitor socket bind");
3023 optlen
= sizeof(optval
);
3026 (drv
->monitor_sock
, SOL_SOCKET
, SO_PRIORITY
, &optval
, optlen
)) {
3027 perror("Failed to set socket priority");
3031 if (eloop_register_read_sock(drv
->monitor_sock
, handle_monitor_read
,
3033 printf("Could not register monitor read socket\n");
3039 nl80211_remove_monitor_interface(drv
);
3044 static const u8 rfc1042_header
[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
3046 static int wpa_driver_nl80211_hapd_send_eapol(
3047 void *priv
, const u8
*addr
, const u8
*data
,
3048 size_t data_len
, int encrypt
, const u8
*own_addr
)
3050 struct wpa_driver_nl80211_data
*drv
= priv
;
3051 struct ieee80211_hdr
*hdr
;
3056 int qos
= sta
->flags
& WPA_STA_WMM
;
3061 len
= sizeof(*hdr
) + (qos
? 2 : 0) + sizeof(rfc1042_header
) + 2 +
3063 hdr
= os_zalloc(len
);
3065 printf("malloc() failed for i802_send_data(len=%lu)\n",
3066 (unsigned long) len
);
3070 hdr
->frame_control
=
3071 IEEE80211_FC(WLAN_FC_TYPE_DATA
, WLAN_FC_STYPE_DATA
);
3072 hdr
->frame_control
|= host_to_le16(WLAN_FC_FROMDS
);
3074 hdr
->frame_control
|= host_to_le16(WLAN_FC_ISWEP
);
3075 #if 0 /* To be enabled if qos determination is added above */
3077 hdr
->frame_control
|=
3078 host_to_le16(WLAN_FC_STYPE_QOS_DATA
<< 4);
3082 memcpy(hdr
->IEEE80211_DA_FROMDS
, addr
, ETH_ALEN
);
3083 memcpy(hdr
->IEEE80211_BSSID_FROMDS
, own_addr
, ETH_ALEN
);
3084 memcpy(hdr
->IEEE80211_SA_FROMDS
, own_addr
, ETH_ALEN
);
3085 pos
= (u8
*) (hdr
+ 1);
3087 #if 0 /* To be enabled if qos determination is added above */
3089 /* add an empty QoS header if needed */
3096 memcpy(pos
, rfc1042_header
, sizeof(rfc1042_header
));
3097 pos
+= sizeof(rfc1042_header
);
3098 WPA_PUT_BE16(pos
, ETH_P_PAE
);
3100 memcpy(pos
, data
, data_len
);
3102 res
= wpa_driver_nl80211_send_frame(drv
, (u8
*) hdr
, len
, encrypt
);
3104 wpa_printf(MSG_ERROR
, "i802_send_eapol - packet len: %lu - "
3106 (unsigned long) len
, errno
, strerror(errno
));
3114 static u32
sta_flags_nl80211(int flags
)
3118 if (flags
& WPA_STA_AUTHORIZED
)
3119 f
|= BIT(NL80211_STA_FLAG_AUTHORIZED
);
3120 if (flags
& WPA_STA_WMM
)
3121 f
|= BIT(NL80211_STA_FLAG_WME
);
3122 if (flags
& WPA_STA_SHORT_PREAMBLE
)
3123 f
|= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
);
3124 if (flags
& WPA_STA_MFP
)
3125 f
|= BIT(NL80211_STA_FLAG_MFP
);
3131 static int wpa_driver_nl80211_sta_set_flags(void *priv
, const u8
*addr
,
3132 int total_flags
, int flags_or
,
3135 struct wpa_driver_nl80211_data
*drv
= priv
;
3136 struct nl_msg
*msg
, *flags
= NULL
;
3137 struct nl80211_sta_flag_update upd
;
3139 msg
= nlmsg_alloc();
3143 flags
= nlmsg_alloc();
3149 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3150 0, NL80211_CMD_SET_STATION
, 0);
3152 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3153 if_nametoindex(drv
->ifname
));
3154 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
3157 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
3158 * can be removed eventually.
3160 if (total_flags
& WPA_STA_AUTHORIZED
)
3161 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_AUTHORIZED
);
3163 if (total_flags
& WPA_STA_WMM
)
3164 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_WME
);
3166 if (total_flags
& WPA_STA_SHORT_PREAMBLE
)
3167 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_SHORT_PREAMBLE
);
3169 if (total_flags
& WPA_STA_MFP
)
3170 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_MFP
);
3172 if (nla_put_nested(msg
, NL80211_ATTR_STA_FLAGS
, flags
))
3173 goto nla_put_failure
;
3175 os_memset(&upd
, 0, sizeof(upd
));
3176 upd
.mask
= sta_flags_nl80211(flags_or
| ~flags_and
);
3177 upd
.set
= sta_flags_nl80211(flags_or
);
3178 NLA_PUT(msg
, NL80211_ATTR_STA_FLAGS2
, sizeof(upd
), &upd
);
3182 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3189 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data
*drv
,
3190 struct wpa_driver_associate_params
*params
)
3192 if (wpa_driver_nl80211_set_mode(drv
, params
->mode
) ||
3193 wpa_driver_nl80211_set_freq(drv
, params
->freq
, 0, 0)) {
3194 nl80211_remove_monitor_interface(drv
);
3198 /* TODO: setup monitor interface (and add code somewhere to remove this
3199 * when AP mode is stopped; associate with mode != 2 or drv_deinit) */
3205 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data
*drv
)
3210 msg
= nlmsg_alloc();
3214 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
3215 NL80211_CMD_LEAVE_IBSS
, 0);
3216 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
3217 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3220 wpa_printf(MSG_DEBUG
, "nl80211: Leave IBSS failed: ret=%d "
3221 "(%s)", ret
, strerror(-ret
));
3222 goto nla_put_failure
;
3226 wpa_printf(MSG_DEBUG
, "nl80211: Leave IBSS request sent successfully");
3234 static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data
*drv
,
3235 struct wpa_driver_associate_params
*params
)
3241 wpa_printf(MSG_DEBUG
, "nl80211: Join IBSS (ifindex=%d)", drv
->ifindex
);
3243 if (wpa_driver_nl80211_set_mode(drv
, params
->mode
)) {
3244 wpa_printf(MSG_INFO
, "nl80211: Failed to set interface into "
3250 msg
= nlmsg_alloc();
3254 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
3255 NL80211_CMD_JOIN_IBSS
, 0);
3256 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
3258 if (params
->ssid
== NULL
|| params
->ssid_len
> sizeof(drv
->ssid
))
3259 goto nla_put_failure
;
3261 wpa_hexdump_ascii(MSG_DEBUG
, " * SSID",
3262 params
->ssid
, params
->ssid_len
);
3263 NLA_PUT(msg
, NL80211_ATTR_SSID
, params
->ssid_len
,
3265 os_memcpy(drv
->ssid
, params
->ssid
, params
->ssid_len
);
3266 drv
->ssid_len
= params
->ssid_len
;
3268 wpa_printf(MSG_DEBUG
, " * freq=%d", params
->freq
);
3269 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, params
->freq
);
3271 ret
= nl80211_set_conn_keys(params
, msg
);
3273 goto nla_put_failure
;
3275 if (params
->wpa_ie
) {
3276 wpa_hexdump(MSG_DEBUG
,
3277 " * Extra IEs for Beacon/Probe Response frames",
3278 params
->wpa_ie
, params
->wpa_ie_len
);
3279 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->wpa_ie_len
,
3283 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3286 wpa_printf(MSG_DEBUG
, "nl80211: Join IBSS failed: ret=%d (%s)",
3287 ret
, strerror(-ret
));
3289 if (ret
== -EALREADY
&& count
== 1) {
3290 wpa_printf(MSG_DEBUG
, "nl80211: Retry IBSS join after "
3292 nl80211_leave_ibss(drv
);
3297 goto nla_put_failure
;
3300 wpa_printf(MSG_DEBUG
, "nl80211: Join IBSS request sent successfully");
3308 static int wpa_driver_nl80211_connect(
3309 struct wpa_driver_nl80211_data
*drv
,
3310 struct wpa_driver_associate_params
*params
)
3313 enum nl80211_auth_type type
;
3316 msg
= nlmsg_alloc();
3320 wpa_printf(MSG_DEBUG
, "nl80211: Connect (ifindex=%d)", drv
->ifindex
);
3321 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
3322 NL80211_CMD_CONNECT
, 0);
3324 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
3325 if (params
->bssid
) {
3326 wpa_printf(MSG_DEBUG
, " * bssid=" MACSTR
,
3327 MAC2STR(params
->bssid
));
3328 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, params
->bssid
);
3331 wpa_printf(MSG_DEBUG
, " * freq=%d", params
->freq
);
3332 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, params
->freq
);
3335 wpa_hexdump_ascii(MSG_DEBUG
, " * SSID",
3336 params
->ssid
, params
->ssid_len
);
3337 NLA_PUT(msg
, NL80211_ATTR_SSID
, params
->ssid_len
,
3339 if (params
->ssid_len
> sizeof(drv
->ssid
))
3340 goto nla_put_failure
;
3341 os_memcpy(drv
->ssid
, params
->ssid
, params
->ssid_len
);
3342 drv
->ssid_len
= params
->ssid_len
;
3344 wpa_hexdump(MSG_DEBUG
, " * IEs", params
->wpa_ie
, params
->wpa_ie_len
);
3346 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->wpa_ie_len
,
3349 if (params
->auth_alg
& WPA_AUTH_ALG_OPEN
)
3350 type
= NL80211_AUTHTYPE_OPEN_SYSTEM
;
3351 else if (params
->auth_alg
& WPA_AUTH_ALG_SHARED
)
3352 type
= NL80211_AUTHTYPE_SHARED_KEY
;
3353 else if (params
->auth_alg
& WPA_AUTH_ALG_LEAP
)
3354 type
= NL80211_AUTHTYPE_NETWORK_EAP
;
3355 else if (params
->auth_alg
& WPA_AUTH_ALG_FT
)
3356 type
= NL80211_AUTHTYPE_FT
;
3358 goto nla_put_failure
;
3360 wpa_printf(MSG_DEBUG
, " * Auth Type %d", type
);
3361 NLA_PUT_U32(msg
, NL80211_ATTR_AUTH_TYPE
, type
);
3363 if (params
->wpa_ie
&& params
->wpa_ie_len
) {
3364 enum nl80211_wpa_versions ver
;
3366 if (params
->wpa_ie
[0] == WLAN_EID_RSN
)
3367 ver
= NL80211_WPA_VERSION_2
;
3369 ver
= NL80211_WPA_VERSION_1
;
3371 wpa_printf(MSG_DEBUG
, " * WPA Version %d", ver
);
3372 NLA_PUT_U32(msg
, NL80211_ATTR_WPA_VERSIONS
, ver
);
3375 if (params
->pairwise_suite
!= CIPHER_NONE
) {
3378 switch (params
->pairwise_suite
) {
3380 cipher
= WLAN_CIPHER_SUITE_WEP40
;
3383 cipher
= WLAN_CIPHER_SUITE_WEP104
;
3386 cipher
= WLAN_CIPHER_SUITE_CCMP
;
3390 cipher
= WLAN_CIPHER_SUITE_TKIP
;
3393 NLA_PUT_U32(msg
, NL80211_ATTR_CIPHER_SUITES_PAIRWISE
, cipher
);
3396 if (params
->group_suite
!= CIPHER_NONE
) {
3399 switch (params
->group_suite
) {
3401 cipher
= WLAN_CIPHER_SUITE_WEP40
;
3404 cipher
= WLAN_CIPHER_SUITE_WEP104
;
3407 cipher
= WLAN_CIPHER_SUITE_CCMP
;
3411 cipher
= WLAN_CIPHER_SUITE_TKIP
;
3414 NLA_PUT_U32(msg
, NL80211_ATTR_CIPHER_SUITE_GROUP
, cipher
);
3417 if (params
->key_mgmt_suite
== KEY_MGMT_802_1X
||
3418 params
->key_mgmt_suite
== KEY_MGMT_PSK
) {
3419 int mgmt
= WLAN_AKM_SUITE_PSK
;
3421 switch (params
->key_mgmt_suite
) {
3422 case KEY_MGMT_802_1X
:
3423 mgmt
= WLAN_AKM_SUITE_8021X
;
3427 mgmt
= WLAN_AKM_SUITE_PSK
;
3430 NLA_PUT_U32(msg
, NL80211_ATTR_AKM_SUITES
, mgmt
);
3433 ret
= nl80211_set_conn_keys(params
, msg
);
3435 goto nla_put_failure
;
3437 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3440 wpa_printf(MSG_DEBUG
, "nl80211: MLME connect failed: ret=%d "
3441 "(%s)", ret
, strerror(-ret
));
3442 goto nla_put_failure
;
3445 wpa_printf(MSG_DEBUG
, "nl80211: Connect request send successfully");
3454 static int wpa_driver_nl80211_associate(
3455 void *priv
, struct wpa_driver_associate_params
*params
)
3457 struct wpa_driver_nl80211_data
*drv
= priv
;
3461 if (params
->mode
== IEEE80211_MODE_AP
)
3462 return wpa_driver_nl80211_ap(drv
, params
);
3464 if (params
->mode
== IEEE80211_MODE_IBSS
)
3465 return wpa_driver_nl80211_ibss(drv
, params
);
3467 if (!(drv
->capa
.flags
& WPA_DRIVER_FLAGS_SME
)) {
3468 if (wpa_driver_nl80211_set_mode(drv
, params
->mode
) < 0)
3470 return wpa_driver_nl80211_connect(drv
, params
);
3473 drv
->associated
= 0;
3475 msg
= nlmsg_alloc();
3479 wpa_printf(MSG_DEBUG
, "nl80211: Associate (ifindex=%d)",
3481 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
3482 NL80211_CMD_ASSOCIATE
, 0);
3484 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
3485 if (params
->bssid
) {
3486 wpa_printf(MSG_DEBUG
, " * bssid=" MACSTR
,
3487 MAC2STR(params
->bssid
));
3488 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, params
->bssid
);
3491 wpa_printf(MSG_DEBUG
, " * freq=%d", params
->freq
);
3492 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, params
->freq
);
3493 drv
->assoc_freq
= params
->freq
;
3495 drv
->assoc_freq
= 0;
3497 wpa_hexdump_ascii(MSG_DEBUG
, " * SSID",
3498 params
->ssid
, params
->ssid_len
);
3499 NLA_PUT(msg
, NL80211_ATTR_SSID
, params
->ssid_len
,
3501 if (params
->ssid_len
> sizeof(drv
->ssid
))
3502 goto nla_put_failure
;
3503 os_memcpy(drv
->ssid
, params
->ssid
, params
->ssid_len
);
3504 drv
->ssid_len
= params
->ssid_len
;
3506 wpa_hexdump(MSG_DEBUG
, " * IEs", params
->wpa_ie
, params
->wpa_ie_len
);
3508 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->wpa_ie_len
,
3511 #ifdef CONFIG_IEEE80211W
3512 if (params
->mgmt_frame_protection
== MGMT_FRAME_PROTECTION_REQUIRED
)
3513 NLA_PUT_U32(msg
, NL80211_ATTR_USE_MFP
, NL80211_MFP_REQUIRED
);
3514 #endif /* CONFIG_IEEE80211W */
3516 NLA_PUT_FLAG(msg
, NL80211_ATTR_CONTROL_PORT
);
3518 if (params
->prev_bssid
) {
3519 wpa_printf(MSG_DEBUG
, " * prev_bssid=" MACSTR
,
3520 MAC2STR(params
->prev_bssid
));
3521 NLA_PUT(msg
, NL80211_ATTR_PREV_BSSID
, ETH_ALEN
,
3522 params
->prev_bssid
);
3525 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3528 wpa_printf(MSG_DEBUG
, "nl80211: MLME command failed: ret=%d "
3529 "(%s)", ret
, strerror(-ret
));
3530 goto nla_put_failure
;
3533 wpa_printf(MSG_DEBUG
, "nl80211: Association request send "
3542 static int nl80211_set_mode(struct wpa_driver_nl80211_data
*drv
,
3543 int ifindex
, int mode
)
3548 msg
= nlmsg_alloc();
3552 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3553 0, NL80211_CMD_SET_INTERFACE
, 0);
3554 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
3555 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, mode
);
3557 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3561 wpa_printf(MSG_DEBUG
, "nl80211: Failed to set interface %d to mode %d:"
3562 " %d (%s)", ifindex
, mode
, ret
, strerror(-ret
));
3567 static int wpa_driver_nl80211_set_mode(void *priv
, int mode
)
3569 struct wpa_driver_nl80211_data
*drv
= priv
;
3575 nlmode
= NL80211_IFTYPE_STATION
;
3578 nlmode
= NL80211_IFTYPE_ADHOC
;
3581 nlmode
= NL80211_IFTYPE_AP
;
3587 if (nl80211_set_mode(drv
, drv
->ifindex
, nlmode
) == 0) {
3588 drv
->nlmode
= nlmode
;
3593 if (nlmode
== drv
->nlmode
) {
3594 wpa_printf(MSG_DEBUG
, "nl80211: Interface already in "
3595 "requested mode - ignore error");
3597 goto done
; /* Already in the requested mode */
3600 /* mac80211 doesn't allow mode changes while the device is up, so
3601 * take the device down, try to set the mode again, and bring the
3604 if (linux_set_iface_flags(drv
->ioctl_sock
, drv
->ifname
, 0) == 0) {
3605 /* Try to set the mode again while the interface is down */
3606 ret
= nl80211_set_mode(drv
, drv
->ifindex
, nlmode
);
3607 if (linux_set_iface_flags(drv
->ioctl_sock
, drv
->ifname
, 1))
3612 wpa_printf(MSG_DEBUG
, "nl80211: Mode change succeeded while "
3613 "interface is down");
3614 drv
->nlmode
= nlmode
;
3618 if (!ret
&& nlmode
== NL80211_IFTYPE_AP
) {
3619 /* Setup additional AP mode functionality if needed */
3620 if (drv
->monitor_ifidx
< 0 &&
3621 nl80211_create_monitor_interface(drv
))
3623 } else if (!ret
&& nlmode
!= NL80211_IFTYPE_AP
) {
3624 /* Remove additional AP mode functionality */
3625 nl80211_remove_monitor_interface(drv
);
3629 wpa_printf(MSG_DEBUG
, "nl80211: Interface mode change to %d "
3630 "from %d failed", nlmode
, drv
->nlmode
);
3636 static int wpa_driver_nl80211_get_capa(void *priv
,
3637 struct wpa_driver_capa
*capa
)
3639 struct wpa_driver_nl80211_data
*drv
= priv
;
3640 if (!drv
->has_capability
)
3642 os_memcpy(capa
, &drv
->capa
, sizeof(*capa
));
3647 static int wpa_driver_nl80211_set_operstate(void *priv
, int state
)
3649 struct wpa_driver_nl80211_data
*drv
= priv
;
3651 wpa_printf(MSG_DEBUG
, "%s: operstate %d->%d (%s)",
3652 __func__
, drv
->operstate
, state
, state
? "UP" : "DORMANT");
3653 drv
->operstate
= state
;
3654 return netlink_send_oper_ifla(drv
->netlink
, drv
->ifindex
, -1,
3655 state
? IF_OPER_UP
: IF_OPER_DORMANT
);
3659 static int wpa_driver_nl80211_set_supp_port(void *priv
, int authorized
)
3661 struct wpa_driver_nl80211_data
*drv
= priv
;
3663 struct nl80211_sta_flag_update upd
;
3665 msg
= nlmsg_alloc();
3669 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3670 0, NL80211_CMD_SET_STATION
, 0);
3672 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3673 if_nametoindex(drv
->ifname
));
3674 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, drv
->bssid
);
3676 os_memset(&upd
, 0, sizeof(upd
));
3677 upd
.mask
= BIT(NL80211_STA_FLAG_AUTHORIZED
);
3679 upd
.set
= BIT(NL80211_STA_FLAG_AUTHORIZED
);
3680 NLA_PUT(msg
, NL80211_ATTR_STA_FLAGS2
, sizeof(upd
), &upd
);
3682 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3690 static struct i802_bss
* get_bss(struct wpa_driver_nl80211_data
*drv
,
3693 struct i802_bss
*bss
= &drv
->bss
;
3695 if (ifindex
== bss
->ifindex
)
3699 wpa_printf(MSG_DEBUG
, "nl80211: get_bss(%d) failed", ifindex
);
3704 static void add_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
)
3709 wpa_printf(MSG_DEBUG
, "nl80211: Add own interface ifindex %d",
3711 for (i
= 0; i
< drv
->num_if_indices
; i
++) {
3712 if (drv
->if_indices
[i
] == 0) {
3713 drv
->if_indices
[i
] = ifidx
;
3718 if (drv
->if_indices
!= drv
->default_if_indices
)
3719 old
= drv
->if_indices
;
3723 drv
->if_indices
= os_realloc(old
,
3724 sizeof(int) * (drv
->num_if_indices
+ 1));
3725 if (!drv
->if_indices
) {
3727 drv
->if_indices
= drv
->default_if_indices
;
3729 drv
->if_indices
= old
;
3730 wpa_printf(MSG_ERROR
, "Failed to reallocate memory for "
3732 wpa_printf(MSG_ERROR
, "Ignoring EAPOL on interface %d", ifidx
);
3735 drv
->if_indices
[drv
->num_if_indices
] = ifidx
;
3736 drv
->num_if_indices
++;
3740 static void del_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
)
3744 for (i
= 0; i
< drv
->num_if_indices
; i
++) {
3745 if (drv
->if_indices
[i
] == ifidx
) {
3746 drv
->if_indices
[i
] = 0;
3753 static int have_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
)
3757 for (i
= 0; i
< drv
->num_if_indices
; i
++)
3758 if (drv
->if_indices
[i
] == ifidx
)
3765 static inline int min_int(int a
, int b
)
3773 static int get_key_handler(struct nl_msg
*msg
, void *arg
)
3775 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
3776 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
3778 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
3779 genlmsg_attrlen(gnlh
, 0), NULL
);
3782 * TODO: validate the key index and mac address!
3783 * Otherwise, there's a race condition as soon as
3784 * the kernel starts sending key notifications.
3787 if (tb
[NL80211_ATTR_KEY_SEQ
])
3788 memcpy(arg
, nla_data(tb
[NL80211_ATTR_KEY_SEQ
]),
3789 min_int(nla_len(tb
[NL80211_ATTR_KEY_SEQ
]), 6));
3794 static int i802_get_seqnum(const char *iface
, void *priv
, const u8
*addr
,
3797 struct wpa_driver_nl80211_data
*drv
= priv
;
3800 msg
= nlmsg_alloc();
3804 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3805 0, NL80211_CMD_GET_KEY
, 0);
3808 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
3809 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, idx
);
3810 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(iface
));
3814 return send_and_recv_msgs(drv
, msg
, get_key_handler
, seq
);
3820 static int i802_set_rate_sets(void *priv
, int *supp_rates
, int *basic_rates
,
3823 struct wpa_driver_nl80211_data
*drv
= priv
;
3825 u8 rates
[NL80211_MAX_SUPP_RATES
];
3829 msg
= nlmsg_alloc();
3833 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
3834 NL80211_CMD_SET_BSS
, 0);
3836 for (i
= 0; i
< NL80211_MAX_SUPP_RATES
&& basic_rates
[i
] >= 0; i
++)
3837 rates
[rates_len
++] = basic_rates
[i
] / 5;
3839 NLA_PUT(msg
, NL80211_ATTR_BSS_BASIC_RATES
, rates_len
, rates
);
3841 /* TODO: multi-BSS support */
3842 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
3844 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3850 /* Set kernel driver on given frequency (MHz) */
3851 static int i802_set_freq(void *priv
, struct hostapd_freq_params
*freq
)
3853 struct wpa_driver_nl80211_data
*drv
= priv
;
3854 return wpa_driver_nl80211_set_freq(drv
, freq
->freq
, freq
->ht_enabled
,
3855 freq
->sec_channel_offset
);
3859 static int i802_set_rts(void *priv
, int rts
)
3861 struct wpa_driver_nl80211_data
*drv
= priv
;
3866 msg
= nlmsg_alloc();
3875 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3876 0, NL80211_CMD_SET_WIPHY
, 0);
3877 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
3878 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_RTS_THRESHOLD
, val
);
3880 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3884 wpa_printf(MSG_DEBUG
, "nl80211: Failed to set RTS threshold %d: "
3885 "%d (%s)", rts
, ret
, strerror(-ret
));
3890 static int i802_set_frag(void *priv
, int frag
)
3892 struct wpa_driver_nl80211_data
*drv
= priv
;
3897 msg
= nlmsg_alloc();
3906 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3907 0, NL80211_CMD_SET_WIPHY
, 0);
3908 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
3909 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FRAG_THRESHOLD
, val
);
3911 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3915 wpa_printf(MSG_DEBUG
, "nl80211: Failed to set fragmentation threshold "
3916 "%d: %d (%s)", frag
, ret
, strerror(-ret
));
3921 static int i802_flush(void *priv
)
3923 struct wpa_driver_nl80211_data
*drv
= priv
;
3926 msg
= nlmsg_alloc();
3930 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3931 0, NL80211_CMD_DEL_STATION
, 0);
3934 * XXX: FIX! this needs to flush all VLANs too
3936 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3937 if_nametoindex(drv
->ifname
));
3939 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3945 static int get_sta_handler(struct nl_msg
*msg
, void *arg
)
3947 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
3948 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
3949 struct hostap_sta_driver_data
*data
= arg
;
3950 struct nlattr
*stats
[NL80211_STA_INFO_MAX
+ 1];
3951 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
3952 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
3953 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
3954 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
3955 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
3956 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
3959 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
3960 genlmsg_attrlen(gnlh
, 0), NULL
);
3963 * TODO: validate the interface and mac address!
3964 * Otherwise, there's a race condition as soon as
3965 * the kernel starts sending station notifications.
3968 if (!tb
[NL80211_ATTR_STA_INFO
]) {
3969 wpa_printf(MSG_DEBUG
, "sta stats missing!");
3972 if (nla_parse_nested(stats
, NL80211_STA_INFO_MAX
,
3973 tb
[NL80211_ATTR_STA_INFO
],
3975 wpa_printf(MSG_DEBUG
, "failed to parse nested attributes!");
3979 if (stats
[NL80211_STA_INFO_INACTIVE_TIME
])
3980 data
->inactive_msec
=
3981 nla_get_u32(stats
[NL80211_STA_INFO_INACTIVE_TIME
]);
3982 if (stats
[NL80211_STA_INFO_RX_BYTES
])
3983 data
->rx_bytes
= nla_get_u32(stats
[NL80211_STA_INFO_RX_BYTES
]);
3984 if (stats
[NL80211_STA_INFO_TX_BYTES
])
3985 data
->tx_bytes
= nla_get_u32(stats
[NL80211_STA_INFO_TX_BYTES
]);
3986 if (stats
[NL80211_STA_INFO_RX_PACKETS
])
3988 nla_get_u32(stats
[NL80211_STA_INFO_RX_PACKETS
]);
3989 if (stats
[NL80211_STA_INFO_TX_PACKETS
])
3991 nla_get_u32(stats
[NL80211_STA_INFO_TX_PACKETS
]);
3996 static int i802_read_sta_data(void *priv
, struct hostap_sta_driver_data
*data
,
3999 struct wpa_driver_nl80211_data
*drv
= priv
;
4002 os_memset(data
, 0, sizeof(*data
));
4003 msg
= nlmsg_alloc();
4007 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
4008 0, NL80211_CMD_GET_STATION
, 0);
4010 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
4011 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
4013 return send_and_recv_msgs(drv
, msg
, get_sta_handler
, data
);
4019 static int i802_set_tx_queue_params(void *priv
, int queue
, int aifs
,
4020 int cw_min
, int cw_max
, int burst_time
)
4022 struct wpa_driver_nl80211_data
*drv
= priv
;
4024 struct nlattr
*txq
, *params
;
4026 msg
= nlmsg_alloc();
4030 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
4031 0, NL80211_CMD_SET_WIPHY
, 0);
4033 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
4035 txq
= nla_nest_start(msg
, NL80211_ATTR_WIPHY_TXQ_PARAMS
);
4037 goto nla_put_failure
;
4039 /* We are only sending parameters for a single TXQ at a time */
4040 params
= nla_nest_start(msg
, 1);
4042 goto nla_put_failure
;
4044 NLA_PUT_U8(msg
, NL80211_TXQ_ATTR_QUEUE
, queue
);
4045 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
4046 * 32 usec, so need to convert the value here. */
4047 NLA_PUT_U16(msg
, NL80211_TXQ_ATTR_TXOP
, (burst_time
* 100 + 16) / 32);
4048 NLA_PUT_U16(msg
, NL80211_TXQ_ATTR_CWMIN
, cw_min
);
4049 NLA_PUT_U16(msg
, NL80211_TXQ_ATTR_CWMAX
, cw_max
);
4050 NLA_PUT_U8(msg
, NL80211_TXQ_ATTR_AIFS
, aifs
);
4052 nla_nest_end(msg
, params
);
4054 nla_nest_end(msg
, txq
);
4056 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
) == 0)
4063 static int i802_set_bss(void *priv
, int cts
, int preamble
, int slot
)
4065 struct wpa_driver_nl80211_data
*drv
= priv
;
4068 msg
= nlmsg_alloc();
4072 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
4073 NL80211_CMD_SET_BSS
, 0);
4076 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_CTS_PROT
, cts
);
4078 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_SHORT_PREAMBLE
, preamble
);
4080 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_SHORT_SLOT_TIME
, slot
);
4082 /* TODO: multi-BSS support */
4083 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
4085 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
4091 static int i802_set_cts_protect(void *priv
, int value
)
4093 return i802_set_bss(priv
, value
, -1, -1);
4097 static int i802_set_preamble(void *priv
, int value
)
4099 return i802_set_bss(priv
, -1, value
, -1);
4103 static int i802_set_short_slot_time(void *priv
, int value
)
4105 return i802_set_bss(priv
, -1, -1, value
);
4109 static int i802_set_sta_vlan(void *priv
, const u8
*addr
,
4110 const char *ifname
, int vlan_id
)
4112 struct wpa_driver_nl80211_data
*drv
= priv
;
4115 msg
= nlmsg_alloc();
4119 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
4120 0, NL80211_CMD_SET_STATION
, 0);
4122 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
4123 if_nametoindex(drv
->ifname
));
4124 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
4125 NLA_PUT_U32(msg
, NL80211_ATTR_STA_VLAN
,
4126 if_nametoindex(ifname
));
4128 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
4134 static int i802_set_wds_sta(void *priv
, const u8
*addr
, int aid
, int val
)
4136 struct wpa_driver_nl80211_data
*drv
= priv
;
4139 os_snprintf(name
, sizeof(name
), "%s.sta%d", drv
->ifname
, aid
);
4141 if (nl80211_create_iface(priv
, name
, NL80211_IFTYPE_AP_VLAN
,
4144 linux_set_iface_flags(drv
->ioctl_sock
, name
, 1);
4145 return i802_set_sta_vlan(priv
, addr
, name
, 0);
4147 i802_set_sta_vlan(priv
, addr
, drv
->ifname
, 0);
4148 return wpa_driver_nl80211_if_remove(priv
, WPA_IF_AP_VLAN
,
4154 static void handle_eapol(int sock
, void *eloop_ctx
, void *sock_ctx
)
4156 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
4157 struct sockaddr_ll lladdr
;
4158 unsigned char buf
[3000];
4160 socklen_t fromlen
= sizeof(lladdr
);
4162 len
= recvfrom(sock
, buf
, sizeof(buf
), 0,
4163 (struct sockaddr
*)&lladdr
, &fromlen
);
4169 if (have_ifidx(drv
, lladdr
.sll_ifindex
))
4170 drv_event_eapol_rx(drv
->ctx
, lladdr
.sll_addr
, buf
, len
);
4174 static int i802_get_inact_sec(void *priv
, const u8
*addr
)
4176 struct hostap_sta_driver_data data
;
4179 data
.inactive_msec
= (unsigned long) -1;
4180 ret
= i802_read_sta_data(priv
, &data
, addr
);
4181 if (ret
|| data
.inactive_msec
== (unsigned long) -1)
4183 return data
.inactive_msec
/ 1000;
4187 static int i802_sta_clear_stats(void *priv
, const u8
*addr
)
4196 static int i802_sta_deauth(void *priv
, const u8
*own_addr
, const u8
*addr
,
4199 struct wpa_driver_nl80211_data
*drv
= priv
;
4200 struct ieee80211_mgmt mgmt
;
4202 memset(&mgmt
, 0, sizeof(mgmt
));
4203 mgmt
.frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
4204 WLAN_FC_STYPE_DEAUTH
);
4205 memcpy(mgmt
.da
, addr
, ETH_ALEN
);
4206 memcpy(mgmt
.sa
, own_addr
, ETH_ALEN
);
4207 memcpy(mgmt
.bssid
, own_addr
, ETH_ALEN
);
4208 mgmt
.u
.deauth
.reason_code
= host_to_le16(reason
);
4209 return wpa_driver_nl80211_send_mlme(drv
, (u8
*) &mgmt
,
4211 sizeof(mgmt
.u
.deauth
));
4215 static int i802_sta_disassoc(void *priv
, const u8
*own_addr
, const u8
*addr
,
4218 struct wpa_driver_nl80211_data
*drv
= priv
;
4219 struct ieee80211_mgmt mgmt
;
4221 memset(&mgmt
, 0, sizeof(mgmt
));
4222 mgmt
.frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
4223 WLAN_FC_STYPE_DISASSOC
);
4224 memcpy(mgmt
.da
, addr
, ETH_ALEN
);
4225 memcpy(mgmt
.sa
, own_addr
, ETH_ALEN
);
4226 memcpy(mgmt
.bssid
, own_addr
, ETH_ALEN
);
4227 mgmt
.u
.disassoc
.reason_code
= host_to_le16(reason
);
4228 return wpa_driver_nl80211_send_mlme(drv
, (u8
*) &mgmt
,
4230 sizeof(mgmt
.u
.disassoc
));
4234 static void *i802_init(struct hostapd_data
*hapd
,
4235 struct wpa_init_params
*params
)
4237 struct wpa_driver_nl80211_data
*drv
;
4240 drv
= wpa_driver_nl80211_init(hapd
, params
->ifname
);
4244 drv
->bss
.ifindex
= drv
->ifindex
;
4246 drv
->num_if_indices
= sizeof(drv
->default_if_indices
) / sizeof(int);
4247 drv
->if_indices
= drv
->default_if_indices
;
4248 for (i
= 0; i
< params
->num_bridge
; i
++) {
4249 if (params
->bridge
[i
])
4250 add_ifidx(drv
, if_nametoindex(params
->bridge
[i
]));
4253 /* start listening for EAPOL on the default AP interface */
4254 add_ifidx(drv
, drv
->ifindex
);
4256 if (linux_set_iface_flags(drv
->ioctl_sock
, drv
->ifname
, 0))
4259 if (params
->bssid
) {
4260 if (linux_set_ifhwaddr(drv
->ioctl_sock
, drv
->ifname
,
4265 if (wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_AP
)) {
4266 wpa_printf(MSG_ERROR
, "nl80211: Failed to set interface %s "
4267 "into AP mode", drv
->ifname
);
4271 if (linux_set_iface_flags(drv
->ioctl_sock
, drv
->ifname
, 1))
4274 drv
->eapol_sock
= socket(PF_PACKET
, SOCK_DGRAM
, htons(ETH_P_PAE
));
4275 if (drv
->eapol_sock
< 0) {
4276 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
4280 if (eloop_register_read_sock(drv
->eapol_sock
, handle_eapol
, drv
, NULL
))
4282 printf("Could not register read socket for eapol\n");
4286 if (linux_get_ifhwaddr(drv
->ioctl_sock
, drv
->ifname
, params
->own_addr
))
4292 nl80211_remove_monitor_interface(drv
);
4293 if (drv
->ioctl_sock
>= 0)
4294 close(drv
->ioctl_sock
);
4296 genl_family_put(drv
->nl80211
);
4297 nl_cache_free(drv
->nl_cache
);
4298 nl_handle_destroy(drv
->nl_handle
);
4299 nl_cb_put(drv
->nl_cb
);
4306 static void i802_deinit(void *priv
)
4308 wpa_driver_nl80211_deinit(priv
);
4311 #endif /* HOSTAPD */
4314 static enum nl80211_iftype
wpa_driver_nl80211_if_type(
4315 enum wpa_driver_if_type type
)
4318 case WPA_IF_STATION
:
4319 return NL80211_IFTYPE_STATION
;
4320 case WPA_IF_AP_VLAN
:
4321 return NL80211_IFTYPE_AP_VLAN
;
4323 return NL80211_IFTYPE_AP
;
4329 static int wpa_driver_nl80211_if_add(const char *iface
, void *priv
,
4330 enum wpa_driver_if_type type
,
4331 const char *ifname
, const u8
*addr
,
4334 struct wpa_driver_nl80211_data
*drv
= priv
;
4337 struct i802_bss
*bss
= NULL
;
4339 if (type
== WPA_IF_AP_BSS
) {
4340 bss
= os_zalloc(sizeof(*bss
));
4344 #endif /* HOSTAPD */
4346 ifidx
= nl80211_create_iface(drv
, ifname
,
4347 wpa_driver_nl80211_if_type(type
), addr
,
4352 #endif /* HOSTAPD */
4357 if (type
== WPA_IF_AP_BSS
) {
4358 if (linux_set_iface_flags(drv
->ioctl_sock
, ifname
, 1)) {
4359 nl80211_remove_iface(drv
, ifidx
);
4363 bss
->ifindex
= ifidx
;
4364 bss
->next
= drv
->bss
.next
;
4365 drv
->bss
.next
= bss
;
4367 #endif /* HOSTAPD */
4373 static int wpa_driver_nl80211_if_remove(void *priv
,
4374 enum wpa_driver_if_type type
,
4377 struct wpa_driver_nl80211_data
*drv
= priv
;
4378 int ifindex
= if_nametoindex(ifname
);
4380 nl80211_remove_iface(drv
, ifindex
);
4383 if (type
== WPA_IF_AP_BSS
) {
4384 struct i802_bss
*bss
, *prev
;
4386 bss
= drv
->bss
.next
;
4388 if (ifindex
== bss
->ifindex
) {
4389 prev
->next
= bss
->next
;
4397 #endif /* HOSTAPD */
4403 static int cookie_handler(struct nl_msg
*msg
, void *arg
)
4405 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
4406 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
4408 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
4409 genlmsg_attrlen(gnlh
, 0), NULL
);
4410 if (tb
[NL80211_ATTR_COOKIE
])
4411 *cookie
= nla_get_u64(tb
[NL80211_ATTR_COOKIE
]);
4416 static int wpa_driver_nl80211_remain_on_channel(void *priv
, unsigned int freq
,
4417 unsigned int duration
)
4419 struct wpa_driver_nl80211_data
*drv
= priv
;
4424 msg
= nlmsg_alloc();
4428 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
4429 NL80211_CMD_REMAIN_ON_CHANNEL
, 0);
4431 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
4432 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, freq
);
4433 NLA_PUT_U32(msg
, NL80211_ATTR_DURATION
, duration
);
4436 ret
= send_and_recv_msgs(drv
, msg
, cookie_handler
, &cookie
);
4438 wpa_printf(MSG_DEBUG
, "nl80211: Remain-on-channel cookie "
4439 "0x%llx for freq=%u MHz duration=%u",
4440 (long long unsigned int) cookie
, freq
, duration
);
4441 drv
->remain_on_chan_cookie
= cookie
;
4444 wpa_printf(MSG_DEBUG
, "nl80211: Failed to request remain-on-channel "
4445 "(freq=%d): %d (%s)", freq
, ret
, strerror(-ret
));
4451 static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv
)
4453 struct wpa_driver_nl80211_data
*drv
= priv
;
4457 if (!drv
->pending_remain_on_chan
) {
4458 wpa_printf(MSG_DEBUG
, "nl80211: No pending remain-on-channel "
4463 wpa_printf(MSG_DEBUG
, "nl80211: Cancel remain-on-channel with cookie "
4465 (long long unsigned int) drv
->remain_on_chan_cookie
);
4467 msg
= nlmsg_alloc();
4471 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
4472 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL
, 0);
4474 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
4475 NLA_PUT_U64(msg
, NL80211_ATTR_COOKIE
, drv
->remain_on_chan_cookie
);
4477 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
4480 wpa_printf(MSG_DEBUG
, "nl80211: Failed to cancel remain-on-channel: "
4481 "%d (%s)", ret
, strerror(-ret
));
4487 static void wpa_driver_nl80211_probe_req_report_timeout(void *eloop_ctx
,
4490 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
4491 if (drv
->monitor_ifidx
< 0)
4492 return; /* monitor interface already removed */
4494 if (drv
->nlmode
!= NL80211_IFTYPE_STATION
)
4495 return; /* not in station mode anymore */
4497 if (drv
->probe_req_report
)
4498 return; /* reporting enabled */
4500 wpa_printf(MSG_DEBUG
, "nl80211: Remove monitor interface due to no "
4501 "Probe Request reporting needed anymore");
4502 nl80211_remove_monitor_interface(drv
);
4506 static int wpa_driver_nl80211_probe_req_report(void *priv
, int report
)
4508 struct wpa_driver_nl80211_data
*drv
= priv
;
4510 if (drv
->nlmode
!= NL80211_IFTYPE_STATION
) {
4511 wpa_printf(MSG_DEBUG
, "nl80211: probe_req_report control only "
4512 "allowed in station mode (iftype=%d)",
4516 drv
->probe_req_report
= report
;
4519 eloop_cancel_timeout(
4520 wpa_driver_nl80211_probe_req_report_timeout
,
4522 if (drv
->monitor_ifidx
< 0 &&
4523 nl80211_create_monitor_interface(drv
))
4527 * It takes a while to remove the monitor interface, so try to
4528 * avoid doing this if it is needed again shortly. Instead,
4529 * schedule the interface to be removed later if no need for it
4532 wpa_printf(MSG_DEBUG
, "nl80211: Scheduling monitor interface "
4533 "to be removed after 10 seconds of no use");
4534 eloop_register_timeout(
4535 10, 0, wpa_driver_nl80211_probe_req_report_timeout
,
4543 static int wpa_driver_nl80211_alloc_interface_addr(void *priv
, u8
*addr
)
4545 struct wpa_driver_nl80211_data
*drv
= priv
;
4547 if (linux_get_ifhwaddr(drv
->ioctl_sock
, drv
->ifname
, addr
) < 0)
4550 if (addr
[0] & 0x02) {
4551 /* TODO: add support for generating multiple addresses */
4554 addr
[0] = 0x02; /* locally administered */
4560 static void wpa_driver_nl80211_release_interface_addr(void *priv
,
4563 /* TODO: keep list of allocated address and release them here */
4567 const struct wpa_driver_ops wpa_driver_nl80211_ops
= {
4569 .desc
= "Linux nl80211/cfg80211",
4570 .get_bssid
= wpa_driver_nl80211_get_bssid
,
4571 .get_ssid
= wpa_driver_nl80211_get_ssid
,
4572 .set_key
= wpa_driver_nl80211_set_key
,
4573 .scan2
= wpa_driver_nl80211_scan
,
4574 .get_scan_results2
= wpa_driver_nl80211_get_scan_results
,
4575 .deauthenticate
= wpa_driver_nl80211_deauthenticate
,
4576 .disassociate
= wpa_driver_nl80211_disassociate
,
4577 .authenticate
= wpa_driver_nl80211_authenticate
,
4578 .associate
= wpa_driver_nl80211_associate
,
4579 .init
= wpa_driver_nl80211_init
,
4580 .deinit
= wpa_driver_nl80211_deinit
,
4581 .get_capa
= wpa_driver_nl80211_get_capa
,
4582 .set_operstate
= wpa_driver_nl80211_set_operstate
,
4583 .set_supp_port
= wpa_driver_nl80211_set_supp_port
,
4584 .set_country
= wpa_driver_nl80211_set_country
,
4585 .set_beacon
= wpa_driver_nl80211_set_beacon
,
4586 .if_add
= wpa_driver_nl80211_if_add
,
4587 .if_remove
= wpa_driver_nl80211_if_remove
,
4588 .send_mlme
= wpa_driver_nl80211_send_mlme
,
4589 .get_hw_feature_data
= wpa_driver_nl80211_get_hw_feature_data
,
4590 .sta_add
= wpa_driver_nl80211_sta_add
,
4591 .sta_remove
= wpa_driver_nl80211_sta_remove
,
4592 .hapd_send_eapol
= wpa_driver_nl80211_hapd_send_eapol
,
4593 .sta_set_flags
= wpa_driver_nl80211_sta_set_flags
,
4595 .hapd_init
= i802_init
,
4596 .hapd_deinit
= i802_deinit
,
4597 .get_seqnum
= i802_get_seqnum
,
4598 .flush
= i802_flush
,
4599 .read_sta_data
= i802_read_sta_data
,
4600 .sta_deauth
= i802_sta_deauth
,
4601 .sta_disassoc
= i802_sta_disassoc
,
4602 .get_inact_sec
= i802_get_inact_sec
,
4603 .sta_clear_stats
= i802_sta_clear_stats
,
4604 .set_freq
= i802_set_freq
,
4605 .set_rts
= i802_set_rts
,
4606 .set_frag
= i802_set_frag
,
4607 .set_rate_sets
= i802_set_rate_sets
,
4608 .set_cts_protect
= i802_set_cts_protect
,
4609 .set_preamble
= i802_set_preamble
,
4610 .set_short_slot_time
= i802_set_short_slot_time
,
4611 .set_tx_queue_params
= i802_set_tx_queue_params
,
4612 .set_sta_vlan
= i802_set_sta_vlan
,
4613 .set_wds_sta
= i802_set_wds_sta
,
4614 #endif /* HOSTAPD */
4615 .remain_on_channel
= wpa_driver_nl80211_remain_on_channel
,
4616 .cancel_remain_on_channel
=
4617 wpa_driver_nl80211_cancel_remain_on_channel
,
4618 .probe_req_report
= wpa_driver_nl80211_probe_req_report
,
4619 .alloc_interface_addr
= wpa_driver_nl80211_alloc_interface_addr
,
4620 .release_interface_addr
= wpa_driver_nl80211_release_interface_addr
,