LCOV - code coverage report
Current view: top level - drivers - driver_nl80211.c (source / functions) Hit Total Coverage
Test: hostapd hwsim test run 1401872338 Lines: 2685 5996 44.8 %
Date: 2014-06-04 Functions: 192 300 64.0 %

          Line data    Source code
       1             : /*
       2             :  * Driver interaction with Linux nl80211/cfg80211
       3             :  * Copyright (c) 2002-2014, 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-2010, Atheros Communications
       8             :  *
       9             :  * This software may be distributed under the terms of the BSD license.
      10             :  * See README for more details.
      11             :  */
      12             : 
      13             : #include "includes.h"
      14             : #include <sys/ioctl.h>
      15             : #include <sys/types.h>
      16             : #include <sys/stat.h>
      17             : #include <fcntl.h>
      18             : #include <net/if.h>
      19             : #include <netlink/genl/genl.h>
      20             : #include <netlink/genl/family.h>
      21             : #include <netlink/genl/ctrl.h>
      22             : #include <linux/rtnetlink.h>
      23             : #include <netpacket/packet.h>
      24             : #include <linux/filter.h>
      25             : #include <linux/errqueue.h>
      26             : #include "nl80211_copy.h"
      27             : 
      28             : #include "common.h"
      29             : #include "eloop.h"
      30             : #include "utils/list.h"
      31             : #include "common/qca-vendor.h"
      32             : #include "common/qca-vendor-attr.h"
      33             : #include "common/ieee802_11_defs.h"
      34             : #include "common/ieee802_11_common.h"
      35             : #include "l2_packet/l2_packet.h"
      36             : #include "netlink.h"
      37             : #include "linux_ioctl.h"
      38             : #include "radiotap.h"
      39             : #include "radiotap_iter.h"
      40             : #include "rfkill.h"
      41             : #include "driver.h"
      42             : 
      43             : #ifndef SO_WIFI_STATUS
      44             : # if defined(__sparc__)
      45             : #  define SO_WIFI_STATUS        0x0025
      46             : # elif defined(__parisc__)
      47             : #  define SO_WIFI_STATUS        0x4022
      48             : # else
      49             : #  define SO_WIFI_STATUS        41
      50             : # endif
      51             : 
      52             : # define SCM_WIFI_STATUS        SO_WIFI_STATUS
      53             : #endif
      54             : 
      55             : #ifndef SO_EE_ORIGIN_TXSTATUS
      56             : #define SO_EE_ORIGIN_TXSTATUS   4
      57             : #endif
      58             : 
      59             : #ifndef PACKET_TX_TIMESTAMP
      60             : #define PACKET_TX_TIMESTAMP     16
      61             : #endif
      62             : 
      63             : #ifdef ANDROID
      64             : #include "android_drv.h"
      65             : #endif /* ANDROID */
      66             : #ifdef CONFIG_LIBNL20
      67             : /* libnl 2.0 compatibility code */
      68             : #define nl_handle nl_sock
      69             : #define nl80211_handle_alloc nl_socket_alloc_cb
      70             : #define nl80211_handle_destroy nl_socket_free
      71             : #else
      72             : /*
      73             :  * libnl 1.1 has a bug, it tries to allocate socket numbers densely
      74             :  * but when you free a socket again it will mess up its bitmap and
      75             :  * and use the wrong number the next time it needs a socket ID.
      76             :  * Therefore, we wrap the handle alloc/destroy and add our own pid
      77             :  * accounting.
      78             :  */
      79             : static uint32_t port_bitmap[32] = { 0 };
      80             : 
      81             : static struct nl_handle *nl80211_handle_alloc(void *cb)
      82             : {
      83             :         struct nl_handle *handle;
      84             :         uint32_t pid = getpid() & 0x3FFFFF;
      85             :         int i;
      86             : 
      87             :         handle = nl_handle_alloc_cb(cb);
      88             : 
      89             :         for (i = 0; i < 1024; i++) {
      90             :                 if (port_bitmap[i / 32] & (1 << (i % 32)))
      91             :                         continue;
      92             :                 port_bitmap[i / 32] |= 1 << (i % 32);
      93             :                 pid += i << 22;
      94             :                 break;
      95             :         }
      96             : 
      97             :         nl_socket_set_local_port(handle, pid);
      98             : 
      99             :         return handle;
     100             : }
     101             : 
     102             : static void nl80211_handle_destroy(struct nl_handle *handle)
     103             : {
     104             :         uint32_t port = nl_socket_get_local_port(handle);
     105             : 
     106             :         port >>= 22;
     107             :         port_bitmap[port / 32] &= ~(1 << (port % 32));
     108             : 
     109             :         nl_handle_destroy(handle);
     110             : }
     111             : #endif /* CONFIG_LIBNL20 */
     112             : 
     113             : 
     114             : #ifdef ANDROID
     115             : /* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
     116             : static int android_nl_socket_set_nonblocking(struct nl_handle *handle)
     117             : {
     118             :         return fcntl(nl_socket_get_fd(handle), F_SETFL, O_NONBLOCK);
     119             : }
     120             : #undef nl_socket_set_nonblocking
     121             : #define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
     122             : #endif /* ANDROID */
     123             : 
     124             : 
     125        1181 : static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
     126             : {
     127             :         struct nl_handle *handle;
     128             : 
     129        1181 :         handle = nl80211_handle_alloc(cb);
     130        1181 :         if (handle == NULL) {
     131           0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
     132             :                            "callbacks (%s)", dbg);
     133           0 :                 return NULL;
     134             :         }
     135             : 
     136        1181 :         if (genl_connect(handle)) {
     137           0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
     138             :                            "netlink (%s)", dbg);
     139           0 :                 nl80211_handle_destroy(handle);
     140           0 :                 return NULL;
     141             :         }
     142             : 
     143        1181 :         return handle;
     144             : }
     145             : 
     146             : 
     147        1181 : static void nl_destroy_handles(struct nl_handle **handle)
     148             : {
     149        1181 :         if (*handle == NULL)
     150        1181 :                 return;
     151        1181 :         nl80211_handle_destroy(*handle);
     152        1181 :         *handle = NULL;
     153             : }
     154             : 
     155             : 
     156             : #if __WORDSIZE == 64
     157             : #define ELOOP_SOCKET_INVALID    (intptr_t) 0x8888888888888889ULL
     158             : #else
     159             : #define ELOOP_SOCKET_INVALID    (intptr_t) 0x88888889ULL
     160             : #endif
     161             : 
     162        1180 : static void nl80211_register_eloop_read(struct nl_handle **handle,
     163             :                                         eloop_sock_handler handler,
     164             :                                         void *eloop_data)
     165             : {
     166        1180 :         nl_socket_set_nonblocking(*handle);
     167        1180 :         eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
     168             :                                  eloop_data, *handle);
     169        1180 :         *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
     170        1180 : }
     171             : 
     172             : 
     173        1180 : static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
     174             : {
     175        1180 :         *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
     176        1180 :         eloop_unregister_read_sock(nl_socket_get_fd(*handle));
     177        1180 :         nl_destroy_handles(handle);
     178        1180 : }
     179             : 
     180             : 
     181             : #ifndef IFF_LOWER_UP
     182             : #define IFF_LOWER_UP   0x10000         /* driver signals L1 up         */
     183             : #endif
     184             : #ifndef IFF_DORMANT
     185             : #define IFF_DORMANT    0x20000         /* driver signals dormant       */
     186             : #endif
     187             : 
     188             : #ifndef IF_OPER_DORMANT
     189             : #define IF_OPER_DORMANT 5
     190             : #endif
     191             : #ifndef IF_OPER_UP
     192             : #define IF_OPER_UP 6
     193             : #endif
     194             : 
     195             : struct nl80211_global {
     196             :         struct dl_list interfaces;
     197             :         int if_add_ifindex;
     198             :         u64 if_add_wdevid;
     199             :         int if_add_wdevid_set;
     200             :         struct netlink_data *netlink;
     201             :         struct nl_cb *nl_cb;
     202             :         struct nl_handle *nl;
     203             :         int nl80211_id;
     204             :         int ioctl_sock; /* socket for ioctl() use */
     205             : 
     206             :         struct nl_handle *nl_event;
     207             : };
     208             : 
     209             : struct nl80211_wiphy_data {
     210             :         struct dl_list list;
     211             :         struct dl_list bsss;
     212             :         struct dl_list drvs;
     213             : 
     214             :         struct nl_handle *nl_beacons;
     215             :         struct nl_cb *nl_cb;
     216             : 
     217             :         int wiphy_idx;
     218             : };
     219             : 
     220             : static void nl80211_global_deinit(void *priv);
     221             : 
     222             : struct i802_bss {
     223             :         struct wpa_driver_nl80211_data *drv;
     224             :         struct i802_bss *next;
     225             :         int ifindex;
     226             :         u64 wdev_id;
     227             :         char ifname[IFNAMSIZ + 1];
     228             :         char brname[IFNAMSIZ];
     229             :         unsigned int beacon_set:1;
     230             :         unsigned int added_if_into_bridge:1;
     231             :         unsigned int added_bridge:1;
     232             :         unsigned int in_deinit:1;
     233             :         unsigned int wdev_id_set:1;
     234             :         unsigned int added_if:1;
     235             : 
     236             :         u8 addr[ETH_ALEN];
     237             : 
     238             :         int freq;
     239             :         int bandwidth;
     240             :         int if_dynamic;
     241             : 
     242             :         void *ctx;
     243             :         struct nl_handle *nl_preq, *nl_mgmt;
     244             :         struct nl_cb *nl_cb;
     245             : 
     246             :         struct nl80211_wiphy_data *wiphy_data;
     247             :         struct dl_list wiphy_list;
     248             : };
     249             : 
     250             : struct wpa_driver_nl80211_data {
     251             :         struct nl80211_global *global;
     252             :         struct dl_list list;
     253             :         struct dl_list wiphy_list;
     254             :         char phyname[32];
     255             :         void *ctx;
     256             :         int ifindex;
     257             :         int if_removed;
     258             :         int if_disabled;
     259             :         int ignore_if_down_event;
     260             :         struct rfkill_data *rfkill;
     261             :         struct wpa_driver_capa capa;
     262             :         u8 *extended_capa, *extended_capa_mask;
     263             :         unsigned int extended_capa_len;
     264             :         int has_capability;
     265             : 
     266             :         int operstate;
     267             : 
     268             :         int scan_complete_events;
     269             :         enum scan_states {
     270             :                 NO_SCAN, SCAN_REQUESTED, SCAN_STARTED, SCAN_COMPLETED,
     271             :                 SCAN_ABORTED, SCHED_SCAN_STARTED, SCHED_SCAN_STOPPED,
     272             :                 SCHED_SCAN_RESULTS
     273             :         } scan_state;
     274             : 
     275             :         struct nl_cb *nl_cb;
     276             : 
     277             :         u8 auth_bssid[ETH_ALEN];
     278             :         u8 auth_attempt_bssid[ETH_ALEN];
     279             :         u8 bssid[ETH_ALEN];
     280             :         u8 prev_bssid[ETH_ALEN];
     281             :         int associated;
     282             :         u8 ssid[32];
     283             :         size_t ssid_len;
     284             :         enum nl80211_iftype nlmode;
     285             :         enum nl80211_iftype ap_scan_as_station;
     286             :         unsigned int assoc_freq;
     287             : 
     288             :         int monitor_sock;
     289             :         int monitor_ifidx;
     290             :         int monitor_refcount;
     291             : 
     292             :         unsigned int disabled_11b_rates:1;
     293             :         unsigned int pending_remain_on_chan:1;
     294             :         unsigned int in_interface_list:1;
     295             :         unsigned int device_ap_sme:1;
     296             :         unsigned int poll_command_supported:1;
     297             :         unsigned int data_tx_status:1;
     298             :         unsigned int scan_for_auth:1;
     299             :         unsigned int retry_auth:1;
     300             :         unsigned int use_monitor:1;
     301             :         unsigned int ignore_next_local_disconnect:1;
     302             :         unsigned int ignore_next_local_deauth:1;
     303             :         unsigned int allow_p2p_device:1;
     304             :         unsigned int hostapd:1;
     305             :         unsigned int start_mode_ap:1;
     306             :         unsigned int start_iface_up:1;
     307             :         unsigned int test_use_roc_tx:1;
     308             :         unsigned int ignore_deauth_event:1;
     309             :         unsigned int dfs_vendor_cmd_avail:1;
     310             : 
     311             :         u64 remain_on_chan_cookie;
     312             :         u64 send_action_cookie;
     313             : 
     314             :         unsigned int last_mgmt_freq;
     315             : 
     316             :         struct wpa_driver_scan_filter *filter_ssids;
     317             :         size_t num_filter_ssids;
     318             : 
     319             :         struct i802_bss *first_bss;
     320             : 
     321             :         int eapol_tx_sock;
     322             : 
     323             :         int eapol_sock; /* socket for EAPOL frames */
     324             : 
     325             :         int default_if_indices[16];
     326             :         int *if_indices;
     327             :         int num_if_indices;
     328             : 
     329             :         /* From failed authentication command */
     330             :         int auth_freq;
     331             :         u8 auth_bssid_[ETH_ALEN];
     332             :         u8 auth_ssid[32];
     333             :         size_t auth_ssid_len;
     334             :         int auth_alg;
     335             :         u8 *auth_ie;
     336             :         size_t auth_ie_len;
     337             :         u8 auth_wep_key[4][16];
     338             :         size_t auth_wep_key_len[4];
     339             :         int auth_wep_tx_keyidx;
     340             :         int auth_local_state_change;
     341             :         int auth_p2p;
     342             : };
     343             : 
     344             : 
     345             : static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
     346             : static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
     347             :                                             void *timeout_ctx);
     348             : static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
     349             :                                        enum nl80211_iftype nlmode);
     350             : static int
     351             : wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
     352             :                                    const u8 *set_addr, int first);
     353             : static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
     354             :                                    const u8 *addr, int cmd, u16 reason_code,
     355             :                                    int local_state_change);
     356             : static void nl80211_remove_monitor_interface(
     357             :         struct wpa_driver_nl80211_data *drv);
     358             : static int nl80211_send_frame_cmd(struct i802_bss *bss,
     359             :                                   unsigned int freq, unsigned int wait,
     360             :                                   const u8 *buf, size_t buf_len, u64 *cookie,
     361             :                                   int no_cck, int no_ack, int offchanok);
     362             : static int nl80211_register_frame(struct i802_bss *bss,
     363             :                                   struct nl_handle *hl_handle,
     364             :                                   u16 type, const u8 *match, size_t match_len);
     365             : static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
     366             :                                                int report);
     367             : #ifdef ANDROID
     368             : static int android_pno_start(struct i802_bss *bss,
     369             :                              struct wpa_driver_scan_params *params);
     370             : static int android_pno_stop(struct i802_bss *bss);
     371             : extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
     372             :                                          size_t buf_len);
     373             : #endif /* ANDROID */
     374             : #ifdef ANDROID_P2P
     375             : #ifdef ANDROID_P2P_STUB
     376             : int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration) {
     377             :         return 0;
     378             : }
     379             : int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len) {
     380             :         return 0;
     381             : }
     382             : int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow) {
     383             :         return -1;
     384             : }
     385             : int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
     386             :                                  const struct wpabuf *proberesp,
     387             :                                  const struct wpabuf *assocresp) {
     388             :         return 0;
     389             : }
     390             : #else /* ANDROID_P2P_STUB */
     391             : int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
     392             : int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
     393             : int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
     394             : int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
     395             :                                  const struct wpabuf *proberesp,
     396             :                                  const struct wpabuf *assocresp);
     397             : #endif /* ANDROID_P2P_STUB */
     398             : #endif /* ANDROID_P2P */
     399             : 
     400             : static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
     401             : static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
     402             : static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
     403             : static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
     404             :                                         enum wpa_driver_if_type type,
     405             :                                         const char *ifname);
     406             : 
     407             : static int nl80211_set_channel(struct i802_bss *bss,
     408             :                                struct hostapd_freq_params *freq, int set_chan);
     409             : static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
     410             :                                      int ifindex, int disabled);
     411             : 
     412             : static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
     413             : static int wpa_driver_nl80211_authenticate_retry(
     414             :         struct wpa_driver_nl80211_data *drv);
     415             : 
     416             : static int i802_set_iface_flags(struct i802_bss *bss, int up);
     417             : 
     418             : 
     419       17644 : static const char * nl80211_command_to_string(enum nl80211_commands cmd)
     420             : {
     421             : #define C2S(x) case x: return #x;
     422       17644 :         switch (cmd) {
     423           0 :         C2S(NL80211_CMD_UNSPEC)
     424           0 :         C2S(NL80211_CMD_GET_WIPHY)
     425           0 :         C2S(NL80211_CMD_SET_WIPHY)
     426           0 :         C2S(NL80211_CMD_NEW_WIPHY)
     427           0 :         C2S(NL80211_CMD_DEL_WIPHY)
     428           0 :         C2S(NL80211_CMD_GET_INTERFACE)
     429           0 :         C2S(NL80211_CMD_SET_INTERFACE)
     430           0 :         C2S(NL80211_CMD_NEW_INTERFACE)
     431           0 :         C2S(NL80211_CMD_DEL_INTERFACE)
     432           0 :         C2S(NL80211_CMD_GET_KEY)
     433           0 :         C2S(NL80211_CMD_SET_KEY)
     434           0 :         C2S(NL80211_CMD_NEW_KEY)
     435           0 :         C2S(NL80211_CMD_DEL_KEY)
     436           0 :         C2S(NL80211_CMD_GET_BEACON)
     437           0 :         C2S(NL80211_CMD_SET_BEACON)
     438           0 :         C2S(NL80211_CMD_START_AP)
     439           0 :         C2S(NL80211_CMD_STOP_AP)
     440           0 :         C2S(NL80211_CMD_GET_STATION)
     441           0 :         C2S(NL80211_CMD_SET_STATION)
     442         993 :         C2S(NL80211_CMD_NEW_STATION)
     443         952 :         C2S(NL80211_CMD_DEL_STATION)
     444           0 :         C2S(NL80211_CMD_GET_MPATH)
     445           0 :         C2S(NL80211_CMD_SET_MPATH)
     446           0 :         C2S(NL80211_CMD_NEW_MPATH)
     447           0 :         C2S(NL80211_CMD_DEL_MPATH)
     448           0 :         C2S(NL80211_CMD_SET_BSS)
     449           0 :         C2S(NL80211_CMD_SET_REG)
     450           0 :         C2S(NL80211_CMD_REQ_SET_REG)
     451           0 :         C2S(NL80211_CMD_GET_MESH_CONFIG)
     452           0 :         C2S(NL80211_CMD_SET_MESH_CONFIG)
     453           0 :         C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
     454           0 :         C2S(NL80211_CMD_GET_REG)
     455           0 :         C2S(NL80211_CMD_GET_SCAN)
     456          64 :         C2S(NL80211_CMD_TRIGGER_SCAN)
     457          59 :         C2S(NL80211_CMD_NEW_SCAN_RESULTS)
     458           0 :         C2S(NL80211_CMD_SCAN_ABORTED)
     459         798 :         C2S(NL80211_CMD_REG_CHANGE)
     460           0 :         C2S(NL80211_CMD_AUTHENTICATE)
     461           0 :         C2S(NL80211_CMD_ASSOCIATE)
     462           0 :         C2S(NL80211_CMD_DEAUTHENTICATE)
     463           0 :         C2S(NL80211_CMD_DISASSOCIATE)
     464           2 :         C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
     465           0 :         C2S(NL80211_CMD_REG_BEACON_HINT)
     466           0 :         C2S(NL80211_CMD_JOIN_IBSS)
     467           0 :         C2S(NL80211_CMD_LEAVE_IBSS)
     468           0 :         C2S(NL80211_CMD_TESTMODE)
     469           0 :         C2S(NL80211_CMD_CONNECT)
     470           0 :         C2S(NL80211_CMD_ROAM)
     471           0 :         C2S(NL80211_CMD_DISCONNECT)
     472           0 :         C2S(NL80211_CMD_SET_WIPHY_NETNS)
     473           0 :         C2S(NL80211_CMD_GET_SURVEY)
     474           0 :         C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
     475           0 :         C2S(NL80211_CMD_SET_PMKSA)
     476           0 :         C2S(NL80211_CMD_DEL_PMKSA)
     477           0 :         C2S(NL80211_CMD_FLUSH_PMKSA)
     478           0 :         C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
     479           0 :         C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
     480           0 :         C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
     481           0 :         C2S(NL80211_CMD_REGISTER_FRAME)
     482        9112 :         C2S(NL80211_CMD_FRAME)
     483        5658 :         C2S(NL80211_CMD_FRAME_TX_STATUS)
     484           0 :         C2S(NL80211_CMD_SET_POWER_SAVE)
     485           0 :         C2S(NL80211_CMD_GET_POWER_SAVE)
     486           0 :         C2S(NL80211_CMD_SET_CQM)
     487           0 :         C2S(NL80211_CMD_NOTIFY_CQM)
     488           0 :         C2S(NL80211_CMD_SET_CHANNEL)
     489           0 :         C2S(NL80211_CMD_SET_WDS_PEER)
     490           0 :         C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
     491           0 :         C2S(NL80211_CMD_JOIN_MESH)
     492           0 :         C2S(NL80211_CMD_LEAVE_MESH)
     493           2 :         C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
     494           0 :         C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
     495           0 :         C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
     496           0 :         C2S(NL80211_CMD_GET_WOWLAN)
     497           0 :         C2S(NL80211_CMD_SET_WOWLAN)
     498           0 :         C2S(NL80211_CMD_START_SCHED_SCAN)
     499           0 :         C2S(NL80211_CMD_STOP_SCHED_SCAN)
     500           0 :         C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
     501           0 :         C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
     502           0 :         C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
     503           0 :         C2S(NL80211_CMD_PMKSA_CANDIDATE)
     504           0 :         C2S(NL80211_CMD_TDLS_OPER)
     505           0 :         C2S(NL80211_CMD_TDLS_MGMT)
     506           1 :         C2S(NL80211_CMD_UNEXPECTED_FRAME)
     507           2 :         C2S(NL80211_CMD_PROBE_CLIENT)
     508           0 :         C2S(NL80211_CMD_REGISTER_BEACONS)
     509           1 :         C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
     510           0 :         C2S(NL80211_CMD_SET_NOACK_MAP)
     511           0 :         C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
     512           0 :         C2S(NL80211_CMD_START_P2P_DEVICE)
     513           0 :         C2S(NL80211_CMD_STOP_P2P_DEVICE)
     514           0 :         C2S(NL80211_CMD_CONN_FAILED)
     515           0 :         C2S(NL80211_CMD_SET_MCAST_RATE)
     516           0 :         C2S(NL80211_CMD_SET_MAC_ACL)
     517           0 :         C2S(NL80211_CMD_RADAR_DETECT)
     518           0 :         C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
     519           0 :         C2S(NL80211_CMD_UPDATE_FT_IES)
     520           0 :         C2S(NL80211_CMD_FT_EVENT)
     521           0 :         C2S(NL80211_CMD_CRIT_PROTOCOL_START)
     522           0 :         C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
     523           0 :         C2S(NL80211_CMD_GET_COALESCE)
     524           0 :         C2S(NL80211_CMD_SET_COALESCE)
     525           0 :         C2S(NL80211_CMD_CHANNEL_SWITCH)
     526           0 :         C2S(NL80211_CMD_VENDOR)
     527           0 :         C2S(NL80211_CMD_SET_QOS_MAP)
     528             :         default:
     529           0 :                 return "NL80211_CMD_UNKNOWN";
     530             :         }
     531             : #undef C2S
     532             : }
     533             : 
     534             : 
     535             : /* Converts nl80211_chan_width to a common format */
     536           0 : static enum chan_width convert2width(int width)
     537             : {
     538           0 :         switch (width) {
     539             :         case NL80211_CHAN_WIDTH_20_NOHT:
     540           0 :                 return CHAN_WIDTH_20_NOHT;
     541             :         case NL80211_CHAN_WIDTH_20:
     542           0 :                 return CHAN_WIDTH_20;
     543             :         case NL80211_CHAN_WIDTH_40:
     544           0 :                 return CHAN_WIDTH_40;
     545             :         case NL80211_CHAN_WIDTH_80:
     546           0 :                 return CHAN_WIDTH_80;
     547             :         case NL80211_CHAN_WIDTH_80P80:
     548           0 :                 return CHAN_WIDTH_80P80;
     549             :         case NL80211_CHAN_WIDTH_160:
     550           0 :                 return CHAN_WIDTH_160;
     551             :         }
     552           0 :         return CHAN_WIDTH_UNKNOWN;
     553             : }
     554             : 
     555             : 
     556       10392 : static int is_ap_interface(enum nl80211_iftype nlmode)
     557             : {
     558       10392 :         return nlmode == NL80211_IFTYPE_AP ||
     559             :                 nlmode == NL80211_IFTYPE_P2P_GO;
     560             : }
     561             : 
     562             : 
     563        5153 : static int is_sta_interface(enum nl80211_iftype nlmode)
     564             : {
     565        5153 :         return nlmode == NL80211_IFTYPE_STATION ||
     566             :                 nlmode == NL80211_IFTYPE_P2P_CLIENT;
     567             : }
     568             : 
     569             : 
     570        1191 : static int is_p2p_net_interface(enum nl80211_iftype nlmode)
     571             : {
     572        1191 :         return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
     573             :                 nlmode == NL80211_IFTYPE_P2P_GO;
     574             : }
     575             : 
     576             : 
     577           0 : static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
     578             : {
     579           0 :         if (drv->associated)
     580           0 :                 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
     581           0 :         drv->associated = 0;
     582           0 :         os_memset(drv->bssid, 0, ETH_ALEN);
     583           0 : }
     584             : 
     585             : 
     586             : struct nl80211_bss_info_arg {
     587             :         struct wpa_driver_nl80211_data *drv;
     588             :         struct wpa_scan_results *res;
     589             :         unsigned int assoc_freq;
     590             :         unsigned int ibss_freq;
     591             :         u8 assoc_bssid[ETH_ALEN];
     592             : };
     593             : 
     594             : static int bss_info_handler(struct nl_msg *msg, void *arg);
     595             : 
     596             : 
     597             : /* nl80211 code */
     598       29486 : static int ack_handler(struct nl_msg *msg, void *arg)
     599             : {
     600       29486 :         int *err = arg;
     601       29486 :         *err = 0;
     602       29486 :         return NL_STOP;
     603             : }
     604             : 
     605        1243 : static int finish_handler(struct nl_msg *msg, void *arg)
     606             : {
     607        1243 :         int *ret = arg;
     608        1243 :         *ret = 0;
     609        1243 :         return NL_SKIP;
     610             : }
     611             : 
     612        6763 : static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
     613             :                          void *arg)
     614             : {
     615        6763 :         int *ret = arg;
     616        6763 :         *ret = err->error;
     617        6763 :         return NL_SKIP;
     618             : }
     619             : 
     620             : 
     621      138739 : static int no_seq_check(struct nl_msg *msg, void *arg)
     622             : {
     623      138739 :         return NL_OK;
     624             : }
     625             : 
     626             : 
     627       37492 : static int send_and_recv(struct nl80211_global *global,
     628             :                          struct nl_handle *nl_handle, struct nl_msg *msg,
     629             :                          int (*valid_handler)(struct nl_msg *, void *),
     630             :                          void *valid_data)
     631             : {
     632             :         struct nl_cb *cb;
     633       37492 :         int err = -ENOMEM;
     634             : 
     635       37492 :         cb = nl_cb_clone(global->nl_cb);
     636       37492 :         if (!cb)
     637           0 :                 goto out;
     638             : 
     639       37492 :         err = nl_send_auto_complete(nl_handle, msg);
     640       37492 :         if (err < 0)
     641           0 :                 goto out;
     642             : 
     643       37492 :         err = 1;
     644             : 
     645       37492 :         nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
     646       37492 :         nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
     647       37492 :         nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
     648             : 
     649       37492 :         if (valid_handler)
     650       11334 :                 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
     651             :                           valid_handler, valid_data);
     652             : 
     653      120844 :         while (err > 0) {
     654       45860 :                 int res = nl_recvmsgs(nl_handle, cb);
     655       45860 :                 if (res < 0) {
     656           0 :                         wpa_printf(MSG_INFO,
     657             :                                    "nl80211: %s->nl_recvmsgs failed: %d",
     658             :                                    __func__, res);
     659             :                 }
     660             :         }
     661             :  out:
     662       37492 :         nl_cb_put(cb);
     663       37492 :         nlmsg_free(msg);
     664       37492 :         return err;
     665             : }
     666             : 
     667             : 
     668           4 : static int send_and_recv_msgs_global(struct nl80211_global *global,
     669             :                                      struct nl_msg *msg,
     670             :                                      int (*valid_handler)(struct nl_msg *, void *),
     671             :                                      void *valid_data)
     672             : {
     673           4 :         return send_and_recv(global, global->nl, msg, valid_handler,
     674             :                              valid_data);
     675             : }
     676             : 
     677             : 
     678       32116 : static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
     679             :                               struct nl_msg *msg,
     680             :                               int (*valid_handler)(struct nl_msg *, void *),
     681             :                               void *valid_data)
     682             : {
     683       32116 :         return send_and_recv(drv->global, drv->global->nl, msg,
     684             :                              valid_handler, valid_data);
     685             : }
     686             : 
     687             : 
     688             : struct family_data {
     689             :         const char *group;
     690             :         int id;
     691             : };
     692             : 
     693             : 
     694       14064 : static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
     695             : {
     696       14064 :         if (bss->wdev_id_set)
     697           0 :                 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
     698             :         else
     699       14064 :                 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
     700       14064 :         return 0;
     701             : 
     702             : nla_put_failure:
     703           0 :         return -1;
     704             : }
     705             : 
     706             : 
     707           4 : static int family_handler(struct nl_msg *msg, void *arg)
     708             : {
     709           4 :         struct family_data *res = arg;
     710             :         struct nlattr *tb[CTRL_ATTR_MAX + 1];
     711           4 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
     712             :         struct nlattr *mcgrp;
     713             :         int i;
     714             : 
     715           4 :         nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
     716             :                   genlmsg_attrlen(gnlh, 0), NULL);
     717           4 :         if (!tb[CTRL_ATTR_MCAST_GROUPS])
     718           0 :                 return NL_SKIP;
     719             : 
     720          28 :         nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
     721             :                 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
     722          14 :                 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
     723             :                           nla_len(mcgrp), NULL);
     724          28 :                 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
     725          28 :                     !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
     726          14 :                     os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
     727             :                                res->group,
     728             :                                nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
     729          10 :                         continue;
     730           4 :                 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
     731           4 :                 break;
     732             :         };
     733             : 
     734           4 :         return NL_SKIP;
     735             : }
     736             : 
     737             : 
     738           4 : static int nl_get_multicast_id(struct nl80211_global *global,
     739             :                                const char *family, const char *group)
     740             : {
     741             :         struct nl_msg *msg;
     742           4 :         int ret = -1;
     743           4 :         struct family_data res = { group, -ENOENT };
     744             : 
     745           4 :         msg = nlmsg_alloc();
     746           4 :         if (!msg)
     747           0 :                 return -ENOMEM;
     748           4 :         genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
     749             :                     0, 0, CTRL_CMD_GETFAMILY, 0);
     750           4 :         NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
     751             : 
     752           4 :         ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
     753           4 :         msg = NULL;
     754           4 :         if (ret == 0)
     755           4 :                 ret = res.id;
     756             : 
     757             : nla_put_failure:
     758           4 :         nlmsg_free(msg);
     759           4 :         return ret;
     760             : }
     761             : 
     762             : 
     763       37488 : static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
     764             :                           struct nl_msg *msg, int flags, uint8_t cmd)
     765             : {
     766       37488 :         return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
     767             :                            0, flags, cmd, 0);
     768             : }
     769             : 
     770             : 
     771             : struct wiphy_idx_data {
     772             :         int wiphy_idx;
     773             :         enum nl80211_iftype nlmode;
     774             :         u8 *macaddr;
     775             : };
     776             : 
     777             : 
     778        2341 : static int netdev_info_handler(struct nl_msg *msg, void *arg)
     779             : {
     780             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
     781        2341 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
     782        2341 :         struct wiphy_idx_data *info = arg;
     783             : 
     784        2341 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
     785             :                   genlmsg_attrlen(gnlh, 0), NULL);
     786             : 
     787        2341 :         if (tb[NL80211_ATTR_WIPHY])
     788        2341 :                 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
     789             : 
     790        2341 :         if (tb[NL80211_ATTR_IFTYPE])
     791        2341 :                 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
     792             : 
     793        2341 :         if (tb[NL80211_ATTR_MAC] && info->macaddr)
     794           0 :                 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
     795             :                           ETH_ALEN);
     796             : 
     797        2341 :         return NL_SKIP;
     798             : }
     799             : 
     800             : 
     801         599 : static int nl80211_get_wiphy_index(struct i802_bss *bss)
     802             : {
     803             :         struct nl_msg *msg;
     804         599 :         struct wiphy_idx_data data = {
     805             :                 .wiphy_idx = -1,
     806             :                 .macaddr = NULL,
     807             :         };
     808             : 
     809         599 :         msg = nlmsg_alloc();
     810         599 :         if (!msg)
     811           0 :                 return NL80211_IFTYPE_UNSPECIFIED;
     812             : 
     813         599 :         nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
     814             : 
     815         599 :         if (nl80211_set_iface_id(msg, bss) < 0)
     816           0 :                 goto nla_put_failure;
     817             : 
     818         599 :         if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
     819         599 :                 return data.wiphy_idx;
     820           0 :         msg = NULL;
     821             : nla_put_failure:
     822           0 :         nlmsg_free(msg);
     823           0 :         return -1;
     824             : }
     825             : 
     826             : 
     827        1742 : static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
     828             : {
     829             :         struct nl_msg *msg;
     830        1742 :         struct wiphy_idx_data data = {
     831             :                 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
     832             :                 .macaddr = NULL,
     833             :         };
     834             : 
     835        1742 :         msg = nlmsg_alloc();
     836        1742 :         if (!msg)
     837           0 :                 return -1;
     838             : 
     839        1742 :         nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
     840             : 
     841        1742 :         if (nl80211_set_iface_id(msg, bss) < 0)
     842           0 :                 goto nla_put_failure;
     843             : 
     844        1742 :         if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
     845        1742 :                 return data.nlmode;
     846           0 :         msg = NULL;
     847             : nla_put_failure:
     848           0 :         nlmsg_free(msg);
     849           0 :         return NL80211_IFTYPE_UNSPECIFIED;
     850             : }
     851             : 
     852             : 
     853           0 : static int nl80211_get_macaddr(struct i802_bss *bss)
     854             : {
     855             :         struct nl_msg *msg;
     856           0 :         struct wiphy_idx_data data = {
     857           0 :                 .macaddr = bss->addr,
     858             :         };
     859             : 
     860           0 :         msg = nlmsg_alloc();
     861           0 :         if (!msg)
     862           0 :                 return NL80211_IFTYPE_UNSPECIFIED;
     863             : 
     864           0 :         nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
     865           0 :         if (nl80211_set_iface_id(msg, bss) < 0)
     866           0 :                 goto nla_put_failure;
     867             : 
     868           0 :         return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
     869             : 
     870             : nla_put_failure:
     871           0 :         nlmsg_free(msg);
     872           0 :         return NL80211_IFTYPE_UNSPECIFIED;
     873             : }
     874             : 
     875             : 
     876         580 : static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
     877             :                                     struct nl80211_wiphy_data *w)
     878             : {
     879             :         struct nl_msg *msg;
     880         580 :         int ret = -1;
     881             : 
     882         580 :         msg = nlmsg_alloc();
     883         580 :         if (!msg)
     884           0 :                 return -1;
     885             : 
     886         580 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
     887             : 
     888         580 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
     889             : 
     890         580 :         ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
     891         580 :         msg = NULL;
     892         580 :         if (ret) {
     893           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
     894             :                            "failed: ret=%d (%s)",
     895             :                            ret, strerror(-ret));
     896           0 :                 goto nla_put_failure;
     897             :         }
     898         580 :         ret = 0;
     899             : nla_put_failure:
     900         580 :         nlmsg_free(msg);
     901         580 :         return ret;
     902             : }
     903             : 
     904             : 
     905        1909 : static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
     906             : {
     907        1909 :         struct nl80211_wiphy_data *w = eloop_ctx;
     908             :         int res;
     909             : 
     910        1909 :         wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
     911             : 
     912        1909 :         res = nl_recvmsgs(handle, w->nl_cb);
     913        1909 :         if (res < 0) {
     914           0 :                 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
     915             :                            __func__, res);
     916             :         }
     917        1909 : }
     918             : 
     919             : 
     920        1909 : static int process_beacon_event(struct nl_msg *msg, void *arg)
     921             : {
     922        1909 :         struct nl80211_wiphy_data *w = arg;
     923             :         struct wpa_driver_nl80211_data *drv;
     924        1909 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
     925             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
     926             :         union wpa_event_data event;
     927             : 
     928        1909 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
     929             :                   genlmsg_attrlen(gnlh, 0), NULL);
     930             : 
     931        1909 :         if (gnlh->cmd != NL80211_CMD_FRAME) {
     932           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
     933           0 :                            gnlh->cmd);
     934           0 :                 return NL_SKIP;
     935             :         }
     936             : 
     937        1909 :         if (!tb[NL80211_ATTR_FRAME])
     938           0 :                 return NL_SKIP;
     939             : 
     940        3818 :         dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
     941             :                          wiphy_list) {
     942        1909 :                 os_memset(&event, 0, sizeof(event));
     943        1909 :                 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
     944        1909 :                 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
     945        1909 :                 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
     946             :         }
     947             : 
     948        1909 :         return NL_SKIP;
     949             : }
     950             : 
     951             : 
     952             : static struct nl80211_wiphy_data *
     953         599 : nl80211_get_wiphy_data_ap(struct i802_bss *bss)
     954             : {
     955             :         static DEFINE_DL_LIST(nl80211_wiphys);
     956             :         struct nl80211_wiphy_data *w;
     957         599 :         int wiphy_idx, found = 0;
     958             :         struct i802_bss *tmp_bss;
     959             : 
     960         599 :         if (bss->wiphy_data != NULL)
     961           0 :                 return bss->wiphy_data;
     962             : 
     963         599 :         wiphy_idx = nl80211_get_wiphy_index(bss);
     964             : 
     965         670 :         dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
     966          90 :                 if (w->wiphy_idx == wiphy_idx)
     967          19 :                         goto add;
     968             :         }
     969             : 
     970             :         /* alloc new one */
     971         580 :         w = os_zalloc(sizeof(*w));
     972         580 :         if (w == NULL)
     973           0 :                 return NULL;
     974         580 :         w->wiphy_idx = wiphy_idx;
     975         580 :         dl_list_init(&w->bsss);
     976         580 :         dl_list_init(&w->drvs);
     977             : 
     978         580 :         w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
     979         580 :         if (!w->nl_cb) {
     980           0 :                 os_free(w);
     981           0 :                 return NULL;
     982             :         }
     983         580 :         nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
     984         580 :         nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
     985             :                   w);
     986             : 
     987         580 :         w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
     988             :                                          "wiphy beacons");
     989         580 :         if (w->nl_beacons == NULL) {
     990           0 :                 os_free(w);
     991           0 :                 return NULL;
     992             :         }
     993             : 
     994         580 :         if (nl80211_register_beacons(bss->drv, w)) {
     995           0 :                 nl_destroy_handles(&w->nl_beacons);
     996           0 :                 os_free(w);
     997           0 :                 return NULL;
     998             :         }
     999             : 
    1000         580 :         nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
    1001             : 
    1002         580 :         dl_list_add(&nl80211_wiphys, &w->list);
    1003             : 
    1004             : add:
    1005             :         /* drv entry for this bss already there? */
    1006         599 :         dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
    1007          19 :                 if (tmp_bss->drv == bss->drv) {
    1008          19 :                         found = 1;
    1009          19 :                         break;
    1010             :                 }
    1011             :         }
    1012             :         /* if not add it */
    1013         599 :         if (!found)
    1014         580 :                 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
    1015             : 
    1016         599 :         dl_list_add(&w->bsss, &bss->wiphy_list);
    1017         599 :         bss->wiphy_data = w;
    1018         599 :         return w;
    1019             : }
    1020             : 
    1021             : 
    1022         599 : static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
    1023             : {
    1024         599 :         struct nl80211_wiphy_data *w = bss->wiphy_data;
    1025             :         struct i802_bss *tmp_bss;
    1026         599 :         int found = 0;
    1027             : 
    1028         599 :         if (w == NULL)
    1029           0 :                 return;
    1030         599 :         bss->wiphy_data = NULL;
    1031         599 :         dl_list_del(&bss->wiphy_list);
    1032             : 
    1033             :         /* still any for this drv present? */
    1034         599 :         dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
    1035          19 :                 if (tmp_bss->drv == bss->drv) {
    1036          19 :                         found = 1;
    1037          19 :                         break;
    1038             :                 }
    1039             :         }
    1040             :         /* if not remove it */
    1041         599 :         if (!found)
    1042         580 :                 dl_list_del(&bss->drv->wiphy_list);
    1043             : 
    1044         599 :         if (!dl_list_empty(&w->bsss))
    1045          19 :                 return;
    1046             : 
    1047         580 :         nl80211_destroy_eloop_handle(&w->nl_beacons);
    1048             : 
    1049         580 :         nl_cb_put(w->nl_cb);
    1050         580 :         dl_list_del(&w->list);
    1051         580 :         os_free(w);
    1052             : }
    1053             : 
    1054             : 
    1055           0 : static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
    1056             : {
    1057           0 :         struct i802_bss *bss = priv;
    1058           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    1059           0 :         if (!drv->associated)
    1060           0 :                 return -1;
    1061           0 :         os_memcpy(bssid, drv->bssid, ETH_ALEN);
    1062           0 :         return 0;
    1063             : }
    1064             : 
    1065             : 
    1066           0 : static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
    1067             : {
    1068           0 :         struct i802_bss *bss = priv;
    1069           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    1070           0 :         if (!drv->associated)
    1071           0 :                 return -1;
    1072           0 :         os_memcpy(ssid, drv->ssid, drv->ssid_len);
    1073           0 :         return drv->ssid_len;
    1074             : }
    1075             : 
    1076             : 
    1077        1355 : static void wpa_driver_nl80211_event_newlink(
    1078             :         struct wpa_driver_nl80211_data *drv, char *ifname)
    1079             : {
    1080             :         union wpa_event_data event;
    1081             : 
    1082        1355 :         if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
    1083        1205 :                 if (if_nametoindex(drv->first_bss->ifname) == 0) {
    1084           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
    1085           0 :                                    drv->first_bss->ifname);
    1086           0 :                         return;
    1087             :                 }
    1088        1205 :                 if (!drv->if_removed)
    1089        1201 :                         return;
    1090           4 :                 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
    1091           4 :                            drv->first_bss->ifname);
    1092           4 :                 drv->if_removed = 0;
    1093             :         }
    1094             : 
    1095         154 :         os_memset(&event, 0, sizeof(event));
    1096         154 :         os_strlcpy(event.interface_status.ifname, ifname,
    1097             :                    sizeof(event.interface_status.ifname));
    1098         154 :         event.interface_status.ievent = EVENT_INTERFACE_ADDED;
    1099         154 :         wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
    1100             : }
    1101             : 
    1102             : 
    1103           5 : static void wpa_driver_nl80211_event_dellink(
    1104             :         struct wpa_driver_nl80211_data *drv, char *ifname)
    1105             : {
    1106             :         union wpa_event_data event;
    1107             : 
    1108           5 :         if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
    1109           4 :                 if (drv->if_removed) {
    1110           0 :                         wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
    1111             :                                    ifname);
    1112           5 :                         return;
    1113             :                 }
    1114           4 :                 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
    1115             :                            ifname);
    1116           4 :                 drv->if_removed = 1;
    1117             :         } else {
    1118           1 :                 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
    1119             :                            ifname);
    1120             :         }
    1121             : 
    1122           5 :         os_memset(&event, 0, sizeof(event));
    1123           5 :         os_strlcpy(event.interface_status.ifname, ifname,
    1124             :                    sizeof(event.interface_status.ifname));
    1125           5 :         event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
    1126           5 :         wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
    1127             : }
    1128             : 
    1129             : 
    1130           0 : static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
    1131             :                                          u8 *buf, size_t len)
    1132             : {
    1133             :         int attrlen, rta_len;
    1134             :         struct rtattr *attr;
    1135             : 
    1136           0 :         attrlen = len;
    1137           0 :         attr = (struct rtattr *) buf;
    1138             : 
    1139           0 :         rta_len = RTA_ALIGN(sizeof(struct rtattr));
    1140           0 :         while (RTA_OK(attr, attrlen)) {
    1141           0 :                 if (attr->rta_type == IFLA_IFNAME) {
    1142           0 :                         if (os_strcmp(((char *) attr) + rta_len,
    1143             :                                       drv->first_bss->ifname) == 0)
    1144           0 :                                 return 1;
    1145             :                         else
    1146           0 :                                 break;
    1147             :                 }
    1148           0 :                 attr = RTA_NEXT(attr, attrlen);
    1149             :         }
    1150             : 
    1151           0 :         return 0;
    1152             : }
    1153             : 
    1154             : 
    1155        5067 : static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
    1156             :                                           int ifindex, u8 *buf, size_t len)
    1157             : {
    1158        5067 :         if (drv->ifindex == ifindex)
    1159        1217 :                 return 1;
    1160             : 
    1161        3850 :         if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
    1162           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
    1163             :                            "interface");
    1164           0 :                 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0);
    1165           0 :                 return 1;
    1166             :         }
    1167             : 
    1168        3850 :         return 0;
    1169             : }
    1170             : 
    1171             : 
    1172             : static struct wpa_driver_nl80211_data *
    1173        6613 : nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
    1174             : {
    1175             :         struct wpa_driver_nl80211_data *drv;
    1176       10238 :         dl_list_for_each(drv, &global->interfaces,
    1177             :                          struct wpa_driver_nl80211_data, list) {
    1178        8917 :                 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
    1179        3850 :                     have_ifidx(drv, idx))
    1180        1442 :                         return drv;
    1181             :         }
    1182        5171 :         return NULL;
    1183             : }
    1184             : 
    1185             : 
    1186        6509 : static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
    1187             :                                                  struct ifinfomsg *ifi,
    1188             :                                                  u8 *buf, size_t len)
    1189             : {
    1190        6509 :         struct nl80211_global *global = ctx;
    1191             :         struct wpa_driver_nl80211_data *drv;
    1192             :         int attrlen;
    1193             :         struct rtattr *attr;
    1194        6509 :         u32 brid = 0;
    1195             :         char namebuf[IFNAMSIZ];
    1196             :         char ifname[IFNAMSIZ + 1];
    1197             :         char extra[100], *pos, *end;
    1198             : 
    1199        6509 :         drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
    1200        6509 :         if (!drv) {
    1201        5072 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
    1202             :                            ifi->ifi_index);
    1203        5072 :                 return;
    1204             :         }
    1205             : 
    1206        1437 :         extra[0] = '\0';
    1207        1437 :         pos = extra;
    1208        1437 :         end = pos + sizeof(extra);
    1209        1437 :         ifname[0] = '\0';
    1210             : 
    1211        1437 :         attrlen = len;
    1212        1437 :         attr = (struct rtattr *) buf;
    1213       27939 :         while (RTA_OK(attr, attrlen)) {
    1214       25065 :                 switch (attr->rta_type) {
    1215             :                 case IFLA_IFNAME:
    1216        1437 :                         if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
    1217           0 :                                 break;
    1218        1437 :                         os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
    1219        1437 :                         ifname[RTA_PAYLOAD(attr)] = '\0';
    1220        1437 :                         break;
    1221             :                 case IFLA_MASTER:
    1222         100 :                         brid = nla_get_u32((struct nlattr *) attr);
    1223         100 :                         pos += os_snprintf(pos, end - pos, " master=%u", brid);
    1224         100 :                         break;
    1225             :                 case IFLA_WIRELESS:
    1226           0 :                         pos += os_snprintf(pos, end - pos, " wext");
    1227           0 :                         break;
    1228             :                 case IFLA_OPERSTATE:
    1229        1437 :                         pos += os_snprintf(pos, end - pos, " operstate=%u",
    1230             :                                            nla_get_u32((struct nlattr *) attr));
    1231        1437 :                         break;
    1232             :                 case IFLA_LINKMODE:
    1233        1363 :                         pos += os_snprintf(pos, end - pos, " linkmode=%u",
    1234             :                                            nla_get_u32((struct nlattr *) attr));
    1235        1363 :                         break;
    1236             :                 }
    1237       25065 :                 attr = RTA_NEXT(attr, attrlen);
    1238             :         }
    1239        1437 :         extra[sizeof(extra) - 1] = '\0';
    1240             : 
    1241        5748 :         wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_flags=0x%x (%s%s%s%s)",
    1242             :                    ifi->ifi_index, ifname, extra, ifi->ifi_flags,
    1243        1437 :                    (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
    1244        1437 :                    (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
    1245        1437 :                    (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
    1246        1437 :                    (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
    1247             : 
    1248        1437 :         if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
    1249         164 :                 if (if_indextoname(ifi->ifi_index, namebuf) &&
    1250          82 :                     linux_iface_up(drv->global->ioctl_sock,
    1251          82 :                                    drv->first_bss->ifname) > 0) {
    1252          82 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
    1253             :                                    "event since interface %s is up", namebuf);
    1254          82 :                         return;
    1255             :                 }
    1256           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
    1257           0 :                 if (drv->ignore_if_down_event) {
    1258           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
    1259             :                                    "event generated by mode change");
    1260           0 :                         drv->ignore_if_down_event = 0;
    1261             :                 } else {
    1262           0 :                         drv->if_disabled = 1;
    1263           0 :                         wpa_supplicant_event(drv->ctx,
    1264             :                                              EVENT_INTERFACE_DISABLED, NULL);
    1265             : 
    1266             :                         /*
    1267             :                          * Try to get drv again, since it may be removed as
    1268             :                          * part of the EVENT_INTERFACE_DISABLED handling for
    1269             :                          * dynamic interfaces
    1270             :                          */
    1271           0 :                         drv = nl80211_find_drv(global, ifi->ifi_index,
    1272             :                                                buf, len);
    1273           0 :                         if (!drv)
    1274           0 :                                 return;
    1275             :                 }
    1276             :         }
    1277             : 
    1278        1355 :         if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
    1279           0 :                 if (if_indextoname(ifi->ifi_index, namebuf) &&
    1280           0 :                     linux_iface_up(drv->global->ioctl_sock,
    1281           0 :                                    drv->first_bss->ifname) == 0) {
    1282           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
    1283             :                                    "event since interface %s is down",
    1284             :                                    namebuf);
    1285           0 :                 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
    1286           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
    1287             :                                    "event since interface %s does not exist",
    1288           0 :                                    drv->first_bss->ifname);
    1289           0 :                 } else if (drv->if_removed) {
    1290           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
    1291             :                                    "event since interface %s is marked "
    1292           0 :                                    "removed", drv->first_bss->ifname);
    1293             :                 } else {
    1294           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Interface up");
    1295           0 :                         drv->if_disabled = 0;
    1296           0 :                         wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
    1297             :                                              NULL);
    1298             :                 }
    1299             :         }
    1300             : 
    1301             :         /*
    1302             :          * Some drivers send the association event before the operup event--in
    1303             :          * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
    1304             :          * fails. This will hit us when wpa_supplicant does not need to do
    1305             :          * IEEE 802.1X authentication
    1306             :          */
    1307        2658 :         if (drv->operstate == 1 &&
    1308        2024 :             (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
    1309         721 :             !(ifi->ifi_flags & IFF_RUNNING)) {
    1310           1 :                 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
    1311           1 :                 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
    1312             :                                        -1, IF_OPER_UP);
    1313             :         }
    1314             : 
    1315        1355 :         if (ifname[0])
    1316        1355 :                 wpa_driver_nl80211_event_newlink(drv, ifname);
    1317             : 
    1318        1355 :         if (ifi->ifi_family == AF_BRIDGE && brid) {
    1319             :                 /* device has been added to bridge */
    1320          73 :                 if_indextoname(brid, namebuf);
    1321          73 :                 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
    1322             :                            brid, namebuf);
    1323          73 :                 add_ifidx(drv, brid);
    1324             :         }
    1325             : }
    1326             : 
    1327             : 
    1328         104 : static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
    1329             :                                                  struct ifinfomsg *ifi,
    1330             :                                                  u8 *buf, size_t len)
    1331             : {
    1332         104 :         struct nl80211_global *global = ctx;
    1333             :         struct wpa_driver_nl80211_data *drv;
    1334             :         int attrlen;
    1335             :         struct rtattr *attr;
    1336         104 :         u32 brid = 0;
    1337             :         char ifname[IFNAMSIZ + 1];
    1338             : 
    1339         104 :         drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
    1340         104 :         if (!drv) {
    1341          99 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
    1342             :                            ifi->ifi_index);
    1343         203 :                 return;
    1344             :         }
    1345             : 
    1346           5 :         ifname[0] = '\0';
    1347             : 
    1348           5 :         attrlen = len;
    1349           5 :         attr = (struct rtattr *) buf;
    1350          35 :         while (RTA_OK(attr, attrlen)) {
    1351          25 :                 switch (attr->rta_type) {
    1352             :                 case IFLA_IFNAME:
    1353           5 :                         if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
    1354           0 :                                 break;
    1355           5 :                         os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
    1356           5 :                         ifname[RTA_PAYLOAD(attr)] = '\0';
    1357           5 :                         break;
    1358             :                 case IFLA_MASTER:
    1359           5 :                         brid = nla_get_u32((struct nlattr *) attr);
    1360           5 :                         break;
    1361             :                 }
    1362          25 :                 attr = RTA_NEXT(attr, attrlen);
    1363             :         }
    1364             : 
    1365           5 :         if (ifname[0])
    1366           5 :                 wpa_driver_nl80211_event_dellink(drv, ifname);
    1367             : 
    1368           5 :         if (ifi->ifi_family == AF_BRIDGE && brid) {
    1369             :                 /* device has been removed from bridge */
    1370             :                 char namebuf[IFNAMSIZ];
    1371           5 :                 if_indextoname(brid, namebuf);
    1372           5 :                 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
    1373             :                            "%s", brid, namebuf);
    1374           5 :                 del_ifidx(drv, brid);
    1375             :         }
    1376             : }
    1377             : 
    1378             : 
    1379           0 : static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
    1380             :                             const u8 *frame, size_t len)
    1381             : {
    1382             :         const struct ieee80211_mgmt *mgmt;
    1383             :         union wpa_event_data event;
    1384             : 
    1385           0 :         wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
    1386           0 :         mgmt = (const struct ieee80211_mgmt *) frame;
    1387           0 :         if (len < 24 + sizeof(mgmt->u.auth)) {
    1388           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
    1389             :                            "frame");
    1390           0 :                 return;
    1391             :         }
    1392             : 
    1393           0 :         os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
    1394           0 :         os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
    1395           0 :         os_memset(&event, 0, sizeof(event));
    1396           0 :         os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
    1397           0 :         event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
    1398           0 :         event.auth.auth_transaction =
    1399           0 :                 le_to_host16(mgmt->u.auth.auth_transaction);
    1400           0 :         event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
    1401           0 :         if (len > 24 + sizeof(mgmt->u.auth)) {
    1402           0 :                 event.auth.ies = mgmt->u.auth.variable;
    1403           0 :                 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
    1404             :         }
    1405             : 
    1406           0 :         wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
    1407             : }
    1408             : 
    1409             : 
    1410           0 : static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
    1411             : {
    1412             :         struct nl_msg *msg;
    1413             :         int ret;
    1414             :         struct nl80211_bss_info_arg arg;
    1415             : 
    1416           0 :         os_memset(&arg, 0, sizeof(arg));
    1417           0 :         msg = nlmsg_alloc();
    1418           0 :         if (!msg)
    1419           0 :                 goto nla_put_failure;
    1420             : 
    1421           0 :         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
    1422           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    1423             : 
    1424           0 :         arg.drv = drv;
    1425           0 :         ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
    1426           0 :         msg = NULL;
    1427           0 :         if (ret == 0) {
    1428           0 :                 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
    1429           0 :                         arg.ibss_freq : arg.assoc_freq;
    1430           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
    1431             :                            "associated BSS from scan results: %u MHz", freq);
    1432           0 :                 if (freq)
    1433           0 :                         drv->assoc_freq = freq;
    1434           0 :                 return drv->assoc_freq;
    1435             :         }
    1436           0 :         wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
    1437             :                    "(%s)", ret, strerror(-ret));
    1438             : nla_put_failure:
    1439           0 :         nlmsg_free(msg);
    1440           0 :         return drv->assoc_freq;
    1441             : }
    1442             : 
    1443             : 
    1444           0 : static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
    1445             :                             const u8 *frame, size_t len)
    1446             : {
    1447             :         const struct ieee80211_mgmt *mgmt;
    1448             :         union wpa_event_data event;
    1449             :         u16 status;
    1450             : 
    1451           0 :         wpa_printf(MSG_DEBUG, "nl80211: Associate event");
    1452           0 :         mgmt = (const struct ieee80211_mgmt *) frame;
    1453           0 :         if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
    1454           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
    1455             :                            "frame");
    1456           0 :                 return;
    1457             :         }
    1458             : 
    1459           0 :         status = le_to_host16(mgmt->u.assoc_resp.status_code);
    1460           0 :         if (status != WLAN_STATUS_SUCCESS) {
    1461           0 :                 os_memset(&event, 0, sizeof(event));
    1462           0 :                 event.assoc_reject.bssid = mgmt->bssid;
    1463           0 :                 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
    1464           0 :                         event.assoc_reject.resp_ies =
    1465           0 :                                 (u8 *) mgmt->u.assoc_resp.variable;
    1466           0 :                         event.assoc_reject.resp_ies_len =
    1467           0 :                                 len - 24 - sizeof(mgmt->u.assoc_resp);
    1468             :                 }
    1469           0 :                 event.assoc_reject.status_code = status;
    1470             : 
    1471           0 :                 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
    1472           0 :                 return;
    1473             :         }
    1474             : 
    1475           0 :         drv->associated = 1;
    1476           0 :         os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
    1477           0 :         os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
    1478             : 
    1479           0 :         os_memset(&event, 0, sizeof(event));
    1480           0 :         if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
    1481           0 :                 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
    1482           0 :                 event.assoc_info.resp_ies_len =
    1483           0 :                         len - 24 - sizeof(mgmt->u.assoc_resp);
    1484             :         }
    1485             : 
    1486           0 :         event.assoc_info.freq = drv->assoc_freq;
    1487             : 
    1488           0 :         wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
    1489             : }
    1490             : 
    1491             : 
    1492           0 : static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
    1493             :                                enum nl80211_commands cmd, struct nlattr *status,
    1494             :                                struct nlattr *addr, struct nlattr *req_ie,
    1495             :                                struct nlattr *resp_ie)
    1496             : {
    1497             :         union wpa_event_data event;
    1498             : 
    1499           0 :         if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
    1500             :                 /*
    1501             :                  * Avoid reporting two association events that would confuse
    1502             :                  * the core code.
    1503             :                  */
    1504           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
    1505             :                            "when using userspace SME", cmd);
    1506           0 :                 return;
    1507             :         }
    1508             : 
    1509           0 :         if (cmd == NL80211_CMD_CONNECT)
    1510           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
    1511           0 :         else if (cmd == NL80211_CMD_ROAM)
    1512           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
    1513             : 
    1514           0 :         os_memset(&event, 0, sizeof(event));
    1515           0 :         if (cmd == NL80211_CMD_CONNECT &&
    1516           0 :             nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
    1517           0 :                 if (addr)
    1518           0 :                         event.assoc_reject.bssid = nla_data(addr);
    1519           0 :                 if (resp_ie) {
    1520           0 :                         event.assoc_reject.resp_ies = nla_data(resp_ie);
    1521           0 :                         event.assoc_reject.resp_ies_len = nla_len(resp_ie);
    1522             :                 }
    1523           0 :                 event.assoc_reject.status_code = nla_get_u16(status);
    1524           0 :                 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
    1525           0 :                 return;
    1526             :         }
    1527             : 
    1528           0 :         drv->associated = 1;
    1529           0 :         if (addr) {
    1530           0 :                 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
    1531           0 :                 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
    1532             :         }
    1533             : 
    1534           0 :         if (req_ie) {
    1535           0 :                 event.assoc_info.req_ies = nla_data(req_ie);
    1536           0 :                 event.assoc_info.req_ies_len = nla_len(req_ie);
    1537             :         }
    1538           0 :         if (resp_ie) {
    1539           0 :                 event.assoc_info.resp_ies = nla_data(resp_ie);
    1540           0 :                 event.assoc_info.resp_ies_len = nla_len(resp_ie);
    1541             :         }
    1542             : 
    1543           0 :         event.assoc_info.freq = nl80211_get_assoc_freq(drv);
    1544             : 
    1545           0 :         wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
    1546             : }
    1547             : 
    1548             : 
    1549           0 : static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
    1550             :                                   struct nlattr *reason, struct nlattr *addr,
    1551             :                                   struct nlattr *by_ap)
    1552             : {
    1553             :         union wpa_event_data data;
    1554           0 :         unsigned int locally_generated = by_ap == NULL;
    1555             : 
    1556           0 :         if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
    1557             :                 /*
    1558             :                  * Avoid reporting two disassociation events that could
    1559             :                  * confuse the core code.
    1560             :                  */
    1561           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
    1562             :                            "event when using userspace SME");
    1563           0 :                 return;
    1564             :         }
    1565             : 
    1566           0 :         if (drv->ignore_next_local_disconnect) {
    1567           0 :                 drv->ignore_next_local_disconnect = 0;
    1568           0 :                 if (locally_generated) {
    1569           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
    1570             :                                    "event triggered during reassociation");
    1571           0 :                         return;
    1572             :                 }
    1573           0 :                 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
    1574             :                            "disconnect but got another disconnect "
    1575             :                            "event first");
    1576             :         }
    1577             : 
    1578           0 :         wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
    1579           0 :         nl80211_mark_disconnected(drv);
    1580           0 :         os_memset(&data, 0, sizeof(data));
    1581           0 :         if (reason)
    1582           0 :                 data.deauth_info.reason_code = nla_get_u16(reason);
    1583           0 :         data.deauth_info.locally_generated = by_ap == NULL;
    1584           0 :         wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
    1585             : }
    1586             : 
    1587             : 
    1588           0 : static int calculate_chan_offset(int width, int freq, int cf1, int cf2)
    1589             : {
    1590           0 :         int freq1 = 0;
    1591             : 
    1592           0 :         switch (convert2width(width)) {
    1593             :         case CHAN_WIDTH_20_NOHT:
    1594             :         case CHAN_WIDTH_20:
    1595           0 :                 return 0;
    1596             :         case CHAN_WIDTH_40:
    1597           0 :                 freq1 = cf1 - 10;
    1598           0 :                 break;
    1599             :         case CHAN_WIDTH_80:
    1600           0 :                 freq1 = cf1 - 30;
    1601           0 :                 break;
    1602             :         case CHAN_WIDTH_160:
    1603           0 :                 freq1 = cf1 - 70;
    1604           0 :                 break;
    1605             :         case CHAN_WIDTH_UNKNOWN:
    1606             :         case CHAN_WIDTH_80P80:
    1607             :                 /* FIXME: implement this */
    1608           0 :                 return 0;
    1609             :         }
    1610             : 
    1611           0 :         return (abs(freq - freq1) / 20) % 2 == 0 ? 1 : -1;
    1612             : }
    1613             : 
    1614             : 
    1615           0 : static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
    1616             :                                  struct nlattr *ifindex, struct nlattr *freq,
    1617             :                                  struct nlattr *type, struct nlattr *bw,
    1618             :                                  struct nlattr *cf1, struct nlattr *cf2)
    1619             : {
    1620             :         struct i802_bss *bss;
    1621             :         union wpa_event_data data;
    1622           0 :         int ht_enabled = 1;
    1623           0 :         int chan_offset = 0;
    1624             :         int ifidx;
    1625             : 
    1626           0 :         wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
    1627             : 
    1628           0 :         if (!freq)
    1629           0 :                 return;
    1630             : 
    1631           0 :         ifidx = nla_get_u32(ifindex);
    1632           0 :         for (bss = drv->first_bss; bss; bss = bss->next)
    1633           0 :                 if (bss->ifindex == ifidx)
    1634           0 :                         break;
    1635             : 
    1636           0 :         if (bss == NULL) {
    1637           0 :                 wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
    1638             :                            ifidx);
    1639           0 :                 return;
    1640             :         }
    1641             : 
    1642           0 :         if (type) {
    1643           0 :                 switch (nla_get_u32(type)) {
    1644             :                 case NL80211_CHAN_NO_HT:
    1645           0 :                         ht_enabled = 0;
    1646           0 :                         break;
    1647             :                 case NL80211_CHAN_HT20:
    1648           0 :                         break;
    1649             :                 case NL80211_CHAN_HT40PLUS:
    1650           0 :                         chan_offset = 1;
    1651           0 :                         break;
    1652             :                 case NL80211_CHAN_HT40MINUS:
    1653           0 :                         chan_offset = -1;
    1654           0 :                         break;
    1655             :                 }
    1656           0 :         } else if (bw && cf1) {
    1657             :                 /* This can happen for example with VHT80 ch switch */
    1658           0 :                 chan_offset = calculate_chan_offset(nla_get_u32(bw),
    1659           0 :                                                     nla_get_u32(freq),
    1660           0 :                                                     nla_get_u32(cf1),
    1661           0 :                                                     cf2 ? nla_get_u32(cf2) : 0);
    1662             :         } else {
    1663           0 :                 wpa_printf(MSG_WARNING, "nl80211: Unknown secondary channel information - following channel definition calculations may fail");
    1664             :         }
    1665             : 
    1666           0 :         os_memset(&data, 0, sizeof(data));
    1667           0 :         data.ch_switch.freq = nla_get_u32(freq);
    1668           0 :         data.ch_switch.ht_enabled = ht_enabled;
    1669           0 :         data.ch_switch.ch_offset = chan_offset;
    1670           0 :         if (bw)
    1671           0 :                 data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
    1672           0 :         if (cf1)
    1673           0 :                 data.ch_switch.cf1 = nla_get_u32(cf1);
    1674           0 :         if (cf2)
    1675           0 :                 data.ch_switch.cf2 = nla_get_u32(cf2);
    1676             : 
    1677           0 :         bss->freq = data.ch_switch.freq;
    1678             : 
    1679           0 :         wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
    1680             : }
    1681             : 
    1682             : 
    1683           0 : static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
    1684             :                                enum nl80211_commands cmd, struct nlattr *addr)
    1685             : {
    1686             :         union wpa_event_data event;
    1687             :         enum wpa_event_type ev;
    1688             : 
    1689           0 :         if (nla_len(addr) != ETH_ALEN)
    1690           0 :                 return;
    1691             : 
    1692           0 :         wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
    1693           0 :                    cmd, MAC2STR((u8 *) nla_data(addr)));
    1694             : 
    1695           0 :         if (cmd == NL80211_CMD_AUTHENTICATE)
    1696           0 :                 ev = EVENT_AUTH_TIMED_OUT;
    1697           0 :         else if (cmd == NL80211_CMD_ASSOCIATE)
    1698           0 :                 ev = EVENT_ASSOC_TIMED_OUT;
    1699             :         else
    1700           0 :                 return;
    1701             : 
    1702           0 :         os_memset(&event, 0, sizeof(event));
    1703           0 :         os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
    1704           0 :         wpa_supplicant_event(drv->ctx, ev, &event);
    1705             : }
    1706             : 
    1707             : 
    1708        4555 : static void mlme_event_mgmt(struct i802_bss *bss,
    1709             :                             struct nlattr *freq, struct nlattr *sig,
    1710             :                             const u8 *frame, size_t len)
    1711             : {
    1712        4555 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    1713             :         const struct ieee80211_mgmt *mgmt;
    1714             :         union wpa_event_data event;
    1715             :         u16 fc, stype;
    1716        4555 :         int ssi_signal = 0;
    1717        4555 :         int rx_freq = 0;
    1718             : 
    1719        4555 :         wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
    1720        4555 :         mgmt = (const struct ieee80211_mgmt *) frame;
    1721        4555 :         if (len < 24) {
    1722           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
    1723        4555 :                 return;
    1724             :         }
    1725             : 
    1726        4555 :         fc = le_to_host16(mgmt->frame_control);
    1727        4555 :         stype = WLAN_FC_GET_STYPE(fc);
    1728             : 
    1729        4555 :         if (sig)
    1730        4555 :                 ssi_signal = (s32) nla_get_u32(sig);
    1731             : 
    1732        4555 :         os_memset(&event, 0, sizeof(event));
    1733        4555 :         if (freq) {
    1734        4555 :                 event.rx_mgmt.freq = nla_get_u32(freq);
    1735        4555 :                 rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
    1736             :         }
    1737        4555 :         wpa_printf(MSG_DEBUG,
    1738             :                    "nl80211: RX frame freq=%d ssi_signal=%d stype=%u len=%u",
    1739             :                    rx_freq, ssi_signal, stype, (unsigned int) len);
    1740        4555 :         event.rx_mgmt.frame = frame;
    1741        4555 :         event.rx_mgmt.frame_len = len;
    1742        4555 :         event.rx_mgmt.ssi_signal = ssi_signal;
    1743        4555 :         event.rx_mgmt.drv_priv = bss;
    1744        4555 :         wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
    1745             : }
    1746             : 
    1747             : 
    1748        2829 : static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
    1749             :                                       struct nlattr *cookie, const u8 *frame,
    1750             :                                       size_t len, struct nlattr *ack)
    1751             : {
    1752             :         union wpa_event_data event;
    1753             :         const struct ieee80211_hdr *hdr;
    1754             :         u16 fc;
    1755             : 
    1756        2829 :         wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
    1757        2829 :         if (!is_ap_interface(drv->nlmode)) {
    1758             :                 u64 cookie_val;
    1759             : 
    1760           0 :                 if (!cookie)
    1761           0 :                         return;
    1762             : 
    1763           0 :                 cookie_val = nla_get_u64(cookie);
    1764           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
    1765             :                            " cookie=0%llx%s (ack=%d)",
    1766             :                            (long long unsigned int) cookie_val,
    1767           0 :                            cookie_val == drv->send_action_cookie ?
    1768             :                            " (match)" : " (unknown)", ack != NULL);
    1769           0 :                 if (cookie_val != drv->send_action_cookie)
    1770           0 :                         return;
    1771             :         }
    1772             : 
    1773        2829 :         hdr = (const struct ieee80211_hdr *) frame;
    1774        2829 :         fc = le_to_host16(hdr->frame_control);
    1775             : 
    1776        2829 :         os_memset(&event, 0, sizeof(event));
    1777        2829 :         event.tx_status.type = WLAN_FC_GET_TYPE(fc);
    1778        2829 :         event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
    1779        2829 :         event.tx_status.dst = hdr->addr1;
    1780        2829 :         event.tx_status.data = frame;
    1781        2829 :         event.tx_status.data_len = len;
    1782        2829 :         event.tx_status.ack = ack != NULL;
    1783        2829 :         wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
    1784             : }
    1785             : 
    1786             : 
    1787           0 : static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
    1788             :                                        enum wpa_event_type type,
    1789             :                                        const u8 *frame, size_t len)
    1790             : {
    1791             :         const struct ieee80211_mgmt *mgmt;
    1792             :         union wpa_event_data event;
    1793           0 :         const u8 *bssid = NULL;
    1794           0 :         u16 reason_code = 0;
    1795             : 
    1796           0 :         if (type == EVENT_DEAUTH)
    1797           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
    1798             :         else
    1799           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
    1800             : 
    1801           0 :         mgmt = (const struct ieee80211_mgmt *) frame;
    1802           0 :         if (len >= 24) {
    1803           0 :                 bssid = mgmt->bssid;
    1804             : 
    1805           0 :                 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
    1806           0 :                     !drv->associated &&
    1807           0 :                     os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
    1808           0 :                     os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
    1809           0 :                     os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
    1810             :                         /*
    1811             :                          * Avoid issues with some roaming cases where
    1812             :                          * disconnection event for the old AP may show up after
    1813             :                          * we have started connection with the new AP.
    1814             :                          */
    1815           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
    1816           0 :                                    MAC2STR(bssid),
    1817           0 :                                    MAC2STR(drv->auth_attempt_bssid));
    1818           0 :                         return;
    1819             :                 }
    1820             : 
    1821           0 :                 if (drv->associated != 0 &&
    1822           0 :                     os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
    1823           0 :                     os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
    1824             :                         /*
    1825             :                          * We have presumably received this deauth as a
    1826             :                          * response to a clear_state_mismatch() outgoing
    1827             :                          * deauth.  Don't let it take us offline!
    1828             :                          */
    1829           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
    1830             :                                    "from Unknown BSSID " MACSTR " -- ignoring",
    1831           0 :                                    MAC2STR(bssid));
    1832           0 :                         return;
    1833             :                 }
    1834             :         }
    1835             : 
    1836           0 :         nl80211_mark_disconnected(drv);
    1837           0 :         os_memset(&event, 0, sizeof(event));
    1838             : 
    1839             :         /* Note: Same offset for Reason Code in both frame subtypes */
    1840           0 :         if (len >= 24 + sizeof(mgmt->u.deauth))
    1841           0 :                 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
    1842             : 
    1843           0 :         if (type == EVENT_DISASSOC) {
    1844           0 :                 event.disassoc_info.locally_generated =
    1845           0 :                         !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
    1846           0 :                 event.disassoc_info.addr = bssid;
    1847           0 :                 event.disassoc_info.reason_code = reason_code;
    1848           0 :                 if (frame + len > mgmt->u.disassoc.variable) {
    1849           0 :                         event.disassoc_info.ie = mgmt->u.disassoc.variable;
    1850           0 :                         event.disassoc_info.ie_len = frame + len -
    1851             :                                 mgmt->u.disassoc.variable;
    1852             :                 }
    1853             :         } else {
    1854           0 :                 if (drv->ignore_deauth_event) {
    1855           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event due to previous forced deauth-during-auth");
    1856           0 :                         drv->ignore_deauth_event = 0;
    1857           0 :                         return;
    1858             :                 }
    1859           0 :                 event.deauth_info.locally_generated =
    1860           0 :                         !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
    1861           0 :                 if (drv->ignore_next_local_deauth) {
    1862           0 :                         drv->ignore_next_local_deauth = 0;
    1863           0 :                         if (event.deauth_info.locally_generated) {
    1864           0 :                                 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event triggered due to own deauth request");
    1865           0 :                                 return;
    1866             :                         }
    1867           0 :                         wpa_printf(MSG_WARNING, "nl80211: Was expecting local deauth but got another disconnect event first");
    1868             :                 }
    1869           0 :                 event.deauth_info.addr = bssid;
    1870           0 :                 event.deauth_info.reason_code = reason_code;
    1871           0 :                 if (frame + len > mgmt->u.deauth.variable) {
    1872           0 :                         event.deauth_info.ie = mgmt->u.deauth.variable;
    1873           0 :                         event.deauth_info.ie_len = frame + len -
    1874             :                                 mgmt->u.deauth.variable;
    1875             :                 }
    1876             :         }
    1877             : 
    1878           0 :         wpa_supplicant_event(drv->ctx, type, &event);
    1879             : }
    1880             : 
    1881             : 
    1882           1 : static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
    1883             :                                          enum wpa_event_type type,
    1884             :                                          const u8 *frame, size_t len)
    1885             : {
    1886             :         const struct ieee80211_mgmt *mgmt;
    1887             :         union wpa_event_data event;
    1888           1 :         u16 reason_code = 0;
    1889             : 
    1890           1 :         if (type == EVENT_UNPROT_DEAUTH)
    1891           1 :                 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
    1892             :         else
    1893           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
    1894             : 
    1895           1 :         if (len < 24)
    1896           1 :                 return;
    1897             : 
    1898           1 :         mgmt = (const struct ieee80211_mgmt *) frame;
    1899             : 
    1900           1 :         os_memset(&event, 0, sizeof(event));
    1901             :         /* Note: Same offset for Reason Code in both frame subtypes */
    1902           1 :         if (len >= 24 + sizeof(mgmt->u.deauth))
    1903           1 :                 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
    1904             : 
    1905           1 :         if (type == EVENT_UNPROT_DISASSOC) {
    1906           0 :                 event.unprot_disassoc.sa = mgmt->sa;
    1907           0 :                 event.unprot_disassoc.da = mgmt->da;
    1908           0 :                 event.unprot_disassoc.reason_code = reason_code;
    1909             :         } else {
    1910           1 :                 event.unprot_deauth.sa = mgmt->sa;
    1911           1 :                 event.unprot_deauth.da = mgmt->da;
    1912           1 :                 event.unprot_deauth.reason_code = reason_code;
    1913             :         }
    1914             : 
    1915           1 :         wpa_supplicant_event(drv->ctx, type, &event);
    1916             : }
    1917             : 
    1918             : 
    1919        7386 : static void mlme_event(struct i802_bss *bss,
    1920             :                        enum nl80211_commands cmd, struct nlattr *frame,
    1921             :                        struct nlattr *addr, struct nlattr *timed_out,
    1922             :                        struct nlattr *freq, struct nlattr *ack,
    1923             :                        struct nlattr *cookie, struct nlattr *sig)
    1924             : {
    1925        7386 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    1926             :         const u8 *data;
    1927             :         size_t len;
    1928             : 
    1929        7386 :         if (timed_out && addr) {
    1930           0 :                 mlme_timeout_event(drv, cmd, addr);
    1931           0 :                 return;
    1932             :         }
    1933             : 
    1934        7386 :         if (frame == NULL) {
    1935           0 :                 wpa_printf(MSG_DEBUG,
    1936             :                            "nl80211: MLME event %d (%s) without frame data",
    1937             :                            cmd, nl80211_command_to_string(cmd));
    1938           0 :                 return;
    1939             :         }
    1940             : 
    1941        7386 :         data = nla_data(frame);
    1942        7386 :         len = nla_len(frame);
    1943        7386 :         if (len < 4 + 2 * ETH_ALEN) {
    1944           0 :                 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
    1945             :                            MACSTR ") - too short",
    1946           0 :                            cmd, nl80211_command_to_string(cmd), bss->ifname,
    1947           0 :                            MAC2STR(bss->addr));
    1948           0 :                 return;
    1949             :         }
    1950      140334 :         wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
    1951             :                    ") A1=" MACSTR " A2=" MACSTR, cmd,
    1952        7386 :                    nl80211_command_to_string(cmd), bss->ifname,
    1953       88632 :                    MAC2STR(bss->addr), MAC2STR(data + 4),
    1954       44316 :                    MAC2STR(data + 4 + ETH_ALEN));
    1955       10658 :         if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
    1956        3273 :             os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
    1957           1 :             os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
    1958           1 :                 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
    1959           1 :                            "for foreign address", bss->ifname);
    1960           1 :                 return;
    1961             :         }
    1962       14770 :         wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
    1963       14770 :                     nla_data(frame), nla_len(frame));
    1964             : 
    1965        7385 :         switch (cmd) {
    1966             :         case NL80211_CMD_AUTHENTICATE:
    1967           0 :                 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
    1968           0 :                 break;
    1969             :         case NL80211_CMD_ASSOCIATE:
    1970           0 :                 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
    1971           0 :                 break;
    1972             :         case NL80211_CMD_DEAUTHENTICATE:
    1973           0 :                 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
    1974           0 :                                            nla_data(frame), nla_len(frame));
    1975           0 :                 break;
    1976             :         case NL80211_CMD_DISASSOCIATE:
    1977           0 :                 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
    1978           0 :                                            nla_data(frame), nla_len(frame));
    1979           0 :                 break;
    1980             :         case NL80211_CMD_FRAME:
    1981        4555 :                 mlme_event_mgmt(bss, freq, sig, nla_data(frame),
    1982        4555 :                                 nla_len(frame));
    1983        4555 :                 break;
    1984             :         case NL80211_CMD_FRAME_TX_STATUS:
    1985        2829 :                 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
    1986        2829 :                                           nla_len(frame), ack);
    1987        2829 :                 break;
    1988             :         case NL80211_CMD_UNPROT_DEAUTHENTICATE:
    1989           2 :                 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
    1990           2 :                                              nla_data(frame), nla_len(frame));
    1991           1 :                 break;
    1992             :         case NL80211_CMD_UNPROT_DISASSOCIATE:
    1993           0 :                 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
    1994           0 :                                              nla_data(frame), nla_len(frame));
    1995           0 :                 break;
    1996             :         default:
    1997           0 :                 break;
    1998             :         }
    1999             : }
    2000             : 
    2001             : 
    2002           2 : static void mlme_event_michael_mic_failure(struct i802_bss *bss,
    2003             :                                            struct nlattr *tb[])
    2004             : {
    2005             :         union wpa_event_data data;
    2006             : 
    2007           2 :         wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
    2008           2 :         os_memset(&data, 0, sizeof(data));
    2009           2 :         if (tb[NL80211_ATTR_MAC]) {
    2010           4 :                 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
    2011           2 :                             nla_data(tb[NL80211_ATTR_MAC]),
    2012           2 :                             nla_len(tb[NL80211_ATTR_MAC]));
    2013           2 :                 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
    2014             :         }
    2015           2 :         if (tb[NL80211_ATTR_KEY_SEQ]) {
    2016           0 :                 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
    2017           0 :                             nla_data(tb[NL80211_ATTR_KEY_SEQ]),
    2018           0 :                             nla_len(tb[NL80211_ATTR_KEY_SEQ]));
    2019             :         }
    2020           2 :         if (tb[NL80211_ATTR_KEY_TYPE]) {
    2021           2 :                 enum nl80211_key_type key_type =
    2022           2 :                         nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
    2023           2 :                 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
    2024           2 :                 if (key_type == NL80211_KEYTYPE_PAIRWISE)
    2025           2 :                         data.michael_mic_failure.unicast = 1;
    2026             :         } else
    2027           0 :                 data.michael_mic_failure.unicast = 1;
    2028             : 
    2029           2 :         if (tb[NL80211_ATTR_KEY_IDX]) {
    2030           2 :                 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
    2031           2 :                 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
    2032             :         }
    2033             : 
    2034           2 :         wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
    2035           2 : }
    2036             : 
    2037             : 
    2038           0 : static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
    2039             :                                  struct nlattr *tb[])
    2040             : {
    2041             :         unsigned int freq;
    2042             : 
    2043           0 :         if (tb[NL80211_ATTR_MAC] == NULL) {
    2044           0 :                 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
    2045             :                            "event");
    2046           0 :                 return;
    2047             :         }
    2048           0 :         os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
    2049             : 
    2050           0 :         drv->associated = 1;
    2051           0 :         wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
    2052           0 :                    MAC2STR(drv->bssid));
    2053             : 
    2054           0 :         freq = nl80211_get_assoc_freq(drv);
    2055           0 :         if (freq) {
    2056           0 :                 wpa_printf(MSG_DEBUG, "nl80211: IBSS on frequency %u MHz",
    2057             :                            freq);
    2058           0 :                 drv->first_bss->freq = freq;
    2059             :         }
    2060             : 
    2061           0 :         wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
    2062             : }
    2063             : 
    2064             : 
    2065           0 : static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
    2066             :                                          int cancel_event, struct nlattr *tb[])
    2067             : {
    2068             :         unsigned int freq, chan_type, duration;
    2069             :         union wpa_event_data data;
    2070             :         u64 cookie;
    2071             : 
    2072           0 :         if (tb[NL80211_ATTR_WIPHY_FREQ])
    2073           0 :                 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
    2074             :         else
    2075           0 :                 freq = 0;
    2076             : 
    2077           0 :         if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
    2078           0 :                 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
    2079             :         else
    2080           0 :                 chan_type = 0;
    2081             : 
    2082           0 :         if (tb[NL80211_ATTR_DURATION])
    2083           0 :                 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
    2084             :         else
    2085           0 :                 duration = 0;
    2086             : 
    2087           0 :         if (tb[NL80211_ATTR_COOKIE])
    2088           0 :                 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
    2089             :         else
    2090           0 :                 cookie = 0;
    2091             : 
    2092           0 :         wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
    2093             :                    "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
    2094             :                    cancel_event, freq, chan_type, duration,
    2095             :                    (long long unsigned int) cookie,
    2096           0 :                    cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
    2097             : 
    2098           0 :         if (cookie != drv->remain_on_chan_cookie)
    2099           0 :                 return; /* not for us */
    2100             : 
    2101           0 :         if (cancel_event)
    2102           0 :                 drv->pending_remain_on_chan = 0;
    2103             : 
    2104           0 :         os_memset(&data, 0, sizeof(data));
    2105           0 :         data.remain_on_channel.freq = freq;
    2106           0 :         data.remain_on_channel.duration = duration;
    2107           0 :         wpa_supplicant_event(drv->ctx, cancel_event ?
    2108             :                              EVENT_CANCEL_REMAIN_ON_CHANNEL :
    2109             :                              EVENT_REMAIN_ON_CHANNEL, &data);
    2110             : }
    2111             : 
    2112             : 
    2113           0 : static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
    2114             :                                 struct nlattr *tb[])
    2115             : {
    2116             :         union wpa_event_data data;
    2117             : 
    2118           0 :         os_memset(&data, 0, sizeof(data));
    2119             : 
    2120           0 :         if (tb[NL80211_ATTR_IE]) {
    2121           0 :                 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
    2122           0 :                 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
    2123             :         }
    2124             : 
    2125           0 :         if (tb[NL80211_ATTR_IE_RIC]) {
    2126           0 :                 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
    2127           0 :                 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
    2128             :         }
    2129             : 
    2130           0 :         if (tb[NL80211_ATTR_MAC])
    2131           0 :                 os_memcpy(data.ft_ies.target_ap,
    2132             :                           nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
    2133             : 
    2134           0 :         wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
    2135           0 :                    MAC2STR(data.ft_ies.target_ap));
    2136             : 
    2137           0 :         wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
    2138           0 : }
    2139             : 
    2140             : 
    2141          59 : static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
    2142             :                             struct nlattr *tb[])
    2143             : {
    2144             :         union wpa_event_data event;
    2145             :         struct nlattr *nl;
    2146             :         int rem;
    2147             :         struct scan_info *info;
    2148             : #define MAX_REPORT_FREQS 50
    2149             :         int freqs[MAX_REPORT_FREQS];
    2150          59 :         int num_freqs = 0;
    2151             : 
    2152          59 :         if (drv->scan_for_auth) {
    2153           0 :                 drv->scan_for_auth = 0;
    2154           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
    2155             :                            "cfg80211 BSS entry");
    2156           0 :                 wpa_driver_nl80211_authenticate_retry(drv);
    2157          59 :                 return;
    2158             :         }
    2159             : 
    2160          59 :         os_memset(&event, 0, sizeof(event));
    2161          59 :         info = &event.scan_info;
    2162          59 :         info->aborted = aborted;
    2163             : 
    2164          59 :         if (tb[NL80211_ATTR_SCAN_SSIDS]) {
    2165          59 :                 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
    2166           0 :                         struct wpa_driver_scan_ssid *s =
    2167           0 :                                 &info->ssids[info->num_ssids];
    2168           0 :                         s->ssid = nla_data(nl);
    2169           0 :                         s->ssid_len = nla_len(nl);
    2170           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
    2171             :                                    wpa_ssid_txt(s->ssid, s->ssid_len));
    2172           0 :                         info->num_ssids++;
    2173           0 :                         if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
    2174           0 :                                 break;
    2175             :                 }
    2176             :         }
    2177          59 :         if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
    2178             :                 char msg[200], *pos, *end;
    2179             :                 int res;
    2180             : 
    2181          59 :                 pos = msg;
    2182          59 :                 end = pos + sizeof(msg);
    2183          59 :                 *pos = '\0';
    2184             : 
    2185         579 :                 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
    2186             :                 {
    2187         520 :                         freqs[num_freqs] = nla_get_u32(nl);
    2188         520 :                         res = os_snprintf(pos, end - pos, " %d",
    2189             :                                           freqs[num_freqs]);
    2190         520 :                         if (res > 0 && end - pos > res)
    2191         520 :                                 pos += res;
    2192         520 :                         num_freqs++;
    2193         520 :                         if (num_freqs == MAX_REPORT_FREQS - 1)
    2194           0 :                                 break;
    2195             :                 }
    2196          59 :                 info->freqs = freqs;
    2197          59 :                 info->num_freqs = num_freqs;
    2198          59 :                 wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
    2199             :                            msg);
    2200             :         }
    2201          59 :         wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
    2202             : }
    2203             : 
    2204             : 
    2205           0 : static int get_link_signal(struct nl_msg *msg, void *arg)
    2206             : {
    2207             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    2208           0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    2209             :         struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
    2210             :         static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
    2211             :                 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
    2212             :                 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
    2213             :         };
    2214             :         struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
    2215             :         static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
    2216             :                 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
    2217             :                 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
    2218             :                 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
    2219             :                 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
    2220             :         };
    2221           0 :         struct wpa_signal_info *sig_change = arg;
    2222             : 
    2223           0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    2224             :                   genlmsg_attrlen(gnlh, 0), NULL);
    2225           0 :         if (!tb[NL80211_ATTR_STA_INFO] ||
    2226           0 :             nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
    2227             :                              tb[NL80211_ATTR_STA_INFO], policy))
    2228           0 :                 return NL_SKIP;
    2229           0 :         if (!sinfo[NL80211_STA_INFO_SIGNAL])
    2230           0 :                 return NL_SKIP;
    2231             : 
    2232           0 :         sig_change->current_signal =
    2233           0 :                 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
    2234             : 
    2235           0 :         if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
    2236           0 :                 sig_change->avg_signal =
    2237           0 :                         (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
    2238             :         else
    2239           0 :                 sig_change->avg_signal = 0;
    2240             : 
    2241           0 :         if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
    2242           0 :                 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
    2243             :                                      sinfo[NL80211_STA_INFO_TX_BITRATE],
    2244             :                                      rate_policy)) {
    2245           0 :                         sig_change->current_txrate = 0;
    2246             :                 } else {
    2247           0 :                         if (rinfo[NL80211_RATE_INFO_BITRATE]) {
    2248           0 :                                 sig_change->current_txrate =
    2249           0 :                                         nla_get_u16(rinfo[
    2250           0 :                                              NL80211_RATE_INFO_BITRATE]) * 100;
    2251             :                         }
    2252             :                 }
    2253             :         }
    2254             : 
    2255           0 :         return NL_SKIP;
    2256             : }
    2257             : 
    2258             : 
    2259           0 : static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
    2260             :                                    struct wpa_signal_info *sig)
    2261             : {
    2262             :         struct nl_msg *msg;
    2263             : 
    2264           0 :         sig->current_signal = -9999;
    2265           0 :         sig->current_txrate = 0;
    2266             : 
    2267           0 :         msg = nlmsg_alloc();
    2268           0 :         if (!msg)
    2269           0 :                 return -ENOMEM;
    2270             : 
    2271           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
    2272             : 
    2273           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    2274           0 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
    2275             : 
    2276           0 :         return send_and_recv_msgs(drv, msg, get_link_signal, sig);
    2277             :  nla_put_failure:
    2278           0 :         nlmsg_free(msg);
    2279           0 :         return -ENOBUFS;
    2280             : }
    2281             : 
    2282             : 
    2283           0 : static int get_link_noise(struct nl_msg *msg, void *arg)
    2284             : {
    2285             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    2286           0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    2287             :         struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
    2288             :         static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
    2289             :                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
    2290             :                 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
    2291             :         };
    2292           0 :         struct wpa_signal_info *sig_change = arg;
    2293             : 
    2294           0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    2295             :                   genlmsg_attrlen(gnlh, 0), NULL);
    2296             : 
    2297           0 :         if (!tb[NL80211_ATTR_SURVEY_INFO]) {
    2298           0 :                 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
    2299           0 :                 return NL_SKIP;
    2300             :         }
    2301             : 
    2302           0 :         if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
    2303             :                              tb[NL80211_ATTR_SURVEY_INFO],
    2304             :                              survey_policy)) {
    2305           0 :                 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
    2306             :                            "attributes!");
    2307           0 :                 return NL_SKIP;
    2308             :         }
    2309             : 
    2310           0 :         if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
    2311           0 :                 return NL_SKIP;
    2312             : 
    2313           0 :         if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
    2314           0 :             sig_change->frequency)
    2315           0 :                 return NL_SKIP;
    2316             : 
    2317           0 :         if (!sinfo[NL80211_SURVEY_INFO_NOISE])
    2318           0 :                 return NL_SKIP;
    2319             : 
    2320           0 :         sig_change->current_noise =
    2321           0 :                 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
    2322             : 
    2323           0 :         return NL_SKIP;
    2324             : }
    2325             : 
    2326             : 
    2327           0 : static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
    2328             :                                   struct wpa_signal_info *sig_change)
    2329             : {
    2330             :         struct nl_msg *msg;
    2331             : 
    2332           0 :         sig_change->current_noise = 9999;
    2333           0 :         sig_change->frequency = drv->assoc_freq;
    2334             : 
    2335           0 :         msg = nlmsg_alloc();
    2336           0 :         if (!msg)
    2337           0 :                 return -ENOMEM;
    2338             : 
    2339           0 :         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
    2340             : 
    2341           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    2342             : 
    2343           0 :         return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
    2344             :  nla_put_failure:
    2345           0 :         nlmsg_free(msg);
    2346           0 :         return -ENOBUFS;
    2347             : }
    2348             : 
    2349             : 
    2350          24 : static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
    2351             : {
    2352             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    2353          24 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    2354             :         struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
    2355             :         static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
    2356             :                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
    2357             :                 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
    2358             :         };
    2359          24 :         struct wpa_scan_results *scan_results = arg;
    2360             :         struct wpa_scan_res *scan_res;
    2361             :         size_t i;
    2362             : 
    2363          24 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    2364             :                   genlmsg_attrlen(gnlh, 0), NULL);
    2365             : 
    2366          24 :         if (!tb[NL80211_ATTR_SURVEY_INFO]) {
    2367           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
    2368           0 :                 return NL_SKIP;
    2369             :         }
    2370             : 
    2371          24 :         if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
    2372             :                              tb[NL80211_ATTR_SURVEY_INFO],
    2373             :                              survey_policy)) {
    2374           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
    2375             :                            "attributes");
    2376           0 :                 return NL_SKIP;
    2377             :         }
    2378             : 
    2379          24 :         if (!sinfo[NL80211_SURVEY_INFO_NOISE])
    2380           0 :                 return NL_SKIP;
    2381             : 
    2382          24 :         if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
    2383           0 :                 return NL_SKIP;
    2384             : 
    2385          31 :         for (i = 0; i < scan_results->num; ++i) {
    2386           7 :                 scan_res = scan_results->res[i];
    2387           7 :                 if (!scan_res)
    2388           0 :                         continue;
    2389          14 :                 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
    2390           7 :                     scan_res->freq)
    2391           4 :                         continue;
    2392           3 :                 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
    2393           0 :                         continue;
    2394           3 :                 scan_res->noise = (s8)
    2395           3 :                         nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
    2396           3 :                 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
    2397             :         }
    2398             : 
    2399          24 :         return NL_SKIP;
    2400             : }
    2401             : 
    2402             : 
    2403          24 : static int nl80211_get_noise_for_scan_results(
    2404             :         struct wpa_driver_nl80211_data *drv,
    2405             :         struct wpa_scan_results *scan_res)
    2406             : {
    2407             :         struct nl_msg *msg;
    2408             : 
    2409          24 :         msg = nlmsg_alloc();
    2410          24 :         if (!msg)
    2411           0 :                 return -ENOMEM;
    2412             : 
    2413          24 :         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
    2414             : 
    2415          24 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    2416             : 
    2417          24 :         return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
    2418             :                                   scan_res);
    2419             :  nla_put_failure:
    2420           0 :         nlmsg_free(msg);
    2421           0 :         return -ENOBUFS;
    2422             : }
    2423             : 
    2424             : 
    2425           0 : static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
    2426             :                               struct nlattr *tb[])
    2427             : {
    2428             :         static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
    2429             :                 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
    2430             :                 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
    2431             :                 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
    2432             :                 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
    2433             :         };
    2434             :         struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
    2435             :         enum nl80211_cqm_rssi_threshold_event event;
    2436             :         union wpa_event_data ed;
    2437             :         struct wpa_signal_info sig;
    2438             :         int res;
    2439             : 
    2440           0 :         if (tb[NL80211_ATTR_CQM] == NULL ||
    2441           0 :             nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
    2442             :                              cqm_policy)) {
    2443           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
    2444           0 :                 return;
    2445             :         }
    2446             : 
    2447           0 :         os_memset(&ed, 0, sizeof(ed));
    2448             : 
    2449           0 :         if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
    2450           0 :                 if (!tb[NL80211_ATTR_MAC])
    2451           0 :                         return;
    2452           0 :                 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
    2453             :                           ETH_ALEN);
    2454           0 :                 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
    2455           0 :                 return;
    2456             :         }
    2457             : 
    2458           0 :         if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
    2459           0 :                 return;
    2460           0 :         event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
    2461             : 
    2462           0 :         if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
    2463           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
    2464             :                            "event: RSSI high");
    2465           0 :                 ed.signal_change.above_threshold = 1;
    2466           0 :         } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
    2467           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
    2468             :                            "event: RSSI low");
    2469           0 :                 ed.signal_change.above_threshold = 0;
    2470             :         } else
    2471           0 :                 return;
    2472             : 
    2473           0 :         res = nl80211_get_link_signal(drv, &sig);
    2474           0 :         if (res == 0) {
    2475           0 :                 ed.signal_change.current_signal = sig.current_signal;
    2476           0 :                 ed.signal_change.current_txrate = sig.current_txrate;
    2477           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm  txrate: %d",
    2478             :                            sig.current_signal, sig.current_txrate);
    2479             :         }
    2480             : 
    2481           0 :         res = nl80211_get_link_noise(drv, &sig);
    2482           0 :         if (res == 0) {
    2483           0 :                 ed.signal_change.current_noise = sig.current_noise;
    2484           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
    2485             :                            sig.current_noise);
    2486             :         }
    2487             : 
    2488           0 :         wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
    2489             : }
    2490             : 
    2491             : 
    2492         993 : static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
    2493             :                                       struct nlattr **tb)
    2494             : {
    2495             :         u8 *addr;
    2496             :         union wpa_event_data data;
    2497             : 
    2498         993 :         if (tb[NL80211_ATTR_MAC] == NULL)
    2499         993 :                 return;
    2500         993 :         addr = nla_data(tb[NL80211_ATTR_MAC]);
    2501         993 :         wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
    2502             : 
    2503         993 :         if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
    2504           0 :                 u8 *ies = NULL;
    2505           0 :                 size_t ies_len = 0;
    2506           0 :                 if (tb[NL80211_ATTR_IE]) {
    2507           0 :                         ies = nla_data(tb[NL80211_ATTR_IE]);
    2508           0 :                         ies_len = nla_len(tb[NL80211_ATTR_IE]);
    2509             :                 }
    2510           0 :                 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
    2511           0 :                 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
    2512           0 :                 return;
    2513             :         }
    2514             : 
    2515         993 :         if (drv->nlmode != NL80211_IFTYPE_ADHOC)
    2516         993 :                 return;
    2517             : 
    2518           0 :         os_memset(&data, 0, sizeof(data));
    2519           0 :         os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
    2520           0 :         wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
    2521             : }
    2522             : 
    2523             : 
    2524         952 : static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
    2525             :                                       struct nlattr **tb)
    2526             : {
    2527             :         u8 *addr;
    2528             :         union wpa_event_data data;
    2529             : 
    2530         952 :         if (tb[NL80211_ATTR_MAC] == NULL)
    2531         952 :                 return;
    2532         952 :         addr = nla_data(tb[NL80211_ATTR_MAC]);
    2533        5712 :         wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
    2534        5712 :                    MAC2STR(addr));
    2535             : 
    2536         952 :         if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
    2537           0 :                 drv_event_disassoc(drv->ctx, addr);
    2538           0 :                 return;
    2539             :         }
    2540             : 
    2541         952 :         if (drv->nlmode != NL80211_IFTYPE_ADHOC)
    2542         952 :                 return;
    2543             : 
    2544           0 :         os_memset(&data, 0, sizeof(data));
    2545           0 :         os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
    2546           0 :         wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
    2547             : }
    2548             : 
    2549             : 
    2550           0 : static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
    2551             :                                         struct nlattr **tb)
    2552             : {
    2553             :         struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
    2554             :         static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
    2555             :                 [NL80211_REKEY_DATA_KEK] = {
    2556             :                         .minlen = NL80211_KEK_LEN,
    2557             :                         .maxlen = NL80211_KEK_LEN,
    2558             :                 },
    2559             :                 [NL80211_REKEY_DATA_KCK] = {
    2560             :                         .minlen = NL80211_KCK_LEN,
    2561             :                         .maxlen = NL80211_KCK_LEN,
    2562             :                 },
    2563             :                 [NL80211_REKEY_DATA_REPLAY_CTR] = {
    2564             :                         .minlen = NL80211_REPLAY_CTR_LEN,
    2565             :                         .maxlen = NL80211_REPLAY_CTR_LEN,
    2566             :                 },
    2567             :         };
    2568             :         union wpa_event_data data;
    2569             : 
    2570           0 :         if (!tb[NL80211_ATTR_MAC])
    2571           0 :                 return;
    2572           0 :         if (!tb[NL80211_ATTR_REKEY_DATA])
    2573           0 :                 return;
    2574           0 :         if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
    2575           0 :                              tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
    2576           0 :                 return;
    2577           0 :         if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
    2578           0 :                 return;
    2579             : 
    2580           0 :         os_memset(&data, 0, sizeof(data));
    2581           0 :         data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
    2582           0 :         wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
    2583           0 :                    MAC2STR(data.driver_gtk_rekey.bssid));
    2584           0 :         data.driver_gtk_rekey.replay_ctr =
    2585           0 :                 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
    2586           0 :         wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
    2587           0 :                     data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
    2588           0 :         wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
    2589             : }
    2590             : 
    2591             : 
    2592           0 : static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
    2593             :                                           struct nlattr **tb)
    2594             : {
    2595             :         struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
    2596             :         static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
    2597             :                 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
    2598             :                 [NL80211_PMKSA_CANDIDATE_BSSID] = {
    2599             :                         .minlen = ETH_ALEN,
    2600             :                         .maxlen = ETH_ALEN,
    2601             :                 },
    2602             :                 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
    2603             :         };
    2604             :         union wpa_event_data data;
    2605             : 
    2606           0 :         wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
    2607             : 
    2608           0 :         if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
    2609           0 :                 return;
    2610           0 :         if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
    2611           0 :                              tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
    2612           0 :                 return;
    2613           0 :         if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
    2614           0 :             !cand[NL80211_PMKSA_CANDIDATE_BSSID])
    2615           0 :                 return;
    2616             : 
    2617           0 :         os_memset(&data, 0, sizeof(data));
    2618           0 :         os_memcpy(data.pmkid_candidate.bssid,
    2619             :                   nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
    2620           0 :         data.pmkid_candidate.index =
    2621           0 :                 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
    2622           0 :         data.pmkid_candidate.preauth =
    2623           0 :                 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
    2624           0 :         wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
    2625             : }
    2626             : 
    2627             : 
    2628           2 : static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
    2629             :                                        struct nlattr **tb)
    2630             : {
    2631             :         union wpa_event_data data;
    2632             : 
    2633           2 :         wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
    2634             : 
    2635           2 :         if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
    2636           4 :                 return;
    2637             : 
    2638           0 :         os_memset(&data, 0, sizeof(data));
    2639           0 :         os_memcpy(data.client_poll.addr,
    2640             :                   nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
    2641             : 
    2642           0 :         wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
    2643             : }
    2644             : 
    2645             : 
    2646           0 : static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
    2647             :                                     struct nlattr **tb)
    2648             : {
    2649             :         union wpa_event_data data;
    2650             : 
    2651           0 :         wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
    2652             : 
    2653           0 :         if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
    2654           0 :                 return;
    2655             : 
    2656           0 :         os_memset(&data, 0, sizeof(data));
    2657           0 :         os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
    2658           0 :         switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
    2659             :         case NL80211_TDLS_SETUP:
    2660           0 :                 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
    2661           0 :                            MACSTR, MAC2STR(data.tdls.peer));
    2662           0 :                 data.tdls.oper = TDLS_REQUEST_SETUP;
    2663           0 :                 break;
    2664             :         case NL80211_TDLS_TEARDOWN:
    2665           0 :                 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
    2666           0 :                            MACSTR, MAC2STR(data.tdls.peer));
    2667           0 :                 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
    2668           0 :                 break;
    2669             :         default:
    2670           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
    2671             :                            "event");
    2672           0 :                 return;
    2673             :         }
    2674           0 :         if (tb[NL80211_ATTR_REASON_CODE]) {
    2675           0 :                 data.tdls.reason_code =
    2676           0 :                         nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
    2677             :         }
    2678             : 
    2679           0 :         wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
    2680             : }
    2681             : 
    2682             : 
    2683           0 : static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
    2684             :                             struct nlattr **tb)
    2685             : {
    2686           0 :         wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
    2687           0 : }
    2688             : 
    2689             : 
    2690           0 : static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
    2691             :                                          struct nlattr **tb)
    2692             : {
    2693             :         union wpa_event_data data;
    2694             :         u32 reason;
    2695             : 
    2696           0 :         wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
    2697             : 
    2698           0 :         if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
    2699           0 :                 return;
    2700             : 
    2701           0 :         os_memset(&data, 0, sizeof(data));
    2702           0 :         os_memcpy(data.connect_failed_reason.addr,
    2703             :                   nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
    2704             : 
    2705           0 :         reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
    2706           0 :         switch (reason) {
    2707             :         case NL80211_CONN_FAIL_MAX_CLIENTS:
    2708           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
    2709           0 :                 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
    2710           0 :                 break;
    2711             :         case NL80211_CONN_FAIL_BLOCKED_CLIENT:
    2712           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
    2713             :                            " tried to connect",
    2714           0 :                            MAC2STR(data.connect_failed_reason.addr));
    2715           0 :                 data.connect_failed_reason.code = BLOCKED_CLIENT;
    2716           0 :                 break;
    2717             :         default:
    2718           0 :                 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
    2719             :                            "%u", reason);
    2720           0 :                 return;
    2721             :         }
    2722             : 
    2723           0 :         wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
    2724             : }
    2725             : 
    2726             : 
    2727           0 : static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
    2728             :                                 struct nlattr **tb)
    2729             : {
    2730             :         union wpa_event_data data;
    2731             :         enum nl80211_radar_event event_type;
    2732             : 
    2733           0 :         if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
    2734           0 :                 return;
    2735             : 
    2736           0 :         os_memset(&data, 0, sizeof(data));
    2737           0 :         data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
    2738           0 :         event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
    2739             : 
    2740             :         /* Check HT params */
    2741           0 :         if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
    2742           0 :                 data.dfs_event.ht_enabled = 1;
    2743           0 :                 data.dfs_event.chan_offset = 0;
    2744             : 
    2745           0 :                 switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
    2746             :                 case NL80211_CHAN_NO_HT:
    2747           0 :                         data.dfs_event.ht_enabled = 0;
    2748           0 :                         break;
    2749             :                 case NL80211_CHAN_HT20:
    2750           0 :                         break;
    2751             :                 case NL80211_CHAN_HT40PLUS:
    2752           0 :                         data.dfs_event.chan_offset = 1;
    2753           0 :                         break;
    2754             :                 case NL80211_CHAN_HT40MINUS:
    2755           0 :                         data.dfs_event.chan_offset = -1;
    2756           0 :                         break;
    2757             :                 }
    2758             :         }
    2759             : 
    2760             :         /* Get VHT params */
    2761           0 :         if (tb[NL80211_ATTR_CHANNEL_WIDTH])
    2762           0 :                 data.dfs_event.chan_width =
    2763           0 :                         convert2width(nla_get_u32(
    2764           0 :                                               tb[NL80211_ATTR_CHANNEL_WIDTH]));
    2765           0 :         if (tb[NL80211_ATTR_CENTER_FREQ1])
    2766           0 :                 data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
    2767           0 :         if (tb[NL80211_ATTR_CENTER_FREQ2])
    2768           0 :                 data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
    2769             : 
    2770           0 :         wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
    2771             :                    data.dfs_event.freq, data.dfs_event.ht_enabled,
    2772           0 :                    data.dfs_event.chan_offset, data.dfs_event.chan_width,
    2773             :                    data.dfs_event.cf1, data.dfs_event.cf2);
    2774             : 
    2775           0 :         switch (event_type) {
    2776             :         case NL80211_RADAR_DETECTED:
    2777           0 :                 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
    2778           0 :                 break;
    2779             :         case NL80211_RADAR_CAC_FINISHED:
    2780           0 :                 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
    2781           0 :                 break;
    2782             :         case NL80211_RADAR_CAC_ABORTED:
    2783           0 :                 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
    2784           0 :                 break;
    2785             :         case NL80211_RADAR_NOP_FINISHED:
    2786           0 :                 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
    2787           0 :                 break;
    2788             :         default:
    2789           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
    2790             :                            "received", event_type);
    2791           0 :                 break;
    2792             :         }
    2793             : }
    2794             : 
    2795             : 
    2796           2 : static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
    2797             :                                    int wds)
    2798             : {
    2799           2 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    2800             :         union wpa_event_data event;
    2801             : 
    2802           2 :         if (!tb[NL80211_ATTR_MAC])
    2803           2 :                 return;
    2804             : 
    2805           2 :         os_memset(&event, 0, sizeof(event));
    2806           2 :         event.rx_from_unknown.bssid = bss->addr;
    2807           2 :         event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
    2808           2 :         event.rx_from_unknown.wds = wds;
    2809             : 
    2810           2 :         wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
    2811             : }
    2812             : 
    2813             : 
    2814           0 : static void qca_nl80211_avoid_freq(struct wpa_driver_nl80211_data *drv,
    2815             :                                    const u8 *data, size_t len)
    2816             : {
    2817             :         u32 i, count;
    2818             :         union wpa_event_data event;
    2819           0 :         struct wpa_freq_range *range = NULL;
    2820             :         const struct qca_avoid_freq_list *freq_range;
    2821             : 
    2822           0 :         freq_range = (const struct qca_avoid_freq_list *) data;
    2823           0 :         if (len < sizeof(freq_range->count))
    2824           0 :                 return;
    2825             : 
    2826           0 :         count = freq_range->count;
    2827           0 :         if (len < sizeof(freq_range->count) +
    2828           0 :             count * sizeof(struct qca_avoid_freq_range)) {
    2829           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignored too short avoid frequency list (len=%u)",
    2830             :                            (unsigned int) len);
    2831           0 :                 return;
    2832             :         }
    2833             : 
    2834           0 :         if (count > 0) {
    2835           0 :                 range = os_calloc(count, sizeof(struct wpa_freq_range));
    2836           0 :                 if (range == NULL)
    2837           0 :                         return;
    2838             :         }
    2839             : 
    2840           0 :         os_memset(&event, 0, sizeof(event));
    2841           0 :         for (i = 0; i < count; i++) {
    2842           0 :                 unsigned int idx = event.freq_range.num;
    2843           0 :                 range[idx].min = freq_range->range[i].start_freq;
    2844           0 :                 range[idx].max = freq_range->range[i].end_freq;
    2845           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Avoid frequency range: %u-%u",
    2846           0 :                            range[idx].min, range[idx].max);
    2847           0 :                 if (range[idx].min > range[idx].max) {
    2848           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid frequency range");
    2849           0 :                         continue;
    2850             :                 }
    2851           0 :                 event.freq_range.num++;
    2852             :         }
    2853           0 :         event.freq_range.range = range;
    2854             : 
    2855           0 :         wpa_supplicant_event(drv->ctx, EVENT_AVOID_FREQUENCIES, &event);
    2856             : 
    2857           0 :         os_free(range);
    2858             : }
    2859             : 
    2860             : 
    2861           0 : static void nl80211_vendor_event_qca(struct wpa_driver_nl80211_data *drv,
    2862             :                                      u32 subcmd, u8 *data, size_t len)
    2863             : {
    2864           0 :         switch (subcmd) {
    2865             :         case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
    2866           0 :                 qca_nl80211_avoid_freq(drv, data, len);
    2867           0 :                 break;
    2868             :         default:
    2869           0 :                 wpa_printf(MSG_DEBUG,
    2870             :                            "nl80211: Ignore unsupported QCA vendor event %u",
    2871             :                            subcmd);
    2872           0 :                 break;
    2873             :         }
    2874           0 : }
    2875             : 
    2876             : 
    2877           0 : static void nl80211_vendor_event(struct wpa_driver_nl80211_data *drv,
    2878             :                                  struct nlattr **tb)
    2879             : {
    2880           0 :         u32 vendor_id, subcmd, wiphy = 0;
    2881             :         int wiphy_idx;
    2882           0 :         u8 *data = NULL;
    2883           0 :         size_t len = 0;
    2884             : 
    2885           0 :         if (!tb[NL80211_ATTR_VENDOR_ID] ||
    2886           0 :             !tb[NL80211_ATTR_VENDOR_SUBCMD])
    2887           0 :                 return;
    2888             : 
    2889           0 :         vendor_id = nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]);
    2890           0 :         subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
    2891             : 
    2892           0 :         if (tb[NL80211_ATTR_WIPHY])
    2893           0 :                 wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
    2894             : 
    2895           0 :         wpa_printf(MSG_DEBUG, "nl80211: Vendor event: wiphy=%u vendor_id=0x%x subcmd=%u",
    2896             :                    wiphy, vendor_id, subcmd);
    2897             : 
    2898           0 :         if (tb[NL80211_ATTR_VENDOR_DATA]) {
    2899           0 :                 data = nla_data(tb[NL80211_ATTR_VENDOR_DATA]);
    2900           0 :                 len = nla_len(tb[NL80211_ATTR_VENDOR_DATA]);
    2901           0 :                 wpa_hexdump(MSG_MSGDUMP, "nl80211: Vendor data", data, len);
    2902             :         }
    2903             : 
    2904           0 :         wiphy_idx = nl80211_get_wiphy_index(drv->first_bss);
    2905           0 :         if (wiphy_idx >= 0 && wiphy_idx != (int) wiphy) {
    2906           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore vendor event for foreign wiphy %u (own: %d)",
    2907             :                            wiphy, wiphy_idx);
    2908           0 :                 return;
    2909             :         }
    2910             : 
    2911           0 :         switch (vendor_id) {
    2912             :         case OUI_QCA:
    2913           0 :                 nl80211_vendor_event_qca(drv, subcmd, data, len);
    2914           0 :                 break;
    2915             :         default:
    2916           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event");
    2917           0 :                 break;
    2918             :         }
    2919             : }
    2920             : 
    2921             : 
    2922         798 : static void nl80211_reg_change_event(struct wpa_driver_nl80211_data *drv,
    2923             :                                      struct nlattr *tb[])
    2924             : {
    2925             :         union wpa_event_data data;
    2926             :         enum nl80211_reg_initiator init;
    2927             : 
    2928         798 :         wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
    2929             : 
    2930         798 :         if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
    2931         798 :                 return;
    2932             : 
    2933         798 :         os_memset(&data, 0, sizeof(data));
    2934         798 :         init = nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]);
    2935         798 :         wpa_printf(MSG_DEBUG, " * initiator=%d", init);
    2936         798 :         switch (init) {
    2937             :         case NL80211_REGDOM_SET_BY_CORE:
    2938         774 :                 data.channel_list_changed.initiator = REGDOM_SET_BY_CORE;
    2939         774 :                 break;
    2940             :         case NL80211_REGDOM_SET_BY_USER:
    2941          22 :                 data.channel_list_changed.initiator = REGDOM_SET_BY_USER;
    2942          22 :                 break;
    2943             :         case NL80211_REGDOM_SET_BY_DRIVER:
    2944           0 :                 data.channel_list_changed.initiator = REGDOM_SET_BY_DRIVER;
    2945           0 :                 break;
    2946             :         case NL80211_REGDOM_SET_BY_COUNTRY_IE:
    2947           2 :                 data.channel_list_changed.initiator = REGDOM_SET_BY_COUNTRY_IE;
    2948           2 :                 break;
    2949             :         }
    2950             : 
    2951         798 :         if (tb[NL80211_ATTR_REG_TYPE]) {
    2952             :                 enum nl80211_reg_type type;
    2953         798 :                 type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
    2954         798 :                 wpa_printf(MSG_DEBUG, " * type=%d", type);
    2955         798 :                 switch (type) {
    2956             :                 case NL80211_REGDOM_TYPE_COUNTRY:
    2957          24 :                         data.channel_list_changed.type = REGDOM_TYPE_COUNTRY;
    2958          24 :                         break;
    2959             :                 case NL80211_REGDOM_TYPE_WORLD:
    2960         774 :                         data.channel_list_changed.type = REGDOM_TYPE_WORLD;
    2961         774 :                         break;
    2962             :                 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
    2963           0 :                         data.channel_list_changed.type =
    2964             :                                 REGDOM_TYPE_CUSTOM_WORLD;
    2965           0 :                         break;
    2966             :                 case NL80211_REGDOM_TYPE_INTERSECTION:
    2967           0 :                         data.channel_list_changed.type =
    2968             :                                 REGDOM_TYPE_INTERSECTION;
    2969           0 :                         break;
    2970             :                 }
    2971             :         }
    2972             : 
    2973         798 :         if (tb[NL80211_ATTR_REG_ALPHA2]) {
    2974          24 :                 os_strlcpy(data.channel_list_changed.alpha2,
    2975          24 :                            nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
    2976             :                            sizeof(data.channel_list_changed.alpha2));
    2977          24 :                 wpa_printf(MSG_DEBUG, " * alpha2=%s",
    2978             :                            data.channel_list_changed.alpha2);
    2979             :         }
    2980             : 
    2981         798 :         wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED, &data);
    2982             : }
    2983             : 
    2984             : 
    2985        5700 : static void do_process_drv_event(struct i802_bss *bss, int cmd,
    2986             :                                  struct nlattr **tb)
    2987             : {
    2988        5700 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    2989             :         union wpa_event_data data;
    2990             : 
    2991        5700 :         wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
    2992        5700 :                    cmd, nl80211_command_to_string(cmd), bss->ifname);
    2993             : 
    2994        5700 :         if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
    2995           0 :             (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
    2996             :              cmd == NL80211_CMD_SCAN_ABORTED)) {
    2997           0 :                 wpa_driver_nl80211_set_mode(drv->first_bss,
    2998             :                                             drv->ap_scan_as_station);
    2999           0 :                 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
    3000             :         }
    3001             : 
    3002        5700 :         switch (cmd) {
    3003             :         case NL80211_CMD_TRIGGER_SCAN:
    3004          64 :                 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
    3005          64 :                 drv->scan_state = SCAN_STARTED;
    3006          64 :                 if (drv->scan_for_auth) {
    3007             :                         /*
    3008             :                          * Cannot indicate EVENT_SCAN_STARTED here since we skip
    3009             :                          * EVENT_SCAN_RESULTS in scan_for_auth case and the
    3010             :                          * upper layer implementation could get confused about
    3011             :                          * scanning state.
    3012             :                          */
    3013           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Do not indicate scan-start event due to internal scan_for_auth");
    3014           0 :                         break;
    3015             :                 }
    3016          64 :                 wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
    3017          64 :                 break;
    3018             :         case NL80211_CMD_START_SCHED_SCAN:
    3019           0 :                 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
    3020           0 :                 drv->scan_state = SCHED_SCAN_STARTED;
    3021           0 :                 break;
    3022             :         case NL80211_CMD_SCHED_SCAN_STOPPED:
    3023           0 :                 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
    3024           0 :                 drv->scan_state = SCHED_SCAN_STOPPED;
    3025           0 :                 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
    3026           0 :                 break;
    3027             :         case NL80211_CMD_NEW_SCAN_RESULTS:
    3028          59 :                 wpa_dbg(drv->ctx, MSG_DEBUG,
    3029             :                         "nl80211: New scan results available");
    3030          59 :                 drv->scan_state = SCAN_COMPLETED;
    3031          59 :                 drv->scan_complete_events = 1;
    3032          59 :                 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
    3033             :                                      drv->ctx);
    3034          59 :                 send_scan_event(drv, 0, tb);
    3035          59 :                 break;
    3036             :         case NL80211_CMD_SCHED_SCAN_RESULTS:
    3037           0 :                 wpa_dbg(drv->ctx, MSG_DEBUG,
    3038             :                         "nl80211: New sched scan results available");
    3039           0 :                 drv->scan_state = SCHED_SCAN_RESULTS;
    3040           0 :                 send_scan_event(drv, 0, tb);
    3041           0 :                 break;
    3042             :         case NL80211_CMD_SCAN_ABORTED:
    3043           0 :                 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
    3044           0 :                 drv->scan_state = SCAN_ABORTED;
    3045             :                 /*
    3046             :                  * Need to indicate that scan results are available in order
    3047             :                  * not to make wpa_supplicant stop its scanning.
    3048             :                  */
    3049           0 :                 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
    3050             :                                      drv->ctx);
    3051           0 :                 send_scan_event(drv, 1, tb);
    3052           0 :                 break;
    3053             :         case NL80211_CMD_AUTHENTICATE:
    3054             :         case NL80211_CMD_ASSOCIATE:
    3055             :         case NL80211_CMD_DEAUTHENTICATE:
    3056             :         case NL80211_CMD_DISASSOCIATE:
    3057             :         case NL80211_CMD_FRAME_TX_STATUS:
    3058             :         case NL80211_CMD_UNPROT_DEAUTHENTICATE:
    3059             :         case NL80211_CMD_UNPROT_DISASSOCIATE:
    3060       16980 :                 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
    3061        5660 :                            tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
    3062        5660 :                            tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
    3063        2830 :                            tb[NL80211_ATTR_COOKIE],
    3064        2830 :                            tb[NL80211_ATTR_RX_SIGNAL_DBM]);
    3065        2830 :                 break;
    3066             :         case NL80211_CMD_CONNECT:
    3067             :         case NL80211_CMD_ROAM:
    3068           0 :                 mlme_event_connect(drv, cmd,
    3069           0 :                                    tb[NL80211_ATTR_STATUS_CODE],
    3070           0 :                                    tb[NL80211_ATTR_MAC],
    3071           0 :                                    tb[NL80211_ATTR_REQ_IE],
    3072           0 :                                    tb[NL80211_ATTR_RESP_IE]);
    3073           0 :                 break;
    3074             :         case NL80211_CMD_CH_SWITCH_NOTIFY:
    3075           0 :                 mlme_event_ch_switch(drv,
    3076           0 :                                      tb[NL80211_ATTR_IFINDEX],
    3077           0 :                                      tb[NL80211_ATTR_WIPHY_FREQ],
    3078           0 :                                      tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
    3079           0 :                                      tb[NL80211_ATTR_CHANNEL_WIDTH],
    3080           0 :                                      tb[NL80211_ATTR_CENTER_FREQ1],
    3081           0 :                                      tb[NL80211_ATTR_CENTER_FREQ2]);
    3082           0 :                 break;
    3083             :         case NL80211_CMD_DISCONNECT:
    3084           0 :                 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
    3085           0 :                                       tb[NL80211_ATTR_MAC],
    3086           0 :                                       tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
    3087           0 :                 break;
    3088             :         case NL80211_CMD_MICHAEL_MIC_FAILURE:
    3089           2 :                 mlme_event_michael_mic_failure(bss, tb);
    3090           2 :                 break;
    3091             :         case NL80211_CMD_JOIN_IBSS:
    3092           0 :                 mlme_event_join_ibss(drv, tb);
    3093           0 :                 break;
    3094             :         case NL80211_CMD_REMAIN_ON_CHANNEL:
    3095           0 :                 mlme_event_remain_on_channel(drv, 0, tb);
    3096           0 :                 break;
    3097             :         case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
    3098           0 :                 mlme_event_remain_on_channel(drv, 1, tb);
    3099           0 :                 break;
    3100             :         case NL80211_CMD_NOTIFY_CQM:
    3101           0 :                 nl80211_cqm_event(drv, tb);
    3102           0 :                 break;
    3103             :         case NL80211_CMD_REG_CHANGE:
    3104         798 :                 nl80211_reg_change_event(drv, tb);
    3105         798 :                 break;
    3106             :         case NL80211_CMD_REG_BEACON_HINT:
    3107           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
    3108           0 :                 os_memset(&data, 0, sizeof(data));
    3109           0 :                 data.channel_list_changed.initiator = REGDOM_BEACON_HINT;
    3110           0 :                 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
    3111             :                                      &data);
    3112           0 :                 break;
    3113             :         case NL80211_CMD_NEW_STATION:
    3114         993 :                 nl80211_new_station_event(drv, tb);
    3115         993 :                 break;
    3116             :         case NL80211_CMD_DEL_STATION:
    3117         952 :                 nl80211_del_station_event(drv, tb);
    3118         952 :                 break;
    3119             :         case NL80211_CMD_SET_REKEY_OFFLOAD:
    3120           0 :                 nl80211_rekey_offload_event(drv, tb);
    3121           0 :                 break;
    3122             :         case NL80211_CMD_PMKSA_CANDIDATE:
    3123           0 :                 nl80211_pmksa_candidate_event(drv, tb);
    3124           0 :                 break;
    3125             :         case NL80211_CMD_PROBE_CLIENT:
    3126           2 :                 nl80211_client_probe_event(drv, tb);
    3127           2 :                 break;
    3128             :         case NL80211_CMD_TDLS_OPER:
    3129           0 :                 nl80211_tdls_oper_event(drv, tb);
    3130           0 :                 break;
    3131             :         case NL80211_CMD_CONN_FAILED:
    3132           0 :                 nl80211_connect_failed_event(drv, tb);
    3133           0 :                 break;
    3134             :         case NL80211_CMD_FT_EVENT:
    3135           0 :                 mlme_event_ft_event(drv, tb);
    3136           0 :                 break;
    3137             :         case NL80211_CMD_RADAR_DETECT:
    3138           0 :                 nl80211_radar_event(drv, tb);
    3139           0 :                 break;
    3140             :         case NL80211_CMD_STOP_AP:
    3141           0 :                 nl80211_stop_ap(drv, tb);
    3142           0 :                 break;
    3143             :         case NL80211_CMD_VENDOR:
    3144           0 :                 nl80211_vendor_event(drv, tb);
    3145           0 :                 break;
    3146             :         default:
    3147           0 :                 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
    3148             :                         "(cmd=%d)", cmd);
    3149           0 :                 break;
    3150             :         }
    3151        5700 : }
    3152             : 
    3153             : 
    3154           0 : static int process_drv_event(struct nl_msg *msg, void *arg)
    3155             : {
    3156           0 :         struct wpa_driver_nl80211_data *drv = arg;
    3157           0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    3158             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    3159             :         struct i802_bss *bss;
    3160           0 :         int ifidx = -1;
    3161             : 
    3162           0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    3163             :                   genlmsg_attrlen(gnlh, 0), NULL);
    3164             : 
    3165           0 :         if (tb[NL80211_ATTR_IFINDEX]) {
    3166           0 :                 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
    3167             : 
    3168           0 :                 for (bss = drv->first_bss; bss; bss = bss->next)
    3169           0 :                         if (ifidx == -1 || ifidx == bss->ifindex) {
    3170           0 :                                 do_process_drv_event(bss, gnlh->cmd, tb);
    3171           0 :                                 return NL_SKIP;
    3172             :                         }
    3173           0 :                 wpa_printf(MSG_DEBUG,
    3174             :                            "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
    3175           0 :                            gnlh->cmd, ifidx);
    3176           0 :         } else if (tb[NL80211_ATTR_WDEV]) {
    3177           0 :                 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
    3178           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
    3179           0 :                 for (bss = drv->first_bss; bss; bss = bss->next) {
    3180           0 :                         if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
    3181           0 :                                 do_process_drv_event(bss, gnlh->cmd, tb);
    3182           0 :                                 return NL_SKIP;
    3183             :                         }
    3184             :                 }
    3185           0 :                 wpa_printf(MSG_DEBUG,
    3186             :                            "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
    3187           0 :                            gnlh->cmd, (long long unsigned int) wdev_id);
    3188             :         }
    3189             : 
    3190           0 :         return NL_SKIP;
    3191             : }
    3192             : 
    3193             : 
    3194       24906 : static int process_global_event(struct nl_msg *msg, void *arg)
    3195             : {
    3196       24906 :         struct nl80211_global *global = arg;
    3197       24906 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    3198             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    3199             :         struct wpa_driver_nl80211_data *drv, *tmp;
    3200       24906 :         int ifidx = -1;
    3201             :         struct i802_bss *bss;
    3202       24906 :         u64 wdev_id = 0;
    3203       24906 :         int wdev_id_set = 0;
    3204             : 
    3205       24906 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    3206             :                   genlmsg_attrlen(gnlh, 0), NULL);
    3207             : 
    3208       24906 :         if (tb[NL80211_ATTR_IFINDEX])
    3209       23776 :                 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
    3210        1130 :         else if (tb[NL80211_ATTR_WDEV]) {
    3211           0 :                 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
    3212           0 :                 wdev_id_set = 1;
    3213             :         }
    3214             : 
    3215       38747 :         dl_list_for_each_safe(drv, tmp, &global->interfaces,
    3216             :                               struct wpa_driver_nl80211_data, list) {
    3217       34211 :                 for (bss = drv->first_bss; bss; bss = bss->next) {
    3218       39941 :                         if ((ifidx == -1 && !wdev_id_set) ||
    3219       34241 :                             ifidx == bss->ifindex ||
    3220           0 :                             (wdev_id_set && bss->wdev_id_set &&
    3221           0 :                              wdev_id == bss->wdev_id)) {
    3222        5700 :                                 do_process_drv_event(bss, gnlh->cmd, tb);
    3223        5700 :                                 return NL_SKIP;
    3224             :                         }
    3225             :                 }
    3226             :         }
    3227             : 
    3228       19206 :         return NL_SKIP;
    3229             : }
    3230             : 
    3231             : 
    3232        4558 : static int process_bss_event(struct nl_msg *msg, void *arg)
    3233             : {
    3234        4558 :         struct i802_bss *bss = arg;
    3235        4558 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    3236             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    3237             : 
    3238        4558 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    3239             :                   genlmsg_attrlen(gnlh, 0), NULL);
    3240             : 
    3241        9116 :         wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
    3242        9116 :                    gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
    3243        4558 :                    bss->ifname);
    3244             : 
    3245        4558 :         switch (gnlh->cmd) {
    3246             :         case NL80211_CMD_FRAME:
    3247             :         case NL80211_CMD_FRAME_TX_STATUS:
    3248        4556 :                 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
    3249             :                            tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
    3250             :                            tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
    3251             :                            tb[NL80211_ATTR_COOKIE],
    3252             :                            tb[NL80211_ATTR_RX_SIGNAL_DBM]);
    3253        4556 :                 break;
    3254             :         case NL80211_CMD_UNEXPECTED_FRAME:
    3255           1 :                 nl80211_spurious_frame(bss, tb, 0);
    3256           1 :                 break;
    3257             :         case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
    3258           1 :                 nl80211_spurious_frame(bss, tb, 1);
    3259           1 :                 break;
    3260             :         default:
    3261           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
    3262           0 :                            "(cmd=%d)", gnlh->cmd);
    3263           0 :                 break;
    3264             :         }
    3265             : 
    3266        4558 :         return NL_SKIP;
    3267             : }
    3268             : 
    3269             : 
    3270       29432 : static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
    3271             :                                              void *handle)
    3272             : {
    3273       29432 :         struct nl_cb *cb = eloop_ctx;
    3274             :         int res;
    3275             : 
    3276       29432 :         wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
    3277             : 
    3278       29432 :         res = nl_recvmsgs(handle, cb);
    3279       29432 :         if (res < 0) {
    3280           0 :                 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
    3281             :                            __func__, res);
    3282             :         }
    3283       29432 : }
    3284             : 
    3285             : 
    3286             : /**
    3287             :  * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
    3288             :  * @priv: driver_nl80211 private data
    3289             :  * @alpha2_arg: country to which to switch to
    3290             :  * Returns: 0 on success, -1 on failure
    3291             :  *
    3292             :  * This asks nl80211 to set the regulatory domain for given
    3293             :  * country ISO / IEC alpha2.
    3294             :  */
    3295          19 : static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
    3296             : {
    3297          19 :         struct i802_bss *bss = priv;
    3298          19 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    3299             :         char alpha2[3];
    3300             :         struct nl_msg *msg;
    3301             : 
    3302          19 :         msg = nlmsg_alloc();
    3303          19 :         if (!msg)
    3304           0 :                 return -ENOMEM;
    3305             : 
    3306          19 :         alpha2[0] = alpha2_arg[0];
    3307          19 :         alpha2[1] = alpha2_arg[1];
    3308          19 :         alpha2[2] = '\0';
    3309             : 
    3310          19 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
    3311             : 
    3312          19 :         NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
    3313          19 :         if (send_and_recv_msgs(drv, msg, NULL, NULL))
    3314           0 :                 return -EINVAL;
    3315          19 :         return 0;
    3316             : nla_put_failure:
    3317           0 :         nlmsg_free(msg);
    3318           0 :         return -EINVAL;
    3319             : }
    3320             : 
    3321             : 
    3322          19 : static int nl80211_get_country(struct nl_msg *msg, void *arg)
    3323             : {
    3324          19 :         char *alpha2 = arg;
    3325             :         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
    3326          19 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    3327             : 
    3328          19 :         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    3329             :                   genlmsg_attrlen(gnlh, 0), NULL);
    3330          19 :         if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
    3331           0 :                 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
    3332           0 :                 return NL_SKIP;
    3333             :         }
    3334          19 :         os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
    3335          19 :         return NL_SKIP;
    3336             : }
    3337             : 
    3338             : 
    3339          19 : static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
    3340             : {
    3341          19 :         struct i802_bss *bss = priv;
    3342          19 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    3343             :         struct nl_msg *msg;
    3344             :         int ret;
    3345             : 
    3346          19 :         msg = nlmsg_alloc();
    3347          19 :         if (!msg)
    3348           0 :                 return -ENOMEM;
    3349             : 
    3350          19 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
    3351          19 :         alpha2[0] = '\0';
    3352          19 :         ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
    3353          19 :         if (!alpha2[0])
    3354           0 :                 ret = -1;
    3355             : 
    3356          19 :         return ret;
    3357             : }
    3358             : 
    3359             : 
    3360        1160 : static int protocol_feature_handler(struct nl_msg *msg, void *arg)
    3361             : {
    3362        1160 :         u32 *feat = arg;
    3363             :         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
    3364        1160 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    3365             : 
    3366        1160 :         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    3367             :                   genlmsg_attrlen(gnlh, 0), NULL);
    3368             : 
    3369        1160 :         if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
    3370        1160 :                 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
    3371             : 
    3372        1160 :         return NL_SKIP;
    3373             : }
    3374             : 
    3375             : 
    3376        1160 : static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
    3377             : {
    3378        1160 :         u32 feat = 0;
    3379             :         struct nl_msg *msg;
    3380             : 
    3381        1160 :         msg = nlmsg_alloc();
    3382        1160 :         if (!msg)
    3383           0 :                 goto nla_put_failure;
    3384             : 
    3385        1160 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
    3386        1160 :         if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
    3387        1160 :                 return feat;
    3388             : 
    3389           0 :         msg = NULL;
    3390             : nla_put_failure:
    3391           0 :         nlmsg_free(msg);
    3392           0 :         return 0;
    3393             : }
    3394             : 
    3395             : 
    3396             : struct wiphy_info_data {
    3397             :         struct wpa_driver_nl80211_data *drv;
    3398             :         struct wpa_driver_capa *capa;
    3399             : 
    3400             :         unsigned int num_multichan_concurrent;
    3401             : 
    3402             :         unsigned int error:1;
    3403             :         unsigned int device_ap_sme:1;
    3404             :         unsigned int poll_command_supported:1;
    3405             :         unsigned int data_tx_status:1;
    3406             :         unsigned int monitor_supported:1;
    3407             :         unsigned int auth_supported:1;
    3408             :         unsigned int connect_supported:1;
    3409             :         unsigned int p2p_go_supported:1;
    3410             :         unsigned int p2p_client_supported:1;
    3411             :         unsigned int p2p_concurrent:1;
    3412             :         unsigned int channel_switch_supported:1;
    3413             :         unsigned int set_qos_map_supported:1;
    3414             : };
    3415             : 
    3416             : 
    3417           0 : static unsigned int probe_resp_offload_support(int supp_protocols)
    3418             : {
    3419           0 :         unsigned int prot = 0;
    3420             : 
    3421           0 :         if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
    3422           0 :                 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
    3423           0 :         if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
    3424           0 :                 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
    3425           0 :         if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
    3426           0 :                 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
    3427           0 :         if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
    3428           0 :                 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
    3429             : 
    3430           0 :         return prot;
    3431             : }
    3432             : 
    3433             : 
    3434       30740 : static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
    3435             :                                          struct nlattr *tb)
    3436             : {
    3437             :         struct nlattr *nl_mode;
    3438             :         int i;
    3439             : 
    3440       30740 :         if (tb == NULL)
    3441       60900 :                 return;
    3442             : 
    3443        5220 :         nla_for_each_nested(nl_mode, tb, i) {
    3444        4640 :                 switch (nla_type(nl_mode)) {
    3445             :                 case NL80211_IFTYPE_AP:
    3446         580 :                         info->capa->flags |= WPA_DRIVER_FLAGS_AP;
    3447         580 :                         break;
    3448             :                 case NL80211_IFTYPE_ADHOC:
    3449         580 :                         info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
    3450         580 :                         break;
    3451             :                 case NL80211_IFTYPE_P2P_DEVICE:
    3452         580 :                         info->capa->flags |=
    3453             :                                 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
    3454         580 :                         break;
    3455             :                 case NL80211_IFTYPE_P2P_GO:
    3456         580 :                         info->p2p_go_supported = 1;
    3457         580 :                         break;
    3458             :                 case NL80211_IFTYPE_P2P_CLIENT:
    3459         580 :                         info->p2p_client_supported = 1;
    3460         580 :                         break;
    3461             :                 case NL80211_IFTYPE_MONITOR:
    3462         580 :                         info->monitor_supported = 1;
    3463         580 :                         break;
    3464             :                 }
    3465             :         }
    3466             : }
    3467             : 
    3468             : 
    3469        1160 : static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
    3470             :                                          struct nlattr *nl_combi)
    3471             : {
    3472             :         struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
    3473             :         struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
    3474             :         struct nlattr *nl_limit, *nl_mode;
    3475             :         int err, rem_limit, rem_mode;
    3476        1160 :         int combination_has_p2p = 0, combination_has_mgd = 0;
    3477             :         static struct nla_policy
    3478             :         iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
    3479             :                 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
    3480             :                 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
    3481             :                 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
    3482             :                 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
    3483             :                 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
    3484             :         },
    3485             :         iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
    3486             :                 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
    3487             :                 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
    3488             :         };
    3489             : 
    3490        1160 :         err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
    3491             :                                nl_combi, iface_combination_policy);
    3492        2320 :         if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
    3493        2320 :             !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
    3494        1160 :             !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
    3495           0 :                 return 0; /* broken combination */
    3496             : 
    3497        1160 :         if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
    3498        1160 :                 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
    3499             : 
    3500        2320 :         nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
    3501             :                             rem_limit) {
    3502        1740 :                 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
    3503             :                                        nl_limit, iface_limit_policy);
    3504        1740 :                 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
    3505           0 :                         return 0; /* broken combination */
    3506             : 
    3507        5220 :                 nla_for_each_nested(nl_mode,
    3508             :                                     tb_limit[NL80211_IFACE_LIMIT_TYPES],
    3509             :                                     rem_mode) {
    3510        3480 :                         int ift = nla_type(nl_mode);
    3511        3480 :                         if (ift == NL80211_IFTYPE_P2P_GO ||
    3512             :                             ift == NL80211_IFTYPE_P2P_CLIENT)
    3513        1160 :                                 combination_has_p2p = 1;
    3514        3480 :                         if (ift == NL80211_IFTYPE_STATION)
    3515         580 :                                 combination_has_mgd = 1;
    3516             :                 }
    3517        1740 :                 if (combination_has_p2p && combination_has_mgd)
    3518         580 :                         break;
    3519             :         }
    3520             : 
    3521        1160 :         if (combination_has_p2p && combination_has_mgd) {
    3522         580 :                 unsigned int num_channels =
    3523         580 :                         nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
    3524             : 
    3525         580 :                 info->p2p_concurrent = 1;
    3526         580 :                 if (info->num_multichan_concurrent < num_channels)
    3527         580 :                         info->num_multichan_concurrent = num_channels;
    3528             :         }
    3529             : 
    3530        1160 :         return 0;
    3531             : }
    3532             : 
    3533             : 
    3534       30740 : static void wiphy_info_iface_comb(struct wiphy_info_data *info,
    3535             :                                   struct nlattr *tb)
    3536             : {
    3537             :         struct nlattr *nl_combi;
    3538             :         int rem_combi;
    3539             : 
    3540       30740 :         if (tb == NULL)
    3541       60900 :                 return;
    3542             : 
    3543        1740 :         nla_for_each_nested(nl_combi, tb, rem_combi) {
    3544        1160 :                 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
    3545           0 :                         break;
    3546             :         }
    3547             : }
    3548             : 
    3549             : 
    3550       30740 : static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
    3551             :                                  struct nlattr *tb)
    3552             : {
    3553             :         struct nlattr *nl_cmd;
    3554             :         int i;
    3555             : 
    3556       30740 :         if (tb == NULL)
    3557       60900 :                 return;
    3558             : 
    3559       16820 :         nla_for_each_nested(nl_cmd, tb, i) {
    3560       16240 :                 switch (nla_get_u32(nl_cmd)) {
    3561             :                 case NL80211_CMD_AUTHENTICATE:
    3562         580 :                         info->auth_supported = 1;
    3563         580 :                         break;
    3564             :                 case NL80211_CMD_CONNECT:
    3565         580 :                         info->connect_supported = 1;
    3566         580 :                         break;
    3567             :                 case NL80211_CMD_START_SCHED_SCAN:
    3568           0 :                         info->capa->sched_scan_supported = 1;
    3569           0 :                         break;
    3570             :                 case NL80211_CMD_PROBE_CLIENT:
    3571         580 :                         info->poll_command_supported = 1;
    3572         580 :                         break;
    3573             :                 case NL80211_CMD_CHANNEL_SWITCH:
    3574           0 :                         info->channel_switch_supported = 1;
    3575           0 :                         break;
    3576             :                 case NL80211_CMD_SET_QOS_MAP:
    3577         580 :                         info->set_qos_map_supported = 1;
    3578         580 :                         break;
    3579             :                 }
    3580             :         }
    3581             : }
    3582             : 
    3583             : 
    3584       30740 : static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
    3585             :                                      struct nlattr *tb)
    3586             : {
    3587             :         int i, num;
    3588             :         u32 *ciphers;
    3589             : 
    3590       30740 :         if (tb == NULL)
    3591       60900 :                 return;
    3592             : 
    3593         580 :         num = nla_len(tb) / sizeof(u32);
    3594         580 :         ciphers = nla_data(tb);
    3595        3480 :         for (i = 0; i < num; i++) {
    3596        2900 :                 u32 c = ciphers[i];
    3597             : 
    3598        8700 :                 wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
    3599        2900 :                            c >> 24, (c >> 16) & 0xff,
    3600        2900 :                            (c >> 8) & 0xff, c & 0xff);
    3601        2900 :                 switch (c) {
    3602             :                 case WLAN_CIPHER_SUITE_CCMP_256:
    3603           0 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
    3604           0 :                         break;
    3605             :                 case WLAN_CIPHER_SUITE_GCMP_256:
    3606           0 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
    3607           0 :                         break;
    3608             :                 case WLAN_CIPHER_SUITE_CCMP:
    3609         580 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
    3610         580 :                         break;
    3611             :                 case WLAN_CIPHER_SUITE_GCMP:
    3612           0 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
    3613           0 :                         break;
    3614             :                 case WLAN_CIPHER_SUITE_TKIP:
    3615         580 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
    3616         580 :                         break;
    3617             :                 case WLAN_CIPHER_SUITE_WEP104:
    3618         580 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
    3619         580 :                         break;
    3620             :                 case WLAN_CIPHER_SUITE_WEP40:
    3621         580 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
    3622         580 :                         break;
    3623             :                 case WLAN_CIPHER_SUITE_AES_CMAC:
    3624         580 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
    3625         580 :                         break;
    3626             :                 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
    3627           0 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
    3628           0 :                         break;
    3629             :                 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
    3630           0 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
    3631           0 :                         break;
    3632             :                 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
    3633           0 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
    3634           0 :                         break;
    3635             :                 case WLAN_CIPHER_SUITE_NO_GROUP_ADDR:
    3636           0 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_GTK_NOT_USED;
    3637           0 :                         break;
    3638             :                 }
    3639             :         }
    3640             : }
    3641             : 
    3642             : 
    3643       30740 : static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
    3644             :                                struct nlattr *tb)
    3645             : {
    3646       30740 :         if (tb)
    3647         580 :                 capa->max_remain_on_chan = nla_get_u32(tb);
    3648       30740 : }
    3649             : 
    3650             : 
    3651       30740 : static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
    3652             :                             struct nlattr *ext_setup)
    3653             : {
    3654       30740 :         if (tdls == NULL)
    3655       60900 :                 return;
    3656             : 
    3657         580 :         wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
    3658         580 :         capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
    3659             : 
    3660         580 :         if (ext_setup) {
    3661         580 :                 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
    3662         580 :                 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
    3663             :         }
    3664             : }
    3665             : 
    3666             : 
    3667       30740 : static void wiphy_info_feature_flags(struct wiphy_info_data *info,
    3668             :                                      struct nlattr *tb)
    3669             : {
    3670             :         u32 flags;
    3671       30740 :         struct wpa_driver_capa *capa = info->capa;
    3672             : 
    3673       30740 :         if (tb == NULL)
    3674       60900 :                 return;
    3675             : 
    3676         580 :         flags = nla_get_u32(tb);
    3677             : 
    3678         580 :         if (flags & NL80211_FEATURE_SK_TX_STATUS)
    3679         580 :                 info->data_tx_status = 1;
    3680             : 
    3681         580 :         if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
    3682           0 :                 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
    3683             : 
    3684         580 :         if (flags & NL80211_FEATURE_SAE)
    3685         580 :                 capa->flags |= WPA_DRIVER_FLAGS_SAE;
    3686             : 
    3687         580 :         if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
    3688           0 :                 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
    3689             : 
    3690         580 :         if (flags & NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)
    3691         580 :                 capa->flags |= WPA_DRIVER_FLAGS_HT_2040_COEX;
    3692             : }
    3693             : 
    3694             : 
    3695       30740 : static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
    3696             :                                           struct nlattr *tb)
    3697             : {
    3698             :         u32 protocols;
    3699             : 
    3700       30740 :         if (tb == NULL)
    3701       61480 :                 return;
    3702             : 
    3703           0 :         protocols = nla_get_u32(tb);
    3704           0 :         wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
    3705             :                    "mode");
    3706           0 :         capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
    3707           0 :         capa->probe_resp_offloads = probe_resp_offload_support(protocols);
    3708             : }
    3709             : 
    3710             : 
    3711       30740 : static void wiphy_info_wowlan_triggers(struct wpa_driver_capa *capa,
    3712             :                                        struct nlattr *tb)
    3713             : {
    3714             :         struct nlattr *triggers[MAX_NL80211_WOWLAN_TRIG + 1];
    3715             : 
    3716       30740 :         if (tb == NULL)
    3717       61480 :                 return;
    3718             : 
    3719           0 :         if (nla_parse_nested(triggers, MAX_NL80211_WOWLAN_TRIG,
    3720             :                              tb, NULL))
    3721           0 :                 return;
    3722             : 
    3723           0 :         if (triggers[NL80211_WOWLAN_TRIG_ANY])
    3724           0 :                 capa->wowlan_triggers.any = 1;
    3725           0 :         if (triggers[NL80211_WOWLAN_TRIG_DISCONNECT])
    3726           0 :                 capa->wowlan_triggers.disconnect = 1;
    3727           0 :         if (triggers[NL80211_WOWLAN_TRIG_MAGIC_PKT])
    3728           0 :                 capa->wowlan_triggers.magic_pkt = 1;
    3729           0 :         if (triggers[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE])
    3730           0 :                 capa->wowlan_triggers.gtk_rekey_failure = 1;
    3731           0 :         if (triggers[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST])
    3732           0 :                 capa->wowlan_triggers.eap_identity_req = 1;
    3733           0 :         if (triggers[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE])
    3734           0 :                 capa->wowlan_triggers.four_way_handshake = 1;
    3735           0 :         if (triggers[NL80211_WOWLAN_TRIG_RFKILL_RELEASE])
    3736           0 :                 capa->wowlan_triggers.rfkill_release = 1;
    3737             : }
    3738             : 
    3739             : 
    3740       30740 : static int wiphy_info_handler(struct nl_msg *msg, void *arg)
    3741             : {
    3742             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    3743       30740 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    3744       30740 :         struct wiphy_info_data *info = arg;
    3745       30740 :         struct wpa_driver_capa *capa = info->capa;
    3746       30740 :         struct wpa_driver_nl80211_data *drv = info->drv;
    3747             : 
    3748       30740 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    3749             :                   genlmsg_attrlen(gnlh, 0), NULL);
    3750             : 
    3751       30740 :         if (tb[NL80211_ATTR_WIPHY_NAME])
    3752       30740 :                 os_strlcpy(drv->phyname,
    3753       30740 :                            nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
    3754             :                            sizeof(drv->phyname));
    3755       30740 :         if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
    3756         580 :                 capa->max_scan_ssids =
    3757         580 :                         nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
    3758             : 
    3759       30740 :         if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
    3760         580 :                 capa->max_sched_scan_ssids =
    3761         580 :                         nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
    3762             : 
    3763       30740 :         if (tb[NL80211_ATTR_MAX_MATCH_SETS])
    3764         580 :                 capa->max_match_sets =
    3765         580 :                         nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
    3766             : 
    3767       30740 :         if (tb[NL80211_ATTR_MAC_ACL_MAX])
    3768           0 :                 capa->max_acl_mac_addrs =
    3769           0 :                         nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
    3770             : 
    3771       30740 :         wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
    3772       30740 :         wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
    3773       30740 :         wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
    3774       30740 :         wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
    3775             : 
    3776       30740 :         if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
    3777         580 :                 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
    3778             :                            "off-channel TX");
    3779         580 :                 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
    3780             :         }
    3781             : 
    3782       30740 :         if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
    3783           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
    3784           0 :                 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
    3785             :         }
    3786             : 
    3787       30740 :         wiphy_info_max_roc(capa,
    3788             :                            tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
    3789             : 
    3790       30740 :         if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
    3791         580 :                 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
    3792             : 
    3793       30740 :         wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
    3794             :                         tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
    3795             : 
    3796       30740 :         if (tb[NL80211_ATTR_DEVICE_AP_SME])
    3797           0 :                 info->device_ap_sme = 1;
    3798             : 
    3799       30740 :         wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
    3800       30740 :         wiphy_info_probe_resp_offload(capa,
    3801             :                                       tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
    3802             : 
    3803       31320 :         if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
    3804         580 :             drv->extended_capa == NULL) {
    3805         580 :                 drv->extended_capa =
    3806         580 :                         os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
    3807         580 :                 if (drv->extended_capa) {
    3808         580 :                         os_memcpy(drv->extended_capa,
    3809             :                                   nla_data(tb[NL80211_ATTR_EXT_CAPA]),
    3810             :                                   nla_len(tb[NL80211_ATTR_EXT_CAPA]));
    3811         580 :                         drv->extended_capa_len =
    3812         580 :                                 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
    3813             :                 }
    3814         580 :                 drv->extended_capa_mask =
    3815         580 :                         os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
    3816         580 :                 if (drv->extended_capa_mask) {
    3817         580 :                         os_memcpy(drv->extended_capa_mask,
    3818             :                                   nla_data(tb[NL80211_ATTR_EXT_CAPA]),
    3819             :                                   nla_len(tb[NL80211_ATTR_EXT_CAPA]));
    3820             :                 } else {
    3821           0 :                         os_free(drv->extended_capa);
    3822           0 :                         drv->extended_capa = NULL;
    3823           0 :                         drv->extended_capa_len = 0;
    3824             :                 }
    3825             :         }
    3826             : 
    3827       30740 :         if (tb[NL80211_ATTR_VENDOR_DATA]) {
    3828             :                 struct nlattr *nl;
    3829             :                 int rem;
    3830             : 
    3831           0 :                 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
    3832             :                         struct nl80211_vendor_cmd_info *vinfo;
    3833           0 :                         if (nla_len(nl) != sizeof(*vinfo)) {
    3834           0 :                                 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
    3835           0 :                                 continue;
    3836             :                         }
    3837           0 :                         vinfo = nla_data(nl);
    3838           0 :                         if (vinfo->subcmd ==
    3839             :                             QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY)
    3840           0 :                                 drv->dfs_vendor_cmd_avail = 1;
    3841             : 
    3842           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
    3843             :                                    vinfo->vendor_id, vinfo->subcmd);
    3844             :                 }
    3845             :         }
    3846             : 
    3847       30740 :         if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
    3848             :                 struct nlattr *nl;
    3849             :                 int rem;
    3850             : 
    3851           0 :                 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
    3852             :                         struct nl80211_vendor_cmd_info *vinfo;
    3853           0 :                         if (nla_len(nl) != sizeof(*vinfo)) {
    3854           0 :                                 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
    3855           0 :                                 continue;
    3856             :                         }
    3857           0 :                         vinfo = nla_data(nl);
    3858           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
    3859             :                                    vinfo->vendor_id, vinfo->subcmd);
    3860             :                 }
    3861             :         }
    3862             : 
    3863       30740 :         wiphy_info_wowlan_triggers(capa,
    3864             :                                    tb[NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED]);
    3865             : 
    3866       30740 :         if (tb[NL80211_ATTR_MAX_AP_ASSOC_STA])
    3867           0 :                 capa->max_stations =
    3868           0 :                         nla_get_u32(tb[NL80211_ATTR_MAX_AP_ASSOC_STA]);
    3869             : 
    3870       30740 :         return NL_SKIP;
    3871             : }
    3872             : 
    3873             : 
    3874         580 : static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
    3875             :                                        struct wiphy_info_data *info)
    3876             : {
    3877             :         u32 feat;
    3878             :         struct nl_msg *msg;
    3879             : 
    3880         580 :         os_memset(info, 0, sizeof(*info));
    3881         580 :         info->capa = &drv->capa;
    3882         580 :         info->drv = drv;
    3883             : 
    3884         580 :         msg = nlmsg_alloc();
    3885         580 :         if (!msg)
    3886           0 :                 return -1;
    3887             : 
    3888         580 :         feat = get_nl80211_protocol_features(drv);
    3889         580 :         if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
    3890         580 :                 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
    3891             :         else
    3892           0 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
    3893             : 
    3894         580 :         NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
    3895         580 :         if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
    3896           0 :                 goto nla_put_failure;
    3897             : 
    3898         580 :         if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
    3899           0 :                 return -1;
    3900             : 
    3901         580 :         if (info->auth_supported)
    3902         580 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
    3903           0 :         else if (!info->connect_supported) {
    3904           0 :                 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
    3905             :                            "authentication/association or connect commands");
    3906           0 :                 info->error = 1;
    3907             :         }
    3908             : 
    3909         580 :         if (info->p2p_go_supported && info->p2p_client_supported)
    3910         580 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
    3911         580 :         if (info->p2p_concurrent) {
    3912         580 :                 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
    3913             :                            "interface (driver advertised support)");
    3914         580 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
    3915         580 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
    3916             :         }
    3917         580 :         if (info->num_multichan_concurrent > 1) {
    3918           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
    3919             :                            "concurrent (driver advertised support)");
    3920           0 :                 drv->capa.num_multichan_concurrent =
    3921           0 :                         info->num_multichan_concurrent;
    3922             :         }
    3923             : 
    3924             :         /* default to 5000 since early versions of mac80211 don't set it */
    3925         580 :         if (!drv->capa.max_remain_on_chan)
    3926           0 :                 drv->capa.max_remain_on_chan = 5000;
    3927             : 
    3928         580 :         if (info->channel_switch_supported)
    3929           0 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
    3930             : 
    3931         580 :         return 0;
    3932             : nla_put_failure:
    3933           0 :         nlmsg_free(msg);
    3934           0 :         return -1;
    3935             : }
    3936             : 
    3937             : 
    3938         580 : static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
    3939             : {
    3940             :         struct wiphy_info_data info;
    3941         580 :         if (wpa_driver_nl80211_get_info(drv, &info))
    3942           0 :                 return -1;
    3943             : 
    3944         580 :         if (info.error)
    3945           0 :                 return -1;
    3946             : 
    3947         580 :         drv->has_capability = 1;
    3948         580 :         drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
    3949             :                 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
    3950             :                 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
    3951             :                 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
    3952         580 :         drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
    3953             :                 WPA_DRIVER_AUTH_SHARED |
    3954             :                 WPA_DRIVER_AUTH_LEAP;
    3955             : 
    3956         580 :         drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
    3957         580 :         drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
    3958         580 :         drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
    3959             : 
    3960             :         /*
    3961             :          * As all cfg80211 drivers must support cases where the AP interface is
    3962             :          * removed without the knowledge of wpa_supplicant/hostapd, e.g., in
    3963             :          * case that the user space daemon has crashed, they must be able to
    3964             :          * cleanup all stations and key entries in the AP tear down flow. Thus,
    3965             :          * this flag can/should always be set for cfg80211 drivers.
    3966             :          */
    3967         580 :         drv->capa.flags |= WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT;
    3968             : 
    3969         580 :         if (!info.device_ap_sme) {
    3970         580 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
    3971             : 
    3972             :                 /*
    3973             :                  * No AP SME is currently assumed to also indicate no AP MLME
    3974             :                  * in the driver/firmware.
    3975             :                  */
    3976         580 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
    3977             :         }
    3978             : 
    3979         580 :         drv->device_ap_sme = info.device_ap_sme;
    3980         580 :         drv->poll_command_supported = info.poll_command_supported;
    3981         580 :         drv->data_tx_status = info.data_tx_status;
    3982         580 :         if (info.set_qos_map_supported)
    3983         580 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
    3984             : 
    3985             :         /*
    3986             :          * If poll command and tx status are supported, mac80211 is new enough
    3987             :          * to have everything we need to not need monitor interfaces.
    3988             :          */
    3989         580 :         drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
    3990             : 
    3991         580 :         if (drv->device_ap_sme && drv->use_monitor) {
    3992             :                 /*
    3993             :                  * Non-mac80211 drivers may not support monitor interface.
    3994             :                  * Make sure we do not get stuck with incorrect capability here
    3995             :                  * by explicitly testing this.
    3996             :                  */
    3997           0 :                 if (!info.monitor_supported) {
    3998           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
    3999             :                                    "with device_ap_sme since no monitor mode "
    4000             :                                    "support detected");
    4001           0 :                         drv->use_monitor = 0;
    4002             :                 }
    4003             :         }
    4004             : 
    4005             :         /*
    4006             :          * If we aren't going to use monitor interfaces, but the
    4007             :          * driver doesn't support data TX status, we won't get TX
    4008             :          * status for EAPOL frames.
    4009             :          */
    4010         580 :         if (!drv->use_monitor && !info.data_tx_status)
    4011           0 :                 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
    4012             : 
    4013         580 :         return 0;
    4014             : }
    4015             : 
    4016             : 
    4017             : #ifdef ANDROID
    4018             : static int android_genl_ctrl_resolve(struct nl_handle *handle,
    4019             :                                      const char *name)
    4020             : {
    4021             :         /*
    4022             :          * Android ICS has very minimal genl_ctrl_resolve() implementation, so
    4023             :          * need to work around that.
    4024             :          */
    4025             :         struct nl_cache *cache = NULL;
    4026             :         struct genl_family *nl80211 = NULL;
    4027             :         int id = -1;
    4028             : 
    4029             :         if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
    4030             :                 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
    4031             :                            "netlink cache");
    4032             :                 goto fail;
    4033             :         }
    4034             : 
    4035             :         nl80211 = genl_ctrl_search_by_name(cache, name);
    4036             :         if (nl80211 == NULL)
    4037             :                 goto fail;
    4038             : 
    4039             :         id = genl_family_get_id(nl80211);
    4040             : 
    4041             : fail:
    4042             :         if (nl80211)
    4043             :                 genl_family_put(nl80211);
    4044             :         if (cache)
    4045             :                 nl_cache_free(cache);
    4046             : 
    4047             :         return id;
    4048             : }
    4049             : #define genl_ctrl_resolve android_genl_ctrl_resolve
    4050             : #endif /* ANDROID */
    4051             : 
    4052             : 
    4053           1 : static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
    4054             : {
    4055             :         int ret;
    4056             : 
    4057           1 :         global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
    4058           1 :         if (global->nl_cb == NULL) {
    4059           0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
    4060             :                            "callbacks");
    4061           0 :                 return -1;
    4062             :         }
    4063             : 
    4064           1 :         global->nl = nl_create_handle(global->nl_cb, "nl");
    4065           1 :         if (global->nl == NULL)
    4066           0 :                 goto err;
    4067             : 
    4068           1 :         global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
    4069           1 :         if (global->nl80211_id < 0) {
    4070           0 :                 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
    4071             :                            "found");
    4072           0 :                 goto err;
    4073             :         }
    4074             : 
    4075           1 :         global->nl_event = nl_create_handle(global->nl_cb, "event");
    4076           1 :         if (global->nl_event == NULL)
    4077           0 :                 goto err;
    4078             : 
    4079           1 :         ret = nl_get_multicast_id(global, "nl80211", "scan");
    4080           1 :         if (ret >= 0)
    4081           1 :                 ret = nl_socket_add_membership(global->nl_event, ret);
    4082           1 :         if (ret < 0) {
    4083           0 :                 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
    4084             :                            "membership for scan events: %d (%s)",
    4085             :                            ret, strerror(-ret));
    4086           0 :                 goto err;
    4087             :         }
    4088             : 
    4089           1 :         ret = nl_get_multicast_id(global, "nl80211", "mlme");
    4090           1 :         if (ret >= 0)
    4091           1 :                 ret = nl_socket_add_membership(global->nl_event, ret);
    4092           1 :         if (ret < 0) {
    4093           0 :                 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
    4094             :                            "membership for mlme events: %d (%s)",
    4095             :                            ret, strerror(-ret));
    4096           0 :                 goto err;
    4097             :         }
    4098             : 
    4099           1 :         ret = nl_get_multicast_id(global, "nl80211", "regulatory");
    4100           1 :         if (ret >= 0)
    4101           1 :                 ret = nl_socket_add_membership(global->nl_event, ret);
    4102           1 :         if (ret < 0) {
    4103           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
    4104             :                            "membership for regulatory events: %d (%s)",
    4105             :                            ret, strerror(-ret));
    4106             :                 /* Continue without regulatory events */
    4107             :         }
    4108             : 
    4109           1 :         ret = nl_get_multicast_id(global, "nl80211", "vendor");
    4110           1 :         if (ret >= 0)
    4111           1 :                 ret = nl_socket_add_membership(global->nl_event, ret);
    4112           1 :         if (ret < 0) {
    4113           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
    4114             :                            "membership for vendor events: %d (%s)",
    4115             :                            ret, strerror(-ret));
    4116             :                 /* Continue without vendor events */
    4117             :         }
    4118             : 
    4119           1 :         nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
    4120             :                   no_seq_check, NULL);
    4121           1 :         nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
    4122             :                   process_global_event, global);
    4123             : 
    4124           1 :         nl80211_register_eloop_read(&global->nl_event,
    4125             :                                     wpa_driver_nl80211_event_receive,
    4126           1 :                                     global->nl_cb);
    4127             : 
    4128           1 :         return 0;
    4129             : 
    4130             : err:
    4131           0 :         nl_destroy_handles(&global->nl_event);
    4132           0 :         nl_destroy_handles(&global->nl);
    4133           0 :         nl_cb_put(global->nl_cb);
    4134           0 :         global->nl_cb = NULL;
    4135           0 :         return -1;
    4136             : }
    4137             : 
    4138             : 
    4139         580 : static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
    4140             : {
    4141         580 :         drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
    4142         580 :         if (!drv->nl_cb) {
    4143           0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
    4144           0 :                 return -1;
    4145             :         }
    4146             : 
    4147         580 :         nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
    4148             :                   no_seq_check, NULL);
    4149         580 :         nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
    4150             :                   process_drv_event, drv);
    4151             : 
    4152         580 :         return 0;
    4153             : }
    4154             : 
    4155             : 
    4156           2 : static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
    4157             : {
    4158           2 :         wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
    4159             :         /*
    4160             :          * This may be for any interface; use ifdown event to disable
    4161             :          * interface.
    4162             :          */
    4163           2 : }
    4164             : 
    4165             : 
    4166           2 : static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
    4167             : {
    4168           2 :         struct wpa_driver_nl80211_data *drv = ctx;
    4169           2 :         wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
    4170           2 :         if (i802_set_iface_flags(drv->first_bss, 1)) {
    4171           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
    4172             :                            "after rfkill unblock");
    4173           0 :                 return;
    4174             :         }
    4175             :         /* rtnetlink ifup handler will report interface as enabled */
    4176             : }
    4177             : 
    4178             : 
    4179        3806 : static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
    4180             :                                                       void *eloop_ctx,
    4181             :                                                       void *handle)
    4182             : {
    4183        3806 :         struct wpa_driver_nl80211_data *drv = eloop_ctx;
    4184             :         u8 data[2048];
    4185             :         struct msghdr msg;
    4186             :         struct iovec entry;
    4187             :         u8 control[512];
    4188             :         struct cmsghdr *cmsg;
    4189        3806 :         int res, found_ee = 0, found_wifi = 0, acked = 0;
    4190             :         union wpa_event_data event;
    4191             : 
    4192        3806 :         memset(&msg, 0, sizeof(msg));
    4193        3806 :         msg.msg_iov = &entry;
    4194        3806 :         msg.msg_iovlen = 1;
    4195        3806 :         entry.iov_base = data;
    4196        3806 :         entry.iov_len = sizeof(data);
    4197        3806 :         msg.msg_control = &control;
    4198        3806 :         msg.msg_controllen = sizeof(control);
    4199             : 
    4200        3806 :         res = recvmsg(sock, &msg, MSG_ERRQUEUE);
    4201             :         /* if error or not fitting 802.3 header, return */
    4202        3806 :         if (res < 14)
    4203           0 :                 return;
    4204             : 
    4205       11418 :         for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
    4206             :         {
    4207       11418 :                 if (cmsg->cmsg_level == SOL_SOCKET &&
    4208        3806 :                     cmsg->cmsg_type == SCM_WIFI_STATUS) {
    4209             :                         int *ack;
    4210             : 
    4211        3806 :                         found_wifi = 1;
    4212        3806 :                         ack = (void *)CMSG_DATA(cmsg);
    4213        3806 :                         acked = *ack;
    4214             :                 }
    4215             : 
    4216       11418 :                 if (cmsg->cmsg_level == SOL_PACKET &&
    4217        3806 :                     cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
    4218        3806 :                         struct sock_extended_err *err =
    4219             :                                 (struct sock_extended_err *)CMSG_DATA(cmsg);
    4220             : 
    4221        3806 :                         if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
    4222        3806 :                                 found_ee = 1;
    4223             :                 }
    4224             :         }
    4225             : 
    4226        3806 :         if (!found_ee || !found_wifi)
    4227           0 :                 return;
    4228             : 
    4229        3806 :         memset(&event, 0, sizeof(event));
    4230        3806 :         event.eapol_tx_status.dst = data;
    4231        3806 :         event.eapol_tx_status.data = data + 14;
    4232        3806 :         event.eapol_tx_status.data_len = res - 14;
    4233        3806 :         event.eapol_tx_status.ack = acked;
    4234        3806 :         wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
    4235             : }
    4236             : 
    4237             : 
    4238         599 : static int nl80211_init_bss(struct i802_bss *bss)
    4239             : {
    4240         599 :         bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
    4241         599 :         if (!bss->nl_cb)
    4242           0 :                 return -1;
    4243             : 
    4244         599 :         nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
    4245             :                   no_seq_check, NULL);
    4246         599 :         nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
    4247             :                   process_bss_event, bss);
    4248             : 
    4249         599 :         return 0;
    4250             : }
    4251             : 
    4252             : 
    4253         599 : static void nl80211_destroy_bss(struct i802_bss *bss)
    4254             : {
    4255         599 :         nl_cb_put(bss->nl_cb);
    4256         599 :         bss->nl_cb = NULL;
    4257         599 : }
    4258             : 
    4259             : 
    4260         580 : static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
    4261             :                                           void *global_priv, int hostapd,
    4262             :                                           const u8 *set_addr)
    4263             : {
    4264             :         struct wpa_driver_nl80211_data *drv;
    4265             :         struct rfkill_config *rcfg;
    4266             :         struct i802_bss *bss;
    4267             : 
    4268         580 :         if (global_priv == NULL)
    4269           0 :                 return NULL;
    4270         580 :         drv = os_zalloc(sizeof(*drv));
    4271         580 :         if (drv == NULL)
    4272           0 :                 return NULL;
    4273         580 :         drv->global = global_priv;
    4274         580 :         drv->ctx = ctx;
    4275         580 :         drv->hostapd = !!hostapd;
    4276         580 :         drv->eapol_sock = -1;
    4277         580 :         drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
    4278         580 :         drv->if_indices = drv->default_if_indices;
    4279             : 
    4280         580 :         drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
    4281         580 :         if (!drv->first_bss) {
    4282           0 :                 os_free(drv);
    4283           0 :                 return NULL;
    4284             :         }
    4285         580 :         bss = drv->first_bss;
    4286         580 :         bss->drv = drv;
    4287         580 :         bss->ctx = ctx;
    4288             : 
    4289         580 :         os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
    4290         580 :         drv->monitor_ifidx = -1;
    4291         580 :         drv->monitor_sock = -1;
    4292         580 :         drv->eapol_tx_sock = -1;
    4293         580 :         drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
    4294             : 
    4295         580 :         if (wpa_driver_nl80211_init_nl(drv)) {
    4296           0 :                 os_free(drv);
    4297           0 :                 return NULL;
    4298             :         }
    4299             : 
    4300         580 :         if (nl80211_init_bss(bss))
    4301           0 :                 goto failed;
    4302             : 
    4303         580 :         rcfg = os_zalloc(sizeof(*rcfg));
    4304         580 :         if (rcfg == NULL)
    4305           0 :                 goto failed;
    4306         580 :         rcfg->ctx = drv;
    4307         580 :         os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
    4308         580 :         rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
    4309         580 :         rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
    4310         580 :         drv->rfkill = rfkill_init(rcfg);
    4311         580 :         if (drv->rfkill == NULL) {
    4312           0 :                 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
    4313           0 :                 os_free(rcfg);
    4314             :         }
    4315             : 
    4316         580 :         if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
    4317           0 :                 drv->start_iface_up = 1;
    4318             : 
    4319         580 :         if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1))
    4320           0 :                 goto failed;
    4321             : 
    4322         580 :         drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
    4323         580 :         if (drv->eapol_tx_sock < 0)
    4324           0 :                 goto failed;
    4325             : 
    4326         580 :         if (drv->data_tx_status) {
    4327         580 :                 int enabled = 1;
    4328             : 
    4329         580 :                 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
    4330             :                                &enabled, sizeof(enabled)) < 0) {
    4331           0 :                         wpa_printf(MSG_DEBUG,
    4332             :                                 "nl80211: wifi status sockopt failed\n");
    4333           0 :                         drv->data_tx_status = 0;
    4334           0 :                         if (!drv->use_monitor)
    4335           0 :                                 drv->capa.flags &=
    4336             :                                         ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
    4337             :                 } else {
    4338         580 :                         eloop_register_read_sock(drv->eapol_tx_sock,
    4339             :                                 wpa_driver_nl80211_handle_eapol_tx_status,
    4340             :                                 drv, NULL);
    4341             :                 }
    4342             :         }
    4343             : 
    4344         580 :         if (drv->global) {
    4345         580 :                 dl_list_add(&drv->global->interfaces, &drv->list);
    4346         580 :                 drv->in_interface_list = 1;
    4347             :         }
    4348             : 
    4349         580 :         return bss;
    4350             : 
    4351             : failed:
    4352           0 :         wpa_driver_nl80211_deinit(bss);
    4353           0 :         return NULL;
    4354             : }
    4355             : 
    4356             : 
    4357             : /**
    4358             :  * wpa_driver_nl80211_init - Initialize nl80211 driver interface
    4359             :  * @ctx: context to be used when calling wpa_supplicant functions,
    4360             :  * e.g., wpa_supplicant_event()
    4361             :  * @ifname: interface name, e.g., wlan0
    4362             :  * @global_priv: private driver global data from global_init()
    4363             :  * Returns: Pointer to private data, %NULL on failure
    4364             :  */
    4365           0 : static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
    4366             :                                       void *global_priv)
    4367             : {
    4368           0 :         return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL);
    4369             : }
    4370             : 
    4371             : 
    4372        4193 : static int nl80211_register_frame(struct i802_bss *bss,
    4373             :                                   struct nl_handle *nl_handle,
    4374             :                                   u16 type, const u8 *match, size_t match_len)
    4375             : {
    4376        4193 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4377             :         struct nl_msg *msg;
    4378        4193 :         int ret = -1;
    4379             :         char buf[30];
    4380             : 
    4381        4193 :         msg = nlmsg_alloc();
    4382        4193 :         if (!msg)
    4383           0 :                 return -1;
    4384             : 
    4385        4193 :         buf[0] = '\0';
    4386        4193 :         wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
    4387        4193 :         wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p match=%s",
    4388             :                    type, nl_handle, buf);
    4389             : 
    4390        4193 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
    4391             : 
    4392        4193 :         if (nl80211_set_iface_id(msg, bss) < 0)
    4393           0 :                 goto nla_put_failure;
    4394             : 
    4395        4193 :         NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
    4396        4193 :         NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
    4397             : 
    4398        4193 :         ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
    4399        4193 :         msg = NULL;
    4400        4193 :         if (ret) {
    4401           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
    4402             :                            "failed (type=%u): ret=%d (%s)",
    4403             :                            type, ret, strerror(-ret));
    4404           0 :                 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
    4405             :                             match, match_len);
    4406           0 :                 goto nla_put_failure;
    4407             :         }
    4408        4193 :         ret = 0;
    4409             : nla_put_failure:
    4410        4193 :         nlmsg_free(msg);
    4411        4193 :         return ret;
    4412             : }
    4413             : 
    4414             : 
    4415         599 : static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
    4416             : {
    4417         599 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4418             : 
    4419         599 :         if (bss->nl_mgmt) {
    4420           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
    4421             :                            "already on! (nl_mgmt=%p)", bss->nl_mgmt);
    4422           0 :                 return -1;
    4423             :         }
    4424             : 
    4425         599 :         bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
    4426         599 :         if (bss->nl_mgmt == NULL)
    4427           0 :                 return -1;
    4428             : 
    4429         599 :         return 0;
    4430             : }
    4431             : 
    4432             : 
    4433         599 : static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
    4434             : {
    4435         599 :         nl80211_register_eloop_read(&bss->nl_mgmt,
    4436             :                                     wpa_driver_nl80211_event_receive,
    4437         599 :                                     bss->nl_cb);
    4438         599 : }
    4439             : 
    4440             : 
    4441           0 : static int nl80211_register_action_frame(struct i802_bss *bss,
    4442             :                                          const u8 *match, size_t match_len)
    4443             : {
    4444           0 :         u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
    4445           0 :         return nl80211_register_frame(bss, bss->nl_mgmt,
    4446             :                                       type, match, match_len);
    4447             : }
    4448             : 
    4449             : 
    4450           0 : static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
    4451             : {
    4452           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4453           0 :         int ret = 0;
    4454             : 
    4455           0 :         if (nl80211_alloc_mgmt_handle(bss))
    4456           0 :                 return -1;
    4457           0 :         wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
    4458             :                    "handle %p", bss->nl_mgmt);
    4459             : 
    4460           0 :         if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
    4461           0 :                 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
    4462             : 
    4463             :                 /* register for any AUTH message */
    4464           0 :                 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
    4465             :         }
    4466             : 
    4467             : #ifdef CONFIG_INTERWORKING
    4468             :         /* QoS Map Configure */
    4469           0 :         if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
    4470           0 :                 ret = -1;
    4471             : #endif /* CONFIG_INTERWORKING */
    4472             : #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
    4473             :         /* GAS Initial Request */
    4474           0 :         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
    4475           0 :                 ret = -1;
    4476             :         /* GAS Initial Response */
    4477           0 :         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
    4478           0 :                 ret = -1;
    4479             :         /* GAS Comeback Request */
    4480           0 :         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
    4481           0 :                 ret = -1;
    4482             :         /* GAS Comeback Response */
    4483           0 :         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
    4484           0 :                 ret = -1;
    4485             :         /* Protected GAS Initial Request */
    4486           0 :         if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
    4487           0 :                 ret = -1;
    4488             :         /* Protected GAS Initial Response */
    4489           0 :         if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
    4490           0 :                 ret = -1;
    4491             :         /* Protected GAS Comeback Request */
    4492           0 :         if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
    4493           0 :                 ret = -1;
    4494             :         /* Protected GAS Comeback Response */
    4495           0 :         if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
    4496           0 :                 ret = -1;
    4497             : #endif /* CONFIG_P2P || CONFIG_INTERWORKING */
    4498             : #ifdef CONFIG_P2P
    4499             :         /* P2P Public Action */
    4500             :         if (nl80211_register_action_frame(bss,
    4501             :                                           (u8 *) "\x04\x09\x50\x6f\x9a\x09",
    4502             :                                           6) < 0)
    4503             :                 ret = -1;
    4504             :         /* P2P Action */
    4505             :         if (nl80211_register_action_frame(bss,
    4506             :                                           (u8 *) "\x7f\x50\x6f\x9a\x09",
    4507             :                                           5) < 0)
    4508             :                 ret = -1;
    4509             : #endif /* CONFIG_P2P */
    4510             : #ifdef CONFIG_IEEE80211W
    4511             :         /* SA Query Response */
    4512           0 :         if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
    4513           0 :                 ret = -1;
    4514             : #endif /* CONFIG_IEEE80211W */
    4515             : #ifdef CONFIG_TDLS
    4516             :         if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
    4517             :                 /* TDLS Discovery Response */
    4518             :                 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
    4519             :                     0)
    4520             :                         ret = -1;
    4521             :         }
    4522             : #endif /* CONFIG_TDLS */
    4523             : 
    4524             :         /* FT Action frames */
    4525           0 :         if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
    4526           0 :                 ret = -1;
    4527             :         else
    4528           0 :                 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
    4529             :                         WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
    4530             : 
    4531             :         /* WNM - BSS Transition Management Request */
    4532           0 :         if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
    4533           0 :                 ret = -1;
    4534             :         /* WNM-Sleep Mode Response */
    4535           0 :         if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
    4536           0 :                 ret = -1;
    4537             : 
    4538             : #ifdef CONFIG_HS20
    4539             :         /* WNM-Notification */
    4540           0 :         if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
    4541           0 :                 return -1;
    4542             : #endif /* CONFIG_HS20 */
    4543             : 
    4544           0 :         nl80211_mgmt_handle_register_eloop(bss);
    4545             : 
    4546           0 :         return ret;
    4547             : }
    4548             : 
    4549             : 
    4550         599 : static int nl80211_register_spurious_class3(struct i802_bss *bss)
    4551             : {
    4552         599 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4553             :         struct nl_msg *msg;
    4554         599 :         int ret = -1;
    4555             : 
    4556         599 :         msg = nlmsg_alloc();
    4557         599 :         if (!msg)
    4558           0 :                 return -1;
    4559             : 
    4560         599 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
    4561             : 
    4562         599 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
    4563             : 
    4564         599 :         ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
    4565         599 :         msg = NULL;
    4566         599 :         if (ret) {
    4567           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
    4568             :                            "failed: ret=%d (%s)",
    4569             :                            ret, strerror(-ret));
    4570           0 :                 goto nla_put_failure;
    4571             :         }
    4572         599 :         ret = 0;
    4573             : nla_put_failure:
    4574         599 :         nlmsg_free(msg);
    4575         599 :         return ret;
    4576             : }
    4577             : 
    4578             : 
    4579         599 : static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
    4580             : {
    4581             :         static const int stypes[] = {
    4582             :                 WLAN_FC_STYPE_AUTH,
    4583             :                 WLAN_FC_STYPE_ASSOC_REQ,
    4584             :                 WLAN_FC_STYPE_REASSOC_REQ,
    4585             :                 WLAN_FC_STYPE_DISASSOC,
    4586             :                 WLAN_FC_STYPE_DEAUTH,
    4587             :                 WLAN_FC_STYPE_ACTION,
    4588             :                 WLAN_FC_STYPE_PROBE_REQ,
    4589             : /* Beacon doesn't work as mac80211 doesn't currently allow
    4590             :  * it, but it wouldn't really be the right thing anyway as
    4591             :  * it isn't per interface ... maybe just dump the scan
    4592             :  * results periodically for OLBC?
    4593             :  */
    4594             :                 /* WLAN_FC_STYPE_BEACON, */
    4595             :         };
    4596             :         unsigned int i;
    4597             : 
    4598         599 :         if (nl80211_alloc_mgmt_handle(bss))
    4599           0 :                 return -1;
    4600         599 :         wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
    4601             :                    "handle %p", bss->nl_mgmt);
    4602             : 
    4603        4792 :         for (i = 0; i < ARRAY_SIZE(stypes); i++) {
    4604        4193 :                 if (nl80211_register_frame(bss, bss->nl_mgmt,
    4605             :                                            (WLAN_FC_TYPE_MGMT << 2) |
    4606        4193 :                                            (stypes[i] << 4),
    4607             :                                            NULL, 0) < 0) {
    4608           0 :                         goto out_err;
    4609             :                 }
    4610             :         }
    4611             : 
    4612         599 :         if (nl80211_register_spurious_class3(bss))
    4613           0 :                 goto out_err;
    4614             : 
    4615         599 :         if (nl80211_get_wiphy_data_ap(bss) == NULL)
    4616           0 :                 goto out_err;
    4617             : 
    4618         599 :         nl80211_mgmt_handle_register_eloop(bss);
    4619         599 :         return 0;
    4620             : 
    4621             : out_err:
    4622           0 :         nl_destroy_handles(&bss->nl_mgmt);
    4623           0 :         return -1;
    4624             : }
    4625             : 
    4626             : 
    4627           0 : static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
    4628             : {
    4629           0 :         if (nl80211_alloc_mgmt_handle(bss))
    4630           0 :                 return -1;
    4631           0 :         wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
    4632             :                    "handle %p (device SME)", bss->nl_mgmt);
    4633             : 
    4634           0 :         if (nl80211_register_frame(bss, bss->nl_mgmt,
    4635             :                                    (WLAN_FC_TYPE_MGMT << 2) |
    4636             :                                    (WLAN_FC_STYPE_ACTION << 4),
    4637             :                                    NULL, 0) < 0)
    4638           0 :                 goto out_err;
    4639             : 
    4640           0 :         nl80211_mgmt_handle_register_eloop(bss);
    4641           0 :         return 0;
    4642             : 
    4643             : out_err:
    4644           0 :         nl_destroy_handles(&bss->nl_mgmt);
    4645           0 :         return -1;
    4646             : }
    4647             : 
    4648             : 
    4649        1759 : static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
    4650             : {
    4651        1759 :         if (bss->nl_mgmt == NULL)
    4652        2919 :                 return;
    4653         599 :         wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
    4654             :                    "(%s)", bss->nl_mgmt, reason);
    4655         599 :         nl80211_destroy_eloop_handle(&bss->nl_mgmt);
    4656             : 
    4657         599 :         nl80211_put_wiphy_data_ap(bss);
    4658             : }
    4659             : 
    4660             : 
    4661           0 : static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
    4662             : {
    4663           0 :         wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
    4664           0 : }
    4665             : 
    4666             : 
    4667           0 : static void nl80211_del_p2pdev(struct i802_bss *bss)
    4668             : {
    4669           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4670             :         struct nl_msg *msg;
    4671             :         int ret;
    4672             : 
    4673           0 :         msg = nlmsg_alloc();
    4674           0 :         if (!msg)
    4675           0 :                 return;
    4676             : 
    4677           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
    4678           0 :         NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
    4679             : 
    4680           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    4681           0 :         msg = NULL;
    4682             : 
    4683           0 :         wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
    4684           0 :                    bss->ifname, (long long unsigned int) bss->wdev_id,
    4685             :                    strerror(-ret));
    4686             : 
    4687             : nla_put_failure:
    4688           0 :         nlmsg_free(msg);
    4689             : }
    4690             : 
    4691             : 
    4692           0 : static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
    4693             : {
    4694           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4695             :         struct nl_msg *msg;
    4696           0 :         int ret = -1;
    4697             : 
    4698           0 :         msg = nlmsg_alloc();
    4699           0 :         if (!msg)
    4700           0 :                 return -1;
    4701             : 
    4702           0 :         if (start)
    4703           0 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
    4704             :         else
    4705           0 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
    4706             : 
    4707           0 :         NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
    4708             : 
    4709           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    4710           0 :         msg = NULL;
    4711             : 
    4712           0 :         wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
    4713             :                    start ? "Start" : "Stop",
    4714           0 :                    bss->ifname, (long long unsigned int) bss->wdev_id,
    4715             :                    strerror(-ret));
    4716             : 
    4717             : nla_put_failure:
    4718           0 :         nlmsg_free(msg);
    4719           0 :         return ret;
    4720             : }
    4721             : 
    4722             : 
    4723        1162 : static int i802_set_iface_flags(struct i802_bss *bss, int up)
    4724             : {
    4725             :         enum nl80211_iftype nlmode;
    4726             : 
    4727        1162 :         nlmode = nl80211_get_ifmode(bss);
    4728        1162 :         if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
    4729        1162 :                 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
    4730        1162 :                                              bss->ifname, up);
    4731             :         }
    4732             : 
    4733             :         /* P2P Device has start/stop which is equivalent */
    4734           0 :         return nl80211_set_p2pdev(bss, up);
    4735             : }
    4736             : 
    4737             : 
    4738             : static int
    4739         580 : wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
    4740             :                                    const u8 *set_addr, int first)
    4741             : {
    4742         580 :         struct i802_bss *bss = drv->first_bss;
    4743         580 :         int send_rfkill_event = 0;
    4744             :         enum nl80211_iftype nlmode;
    4745             : 
    4746         580 :         drv->ifindex = if_nametoindex(bss->ifname);
    4747         580 :         bss->ifindex = drv->ifindex;
    4748         580 :         bss->wdev_id = drv->global->if_add_wdevid;
    4749         580 :         bss->wdev_id_set = drv->global->if_add_wdevid_set;
    4750             : 
    4751         580 :         bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
    4752         580 :         bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
    4753         580 :         drv->global->if_add_wdevid_set = 0;
    4754             : 
    4755         580 :         if (wpa_driver_nl80211_capa(drv))
    4756           0 :                 return -1;
    4757             : 
    4758         580 :         wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
    4759         580 :                    bss->ifname, drv->phyname);
    4760             : 
    4761         588 :         if (set_addr &&
    4762          16 :             (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
    4763           8 :              linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
    4764             :                                 set_addr)))
    4765           0 :                 return -1;
    4766             : 
    4767         580 :         if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
    4768           0 :                 drv->start_mode_ap = 1;
    4769             : 
    4770         580 :         if (drv->hostapd)
    4771         580 :                 nlmode = NL80211_IFTYPE_AP;
    4772           0 :         else if (bss->if_dynamic)
    4773           0 :                 nlmode = nl80211_get_ifmode(bss);
    4774             :         else
    4775           0 :                 nlmode = NL80211_IFTYPE_STATION;
    4776             : 
    4777         580 :         if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
    4778           0 :                 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
    4779           0 :                 return -1;
    4780             :         }
    4781             : 
    4782         580 :         if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
    4783           0 :                 nl80211_get_macaddr(bss);
    4784             : 
    4785         580 :         if (!rfkill_is_blocked(drv->rfkill)) {
    4786         580 :                 int ret = i802_set_iface_flags(bss, 1);
    4787         580 :                 if (ret) {
    4788           0 :                         wpa_printf(MSG_ERROR, "nl80211: Could not set "
    4789           0 :                                    "interface '%s' UP", bss->ifname);
    4790           0 :                         return ret;
    4791             :                 }
    4792         580 :                 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
    4793           0 :                         return ret;
    4794             :         } else {
    4795           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
    4796           0 :                            "interface '%s' due to rfkill", bss->ifname);
    4797           0 :                 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
    4798           0 :                         return 0;
    4799           0 :                 drv->if_disabled = 1;
    4800           0 :                 send_rfkill_event = 1;
    4801             :         }
    4802             : 
    4803         580 :         if (!drv->hostapd)
    4804           0 :                 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
    4805             :                                        1, IF_OPER_DORMANT);
    4806             : 
    4807         580 :         if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
    4808         580 :                                bss->addr))
    4809           0 :                 return -1;
    4810             : 
    4811         580 :         if (send_rfkill_event) {
    4812           0 :                 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
    4813             :                                        drv, drv->ctx);
    4814             :         }
    4815             : 
    4816         580 :         return 0;
    4817             : }
    4818             : 
    4819             : 
    4820         580 : static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
    4821             : {
    4822             :         struct nl_msg *msg;
    4823             : 
    4824         580 :         msg = nlmsg_alloc();
    4825         580 :         if (!msg)
    4826           0 :                 return -ENOMEM;
    4827             : 
    4828         580 :         wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
    4829             :                    drv->ifindex);
    4830         580 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
    4831         580 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    4832             : 
    4833         580 :         return send_and_recv_msgs(drv, msg, NULL, NULL);
    4834             :  nla_put_failure:
    4835           0 :         nlmsg_free(msg);
    4836           0 :         return -ENOBUFS;
    4837             : }
    4838             : 
    4839             : 
    4840             : /**
    4841             :  * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
    4842             :  * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
    4843             :  *
    4844             :  * Shut down driver interface and processing of driver events. Free
    4845             :  * private data buffer if one was allocated in wpa_driver_nl80211_init().
    4846             :  */
    4847         580 : static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
    4848             : {
    4849         580 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4850             : 
    4851         580 :         bss->in_deinit = 1;
    4852         580 :         if (drv->data_tx_status)
    4853         580 :                 eloop_unregister_read_sock(drv->eapol_tx_sock);
    4854         580 :         if (drv->eapol_tx_sock >= 0)
    4855         580 :                 close(drv->eapol_tx_sock);
    4856             : 
    4857         580 :         if (bss->nl_preq)
    4858           0 :                 wpa_driver_nl80211_probe_req_report(bss, 0);
    4859         580 :         if (bss->added_if_into_bridge) {
    4860           4 :                 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
    4861           4 :                                     bss->ifname) < 0)
    4862           8 :                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
    4863             :                                    "interface %s from bridge %s: %s",
    4864           8 :                                    bss->ifname, bss->brname, strerror(errno));
    4865             :         }
    4866         580 :         if (bss->added_bridge) {
    4867           2 :                 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
    4868           4 :                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
    4869             :                                    "bridge %s: %s",
    4870           4 :                                    bss->brname, strerror(errno));
    4871             :         }
    4872             : 
    4873         580 :         nl80211_remove_monitor_interface(drv);
    4874             : 
    4875         580 :         if (is_ap_interface(drv->nlmode))
    4876         580 :                 wpa_driver_nl80211_del_beacon(drv);
    4877             : 
    4878         580 :         if (drv->eapol_sock >= 0) {
    4879         580 :                 eloop_unregister_read_sock(drv->eapol_sock);
    4880         580 :                 close(drv->eapol_sock);
    4881             :         }
    4882             : 
    4883         580 :         if (drv->if_indices != drv->default_if_indices)
    4884           0 :                 os_free(drv->if_indices);
    4885             : 
    4886         580 :         if (drv->disabled_11b_rates)
    4887           0 :                 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
    4888             : 
    4889         580 :         netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
    4890             :                                IF_OPER_UP);
    4891         580 :         eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
    4892         580 :         rfkill_deinit(drv->rfkill);
    4893             : 
    4894         580 :         eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
    4895             : 
    4896         580 :         if (!drv->start_iface_up)
    4897         580 :                 (void) i802_set_iface_flags(bss, 0);
    4898         580 :         if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
    4899         580 :                 if (!drv->hostapd || !drv->start_mode_ap)
    4900         580 :                         wpa_driver_nl80211_set_mode(bss,
    4901             :                                                     NL80211_IFTYPE_STATION);
    4902         580 :                 nl80211_mgmt_unsubscribe(bss, "deinit");
    4903             :         } else {
    4904           0 :                 nl80211_mgmt_unsubscribe(bss, "deinit");
    4905           0 :                 nl80211_del_p2pdev(bss);
    4906             :         }
    4907         580 :         nl_cb_put(drv->nl_cb);
    4908             : 
    4909         580 :         nl80211_destroy_bss(drv->first_bss);
    4910             : 
    4911         580 :         os_free(drv->filter_ssids);
    4912             : 
    4913         580 :         os_free(drv->auth_ie);
    4914             : 
    4915         580 :         if (drv->in_interface_list)
    4916         580 :                 dl_list_del(&drv->list);
    4917             : 
    4918         580 :         os_free(drv->extended_capa);
    4919         580 :         os_free(drv->extended_capa_mask);
    4920         580 :         os_free(drv->first_bss);
    4921         580 :         os_free(drv);
    4922         580 : }
    4923             : 
    4924             : 
    4925             : /**
    4926             :  * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
    4927             :  * @eloop_ctx: Driver private data
    4928             :  * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
    4929             :  *
    4930             :  * This function can be used as registered timeout when starting a scan to
    4931             :  * generate a scan completed event if the driver does not report this.
    4932             :  */
    4933           0 : static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
    4934             : {
    4935           0 :         struct wpa_driver_nl80211_data *drv = eloop_ctx;
    4936           0 :         if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
    4937           0 :                 wpa_driver_nl80211_set_mode(drv->first_bss,
    4938             :                                             drv->ap_scan_as_station);
    4939           0 :                 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
    4940             :         }
    4941           0 :         wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
    4942           0 :         wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
    4943           0 : }
    4944             : 
    4945             : 
    4946             : static struct nl_msg *
    4947          64 : nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
    4948             :                     struct wpa_driver_scan_params *params, u64 *wdev_id)
    4949             : {
    4950             :         struct nl_msg *msg;
    4951             :         size_t i;
    4952             : 
    4953          64 :         msg = nlmsg_alloc();
    4954          64 :         if (!msg)
    4955           0 :                 return NULL;
    4956             : 
    4957          64 :         nl80211_cmd(drv, msg, 0, cmd);
    4958             : 
    4959          64 :         if (!wdev_id)
    4960          64 :                 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    4961             :         else
    4962           0 :                 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
    4963             : 
    4964          64 :         if (params->num_ssids) {
    4965             :                 struct nlattr *ssids;
    4966             : 
    4967           0 :                 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
    4968           0 :                 if (ssids == NULL)
    4969           0 :                         goto fail;
    4970           0 :                 for (i = 0; i < params->num_ssids; i++) {
    4971           0 :                         wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
    4972           0 :                                           params->ssids[i].ssid,
    4973             :                                           params->ssids[i].ssid_len);
    4974           0 :                         if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
    4975           0 :                                     params->ssids[i].ssid) < 0)
    4976           0 :                                 goto fail;
    4977             :                 }
    4978           0 :                 nla_nest_end(msg, ssids);
    4979             :         }
    4980             : 
    4981          64 :         if (params->extra_ies) {
    4982           0 :                 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
    4983           0 :                             params->extra_ies, params->extra_ies_len);
    4984           0 :                 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
    4985           0 :                             params->extra_ies) < 0)
    4986           0 :                         goto fail;
    4987             :         }
    4988             : 
    4989          64 :         if (params->freqs) {
    4990             :                 struct nlattr *freqs;
    4991          64 :                 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
    4992          64 :                 if (freqs == NULL)
    4993           0 :                         goto fail;
    4994         632 :                 for (i = 0; params->freqs[i]; i++) {
    4995         568 :                         wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
    4996         568 :                                    "MHz", params->freqs[i]);
    4997         568 :                         if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
    4998           0 :                                 goto fail;
    4999             :                 }
    5000          64 :                 nla_nest_end(msg, freqs);
    5001             :         }
    5002             : 
    5003          64 :         os_free(drv->filter_ssids);
    5004          64 :         drv->filter_ssids = params->filter_ssids;
    5005          64 :         params->filter_ssids = NULL;
    5006          64 :         drv->num_filter_ssids = params->num_filter_ssids;
    5007             : 
    5008          64 :         if (params->only_new_results) {
    5009           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
    5010           0 :                 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_FLAGS,
    5011             :                             NL80211_SCAN_FLAG_FLUSH);
    5012             :         }
    5013             : 
    5014          64 :         return msg;
    5015             : 
    5016             : fail:
    5017             : nla_put_failure:
    5018           0 :         nlmsg_free(msg);
    5019           0 :         return NULL;
    5020             : }
    5021             : 
    5022             : 
    5023             : /**
    5024             :  * wpa_driver_nl80211_scan - Request the driver to initiate scan
    5025             :  * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
    5026             :  * @params: Scan parameters
    5027             :  * Returns: 0 on success, -1 on failure
    5028             :  */
    5029          64 : static int wpa_driver_nl80211_scan(struct i802_bss *bss,
    5030             :                                    struct wpa_driver_scan_params *params)
    5031             : {
    5032          64 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    5033          64 :         int ret = -1, timeout;
    5034          64 :         struct nl_msg *msg = NULL;
    5035             : 
    5036          64 :         wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
    5037          64 :         drv->scan_for_auth = 0;
    5038             : 
    5039          64 :         msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
    5040          64 :                                   bss->wdev_id_set ? &bss->wdev_id : NULL);
    5041          64 :         if (!msg)
    5042           0 :                 return -1;
    5043             : 
    5044          64 :         if (params->p2p_probe) {
    5045             :                 struct nlattr *rates;
    5046             : 
    5047           0 :                 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
    5048             : 
    5049           0 :                 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
    5050           0 :                 if (rates == NULL)
    5051           0 :                         goto nla_put_failure;
    5052             : 
    5053             :                 /*
    5054             :                  * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
    5055             :                  * by masking out everything else apart from the OFDM rates 6,
    5056             :                  * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
    5057             :                  * rates are left enabled.
    5058             :                  */
    5059           0 :                 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
    5060             :                         "\x0c\x12\x18\x24\x30\x48\x60\x6c");
    5061           0 :                 nla_nest_end(msg, rates);
    5062             : 
    5063           0 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
    5064             :         }
    5065             : 
    5066          64 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    5067          64 :         msg = NULL;
    5068          64 :         if (ret) {
    5069           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
    5070             :                            "(%s)", ret, strerror(-ret));
    5071           0 :                 if (drv->hostapd && is_ap_interface(drv->nlmode)) {
    5072           0 :                         enum nl80211_iftype old_mode = drv->nlmode;
    5073             : 
    5074             :                         /*
    5075             :                          * mac80211 does not allow scan requests in AP mode, so
    5076             :                          * try to do this in station mode.
    5077             :                          */
    5078           0 :                         if (wpa_driver_nl80211_set_mode(
    5079             :                                     bss, NL80211_IFTYPE_STATION))
    5080           0 :                                 goto nla_put_failure;
    5081             : 
    5082           0 :                         if (wpa_driver_nl80211_scan(bss, params)) {
    5083           0 :                                 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
    5084           0 :                                 goto nla_put_failure;
    5085             :                         }
    5086             : 
    5087             :                         /* Restore AP mode when processing scan results */
    5088           0 :                         drv->ap_scan_as_station = old_mode;
    5089           0 :                         ret = 0;
    5090             :                 } else
    5091             :                         goto nla_put_failure;
    5092             :         }
    5093             : 
    5094          64 :         drv->scan_state = SCAN_REQUESTED;
    5095             :         /* Not all drivers generate "scan completed" wireless event, so try to
    5096             :          * read results after a timeout. */
    5097          64 :         timeout = 10;
    5098          64 :         if (drv->scan_complete_events) {
    5099             :                 /*
    5100             :                  * The driver seems to deliver events to notify when scan is
    5101             :                  * complete, so use longer timeout to avoid race conditions
    5102             :                  * with scanning and following association request.
    5103             :                  */
    5104          29 :                 timeout = 30;
    5105             :         }
    5106          64 :         wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
    5107             :                    "seconds", ret, timeout);
    5108          64 :         eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
    5109          64 :         eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
    5110             :                                drv, drv->ctx);
    5111             : 
    5112             : nla_put_failure:
    5113          64 :         nlmsg_free(msg);
    5114          64 :         return ret;
    5115             : }
    5116             : 
    5117             : 
    5118             : /**
    5119             :  * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
    5120             :  * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
    5121             :  * @params: Scan parameters
    5122             :  * @interval: Interval between scan cycles in milliseconds
    5123             :  * Returns: 0 on success, -1 on failure or if not supported
    5124             :  */
    5125           0 : static int wpa_driver_nl80211_sched_scan(void *priv,
    5126             :                                          struct wpa_driver_scan_params *params,
    5127             :                                          u32 interval)
    5128             : {
    5129           0 :         struct i802_bss *bss = priv;
    5130           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    5131           0 :         int ret = -1;
    5132             :         struct nl_msg *msg;
    5133             :         size_t i;
    5134             : 
    5135           0 :         wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
    5136             : 
    5137             : #ifdef ANDROID
    5138             :         if (!drv->capa.sched_scan_supported)
    5139             :                 return android_pno_start(bss, params);
    5140             : #endif /* ANDROID */
    5141             : 
    5142           0 :         msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
    5143           0 :                                   bss->wdev_id_set ? &bss->wdev_id : NULL);
    5144           0 :         if (!msg)
    5145           0 :                 goto nla_put_failure;
    5146             : 
    5147           0 :         NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
    5148             : 
    5149           0 :         if ((drv->num_filter_ssids &&
    5150           0 :             (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
    5151           0 :             params->filter_rssi) {
    5152             :                 struct nlattr *match_sets;
    5153           0 :                 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
    5154           0 :                 if (match_sets == NULL)
    5155           0 :                         goto nla_put_failure;
    5156             : 
    5157           0 :                 for (i = 0; i < drv->num_filter_ssids; i++) {
    5158             :                         struct nlattr *match_set_ssid;
    5159           0 :                         wpa_hexdump_ascii(MSG_MSGDUMP,
    5160             :                                           "nl80211: Sched scan filter SSID",
    5161           0 :                                           drv->filter_ssids[i].ssid,
    5162           0 :                                           drv->filter_ssids[i].ssid_len);
    5163             : 
    5164           0 :                         match_set_ssid = nla_nest_start(msg, i + 1);
    5165           0 :                         if (match_set_ssid == NULL)
    5166           0 :                                 goto nla_put_failure;
    5167           0 :                         NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
    5168             :                                 drv->filter_ssids[i].ssid_len,
    5169             :                                 drv->filter_ssids[i].ssid);
    5170           0 :                         if (params->filter_rssi)
    5171           0 :                                 NLA_PUT_U32(msg,
    5172             :                                             NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
    5173             :                                             params->filter_rssi);
    5174             : 
    5175           0 :                         nla_nest_end(msg, match_set_ssid);
    5176             :                 }
    5177             : 
    5178             :                 /*
    5179             :                  * Due to backward compatibility code, newer kernels treat this
    5180             :                  * matchset (with only an RSSI filter) as the default for all
    5181             :                  * other matchsets, unless it's the only one, in which case the
    5182             :                  * matchset will actually allow all SSIDs above the RSSI.
    5183             :                  */
    5184           0 :                 if (params->filter_rssi) {
    5185             :                         struct nlattr *match_set_rssi;
    5186           0 :                         match_set_rssi = nla_nest_start(msg, 0);
    5187           0 :                         if (match_set_rssi == NULL)
    5188           0 :                                 goto nla_put_failure;
    5189           0 :                         NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
    5190             :                                     params->filter_rssi);
    5191           0 :                         wpa_printf(MSG_MSGDUMP,
    5192             :                                    "nl80211: Sched scan RSSI filter %d dBm",
    5193             :                                    params->filter_rssi);
    5194           0 :                         nla_nest_end(msg, match_set_rssi);
    5195             :                 }
    5196             : 
    5197           0 :                 nla_nest_end(msg, match_sets);
    5198             :         }
    5199             : 
    5200           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    5201             : 
    5202             :         /* TODO: if we get an error here, we should fall back to normal scan */
    5203             : 
    5204           0 :         msg = NULL;
    5205           0 :         if (ret) {
    5206           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
    5207             :                            "ret=%d (%s)", ret, strerror(-ret));
    5208           0 :                 goto nla_put_failure;
    5209             :         }
    5210             : 
    5211           0 :         wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
    5212             :                    "scan interval %d msec", ret, interval);
    5213             : 
    5214             : nla_put_failure:
    5215           0 :         nlmsg_free(msg);
    5216           0 :         return ret;
    5217             : }
    5218             : 
    5219             : 
    5220             : /**
    5221             :  * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
    5222             :  * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
    5223             :  * Returns: 0 on success, -1 on failure or if not supported
    5224             :  */
    5225           0 : static int wpa_driver_nl80211_stop_sched_scan(void *priv)
    5226             : {
    5227           0 :         struct i802_bss *bss = priv;
    5228           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    5229           0 :         int ret = 0;
    5230             :         struct nl_msg *msg;
    5231             : 
    5232             : #ifdef ANDROID
    5233             :         if (!drv->capa.sched_scan_supported)
    5234             :                 return android_pno_stop(bss);
    5235             : #endif /* ANDROID */
    5236             : 
    5237           0 :         msg = nlmsg_alloc();
    5238           0 :         if (!msg)
    5239           0 :                 return -1;
    5240             : 
    5241           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
    5242             : 
    5243           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    5244             : 
    5245           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    5246           0 :         msg = NULL;
    5247           0 :         if (ret) {
    5248           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
    5249             :                            "ret=%d (%s)", ret, strerror(-ret));
    5250           0 :                 goto nla_put_failure;
    5251             :         }
    5252             : 
    5253           0 :         wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
    5254             : 
    5255             : nla_put_failure:
    5256           0 :         nlmsg_free(msg);
    5257           0 :         return ret;
    5258             : }
    5259             : 
    5260             : 
    5261           0 : static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
    5262             : {
    5263             :         const u8 *end, *pos;
    5264             : 
    5265           0 :         if (ies == NULL)
    5266           0 :                 return NULL;
    5267             : 
    5268           0 :         pos = ies;
    5269           0 :         end = ies + ies_len;
    5270             : 
    5271           0 :         while (pos + 1 < end) {
    5272           0 :                 if (pos + 2 + pos[1] > end)
    5273           0 :                         break;
    5274           0 :                 if (pos[0] == ie)
    5275           0 :                         return pos;
    5276           0 :                 pos += 2 + pos[1];
    5277             :         }
    5278             : 
    5279           0 :         return NULL;
    5280             : }
    5281             : 
    5282             : 
    5283           7 : static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
    5284             :                                  const u8 *ie, size_t ie_len)
    5285             : {
    5286             :         const u8 *ssid;
    5287             :         size_t i;
    5288             : 
    5289           7 :         if (drv->filter_ssids == NULL)
    5290           7 :                 return 0;
    5291             : 
    5292           0 :         ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
    5293           0 :         if (ssid == NULL)
    5294           0 :                 return 1;
    5295             : 
    5296           0 :         for (i = 0; i < drv->num_filter_ssids; i++) {
    5297           0 :                 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
    5298           0 :                     os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
    5299             :                     0)
    5300           0 :                         return 0;
    5301             :         }
    5302             : 
    5303           0 :         return 1;
    5304             : }
    5305             : 
    5306             : 
    5307           7 : static int bss_info_handler(struct nl_msg *msg, void *arg)
    5308             : {
    5309             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    5310           7 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    5311             :         struct nlattr *bss[NL80211_BSS_MAX + 1];
    5312             :         static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
    5313             :                 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
    5314             :                 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
    5315             :                 [NL80211_BSS_TSF] = { .type = NLA_U64 },
    5316             :                 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
    5317             :                 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
    5318             :                 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
    5319             :                 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
    5320             :                 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
    5321             :                 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
    5322             :                 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
    5323             :                 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
    5324             :         };
    5325           7 :         struct nl80211_bss_info_arg *_arg = arg;
    5326           7 :         struct wpa_scan_results *res = _arg->res;
    5327             :         struct wpa_scan_res **tmp;
    5328             :         struct wpa_scan_res *r;
    5329             :         const u8 *ie, *beacon_ie;
    5330             :         size_t ie_len, beacon_ie_len;
    5331             :         u8 *pos;
    5332             :         size_t i;
    5333             : 
    5334           7 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    5335             :                   genlmsg_attrlen(gnlh, 0), NULL);
    5336           7 :         if (!tb[NL80211_ATTR_BSS])
    5337           0 :                 return NL_SKIP;
    5338           7 :         if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
    5339             :                              bss_policy))
    5340           0 :                 return NL_SKIP;
    5341           7 :         if (bss[NL80211_BSS_STATUS]) {
    5342             :                 enum nl80211_bss_status status;
    5343           0 :                 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
    5344           0 :                 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
    5345           0 :                     bss[NL80211_BSS_FREQUENCY]) {
    5346           0 :                         _arg->assoc_freq =
    5347           0 :                                 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
    5348           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
    5349             :                                    _arg->assoc_freq);
    5350             :                 }
    5351           0 :                 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
    5352           0 :                     bss[NL80211_BSS_FREQUENCY]) {
    5353           0 :                         _arg->ibss_freq =
    5354           0 :                                 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
    5355           0 :                         wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
    5356             :                                    _arg->ibss_freq);
    5357             :                 }
    5358           0 :                 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
    5359           0 :                     bss[NL80211_BSS_BSSID]) {
    5360           0 :                         os_memcpy(_arg->assoc_bssid,
    5361             :                                   nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
    5362           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Associated with "
    5363           0 :                                    MACSTR, MAC2STR(_arg->assoc_bssid));
    5364             :                 }
    5365             :         }
    5366           7 :         if (!res)
    5367           0 :                 return NL_SKIP;
    5368           7 :         if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
    5369           7 :                 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
    5370           7 :                 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
    5371             :         } else {
    5372           0 :                 ie = NULL;
    5373           0 :                 ie_len = 0;
    5374             :         }
    5375           7 :         if (bss[NL80211_BSS_BEACON_IES]) {
    5376           7 :                 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
    5377           7 :                 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
    5378             :         } else {
    5379           0 :                 beacon_ie = NULL;
    5380           0 :                 beacon_ie_len = 0;
    5381             :         }
    5382             : 
    5383           7 :         if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
    5384             :                                   ie ? ie_len : beacon_ie_len))
    5385           0 :                 return NL_SKIP;
    5386             : 
    5387           7 :         r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
    5388           7 :         if (r == NULL)
    5389           0 :                 return NL_SKIP;
    5390           7 :         if (bss[NL80211_BSS_BSSID])
    5391           7 :                 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
    5392             :                           ETH_ALEN);
    5393           7 :         if (bss[NL80211_BSS_FREQUENCY])
    5394           7 :                 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
    5395           7 :         if (bss[NL80211_BSS_BEACON_INTERVAL])
    5396           7 :                 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
    5397           7 :         if (bss[NL80211_BSS_CAPABILITY])
    5398           7 :                 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
    5399           7 :         r->flags |= WPA_SCAN_NOISE_INVALID;
    5400           7 :         if (bss[NL80211_BSS_SIGNAL_MBM]) {
    5401           7 :                 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
    5402           7 :                 r->level /= 100; /* mBm to dBm */
    5403           7 :                 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
    5404           0 :         } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
    5405           0 :                 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
    5406           0 :                 r->flags |= WPA_SCAN_QUAL_INVALID;
    5407             :         } else
    5408           0 :                 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
    5409           7 :         if (bss[NL80211_BSS_TSF])
    5410           7 :                 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
    5411           7 :         if (bss[NL80211_BSS_SEEN_MS_AGO])
    5412           7 :                 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
    5413           7 :         r->ie_len = ie_len;
    5414           7 :         pos = (u8 *) (r + 1);
    5415           7 :         if (ie) {
    5416           7 :                 os_memcpy(pos, ie, ie_len);
    5417           7 :                 pos += ie_len;
    5418             :         }
    5419           7 :         r->beacon_ie_len = beacon_ie_len;
    5420           7 :         if (beacon_ie)
    5421           7 :                 os_memcpy(pos, beacon_ie, beacon_ie_len);
    5422             : 
    5423           7 :         if (bss[NL80211_BSS_STATUS]) {
    5424             :                 enum nl80211_bss_status status;
    5425           0 :                 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
    5426           0 :                 switch (status) {
    5427             :                 case NL80211_BSS_STATUS_AUTHENTICATED:
    5428           0 :                         r->flags |= WPA_SCAN_AUTHENTICATED;
    5429           0 :                         break;
    5430             :                 case NL80211_BSS_STATUS_ASSOCIATED:
    5431           0 :                         r->flags |= WPA_SCAN_ASSOCIATED;
    5432           0 :                         break;
    5433             :                 default:
    5434           0 :                         break;
    5435             :                 }
    5436             :         }
    5437             : 
    5438             :         /*
    5439             :          * cfg80211 maintains separate BSS table entries for APs if the same
    5440             :          * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
    5441             :          * not use frequency as a separate key in the BSS table, so filter out
    5442             :          * duplicated entries. Prefer associated BSS entry in such a case in
    5443             :          * order to get the correct frequency into the BSS table. Similarly,
    5444             :          * prefer newer entries over older.
    5445             :          */
    5446           8 :         for (i = 0; i < res->num; i++) {
    5447             :                 const u8 *s1, *s2;
    5448           1 :                 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
    5449           1 :                         continue;
    5450             : 
    5451           0 :                 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
    5452           0 :                                     res->res[i]->ie_len, WLAN_EID_SSID);
    5453           0 :                 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
    5454           0 :                 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
    5455           0 :                     os_memcmp(s1, s2, 2 + s1[1]) != 0)
    5456           0 :                         continue;
    5457             : 
    5458             :                 /* Same BSSID,SSID was already included in scan results */
    5459           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
    5460           0 :                            "for " MACSTR, MAC2STR(r->bssid));
    5461             : 
    5462           0 :                 if (((r->flags & WPA_SCAN_ASSOCIATED) &&
    5463           0 :                      !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) ||
    5464           0 :                     r->age < res->res[i]->age) {
    5465           0 :                         os_free(res->res[i]);
    5466           0 :                         res->res[i] = r;
    5467             :                 } else
    5468           0 :                         os_free(r);
    5469           0 :                 return NL_SKIP;
    5470             :         }
    5471             : 
    5472           7 :         tmp = os_realloc_array(res->res, res->num + 1,
    5473             :                                sizeof(struct wpa_scan_res *));
    5474           7 :         if (tmp == NULL) {
    5475           0 :                 os_free(r);
    5476           0 :                 return NL_SKIP;
    5477             :         }
    5478           7 :         tmp[res->num++] = r;
    5479           7 :         res->res = tmp;
    5480             : 
    5481           7 :         return NL_SKIP;
    5482             : }
    5483             : 
    5484             : 
    5485           0 : static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
    5486             :                                  const u8 *addr)
    5487             : {
    5488           0 :         if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
    5489           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
    5490           0 :                            "mismatch (" MACSTR ")", MAC2STR(addr));
    5491           0 :                 wpa_driver_nl80211_mlme(drv, addr,
    5492             :                                         NL80211_CMD_DEAUTHENTICATE,
    5493             :                                         WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
    5494             :         }
    5495           0 : }
    5496             : 
    5497             : 
    5498          24 : static void wpa_driver_nl80211_check_bss_status(
    5499             :         struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
    5500             : {
    5501             :         size_t i;
    5502             : 
    5503          31 :         for (i = 0; i < res->num; i++) {
    5504           7 :                 struct wpa_scan_res *r = res->res[i];
    5505           7 :                 if (r->flags & WPA_SCAN_AUTHENTICATED) {
    5506           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Scan results "
    5507             :                                    "indicates BSS status with " MACSTR
    5508             :                                    " as authenticated",
    5509           0 :                                    MAC2STR(r->bssid));
    5510           0 :                         if (is_sta_interface(drv->nlmode) &&
    5511           0 :                             os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
    5512           0 :                             os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
    5513             :                             0) {
    5514           0 :                                 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
    5515             :                                            " in local state (auth=" MACSTR
    5516             :                                            " assoc=" MACSTR ")",
    5517           0 :                                            MAC2STR(drv->auth_bssid),
    5518           0 :                                            MAC2STR(drv->bssid));
    5519           0 :                                 clear_state_mismatch(drv, r->bssid);
    5520             :                         }
    5521             :                 }
    5522             : 
    5523           7 :                 if (r->flags & WPA_SCAN_ASSOCIATED) {
    5524           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Scan results "
    5525             :                                    "indicate BSS status with " MACSTR
    5526             :                                    " as associated",
    5527           0 :                                    MAC2STR(r->bssid));
    5528           0 :                         if (is_sta_interface(drv->nlmode) &&
    5529           0 :                             !drv->associated) {
    5530           0 :                                 wpa_printf(MSG_DEBUG, "nl80211: Local state "
    5531             :                                            "(not associated) does not match "
    5532             :                                            "with BSS state");
    5533           0 :                                 clear_state_mismatch(drv, r->bssid);
    5534           0 :                         } else if (is_sta_interface(drv->nlmode) &&
    5535           0 :                                    os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
    5536             :                                    0) {
    5537           0 :                                 wpa_printf(MSG_DEBUG, "nl80211: Local state "
    5538             :                                            "(associated with " MACSTR ") does "
    5539             :                                            "not match with BSS state",
    5540           0 :                                            MAC2STR(drv->bssid));
    5541           0 :                                 clear_state_mismatch(drv, r->bssid);
    5542           0 :                                 clear_state_mismatch(drv, drv->bssid);
    5543             :                         }
    5544             :                 }
    5545             :         }
    5546          24 : }
    5547             : 
    5548             : 
    5549             : static struct wpa_scan_results *
    5550          24 : nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
    5551             : {
    5552             :         struct nl_msg *msg;
    5553             :         struct wpa_scan_results *res;
    5554             :         int ret;
    5555             :         struct nl80211_bss_info_arg arg;
    5556             : 
    5557          24 :         res = os_zalloc(sizeof(*res));
    5558          24 :         if (res == NULL)
    5559           0 :                 return NULL;
    5560          24 :         msg = nlmsg_alloc();
    5561          24 :         if (!msg)
    5562           0 :                 goto nla_put_failure;
    5563             : 
    5564          24 :         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
    5565          24 :         if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
    5566           0 :                 goto nla_put_failure;
    5567             : 
    5568          24 :         arg.drv = drv;
    5569          24 :         arg.res = res;
    5570          24 :         ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
    5571          24 :         msg = NULL;
    5572          24 :         if (ret == 0) {
    5573          24 :                 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
    5574             :                            "BSSes)", (unsigned long) res->num);
    5575          24 :                 nl80211_get_noise_for_scan_results(drv, res);
    5576          24 :                 return res;
    5577             :         }
    5578           0 :         wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
    5579             :                    "(%s)", ret, strerror(-ret));
    5580             : nla_put_failure:
    5581           0 :         nlmsg_free(msg);
    5582           0 :         wpa_scan_results_free(res);
    5583           0 :         return NULL;
    5584             : }
    5585             : 
    5586             : 
    5587             : /**
    5588             :  * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
    5589             :  * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
    5590             :  * Returns: Scan results on success, -1 on failure
    5591             :  */
    5592             : static struct wpa_scan_results *
    5593          24 : wpa_driver_nl80211_get_scan_results(void *priv)
    5594             : {
    5595          24 :         struct i802_bss *bss = priv;
    5596          24 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    5597             :         struct wpa_scan_results *res;
    5598             : 
    5599          24 :         res = nl80211_get_scan_results(drv);
    5600          24 :         if (res)
    5601          24 :                 wpa_driver_nl80211_check_bss_status(drv, res);
    5602          24 :         return res;
    5603             : }
    5604             : 
    5605             : 
    5606           0 : static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
    5607             : {
    5608             :         struct wpa_scan_results *res;
    5609             :         size_t i;
    5610             : 
    5611           0 :         res = nl80211_get_scan_results(drv);
    5612           0 :         if (res == NULL) {
    5613           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
    5614           0 :                 return;
    5615             :         }
    5616             : 
    5617           0 :         wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
    5618           0 :         for (i = 0; i < res->num; i++) {
    5619           0 :                 struct wpa_scan_res *r = res->res[i];
    5620           0 :                 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
    5621           0 :                            (int) i, (int) res->num, MAC2STR(r->bssid),
    5622           0 :                            r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
    5623           0 :                            r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
    5624             :         }
    5625             : 
    5626           0 :         wpa_scan_results_free(res);
    5627             : }
    5628             : 
    5629             : 
    5630        1915 : static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
    5631             : {
    5632        1915 :         switch (alg) {
    5633             :         case WPA_ALG_WEP:
    5634          10 :                 if (key_len == 5)
    5635           4 :                         return WLAN_CIPHER_SUITE_WEP40;
    5636           6 :                 return WLAN_CIPHER_SUITE_WEP104;
    5637             :         case WPA_ALG_TKIP:
    5638          70 :                 return WLAN_CIPHER_SUITE_TKIP;
    5639             :         case WPA_ALG_CCMP:
    5640        1630 :                 return WLAN_CIPHER_SUITE_CCMP;
    5641             :         case WPA_ALG_GCMP:
    5642           0 :                 return WLAN_CIPHER_SUITE_GCMP;
    5643             :         case WPA_ALG_CCMP_256:
    5644           0 :                 return WLAN_CIPHER_SUITE_CCMP_256;
    5645             :         case WPA_ALG_GCMP_256:
    5646           0 :                 return WLAN_CIPHER_SUITE_GCMP_256;
    5647             :         case WPA_ALG_IGTK:
    5648         205 :                 return WLAN_CIPHER_SUITE_AES_CMAC;
    5649             :         case WPA_ALG_BIP_GMAC_128:
    5650           0 :                 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
    5651             :         case WPA_ALG_BIP_GMAC_256:
    5652           0 :                 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
    5653             :         case WPA_ALG_BIP_CMAC_256:
    5654           0 :                 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
    5655             :         case WPA_ALG_SMS4:
    5656           0 :                 return WLAN_CIPHER_SUITE_SMS4;
    5657             :         case WPA_ALG_KRK:
    5658           0 :                 return WLAN_CIPHER_SUITE_KRK;
    5659             :         case WPA_ALG_NONE:
    5660             :         case WPA_ALG_PMK:
    5661           0 :                 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
    5662             :                            alg);
    5663           0 :                 return 0;
    5664             :         }
    5665             : 
    5666           0 :         wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
    5667             :                    alg);
    5668           0 :         return 0;
    5669             : }
    5670             : 
    5671             : 
    5672         805 : static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
    5673             : {
    5674         805 :         switch (cipher) {
    5675             :         case WPA_CIPHER_CCMP_256:
    5676           0 :                 return WLAN_CIPHER_SUITE_CCMP_256;
    5677             :         case WPA_CIPHER_GCMP_256:
    5678           0 :                 return WLAN_CIPHER_SUITE_GCMP_256;
    5679             :         case WPA_CIPHER_CCMP:
    5680         504 :                 return WLAN_CIPHER_SUITE_CCMP;
    5681             :         case WPA_CIPHER_GCMP:
    5682           0 :                 return WLAN_CIPHER_SUITE_GCMP;
    5683             :         case WPA_CIPHER_TKIP:
    5684          76 :                 return WLAN_CIPHER_SUITE_TKIP;
    5685             :         case WPA_CIPHER_WEP104:
    5686           5 :                 return WLAN_CIPHER_SUITE_WEP104;
    5687             :         case WPA_CIPHER_WEP40:
    5688           7 :                 return WLAN_CIPHER_SUITE_WEP40;
    5689             :         case WPA_CIPHER_GTK_NOT_USED:
    5690           0 :                 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
    5691             :         }
    5692             : 
    5693         213 :         return 0;
    5694             : }
    5695             : 
    5696             : 
    5697         805 : static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
    5698             :                                        int max_suites)
    5699             : {
    5700         805 :         int num_suites = 0;
    5701             : 
    5702         805 :         if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
    5703           0 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
    5704         805 :         if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
    5705           0 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
    5706         805 :         if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
    5707         553 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
    5708         805 :         if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
    5709           0 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
    5710         805 :         if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
    5711          79 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
    5712         805 :         if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
    5713           5 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
    5714         805 :         if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
    5715           7 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
    5716             : 
    5717         805 :         return num_suites;
    5718             : }
    5719             : 
    5720             : 
    5721        7696 : static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
    5722             :                                       enum wpa_alg alg, const u8 *addr,
    5723             :                                       int key_idx, int set_tx,
    5724             :                                       const u8 *seq, size_t seq_len,
    5725             :                                       const u8 *key, size_t key_len)
    5726             : {
    5727        7696 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    5728             :         int ifindex;
    5729             :         struct nl_msg *msg;
    5730             :         int ret;
    5731        7696 :         int tdls = 0;
    5732             : 
    5733             :         /* Ignore for P2P Device */
    5734        7696 :         if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
    5735           0 :                 return 0;
    5736             : 
    5737        7696 :         ifindex = if_nametoindex(ifname);
    5738        7696 :         wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
    5739             :                    "set_tx=%d seq_len=%lu key_len=%lu",
    5740             :                    __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
    5741             :                    (unsigned long) seq_len, (unsigned long) key_len);
    5742             : #ifdef CONFIG_TDLS
    5743             :         if (key_idx == -1) {
    5744             :                 key_idx = 0;
    5745             :                 tdls = 1;
    5746             :         }
    5747             : #endif /* CONFIG_TDLS */
    5748             : 
    5749        7696 :         msg = nlmsg_alloc();
    5750        7696 :         if (!msg)
    5751           0 :                 return -ENOMEM;
    5752             : 
    5753        7696 :         if (alg == WPA_ALG_NONE) {
    5754        5781 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
    5755             :         } else {
    5756        1915 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
    5757        1915 :                 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
    5758        1915 :                 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
    5759        1915 :                 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
    5760             :                             wpa_alg_to_cipher_suite(alg, key_len));
    5761             :         }
    5762             : 
    5763        7696 :         if (seq && seq_len) {
    5764           0 :                 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
    5765           0 :                 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
    5766             :         }
    5767             : 
    5768        7696 :         if (addr && !is_broadcast_ether_addr(addr)) {
    5769        3994 :                 wpa_printf(MSG_DEBUG, "   addr=" MACSTR, MAC2STR(addr));
    5770        3994 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    5771             : 
    5772        7988 :                 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
    5773           0 :                         wpa_printf(MSG_DEBUG, "   RSN IBSS RX GTK");
    5774           0 :                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
    5775             :                                     NL80211_KEYTYPE_GROUP);
    5776             :                 }
    5777        3702 :         } else if (addr && is_broadcast_ether_addr(addr)) {
    5778             :                 struct nlattr *types;
    5779             : 
    5780         958 :                 wpa_printf(MSG_DEBUG, "   broadcast key");
    5781             : 
    5782         958 :                 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
    5783         958 :                 if (!types)
    5784           0 :                         goto nla_put_failure;
    5785         958 :                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
    5786         958 :                 nla_nest_end(msg, types);
    5787             :         }
    5788        7696 :         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
    5789        7696 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
    5790             : 
    5791        7696 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    5792        7696 :         if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
    5793        5339 :                 ret = 0;
    5794        7696 :         if (ret)
    5795          19 :                 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
    5796             :                            ret, strerror(-ret));
    5797             : 
    5798             :         /*
    5799             :          * If we failed or don't need to set the default TX key (below),
    5800             :          * we're done here.
    5801             :          */
    5802        7696 :         if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
    5803        5796 :                 return ret;
    5804        3796 :         if (is_ap_interface(drv->nlmode) && addr &&
    5805        1896 :             !is_broadcast_ether_addr(addr))
    5806         940 :                 return ret;
    5807             : 
    5808         960 :         msg = nlmsg_alloc();
    5809         960 :         if (!msg)
    5810           0 :                 return -ENOMEM;
    5811             : 
    5812         960 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
    5813         960 :         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
    5814         960 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
    5815         960 :         if (alg == WPA_ALG_IGTK)
    5816         205 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
    5817             :         else
    5818         755 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
    5819        1916 :         if (addr && is_broadcast_ether_addr(addr)) {
    5820             :                 struct nlattr *types;
    5821             : 
    5822         956 :                 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
    5823         956 :                 if (!types)
    5824           0 :                         goto nla_put_failure;
    5825         956 :                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
    5826         956 :                 nla_nest_end(msg, types);
    5827           4 :         } else if (addr) {
    5828             :                 struct nlattr *types;
    5829             : 
    5830           0 :                 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
    5831           0 :                 if (!types)
    5832           0 :                         goto nla_put_failure;
    5833           0 :                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
    5834           0 :                 nla_nest_end(msg, types);
    5835             :         }
    5836             : 
    5837         960 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    5838         960 :         if (ret == -ENOENT)
    5839           0 :                 ret = 0;
    5840         960 :         if (ret)
    5841           0 :                 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
    5842             :                            "err=%d %s)", ret, strerror(-ret));
    5843         960 :         return ret;
    5844             : 
    5845             : nla_put_failure:
    5846           0 :         nlmsg_free(msg);
    5847           0 :         return -ENOBUFS;
    5848             : }
    5849             : 
    5850             : 
    5851           0 : static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
    5852             :                       int key_idx, int defkey,
    5853             :                       const u8 *seq, size_t seq_len,
    5854             :                       const u8 *key, size_t key_len)
    5855             : {
    5856           0 :         struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
    5857           0 :         if (!key_attr)
    5858           0 :                 return -1;
    5859             : 
    5860           0 :         if (defkey && alg == WPA_ALG_IGTK)
    5861           0 :                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
    5862           0 :         else if (defkey)
    5863           0 :                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
    5864             : 
    5865           0 :         NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
    5866             : 
    5867           0 :         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
    5868             :                     wpa_alg_to_cipher_suite(alg, key_len));
    5869             : 
    5870           0 :         if (seq && seq_len)
    5871           0 :                 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
    5872             : 
    5873           0 :         NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
    5874             : 
    5875           0 :         nla_nest_end(msg, key_attr);
    5876             : 
    5877           0 :         return 0;
    5878             :  nla_put_failure:
    5879           0 :         return -1;
    5880             : }
    5881             : 
    5882             : 
    5883           0 : static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
    5884             :                                  struct nl_msg *msg)
    5885             : {
    5886           0 :         int i, privacy = 0;
    5887             :         struct nlattr *nl_keys, *nl_key;
    5888             : 
    5889           0 :         for (i = 0; i < 4; i++) {
    5890           0 :                 if (!params->wep_key[i])
    5891           0 :                         continue;
    5892           0 :                 privacy = 1;
    5893           0 :                 break;
    5894             :         }
    5895           0 :         if (params->wps == WPS_MODE_PRIVACY)
    5896           0 :                 privacy = 1;
    5897           0 :         if (params->pairwise_suite &&
    5898           0 :             params->pairwise_suite != WPA_CIPHER_NONE)
    5899           0 :                 privacy = 1;
    5900             : 
    5901           0 :         if (!privacy)
    5902           0 :                 return 0;
    5903             : 
    5904           0 :         NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
    5905             : 
    5906           0 :         nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
    5907           0 :         if (!nl_keys)
    5908           0 :                 goto nla_put_failure;
    5909             : 
    5910           0 :         for (i = 0; i < 4; i++) {
    5911           0 :                 if (!params->wep_key[i])
    5912           0 :                         continue;
    5913             : 
    5914           0 :                 nl_key = nla_nest_start(msg, i);
    5915           0 :                 if (!nl_key)
    5916           0 :                         goto nla_put_failure;
    5917             : 
    5918           0 :                 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
    5919             :                         params->wep_key[i]);
    5920           0 :                 if (params->wep_key_len[i] == 5)
    5921           0 :                         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
    5922             :                                     WLAN_CIPHER_SUITE_WEP40);
    5923             :                 else
    5924           0 :                         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
    5925             :                                     WLAN_CIPHER_SUITE_WEP104);
    5926             : 
    5927           0 :                 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
    5928             : 
    5929           0 :                 if (i == params->wep_tx_keyidx)
    5930           0 :                         NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
    5931             : 
    5932           0 :                 nla_nest_end(msg, nl_key);
    5933             :         }
    5934           0 :         nla_nest_end(msg, nl_keys);
    5935             : 
    5936           0 :         return 0;
    5937             : 
    5938             : nla_put_failure:
    5939           0 :         return -ENOBUFS;
    5940             : }
    5941             : 
    5942             : 
    5943           0 : static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
    5944             :                                    const u8 *addr, int cmd, u16 reason_code,
    5945             :                                    int local_state_change)
    5946             : {
    5947           0 :         int ret = -1;
    5948             :         struct nl_msg *msg;
    5949             : 
    5950           0 :         msg = nlmsg_alloc();
    5951           0 :         if (!msg)
    5952           0 :                 return -1;
    5953             : 
    5954           0 :         nl80211_cmd(drv, msg, 0, cmd);
    5955             : 
    5956           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    5957           0 :         NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
    5958           0 :         if (addr)
    5959           0 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    5960           0 :         if (local_state_change)
    5961           0 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
    5962             : 
    5963           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    5964           0 :         msg = NULL;
    5965           0 :         if (ret) {
    5966           0 :                 wpa_dbg(drv->ctx, MSG_DEBUG,
    5967             :                         "nl80211: MLME command failed: reason=%u ret=%d (%s)",
    5968             :                         reason_code, ret, strerror(-ret));
    5969           0 :                 goto nla_put_failure;
    5970             :         }
    5971           0 :         ret = 0;
    5972             : 
    5973             : nla_put_failure:
    5974           0 :         nlmsg_free(msg);
    5975           0 :         return ret;
    5976             : }
    5977             : 
    5978             : 
    5979           0 : static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
    5980             :                                          int reason_code)
    5981             : {
    5982             :         int ret;
    5983             : 
    5984           0 :         wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
    5985           0 :         nl80211_mark_disconnected(drv);
    5986             :         /* Disconnect command doesn't need BSSID - it uses cached value */
    5987           0 :         ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
    5988             :                                       reason_code, 0);
    5989             :         /*
    5990             :          * For locally generated disconnect, supplicant already generates a
    5991             :          * DEAUTH event, so ignore the event from NL80211.
    5992             :          */
    5993           0 :         drv->ignore_next_local_disconnect = ret == 0;
    5994             : 
    5995           0 :         return ret;
    5996             : }
    5997             : 
    5998             : 
    5999           0 : static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
    6000             :                                              const u8 *addr, int reason_code)
    6001             : {
    6002           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    6003             :         int ret;
    6004             : 
    6005           0 :         if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
    6006           0 :                 nl80211_mark_disconnected(drv);
    6007           0 :                 return nl80211_leave_ibss(drv);
    6008             :         }
    6009           0 :         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
    6010           0 :                 return wpa_driver_nl80211_disconnect(drv, reason_code);
    6011           0 :         wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
    6012           0 :                    __func__, MAC2STR(addr), reason_code);
    6013           0 :         nl80211_mark_disconnected(drv);
    6014           0 :         ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
    6015             :                                       reason_code, 0);
    6016             :         /*
    6017             :          * For locally generated deauthenticate, supplicant already generates a
    6018             :          * DEAUTH event, so ignore the event from NL80211.
    6019             :          */
    6020           0 :         drv->ignore_next_local_deauth = ret == 0;
    6021           0 :         return ret;
    6022             : }
    6023             : 
    6024             : 
    6025           0 : static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
    6026             :                                      struct wpa_driver_auth_params *params)
    6027             : {
    6028             :         int i;
    6029             : 
    6030           0 :         drv->auth_freq = params->freq;
    6031           0 :         drv->auth_alg = params->auth_alg;
    6032           0 :         drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
    6033           0 :         drv->auth_local_state_change = params->local_state_change;
    6034           0 :         drv->auth_p2p = params->p2p;
    6035             : 
    6036           0 :         if (params->bssid)
    6037           0 :                 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
    6038             :         else
    6039           0 :                 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
    6040             : 
    6041           0 :         if (params->ssid) {
    6042           0 :                 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
    6043           0 :                 drv->auth_ssid_len = params->ssid_len;
    6044             :         } else
    6045           0 :                 drv->auth_ssid_len = 0;
    6046             : 
    6047             : 
    6048           0 :         os_free(drv->auth_ie);
    6049           0 :         drv->auth_ie = NULL;
    6050           0 :         drv->auth_ie_len = 0;
    6051           0 :         if (params->ie) {
    6052           0 :                 drv->auth_ie = os_malloc(params->ie_len);
    6053           0 :                 if (drv->auth_ie) {
    6054           0 :                         os_memcpy(drv->auth_ie, params->ie, params->ie_len);
    6055           0 :                         drv->auth_ie_len = params->ie_len;
    6056             :                 }
    6057             :         }
    6058             : 
    6059           0 :         for (i = 0; i < 4; i++) {
    6060           0 :                 if (params->wep_key[i] && params->wep_key_len[i] &&
    6061           0 :                     params->wep_key_len[i] <= 16) {
    6062           0 :                         os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
    6063             :                                   params->wep_key_len[i]);
    6064           0 :                         drv->auth_wep_key_len[i] = params->wep_key_len[i];
    6065             :                 } else
    6066           0 :                         drv->auth_wep_key_len[i] = 0;
    6067             :         }
    6068           0 : }
    6069             : 
    6070             : 
    6071           0 : static int wpa_driver_nl80211_authenticate(
    6072             :         struct i802_bss *bss, struct wpa_driver_auth_params *params)
    6073             : {
    6074           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    6075           0 :         int ret = -1, i;
    6076             :         struct nl_msg *msg;
    6077             :         enum nl80211_auth_type type;
    6078             :         enum nl80211_iftype nlmode;
    6079           0 :         int count = 0;
    6080             :         int is_retry;
    6081             : 
    6082           0 :         is_retry = drv->retry_auth;
    6083           0 :         drv->retry_auth = 0;
    6084           0 :         drv->ignore_deauth_event = 0;
    6085             : 
    6086           0 :         nl80211_mark_disconnected(drv);
    6087           0 :         os_memset(drv->auth_bssid, 0, ETH_ALEN);
    6088           0 :         if (params->bssid)
    6089           0 :                 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
    6090             :         else
    6091           0 :                 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
    6092             :         /* FIX: IBSS mode */
    6093           0 :         nlmode = params->p2p ?
    6094             :                 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
    6095           0 :         if (drv->nlmode != nlmode &&
    6096           0 :             wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
    6097           0 :                 return -1;
    6098             : 
    6099             : retry:
    6100           0 :         msg = nlmsg_alloc();
    6101           0 :         if (!msg)
    6102           0 :                 return -1;
    6103             : 
    6104           0 :         wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
    6105             :                    drv->ifindex);
    6106             : 
    6107           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
    6108             : 
    6109           0 :         for (i = 0; i < 4; i++) {
    6110           0 :                 if (!params->wep_key[i])
    6111           0 :                         continue;
    6112           0 :                 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
    6113             :                                            NULL, i,
    6114           0 :                                            i == params->wep_tx_keyidx, NULL, 0,
    6115             :                                            params->wep_key[i],
    6116             :                                            params->wep_key_len[i]);
    6117           0 :                 if (params->wep_tx_keyidx != i)
    6118           0 :                         continue;
    6119           0 :                 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
    6120             :                                params->wep_key[i], params->wep_key_len[i])) {
    6121           0 :                         nlmsg_free(msg);
    6122           0 :                         return -1;
    6123             :                 }
    6124             :         }
    6125             : 
    6126           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    6127           0 :         if (params->bssid) {
    6128           0 :                 wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
    6129           0 :                            MAC2STR(params->bssid));
    6130           0 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
    6131             :         }
    6132           0 :         if (params->freq) {
    6133           0 :                 wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
    6134           0 :                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
    6135             :         }
    6136           0 :         if (params->ssid) {
    6137           0 :                 wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
    6138           0 :                                   params->ssid, params->ssid_len);
    6139           0 :                 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
    6140             :                         params->ssid);
    6141             :         }
    6142           0 :         wpa_hexdump(MSG_DEBUG, "  * IEs", params->ie, params->ie_len);
    6143           0 :         if (params->ie)
    6144           0 :                 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
    6145           0 :         if (params->sae_data) {
    6146           0 :                 wpa_hexdump(MSG_DEBUG, "  * SAE data", params->sae_data,
    6147             :                             params->sae_data_len);
    6148           0 :                 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
    6149             :                         params->sae_data);
    6150             :         }
    6151           0 :         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
    6152           0 :                 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
    6153           0 :         else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
    6154           0 :                 type = NL80211_AUTHTYPE_SHARED_KEY;
    6155           0 :         else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
    6156           0 :                 type = NL80211_AUTHTYPE_NETWORK_EAP;
    6157           0 :         else if (params->auth_alg & WPA_AUTH_ALG_FT)
    6158           0 :                 type = NL80211_AUTHTYPE_FT;
    6159           0 :         else if (params->auth_alg & WPA_AUTH_ALG_SAE)
    6160           0 :                 type = NL80211_AUTHTYPE_SAE;
    6161             :         else
    6162           0 :                 goto nla_put_failure;
    6163           0 :         wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
    6164           0 :         NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
    6165           0 :         if (params->local_state_change) {
    6166           0 :                 wpa_printf(MSG_DEBUG, "  * Local state change only");
    6167           0 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
    6168             :         }
    6169             : 
    6170           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    6171           0 :         msg = NULL;
    6172           0 :         if (ret) {
    6173           0 :                 wpa_dbg(drv->ctx, MSG_DEBUG,
    6174             :                         "nl80211: MLME command failed (auth): ret=%d (%s)",
    6175             :                         ret, strerror(-ret));
    6176           0 :                 count++;
    6177           0 :                 if (ret == -EALREADY && count == 1 && params->bssid &&
    6178           0 :                     !params->local_state_change) {
    6179             :                         /*
    6180             :                          * mac80211 does not currently accept new
    6181             :                          * authentication if we are already authenticated. As a
    6182             :                          * workaround, force deauthentication and try again.
    6183             :                          */
    6184           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
    6185             :                                    "after forced deauthentication");
    6186           0 :                         drv->ignore_deauth_event = 1;
    6187           0 :                         wpa_driver_nl80211_deauthenticate(
    6188             :                                 bss, params->bssid,
    6189             :                                 WLAN_REASON_PREV_AUTH_NOT_VALID);
    6190           0 :                         nlmsg_free(msg);
    6191           0 :                         goto retry;
    6192             :                 }
    6193             : 
    6194           0 :                 if (ret == -ENOENT && params->freq && !is_retry) {
    6195             :                         /*
    6196             :                          * cfg80211 has likely expired the BSS entry even
    6197             :                          * though it was previously available in our internal
    6198             :                          * BSS table. To recover quickly, start a single
    6199             :                          * channel scan on the specified channel.
    6200             :                          */
    6201             :                         struct wpa_driver_scan_params scan;
    6202             :                         int freqs[2];
    6203             : 
    6204           0 :                         os_memset(&scan, 0, sizeof(scan));
    6205           0 :                         scan.num_ssids = 1;
    6206           0 :                         if (params->ssid) {
    6207           0 :                                 scan.ssids[0].ssid = params->ssid;
    6208           0 :                                 scan.ssids[0].ssid_len = params->ssid_len;
    6209             :                         }
    6210           0 :                         freqs[0] = params->freq;
    6211           0 :                         freqs[1] = 0;
    6212           0 :                         scan.freqs = freqs;
    6213           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
    6214             :                                    "channel scan to refresh cfg80211 BSS "
    6215             :                                    "entry");
    6216           0 :                         ret = wpa_driver_nl80211_scan(bss, &scan);
    6217           0 :                         if (ret == 0) {
    6218           0 :                                 nl80211_copy_auth_params(drv, params);
    6219           0 :                                 drv->scan_for_auth = 1;
    6220             :                         }
    6221           0 :                 } else if (is_retry) {
    6222             :                         /*
    6223             :                          * Need to indicate this with an event since the return
    6224             :                          * value from the retry is not delivered to core code.
    6225             :                          */
    6226             :                         union wpa_event_data event;
    6227           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
    6228             :                                    "failed");
    6229           0 :                         os_memset(&event, 0, sizeof(event));
    6230           0 :                         os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
    6231             :                                   ETH_ALEN);
    6232           0 :                         wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
    6233             :                                              &event);
    6234             :                 }
    6235             : 
    6236           0 :                 goto nla_put_failure;
    6237             :         }
    6238           0 :         ret = 0;
    6239           0 :         wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
    6240             :                    "successfully");
    6241             : 
    6242             : nla_put_failure:
    6243           0 :         nlmsg_free(msg);
    6244           0 :         return ret;
    6245             : }
    6246             : 
    6247             : 
    6248           0 : static int wpa_driver_nl80211_authenticate_retry(
    6249             :         struct wpa_driver_nl80211_data *drv)
    6250             : {
    6251             :         struct wpa_driver_auth_params params;
    6252           0 :         struct i802_bss *bss = drv->first_bss;
    6253             :         int i;
    6254             : 
    6255           0 :         wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
    6256             : 
    6257           0 :         os_memset(&params, 0, sizeof(params));
    6258           0 :         params.freq = drv->auth_freq;
    6259           0 :         params.auth_alg = drv->auth_alg;
    6260           0 :         params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
    6261           0 :         params.local_state_change = drv->auth_local_state_change;
    6262           0 :         params.p2p = drv->auth_p2p;
    6263             : 
    6264           0 :         if (!is_zero_ether_addr(drv->auth_bssid_))
    6265           0 :                 params.bssid = drv->auth_bssid_;
    6266             : 
    6267           0 :         if (drv->auth_ssid_len) {
    6268           0 :                 params.ssid = drv->auth_ssid;
    6269           0 :                 params.ssid_len = drv->auth_ssid_len;
    6270             :         }
    6271             : 
    6272           0 :         params.ie = drv->auth_ie;
    6273           0 :         params.ie_len = drv->auth_ie_len;
    6274             : 
    6275           0 :         for (i = 0; i < 4; i++) {
    6276           0 :                 if (drv->auth_wep_key_len[i]) {
    6277           0 :                         params.wep_key[i] = drv->auth_wep_key[i];
    6278           0 :                         params.wep_key_len[i] = drv->auth_wep_key_len[i];
    6279             :                 }
    6280             :         }
    6281             : 
    6282           0 :         drv->retry_auth = 1;
    6283           0 :         return wpa_driver_nl80211_authenticate(bss, &params);
    6284             : }
    6285             : 
    6286             : 
    6287             : struct phy_info_arg {
    6288             :         u16 *num_modes;
    6289             :         struct hostapd_hw_modes *modes;
    6290             :         int last_mode, last_chan_idx;
    6291             : };
    6292             : 
    6293       24360 : static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
    6294             :                              struct nlattr *ampdu_factor,
    6295             :                              struct nlattr *ampdu_density,
    6296             :                              struct nlattr *mcs_set)
    6297             : {
    6298       24360 :         if (capa)
    6299        1160 :                 mode->ht_capab = nla_get_u16(capa);
    6300             : 
    6301       24360 :         if (ampdu_factor)
    6302        1160 :                 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
    6303             : 
    6304       24360 :         if (ampdu_density)
    6305        1160 :                 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
    6306             : 
    6307       24360 :         if (mcs_set && nla_len(mcs_set) >= 16) {
    6308             :                 u8 *mcs;
    6309        1160 :                 mcs = nla_data(mcs_set);
    6310        1160 :                 os_memcpy(mode->mcs_set, mcs, 16);
    6311             :         }
    6312       24360 : }
    6313             : 
    6314             : 
    6315       24360 : static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
    6316             :                               struct nlattr *capa,
    6317             :                               struct nlattr *mcs_set)
    6318             : {
    6319       24360 :         if (capa)
    6320        1160 :                 mode->vht_capab = nla_get_u32(capa);
    6321             : 
    6322       24360 :         if (mcs_set && nla_len(mcs_set) >= 8) {
    6323             :                 u8 *mcs;
    6324        1160 :                 mcs = nla_data(mcs_set);
    6325        1160 :                 os_memcpy(mode->vht_mcs_set, mcs, 8);
    6326             :         }
    6327       24360 : }
    6328             : 
    6329             : 
    6330       22040 : static void phy_info_freq(struct hostapd_hw_modes *mode,
    6331             :                           struct hostapd_channel_data *chan,
    6332             :                           struct nlattr *tb_freq[])
    6333             : {
    6334             :         u8 channel;
    6335       22040 :         chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
    6336       22040 :         chan->flag = 0;
    6337       22040 :         chan->dfs_cac_ms = 0;
    6338       22040 :         if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
    6339       22040 :                 chan->chan = channel;
    6340             : 
    6341       22040 :         if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
    6342        8562 :                 chan->flag |= HOSTAPD_CHAN_DISABLED;
    6343       22040 :         if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
    6344        6506 :                 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
    6345       22040 :         if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
    6346        8696 :                 chan->flag |= HOSTAPD_CHAN_RADAR;
    6347             : 
    6348       22040 :         if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
    6349        8696 :                 enum nl80211_dfs_state state =
    6350        8696 :                         nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
    6351             : 
    6352        8696 :                 switch (state) {
    6353             :                 case NL80211_DFS_USABLE:
    6354        8696 :                         chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
    6355        8696 :                         break;
    6356             :                 case NL80211_DFS_AVAILABLE:
    6357           0 :                         chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
    6358           0 :                         break;
    6359             :                 case NL80211_DFS_UNAVAILABLE:
    6360           0 :                         chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
    6361           0 :                         break;
    6362             :                 }
    6363             :         }
    6364             : 
    6365       22040 :         if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]) {
    6366        8696 :                 chan->dfs_cac_ms = nla_get_u32(
    6367        8696 :                         tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]);
    6368             :         }
    6369       22040 : }
    6370             : 
    6371             : 
    6372       24360 : static int phy_info_freqs(struct phy_info_arg *phy_info,
    6373             :                           struct hostapd_hw_modes *mode, struct nlattr *tb)
    6374             : {
    6375             :         static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
    6376             :                 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
    6377             :                 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
    6378             :                 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
    6379             :                 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
    6380             :                 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
    6381             :                 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
    6382             :         };
    6383       24360 :         int new_channels = 0;
    6384             :         struct hostapd_channel_data *channel;
    6385             :         struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
    6386             :         struct nlattr *nl_freq;
    6387             :         int rem_freq, idx;
    6388             : 
    6389       24360 :         if (tb == NULL)
    6390        1160 :                 return NL_OK;
    6391             : 
    6392       45240 :         nla_for_each_nested(nl_freq, tb, rem_freq) {
    6393       44080 :                 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
    6394       22040 :                           nla_data(nl_freq), nla_len(nl_freq), freq_policy);
    6395       22040 :                 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
    6396           0 :                         continue;
    6397       22040 :                 new_channels++;
    6398             :         }
    6399             : 
    6400       23200 :         channel = os_realloc_array(mode->channels,
    6401       23200 :                                    mode->num_channels + new_channels,
    6402             :                                    sizeof(struct hostapd_channel_data));
    6403       23200 :         if (!channel)
    6404           0 :                 return NL_SKIP;
    6405             : 
    6406       23200 :         mode->channels = channel;
    6407       23200 :         mode->num_channels += new_channels;
    6408             : 
    6409       23200 :         idx = phy_info->last_chan_idx;
    6410             : 
    6411       45240 :         nla_for_each_nested(nl_freq, tb, rem_freq) {
    6412       44080 :                 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
    6413       22040 :                           nla_data(nl_freq), nla_len(nl_freq), freq_policy);
    6414       22040 :                 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
    6415           0 :                         continue;
    6416       22040 :                 phy_info_freq(mode, &mode->channels[idx], tb_freq);
    6417       22040 :                 idx++;
    6418             :         }
    6419       23200 :         phy_info->last_chan_idx = idx;
    6420             : 
    6421       23200 :         return NL_OK;
    6422             : }
    6423             : 
    6424             : 
    6425       24360 : static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
    6426             : {
    6427             :         static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
    6428             :                 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
    6429             :                 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
    6430             :                 { .type = NLA_FLAG },
    6431             :         };
    6432             :         struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
    6433             :         struct nlattr *nl_rate;
    6434             :         int rem_rate, idx;
    6435             : 
    6436       24360 :         if (tb == NULL)
    6437       23200 :                 return NL_OK;
    6438             : 
    6439       12760 :         nla_for_each_nested(nl_rate, tb, rem_rate) {
    6440       23200 :                 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
    6441       11600 :                           nla_data(nl_rate), nla_len(nl_rate),
    6442             :                           rate_policy);
    6443       11600 :                 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
    6444           0 :                         continue;
    6445       11600 :                 mode->num_rates++;
    6446             :         }
    6447             : 
    6448        1160 :         mode->rates = os_calloc(mode->num_rates, sizeof(int));
    6449        1160 :         if (!mode->rates)
    6450           0 :                 return NL_SKIP;
    6451             : 
    6452        1160 :         idx = 0;
    6453             : 
    6454       12760 :         nla_for_each_nested(nl_rate, tb, rem_rate) {
    6455       23200 :                 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
    6456       11600 :                           nla_data(nl_rate), nla_len(nl_rate),
    6457             :                           rate_policy);
    6458       11600 :                 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
    6459           0 :                         continue;
    6460       11600 :                 mode->rates[idx] = nla_get_u32(
    6461             :                         tb_rate[NL80211_BITRATE_ATTR_RATE]);
    6462       11600 :                 idx++;
    6463             :         }
    6464             : 
    6465        1160 :         return NL_OK;
    6466             : }
    6467             : 
    6468             : 
    6469       24360 : static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
    6470             : {
    6471             :         struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
    6472             :         struct hostapd_hw_modes *mode;
    6473             :         int ret;
    6474             : 
    6475       24360 :         if (phy_info->last_mode != nl_band->nla_type) {
    6476        1160 :                 mode = os_realloc_array(phy_info->modes,
    6477        1160 :                                         *phy_info->num_modes + 1,
    6478             :                                         sizeof(*mode));
    6479        1160 :                 if (!mode)
    6480           0 :                         return NL_SKIP;
    6481        1160 :                 phy_info->modes = mode;
    6482             : 
    6483        1160 :                 mode = &phy_info->modes[*(phy_info->num_modes)];
    6484        1160 :                 os_memset(mode, 0, sizeof(*mode));
    6485        1160 :                 mode->mode = NUM_HOSTAPD_MODES;
    6486        1160 :                 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
    6487             :                         HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
    6488             : 
    6489             :                 /*
    6490             :                  * Unsupported VHT MCS stream is defined as value 3, so the VHT
    6491             :                  * MCS RX/TX map must be initialized with 0xffff to mark all 8
    6492             :                  * possible streams as unsupported. This will be overridden if
    6493             :                  * driver advertises VHT support.
    6494             :                  */
    6495        1160 :                 mode->vht_mcs_set[0] = 0xff;
    6496        1160 :                 mode->vht_mcs_set[1] = 0xff;
    6497        1160 :                 mode->vht_mcs_set[4] = 0xff;
    6498        1160 :                 mode->vht_mcs_set[5] = 0xff;
    6499             : 
    6500        1160 :                 *(phy_info->num_modes) += 1;
    6501        1160 :                 phy_info->last_mode = nl_band->nla_type;
    6502        1160 :                 phy_info->last_chan_idx = 0;
    6503             :         } else
    6504       23200 :                 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
    6505             : 
    6506       24360 :         nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
    6507             :                   nla_len(nl_band), NULL);
    6508             : 
    6509       24360 :         phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
    6510             :                          tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
    6511             :                          tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
    6512             :                          tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
    6513       24360 :         phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
    6514             :                           tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
    6515       24360 :         ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
    6516       24360 :         if (ret != NL_OK)
    6517           0 :                 return ret;
    6518       24360 :         ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
    6519       24360 :         if (ret != NL_OK)
    6520           0 :                 return ret;
    6521             : 
    6522       24360 :         return NL_OK;
    6523             : }
    6524             : 
    6525             : 
    6526       30740 : static int phy_info_handler(struct nl_msg *msg, void *arg)
    6527             : {
    6528             :         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
    6529       30740 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    6530       30740 :         struct phy_info_arg *phy_info = arg;
    6531             :         struct nlattr *nl_band;
    6532             :         int rem_band;
    6533             : 
    6534       30740 :         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    6535             :                   genlmsg_attrlen(gnlh, 0), NULL);
    6536             : 
    6537       30740 :         if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
    6538        5800 :                 return NL_SKIP;
    6539             : 
    6540       49300 :         nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
    6541             :         {
    6542       24360 :                 int res = phy_info_band(phy_info, nl_band);
    6543       24360 :                 if (res != NL_OK)
    6544           0 :                         return res;
    6545             :         }
    6546             : 
    6547       24940 :         return NL_SKIP;
    6548             : }
    6549             : 
    6550             : 
    6551             : static struct hostapd_hw_modes *
    6552         580 : wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
    6553             :                                      u16 *num_modes)
    6554             : {
    6555             :         u16 m;
    6556         580 :         struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
    6557         580 :         int i, mode11g_idx = -1;
    6558             : 
    6559             :         /* heuristic to set up modes */
    6560        1740 :         for (m = 0; m < *num_modes; m++) {
    6561        1160 :                 if (!modes[m].num_channels)
    6562           0 :                         continue;
    6563        1160 :                 if (modes[m].channels[0].freq < 4000) {
    6564         580 :                         modes[m].mode = HOSTAPD_MODE_IEEE80211B;
    6565        5220 :                         for (i = 0; i < modes[m].num_rates; i++) {
    6566        5220 :                                 if (modes[m].rates[i] > 200) {
    6567         580 :                                         modes[m].mode = HOSTAPD_MODE_IEEE80211G;
    6568         580 :                                         break;
    6569             :                                 }
    6570             :                         }
    6571         580 :                 } else if (modes[m].channels[0].freq > 50000)
    6572           0 :                         modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
    6573             :                 else
    6574         580 :                         modes[m].mode = HOSTAPD_MODE_IEEE80211A;
    6575             :         }
    6576             : 
    6577             :         /* If only 802.11g mode is included, use it to construct matching
    6578             :          * 802.11b mode data. */
    6579             : 
    6580        1740 :         for (m = 0; m < *num_modes; m++) {
    6581        1160 :                 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
    6582           0 :                         return modes; /* 802.11b already included */
    6583        1160 :                 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
    6584         580 :                         mode11g_idx = m;
    6585             :         }
    6586             : 
    6587         580 :         if (mode11g_idx < 0)
    6588           0 :                 return modes; /* 2.4 GHz band not supported at all */
    6589             : 
    6590         580 :         nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
    6591         580 :         if (nmodes == NULL)
    6592           0 :                 return modes; /* Could not add 802.11b mode */
    6593             : 
    6594         580 :         mode = &nmodes[*num_modes];
    6595         580 :         os_memset(mode, 0, sizeof(*mode));
    6596         580 :         (*num_modes)++;
    6597         580 :         modes = nmodes;
    6598             : 
    6599         580 :         mode->mode = HOSTAPD_MODE_IEEE80211B;
    6600             : 
    6601         580 :         mode11g = &modes[mode11g_idx];
    6602         580 :         mode->num_channels = mode11g->num_channels;
    6603         580 :         mode->channels = os_malloc(mode11g->num_channels *
    6604             :                                    sizeof(struct hostapd_channel_data));
    6605         580 :         if (mode->channels == NULL) {
    6606           0 :                 (*num_modes)--;
    6607           0 :                 return modes; /* Could not add 802.11b mode */
    6608             :         }
    6609         580 :         os_memcpy(mode->channels, mode11g->channels,
    6610             :                   mode11g->num_channels * sizeof(struct hostapd_channel_data));
    6611             : 
    6612         580 :         mode->num_rates = 0;
    6613         580 :         mode->rates = os_malloc(4 * sizeof(int));
    6614         580 :         if (mode->rates == NULL) {
    6615           0 :                 os_free(mode->channels);
    6616           0 :                 (*num_modes)--;
    6617           0 :                 return modes; /* Could not add 802.11b mode */
    6618             :         }
    6619             : 
    6620        2320 :         for (i = 0; i < mode11g->num_rates; i++) {
    6621        3480 :                 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
    6622        1740 :                     mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
    6623           0 :                         continue;
    6624        2320 :                 mode->rates[mode->num_rates] = mode11g->rates[i];
    6625        2320 :                 mode->num_rates++;
    6626        2320 :                 if (mode->num_rates == 4)
    6627         580 :                         break;
    6628             :         }
    6629             : 
    6630         580 :         if (mode->num_rates == 0) {
    6631           0 :                 os_free(mode->channels);
    6632           0 :                 os_free(mode->rates);
    6633           0 :                 (*num_modes)--;
    6634           0 :                 return modes; /* No 802.11b rates */
    6635             :         }
    6636             : 
    6637         580 :         wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
    6638             :                    "information");
    6639             : 
    6640         580 :         return modes;
    6641             : }
    6642             : 
    6643             : 
    6644        5804 : static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
    6645             :                                   int end)
    6646             : {
    6647             :         int c;
    6648             : 
    6649      116080 :         for (c = 0; c < mode->num_channels; c++) {
    6650      110276 :                 struct hostapd_channel_data *chan = &mode->channels[c];
    6651      110276 :                 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
    6652       12918 :                         chan->flag |= HOSTAPD_CHAN_HT40;
    6653             :         }
    6654        5804 : }
    6655             : 
    6656             : 
    6657        6926 : static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
    6658             :                                       int end)
    6659             : {
    6660             :         int c;
    6661             : 
    6662      138520 :         for (c = 0; c < mode->num_channels; c++) {
    6663      131594 :                 struct hostapd_channel_data *chan = &mode->channels[c];
    6664      131594 :                 if (!(chan->flag & HOSTAPD_CHAN_HT40))
    6665       54558 :                         continue;
    6666       77036 :                 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
    6667        9440 :                         chan->flag |= HOSTAPD_CHAN_HT40MINUS;
    6668       77036 :                 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
    6669        9440 :                         chan->flag |= HOSTAPD_CHAN_HT40PLUS;
    6670             :         }
    6671        6926 : }
    6672             : 
    6673             : 
    6674        3463 : static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
    6675             :                                       struct phy_info_arg *results)
    6676             : {
    6677             :         u16 m;
    6678             : 
    6679       10389 :         for (m = 0; m < *results->num_modes; m++) {
    6680             :                 int c;
    6681        6926 :                 struct hostapd_hw_modes *mode = &results->modes[m];
    6682             : 
    6683      138520 :                 for (c = 0; c < mode->num_channels; c++) {
    6684      131594 :                         struct hostapd_channel_data *chan = &mode->channels[c];
    6685      200283 :                         if ((u32) chan->freq - 10 >= start &&
    6686       68689 :                             (u32) chan->freq + 10 <= end)
    6687       13479 :                                 chan->max_tx_power = max_eirp;
    6688             :                 }
    6689             :         }
    6690        3463 : }
    6691             : 
    6692             : 
    6693        2902 : static void nl80211_reg_rule_ht40(u32 start, u32 end,
    6694             :                                   struct phy_info_arg *results)
    6695             : {
    6696             :         u16 m;
    6697             : 
    6698        8706 :         for (m = 0; m < *results->num_modes; m++) {
    6699        5804 :                 if (!(results->modes[m].ht_capab &
    6700             :                       HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
    6701           0 :                         continue;
    6702        5804 :                 nl80211_set_ht40_mode(&results->modes[m], start, end);
    6703             :         }
    6704        2902 : }
    6705             : 
    6706             : 
    6707        3463 : static void nl80211_reg_rule_sec(struct nlattr *tb[],
    6708             :                                  struct phy_info_arg *results)
    6709             : {
    6710             :         u32 start, end, max_bw;
    6711             :         u16 m;
    6712             : 
    6713        6926 :         if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
    6714        6926 :             tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
    6715        3463 :             tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
    6716           0 :                 return;
    6717             : 
    6718        3463 :         start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
    6719        3463 :         end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
    6720        3463 :         max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
    6721             : 
    6722        3463 :         if (max_bw < 20)
    6723           0 :                 return;
    6724             : 
    6725       10389 :         for (m = 0; m < *results->num_modes; m++) {
    6726        6926 :                 if (!(results->modes[m].ht_capab &
    6727             :                       HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
    6728           0 :                         continue;
    6729        6926 :                 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
    6730             :         }
    6731             : }
    6732             : 
    6733             : 
    6734        3516 : static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
    6735             :                                  int end)
    6736             : {
    6737             :         int c;
    6738             : 
    6739       70320 :         for (c = 0; c < mode->num_channels; c++) {
    6740       66804 :                 struct hostapd_channel_data *chan = &mode->channels[c];
    6741       66804 :                 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
    6742        1852 :                         chan->flag |= HOSTAPD_CHAN_VHT_10_70;
    6743             : 
    6744       66804 :                 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
    6745        1854 :                         chan->flag |= HOSTAPD_CHAN_VHT_30_50;
    6746             : 
    6747       66804 :                 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
    6748        1854 :                         chan->flag |= HOSTAPD_CHAN_VHT_50_30;
    6749             : 
    6750       66804 :                 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
    6751        1852 :                         chan->flag |= HOSTAPD_CHAN_VHT_70_10;
    6752             :         }
    6753        3516 : }
    6754             : 
    6755             : 
    6756        3463 : static void nl80211_reg_rule_vht(struct nlattr *tb[],
    6757             :                                  struct phy_info_arg *results)
    6758             : {
    6759             :         u32 start, end, max_bw;
    6760             :         u16 m;
    6761             : 
    6762        6926 :         if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
    6763        6926 :             tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
    6764        3463 :             tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
    6765           0 :                 return;
    6766             : 
    6767        3463 :         start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
    6768        3463 :         end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
    6769        3463 :         max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
    6770             : 
    6771        3463 :         if (max_bw < 80)
    6772        1705 :                 return;
    6773             : 
    6774        5274 :         for (m = 0; m < *results->num_modes; m++) {
    6775        3516 :                 if (!(results->modes[m].ht_capab &
    6776             :                       HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
    6777           0 :                         continue;
    6778             :                 /* TODO: use a real VHT support indication */
    6779        3516 :                 if (!results->modes[m].vht_capab)
    6780           0 :                         continue;
    6781             : 
    6782        3516 :                 nl80211_set_vht_mode(&results->modes[m], start, end);
    6783             :         }
    6784             : }
    6785             : 
    6786             : 
    6787           0 : static const char * dfs_domain_name(enum nl80211_dfs_regions region)
    6788             : {
    6789           0 :         switch (region) {
    6790             :         case NL80211_DFS_UNSET:
    6791           0 :                 return "DFS-UNSET";
    6792             :         case NL80211_DFS_FCC:
    6793           0 :                 return "DFS-FCC";
    6794             :         case NL80211_DFS_ETSI:
    6795           0 :                 return "DFS-ETSI";
    6796             :         case NL80211_DFS_JP:
    6797           0 :                 return "DFS-JP";
    6798             :         default:
    6799           0 :                 return "DFS-invalid";
    6800             :         }
    6801             : }
    6802             : 
    6803             : 
    6804         580 : static int nl80211_get_reg(struct nl_msg *msg, void *arg)
    6805             : {
    6806         580 :         struct phy_info_arg *results = arg;
    6807             :         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
    6808         580 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    6809             :         struct nlattr *nl_rule;
    6810             :         struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
    6811             :         int rem_rule;
    6812             :         static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
    6813             :                 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
    6814             :                 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
    6815             :                 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
    6816             :                 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
    6817             :                 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
    6818             :                 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
    6819             :         };
    6820             : 
    6821         580 :         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    6822             :                   genlmsg_attrlen(gnlh, 0), NULL);
    6823        1160 :         if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
    6824         580 :             !tb_msg[NL80211_ATTR_REG_RULES]) {
    6825           0 :                 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
    6826             :                            "available");
    6827           0 :                 return NL_SKIP;
    6828             :         }
    6829             : 
    6830         580 :         if (tb_msg[NL80211_ATTR_DFS_REGION]) {
    6831             :                 enum nl80211_dfs_regions dfs_domain;
    6832           0 :                 dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
    6833           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
    6834           0 :                            (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
    6835             :                            dfs_domain_name(dfs_domain));
    6836             :         } else {
    6837         580 :                 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
    6838         580 :                            (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
    6839             :         }
    6840             : 
    6841        4043 :         nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
    6842             :         {
    6843        3463 :                 u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
    6844        6926 :                 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
    6845        3463 :                           nla_data(nl_rule), nla_len(nl_rule), reg_policy);
    6846        6926 :                 if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
    6847        3463 :                     tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
    6848           0 :                         continue;
    6849        3463 :                 start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
    6850        3463 :                 end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
    6851        3463 :                 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
    6852        3463 :                         max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
    6853        3463 :                 if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
    6854        3463 :                         max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
    6855        3463 :                 if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
    6856        3463 :                         flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
    6857             : 
    6858       27704 :                 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
    6859             :                            start, end, max_bw, max_eirp,
    6860        3463 :                            flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
    6861        3463 :                            flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
    6862        3463 :                            flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
    6863        3463 :                            flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
    6864             :                            "",
    6865        3463 :                            flags & NL80211_RRF_DFS ? " (DFS)" : "",
    6866        3463 :                            flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
    6867        3463 :                            flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
    6868        3463 :                            flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
    6869        3463 :                 if (max_bw >= 40)
    6870        2902 :                         nl80211_reg_rule_ht40(start, end, results);
    6871        3463 :                 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
    6872        3463 :                         nl80211_reg_rule_max_eirp(start, end, max_eirp,
    6873             :                                                   results);
    6874             :         }
    6875             : 
    6876        4043 :         nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
    6877             :         {
    6878        6926 :                 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
    6879        3463 :                           nla_data(nl_rule), nla_len(nl_rule), reg_policy);
    6880        3463 :                 nl80211_reg_rule_sec(tb_rule, results);
    6881             :         }
    6882             : 
    6883        4043 :         nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
    6884             :         {
    6885        6926 :                 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
    6886        3463 :                           nla_data(nl_rule), nla_len(nl_rule), reg_policy);
    6887        3463 :                 nl80211_reg_rule_vht(tb_rule, results);
    6888             :         }
    6889             : 
    6890         580 :         return NL_SKIP;
    6891             : }
    6892             : 
    6893             : 
    6894         580 : static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
    6895             :                                         struct phy_info_arg *results)
    6896             : {
    6897             :         struct nl_msg *msg;
    6898             : 
    6899         580 :         msg = nlmsg_alloc();
    6900         580 :         if (!msg)
    6901           0 :                 return -ENOMEM;
    6902             : 
    6903         580 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
    6904         580 :         return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
    6905             : }
    6906             : 
    6907             : 
    6908             : static struct hostapd_hw_modes *
    6909         580 : wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
    6910             : {
    6911             :         u32 feat;
    6912         580 :         struct i802_bss *bss = priv;
    6913         580 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    6914             :         struct nl_msg *msg;
    6915         580 :         struct phy_info_arg result = {
    6916             :                 .num_modes = num_modes,
    6917             :                 .modes = NULL,
    6918             :                 .last_mode = -1,
    6919             :         };
    6920             : 
    6921         580 :         *num_modes = 0;
    6922         580 :         *flags = 0;
    6923             : 
    6924         580 :         msg = nlmsg_alloc();
    6925         580 :         if (!msg)
    6926           0 :                 return NULL;
    6927             : 
    6928         580 :         feat = get_nl80211_protocol_features(drv);
    6929         580 :         if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
    6930         580 :                 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
    6931             :         else
    6932           0 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
    6933             : 
    6934         580 :         NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
    6935         580 :         if (nl80211_set_iface_id(msg, bss) < 0)
    6936           0 :                 goto nla_put_failure;
    6937             : 
    6938         580 :         if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
    6939         580 :                 nl80211_set_regulatory_flags(drv, &result);
    6940         580 :                 return wpa_driver_nl80211_postprocess_modes(result.modes,
    6941             :                                                             num_modes);
    6942             :         }
    6943           0 :         msg = NULL;
    6944             :  nla_put_failure:
    6945           0 :         nlmsg_free(msg);
    6946           0 :         return NULL;
    6947             : }
    6948             : 
    6949             : 
    6950           0 : static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
    6951             :                                         const void *data, size_t len,
    6952             :                                         int encrypt, int noack)
    6953             : {
    6954           0 :         __u8 rtap_hdr[] = {
    6955             :                 0x00, 0x00, /* radiotap version */
    6956             :                 0x0e, 0x00, /* radiotap length */
    6957             :                 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
    6958             :                 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
    6959             :                 0x00,       /* padding */
    6960             :                 0x00, 0x00, /* RX and TX flags to indicate that */
    6961             :                 0x00, 0x00, /* this is the injected frame directly */
    6962             :         };
    6963           0 :         struct iovec iov[2] = {
    6964             :                 {
    6965             :                         .iov_base = &rtap_hdr,
    6966             :                         .iov_len = sizeof(rtap_hdr),
    6967             :                 },
    6968             :                 {
    6969             :                         .iov_base = (void *) data,
    6970             :                         .iov_len = len,
    6971             :                 }
    6972             :         };
    6973           0 :         struct msghdr msg = {
    6974             :                 .msg_name = NULL,
    6975             :                 .msg_namelen = 0,
    6976             :                 .msg_iov = iov,
    6977             :                 .msg_iovlen = 2,
    6978             :                 .msg_control = NULL,
    6979             :                 .msg_controllen = 0,
    6980             :                 .msg_flags = 0,
    6981             :         };
    6982             :         int res;
    6983           0 :         u16 txflags = 0;
    6984             : 
    6985           0 :         if (encrypt)
    6986           0 :                 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
    6987             : 
    6988           0 :         if (drv->monitor_sock < 0) {
    6989           0 :                 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
    6990             :                            "for %s", __func__);
    6991           0 :                 return -1;
    6992             :         }
    6993             : 
    6994           0 :         if (noack)
    6995           0 :                 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
    6996           0 :         WPA_PUT_LE16(&rtap_hdr[12], txflags);
    6997             : 
    6998           0 :         res = sendmsg(drv->monitor_sock, &msg, 0);
    6999           0 :         if (res < 0) {
    7000           0 :                 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
    7001           0 :                 return -1;
    7002             :         }
    7003           0 :         return 0;
    7004             : }
    7005             : 
    7006             : 
    7007        5155 : static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
    7008             :                                          const void *data, size_t len,
    7009             :                                          int encrypt, int noack,
    7010             :                                          unsigned int freq, int no_cck,
    7011             :                                          int offchanok, unsigned int wait_time)
    7012             : {
    7013        5155 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7014             :         u64 cookie;
    7015             :         int res;
    7016             : 
    7017        5155 :         if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
    7018           0 :                 freq = nl80211_get_assoc_freq(drv);
    7019           0 :                 wpa_printf(MSG_DEBUG,
    7020             :                            "nl80211: send_frame - Use assoc_freq=%u for IBSS",
    7021             :                            freq);
    7022             :         }
    7023        5155 :         if (freq == 0) {
    7024        4917 :                 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
    7025             :                            bss->freq);
    7026        4917 :                 freq = bss->freq;
    7027             :         }
    7028             : 
    7029        5155 :         if (drv->use_monitor) {
    7030           0 :                 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
    7031             :                            freq, bss->freq);
    7032           0 :                 return wpa_driver_nl80211_send_mntr(drv, data, len,
    7033             :                                                     encrypt, noack);
    7034             :         }
    7035             : 
    7036        5155 :         wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
    7037        5155 :         res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
    7038             :                                      &cookie, no_cck, noack, offchanok);
    7039        5155 :         if (res == 0 && !noack) {
    7040             :                 const struct ieee80211_mgmt *mgmt;
    7041             :                 u16 fc;
    7042             : 
    7043        3401 :                 mgmt = (const struct ieee80211_mgmt *) data;
    7044        3401 :                 fc = le_to_host16(mgmt->frame_control);
    7045        6802 :                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
    7046        3401 :                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
    7047        1224 :                         wpa_printf(MSG_MSGDUMP,
    7048             :                                    "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
    7049             :                                    (long long unsigned int)
    7050         612 :                                    drv->send_action_cookie,
    7051             :                                    (long long unsigned int) cookie);
    7052         612 :                         drv->send_action_cookie = cookie;
    7053             :                 }
    7054             :         }
    7055             : 
    7056        5155 :         return res;
    7057             : }
    7058             : 
    7059             : 
    7060        5153 : static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
    7061             :                                         size_t data_len, int noack,
    7062             :                                         unsigned int freq, int no_cck,
    7063             :                                         int offchanok,
    7064             :                                         unsigned int wait_time)
    7065             : {
    7066        5153 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7067             :         struct ieee80211_mgmt *mgmt;
    7068        5153 :         int encrypt = 1;
    7069             :         u16 fc;
    7070             : 
    7071        5153 :         mgmt = (struct ieee80211_mgmt *) data;
    7072        5153 :         fc = le_to_host16(mgmt->frame_control);
    7073        5153 :         wpa_printf(MSG_DEBUG, "nl80211: send_mlme - noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x nlmode=%d",
    7074        5153 :                    noack, freq, no_cck, offchanok, wait_time, fc, drv->nlmode);
    7075             : 
    7076       10306 :         if ((is_sta_interface(drv->nlmode) ||
    7077        5153 :              drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
    7078           0 :             WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
    7079           0 :             WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
    7080             :                 /*
    7081             :                  * The use of last_mgmt_freq is a bit of a hack,
    7082             :                  * but it works due to the single-threaded nature
    7083             :                  * of wpa_supplicant.
    7084             :                  */
    7085           0 :                 if (freq == 0) {
    7086           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
    7087             :                                    drv->last_mgmt_freq);
    7088           0 :                         freq = drv->last_mgmt_freq;
    7089             :                 }
    7090           0 :                 return nl80211_send_frame_cmd(bss, freq, 0,
    7091             :                                               data, data_len, NULL, 1, noack,
    7092             :                                               1);
    7093             :         }
    7094             : 
    7095        5153 :         if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
    7096           0 :                 if (freq == 0) {
    7097           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
    7098             :                                    bss->freq);
    7099           0 :                         freq = bss->freq;
    7100             :                 }
    7101           0 :                 return nl80211_send_frame_cmd(bss, freq,
    7102           0 :                                               (int) freq == bss->freq ? 0 :
    7103             :                                               wait_time,
    7104             :                                               data, data_len,
    7105             :                                               &drv->send_action_cookie,
    7106             :                                               no_cck, noack, offchanok);
    7107             :         }
    7108             : 
    7109       10306 :         if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
    7110        5153 :             WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
    7111             :                 /*
    7112             :                  * Only one of the authentication frame types is encrypted.
    7113             :                  * In order for static WEP encryption to work properly (i.e.,
    7114             :                  * to not encrypt the frame), we need to tell mac80211 about
    7115             :                  * the frames that must not be encrypted.
    7116             :                  */
    7117         945 :                 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
    7118         945 :                 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
    7119         945 :                 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
    7120         945 :                         encrypt = 0;
    7121             :         }
    7122             : 
    7123        5153 :         wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
    7124        5153 :         return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
    7125             :                                              noack, freq, no_cck, offchanok,
    7126             :                                              wait_time);
    7127             : }
    7128             : 
    7129             : 
    7130         805 : static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
    7131             :                            int slot, int ht_opmode, int ap_isolate,
    7132             :                            int *basic_rates)
    7133             : {
    7134         805 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7135             :         struct nl_msg *msg;
    7136             : 
    7137         805 :         msg = nlmsg_alloc();
    7138         805 :         if (!msg)
    7139           0 :                 return -ENOMEM;
    7140             : 
    7141         805 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
    7142             : 
    7143         805 :         if (cts >= 0)
    7144         805 :                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
    7145         805 :         if (preamble >= 0)
    7146         805 :                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
    7147         805 :         if (slot >= 0)
    7148         776 :                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
    7149         805 :         if (ht_opmode >= 0)
    7150         757 :                 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
    7151         805 :         if (ap_isolate >= 0)
    7152         805 :                 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
    7153             : 
    7154         805 :         if (basic_rates) {
    7155             :                 u8 rates[NL80211_MAX_SUPP_RATES];
    7156         805 :                 u8 rates_len = 0;
    7157             :                 int i;
    7158             : 
    7159        4801 :                 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
    7160        3191 :                      i++)
    7161        3191 :                         rates[rates_len++] = basic_rates[i] / 5;
    7162             : 
    7163         805 :                 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
    7164             :         }
    7165             : 
    7166         805 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
    7167             : 
    7168         805 :         return send_and_recv_msgs(drv, msg, NULL, NULL);
    7169             :  nla_put_failure:
    7170           0 :         nlmsg_free(msg);
    7171           0 :         return -ENOBUFS;
    7172             : }
    7173             : 
    7174             : 
    7175           0 : static int wpa_driver_nl80211_set_acl(void *priv,
    7176             :                                       struct hostapd_acl_params *params)
    7177             : {
    7178           0 :         struct i802_bss *bss = priv;
    7179           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7180             :         struct nl_msg *msg;
    7181             :         struct nlattr *acl;
    7182             :         unsigned int i;
    7183           0 :         int ret = 0;
    7184             : 
    7185           0 :         if (!(drv->capa.max_acl_mac_addrs))
    7186           0 :                 return -ENOTSUP;
    7187             : 
    7188           0 :         if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
    7189           0 :                 return -ENOTSUP;
    7190             : 
    7191           0 :         msg = nlmsg_alloc();
    7192           0 :         if (!msg)
    7193           0 :                 return -ENOMEM;
    7194             : 
    7195           0 :         wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
    7196           0 :                    params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
    7197             : 
    7198           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
    7199             : 
    7200           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    7201             : 
    7202           0 :         NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
    7203             :                     NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
    7204             :                     NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
    7205             : 
    7206           0 :         acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
    7207           0 :         if (acl == NULL)
    7208           0 :                 goto nla_put_failure;
    7209             : 
    7210           0 :         for (i = 0; i < params->num_mac_acl; i++)
    7211           0 :                 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
    7212             : 
    7213           0 :         nla_nest_end(msg, acl);
    7214             : 
    7215           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    7216           0 :         msg = NULL;
    7217           0 :         if (ret) {
    7218           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
    7219             :                            ret, strerror(-ret));
    7220             :         }
    7221             : 
    7222             : nla_put_failure:
    7223           0 :         nlmsg_free(msg);
    7224             : 
    7225           0 :         return ret;
    7226             : }
    7227             : 
    7228             : 
    7229         805 : static int wpa_driver_nl80211_set_ap(void *priv,
    7230             :                                      struct wpa_driver_ap_params *params)
    7231             : {
    7232         805 :         struct i802_bss *bss = priv;
    7233         805 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7234             :         struct nl_msg *msg;
    7235         805 :         u8 cmd = NL80211_CMD_NEW_BEACON;
    7236             :         int ret;
    7237             :         int beacon_set;
    7238         805 :         int ifindex = if_nametoindex(bss->ifname);
    7239             :         int num_suites;
    7240             :         u32 suites[10], suite;
    7241             :         u32 ver;
    7242             : 
    7243         805 :         beacon_set = bss->beacon_set;
    7244             : 
    7245         805 :         msg = nlmsg_alloc();
    7246         805 :         if (!msg)
    7247           0 :                 return -ENOMEM;
    7248             : 
    7249         805 :         wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
    7250             :                    beacon_set);
    7251         805 :         if (beacon_set)
    7252         223 :                 cmd = NL80211_CMD_SET_BEACON;
    7253             : 
    7254         805 :         nl80211_cmd(drv, msg, 0, cmd);
    7255        1610 :         wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
    7256         805 :                     params->head, params->head_len);
    7257         805 :         NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
    7258        1610 :         wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
    7259         805 :                     params->tail, params->tail_len);
    7260         805 :         NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
    7261         805 :         wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
    7262         805 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
    7263         805 :         wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
    7264         805 :         NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
    7265         805 :         wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
    7266         805 :         NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
    7267        1610 :         wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
    7268         805 :                           params->ssid, params->ssid_len);
    7269         805 :         NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
    7270             :                 params->ssid);
    7271         805 :         if (params->proberesp && params->proberesp_len) {
    7272           0 :                 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
    7273           0 :                             params->proberesp, params->proberesp_len);
    7274           0 :                 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
    7275             :                         params->proberesp);
    7276             :         }
    7277         805 :         switch (params->hide_ssid) {
    7278             :         case NO_SSID_HIDING:
    7279         802 :                 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
    7280         802 :                 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
    7281             :                             NL80211_HIDDEN_SSID_NOT_IN_USE);
    7282         802 :                 break;
    7283             :         case HIDDEN_SSID_ZERO_LEN:
    7284           2 :                 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
    7285           2 :                 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
    7286             :                             NL80211_HIDDEN_SSID_ZERO_LEN);
    7287           2 :                 break;
    7288             :         case HIDDEN_SSID_ZERO_CONTENTS:
    7289           1 :                 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
    7290           1 :                 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
    7291             :                             NL80211_HIDDEN_SSID_ZERO_CONTENTS);
    7292           1 :                 break;
    7293             :         }
    7294         805 :         wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
    7295         805 :         if (params->privacy)
    7296         590 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
    7297         805 :         wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
    7298         805 :         if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
    7299             :             (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
    7300             :                 /* Leave out the attribute */
    7301          58 :         } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
    7302           3 :                 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
    7303             :                             NL80211_AUTHTYPE_SHARED_KEY);
    7304             :         else
    7305          55 :                 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
    7306             :                             NL80211_AUTHTYPE_OPEN_SYSTEM);
    7307             : 
    7308         805 :         wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
    7309         805 :         ver = 0;
    7310         805 :         if (params->wpa_version & WPA_PROTO_WPA)
    7311          71 :                 ver |= NL80211_WPA_VERSION_1;
    7312         805 :         if (params->wpa_version & WPA_PROTO_RSN)
    7313         552 :                 ver |= NL80211_WPA_VERSION_2;
    7314         805 :         if (ver)
    7315         577 :                 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
    7316             : 
    7317         805 :         wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
    7318             :                    params->key_mgmt_suites);
    7319         805 :         num_suites = 0;
    7320         805 :         if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
    7321         217 :                 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
    7322         805 :         if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
    7323         541 :                 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
    7324         805 :         if (num_suites) {
    7325         758 :                 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
    7326             :                         num_suites * sizeof(u32), suites);
    7327             :         }
    7328             : 
    7329        1022 :         if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
    7330         217 :             params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
    7331           0 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
    7332             : 
    7333         805 :         wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
    7334             :                    params->pairwise_ciphers);
    7335         805 :         num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
    7336             :                                                  suites, ARRAY_SIZE(suites));
    7337         805 :         if (num_suites) {
    7338         592 :                 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
    7339             :                         num_suites * sizeof(u32), suites);
    7340             :         }
    7341             : 
    7342         805 :         wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
    7343             :                    params->group_cipher);
    7344         805 :         suite = wpa_cipher_to_cipher_suite(params->group_cipher);
    7345         805 :         if (suite)
    7346         592 :                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite);
    7347             : 
    7348         805 :         if (params->beacon_ies) {
    7349         805 :                 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
    7350             :                                 params->beacon_ies);
    7351         805 :                 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
    7352             :                         wpabuf_head(params->beacon_ies));
    7353             :         }
    7354         805 :         if (params->proberesp_ies) {
    7355         805 :                 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
    7356             :                                 params->proberesp_ies);
    7357         805 :                 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
    7358             :                         wpabuf_len(params->proberesp_ies),
    7359             :                         wpabuf_head(params->proberesp_ies));
    7360             :         }
    7361         805 :         if (params->assocresp_ies) {
    7362         805 :                 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
    7363             :                                 params->assocresp_ies);
    7364         805 :                 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
    7365             :                         wpabuf_len(params->assocresp_ies),
    7366             :                         wpabuf_head(params->assocresp_ies));
    7367             :         }
    7368             : 
    7369         805 :         if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)  {
    7370           0 :                 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
    7371             :                            params->ap_max_inactivity);
    7372           0 :                 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
    7373             :                             params->ap_max_inactivity);
    7374             :         }
    7375             : 
    7376         805 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    7377         805 :         if (ret) {
    7378           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
    7379             :                            ret, strerror(-ret));
    7380             :         } else {
    7381         805 :                 bss->beacon_set = 1;
    7382         805 :                 nl80211_set_bss(bss, params->cts_protect, params->preamble,
    7383             :                                 params->short_slot_time, params->ht_opmode,
    7384             :                                 params->isolate, params->basic_rates);
    7385        1028 :                 if (beacon_set && params->freq &&
    7386         223 :                     params->freq->bandwidth != bss->bandwidth) {
    7387          12 :                         wpa_printf(MSG_DEBUG,
    7388             :                                    "nl80211: Update BSS %s bandwidth: %d -> %d",
    7389           6 :                                    bss->ifname, bss->bandwidth,
    7390           6 :                                    params->freq->bandwidth);
    7391           6 :                         ret = nl80211_set_channel(bss, params->freq, 1);
    7392          12 :                         if (ret) {
    7393           0 :                                 wpa_printf(MSG_DEBUG,
    7394             :                                            "nl80211: Frequency set failed: %d (%s)",
    7395             :                                            ret, strerror(-ret));
    7396             :                         } else {
    7397           6 :                                 wpa_printf(MSG_DEBUG,
    7398             :                                            "nl80211: Frequency set succeeded for ht2040 coex");
    7399           6 :                                 bss->bandwidth = params->freq->bandwidth;
    7400             :                         }
    7401         799 :                 } else if (!beacon_set) {
    7402             :                         /*
    7403             :                          * cfg80211 updates the driver on frequence change in AP
    7404             :                          * mode only at the point when beaconing is started, so
    7405             :                          * set the initial value here.
    7406             :                          */
    7407         582 :                         bss->bandwidth = params->freq->bandwidth;
    7408             :                 }
    7409             :         }
    7410         805 :         return ret;
    7411             :  nla_put_failure:
    7412           0 :         nlmsg_free(msg);
    7413           0 :         return -ENOBUFS;
    7414             : }
    7415             : 
    7416             : 
    7417         573 : static int nl80211_put_freq_params(struct nl_msg *msg,
    7418             :                                    struct hostapd_freq_params *freq)
    7419             : {
    7420         573 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
    7421         573 :         if (freq->vht_enabled) {
    7422           3 :                 switch (freq->bandwidth) {
    7423             :                 case 20:
    7424           1 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
    7425             :                                     NL80211_CHAN_WIDTH_20);
    7426           1 :                         break;
    7427             :                 case 40:
    7428           0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
    7429             :                                     NL80211_CHAN_WIDTH_40);
    7430           0 :                         break;
    7431             :                 case 80:
    7432           2 :                         if (freq->center_freq2)
    7433           0 :                                 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
    7434             :                                             NL80211_CHAN_WIDTH_80P80);
    7435             :                         else
    7436           2 :                                 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
    7437             :                                             NL80211_CHAN_WIDTH_80);
    7438           2 :                         break;
    7439             :                 case 160:
    7440           0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
    7441             :                                     NL80211_CHAN_WIDTH_160);
    7442           0 :                         break;
    7443             :                 default:
    7444           0 :                         return -EINVAL;
    7445             :                 }
    7446           3 :                 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
    7447           3 :                 if (freq->center_freq2)
    7448           0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
    7449             :                                     freq->center_freq2);
    7450         570 :         } else if (freq->ht_enabled) {
    7451         565 :                 switch (freq->sec_channel_offset) {
    7452             :                 case -1:
    7453          11 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
    7454             :                                     NL80211_CHAN_HT40MINUS);
    7455          11 :                         break;
    7456             :                 case 1:
    7457          12 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
    7458             :                                     NL80211_CHAN_HT40PLUS);
    7459          12 :                         break;
    7460             :                 default:
    7461         542 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
    7462             :                                     NL80211_CHAN_HT20);
    7463         542 :                         break;
    7464             :                 }
    7465             :         }
    7466         573 :         return 0;
    7467             : 
    7468             : nla_put_failure:
    7469           0 :         return -ENOBUFS;
    7470             : }
    7471             : 
    7472             : 
    7473         573 : static int nl80211_set_channel(struct i802_bss *bss,
    7474             :                                struct hostapd_freq_params *freq, int set_chan)
    7475             : {
    7476         573 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7477             :         struct nl_msg *msg;
    7478             :         int ret;
    7479             : 
    7480         573 :         wpa_printf(MSG_DEBUG,
    7481             :                    "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
    7482             :                    freq->freq, freq->ht_enabled, freq->vht_enabled,
    7483             :                    freq->bandwidth, freq->center_freq1, freq->center_freq2);
    7484         573 :         msg = nlmsg_alloc();
    7485         573 :         if (!msg)
    7486           0 :                 return -1;
    7487             : 
    7488         573 :         nl80211_cmd(drv, msg, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
    7489             :                     NL80211_CMD_SET_WIPHY);
    7490             : 
    7491         573 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    7492         573 :         if (nl80211_put_freq_params(msg, freq) < 0)
    7493           0 :                 goto nla_put_failure;
    7494             : 
    7495         573 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    7496         573 :         msg = NULL;
    7497         573 :         if (ret == 0) {
    7498         573 :                 bss->freq = freq->freq;
    7499         573 :                 return 0;
    7500             :         }
    7501           0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
    7502             :                    "%d (%s)", freq->freq, ret, strerror(-ret));
    7503             : nla_put_failure:
    7504           0 :         nlmsg_free(msg);
    7505           0 :         return -1;
    7506             : }
    7507             : 
    7508             : 
    7509        5849 : static u32 sta_flags_nl80211(int flags)
    7510             : {
    7511        5849 :         u32 f = 0;
    7512             : 
    7513        5849 :         if (flags & WPA_STA_AUTHORIZED)
    7514        3167 :                 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
    7515        5849 :         if (flags & WPA_STA_WMM)
    7516        2971 :                 f |= BIT(NL80211_STA_FLAG_WME);
    7517        5849 :         if (flags & WPA_STA_SHORT_PREAMBLE)
    7518        2955 :                 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
    7519        5849 :         if (flags & WPA_STA_MFP)
    7520        1055 :                 f |= BIT(NL80211_STA_FLAG_MFP);
    7521        5849 :         if (flags & WPA_STA_TDLS_PEER)
    7522           0 :                 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
    7523             : 
    7524        5849 :         return f;
    7525             : }
    7526             : 
    7527             : 
    7528         993 : static int wpa_driver_nl80211_sta_add(void *priv,
    7529             :                                       struct hostapd_sta_add_params *params)
    7530             : {
    7531         993 :         struct i802_bss *bss = priv;
    7532         993 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7533             :         struct nl_msg *msg;
    7534             :         struct nl80211_sta_flag_update upd;
    7535         993 :         int ret = -ENOBUFS;
    7536             : 
    7537         993 :         if ((params->flags & WPA_STA_TDLS_PEER) &&
    7538           0 :             !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
    7539           0 :                 return -EOPNOTSUPP;
    7540             : 
    7541         993 :         msg = nlmsg_alloc();
    7542         993 :         if (!msg)
    7543           0 :                 return -ENOMEM;
    7544             : 
    7545        6951 :         wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
    7546        6951 :                    params->set ? "Set" : "Add", MAC2STR(params->addr));
    7547         993 :         nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
    7548             :                     NL80211_CMD_NEW_STATION);
    7549             : 
    7550         993 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
    7551         993 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
    7552         993 :         NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
    7553             :                 params->supp_rates);
    7554         993 :         wpa_hexdump(MSG_DEBUG, "  * supported rates", params->supp_rates,
    7555             :                     params->supp_rates_len);
    7556         993 :         if (!params->set) {
    7557         993 :                 if (params->aid) {
    7558         993 :                         wpa_printf(MSG_DEBUG, "  * aid=%u", params->aid);
    7559         993 :                         NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
    7560             :                 } else {
    7561             :                         /*
    7562             :                          * cfg80211 validates that AID is non-zero, so we have
    7563             :                          * to make this a non-zero value for the TDLS case where
    7564             :                          * a dummy STA entry is used for now.
    7565             :                          */
    7566           0 :                         wpa_printf(MSG_DEBUG, "  * aid=1 (TDLS workaround)");
    7567           0 :                         NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
    7568             :                 }
    7569         993 :                 wpa_printf(MSG_DEBUG, "  * listen_interval=%u",
    7570         993 :                            params->listen_interval);
    7571         993 :                 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
    7572             :                             params->listen_interval);
    7573           0 :         } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
    7574           0 :                 wpa_printf(MSG_DEBUG, "  * peer_aid=%u", params->aid);
    7575           0 :                 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
    7576             :         }
    7577         993 :         if (params->ht_capabilities) {
    7578         970 :                 wpa_hexdump(MSG_DEBUG, "  * ht_capabilities",
    7579         970 :                             (u8 *) params->ht_capabilities,
    7580             :                             sizeof(*params->ht_capabilities));
    7581         970 :                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
    7582             :                         sizeof(*params->ht_capabilities),
    7583             :                         params->ht_capabilities);
    7584             :         }
    7585             : 
    7586         993 :         if (params->vht_capabilities) {
    7587           3 :                 wpa_hexdump(MSG_DEBUG, "  * vht_capabilities",
    7588           3 :                             (u8 *) params->vht_capabilities,
    7589             :                             sizeof(*params->vht_capabilities));
    7590           3 :                 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
    7591             :                         sizeof(*params->vht_capabilities),
    7592             :                         params->vht_capabilities);
    7593             :         }
    7594             : 
    7595         993 :         if (params->vht_opmode_enabled) {
    7596           0 :                 wpa_printf(MSG_DEBUG, "  * opmode=%u", params->vht_opmode);
    7597           0 :                 NLA_PUT_U8(msg, NL80211_ATTR_OPMODE_NOTIF,
    7598             :                            params->vht_opmode);
    7599             :         }
    7600             : 
    7601         993 :         wpa_printf(MSG_DEBUG, "  * capability=0x%x", params->capability);
    7602         993 :         NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
    7603             : 
    7604         993 :         if (params->ext_capab) {
    7605           0 :                 wpa_hexdump(MSG_DEBUG, "  * ext_capab",
    7606           0 :                             params->ext_capab, params->ext_capab_len);
    7607           0 :                 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
    7608             :                         params->ext_capab_len, params->ext_capab);
    7609             :         }
    7610             : 
    7611         993 :         if (params->supp_channels) {
    7612           0 :                 wpa_hexdump(MSG_DEBUG, "  * supported channels",
    7613           0 :                             params->supp_channels, params->supp_channels_len);
    7614           0 :                 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
    7615             :                         params->supp_channels_len, params->supp_channels);
    7616             :         }
    7617             : 
    7618         993 :         if (params->supp_oper_classes) {
    7619           0 :                 wpa_hexdump(MSG_DEBUG, "  * supported operating classes",
    7620           0 :                             params->supp_oper_classes,
    7621             :                             params->supp_oper_classes_len);
    7622           0 :                 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
    7623             :                         params->supp_oper_classes_len,
    7624             :                         params->supp_oper_classes);
    7625             :         }
    7626             : 
    7627         993 :         os_memset(&upd, 0, sizeof(upd));
    7628         993 :         upd.mask = sta_flags_nl80211(params->flags);
    7629         993 :         upd.set = upd.mask;
    7630         993 :         wpa_printf(MSG_DEBUG, "  * flags set=0x%x mask=0x%x",
    7631             :                    upd.set, upd.mask);
    7632         993 :         NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
    7633             : 
    7634         993 :         if (params->flags & WPA_STA_WMM) {
    7635         989 :                 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
    7636             : 
    7637         989 :                 if (!wme)
    7638           0 :                         goto nla_put_failure;
    7639             : 
    7640         989 :                 wpa_printf(MSG_DEBUG, "  * qosinfo=0x%x", params->qosinfo);
    7641         989 :                 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
    7642             :                                 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
    7643         989 :                 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
    7644             :                                 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
    7645             :                                 WMM_QOSINFO_STA_SP_MASK);
    7646         989 :                 nla_nest_end(msg, wme);
    7647             :         }
    7648             : 
    7649         993 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    7650         993 :         msg = NULL;
    7651         993 :         if (ret)
    7652           0 :                 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
    7653           0 :                            "result: %d (%s)", params->set ? "SET" : "NEW", ret,
    7654             :                            strerror(-ret));
    7655         993 :         if (ret == -EEXIST)
    7656           0 :                 ret = 0;
    7657             :  nla_put_failure:
    7658         993 :         nlmsg_free(msg);
    7659         993 :         return ret;
    7660             : }
    7661             : 
    7662             : 
    7663        1744 : static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
    7664             : {
    7665        1744 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7666             :         struct nl_msg *msg;
    7667             :         int ret;
    7668             : 
    7669        1744 :         msg = nlmsg_alloc();
    7670        1744 :         if (!msg)
    7671           0 :                 return -ENOMEM;
    7672             : 
    7673        1744 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
    7674             : 
    7675        1744 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
    7676             :                     if_nametoindex(bss->ifname));
    7677        1744 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    7678             : 
    7679        1744 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    7680       13952 :         wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
    7681             :                    " --> %d (%s)",
    7682       12208 :                    bss->ifname, MAC2STR(addr), ret, strerror(-ret));
    7683        1744 :         if (ret == -ENOENT)
    7684         781 :                 return 0;
    7685         963 :         return ret;
    7686             :  nla_put_failure:
    7687           0 :         nlmsg_free(msg);
    7688           0 :         return -ENOBUFS;
    7689             : }
    7690             : 
    7691             : 
    7692          31 : static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
    7693             :                                  int ifidx)
    7694             : {
    7695             :         struct nl_msg *msg;
    7696             :         struct wpa_driver_nl80211_data *drv2;
    7697             : 
    7698          31 :         wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
    7699             : 
    7700             :         /* stop listening for EAPOL on this interface */
    7701          62 :         dl_list_for_each(drv2, &drv->global->interfaces,
    7702             :                          struct wpa_driver_nl80211_data, list)
    7703          31 :                 del_ifidx(drv2, ifidx);
    7704             : 
    7705          31 :         msg = nlmsg_alloc();
    7706          31 :         if (!msg)
    7707           0 :                 goto nla_put_failure;
    7708             : 
    7709          31 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
    7710          31 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
    7711             : 
    7712          31 :         if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
    7713          62 :                 return;
    7714           0 :         msg = NULL;
    7715             :  nla_put_failure:
    7716           0 :         nlmsg_free(msg);
    7717           0 :         wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
    7718             : }
    7719             : 
    7720             : 
    7721        1191 : static const char * nl80211_iftype_str(enum nl80211_iftype mode)
    7722             : {
    7723        1191 :         switch (mode) {
    7724             :         case NL80211_IFTYPE_ADHOC:
    7725           0 :                 return "ADHOC";
    7726             :         case NL80211_IFTYPE_STATION:
    7727         580 :                 return "STATION";
    7728             :         case NL80211_IFTYPE_AP:
    7729         599 :                 return "AP";
    7730             :         case NL80211_IFTYPE_AP_VLAN:
    7731          12 :                 return "AP_VLAN";
    7732             :         case NL80211_IFTYPE_WDS:
    7733           0 :                 return "WDS";
    7734             :         case NL80211_IFTYPE_MONITOR:
    7735           0 :                 return "MONITOR";
    7736             :         case NL80211_IFTYPE_MESH_POINT:
    7737           0 :                 return "MESH_POINT";
    7738             :         case NL80211_IFTYPE_P2P_CLIENT:
    7739           0 :                 return "P2P_CLIENT";
    7740             :         case NL80211_IFTYPE_P2P_GO:
    7741           0 :                 return "P2P_GO";
    7742             :         case NL80211_IFTYPE_P2P_DEVICE:
    7743           0 :                 return "P2P_DEVICE";
    7744             :         default:
    7745           0 :                 return "unknown";
    7746             :         }
    7747             : }
    7748             : 
    7749             : 
    7750          31 : static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
    7751             :                                      const char *ifname,
    7752             :                                      enum nl80211_iftype iftype,
    7753             :                                      const u8 *addr, int wds,
    7754             :                                      int (*handler)(struct nl_msg *, void *),
    7755             :                                      void *arg)
    7756             : {
    7757             :         struct nl_msg *msg;
    7758             :         int ifidx;
    7759          31 :         int ret = -ENOBUFS;
    7760             : 
    7761          31 :         wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
    7762             :                    iftype, nl80211_iftype_str(iftype));
    7763             : 
    7764          31 :         msg = nlmsg_alloc();
    7765          31 :         if (!msg)
    7766           0 :                 return -1;
    7767             : 
    7768          31 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
    7769          31 :         if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
    7770           0 :                 goto nla_put_failure;
    7771          31 :         NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
    7772          31 :         NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
    7773             : 
    7774          31 :         if (iftype == NL80211_IFTYPE_MONITOR) {
    7775             :                 struct nlattr *flags;
    7776             : 
    7777           0 :                 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
    7778           0 :                 if (!flags)
    7779           0 :                         goto nla_put_failure;
    7780             : 
    7781           0 :                 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
    7782             : 
    7783           0 :                 nla_nest_end(msg, flags);
    7784          31 :         } else if (wds) {
    7785           1 :                 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
    7786             :         }
    7787             : 
    7788             :         /*
    7789             :          * Tell cfg80211 that the interface belongs to the socket that created
    7790             :          * it, and the interface should be deleted when the socket is closed.
    7791             :          */
    7792          31 :         NLA_PUT_FLAG(msg, NL80211_ATTR_IFACE_SOCKET_OWNER);
    7793             : 
    7794          31 :         ret = send_and_recv_msgs(drv, msg, handler, arg);
    7795          31 :         msg = NULL;
    7796          31 :         if (ret) {
    7797             :  nla_put_failure:
    7798           0 :                 nlmsg_free(msg);
    7799           0 :                 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
    7800             :                            ifname, ret, strerror(-ret));
    7801           0 :                 return ret;
    7802             :         }
    7803             : 
    7804          31 :         if (iftype == NL80211_IFTYPE_P2P_DEVICE)
    7805           0 :                 return 0;
    7806             : 
    7807          31 :         ifidx = if_nametoindex(ifname);
    7808          31 :         wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
    7809             :                    ifname, ifidx);
    7810             : 
    7811          31 :         if (ifidx <= 0)
    7812           0 :                 return -1;
    7813             : 
    7814             :         /*
    7815             :          * Some virtual interfaces need to process EAPOL packets and events on
    7816             :          * the parent interface. This is used mainly with hostapd.
    7817             :          */
    7818          31 :         if (drv->hostapd ||
    7819           0 :             iftype == NL80211_IFTYPE_AP_VLAN ||
    7820           0 :             iftype == NL80211_IFTYPE_WDS ||
    7821             :             iftype == NL80211_IFTYPE_MONITOR) {
    7822             :                 /* start listening for EAPOL on this interface */
    7823          31 :                 add_ifidx(drv, ifidx);
    7824             :         }
    7825             : 
    7826          62 :         if (addr && iftype != NL80211_IFTYPE_MONITOR &&
    7827          31 :             linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
    7828           0 :                 nl80211_remove_iface(drv, ifidx);
    7829           0 :                 return -1;
    7830             :         }
    7831             : 
    7832          31 :         return ifidx;
    7833             : }
    7834             : 
    7835             : 
    7836          31 : static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
    7837             :                                 const char *ifname, enum nl80211_iftype iftype,
    7838             :                                 const u8 *addr, int wds,
    7839             :                                 int (*handler)(struct nl_msg *, void *),
    7840             :                                 void *arg, int use_existing)
    7841             : {
    7842             :         int ret;
    7843             : 
    7844          31 :         ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
    7845             :                                         arg);
    7846             : 
    7847             :         /* if error occurred and interface exists already */
    7848          31 :         if (ret == -ENFILE && if_nametoindex(ifname)) {
    7849           0 :                 if (use_existing) {
    7850           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
    7851             :                                    ifname);
    7852           0 :                         if (addr && iftype != NL80211_IFTYPE_MONITOR &&
    7853           0 :                             linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
    7854           0 :                                                addr) < 0 &&
    7855           0 :                             (linux_set_iface_flags(drv->global->ioctl_sock,
    7856           0 :                                                    ifname, 0) < 0 ||
    7857           0 :                              linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
    7858           0 :                                                 addr) < 0 ||
    7859           0 :                              linux_set_iface_flags(drv->global->ioctl_sock,
    7860             :                                                    ifname, 1) < 0))
    7861           0 :                                         return -1;
    7862           0 :                         return -ENFILE;
    7863             :                 }
    7864           0 :                 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
    7865             : 
    7866             :                 /* Try to remove the interface that was already there. */
    7867           0 :                 nl80211_remove_iface(drv, if_nametoindex(ifname));
    7868             : 
    7869             :                 /* Try to create the interface again */
    7870           0 :                 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
    7871             :                                                 wds, handler, arg);
    7872             :         }
    7873             : 
    7874          31 :         if (ret >= 0 && is_p2p_net_interface(iftype))
    7875           0 :                 nl80211_disable_11b_rates(drv, ret, 1);
    7876             : 
    7877          31 :         return ret;
    7878             : }
    7879             : 
    7880             : 
    7881           0 : static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
    7882             : {
    7883             :         struct ieee80211_hdr *hdr;
    7884             :         u16 fc;
    7885             :         union wpa_event_data event;
    7886             : 
    7887           0 :         hdr = (struct ieee80211_hdr *) buf;
    7888           0 :         fc = le_to_host16(hdr->frame_control);
    7889             : 
    7890           0 :         os_memset(&event, 0, sizeof(event));
    7891           0 :         event.tx_status.type = WLAN_FC_GET_TYPE(fc);
    7892           0 :         event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
    7893           0 :         event.tx_status.dst = hdr->addr1;
    7894           0 :         event.tx_status.data = buf;
    7895           0 :         event.tx_status.data_len = len;
    7896           0 :         event.tx_status.ack = ok;
    7897           0 :         wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
    7898           0 : }
    7899             : 
    7900             : 
    7901           0 : static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
    7902             :                              u8 *buf, size_t len)
    7903             : {
    7904           0 :         struct ieee80211_hdr *hdr = (void *)buf;
    7905             :         u16 fc;
    7906             :         union wpa_event_data event;
    7907             : 
    7908           0 :         if (len < sizeof(*hdr))
    7909           0 :                 return;
    7910             : 
    7911           0 :         fc = le_to_host16(hdr->frame_control);
    7912             : 
    7913           0 :         os_memset(&event, 0, sizeof(event));
    7914           0 :         event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
    7915           0 :         event.rx_from_unknown.addr = hdr->addr2;
    7916           0 :         event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
    7917             :                 (WLAN_FC_FROMDS | WLAN_FC_TODS);
    7918           0 :         wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
    7919             : }
    7920             : 
    7921             : 
    7922           0 : static void handle_frame(struct wpa_driver_nl80211_data *drv,
    7923             :                          u8 *buf, size_t len, int datarate, int ssi_signal)
    7924             : {
    7925             :         struct ieee80211_hdr *hdr;
    7926             :         u16 fc;
    7927             :         union wpa_event_data event;
    7928             : 
    7929           0 :         hdr = (struct ieee80211_hdr *) buf;
    7930           0 :         fc = le_to_host16(hdr->frame_control);
    7931             : 
    7932           0 :         switch (WLAN_FC_GET_TYPE(fc)) {
    7933             :         case WLAN_FC_TYPE_MGMT:
    7934           0 :                 os_memset(&event, 0, sizeof(event));
    7935           0 :                 event.rx_mgmt.frame = buf;
    7936           0 :                 event.rx_mgmt.frame_len = len;
    7937           0 :                 event.rx_mgmt.datarate = datarate;
    7938           0 :                 event.rx_mgmt.ssi_signal = ssi_signal;
    7939           0 :                 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
    7940           0 :                 break;
    7941             :         case WLAN_FC_TYPE_CTRL:
    7942             :                 /* can only get here with PS-Poll frames */
    7943           0 :                 wpa_printf(MSG_DEBUG, "CTRL");
    7944           0 :                 from_unknown_sta(drv, buf, len);
    7945           0 :                 break;
    7946             :         case WLAN_FC_TYPE_DATA:
    7947           0 :                 from_unknown_sta(drv, buf, len);
    7948           0 :                 break;
    7949             :         }
    7950           0 : }
    7951             : 
    7952             : 
    7953           0 : static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
    7954             : {
    7955           0 :         struct wpa_driver_nl80211_data *drv = eloop_ctx;
    7956             :         int len;
    7957             :         unsigned char buf[3000];
    7958             :         struct ieee80211_radiotap_iterator iter;
    7959             :         int ret;
    7960           0 :         int datarate = 0, ssi_signal = 0;
    7961           0 :         int injected = 0, failed = 0, rxflags = 0;
    7962             : 
    7963           0 :         len = recv(sock, buf, sizeof(buf), 0);
    7964           0 :         if (len < 0) {
    7965           0 :                 wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
    7966           0 :                            strerror(errno));
    7967           0 :                 return;
    7968             :         }
    7969             : 
    7970           0 :         if (ieee80211_radiotap_iterator_init(&iter, (void *) buf, len, NULL)) {
    7971           0 :                 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
    7972           0 :                 return;
    7973             :         }
    7974             : 
    7975             :         while (1) {
    7976           0 :                 ret = ieee80211_radiotap_iterator_next(&iter);
    7977           0 :                 if (ret == -ENOENT)
    7978           0 :                         break;
    7979           0 :                 if (ret) {
    7980           0 :                         wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
    7981             :                                    ret);
    7982           0 :                         return;
    7983             :                 }
    7984           0 :                 switch (iter.this_arg_index) {
    7985             :                 case IEEE80211_RADIOTAP_FLAGS:
    7986           0 :                         if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
    7987           0 :                                 len -= 4;
    7988           0 :                         break;
    7989             :                 case IEEE80211_RADIOTAP_RX_FLAGS:
    7990           0 :                         rxflags = 1;
    7991           0 :                         break;
    7992             :                 case IEEE80211_RADIOTAP_TX_FLAGS:
    7993           0 :                         injected = 1;
    7994           0 :                         failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
    7995             :                                         IEEE80211_RADIOTAP_F_TX_FAIL;
    7996           0 :                         break;
    7997             :                 case IEEE80211_RADIOTAP_DATA_RETRIES:
    7998           0 :                         break;
    7999             :                 case IEEE80211_RADIOTAP_CHANNEL:
    8000             :                         /* TODO: convert from freq/flags to channel number */
    8001           0 :                         break;
    8002             :                 case IEEE80211_RADIOTAP_RATE:
    8003           0 :                         datarate = *iter.this_arg * 5;
    8004           0 :                         break;
    8005             :                 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
    8006           0 :                         ssi_signal = (s8) *iter.this_arg;
    8007           0 :                         break;
    8008             :                 }
    8009           0 :         }
    8010             : 
    8011           0 :         if (rxflags && injected)
    8012           0 :                 return;
    8013             : 
    8014           0 :         if (!injected)
    8015           0 :                 handle_frame(drv, buf + iter._max_length,
    8016           0 :                              len - iter._max_length, datarate, ssi_signal);
    8017             :         else
    8018           0 :                 handle_tx_callback(drv->ctx, buf + iter._max_length,
    8019           0 :                                    len - iter._max_length, !failed);
    8020             : }
    8021             : 
    8022             : 
    8023             : /*
    8024             :  * we post-process the filter code later and rewrite
    8025             :  * this to the offset to the last instruction
    8026             :  */
    8027             : #define PASS    0xFF
    8028             : #define FAIL    0xFE
    8029             : 
    8030             : static struct sock_filter msock_filter_insns[] = {
    8031             :         /*
    8032             :          * do a little-endian load of the radiotap length field
    8033             :          */
    8034             :         /* load lower byte into A */
    8035             :         BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 2),
    8036             :         /* put it into X (== index register) */
    8037             :         BPF_STMT(BPF_MISC| BPF_TAX, 0),
    8038             :         /* load upper byte into A */
    8039             :         BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 3),
    8040             :         /* left-shift it by 8 */
    8041             :         BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
    8042             :         /* or with X */
    8043             :         BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
    8044             :         /* put result into X */
    8045             :         BPF_STMT(BPF_MISC| BPF_TAX, 0),
    8046             : 
    8047             :         /*
    8048             :          * Allow management frames through, this also gives us those
    8049             :          * management frames that we sent ourselves with status
    8050             :          */
    8051             :         /* load the lower byte of the IEEE 802.11 frame control field */
    8052             :         BPF_STMT(BPF_LD  | BPF_B | BPF_IND, 0),
    8053             :         /* mask off frame type and version */
    8054             :         BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
    8055             :         /* accept frame if it's both 0, fall through otherwise */
    8056             :         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
    8057             : 
    8058             :         /*
    8059             :          * TODO: add a bit to radiotap RX flags that indicates
    8060             :          * that the sending station is not associated, then
    8061             :          * add a filter here that filters on our DA and that flag
    8062             :          * to allow us to deauth frames to that bad station.
    8063             :          *
    8064             :          * For now allow all To DS data frames through.
    8065             :          */
    8066             :         /* load the IEEE 802.11 frame control field */
    8067             :         BPF_STMT(BPF_LD  | BPF_H | BPF_IND, 0),
    8068             :         /* mask off frame type, version and DS status */
    8069             :         BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
    8070             :         /* accept frame if version 0, type 2 and To DS, fall through otherwise
    8071             :          */
    8072             :         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
    8073             : 
    8074             : #if 0
    8075             :         /*
    8076             :          * drop non-data frames
    8077             :          */
    8078             :         /* load the lower byte of the frame control field */
    8079             :         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
    8080             :         /* mask off QoS bit */
    8081             :         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x0c),
    8082             :         /* drop non-data frames */
    8083             :         BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 8, 0, FAIL),
    8084             : #endif
    8085             :         /* load the upper byte of the frame control field */
    8086             :         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 1),
    8087             :         /* mask off toDS/fromDS */
    8088             :         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x03),
    8089             :         /* accept WDS frames */
    8090             :         BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 3, PASS, 0),
    8091             : 
    8092             :         /*
    8093             :          * add header length to index
    8094             :          */
    8095             :         /* load the lower byte of the frame control field */
    8096             :         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
    8097             :         /* mask off QoS bit */
    8098             :         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x80),
    8099             :         /* right shift it by 6 to give 0 or 2 */
    8100             :         BPF_STMT(BPF_ALU  | BPF_RSH | BPF_K, 6),
    8101             :         /* add data frame header length */
    8102             :         BPF_STMT(BPF_ALU  | BPF_ADD | BPF_K, 24),
    8103             :         /* add index, was start of 802.11 header */
    8104             :         BPF_STMT(BPF_ALU  | BPF_ADD | BPF_X, 0),
    8105             :         /* move to index, now start of LL header */
    8106             :         BPF_STMT(BPF_MISC | BPF_TAX, 0),
    8107             : 
    8108             :         /*
    8109             :          * Accept empty data frames, we use those for
    8110             :          * polling activity.
    8111             :          */
    8112             :         BPF_STMT(BPF_LD  | BPF_W | BPF_LEN, 0),
    8113             :         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
    8114             : 
    8115             :         /*
    8116             :          * Accept EAPOL frames
    8117             :          */
    8118             :         BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 0),
    8119             :         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
    8120             :         BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 4),
    8121             :         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
    8122             : 
    8123             :         /* keep these last two statements or change the code below */
    8124             :         /* return 0 == "DROP" */
    8125             :         BPF_STMT(BPF_RET | BPF_K, 0),
    8126             :         /* return ~0 == "keep all" */
    8127             :         BPF_STMT(BPF_RET | BPF_K, ~0),
    8128             : };
    8129             : 
    8130             : static struct sock_fprog msock_filter = {
    8131             :         .len = ARRAY_SIZE(msock_filter_insns),
    8132             :         .filter = msock_filter_insns,
    8133             : };
    8134             : 
    8135             : 
    8136           0 : static int add_monitor_filter(int s)
    8137             : {
    8138             :         int idx;
    8139             : 
    8140             :         /* rewrite all PASS/FAIL jump offsets */
    8141           0 :         for (idx = 0; idx < msock_filter.len; idx++) {
    8142           0 :                 struct sock_filter *insn = &msock_filter_insns[idx];
    8143             : 
    8144           0 :                 if (BPF_CLASS(insn->code) == BPF_JMP) {
    8145           0 :                         if (insn->code == (BPF_JMP|BPF_JA)) {
    8146           0 :                                 if (insn->k == PASS)
    8147           0 :                                         insn->k = msock_filter.len - idx - 2;
    8148           0 :                                 else if (insn->k == FAIL)
    8149           0 :                                         insn->k = msock_filter.len - idx - 3;
    8150             :                         }
    8151             : 
    8152           0 :                         if (insn->jt == PASS)
    8153           0 :                                 insn->jt = msock_filter.len - idx - 2;
    8154           0 :                         else if (insn->jt == FAIL)
    8155           0 :                                 insn->jt = msock_filter.len - idx - 3;
    8156             : 
    8157           0 :                         if (insn->jf == PASS)
    8158           0 :                                 insn->jf = msock_filter.len - idx - 2;
    8159           0 :                         else if (insn->jf == FAIL)
    8160           0 :                                 insn->jf = msock_filter.len - idx - 3;
    8161             :                 }
    8162             :         }
    8163             : 
    8164           0 :         if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
    8165             :                        &msock_filter, sizeof(msock_filter))) {
    8166           0 :                 wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
    8167           0 :                            strerror(errno));
    8168           0 :                 return -1;
    8169             :         }
    8170             : 
    8171           0 :         return 0;
    8172             : }
    8173             : 
    8174             : 
    8175         580 : static void nl80211_remove_monitor_interface(
    8176             :         struct wpa_driver_nl80211_data *drv)
    8177             : {
    8178         580 :         if (drv->monitor_refcount > 0)
    8179           0 :                 drv->monitor_refcount--;
    8180         580 :         wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
    8181             :                    drv->monitor_refcount);
    8182         580 :         if (drv->monitor_refcount > 0)
    8183         580 :                 return;
    8184             : 
    8185         580 :         if (drv->monitor_ifidx >= 0) {
    8186           0 :                 nl80211_remove_iface(drv, drv->monitor_ifidx);
    8187           0 :                 drv->monitor_ifidx = -1;
    8188             :         }
    8189         580 :         if (drv->monitor_sock >= 0) {
    8190           0 :                 eloop_unregister_read_sock(drv->monitor_sock);
    8191           0 :                 close(drv->monitor_sock);
    8192           0 :                 drv->monitor_sock = -1;
    8193             :         }
    8194             : }
    8195             : 
    8196             : 
    8197             : static int
    8198           0 : nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
    8199             : {
    8200             :         char buf[IFNAMSIZ];
    8201             :         struct sockaddr_ll ll;
    8202             :         int optval;
    8203             :         socklen_t optlen;
    8204             : 
    8205           0 :         if (drv->monitor_ifidx >= 0) {
    8206           0 :                 drv->monitor_refcount++;
    8207           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
    8208             :                            drv->monitor_refcount);
    8209           0 :                 return 0;
    8210             :         }
    8211             : 
    8212           0 :         if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
    8213             :                 /*
    8214             :                  * P2P interface name is of the format p2p-%s-%d. For monitor
    8215             :                  * interface name corresponding to P2P GO, replace "p2p-" with
    8216             :                  * "mon-" to retain the same interface name length and to
    8217             :                  * indicate that it is a monitor interface.
    8218             :                  */
    8219           0 :                 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
    8220             :         } else {
    8221             :                 /* Non-P2P interface with AP functionality. */
    8222           0 :                 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
    8223             :         }
    8224             : 
    8225           0 :         buf[IFNAMSIZ - 1] = '\0';
    8226             : 
    8227           0 :         drv->monitor_ifidx =
    8228           0 :                 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
    8229             :                                      0, NULL, NULL, 0);
    8230             : 
    8231           0 :         if (drv->monitor_ifidx == -EOPNOTSUPP) {
    8232             :                 /*
    8233             :                  * This is backward compatibility for a few versions of
    8234             :                  * the kernel only that didn't advertise the right
    8235             :                  * attributes for the only driver that then supported
    8236             :                  * AP mode w/o monitor -- ath6kl.
    8237             :                  */
    8238           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
    8239             :                            "monitor interface type - try to run without it");
    8240           0 :                 drv->device_ap_sme = 1;
    8241             :         }
    8242             : 
    8243           0 :         if (drv->monitor_ifidx < 0)
    8244           0 :                 return -1;
    8245             : 
    8246           0 :         if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
    8247           0 :                 goto error;
    8248             : 
    8249           0 :         memset(&ll, 0, sizeof(ll));
    8250           0 :         ll.sll_family = AF_PACKET;
    8251           0 :         ll.sll_ifindex = drv->monitor_ifidx;
    8252           0 :         drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    8253           0 :         if (drv->monitor_sock < 0) {
    8254           0 :                 wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
    8255           0 :                            strerror(errno));
    8256           0 :                 goto error;
    8257             :         }
    8258             : 
    8259           0 :         if (add_monitor_filter(drv->monitor_sock)) {
    8260           0 :                 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
    8261             :                            "interface; do filtering in user space");
    8262             :                 /* This works, but will cost in performance. */
    8263             :         }
    8264             : 
    8265           0 :         if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
    8266           0 :                 wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
    8267           0 :                            strerror(errno));
    8268           0 :                 goto error;
    8269             :         }
    8270             : 
    8271           0 :         optlen = sizeof(optval);
    8272           0 :         optval = 20;
    8273           0 :         if (setsockopt
    8274           0 :             (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
    8275           0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
    8276           0 :                            strerror(errno));
    8277           0 :                 goto error;
    8278             :         }
    8279             : 
    8280           0 :         if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
    8281             :                                      drv, NULL)) {
    8282           0 :                 wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
    8283           0 :                 goto error;
    8284             :         }
    8285             : 
    8286           0 :         drv->monitor_refcount++;
    8287           0 :         return 0;
    8288             :  error:
    8289           0 :         nl80211_remove_monitor_interface(drv);
    8290           0 :         return -1;
    8291             : }
    8292             : 
    8293             : 
    8294         599 : static int nl80211_setup_ap(struct i802_bss *bss)
    8295             : {
    8296         599 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8297             : 
    8298        1797 :         wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
    8299        1797 :                    bss->ifname, drv->device_ap_sme, drv->use_monitor);
    8300             : 
    8301             :         /*
    8302             :          * Disable Probe Request reporting unless we need it in this way for
    8303             :          * devices that include the AP SME, in the other case (unless using
    8304             :          * monitor iface) we'll get it through the nl_mgmt socket instead.
    8305             :          */
    8306         599 :         if (!drv->device_ap_sme)
    8307         599 :                 wpa_driver_nl80211_probe_req_report(bss, 0);
    8308             : 
    8309         599 :         if (!drv->device_ap_sme && !drv->use_monitor)
    8310         599 :                 if (nl80211_mgmt_subscribe_ap(bss))
    8311           0 :                         return -1;
    8312             : 
    8313         599 :         if (drv->device_ap_sme && !drv->use_monitor)
    8314           0 :                 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
    8315           0 :                         return -1;
    8316             : 
    8317         599 :         if (!drv->device_ap_sme && drv->use_monitor &&
    8318           0 :             nl80211_create_monitor_interface(drv) &&
    8319           0 :             !drv->device_ap_sme)
    8320           0 :                 return -1;
    8321             : 
    8322         599 :         if (drv->device_ap_sme &&
    8323           0 :             wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
    8324           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
    8325             :                            "Probe Request frame reporting in AP mode");
    8326             :                 /* Try to survive without this */
    8327             :         }
    8328             : 
    8329         599 :         return 0;
    8330             : }
    8331             : 
    8332             : 
    8333         599 : static void nl80211_teardown_ap(struct i802_bss *bss)
    8334             : {
    8335         599 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8336             : 
    8337        1797 :         wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
    8338        1797 :                    bss->ifname, drv->device_ap_sme, drv->use_monitor);
    8339         599 :         if (drv->device_ap_sme) {
    8340           0 :                 wpa_driver_nl80211_probe_req_report(bss, 0);
    8341           0 :                 if (!drv->use_monitor)
    8342           0 :                         nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
    8343         599 :         } else if (drv->use_monitor)
    8344           0 :                 nl80211_remove_monitor_interface(drv);
    8345             :         else
    8346         599 :                 nl80211_mgmt_unsubscribe(bss, "AP teardown");
    8347             : 
    8348         599 :         bss->beacon_set = 0;
    8349         599 : }
    8350             : 
    8351             : 
    8352        3806 : static int nl80211_send_eapol_data(struct i802_bss *bss,
    8353             :                                    const u8 *addr, const u8 *data,
    8354             :                                    size_t data_len)
    8355             : {
    8356             :         struct sockaddr_ll ll;
    8357             :         int ret;
    8358             : 
    8359        3806 :         if (bss->drv->eapol_tx_sock < 0) {
    8360           0 :                 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
    8361           0 :                 return -1;
    8362             :         }
    8363             : 
    8364        3806 :         os_memset(&ll, 0, sizeof(ll));
    8365        3806 :         ll.sll_family = AF_PACKET;
    8366        3806 :         ll.sll_ifindex = bss->ifindex;
    8367        3806 :         ll.sll_protocol = htons(ETH_P_PAE);
    8368        3806 :         ll.sll_halen = ETH_ALEN;
    8369        3806 :         os_memcpy(ll.sll_addr, addr, ETH_ALEN);
    8370        3806 :         ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
    8371             :                      (struct sockaddr *) &ll, sizeof(ll));
    8372        3806 :         if (ret < 0)
    8373           0 :                 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
    8374           0 :                            strerror(errno));
    8375             : 
    8376        3806 :         return ret;
    8377             : }
    8378             : 
    8379             : 
    8380             : static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
    8381             : 
    8382        3806 : static int wpa_driver_nl80211_hapd_send_eapol(
    8383             :         void *priv, const u8 *addr, const u8 *data,
    8384             :         size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
    8385             : {
    8386        3806 :         struct i802_bss *bss = priv;
    8387        3806 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8388             :         struct ieee80211_hdr *hdr;
    8389             :         size_t len;
    8390             :         u8 *pos;
    8391             :         int res;
    8392        3806 :         int qos = flags & WPA_STA_WMM;
    8393             : 
    8394        3806 :         if (drv->device_ap_sme || !drv->use_monitor)
    8395        3806 :                 return nl80211_send_eapol_data(bss, addr, data, data_len);
    8396             : 
    8397           0 :         len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
    8398             :                 data_len;
    8399           0 :         hdr = os_zalloc(len);
    8400           0 :         if (hdr == NULL) {
    8401           0 :                 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
    8402             :                            (unsigned long) len);
    8403           0 :                 return -1;
    8404             :         }
    8405             : 
    8406           0 :         hdr->frame_control =
    8407             :                 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
    8408           0 :         hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
    8409           0 :         if (encrypt)
    8410           0 :                 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
    8411           0 :         if (qos) {
    8412           0 :                 hdr->frame_control |=
    8413             :                         host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
    8414             :         }
    8415             : 
    8416           0 :         memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
    8417           0 :         memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
    8418           0 :         memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
    8419           0 :         pos = (u8 *) (hdr + 1);
    8420             : 
    8421           0 :         if (qos) {
    8422             :                 /* Set highest priority in QoS header */
    8423           0 :                 pos[0] = 7;
    8424           0 :                 pos[1] = 0;
    8425           0 :                 pos += 2;
    8426             :         }
    8427             : 
    8428           0 :         memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
    8429           0 :         pos += sizeof(rfc1042_header);
    8430           0 :         WPA_PUT_BE16(pos, ETH_P_PAE);
    8431           0 :         pos += 2;
    8432           0 :         memcpy(pos, data, data_len);
    8433             : 
    8434           0 :         res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
    8435             :                                             0, 0, 0, 0);
    8436           0 :         if (res < 0) {
    8437           0 :                 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
    8438             :                            "failed: %d (%s)",
    8439           0 :                            (unsigned long) len, errno, strerror(errno));
    8440             :         }
    8441           0 :         os_free(hdr);
    8442             : 
    8443           0 :         return res;
    8444             : }
    8445             : 
    8446             : 
    8447        2428 : static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
    8448             :                                             int total_flags,
    8449             :                                             int flags_or, int flags_and)
    8450             : {
    8451        2428 :         struct i802_bss *bss = priv;
    8452        2428 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8453             :         struct nl_msg *msg;
    8454             :         struct nlattr *flags;
    8455             :         struct nl80211_sta_flag_update upd;
    8456             : 
    8457        2428 :         msg = nlmsg_alloc();
    8458        2428 :         if (!msg)
    8459           0 :                 return -ENOMEM;
    8460             : 
    8461        2428 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
    8462             : 
    8463        2428 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
    8464             :                     if_nametoindex(bss->ifname));
    8465        2428 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    8466             : 
    8467             :         /*
    8468             :          * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
    8469             :          * can be removed eventually.
    8470             :          */
    8471        2428 :         flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
    8472        2428 :         if (!flags)
    8473           0 :                 goto nla_put_failure;
    8474        2428 :         if (total_flags & WPA_STA_AUTHORIZED)
    8475         922 :                 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
    8476             : 
    8477        2428 :         if (total_flags & WPA_STA_WMM)
    8478        2414 :                 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
    8479             : 
    8480        2428 :         if (total_flags & WPA_STA_SHORT_PREAMBLE)
    8481        2403 :                 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
    8482             : 
    8483        2428 :         if (total_flags & WPA_STA_MFP)
    8484          98 :                 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
    8485             : 
    8486        2428 :         if (total_flags & WPA_STA_TDLS_PEER)
    8487           0 :                 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
    8488             : 
    8489        2428 :         nla_nest_end(msg, flags);
    8490             : 
    8491        2428 :         os_memset(&upd, 0, sizeof(upd));
    8492        2428 :         upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
    8493        2428 :         upd.set = sta_flags_nl80211(flags_or);
    8494        2428 :         NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
    8495             : 
    8496        2428 :         return send_and_recv_msgs(drv, msg, NULL, NULL);
    8497             :  nla_put_failure:
    8498           0 :         nlmsg_free(msg);
    8499           0 :         return -ENOBUFS;
    8500             : }
    8501             : 
    8502             : 
    8503           0 : static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
    8504             :                                  struct wpa_driver_associate_params *params)
    8505             : {
    8506             :         enum nl80211_iftype nlmode, old_mode;
    8507           0 :         struct hostapd_freq_params freq = {
    8508           0 :                 .freq = params->freq,
    8509             :         };
    8510             : 
    8511           0 :         if (params->p2p) {
    8512           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
    8513             :                            "group (GO)");
    8514           0 :                 nlmode = NL80211_IFTYPE_P2P_GO;
    8515             :         } else
    8516           0 :                 nlmode = NL80211_IFTYPE_AP;
    8517             : 
    8518           0 :         old_mode = drv->nlmode;
    8519           0 :         if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
    8520           0 :                 nl80211_remove_monitor_interface(drv);
    8521           0 :                 return -1;
    8522             :         }
    8523             : 
    8524           0 :         if (nl80211_set_channel(drv->first_bss, &freq, 0)) {
    8525           0 :                 if (old_mode != nlmode)
    8526           0 :                         wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
    8527           0 :                 nl80211_remove_monitor_interface(drv);
    8528           0 :                 return -1;
    8529             :         }
    8530             : 
    8531           0 :         return 0;
    8532             : }
    8533             : 
    8534             : 
    8535           0 : static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
    8536             : {
    8537             :         struct nl_msg *msg;
    8538           0 :         int ret = -1;
    8539             : 
    8540           0 :         msg = nlmsg_alloc();
    8541           0 :         if (!msg)
    8542           0 :                 return -1;
    8543             : 
    8544           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
    8545           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    8546           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    8547           0 :         msg = NULL;
    8548           0 :         if (ret) {
    8549           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
    8550             :                            "(%s)", ret, strerror(-ret));
    8551           0 :                 goto nla_put_failure;
    8552             :         }
    8553             : 
    8554           0 :         ret = 0;
    8555           0 :         wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
    8556             : 
    8557             : nla_put_failure:
    8558           0 :         if (wpa_driver_nl80211_set_mode(drv->first_bss,
    8559             :                                         NL80211_IFTYPE_STATION)) {
    8560           0 :                 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
    8561             :                            "station mode");
    8562             :         }
    8563             : 
    8564           0 :         nlmsg_free(msg);
    8565           0 :         return ret;
    8566             : }
    8567             : 
    8568             : 
    8569           0 : static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
    8570             :                                    struct wpa_driver_associate_params *params)
    8571             : {
    8572             :         struct nl_msg *msg;
    8573           0 :         int ret = -1;
    8574           0 :         int count = 0;
    8575             : 
    8576           0 :         wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
    8577             : 
    8578           0 :         if (wpa_driver_nl80211_set_mode(drv->first_bss,
    8579             :                                         NL80211_IFTYPE_ADHOC)) {
    8580           0 :                 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
    8581             :                            "IBSS mode");
    8582           0 :                 return -1;
    8583             :         }
    8584             : 
    8585             : retry:
    8586           0 :         msg = nlmsg_alloc();
    8587           0 :         if (!msg)
    8588           0 :                 return -1;
    8589             : 
    8590           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
    8591           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    8592             : 
    8593           0 :         if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
    8594             :                 goto nla_put_failure;
    8595             : 
    8596           0 :         wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
    8597           0 :                           params->ssid, params->ssid_len);
    8598           0 :         NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
    8599             :                 params->ssid);
    8600           0 :         os_memcpy(drv->ssid, params->ssid, params->ssid_len);
    8601           0 :         drv->ssid_len = params->ssid_len;
    8602             : 
    8603           0 :         wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
    8604           0 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
    8605             : 
    8606           0 :         if (params->beacon_int > 0) {
    8607           0 :                 wpa_printf(MSG_DEBUG, "  * beacon_int=%d", params->beacon_int);
    8608           0 :                 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL,
    8609             :                             params->beacon_int);
    8610             :         }
    8611             : 
    8612           0 :         ret = nl80211_set_conn_keys(params, msg);
    8613           0 :         if (ret)
    8614           0 :                 goto nla_put_failure;
    8615             : 
    8616           0 :         if (params->bssid && params->fixed_bssid) {
    8617           0 :                 wpa_printf(MSG_DEBUG, "  * BSSID=" MACSTR,
    8618           0 :                            MAC2STR(params->bssid));
    8619           0 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
    8620             :         }
    8621             : 
    8622           0 :         if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
    8623           0 :             params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
    8624           0 :             params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
    8625           0 :             params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
    8626           0 :                 wpa_printf(MSG_DEBUG, "  * control port");
    8627           0 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
    8628             :         }
    8629             : 
    8630           0 :         if (params->wpa_ie) {
    8631           0 :                 wpa_hexdump(MSG_DEBUG,
    8632             :                             "  * Extra IEs for Beacon/Probe Response frames",
    8633           0 :                             params->wpa_ie, params->wpa_ie_len);
    8634           0 :                 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
    8635             :                         params->wpa_ie);
    8636             :         }
    8637             : 
    8638           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    8639           0 :         msg = NULL;
    8640           0 :         if (ret) {
    8641           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
    8642             :                            ret, strerror(-ret));
    8643           0 :                 count++;
    8644           0 :                 if (ret == -EALREADY && count == 1) {
    8645           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
    8646             :                                    "forced leave");
    8647           0 :                         nl80211_leave_ibss(drv);
    8648           0 :                         nlmsg_free(msg);
    8649           0 :                         goto retry;
    8650             :                 }
    8651             : 
    8652           0 :                 goto nla_put_failure;
    8653             :         }
    8654           0 :         ret = 0;
    8655           0 :         wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
    8656             : 
    8657             : nla_put_failure:
    8658           0 :         nlmsg_free(msg);
    8659           0 :         return ret;
    8660             : }
    8661             : 
    8662             : 
    8663           0 : static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
    8664             :                                   struct wpa_driver_associate_params *params,
    8665             :                                   struct nl_msg *msg)
    8666             : {
    8667           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    8668             : 
    8669           0 :         if (params->bssid) {
    8670           0 :                 wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
    8671           0 :                            MAC2STR(params->bssid));
    8672           0 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
    8673             :         }
    8674             : 
    8675           0 :         if (params->bssid_hint) {
    8676           0 :                 wpa_printf(MSG_DEBUG, "  * bssid_hint=" MACSTR,
    8677           0 :                            MAC2STR(params->bssid_hint));
    8678           0 :                 NLA_PUT(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
    8679             :                         params->bssid_hint);
    8680             :         }
    8681             : 
    8682           0 :         if (params->freq) {
    8683           0 :                 wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
    8684           0 :                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
    8685           0 :                 drv->assoc_freq = params->freq;
    8686             :         } else
    8687           0 :                 drv->assoc_freq = 0;
    8688             : 
    8689           0 :         if (params->freq_hint) {
    8690           0 :                 wpa_printf(MSG_DEBUG, "  * freq_hint=%d", params->freq_hint);
    8691           0 :                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
    8692             :                             params->freq_hint);
    8693             :         }
    8694             : 
    8695           0 :         if (params->bg_scan_period >= 0) {
    8696           0 :                 wpa_printf(MSG_DEBUG, "  * bg scan period=%d",
    8697             :                            params->bg_scan_period);
    8698           0 :                 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
    8699             :                             params->bg_scan_period);
    8700             :         }
    8701             : 
    8702           0 :         if (params->ssid) {
    8703           0 :                 wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
    8704           0 :                                   params->ssid, params->ssid_len);
    8705           0 :                 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
    8706             :                         params->ssid);
    8707           0 :                 if (params->ssid_len > sizeof(drv->ssid))
    8708           0 :                         goto nla_put_failure;
    8709           0 :                 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
    8710           0 :                 drv->ssid_len = params->ssid_len;
    8711             :         }
    8712             : 
    8713           0 :         wpa_hexdump(MSG_DEBUG, "  * IEs", params->wpa_ie, params->wpa_ie_len);
    8714           0 :         if (params->wpa_ie)
    8715           0 :                 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
    8716             :                         params->wpa_ie);
    8717             : 
    8718           0 :         if (params->wpa_proto) {
    8719           0 :                 enum nl80211_wpa_versions ver = 0;
    8720             : 
    8721           0 :                 if (params->wpa_proto & WPA_PROTO_WPA)
    8722           0 :                         ver |= NL80211_WPA_VERSION_1;
    8723           0 :                 if (params->wpa_proto & WPA_PROTO_RSN)
    8724           0 :                         ver |= NL80211_WPA_VERSION_2;
    8725             : 
    8726           0 :                 wpa_printf(MSG_DEBUG, "  * WPA Versions 0x%x", ver);
    8727           0 :                 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
    8728             :         }
    8729             : 
    8730           0 :         if (params->pairwise_suite != WPA_CIPHER_NONE) {
    8731           0 :                 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
    8732           0 :                 wpa_printf(MSG_DEBUG, "  * pairwise=0x%x", cipher);
    8733           0 :                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
    8734             :         }
    8735             : 
    8736           0 :         if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
    8737           0 :             !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
    8738             :                 /*
    8739             :                  * This is likely to work even though many drivers do not
    8740             :                  * advertise support for operations without GTK.
    8741             :                  */
    8742           0 :                 wpa_printf(MSG_DEBUG, "  * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
    8743           0 :         } else if (params->group_suite != WPA_CIPHER_NONE) {
    8744           0 :                 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
    8745           0 :                 wpa_printf(MSG_DEBUG, "  * group=0x%x", cipher);
    8746           0 :                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
    8747             :         }
    8748             : 
    8749           0 :         if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
    8750           0 :             params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
    8751           0 :             params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
    8752           0 :             params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
    8753           0 :             params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
    8754           0 :             params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
    8755           0 :             params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
    8756           0 :             params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
    8757           0 :                 int mgmt = WLAN_AKM_SUITE_PSK;
    8758             : 
    8759           0 :                 switch (params->key_mgmt_suite) {
    8760             :                 case WPA_KEY_MGMT_CCKM:
    8761           0 :                         mgmt = WLAN_AKM_SUITE_CCKM;
    8762           0 :                         break;
    8763             :                 case WPA_KEY_MGMT_IEEE8021X:
    8764           0 :                         mgmt = WLAN_AKM_SUITE_8021X;
    8765           0 :                         break;
    8766             :                 case WPA_KEY_MGMT_FT_IEEE8021X:
    8767           0 :                         mgmt = WLAN_AKM_SUITE_FT_8021X;
    8768           0 :                         break;
    8769             :                 case WPA_KEY_MGMT_FT_PSK:
    8770           0 :                         mgmt = WLAN_AKM_SUITE_FT_PSK;
    8771           0 :                         break;
    8772             :                 case WPA_KEY_MGMT_IEEE8021X_SHA256:
    8773           0 :                         mgmt = WLAN_AKM_SUITE_8021X_SHA256;
    8774           0 :                         break;
    8775             :                 case WPA_KEY_MGMT_PSK_SHA256:
    8776           0 :                         mgmt = WLAN_AKM_SUITE_PSK_SHA256;
    8777           0 :                         break;
    8778             :                 case WPA_KEY_MGMT_OSEN:
    8779           0 :                         mgmt = WLAN_AKM_SUITE_OSEN;
    8780           0 :                         break;
    8781             :                 case WPA_KEY_MGMT_PSK:
    8782             :                 default:
    8783           0 :                         mgmt = WLAN_AKM_SUITE_PSK;
    8784           0 :                         break;
    8785             :                 }
    8786           0 :                 wpa_printf(MSG_DEBUG, "  * akm=0x%x", mgmt);
    8787           0 :                 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
    8788             :         }
    8789             : 
    8790           0 :         NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
    8791             : 
    8792           0 :         if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
    8793           0 :                 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
    8794             : 
    8795           0 :         if (params->disable_ht)
    8796           0 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
    8797             : 
    8798           0 :         if (params->htcaps && params->htcaps_mask) {
    8799           0 :                 int sz = sizeof(struct ieee80211_ht_capabilities);
    8800           0 :                 wpa_hexdump(MSG_DEBUG, "  * htcaps", params->htcaps, sz);
    8801           0 :                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
    8802           0 :                 wpa_hexdump(MSG_DEBUG, "  * htcaps_mask",
    8803           0 :                             params->htcaps_mask, sz);
    8804           0 :                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
    8805             :                         params->htcaps_mask);
    8806             :         }
    8807             : 
    8808             : #ifdef CONFIG_VHT_OVERRIDES
    8809             :         if (params->disable_vht) {
    8810             :                 wpa_printf(MSG_DEBUG, "  * VHT disabled");
    8811             :                 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
    8812             :         }
    8813             : 
    8814             :         if (params->vhtcaps && params->vhtcaps_mask) {
    8815             :                 int sz = sizeof(struct ieee80211_vht_capabilities);
    8816             :                 wpa_hexdump(MSG_DEBUG, "  * vhtcaps", params->vhtcaps, sz);
    8817             :                 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
    8818             :                 wpa_hexdump(MSG_DEBUG, "  * vhtcaps_mask",
    8819             :                             params->vhtcaps_mask, sz);
    8820             :                 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
    8821             :                         params->vhtcaps_mask);
    8822             :         }
    8823             : #endif /* CONFIG_VHT_OVERRIDES */
    8824             : 
    8825           0 :         if (params->p2p)
    8826           0 :                 wpa_printf(MSG_DEBUG, "  * P2P group");
    8827             : 
    8828           0 :         return 0;
    8829             : nla_put_failure:
    8830           0 :         return -1;
    8831             : }
    8832             : 
    8833             : 
    8834           0 : static int wpa_driver_nl80211_try_connect(
    8835             :         struct wpa_driver_nl80211_data *drv,
    8836             :         struct wpa_driver_associate_params *params)
    8837             : {
    8838             :         struct nl_msg *msg;
    8839             :         enum nl80211_auth_type type;
    8840             :         int ret;
    8841             :         int algs;
    8842             : 
    8843           0 :         msg = nlmsg_alloc();
    8844           0 :         if (!msg)
    8845           0 :                 return -1;
    8846             : 
    8847           0 :         wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
    8848           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
    8849             : 
    8850           0 :         ret = nl80211_connect_common(drv, params, msg);
    8851           0 :         if (ret)
    8852           0 :                 goto nla_put_failure;
    8853             : 
    8854           0 :         algs = 0;
    8855           0 :         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
    8856           0 :                 algs++;
    8857           0 :         if (params->auth_alg & WPA_AUTH_ALG_SHARED)
    8858           0 :                 algs++;
    8859           0 :         if (params->auth_alg & WPA_AUTH_ALG_LEAP)
    8860           0 :                 algs++;
    8861           0 :         if (algs > 1) {
    8862           0 :                 wpa_printf(MSG_DEBUG, "  * Leave out Auth Type for automatic "
    8863             :                            "selection");
    8864           0 :                 goto skip_auth_type;
    8865             :         }
    8866             : 
    8867           0 :         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
    8868           0 :                 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
    8869           0 :         else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
    8870           0 :                 type = NL80211_AUTHTYPE_SHARED_KEY;
    8871           0 :         else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
    8872           0 :                 type = NL80211_AUTHTYPE_NETWORK_EAP;
    8873           0 :         else if (params->auth_alg & WPA_AUTH_ALG_FT)
    8874           0 :                 type = NL80211_AUTHTYPE_FT;
    8875             :         else
    8876           0 :                 goto nla_put_failure;
    8877             : 
    8878           0 :         wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
    8879           0 :         NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
    8880             : 
    8881             : skip_auth_type:
    8882           0 :         ret = nl80211_set_conn_keys(params, msg);
    8883           0 :         if (ret)
    8884           0 :                 goto nla_put_failure;
    8885             : 
    8886           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    8887           0 :         msg = NULL;
    8888           0 :         if (ret) {
    8889           0 :                 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
    8890             :                            "(%s)", ret, strerror(-ret));
    8891           0 :                 goto nla_put_failure;
    8892             :         }
    8893           0 :         ret = 0;
    8894           0 :         wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
    8895             : 
    8896             : nla_put_failure:
    8897           0 :         nlmsg_free(msg);
    8898           0 :         return ret;
    8899             : 
    8900             : }
    8901             : 
    8902             : 
    8903           0 : static int wpa_driver_nl80211_connect(
    8904             :         struct wpa_driver_nl80211_data *drv,
    8905             :         struct wpa_driver_associate_params *params)
    8906             : {
    8907           0 :         int ret = wpa_driver_nl80211_try_connect(drv, params);
    8908           0 :         if (ret == -EALREADY) {
    8909             :                 /*
    8910             :                  * cfg80211 does not currently accept new connections if
    8911             :                  * we are already connected. As a workaround, force
    8912             :                  * disconnection and try again.
    8913             :                  */
    8914           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
    8915             :                            "disconnecting before reassociation "
    8916             :                            "attempt");
    8917           0 :                 if (wpa_driver_nl80211_disconnect(
    8918             :                             drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
    8919           0 :                         return -1;
    8920           0 :                 ret = wpa_driver_nl80211_try_connect(drv, params);
    8921             :         }
    8922           0 :         return ret;
    8923             : }
    8924             : 
    8925             : 
    8926           0 : static int wpa_driver_nl80211_associate(
    8927             :         void *priv, struct wpa_driver_associate_params *params)
    8928             : {
    8929           0 :         struct i802_bss *bss = priv;
    8930           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8931             :         int ret;
    8932             :         struct nl_msg *msg;
    8933             : 
    8934           0 :         if (params->mode == IEEE80211_MODE_AP)
    8935           0 :                 return wpa_driver_nl80211_ap(drv, params);
    8936             : 
    8937           0 :         if (params->mode == IEEE80211_MODE_IBSS)
    8938           0 :                 return wpa_driver_nl80211_ibss(drv, params);
    8939             : 
    8940           0 :         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
    8941           0 :                 enum nl80211_iftype nlmode = params->p2p ?
    8942             :                         NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
    8943             : 
    8944           0 :                 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
    8945           0 :                         return -1;
    8946           0 :                 return wpa_driver_nl80211_connect(drv, params);
    8947             :         }
    8948             : 
    8949           0 :         nl80211_mark_disconnected(drv);
    8950             : 
    8951           0 :         msg = nlmsg_alloc();
    8952           0 :         if (!msg)
    8953           0 :                 return -1;
    8954             : 
    8955           0 :         wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
    8956             :                    drv->ifindex);
    8957           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
    8958             : 
    8959           0 :         ret = nl80211_connect_common(drv, params, msg);
    8960           0 :         if (ret)
    8961           0 :                 goto nla_put_failure;
    8962             : 
    8963           0 :         if (params->prev_bssid) {
    8964           0 :                 wpa_printf(MSG_DEBUG, "  * prev_bssid=" MACSTR,
    8965           0 :                            MAC2STR(params->prev_bssid));
    8966           0 :                 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
    8967             :                         params->prev_bssid);
    8968             :         }
    8969             : 
    8970           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    8971           0 :         msg = NULL;
    8972           0 :         if (ret) {
    8973           0 :                 wpa_dbg(drv->ctx, MSG_DEBUG,
    8974             :                         "nl80211: MLME command failed (assoc): ret=%d (%s)",
    8975             :                         ret, strerror(-ret));
    8976           0 :                 nl80211_dump_scan(drv);
    8977           0 :                 goto nla_put_failure;
    8978             :         }
    8979           0 :         ret = 0;
    8980           0 :         wpa_printf(MSG_DEBUG, "nl80211: Association request send "
    8981             :                    "successfully");
    8982             : 
    8983             : nla_put_failure:
    8984           0 :         nlmsg_free(msg);
    8985           0 :         return ret;
    8986             : }
    8987             : 
    8988             : 
    8989        1160 : static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
    8990             :                             int ifindex, enum nl80211_iftype mode)
    8991             : {
    8992             :         struct nl_msg *msg;
    8993        1160 :         int ret = -ENOBUFS;
    8994             : 
    8995        1160 :         wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
    8996             :                    ifindex, mode, nl80211_iftype_str(mode));
    8997             : 
    8998        1160 :         msg = nlmsg_alloc();
    8999        1160 :         if (!msg)
    9000           0 :                 return -ENOMEM;
    9001             : 
    9002        1160 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
    9003        1160 :         if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
    9004           0 :                 goto nla_put_failure;
    9005        1160 :         NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
    9006             : 
    9007        1160 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    9008        1160 :         msg = NULL;
    9009        1160 :         if (!ret)
    9010        1160 :                 return 0;
    9011             : nla_put_failure:
    9012           0 :         nlmsg_free(msg);
    9013           0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
    9014             :                    " %d (%s)", ifindex, mode, ret, strerror(-ret));
    9015           0 :         return ret;
    9016             : }
    9017             : 
    9018             : 
    9019        1160 : static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
    9020             :                                        enum nl80211_iftype nlmode)
    9021             : {
    9022        1160 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9023        1160 :         int ret = -1;
    9024             :         int i;
    9025        1160 :         int was_ap = is_ap_interface(drv->nlmode);
    9026             :         int res;
    9027             : 
    9028        1160 :         res = nl80211_set_mode(drv, drv->ifindex, nlmode);
    9029        1160 :         if (res && nlmode == nl80211_get_ifmode(bss))
    9030           0 :                 res = 0;
    9031             : 
    9032        1160 :         if (res == 0) {
    9033        1160 :                 drv->nlmode = nlmode;
    9034        1160 :                 ret = 0;
    9035        1160 :                 goto done;
    9036             :         }
    9037             : 
    9038           0 :         if (res == -ENODEV)
    9039           0 :                 return -1;
    9040             : 
    9041           0 :         if (nlmode == drv->nlmode) {
    9042           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
    9043             :                            "requested mode - ignore error");
    9044           0 :                 ret = 0;
    9045           0 :                 goto done; /* Already in the requested mode */
    9046             :         }
    9047             : 
    9048             :         /* mac80211 doesn't allow mode changes while the device is up, so
    9049             :          * take the device down, try to set the mode again, and bring the
    9050             :          * device back up.
    9051             :          */
    9052           0 :         wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
    9053             :                    "interface down");
    9054           0 :         for (i = 0; i < 10; i++) {
    9055           0 :                 res = i802_set_iface_flags(bss, 0);
    9056           0 :                 if (res == -EACCES || res == -ENODEV)
    9057             :                         break;
    9058           0 :                 if (res == 0) {
    9059             :                         /* Try to set the mode again while the interface is
    9060             :                          * down */
    9061           0 :                         ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
    9062           0 :                         if (ret == -EACCES)
    9063           0 :                                 break;
    9064           0 :                         res = i802_set_iface_flags(bss, 1);
    9065           0 :                         if (res && !ret)
    9066           0 :                                 ret = -1;
    9067           0 :                         else if (ret != -EBUSY)
    9068           0 :                                 break;
    9069             :                 } else
    9070           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
    9071             :                                    "interface down");
    9072           0 :                 os_sleep(0, 100000);
    9073             :         }
    9074             : 
    9075           0 :         if (!ret) {
    9076           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
    9077             :                            "interface is down");
    9078           0 :                 drv->nlmode = nlmode;
    9079           0 :                 drv->ignore_if_down_event = 1;
    9080             :         }
    9081             : 
    9082             : done:
    9083        1160 :         if (ret) {
    9084           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
    9085           0 :                            "from %d failed", nlmode, drv->nlmode);
    9086           0 :                 return ret;
    9087             :         }
    9088             : 
    9089        1160 :         if (is_p2p_net_interface(nlmode))
    9090           0 :                 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
    9091        1160 :         else if (drv->disabled_11b_rates)
    9092           0 :                 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
    9093             : 
    9094        1160 :         if (is_ap_interface(nlmode)) {
    9095         580 :                 nl80211_mgmt_unsubscribe(bss, "start AP");
    9096             :                 /* Setup additional AP mode functionality if needed */
    9097         580 :                 if (nl80211_setup_ap(bss))
    9098           0 :                         return -1;
    9099         580 :         } else if (was_ap) {
    9100             :                 /* Remove additional AP mode functionality */
    9101         580 :                 nl80211_teardown_ap(bss);
    9102             :         } else {
    9103           0 :                 nl80211_mgmt_unsubscribe(bss, "mode change");
    9104             :         }
    9105             : 
    9106        1160 :         if (!bss->in_deinit && !is_ap_interface(nlmode) &&
    9107           0 :             nl80211_mgmt_subscribe_non_ap(bss) < 0)
    9108           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
    9109             :                            "frame processing - ignore for now");
    9110             : 
    9111        1160 :         return 0;
    9112             : }
    9113             : 
    9114             : 
    9115           0 : static int dfs_info_handler(struct nl_msg *msg, void *arg)
    9116             : {
    9117             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    9118           0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    9119           0 :         int *dfs_capability_ptr = arg;
    9120             : 
    9121           0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    9122             :                   genlmsg_attrlen(gnlh, 0), NULL);
    9123             : 
    9124           0 :         if (tb[NL80211_ATTR_VENDOR_DATA]) {
    9125           0 :                 struct nlattr *nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
    9126             :                 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
    9127             : 
    9128           0 :                 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
    9129           0 :                           nla_data(nl_vend), nla_len(nl_vend), NULL);
    9130             : 
    9131           0 :                 if (tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]) {
    9132             :                         u32 val;
    9133           0 :                         val = nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]);
    9134           0 :                         wpa_printf(MSG_DEBUG, "nl80211: DFS offload capability: %u",
    9135             :                                    val);
    9136           0 :                         *dfs_capability_ptr = val;
    9137             :                 }
    9138             :         }
    9139             : 
    9140           0 :         return NL_SKIP;
    9141             : }
    9142             : 
    9143             : 
    9144         580 : static int wpa_driver_nl80211_get_capa(void *priv,
    9145             :                                        struct wpa_driver_capa *capa)
    9146             : {
    9147         580 :         struct i802_bss *bss = priv;
    9148         580 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9149             :         struct nl_msg *msg;
    9150         580 :         int dfs_capability = 0;
    9151         580 :         int ret = 0;
    9152             : 
    9153         580 :         if (!drv->has_capability)
    9154           0 :                 return -1;
    9155         580 :         os_memcpy(capa, &drv->capa, sizeof(*capa));
    9156         580 :         if (drv->extended_capa && drv->extended_capa_mask) {
    9157         580 :                 capa->extended_capa = drv->extended_capa;
    9158         580 :                 capa->extended_capa_mask = drv->extended_capa_mask;
    9159         580 :                 capa->extended_capa_len = drv->extended_capa_len;
    9160             :         }
    9161             : 
    9162        1160 :         if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
    9163         580 :             !drv->allow_p2p_device) {
    9164         580 :                 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
    9165         580 :                 capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
    9166             :         }
    9167             : 
    9168         580 :         if (drv->dfs_vendor_cmd_avail == 1) {
    9169           0 :                 msg = nlmsg_alloc();
    9170           0 :                 if (!msg)
    9171           0 :                         return -ENOMEM;
    9172             : 
    9173           0 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
    9174             : 
    9175           0 :                 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    9176           0 :                 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA);
    9177           0 :                 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD,
    9178             :                             QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY);
    9179             : 
    9180           0 :                 ret = send_and_recv_msgs(drv, msg, dfs_info_handler,
    9181             :                                          &dfs_capability);
    9182           0 :                 if (!ret) {
    9183           0 :                         if (dfs_capability)
    9184           0 :                                 capa->flags |= WPA_DRIVER_FLAGS_DFS_OFFLOAD;
    9185             :                 }
    9186             :         }
    9187             : 
    9188         580 :         return ret;
    9189             : 
    9190             :  nla_put_failure:
    9191           0 :         nlmsg_free(msg);
    9192           0 :         return -ENOBUFS;
    9193             : }
    9194             : 
    9195             : 
    9196         582 : static int wpa_driver_nl80211_set_operstate(void *priv, int state)
    9197             : {
    9198         582 :         struct i802_bss *bss = priv;
    9199         582 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9200             : 
    9201        1164 :         wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
    9202         582 :                    bss->ifname, drv->operstate, state,
    9203             :                    state ? "UP" : "DORMANT");
    9204         582 :         drv->operstate = state;
    9205         582 :         return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
    9206             :                                       state ? IF_OPER_UP : IF_OPER_DORMANT);
    9207             : }
    9208             : 
    9209             : 
    9210           0 : static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
    9211             : {
    9212           0 :         struct i802_bss *bss = priv;
    9213           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9214             :         struct nl_msg *msg;
    9215             :         struct nl80211_sta_flag_update upd;
    9216           0 :         int ret = -ENOBUFS;
    9217             : 
    9218           0 :         if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
    9219           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
    9220           0 :                 return 0;
    9221             :         }
    9222             : 
    9223           0 :         wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
    9224           0 :                    MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
    9225             : 
    9226           0 :         msg = nlmsg_alloc();
    9227           0 :         if (!msg)
    9228           0 :                 return -ENOMEM;
    9229             : 
    9230           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
    9231             : 
    9232           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
    9233             :                     if_nametoindex(bss->ifname));
    9234           0 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
    9235             : 
    9236           0 :         os_memset(&upd, 0, sizeof(upd));
    9237           0 :         upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
    9238           0 :         if (authorized)
    9239           0 :                 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
    9240           0 :         NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
    9241             : 
    9242           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    9243           0 :         msg = NULL;
    9244           0 :         if (!ret)
    9245           0 :                 return 0;
    9246             :  nla_put_failure:
    9247           0 :         nlmsg_free(msg);
    9248           0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
    9249             :                    ret, strerror(-ret));
    9250           0 :         return ret;
    9251             : }
    9252             : 
    9253             : 
    9254             : /* Set kernel driver on given frequency (MHz) */
    9255         567 : static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
    9256             : {
    9257         567 :         struct i802_bss *bss = priv;
    9258         567 :         return nl80211_set_channel(bss, freq, 0);
    9259             : }
    9260             : 
    9261             : 
    9262         783 : static inline int min_int(int a, int b)
    9263             : {
    9264         783 :         if (a < b)
    9265           0 :                 return a;
    9266         783 :         return b;
    9267             : }
    9268             : 
    9269             : 
    9270         783 : static int get_key_handler(struct nl_msg *msg, void *arg)
    9271             : {
    9272             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    9273         783 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    9274             : 
    9275         783 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    9276             :                   genlmsg_attrlen(gnlh, 0), NULL);
    9277             : 
    9278             :         /*
    9279             :          * TODO: validate the key index and mac address!
    9280             :          * Otherwise, there's a race condition as soon as
    9281             :          * the kernel starts sending key notifications.
    9282             :          */
    9283             : 
    9284         783 :         if (tb[NL80211_ATTR_KEY_SEQ])
    9285         783 :                 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
    9286         783 :                        min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
    9287         783 :         return NL_SKIP;
    9288             : }
    9289             : 
    9290             : 
    9291         783 : static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
    9292             :                            int idx, u8 *seq)
    9293             : {
    9294         783 :         struct i802_bss *bss = priv;
    9295         783 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9296             :         struct nl_msg *msg;
    9297             : 
    9298         783 :         msg = nlmsg_alloc();
    9299         783 :         if (!msg)
    9300           0 :                 return -ENOMEM;
    9301             : 
    9302         783 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
    9303             : 
    9304         783 :         if (addr)
    9305           0 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    9306         783 :         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
    9307         783 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
    9308             : 
    9309         783 :         memset(seq, 0, 6);
    9310             : 
    9311         783 :         return send_and_recv_msgs(drv, msg, get_key_handler, seq);
    9312             :  nla_put_failure:
    9313           0 :         nlmsg_free(msg);
    9314           0 :         return -ENOBUFS;
    9315             : }
    9316             : 
    9317             : 
    9318           1 : static int i802_set_rts(void *priv, int rts)
    9319             : {
    9320           1 :         struct i802_bss *bss = priv;
    9321           1 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9322             :         struct nl_msg *msg;
    9323           1 :         int ret = -ENOBUFS;
    9324             :         u32 val;
    9325             : 
    9326           1 :         msg = nlmsg_alloc();
    9327           1 :         if (!msg)
    9328           0 :                 return -ENOMEM;
    9329             : 
    9330           1 :         if (rts >= 2347)
    9331           0 :                 val = (u32) -1;
    9332             :         else
    9333           1 :                 val = rts;
    9334             : 
    9335           1 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
    9336           1 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    9337           1 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
    9338             : 
    9339           1 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    9340           1 :         msg = NULL;
    9341           1 :         if (!ret)
    9342           1 :                 return 0;
    9343             : nla_put_failure:
    9344           0 :         nlmsg_free(msg);
    9345           0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
    9346             :                    "%d (%s)", rts, ret, strerror(-ret));
    9347           0 :         return ret;
    9348             : }
    9349             : 
    9350             : 
    9351           3 : static int i802_set_frag(void *priv, int frag)
    9352             : {
    9353           3 :         struct i802_bss *bss = priv;
    9354           3 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9355             :         struct nl_msg *msg;
    9356           3 :         int ret = -ENOBUFS;
    9357             :         u32 val;
    9358             : 
    9359           3 :         msg = nlmsg_alloc();
    9360           3 :         if (!msg)
    9361           0 :                 return -ENOMEM;
    9362             : 
    9363           3 :         if (frag >= 2346)
    9364           0 :                 val = (u32) -1;
    9365             :         else
    9366           3 :                 val = frag;
    9367             : 
    9368           3 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
    9369           3 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    9370           3 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
    9371             : 
    9372           3 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    9373           3 :         msg = NULL;
    9374           3 :         if (!ret)
    9375           3 :                 return 0;
    9376             : nla_put_failure:
    9377           0 :         nlmsg_free(msg);
    9378           0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
    9379             :                    "%d: %d (%s)", frag, ret, strerror(-ret));
    9380           0 :         return ret;
    9381             : }
    9382             : 
    9383             : 
    9384         622 : static int i802_flush(void *priv)
    9385             : {
    9386         622 :         struct i802_bss *bss = priv;
    9387         622 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9388             :         struct nl_msg *msg;
    9389             :         int res;
    9390             : 
    9391         622 :         msg = nlmsg_alloc();
    9392         622 :         if (!msg)
    9393           0 :                 return -1;
    9394             : 
    9395         622 :         wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
    9396         622 :                    bss->ifname);
    9397         622 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
    9398             : 
    9399             :         /*
    9400             :          * XXX: FIX! this needs to flush all VLANs too
    9401             :          */
    9402         622 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
    9403             :                     if_nametoindex(bss->ifname));
    9404             : 
    9405         622 :         res = send_and_recv_msgs(drv, msg, NULL, NULL);
    9406         622 :         if (res) {
    9407           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
    9408             :                            "(%s)", res, strerror(-res));
    9409             :         }
    9410         622 :         return res;
    9411             :  nla_put_failure:
    9412           0 :         nlmsg_free(msg);
    9413           0 :         return -ENOBUFS;
    9414             : }
    9415             : 
    9416             : 
    9417          48 : static int get_sta_handler(struct nl_msg *msg, void *arg)
    9418             : {
    9419             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    9420          48 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    9421          48 :         struct hostap_sta_driver_data *data = arg;
    9422             :         struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
    9423             :         static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
    9424             :                 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
    9425             :                 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
    9426             :                 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
    9427             :                 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
    9428             :                 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
    9429             :                 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
    9430             :         };
    9431             : 
    9432          48 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    9433             :                   genlmsg_attrlen(gnlh, 0), NULL);
    9434             : 
    9435             :         /*
    9436             :          * TODO: validate the interface and mac address!
    9437             :          * Otherwise, there's a race condition as soon as
    9438             :          * the kernel starts sending station notifications.
    9439             :          */
    9440             : 
    9441          48 :         if (!tb[NL80211_ATTR_STA_INFO]) {
    9442           0 :                 wpa_printf(MSG_DEBUG, "sta stats missing!");
    9443           0 :                 return NL_SKIP;
    9444             :         }
    9445          48 :         if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
    9446             :                              tb[NL80211_ATTR_STA_INFO],
    9447             :                              stats_policy)) {
    9448           0 :                 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
    9449           0 :                 return NL_SKIP;
    9450             :         }
    9451             : 
    9452          48 :         if (stats[NL80211_STA_INFO_INACTIVE_TIME])
    9453          48 :                 data->inactive_msec =
    9454          48 :                         nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
    9455          48 :         if (stats[NL80211_STA_INFO_RX_BYTES])
    9456          48 :                 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
    9457          48 :         if (stats[NL80211_STA_INFO_TX_BYTES])
    9458          48 :                 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
    9459          48 :         if (stats[NL80211_STA_INFO_RX_PACKETS])
    9460          48 :                 data->rx_packets =
    9461          48 :                         nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
    9462          48 :         if (stats[NL80211_STA_INFO_TX_PACKETS])
    9463          48 :                 data->tx_packets =
    9464          48 :                         nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
    9465          48 :         if (stats[NL80211_STA_INFO_TX_FAILED])
    9466          48 :                 data->tx_retry_failed =
    9467          48 :                         nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
    9468             : 
    9469          48 :         return NL_SKIP;
    9470             : }
    9471             : 
    9472          49 : static int i802_read_sta_data(struct i802_bss *bss,
    9473             :                               struct hostap_sta_driver_data *data,
    9474             :                               const u8 *addr)
    9475             : {
    9476          49 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9477             :         struct nl_msg *msg;
    9478             : 
    9479          49 :         os_memset(data, 0, sizeof(*data));
    9480          49 :         msg = nlmsg_alloc();
    9481          49 :         if (!msg)
    9482           0 :                 return -ENOMEM;
    9483             : 
    9484          49 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
    9485             : 
    9486          49 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    9487          49 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
    9488             : 
    9489          49 :         return send_and_recv_msgs(drv, msg, get_sta_handler, data);
    9490             :  nla_put_failure:
    9491           0 :         nlmsg_free(msg);
    9492           0 :         return -ENOBUFS;
    9493             : }
    9494             : 
    9495             : 
    9496        2252 : static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
    9497             :                                     int cw_min, int cw_max, int burst_time)
    9498             : {
    9499        2252 :         struct i802_bss *bss = priv;
    9500        2252 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9501             :         struct nl_msg *msg;
    9502             :         struct nlattr *txq, *params;
    9503             : 
    9504        2252 :         msg = nlmsg_alloc();
    9505        2252 :         if (!msg)
    9506           0 :                 return -1;
    9507             : 
    9508        2252 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
    9509             : 
    9510        2252 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
    9511             : 
    9512        2252 :         txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
    9513        2252 :         if (!txq)
    9514           0 :                 goto nla_put_failure;
    9515             : 
    9516             :         /* We are only sending parameters for a single TXQ at a time */
    9517        2252 :         params = nla_nest_start(msg, 1);
    9518        2252 :         if (!params)
    9519           0 :                 goto nla_put_failure;
    9520             : 
    9521        2252 :         switch (queue) {
    9522             :         case 0:
    9523         563 :                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
    9524         563 :                 break;
    9525             :         case 1:
    9526         563 :                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
    9527         563 :                 break;
    9528             :         case 2:
    9529         563 :                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
    9530         563 :                 break;
    9531             :         case 3:
    9532         563 :                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
    9533         563 :                 break;
    9534             :         }
    9535             :         /* Burst time is configured in units of 0.1 msec and TXOP parameter in
    9536             :          * 32 usec, so need to convert the value here. */
    9537        2252 :         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
    9538        2252 :         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
    9539        2252 :         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
    9540        2252 :         NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
    9541             : 
    9542        2252 :         nla_nest_end(msg, params);
    9543             : 
    9544        2252 :         nla_nest_end(msg, txq);
    9545             : 
    9546        2252 :         if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
    9547        2252 :                 return 0;
    9548           0 :         msg = NULL;
    9549             :  nla_put_failure:
    9550           0 :         nlmsg_free(msg);
    9551           0 :         return -1;
    9552             : }
    9553             : 
    9554             : 
    9555          14 : static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
    9556             :                              const char *ifname, int vlan_id)
    9557             : {
    9558          14 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9559             :         struct nl_msg *msg;
    9560          14 :         int ret = -ENOBUFS;
    9561             : 
    9562          14 :         msg = nlmsg_alloc();
    9563          14 :         if (!msg)
    9564           0 :                 return -ENOMEM;
    9565             : 
    9566         126 :         wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
    9567             :                    ", ifname=%s[%d], vlan_id=%d)",
    9568          28 :                    bss->ifname, if_nametoindex(bss->ifname),
    9569          84 :                    MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
    9570          14 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
    9571             : 
    9572          14 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
    9573             :                     if_nametoindex(bss->ifname));
    9574          14 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    9575          14 :         NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
    9576             :                     if_nametoindex(ifname));
    9577             : 
    9578          14 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    9579          14 :         msg = NULL;
    9580          14 :         if (ret < 0) {
    9581           0 :                 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
    9582             :                            MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
    9583           0 :                            MAC2STR(addr), ifname, vlan_id, ret,
    9584             :                            strerror(-ret));
    9585             :         }
    9586             :  nla_put_failure:
    9587          14 :         nlmsg_free(msg);
    9588          14 :         return ret;
    9589             : }
    9590             : 
    9591             : 
    9592           6 : static int i802_get_inact_sec(void *priv, const u8 *addr)
    9593             : {
    9594             :         struct hostap_sta_driver_data data;
    9595             :         int ret;
    9596             : 
    9597           6 :         data.inactive_msec = (unsigned long) -1;
    9598           6 :         ret = i802_read_sta_data(priv, &data, addr);
    9599           6 :         if (ret || data.inactive_msec == (unsigned long) -1)
    9600           0 :                 return -1;
    9601           6 :         return data.inactive_msec / 1000;
    9602             : }
    9603             : 
    9604             : 
    9605         697 : static int i802_sta_clear_stats(void *priv, const u8 *addr)
    9606             : {
    9607             : #if 0
    9608             :         /* TODO */
    9609             : #endif
    9610         697 :         return 0;
    9611             : }
    9612             : 
    9613             : 
    9614        1353 : static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
    9615             :                            int reason)
    9616             : {
    9617        1353 :         struct i802_bss *bss = priv;
    9618        1353 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9619             :         struct ieee80211_mgmt mgmt;
    9620             : 
    9621        1353 :         if (drv->device_ap_sme)
    9622           0 :                 return wpa_driver_nl80211_sta_remove(bss, addr);
    9623             : 
    9624        1353 :         memset(&mgmt, 0, sizeof(mgmt));
    9625        1353 :         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
    9626             :                                           WLAN_FC_STYPE_DEAUTH);
    9627        1353 :         memcpy(mgmt.da, addr, ETH_ALEN);
    9628        1353 :         memcpy(mgmt.sa, own_addr, ETH_ALEN);
    9629        1353 :         memcpy(mgmt.bssid, own_addr, ETH_ALEN);
    9630        1353 :         mgmt.u.deauth.reason_code = host_to_le16(reason);
    9631        1353 :         return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
    9632             :                                             IEEE80211_HDRLEN +
    9633             :                                             sizeof(mgmt.u.deauth), 0, 0, 0, 0,
    9634             :                                             0);
    9635             : }
    9636             : 
    9637             : 
    9638           3 : static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
    9639             :                              int reason)
    9640             : {
    9641           3 :         struct i802_bss *bss = priv;
    9642           3 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9643             :         struct ieee80211_mgmt mgmt;
    9644             : 
    9645           3 :         if (drv->device_ap_sme)
    9646           0 :                 return wpa_driver_nl80211_sta_remove(bss, addr);
    9647             : 
    9648           3 :         memset(&mgmt, 0, sizeof(mgmt));
    9649           3 :         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
    9650             :                                           WLAN_FC_STYPE_DISASSOC);
    9651           3 :         memcpy(mgmt.da, addr, ETH_ALEN);
    9652           3 :         memcpy(mgmt.sa, own_addr, ETH_ALEN);
    9653           3 :         memcpy(mgmt.bssid, own_addr, ETH_ALEN);
    9654           3 :         mgmt.u.disassoc.reason_code = host_to_le16(reason);
    9655           3 :         return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
    9656             :                                             IEEE80211_HDRLEN +
    9657             :                                             sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
    9658             :                                             0);
    9659             : }
    9660             : 
    9661             : 
    9662         663 : static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
    9663             : {
    9664             :         char buf[200], *pos, *end;
    9665             :         int i, res;
    9666             : 
    9667         663 :         pos = buf;
    9668         663 :         end = pos + sizeof(buf);
    9669             : 
    9670       11271 :         for (i = 0; i < drv->num_if_indices; i++) {
    9671       10608 :                 if (!drv->if_indices[i])
    9672        9821 :                         continue;
    9673         787 :                 res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
    9674         787 :                 if (res < 0 || res >= end - pos)
    9675             :                         break;
    9676         787 :                 pos += res;
    9677             :         }
    9678         663 :         *pos = '\0';
    9679             : 
    9680         663 :         wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
    9681             :                    drv->num_if_indices, buf);
    9682         663 : }
    9683             : 
    9684             : 
    9685         718 : static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
    9686             : {
    9687             :         int i;
    9688             :         int *old;
    9689             : 
    9690         718 :         wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
    9691             :                    ifidx);
    9692         718 :         if (have_ifidx(drv, ifidx)) {
    9693          91 :                 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
    9694             :                            ifidx);
    9695          91 :                 return;
    9696             :         }
    9697         714 :         for (i = 0; i < drv->num_if_indices; i++) {
    9698         714 :                 if (drv->if_indices[i] == 0) {
    9699         627 :                         drv->if_indices[i] = ifidx;
    9700         627 :                         dump_ifidx(drv);
    9701         627 :                         return;
    9702             :                 }
    9703             :         }
    9704             : 
    9705           0 :         if (drv->if_indices != drv->default_if_indices)
    9706           0 :                 old = drv->if_indices;
    9707             :         else
    9708           0 :                 old = NULL;
    9709             : 
    9710           0 :         drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
    9711             :                                            sizeof(int));
    9712           0 :         if (!drv->if_indices) {
    9713           0 :                 if (!old)
    9714           0 :                         drv->if_indices = drv->default_if_indices;
    9715             :                 else
    9716           0 :                         drv->if_indices = old;
    9717           0 :                 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
    9718             :                            "interfaces");
    9719           0 :                 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
    9720           0 :                 return;
    9721           0 :         } else if (!old)
    9722           0 :                 os_memcpy(drv->if_indices, drv->default_if_indices,
    9723             :                           sizeof(drv->default_if_indices));
    9724           0 :         drv->if_indices[drv->num_if_indices] = ifidx;
    9725           0 :         drv->num_if_indices++;
    9726           0 :         dump_ifidx(drv);
    9727             : }
    9728             : 
    9729             : 
    9730          36 : static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
    9731             : {
    9732             :         int i;
    9733             : 
    9734          89 :         for (i = 0; i < drv->num_if_indices; i++) {
    9735          89 :                 if (drv->if_indices[i] == ifidx) {
    9736          36 :                         drv->if_indices[i] = 0;
    9737          36 :                         break;
    9738             :                 }
    9739             :         }
    9740          36 :         dump_ifidx(drv);
    9741          36 : }
    9742             : 
    9743             : 
    9744       12725 : static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
    9745             : {
    9746             :         int i;
    9747             : 
    9748      156507 :         for (i = 0; i < drv->num_if_indices; i++)
    9749      147556 :                 if (drv->if_indices[i] == ifidx)
    9750        3774 :                         return 1;
    9751             : 
    9752        8951 :         return 0;
    9753             : }
    9754             : 
    9755             : 
    9756           2 : static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
    9757             :                             const char *bridge_ifname, char *ifname_wds)
    9758             : {
    9759           2 :         struct i802_bss *bss = priv;
    9760           2 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9761             :         char name[IFNAMSIZ + 1];
    9762             : 
    9763           2 :         os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
    9764           2 :         if (ifname_wds)
    9765           1 :                 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
    9766             : 
    9767          12 :         wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
    9768          12 :                    " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
    9769           2 :         if (val) {
    9770           1 :                 if (!if_nametoindex(name)) {
    9771           1 :                         if (nl80211_create_iface(drv, name,
    9772             :                                                  NL80211_IFTYPE_AP_VLAN,
    9773           1 :                                                  bss->addr, 1, NULL, NULL, 0) <
    9774             :                             0)
    9775           0 :                                 return -1;
    9776           2 :                         if (bridge_ifname &&
    9777           1 :                             linux_br_add_if(drv->global->ioctl_sock,
    9778             :                                             bridge_ifname, name) < 0)
    9779           0 :                                 return -1;
    9780             :                 }
    9781           1 :                 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
    9782           0 :                         wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
    9783             :                                    "interface %s up", name);
    9784             :                 }
    9785           1 :                 return i802_set_sta_vlan(priv, addr, name, 0);
    9786             :         } else {
    9787           1 :                 if (bridge_ifname)
    9788           1 :                         linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
    9789             :                                         name);
    9790             : 
    9791           1 :                 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
    9792           1 :                 nl80211_remove_iface(drv, if_nametoindex(name));
    9793           1 :                 return 0;
    9794             :         }
    9795             : }
    9796             : 
    9797             : 
    9798        8157 : static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
    9799             : {
    9800        8157 :         struct wpa_driver_nl80211_data *drv = eloop_ctx;
    9801             :         struct sockaddr_ll lladdr;
    9802             :         unsigned char buf[3000];
    9803             :         int len;
    9804        8157 :         socklen_t fromlen = sizeof(lladdr);
    9805             : 
    9806        8157 :         len = recvfrom(sock, buf, sizeof(buf), 0,
    9807             :                        (struct sockaddr *)&lladdr, &fromlen);
    9808        8157 :         if (len < 0) {
    9809           0 :                 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
    9810           0 :                            strerror(errno));
    9811        8157 :                 return;
    9812             :         }
    9813             : 
    9814        8157 :         if (have_ifidx(drv, lladdr.sll_ifindex))
    9815        3458 :                 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
    9816             : }
    9817             : 
    9818             : 
    9819           4 : static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
    9820             :                              struct i802_bss *bss,
    9821             :                              const char *brname, const char *ifname)
    9822             : {
    9823             :         int ifindex;
    9824             :         char in_br[IFNAMSIZ];
    9825             : 
    9826           4 :         os_strlcpy(bss->brname, brname, IFNAMSIZ);
    9827           4 :         ifindex = if_nametoindex(brname);
    9828           4 :         if (ifindex == 0) {
    9829             :                 /*
    9830             :                  * Bridge was configured, but the bridge device does
    9831             :                  * not exist. Try to add it now.
    9832             :                  */
    9833           2 :                 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
    9834           0 :                         wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
    9835             :                                    "bridge interface %s: %s",
    9836           0 :                                    brname, strerror(errno));
    9837           0 :                         return -1;
    9838             :                 }
    9839           2 :                 bss->added_bridge = 1;
    9840           2 :                 add_ifidx(drv, if_nametoindex(brname));
    9841             :         }
    9842             : 
    9843           4 :         if (linux_br_get(in_br, ifname) == 0) {
    9844           0 :                 if (os_strcmp(in_br, brname) == 0)
    9845           0 :                         return 0; /* already in the bridge */
    9846             : 
    9847           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
    9848             :                            "bridge %s", ifname, in_br);
    9849           0 :                 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
    9850             :                     0) {
    9851           0 :                         wpa_printf(MSG_ERROR, "nl80211: Failed to "
    9852             :                                    "remove interface %s from bridge "
    9853             :                                    "%s: %s",
    9854           0 :                                    ifname, brname, strerror(errno));
    9855           0 :                         return -1;
    9856             :                 }
    9857             :         }
    9858             : 
    9859           4 :         wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
    9860             :                    ifname, brname);
    9861           4 :         if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
    9862           0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
    9863             :                            "into bridge %s: %s",
    9864           0 :                            ifname, brname, strerror(errno));
    9865           0 :                 return -1;
    9866             :         }
    9867           4 :         bss->added_if_into_bridge = 1;
    9868             : 
    9869           4 :         return 0;
    9870             : }
    9871             : 
    9872             : 
    9873         580 : static void *i802_init(struct hostapd_data *hapd,
    9874             :                        struct wpa_init_params *params)
    9875             : {
    9876             :         struct wpa_driver_nl80211_data *drv;
    9877             :         struct i802_bss *bss;
    9878             :         size_t i;
    9879             :         char brname[IFNAMSIZ];
    9880             :         int ifindex, br_ifindex;
    9881         580 :         int br_added = 0;
    9882             : 
    9883         580 :         bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
    9884             :                                           params->global_priv, 1,
    9885             :                                           params->bssid);
    9886         580 :         if (bss == NULL)
    9887           0 :                 return NULL;
    9888             : 
    9889         580 :         drv = bss->drv;
    9890             : 
    9891         580 :         if (linux_br_get(brname, params->ifname) == 0) {
    9892           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
    9893             :                            params->ifname, brname);
    9894           0 :                 br_ifindex = if_nametoindex(brname);
    9895             :         } else {
    9896         580 :                 brname[0] = '\0';
    9897         580 :                 br_ifindex = 0;
    9898             :         }
    9899             : 
    9900        1167 :         for (i = 0; i < params->num_bridge; i++) {
    9901         587 :                 if (params->bridge[i]) {
    9902           4 :                         ifindex = if_nametoindex(params->bridge[i]);
    9903           4 :                         if (ifindex)
    9904           2 :                                 add_ifidx(drv, ifindex);
    9905           4 :                         if (ifindex == br_ifindex)
    9906           2 :                                 br_added = 1;
    9907             :                 }
    9908             :         }
    9909         580 :         if (!br_added && br_ifindex &&
    9910           0 :             (params->num_bridge == 0 || !params->bridge[0]))
    9911           0 :                 add_ifidx(drv, br_ifindex);
    9912             : 
    9913             :         /* start listening for EAPOL on the default AP interface */
    9914         580 :         add_ifidx(drv, drv->ifindex);
    9915             : 
    9916         584 :         if (params->num_bridge && params->bridge[0] &&
    9917           4 :             i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
    9918           0 :                 goto failed;
    9919             : 
    9920         580 :         drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
    9921         580 :         if (drv->eapol_sock < 0) {
    9922           0 :                 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
    9923           0 :                            strerror(errno));
    9924           0 :                 goto failed;
    9925             :         }
    9926             : 
    9927         580 :         if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
    9928             :         {
    9929           0 :                 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
    9930           0 :                 goto failed;
    9931             :         }
    9932             : 
    9933         580 :         if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
    9934             :                                params->own_addr))
    9935           0 :                 goto failed;
    9936             : 
    9937         580 :         memcpy(bss->addr, params->own_addr, ETH_ALEN);
    9938             : 
    9939         580 :         return bss;
    9940             : 
    9941             : failed:
    9942           0 :         wpa_driver_nl80211_deinit(bss);
    9943           0 :         return NULL;
    9944             : }
    9945             : 
    9946             : 
    9947         580 : static void i802_deinit(void *priv)
    9948             : {
    9949         580 :         struct i802_bss *bss = priv;
    9950         580 :         wpa_driver_nl80211_deinit(bss);
    9951         580 : }
    9952             : 
    9953             : 
    9954          30 : static enum nl80211_iftype wpa_driver_nl80211_if_type(
    9955             :         enum wpa_driver_if_type type)
    9956             : {
    9957          30 :         switch (type) {
    9958             :         case WPA_IF_STATION:
    9959           0 :                 return NL80211_IFTYPE_STATION;
    9960             :         case WPA_IF_P2P_CLIENT:
    9961             :         case WPA_IF_P2P_GROUP:
    9962           0 :                 return NL80211_IFTYPE_P2P_CLIENT;
    9963             :         case WPA_IF_AP_VLAN:
    9964          11 :                 return NL80211_IFTYPE_AP_VLAN;
    9965             :         case WPA_IF_AP_BSS:
    9966          19 :                 return NL80211_IFTYPE_AP;
    9967             :         case WPA_IF_P2P_GO:
    9968           0 :                 return NL80211_IFTYPE_P2P_GO;
    9969             :         case WPA_IF_P2P_DEVICE:
    9970           0 :                 return NL80211_IFTYPE_P2P_DEVICE;
    9971             :         }
    9972           0 :         return -1;
    9973             : }
    9974             : 
    9975             : 
    9976             : #ifdef CONFIG_P2P
    9977             : 
    9978             : static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
    9979             : {
    9980             :         struct wpa_driver_nl80211_data *drv;
    9981             :         dl_list_for_each(drv, &global->interfaces,
    9982             :                          struct wpa_driver_nl80211_data, list) {
    9983             :                 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
    9984             :                         return 1;
    9985             :         }
    9986             :         return 0;
    9987             : }
    9988             : 
    9989             : 
    9990             : static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
    9991             :                                       u8 *new_addr)
    9992             : {
    9993             :         unsigned int idx;
    9994             : 
    9995             :         if (!drv->global)
    9996             :                 return -1;
    9997             : 
    9998             :         os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
    9999             :         for (idx = 0; idx < 64; idx++) {
   10000             :                 new_addr[0] = drv->first_bss->addr[0] | 0x02;
   10001             :                 new_addr[0] ^= idx << 2;
   10002             :                 if (!nl80211_addr_in_use(drv->global, new_addr))
   10003             :                         break;
   10004             :         }
   10005             :         if (idx == 64)
   10006             :                 return -1;
   10007             : 
   10008             :         wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
   10009             :                    MACSTR, MAC2STR(new_addr));
   10010             : 
   10011             :         return 0;
   10012             : }
   10013             : 
   10014             : #endif /* CONFIG_P2P */
   10015             : 
   10016             : 
   10017             : struct wdev_info {
   10018             :         u64 wdev_id;
   10019             :         int wdev_id_set;
   10020             :         u8 macaddr[ETH_ALEN];
   10021             : };
   10022             : 
   10023           0 : static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
   10024             : {
   10025           0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
   10026             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
   10027           0 :         struct wdev_info *wi = arg;
   10028             : 
   10029           0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
   10030             :                   genlmsg_attrlen(gnlh, 0), NULL);
   10031           0 :         if (tb[NL80211_ATTR_WDEV]) {
   10032           0 :                 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
   10033           0 :                 wi->wdev_id_set = 1;
   10034             :         }
   10035             : 
   10036           0 :         if (tb[NL80211_ATTR_MAC])
   10037           0 :                 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
   10038             :                           ETH_ALEN);
   10039             : 
   10040           0 :         return NL_SKIP;
   10041             : }
   10042             : 
   10043             : 
   10044          30 : static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
   10045             :                                      const char *ifname, const u8 *addr,
   10046             :                                      void *bss_ctx, void **drv_priv,
   10047             :                                      char *force_ifname, u8 *if_addr,
   10048             :                                      const char *bridge, int use_existing)
   10049             : {
   10050             :         enum nl80211_iftype nlmode;
   10051          30 :         struct i802_bss *bss = priv;
   10052          30 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10053             :         int ifidx;
   10054          30 :         int added = 1;
   10055             : 
   10056          30 :         if (addr)
   10057          30 :                 os_memcpy(if_addr, addr, ETH_ALEN);
   10058          30 :         nlmode = wpa_driver_nl80211_if_type(type);
   10059          30 :         if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
   10060             :                 struct wdev_info p2pdev_info;
   10061             : 
   10062           0 :                 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
   10063           0 :                 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
   10064             :                                              0, nl80211_wdev_handler,
   10065             :                                              &p2pdev_info, use_existing);
   10066           0 :                 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
   10067           0 :                         wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
   10068             :                                    ifname);
   10069           0 :                         return -1;
   10070             :                 }
   10071             : 
   10072           0 :                 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
   10073           0 :                 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
   10074           0 :                 if (!is_zero_ether_addr(p2pdev_info.macaddr))
   10075           0 :                         os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
   10076           0 :                 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
   10077             :                            ifname,
   10078           0 :                            (long long unsigned int) p2pdev_info.wdev_id);
   10079             :         } else {
   10080          30 :                 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
   10081             :                                              0, NULL, NULL, use_existing);
   10082          30 :                 if (use_existing && ifidx == -ENFILE) {
   10083           0 :                         added = 0;
   10084           0 :                         ifidx = if_nametoindex(ifname);
   10085          30 :                 } else if (ifidx < 0) {
   10086           0 :                         return -1;
   10087             :                 }
   10088             :         }
   10089             : 
   10090          30 :         if (!addr) {
   10091           0 :                 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
   10092           0 :                         os_memcpy(if_addr, bss->addr, ETH_ALEN);
   10093           0 :                 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
   10094           0 :                                             bss->ifname, if_addr) < 0) {
   10095           0 :                         if (added)
   10096           0 :                                 nl80211_remove_iface(drv, ifidx);
   10097           0 :                         return -1;
   10098             :                 }
   10099             :         }
   10100             : 
   10101             : #ifdef CONFIG_P2P
   10102             :         if (!addr &&
   10103             :             (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
   10104             :              type == WPA_IF_P2P_GO)) {
   10105             :                 /* Enforce unique P2P Interface Address */
   10106             :                 u8 new_addr[ETH_ALEN];
   10107             : 
   10108             :                 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
   10109             :                                        new_addr) < 0) {
   10110             :                         if (added)
   10111             :                                 nl80211_remove_iface(drv, ifidx);
   10112             :                         return -1;
   10113             :                 }
   10114             :                 if (nl80211_addr_in_use(drv->global, new_addr)) {
   10115             :                         wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
   10116             :                                    "for P2P group interface");
   10117             :                         if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
   10118             :                                 if (added)
   10119             :                                         nl80211_remove_iface(drv, ifidx);
   10120             :                                 return -1;
   10121             :                         }
   10122             :                         if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
   10123             :                                                new_addr) < 0) {
   10124             :                                 if (added)
   10125             :                                         nl80211_remove_iface(drv, ifidx);
   10126             :                                 return -1;
   10127             :                         }
   10128             :                 }
   10129             :                 os_memcpy(if_addr, new_addr, ETH_ALEN);
   10130             :         }
   10131             : #endif /* CONFIG_P2P */
   10132             : 
   10133          30 :         if (type == WPA_IF_AP_BSS) {
   10134          19 :                 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
   10135          19 :                 if (new_bss == NULL) {
   10136           0 :                         if (added)
   10137           0 :                                 nl80211_remove_iface(drv, ifidx);
   10138           0 :                         return -1;
   10139             :                 }
   10140             : 
   10141          19 :                 if (bridge &&
   10142           0 :                     i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
   10143           0 :                         wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
   10144             :                                    "interface %s to a bridge %s",
   10145             :                                    ifname, bridge);
   10146           0 :                         if (added)
   10147           0 :                                 nl80211_remove_iface(drv, ifidx);
   10148           0 :                         os_free(new_bss);
   10149           0 :                         return -1;
   10150             :                 }
   10151             : 
   10152          19 :                 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
   10153             :                 {
   10154           0 :                         if (added)
   10155           0 :                                 nl80211_remove_iface(drv, ifidx);
   10156           0 :                         os_free(new_bss);
   10157           0 :                         return -1;
   10158             :                 }
   10159          19 :                 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
   10160          19 :                 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
   10161          19 :                 new_bss->ifindex = ifidx;
   10162          19 :                 new_bss->drv = drv;
   10163          19 :                 new_bss->next = drv->first_bss->next;
   10164          19 :                 new_bss->freq = drv->first_bss->freq;
   10165          19 :                 new_bss->ctx = bss_ctx;
   10166          19 :                 new_bss->added_if = added;
   10167          19 :                 drv->first_bss->next = new_bss;
   10168          19 :                 if (drv_priv)
   10169          19 :                         *drv_priv = new_bss;
   10170          19 :                 nl80211_init_bss(new_bss);
   10171             : 
   10172             :                 /* Subscribe management frames for this WPA_IF_AP_BSS */
   10173          19 :                 if (nl80211_setup_ap(new_bss))
   10174           0 :                         return -1;
   10175             :         }
   10176             : 
   10177          30 :         if (drv->global)
   10178          30 :                 drv->global->if_add_ifindex = ifidx;
   10179             : 
   10180          30 :         if (ifidx > 0)
   10181          30 :                 add_ifidx(drv, ifidx);
   10182             : 
   10183          30 :         return 0;
   10184             : }
   10185             : 
   10186             : 
   10187          30 : static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
   10188             :                                         enum wpa_driver_if_type type,
   10189             :                                         const char *ifname)
   10190             : {
   10191          30 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10192          30 :         int ifindex = if_nametoindex(ifname);
   10193             : 
   10194          30 :         wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
   10195          30 :                    __func__, type, ifname, ifindex, bss->added_if);
   10196          30 :         if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
   10197          30 :                 nl80211_remove_iface(drv, ifindex);
   10198           0 :         else if (ifindex > 0 && !bss->added_if) {
   10199             :                 struct wpa_driver_nl80211_data *drv2;
   10200           0 :                 dl_list_for_each(drv2, &drv->global->interfaces,
   10201             :                                  struct wpa_driver_nl80211_data, list)
   10202           0 :                         del_ifidx(drv2, ifindex);
   10203             :         }
   10204             : 
   10205          30 :         if (type != WPA_IF_AP_BSS)
   10206          11 :                 return 0;
   10207             : 
   10208          19 :         if (bss->added_if_into_bridge) {
   10209           0 :                 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
   10210           0 :                                     bss->ifname) < 0)
   10211           0 :                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
   10212             :                                    "interface %s from bridge %s: %s",
   10213           0 :                                    bss->ifname, bss->brname, strerror(errno));
   10214             :         }
   10215          19 :         if (bss->added_bridge) {
   10216           0 :                 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
   10217           0 :                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
   10218             :                                    "bridge %s: %s",
   10219           0 :                                    bss->brname, strerror(errno));
   10220             :         }
   10221             : 
   10222          19 :         if (bss != drv->first_bss) {
   10223             :                 struct i802_bss *tbss;
   10224             : 
   10225          19 :                 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
   10226          22 :                 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
   10227          22 :                         if (tbss->next == bss) {
   10228          19 :                                 tbss->next = bss->next;
   10229             :                                 /* Unsubscribe management frames */
   10230          19 :                                 nl80211_teardown_ap(bss);
   10231          19 :                                 nl80211_destroy_bss(bss);
   10232          19 :                                 if (!bss->added_if)
   10233           0 :                                         i802_set_iface_flags(bss, 0);
   10234          19 :                                 os_free(bss);
   10235          19 :                                 bss = NULL;
   10236          19 :                                 break;
   10237             :                         }
   10238             :                 }
   10239          19 :                 if (bss)
   10240           0 :                         wpa_printf(MSG_INFO, "nl80211: %s - could not find "
   10241             :                                    "BSS %p in the list", __func__, bss);
   10242             :         } else {
   10243           0 :                 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
   10244           0 :                 nl80211_teardown_ap(bss);
   10245           0 :                 if (!bss->added_if && !drv->first_bss->next)
   10246           0 :                         wpa_driver_nl80211_del_beacon(drv);
   10247           0 :                 nl80211_destroy_bss(bss);
   10248           0 :                 if (!bss->added_if)
   10249           0 :                         i802_set_iface_flags(bss, 0);
   10250           0 :                 if (drv->first_bss->next) {
   10251           0 :                         drv->first_bss = drv->first_bss->next;
   10252           0 :                         drv->ctx = drv->first_bss->ctx;
   10253           0 :                         os_free(bss);
   10254             :                 } else {
   10255           0 :                         wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
   10256             :                 }
   10257             :         }
   10258             : 
   10259          19 :         return 0;
   10260             : }
   10261             : 
   10262             : 
   10263        3401 : static int cookie_handler(struct nl_msg *msg, void *arg)
   10264             : {
   10265             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
   10266        3401 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
   10267        3401 :         u64 *cookie = arg;
   10268        3401 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
   10269             :                   genlmsg_attrlen(gnlh, 0), NULL);
   10270        3401 :         if (tb[NL80211_ATTR_COOKIE])
   10271        3401 :                 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
   10272        3401 :         return NL_SKIP;
   10273             : }
   10274             : 
   10275             : 
   10276        5155 : static int nl80211_send_frame_cmd(struct i802_bss *bss,
   10277             :                                   unsigned int freq, unsigned int wait,
   10278             :                                   const u8 *buf, size_t buf_len,
   10279             :                                   u64 *cookie_out, int no_cck, int no_ack,
   10280             :                                   int offchanok)
   10281             : {
   10282        5155 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10283             :         struct nl_msg *msg;
   10284             :         u64 cookie;
   10285        5155 :         int ret = -1;
   10286             : 
   10287        5155 :         msg = nlmsg_alloc();
   10288        5155 :         if (!msg)
   10289           0 :                 return -1;
   10290             : 
   10291        5155 :         wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
   10292             :                    "no_ack=%d offchanok=%d",
   10293             :                    freq, wait, no_cck, no_ack, offchanok);
   10294        5155 :         wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
   10295        5155 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
   10296             : 
   10297        5155 :         if (nl80211_set_iface_id(msg, bss) < 0)
   10298           0 :                 goto nla_put_failure;
   10299        5155 :         if (freq)
   10300        5140 :                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
   10301        5155 :         if (wait)
   10302           0 :                 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
   10303        5155 :         if (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
   10304             :                           drv->test_use_roc_tx))
   10305         238 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
   10306        5155 :         if (no_cck)
   10307           0 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
   10308        5155 :         if (no_ack)
   10309        1152 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
   10310             : 
   10311        5155 :         NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
   10312             : 
   10313        5155 :         cookie = 0;
   10314        5155 :         ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
   10315        5155 :         msg = NULL;
   10316        5155 :         if (ret) {
   10317         605 :                 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
   10318             :                            "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
   10319             :                            freq, wait);
   10320         605 :                 goto nla_put_failure;
   10321             :         }
   10322        4550 :         wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
   10323             :                    "cookie 0x%llx", no_ack ? " (no ACK)" : "",
   10324             :                    (long long unsigned int) cookie);
   10325             : 
   10326        4550 :         if (cookie_out)
   10327        4550 :                 *cookie_out = no_ack ? (u64) -1 : cookie;
   10328             : 
   10329             : nla_put_failure:
   10330        5155 :         nlmsg_free(msg);
   10331        5155 :         return ret;
   10332             : }
   10333             : 
   10334             : 
   10335         238 : static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
   10336             :                                           unsigned int freq,
   10337             :                                           unsigned int wait_time,
   10338             :                                           const u8 *dst, const u8 *src,
   10339             :                                           const u8 *bssid,
   10340             :                                           const u8 *data, size_t data_len,
   10341             :                                           int no_cck)
   10342             : {
   10343         238 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10344         238 :         int ret = -1;
   10345             :         u8 *buf;
   10346             :         struct ieee80211_hdr *hdr;
   10347             : 
   10348         238 :         wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
   10349             :                    "freq=%u MHz wait=%d ms no_cck=%d)",
   10350             :                    drv->ifindex, freq, wait_time, no_cck);
   10351             : 
   10352         238 :         buf = os_zalloc(24 + data_len);
   10353         238 :         if (buf == NULL)
   10354           0 :                 return ret;
   10355         238 :         os_memcpy(buf + 24, data, data_len);
   10356         238 :         hdr = (struct ieee80211_hdr *) buf;
   10357         238 :         hdr->frame_control =
   10358             :                 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
   10359         238 :         os_memcpy(hdr->addr1, dst, ETH_ALEN);
   10360         238 :         os_memcpy(hdr->addr2, src, ETH_ALEN);
   10361         238 :         os_memcpy(hdr->addr3, bssid, ETH_ALEN);
   10362             : 
   10363         476 :         if (is_ap_interface(drv->nlmode) &&
   10364         476 :             (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
   10365         238 :              (int) freq == bss->freq || drv->device_ap_sme ||
   10366           0 :              !drv->use_monitor))
   10367         238 :                 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
   10368             :                                                    0, freq, no_cck, 1,
   10369             :                                                    wait_time);
   10370             :         else
   10371           0 :                 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
   10372             :                                              24 + data_len,
   10373             :                                              &drv->send_action_cookie,
   10374             :                                              no_cck, 0, 1);
   10375             : 
   10376         238 :         os_free(buf);
   10377         238 :         return ret;
   10378             : }
   10379             : 
   10380             : 
   10381           0 : static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
   10382             : {
   10383           0 :         struct i802_bss *bss = priv;
   10384           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10385             :         struct nl_msg *msg;
   10386             :         int ret;
   10387             : 
   10388           0 :         msg = nlmsg_alloc();
   10389           0 :         if (!msg)
   10390           0 :                 return;
   10391             : 
   10392           0 :         wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
   10393           0 :                    (long long unsigned int) drv->send_action_cookie);
   10394           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
   10395             : 
   10396           0 :         if (nl80211_set_iface_id(msg, bss) < 0)
   10397           0 :                 goto nla_put_failure;
   10398           0 :         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
   10399             : 
   10400           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   10401           0 :         msg = NULL;
   10402           0 :         if (ret)
   10403           0 :                 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
   10404             :                            "(%s)", ret, strerror(-ret));
   10405             : 
   10406             :  nla_put_failure:
   10407           0 :         nlmsg_free(msg);
   10408             : }
   10409             : 
   10410             : 
   10411           0 : static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
   10412             :                                                 unsigned int duration)
   10413             : {
   10414           0 :         struct i802_bss *bss = priv;
   10415           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10416             :         struct nl_msg *msg;
   10417             :         int ret;
   10418             :         u64 cookie;
   10419             : 
   10420           0 :         msg = nlmsg_alloc();
   10421           0 :         if (!msg)
   10422           0 :                 return -1;
   10423             : 
   10424           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
   10425             : 
   10426           0 :         if (nl80211_set_iface_id(msg, bss) < 0)
   10427           0 :                 goto nla_put_failure;
   10428             : 
   10429           0 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
   10430           0 :         NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
   10431             : 
   10432           0 :         cookie = 0;
   10433           0 :         ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
   10434           0 :         msg = NULL;
   10435           0 :         if (ret == 0) {
   10436           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
   10437             :                            "0x%llx for freq=%u MHz duration=%u",
   10438             :                            (long long unsigned int) cookie, freq, duration);
   10439           0 :                 drv->remain_on_chan_cookie = cookie;
   10440           0 :                 drv->pending_remain_on_chan = 1;
   10441           0 :                 return 0;
   10442             :         }
   10443           0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
   10444             :                    "(freq=%d duration=%u): %d (%s)",
   10445             :                    freq, duration, ret, strerror(-ret));
   10446             : nla_put_failure:
   10447           0 :         nlmsg_free(msg);
   10448           0 :         return -1;
   10449             : }
   10450             : 
   10451             : 
   10452           0 : static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
   10453             : {
   10454           0 :         struct i802_bss *bss = priv;
   10455           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10456             :         struct nl_msg *msg;
   10457             :         int ret;
   10458             : 
   10459           0 :         if (!drv->pending_remain_on_chan) {
   10460           0 :                 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
   10461             :                            "to cancel");
   10462           0 :                 return -1;
   10463             :         }
   10464             : 
   10465           0 :         wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
   10466             :                    "0x%llx",
   10467           0 :                    (long long unsigned int) drv->remain_on_chan_cookie);
   10468             : 
   10469           0 :         msg = nlmsg_alloc();
   10470           0 :         if (!msg)
   10471           0 :                 return -1;
   10472             : 
   10473           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
   10474             : 
   10475           0 :         if (nl80211_set_iface_id(msg, bss) < 0)
   10476           0 :                 goto nla_put_failure;
   10477             : 
   10478           0 :         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
   10479             : 
   10480           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   10481           0 :         msg = NULL;
   10482           0 :         if (ret == 0)
   10483           0 :                 return 0;
   10484           0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
   10485             :                    "%d (%s)", ret, strerror(-ret));
   10486             : nla_put_failure:
   10487           0 :         nlmsg_free(msg);
   10488           0 :         return -1;
   10489             : }
   10490             : 
   10491             : 
   10492         599 : static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
   10493             : {
   10494         599 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10495             : 
   10496         599 :         if (!report) {
   10497         599 :                 if (bss->nl_preq && drv->device_ap_sme &&
   10498           0 :                     is_ap_interface(drv->nlmode)) {
   10499             :                         /*
   10500             :                          * Do not disable Probe Request reporting that was
   10501             :                          * enabled in nl80211_setup_ap().
   10502             :                          */
   10503           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
   10504             :                                    "Probe Request reporting nl_preq=%p while "
   10505             :                                    "in AP mode", bss->nl_preq);
   10506         599 :                 } else if (bss->nl_preq) {
   10507           0 :                         wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
   10508             :                                    "reporting nl_preq=%p", bss->nl_preq);
   10509           0 :                         nl80211_destroy_eloop_handle(&bss->nl_preq);
   10510             :                 }
   10511         599 :                 return 0;
   10512             :         }
   10513             : 
   10514           0 :         if (bss->nl_preq) {
   10515           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
   10516             :                            "already on! nl_preq=%p", bss->nl_preq);
   10517           0 :                 return 0;
   10518             :         }
   10519             : 
   10520           0 :         bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
   10521           0 :         if (bss->nl_preq == NULL)
   10522           0 :                 return -1;
   10523           0 :         wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
   10524             :                    "reporting nl_preq=%p", bss->nl_preq);
   10525             : 
   10526           0 :         if (nl80211_register_frame(bss, bss->nl_preq,
   10527             :                                    (WLAN_FC_TYPE_MGMT << 2) |
   10528             :                                    (WLAN_FC_STYPE_PROBE_REQ << 4),
   10529             :                                    NULL, 0) < 0)
   10530           0 :                 goto out_err;
   10531             : 
   10532           0 :         nl80211_register_eloop_read(&bss->nl_preq,
   10533             :                                     wpa_driver_nl80211_event_receive,
   10534           0 :                                     bss->nl_cb);
   10535             : 
   10536           0 :         return 0;
   10537             : 
   10538             :  out_err:
   10539           0 :         nl_destroy_handles(&bss->nl_preq);
   10540           0 :         return -1;
   10541             : }
   10542             : 
   10543             : 
   10544           0 : static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
   10545             :                                      int ifindex, int disabled)
   10546             : {
   10547             :         struct nl_msg *msg;
   10548             :         struct nlattr *bands, *band;
   10549             :         int ret;
   10550             : 
   10551           0 :         msg = nlmsg_alloc();
   10552           0 :         if (!msg)
   10553           0 :                 return -1;
   10554             : 
   10555           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
   10556           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
   10557             : 
   10558           0 :         bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
   10559           0 :         if (!bands)
   10560           0 :                 goto nla_put_failure;
   10561             : 
   10562             :         /*
   10563             :          * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
   10564             :          * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
   10565             :          * rates. All 5 GHz rates are left enabled.
   10566             :          */
   10567           0 :         band = nla_nest_start(msg, NL80211_BAND_2GHZ);
   10568           0 :         if (!band)
   10569           0 :                 goto nla_put_failure;
   10570           0 :         if (disabled) {
   10571           0 :                 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
   10572             :                         "\x0c\x12\x18\x24\x30\x48\x60\x6c");
   10573             :         }
   10574           0 :         nla_nest_end(msg, band);
   10575             : 
   10576           0 :         nla_nest_end(msg, bands);
   10577             : 
   10578           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   10579           0 :         msg = NULL;
   10580           0 :         if (ret) {
   10581           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
   10582             :                            "(%s)", ret, strerror(-ret));
   10583             :         } else
   10584           0 :                 drv->disabled_11b_rates = disabled;
   10585             : 
   10586           0 :         return ret;
   10587             : 
   10588             : nla_put_failure:
   10589           0 :         nlmsg_free(msg);
   10590           0 :         return -1;
   10591             : }
   10592             : 
   10593             : 
   10594           0 : static int wpa_driver_nl80211_deinit_ap(void *priv)
   10595             : {
   10596           0 :         struct i802_bss *bss = priv;
   10597           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10598           0 :         if (!is_ap_interface(drv->nlmode))
   10599           0 :                 return -1;
   10600           0 :         wpa_driver_nl80211_del_beacon(drv);
   10601             : 
   10602             :         /*
   10603             :          * If the P2P GO interface was dynamically added, then it is
   10604             :          * possible that the interface change to station is not possible.
   10605             :          */
   10606           0 :         if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
   10607           0 :                 return 0;
   10608             : 
   10609           0 :         return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
   10610             : }
   10611             : 
   10612             : 
   10613           0 : static int wpa_driver_nl80211_stop_ap(void *priv)
   10614             : {
   10615           0 :         struct i802_bss *bss = priv;
   10616           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10617           0 :         if (!is_ap_interface(drv->nlmode))
   10618           0 :                 return -1;
   10619           0 :         wpa_driver_nl80211_del_beacon(drv);
   10620           0 :         bss->beacon_set = 0;
   10621           0 :         return 0;
   10622             : }
   10623             : 
   10624             : 
   10625           0 : static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
   10626             : {
   10627           0 :         struct i802_bss *bss = priv;
   10628           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10629           0 :         if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
   10630           0 :                 return -1;
   10631             : 
   10632             :         /*
   10633             :          * If the P2P Client interface was dynamically added, then it is
   10634             :          * possible that the interface change to station is not possible.
   10635             :          */
   10636           0 :         if (bss->if_dynamic)
   10637           0 :                 return 0;
   10638             : 
   10639           0 :         return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
   10640             : }
   10641             : 
   10642             : 
   10643           0 : static void wpa_driver_nl80211_resume(void *priv)
   10644             : {
   10645           0 :         struct i802_bss *bss = priv;
   10646             : 
   10647           0 :         if (i802_set_iface_flags(bss, 1))
   10648           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
   10649           0 : }
   10650             : 
   10651             : 
   10652           0 : static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
   10653             :                                   const u8 *ies, size_t ies_len)
   10654             : {
   10655           0 :         struct i802_bss *bss = priv;
   10656           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10657             :         int ret;
   10658             :         u8 *data, *pos;
   10659             :         size_t data_len;
   10660           0 :         const u8 *own_addr = bss->addr;
   10661             : 
   10662           0 :         if (action != 1) {
   10663           0 :                 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
   10664             :                            "action %d", action);
   10665           0 :                 return -1;
   10666             :         }
   10667             : 
   10668             :         /*
   10669             :          * Action frame payload:
   10670             :          * Category[1] = 6 (Fast BSS Transition)
   10671             :          * Action[1] = 1 (Fast BSS Transition Request)
   10672             :          * STA Address
   10673             :          * Target AP Address
   10674             :          * FT IEs
   10675             :          */
   10676             : 
   10677           0 :         data_len = 2 + 2 * ETH_ALEN + ies_len;
   10678           0 :         data = os_malloc(data_len);
   10679           0 :         if (data == NULL)
   10680           0 :                 return -1;
   10681           0 :         pos = data;
   10682           0 :         *pos++ = 0x06; /* FT Action category */
   10683           0 :         *pos++ = action;
   10684           0 :         os_memcpy(pos, own_addr, ETH_ALEN);
   10685           0 :         pos += ETH_ALEN;
   10686           0 :         os_memcpy(pos, target_ap, ETH_ALEN);
   10687           0 :         pos += ETH_ALEN;
   10688           0 :         os_memcpy(pos, ies, ies_len);
   10689             : 
   10690           0 :         ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
   10691           0 :                                              drv->bssid, own_addr, drv->bssid,
   10692             :                                              data, data_len, 0);
   10693           0 :         os_free(data);
   10694             : 
   10695           0 :         return ret;
   10696             : }
   10697             : 
   10698             : 
   10699           0 : static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
   10700             : {
   10701           0 :         struct i802_bss *bss = priv;
   10702           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10703             :         struct nl_msg *msg;
   10704             :         struct nlattr *cqm;
   10705           0 :         int ret = -1;
   10706             : 
   10707           0 :         wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
   10708             :                    "hysteresis=%d", threshold, hysteresis);
   10709             : 
   10710           0 :         msg = nlmsg_alloc();
   10711           0 :         if (!msg)
   10712           0 :                 return -1;
   10713             : 
   10714           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
   10715             : 
   10716           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
   10717             : 
   10718           0 :         cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
   10719           0 :         if (cqm == NULL)
   10720           0 :                 goto nla_put_failure;
   10721             : 
   10722           0 :         NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
   10723           0 :         NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
   10724           0 :         nla_nest_end(msg, cqm);
   10725             : 
   10726           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   10727           0 :         msg = NULL;
   10728             : 
   10729             : nla_put_failure:
   10730           0 :         nlmsg_free(msg);
   10731           0 :         return ret;
   10732             : }
   10733             : 
   10734             : 
   10735           0 : static int get_channel_width(struct nl_msg *msg, void *arg)
   10736             : {
   10737             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
   10738           0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
   10739           0 :         struct wpa_signal_info *sig_change = arg;
   10740             : 
   10741           0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
   10742             :                   genlmsg_attrlen(gnlh, 0), NULL);
   10743             : 
   10744           0 :         sig_change->center_frq1 = -1;
   10745           0 :         sig_change->center_frq2 = -1;
   10746           0 :         sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
   10747             : 
   10748           0 :         if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
   10749           0 :                 sig_change->chanwidth = convert2width(
   10750           0 :                         nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
   10751           0 :                 if (tb[NL80211_ATTR_CENTER_FREQ1])
   10752           0 :                         sig_change->center_frq1 =
   10753           0 :                                 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
   10754           0 :                 if (tb[NL80211_ATTR_CENTER_FREQ2])
   10755           0 :                         sig_change->center_frq2 =
   10756           0 :                                 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
   10757             :         }
   10758             : 
   10759           0 :         return NL_SKIP;
   10760             : }
   10761             : 
   10762             : 
   10763           0 : static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
   10764             :                                      struct wpa_signal_info *sig)
   10765             : {
   10766             :         struct nl_msg *msg;
   10767             : 
   10768           0 :         msg = nlmsg_alloc();
   10769           0 :         if (!msg)
   10770           0 :                 return -ENOMEM;
   10771             : 
   10772           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
   10773           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   10774             : 
   10775           0 :         return send_and_recv_msgs(drv, msg, get_channel_width, sig);
   10776             : 
   10777             : nla_put_failure:
   10778           0 :         nlmsg_free(msg);
   10779           0 :         return -ENOBUFS;
   10780             : }
   10781             : 
   10782             : 
   10783           0 : static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
   10784             : {
   10785           0 :         struct i802_bss *bss = priv;
   10786           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10787             :         int res;
   10788             : 
   10789           0 :         os_memset(si, 0, sizeof(*si));
   10790           0 :         res = nl80211_get_link_signal(drv, si);
   10791           0 :         if (res != 0)
   10792           0 :                 return res;
   10793             : 
   10794           0 :         res = nl80211_get_channel_width(drv, si);
   10795           0 :         if (res != 0)
   10796           0 :                 return res;
   10797             : 
   10798           0 :         return nl80211_get_link_noise(drv, si);
   10799             : }
   10800             : 
   10801             : 
   10802           0 : static int wpa_driver_nl80211_shared_freq(void *priv)
   10803             : {
   10804           0 :         struct i802_bss *bss = priv;
   10805           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10806             :         struct wpa_driver_nl80211_data *driver;
   10807           0 :         int freq = 0;
   10808             : 
   10809             :         /*
   10810             :          * If the same PHY is in connected state with some other interface,
   10811             :          * then retrieve the assoc freq.
   10812             :          */
   10813           0 :         wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
   10814           0 :                    drv->phyname);
   10815             : 
   10816           0 :         dl_list_for_each(driver, &drv->global->interfaces,
   10817             :                          struct wpa_driver_nl80211_data, list) {
   10818           0 :                 if (drv == driver ||
   10819           0 :                     os_strcmp(drv->phyname, driver->phyname) != 0 ||
   10820           0 :                     !driver->associated)
   10821           0 :                         continue;
   10822             : 
   10823           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
   10824             :                            MACSTR,
   10825           0 :                            driver->phyname, driver->first_bss->ifname,
   10826           0 :                            MAC2STR(driver->first_bss->addr));
   10827           0 :                 if (is_ap_interface(driver->nlmode))
   10828           0 :                         freq = driver->first_bss->freq;
   10829             :                 else
   10830           0 :                         freq = nl80211_get_assoc_freq(driver);
   10831           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
   10832           0 :                            drv->phyname, freq);
   10833             :         }
   10834             : 
   10835           0 :         if (!freq)
   10836           0 :                 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
   10837           0 :                            "PHY (%s) in associated state", drv->phyname);
   10838             : 
   10839           0 :         return freq;
   10840             : }
   10841             : 
   10842             : 
   10843           2 : static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
   10844             :                               int encrypt)
   10845             : {
   10846           2 :         struct i802_bss *bss = priv;
   10847           2 :         return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
   10848             :                                              0, 0, 0, 0);
   10849             : }
   10850             : 
   10851             : 
   10852           0 : static int nl80211_set_param(void *priv, const char *param)
   10853             : {
   10854           0 :         wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
   10855           0 :         if (param == NULL)
   10856           0 :                 return 0;
   10857             : 
   10858             : #ifdef CONFIG_P2P
   10859             :         if (os_strstr(param, "use_p2p_group_interface=1")) {
   10860             :                 struct i802_bss *bss = priv;
   10861             :                 struct wpa_driver_nl80211_data *drv = bss->drv;
   10862             : 
   10863             :                 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
   10864             :                            "interface");
   10865             :                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
   10866             :                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
   10867             :         }
   10868             : 
   10869             :         if (os_strstr(param, "p2p_device=1")) {
   10870             :                 struct i802_bss *bss = priv;
   10871             :                 struct wpa_driver_nl80211_data *drv = bss->drv;
   10872             :                 drv->allow_p2p_device = 1;
   10873             :         }
   10874             : #endif /* CONFIG_P2P */
   10875             : 
   10876           0 :         if (os_strstr(param, "use_monitor=1")) {
   10877           0 :                 struct i802_bss *bss = priv;
   10878           0 :                 struct wpa_driver_nl80211_data *drv = bss->drv;
   10879           0 :                 drv->use_monitor = 1;
   10880             :         }
   10881             : 
   10882           0 :         if (os_strstr(param, "force_connect_cmd=1")) {
   10883           0 :                 struct i802_bss *bss = priv;
   10884           0 :                 struct wpa_driver_nl80211_data *drv = bss->drv;
   10885           0 :                 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
   10886             :         }
   10887             : 
   10888           0 :         if (os_strstr(param, "no_offchannel_tx=1")) {
   10889           0 :                 struct i802_bss *bss = priv;
   10890           0 :                 struct wpa_driver_nl80211_data *drv = bss->drv;
   10891           0 :                 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
   10892           0 :                 drv->test_use_roc_tx = 1;
   10893             :         }
   10894             : 
   10895           0 :         return 0;
   10896             : }
   10897             : 
   10898             : 
   10899           1 : static void * nl80211_global_init(void)
   10900             : {
   10901             :         struct nl80211_global *global;
   10902             :         struct netlink_config *cfg;
   10903             : 
   10904           1 :         global = os_zalloc(sizeof(*global));
   10905           1 :         if (global == NULL)
   10906           0 :                 return NULL;
   10907           1 :         global->ioctl_sock = -1;
   10908           1 :         dl_list_init(&global->interfaces);
   10909           1 :         global->if_add_ifindex = -1;
   10910             : 
   10911           1 :         cfg = os_zalloc(sizeof(*cfg));
   10912           1 :         if (cfg == NULL)
   10913           0 :                 goto err;
   10914             : 
   10915           1 :         cfg->ctx = global;
   10916           1 :         cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
   10917           1 :         cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
   10918           1 :         global->netlink = netlink_init(cfg);
   10919           1 :         if (global->netlink == NULL) {
   10920           0 :                 os_free(cfg);
   10921           0 :                 goto err;
   10922             :         }
   10923             : 
   10924           1 :         if (wpa_driver_nl80211_init_nl_global(global) < 0)
   10925           0 :                 goto err;
   10926             : 
   10927           1 :         global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
   10928           1 :         if (global->ioctl_sock < 0) {
   10929           0 :                 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
   10930           0 :                            strerror(errno));
   10931           0 :                 goto err;
   10932             :         }
   10933             : 
   10934           1 :         return global;
   10935             : 
   10936             : err:
   10937           0 :         nl80211_global_deinit(global);
   10938           0 :         return NULL;
   10939             : }
   10940             : 
   10941             : 
   10942           1 : static void nl80211_global_deinit(void *priv)
   10943             : {
   10944           1 :         struct nl80211_global *global = priv;
   10945           1 :         if (global == NULL)
   10946           1 :                 return;
   10947           1 :         if (!dl_list_empty(&global->interfaces)) {
   10948           0 :                 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
   10949             :                            "nl80211_global_deinit",
   10950             :                            dl_list_len(&global->interfaces));
   10951             :         }
   10952             : 
   10953           1 :         if (global->netlink)
   10954           1 :                 netlink_deinit(global->netlink);
   10955             : 
   10956           1 :         nl_destroy_handles(&global->nl);
   10957             : 
   10958           1 :         if (global->nl_event)
   10959           1 :                 nl80211_destroy_eloop_handle(&global->nl_event);
   10960             : 
   10961           1 :         nl_cb_put(global->nl_cb);
   10962             : 
   10963           1 :         if (global->ioctl_sock >= 0)
   10964           1 :                 close(global->ioctl_sock);
   10965             : 
   10966           1 :         os_free(global);
   10967             : }
   10968             : 
   10969             : 
   10970         565 : static const char * nl80211_get_radio_name(void *priv)
   10971             : {
   10972         565 :         struct i802_bss *bss = priv;
   10973         565 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10974         565 :         return drv->phyname;
   10975             : }
   10976             : 
   10977             : 
   10978           0 : static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
   10979             :                          const u8 *pmkid)
   10980             : {
   10981             :         struct nl_msg *msg;
   10982             : 
   10983           0 :         msg = nlmsg_alloc();
   10984           0 :         if (!msg)
   10985           0 :                 return -ENOMEM;
   10986             : 
   10987           0 :         nl80211_cmd(bss->drv, msg, 0, cmd);
   10988             : 
   10989           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
   10990           0 :         if (pmkid)
   10991           0 :                 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
   10992           0 :         if (bssid)
   10993           0 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
   10994             : 
   10995           0 :         return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
   10996             :  nla_put_failure:
   10997           0 :         nlmsg_free(msg);
   10998           0 :         return -ENOBUFS;
   10999             : }
   11000             : 
   11001             : 
   11002           0 : static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
   11003             : {
   11004           0 :         struct i802_bss *bss = priv;
   11005           0 :         wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
   11006           0 :         return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
   11007             : }
   11008             : 
   11009             : 
   11010           0 : static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
   11011             : {
   11012           0 :         struct i802_bss *bss = priv;
   11013           0 :         wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
   11014           0 :                    MAC2STR(bssid));
   11015           0 :         return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
   11016             : }
   11017             : 
   11018             : 
   11019           0 : static int nl80211_flush_pmkid(void *priv)
   11020             : {
   11021           0 :         struct i802_bss *bss = priv;
   11022           0 :         wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
   11023           0 :         return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
   11024             : }
   11025             : 
   11026             : 
   11027          35 : static void clean_survey_results(struct survey_results *survey_results)
   11028             : {
   11029             :         struct freq_survey *survey, *tmp;
   11030             : 
   11031          35 :         if (dl_list_empty(&survey_results->survey_list))
   11032          55 :                 return;
   11033             : 
   11034          30 :         dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
   11035             :                               struct freq_survey, list) {
   11036          15 :                 dl_list_del(&survey->list);
   11037          15 :                 os_free(survey);
   11038             :         }
   11039             : }
   11040             : 
   11041             : 
   11042          35 : static void add_survey(struct nlattr **sinfo, u32 ifidx,
   11043             :                        struct dl_list *survey_list)
   11044             : {
   11045             :         struct freq_survey *survey;
   11046             : 
   11047          35 :         survey = os_zalloc(sizeof(struct freq_survey));
   11048          35 :         if  (!survey)
   11049          35 :                 return;
   11050             : 
   11051          35 :         survey->ifidx = ifidx;
   11052          35 :         survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
   11053          35 :         survey->filled = 0;
   11054             : 
   11055          35 :         if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
   11056          35 :                 survey->nf = (int8_t)
   11057          35 :                         nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
   11058          35 :                 survey->filled |= SURVEY_HAS_NF;
   11059             :         }
   11060             : 
   11061          35 :         if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
   11062           0 :                 survey->channel_time =
   11063           0 :                         nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
   11064           0 :                 survey->filled |= SURVEY_HAS_CHAN_TIME;
   11065             :         }
   11066             : 
   11067          35 :         if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
   11068           0 :                 survey->channel_time_busy =
   11069           0 :                         nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
   11070           0 :                 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
   11071             :         }
   11072             : 
   11073          35 :         if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
   11074           0 :                 survey->channel_time_rx =
   11075           0 :                         nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
   11076           0 :                 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
   11077             :         }
   11078             : 
   11079          35 :         if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
   11080           0 :                 survey->channel_time_tx =
   11081           0 :                         nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
   11082           0 :                 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
   11083             :         }
   11084             : 
   11085          70 :         wpa_printf(MSG_DEBUG, "nl80211: Freq survey dump event (freq=%d MHz noise=%d channel_time=%ld busy_time=%ld tx_time=%ld rx_time=%ld filled=%04x)",
   11086             :                    survey->freq,
   11087          35 :                    survey->nf,
   11088             :                    (unsigned long int) survey->channel_time,
   11089             :                    (unsigned long int) survey->channel_time_busy,
   11090             :                    (unsigned long int) survey->channel_time_tx,
   11091             :                    (unsigned long int) survey->channel_time_rx,
   11092             :                    survey->filled);
   11093             : 
   11094          35 :         dl_list_add_tail(survey_list, &survey->list);
   11095             : }
   11096             : 
   11097             : 
   11098          35 : static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
   11099             :                            unsigned int freq_filter)
   11100             : {
   11101          35 :         if (!freq_filter)
   11102          35 :                 return 1;
   11103             : 
   11104           0 :         return freq_filter == surveyed_freq;
   11105             : }
   11106             : 
   11107             : 
   11108          35 : static int survey_handler(struct nl_msg *msg, void *arg)
   11109             : {
   11110             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
   11111          35 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
   11112             :         struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
   11113             :         struct survey_results *survey_results;
   11114          35 :         u32 surveyed_freq = 0;
   11115             :         u32 ifidx;
   11116             : 
   11117             :         static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
   11118             :                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
   11119             :                 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
   11120             :         };
   11121             : 
   11122          35 :         survey_results = (struct survey_results *) arg;
   11123             : 
   11124          35 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
   11125             :                   genlmsg_attrlen(gnlh, 0), NULL);
   11126             : 
   11127          35 :         if (!tb[NL80211_ATTR_IFINDEX])
   11128           0 :                 return NL_SKIP;
   11129             : 
   11130          35 :         ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
   11131             : 
   11132          35 :         if (!tb[NL80211_ATTR_SURVEY_INFO])
   11133           0 :                 return NL_SKIP;
   11134             : 
   11135          35 :         if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
   11136             :                              tb[NL80211_ATTR_SURVEY_INFO],
   11137             :                              survey_policy))
   11138           0 :                 return NL_SKIP;
   11139             : 
   11140          35 :         if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
   11141           0 :                 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
   11142           0 :                 return NL_SKIP;
   11143             :         }
   11144             : 
   11145          35 :         surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
   11146             : 
   11147          35 :         if (!check_survey_ok(sinfo, surveyed_freq,
   11148             :                              survey_results->freq_filter))
   11149           0 :                 return NL_SKIP;
   11150             : 
   11151          35 :         if (survey_results->freq_filter &&
   11152           0 :             survey_results->freq_filter != surveyed_freq) {
   11153           0 :                 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
   11154             :                            surveyed_freq);
   11155           0 :                 return NL_SKIP;
   11156             :         }
   11157             : 
   11158          35 :         add_survey(sinfo, ifidx, &survey_results->survey_list);
   11159             : 
   11160          35 :         return NL_SKIP;
   11161             : }
   11162             : 
   11163             : 
   11164          35 : static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
   11165             : {
   11166          35 :         struct i802_bss *bss = priv;
   11167          35 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11168             :         struct nl_msg *msg;
   11169          35 :         int err = -ENOBUFS;
   11170             :         union wpa_event_data data;
   11171             :         struct survey_results *survey_results;
   11172             : 
   11173          35 :         os_memset(&data, 0, sizeof(data));
   11174          35 :         survey_results = &data.survey_results;
   11175             : 
   11176          35 :         dl_list_init(&survey_results->survey_list);
   11177             : 
   11178          35 :         msg = nlmsg_alloc();
   11179          35 :         if (!msg)
   11180           0 :                 goto nla_put_failure;
   11181             : 
   11182          35 :         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
   11183             : 
   11184          35 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   11185             : 
   11186          35 :         if (freq)
   11187           0 :                 data.survey_results.freq_filter = freq;
   11188             : 
   11189             :         do {
   11190          35 :                 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
   11191          35 :                 err = send_and_recv_msgs(drv, msg, survey_handler,
   11192             :                                          survey_results);
   11193          35 :         } while (err > 0);
   11194             : 
   11195          35 :         if (err) {
   11196           0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
   11197           0 :                 goto out_clean;
   11198             :         }
   11199             : 
   11200          35 :         wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
   11201             : 
   11202             : out_clean:
   11203          35 :         clean_survey_results(survey_results);
   11204             : nla_put_failure:
   11205          35 :         return err;
   11206             : }
   11207             : 
   11208             : 
   11209           0 : static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
   11210             :                                    const u8 *replay_ctr)
   11211             : {
   11212           0 :         struct i802_bss *bss = priv;
   11213           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11214             :         struct nlattr *replay_nested;
   11215             :         struct nl_msg *msg;
   11216             : 
   11217           0 :         msg = nlmsg_alloc();
   11218           0 :         if (!msg)
   11219           0 :                 return;
   11220             : 
   11221           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
   11222             : 
   11223           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
   11224             : 
   11225           0 :         replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
   11226           0 :         if (!replay_nested)
   11227           0 :                 goto nla_put_failure;
   11228             : 
   11229           0 :         NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
   11230           0 :         NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
   11231           0 :         NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
   11232             :                 replay_ctr);
   11233             : 
   11234           0 :         nla_nest_end(msg, replay_nested);
   11235             : 
   11236           0 :         send_and_recv_msgs(drv, msg, NULL, NULL);
   11237           0 :         return;
   11238             :  nla_put_failure:
   11239           0 :         nlmsg_free(msg);
   11240             : }
   11241             : 
   11242             : 
   11243           0 : static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
   11244             :                                     const u8 *addr, int qos)
   11245             : {
   11246             :         /* send data frame to poll STA and check whether
   11247             :          * this frame is ACKed */
   11248             :         struct {
   11249             :                 struct ieee80211_hdr hdr;
   11250             :                 u16 qos_ctl;
   11251             :         } STRUCT_PACKED nulldata;
   11252             :         size_t size;
   11253             : 
   11254             :         /* Send data frame to poll STA and check whether this frame is ACKed */
   11255             : 
   11256           0 :         os_memset(&nulldata, 0, sizeof(nulldata));
   11257             : 
   11258           0 :         if (qos) {
   11259           0 :                 nulldata.hdr.frame_control =
   11260             :                         IEEE80211_FC(WLAN_FC_TYPE_DATA,
   11261             :                                      WLAN_FC_STYPE_QOS_NULL);
   11262           0 :                 size = sizeof(nulldata);
   11263             :         } else {
   11264           0 :                 nulldata.hdr.frame_control =
   11265             :                         IEEE80211_FC(WLAN_FC_TYPE_DATA,
   11266             :                                      WLAN_FC_STYPE_NULLFUNC);
   11267           0 :                 size = sizeof(struct ieee80211_hdr);
   11268             :         }
   11269             : 
   11270           0 :         nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
   11271           0 :         os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
   11272           0 :         os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
   11273           0 :         os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
   11274             : 
   11275           0 :         if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
   11276             :                                          0, 0) < 0)
   11277           0 :                 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
   11278             :                            "send poll frame");
   11279           0 : }
   11280             : 
   11281           1 : static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
   11282             :                                 int qos)
   11283             : {
   11284           1 :         struct i802_bss *bss = priv;
   11285           1 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11286             :         struct nl_msg *msg;
   11287             : 
   11288           1 :         if (!drv->poll_command_supported) {
   11289           0 :                 nl80211_send_null_frame(bss, own_addr, addr, qos);
   11290           0 :                 return;
   11291             :         }
   11292             : 
   11293           1 :         msg = nlmsg_alloc();
   11294           1 :         if (!msg)
   11295           0 :                 return;
   11296             : 
   11297           1 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
   11298             : 
   11299           1 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
   11300           1 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
   11301             : 
   11302           1 :         send_and_recv_msgs(drv, msg, NULL, NULL);
   11303           1 :         return;
   11304             :  nla_put_failure:
   11305           0 :         nlmsg_free(msg);
   11306             : }
   11307             : 
   11308             : 
   11309           0 : static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
   11310             : {
   11311             :         struct nl_msg *msg;
   11312             : 
   11313           0 :         msg = nlmsg_alloc();
   11314           0 :         if (!msg)
   11315           0 :                 return -ENOMEM;
   11316             : 
   11317           0 :         nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
   11318           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
   11319           0 :         NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
   11320             :                     enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
   11321           0 :         return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
   11322             : nla_put_failure:
   11323           0 :         nlmsg_free(msg);
   11324           0 :         return -ENOBUFS;
   11325             : }
   11326             : 
   11327             : 
   11328           0 : static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
   11329             :                                      int ctwindow)
   11330             : {
   11331           0 :         struct i802_bss *bss = priv;
   11332             : 
   11333           0 :         wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
   11334             :                    "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
   11335             : 
   11336           0 :         if (opp_ps != -1 || ctwindow != -1) {
   11337             : #ifdef ANDROID_P2P
   11338             :                 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
   11339             : #else /* ANDROID_P2P */
   11340           0 :                 return -1; /* Not yet supported */
   11341             : #endif /* ANDROID_P2P */
   11342             :         }
   11343             : 
   11344           0 :         if (legacy_ps == -1)
   11345           0 :                 return 0;
   11346           0 :         if (legacy_ps != 0 && legacy_ps != 1)
   11347           0 :                 return -1; /* Not yet supported */
   11348             : 
   11349           0 :         return nl80211_set_power_save(bss, legacy_ps);
   11350             : }
   11351             : 
   11352             : 
   11353           1 : static int nl80211_start_radar_detection(void *priv,
   11354             :                                          struct hostapd_freq_params *freq)
   11355             : {
   11356           1 :         struct i802_bss *bss = priv;
   11357           1 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11358             :         struct nl_msg *msg;
   11359             :         int ret;
   11360             : 
   11361           1 :         wpa_printf(MSG_DEBUG, "nl80211: Start radar detection (CAC) %d MHz (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
   11362             :                    freq->freq, freq->ht_enabled, freq->vht_enabled,
   11363             :                    freq->bandwidth, freq->center_freq1, freq->center_freq2);
   11364             : 
   11365           1 :         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
   11366           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
   11367             :                            "detection");
   11368           0 :                 return -1;
   11369             :         }
   11370             : 
   11371           1 :         msg = nlmsg_alloc();
   11372           1 :         if (!msg)
   11373           0 :                 return -1;
   11374             : 
   11375           1 :         nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
   11376           1 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   11377           1 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
   11378             : 
   11379           1 :         if (freq->vht_enabled) {
   11380           0 :                 switch (freq->bandwidth) {
   11381             :                 case 20:
   11382           0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
   11383             :                                     NL80211_CHAN_WIDTH_20);
   11384           0 :                         break;
   11385             :                 case 40:
   11386           0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
   11387             :                                     NL80211_CHAN_WIDTH_40);
   11388           0 :                         break;
   11389             :                 case 80:
   11390           0 :                         if (freq->center_freq2)
   11391           0 :                                 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
   11392             :                                             NL80211_CHAN_WIDTH_80P80);
   11393             :                         else
   11394           0 :                                 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
   11395             :                                             NL80211_CHAN_WIDTH_80);
   11396           0 :                         break;
   11397             :                 case 160:
   11398           0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
   11399             :                                     NL80211_CHAN_WIDTH_160);
   11400           0 :                         break;
   11401             :                 default:
   11402           0 :                         return -1;
   11403             :                 }
   11404           0 :                 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
   11405           0 :                 if (freq->center_freq2)
   11406           0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
   11407             :                                     freq->center_freq2);
   11408           1 :         } else if (freq->ht_enabled) {
   11409           1 :                 switch (freq->sec_channel_offset) {
   11410             :                 case -1:
   11411           0 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
   11412             :                                     NL80211_CHAN_HT40MINUS);
   11413           0 :                         break;
   11414             :                 case 1:
   11415           0 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
   11416             :                                     NL80211_CHAN_HT40PLUS);
   11417           0 :                         break;
   11418             :                 default:
   11419           1 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
   11420             :                                     NL80211_CHAN_HT20);
   11421           1 :                         break;
   11422             :                 }
   11423             :         }
   11424             : 
   11425           1 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   11426           1 :         if (ret == 0)
   11427           0 :                 return 0;
   11428           1 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
   11429             :                    "%d (%s)", ret, strerror(-ret));
   11430             : nla_put_failure:
   11431           1 :         return -1;
   11432             : }
   11433             : 
   11434             : #ifdef CONFIG_TDLS
   11435             : 
   11436             : static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
   11437             :                                   u8 dialog_token, u16 status_code,
   11438             :                                   u32 peer_capab, const u8 *buf, size_t len)
   11439             : {
   11440             :         struct i802_bss *bss = priv;
   11441             :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11442             :         struct nl_msg *msg;
   11443             : 
   11444             :         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
   11445             :                 return -EOPNOTSUPP;
   11446             : 
   11447             :         if (!dst)
   11448             :                 return -EINVAL;
   11449             : 
   11450             :         msg = nlmsg_alloc();
   11451             :         if (!msg)
   11452             :                 return -ENOMEM;
   11453             : 
   11454             :         nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
   11455             :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   11456             :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
   11457             :         NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
   11458             :         NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
   11459             :         NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
   11460             :         if (peer_capab) {
   11461             :                 /*
   11462             :                  * The internal enum tdls_peer_capability definition is
   11463             :                  * currently identical with the nl80211 enum
   11464             :                  * nl80211_tdls_peer_capability, so no conversion is needed
   11465             :                  * here.
   11466             :                  */
   11467             :                 NLA_PUT_U32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY, peer_capab);
   11468             :         }
   11469             :         NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
   11470             : 
   11471             :         return send_and_recv_msgs(drv, msg, NULL, NULL);
   11472             : 
   11473             : nla_put_failure:
   11474             :         nlmsg_free(msg);
   11475             :         return -ENOBUFS;
   11476             : }
   11477             : 
   11478             : 
   11479             : static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
   11480             : {
   11481             :         struct i802_bss *bss = priv;
   11482             :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11483             :         struct nl_msg *msg;
   11484             :         enum nl80211_tdls_operation nl80211_oper;
   11485             : 
   11486             :         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
   11487             :                 return -EOPNOTSUPP;
   11488             : 
   11489             :         switch (oper) {
   11490             :         case TDLS_DISCOVERY_REQ:
   11491             :                 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
   11492             :                 break;
   11493             :         case TDLS_SETUP:
   11494             :                 nl80211_oper = NL80211_TDLS_SETUP;
   11495             :                 break;
   11496             :         case TDLS_TEARDOWN:
   11497             :                 nl80211_oper = NL80211_TDLS_TEARDOWN;
   11498             :                 break;
   11499             :         case TDLS_ENABLE_LINK:
   11500             :                 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
   11501             :                 break;
   11502             :         case TDLS_DISABLE_LINK:
   11503             :                 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
   11504             :                 break;
   11505             :         case TDLS_ENABLE:
   11506             :                 return 0;
   11507             :         case TDLS_DISABLE:
   11508             :                 return 0;
   11509             :         default:
   11510             :                 return -EINVAL;
   11511             :         }
   11512             : 
   11513             :         msg = nlmsg_alloc();
   11514             :         if (!msg)
   11515             :                 return -ENOMEM;
   11516             : 
   11517             :         nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
   11518             :         NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
   11519             :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   11520             :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
   11521             : 
   11522             :         return send_and_recv_msgs(drv, msg, NULL, NULL);
   11523             : 
   11524             : nla_put_failure:
   11525             :         nlmsg_free(msg);
   11526             :         return -ENOBUFS;
   11527             : }
   11528             : 
   11529             : #endif /* CONFIG TDLS */
   11530             : 
   11531             : 
   11532             : #ifdef ANDROID
   11533             : 
   11534             : typedef struct android_wifi_priv_cmd {
   11535             :         char *buf;
   11536             :         int used_len;
   11537             :         int total_len;
   11538             : } android_wifi_priv_cmd;
   11539             : 
   11540             : static int drv_errors = 0;
   11541             : 
   11542             : static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
   11543             : {
   11544             :         drv_errors++;
   11545             :         if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
   11546             :                 drv_errors = 0;
   11547             :                 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
   11548             :         }
   11549             : }
   11550             : 
   11551             : 
   11552             : static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
   11553             : {
   11554             :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11555             :         struct ifreq ifr;
   11556             :         android_wifi_priv_cmd priv_cmd;
   11557             :         char buf[MAX_DRV_CMD_SIZE];
   11558             :         int ret;
   11559             : 
   11560             :         os_memset(&ifr, 0, sizeof(ifr));
   11561             :         os_memset(&priv_cmd, 0, sizeof(priv_cmd));
   11562             :         os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
   11563             : 
   11564             :         os_memset(buf, 0, sizeof(buf));
   11565             :         os_strlcpy(buf, cmd, sizeof(buf));
   11566             : 
   11567             :         priv_cmd.buf = buf;
   11568             :         priv_cmd.used_len = sizeof(buf);
   11569             :         priv_cmd.total_len = sizeof(buf);
   11570             :         ifr.ifr_data = &priv_cmd;
   11571             : 
   11572             :         ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
   11573             :         if (ret < 0) {
   11574             :                 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
   11575             :                            __func__);
   11576             :                 wpa_driver_send_hang_msg(drv);
   11577             :                 return ret;
   11578             :         }
   11579             : 
   11580             :         drv_errors = 0;
   11581             :         return 0;
   11582             : }
   11583             : 
   11584             : 
   11585             : static int android_pno_start(struct i802_bss *bss,
   11586             :                              struct wpa_driver_scan_params *params)
   11587             : {
   11588             :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11589             :         struct ifreq ifr;
   11590             :         android_wifi_priv_cmd priv_cmd;
   11591             :         int ret = 0, i = 0, bp;
   11592             :         char buf[WEXT_PNO_MAX_COMMAND_SIZE];
   11593             : 
   11594             :         bp = WEXT_PNOSETUP_HEADER_SIZE;
   11595             :         os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
   11596             :         buf[bp++] = WEXT_PNO_TLV_PREFIX;
   11597             :         buf[bp++] = WEXT_PNO_TLV_VERSION;
   11598             :         buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
   11599             :         buf[bp++] = WEXT_PNO_TLV_RESERVED;
   11600             : 
   11601             :         while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
   11602             :                 /* Check that there is enough space needed for 1 more SSID, the
   11603             :                  * other sections and null termination */
   11604             :                 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
   11605             :                      WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
   11606             :                         break;
   11607             :                 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
   11608             :                                   params->ssids[i].ssid,
   11609             :                                   params->ssids[i].ssid_len);
   11610             :                 buf[bp++] = WEXT_PNO_SSID_SECTION;
   11611             :                 buf[bp++] = params->ssids[i].ssid_len;
   11612             :                 os_memcpy(&buf[bp], params->ssids[i].ssid,
   11613             :                           params->ssids[i].ssid_len);
   11614             :                 bp += params->ssids[i].ssid_len;
   11615             :                 i++;
   11616             :         }
   11617             : 
   11618             :         buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
   11619             :         os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
   11620             :                     WEXT_PNO_SCAN_INTERVAL);
   11621             :         bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
   11622             : 
   11623             :         buf[bp++] = WEXT_PNO_REPEAT_SECTION;
   11624             :         os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
   11625             :                     WEXT_PNO_REPEAT);
   11626             :         bp += WEXT_PNO_REPEAT_LENGTH;
   11627             : 
   11628             :         buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
   11629             :         os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
   11630             :                     WEXT_PNO_MAX_REPEAT);
   11631             :         bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
   11632             : 
   11633             :         memset(&ifr, 0, sizeof(ifr));
   11634             :         memset(&priv_cmd, 0, sizeof(priv_cmd));
   11635             :         os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
   11636             : 
   11637             :         priv_cmd.buf = buf;
   11638             :         priv_cmd.used_len = bp;
   11639             :         priv_cmd.total_len = bp;
   11640             :         ifr.ifr_data = &priv_cmd;
   11641             : 
   11642             :         ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
   11643             : 
   11644             :         if (ret < 0) {
   11645             :                 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
   11646             :                            ret);
   11647             :                 wpa_driver_send_hang_msg(drv);
   11648             :                 return ret;
   11649             :         }
   11650             : 
   11651             :         drv_errors = 0;
   11652             : 
   11653             :         return android_priv_cmd(bss, "PNOFORCE 1");
   11654             : }
   11655             : 
   11656             : 
   11657             : static int android_pno_stop(struct i802_bss *bss)
   11658             : {
   11659             :         return android_priv_cmd(bss, "PNOFORCE 0");
   11660             : }
   11661             : 
   11662             : #endif /* ANDROID */
   11663             : 
   11664             : 
   11665        7696 : static int driver_nl80211_set_key(const char *ifname, void *priv,
   11666             :                                   enum wpa_alg alg, const u8 *addr,
   11667             :                                   int key_idx, int set_tx,
   11668             :                                   const u8 *seq, size_t seq_len,
   11669             :                                   const u8 *key, size_t key_len)
   11670             : {
   11671        7696 :         struct i802_bss *bss = priv;
   11672        7696 :         return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
   11673             :                                           set_tx, seq, seq_len, key, key_len);
   11674             : }
   11675             : 
   11676             : 
   11677          64 : static int driver_nl80211_scan2(void *priv,
   11678             :                                 struct wpa_driver_scan_params *params)
   11679             : {
   11680          64 :         struct i802_bss *bss = priv;
   11681          64 :         return wpa_driver_nl80211_scan(bss, params);
   11682             : }
   11683             : 
   11684             : 
   11685           0 : static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
   11686             :                                          int reason_code)
   11687             : {
   11688           0 :         struct i802_bss *bss = priv;
   11689           0 :         return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
   11690             : }
   11691             : 
   11692             : 
   11693           0 : static int driver_nl80211_authenticate(void *priv,
   11694             :                                        struct wpa_driver_auth_params *params)
   11695             : {
   11696           0 :         struct i802_bss *bss = priv;
   11697           0 :         return wpa_driver_nl80211_authenticate(bss, params);
   11698             : }
   11699             : 
   11700             : 
   11701           0 : static void driver_nl80211_deinit(void *priv)
   11702             : {
   11703           0 :         struct i802_bss *bss = priv;
   11704           0 :         wpa_driver_nl80211_deinit(bss);
   11705           0 : }
   11706             : 
   11707             : 
   11708          30 : static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
   11709             :                                     const char *ifname)
   11710             : {
   11711          30 :         struct i802_bss *bss = priv;
   11712          30 :         return wpa_driver_nl80211_if_remove(bss, type, ifname);
   11713             : }
   11714             : 
   11715             : 
   11716        3559 : static int driver_nl80211_send_mlme(void *priv, const u8 *data,
   11717             :                                     size_t data_len, int noack)
   11718             : {
   11719        3559 :         struct i802_bss *bss = priv;
   11720        3559 :         return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
   11721             :                                             0, 0, 0, 0);
   11722             : }
   11723             : 
   11724             : 
   11725        1744 : static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
   11726             : {
   11727        1744 :         struct i802_bss *bss = priv;
   11728        1744 :         return wpa_driver_nl80211_sta_remove(bss, addr);
   11729             : }
   11730             : 
   11731             : 
   11732          12 : static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
   11733             :                                        const char *ifname, int vlan_id)
   11734             : {
   11735          12 :         struct i802_bss *bss = priv;
   11736          12 :         return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
   11737             : }
   11738             : 
   11739             : 
   11740          43 : static int driver_nl80211_read_sta_data(void *priv,
   11741             :                                         struct hostap_sta_driver_data *data,
   11742             :                                         const u8 *addr)
   11743             : {
   11744          43 :         struct i802_bss *bss = priv;
   11745          43 :         return i802_read_sta_data(bss, data, addr);
   11746             : }
   11747             : 
   11748             : 
   11749         238 : static int driver_nl80211_send_action(void *priv, unsigned int freq,
   11750             :                                       unsigned int wait_time,
   11751             :                                       const u8 *dst, const u8 *src,
   11752             :                                       const u8 *bssid,
   11753             :                                       const u8 *data, size_t data_len,
   11754             :                                       int no_cck)
   11755             : {
   11756         238 :         struct i802_bss *bss = priv;
   11757         238 :         return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
   11758             :                                               bssid, data, data_len, no_cck);
   11759             : }
   11760             : 
   11761             : 
   11762           0 : static int driver_nl80211_probe_req_report(void *priv, int report)
   11763             : {
   11764           0 :         struct i802_bss *bss = priv;
   11765           0 :         return wpa_driver_nl80211_probe_req_report(bss, report);
   11766             : }
   11767             : 
   11768             : 
   11769           0 : static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
   11770             :                                             const u8 *ies, size_t ies_len)
   11771             : {
   11772             :         int ret;
   11773             :         struct nl_msg *msg;
   11774           0 :         struct i802_bss *bss = priv;
   11775           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11776           0 :         u16 mdid = WPA_GET_LE16(md);
   11777             : 
   11778           0 :         msg = nlmsg_alloc();
   11779           0 :         if (!msg)
   11780           0 :                 return -ENOMEM;
   11781             : 
   11782           0 :         wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
   11783           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
   11784           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   11785           0 :         NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
   11786           0 :         NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
   11787             : 
   11788           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   11789           0 :         if (ret) {
   11790           0 :                 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
   11791             :                            "err=%d (%s)", ret, strerror(-ret));
   11792             :         }
   11793             : 
   11794           0 :         return ret;
   11795             : 
   11796             : nla_put_failure:
   11797           0 :         nlmsg_free(msg);
   11798           0 :         return -ENOBUFS;
   11799             : }
   11800             : 
   11801             : 
   11802           0 : const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
   11803             : {
   11804           0 :         struct i802_bss *bss = priv;
   11805           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11806             : 
   11807           0 :         if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
   11808           0 :                 return NULL;
   11809             : 
   11810           0 :         return bss->addr;
   11811             : }
   11812             : 
   11813             : 
   11814           2 : static const char * scan_state_str(enum scan_states scan_state)
   11815             : {
   11816           2 :         switch (scan_state) {
   11817             :         case NO_SCAN:
   11818           2 :                 return "NO_SCAN";
   11819             :         case SCAN_REQUESTED:
   11820           0 :                 return "SCAN_REQUESTED";
   11821             :         case SCAN_STARTED:
   11822           0 :                 return "SCAN_STARTED";
   11823             :         case SCAN_COMPLETED:
   11824           0 :                 return "SCAN_COMPLETED";
   11825             :         case SCAN_ABORTED:
   11826           0 :                 return "SCAN_ABORTED";
   11827             :         case SCHED_SCAN_STARTED:
   11828           0 :                 return "SCHED_SCAN_STARTED";
   11829             :         case SCHED_SCAN_STOPPED:
   11830           0 :                 return "SCHED_SCAN_STOPPED";
   11831             :         case SCHED_SCAN_RESULTS:
   11832           0 :                 return "SCHED_SCAN_RESULTS";
   11833             :         }
   11834             : 
   11835           0 :         return "??";
   11836             : }
   11837             : 
   11838             : 
   11839           2 : static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
   11840             : {
   11841           2 :         struct i802_bss *bss = priv;
   11842           2 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11843             :         int res;
   11844             :         char *pos, *end;
   11845             : 
   11846           2 :         pos = buf;
   11847           2 :         end = buf + buflen;
   11848             : 
   11849          24 :         res = os_snprintf(pos, end - pos,
   11850             :                           "ifindex=%d\n"
   11851             :                           "ifname=%s\n"
   11852             :                           "brname=%s\n"
   11853             :                           "addr=" MACSTR "\n"
   11854             :                           "freq=%d\n"
   11855             :                           "%s%s%s%s%s",
   11856             :                           bss->ifindex,
   11857           2 :                           bss->ifname,
   11858           2 :                           bss->brname,
   11859          12 :                           MAC2STR(bss->addr),
   11860             :                           bss->freq,
   11861           2 :                           bss->beacon_set ? "beacon_set=1\n" : "",
   11862           2 :                           bss->added_if_into_bridge ?
   11863             :                           "added_if_into_bridge=1\n" : "",
   11864           2 :                           bss->added_bridge ? "added_bridge=1\n" : "",
   11865           2 :                           bss->in_deinit ? "in_deinit=1\n" : "",
   11866           2 :                           bss->if_dynamic ? "if_dynamic=1\n" : "");
   11867           2 :         if (res < 0 || res >= end - pos)
   11868           0 :                 return pos - buf;
   11869           2 :         pos += res;
   11870             : 
   11871           2 :         if (bss->wdev_id_set) {
   11872           0 :                 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
   11873           0 :                                   (unsigned long long) bss->wdev_id);
   11874           0 :                 if (res < 0 || res >= end - pos)
   11875           0 :                         return pos - buf;
   11876           0 :                 pos += res;
   11877             :         }
   11878             : 
   11879          78 :         res = os_snprintf(pos, end - pos,
   11880             :                           "phyname=%s\n"
   11881             :                           "drv_ifindex=%d\n"
   11882             :                           "operstate=%d\n"
   11883             :                           "scan_state=%s\n"
   11884             :                           "auth_bssid=" MACSTR "\n"
   11885             :                           "auth_attempt_bssid=" MACSTR "\n"
   11886             :                           "bssid=" MACSTR "\n"
   11887             :                           "prev_bssid=" MACSTR "\n"
   11888             :                           "associated=%d\n"
   11889             :                           "assoc_freq=%u\n"
   11890             :                           "monitor_sock=%d\n"
   11891             :                           "monitor_ifidx=%d\n"
   11892             :                           "monitor_refcount=%d\n"
   11893             :                           "last_mgmt_freq=%u\n"
   11894             :                           "eapol_tx_sock=%d\n"
   11895             :                           "%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
   11896           2 :                           drv->phyname,
   11897             :                           drv->ifindex,
   11898             :                           drv->operstate,
   11899             :                           scan_state_str(drv->scan_state),
   11900          12 :                           MAC2STR(drv->auth_bssid),
   11901          12 :                           MAC2STR(drv->auth_attempt_bssid),
   11902          12 :                           MAC2STR(drv->bssid),
   11903          12 :                           MAC2STR(drv->prev_bssid),
   11904             :                           drv->associated,
   11905             :                           drv->assoc_freq,
   11906             :                           drv->monitor_sock,
   11907             :                           drv->monitor_ifidx,
   11908             :                           drv->monitor_refcount,
   11909             :                           drv->last_mgmt_freq,
   11910             :                           drv->eapol_tx_sock,
   11911           2 :                           drv->ignore_if_down_event ?
   11912             :                           "ignore_if_down_event=1\n" : "",
   11913           2 :                           drv->scan_complete_events ?
   11914             :                           "scan_complete_events=1\n" : "",
   11915           2 :                           drv->disabled_11b_rates ?
   11916             :                           "disabled_11b_rates=1\n" : "",
   11917           2 :                           drv->pending_remain_on_chan ?
   11918             :                           "pending_remain_on_chan=1\n" : "",
   11919           2 :                           drv->in_interface_list ? "in_interface_list=1\n" : "",
   11920           2 :                           drv->device_ap_sme ? "device_ap_sme=1\n" : "",
   11921           2 :                           drv->poll_command_supported ?
   11922             :                           "poll_command_supported=1\n" : "",
   11923           2 :                           drv->data_tx_status ? "data_tx_status=1\n" : "",
   11924           2 :                           drv->scan_for_auth ? "scan_for_auth=1\n" : "",
   11925           2 :                           drv->retry_auth ? "retry_auth=1\n" : "",
   11926           2 :                           drv->use_monitor ? "use_monitor=1\n" : "",
   11927           2 :                           drv->ignore_next_local_disconnect ?
   11928             :                           "ignore_next_local_disconnect=1\n" : "",
   11929           2 :                           drv->ignore_next_local_deauth ?
   11930             :                           "ignore_next_local_deauth=1\n" : "",
   11931           2 :                           drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
   11932           2 :         if (res < 0 || res >= end - pos)
   11933           0 :                 return pos - buf;
   11934           2 :         pos += res;
   11935             : 
   11936           2 :         if (drv->has_capability) {
   11937           2 :                 res = os_snprintf(pos, end - pos,
   11938             :                                   "capa.key_mgmt=0x%x\n"
   11939             :                                   "capa.enc=0x%x\n"
   11940             :                                   "capa.auth=0x%x\n"
   11941             :                                   "capa.flags=0x%x\n"
   11942             :                                   "capa.max_scan_ssids=%d\n"
   11943             :                                   "capa.max_sched_scan_ssids=%d\n"
   11944             :                                   "capa.sched_scan_supported=%d\n"
   11945             :                                   "capa.max_match_sets=%d\n"
   11946             :                                   "capa.max_remain_on_chan=%u\n"
   11947             :                                   "capa.max_stations=%u\n"
   11948             :                                   "capa.probe_resp_offloads=0x%x\n"
   11949             :                                   "capa.max_acl_mac_addrs=%u\n"
   11950             :                                   "capa.num_multichan_concurrent=%u\n",
   11951             :                                   drv->capa.key_mgmt,
   11952             :                                   drv->capa.enc,
   11953             :                                   drv->capa.auth,
   11954             :                                   drv->capa.flags,
   11955             :                                   drv->capa.max_scan_ssids,
   11956             :                                   drv->capa.max_sched_scan_ssids,
   11957             :                                   drv->capa.sched_scan_supported,
   11958             :                                   drv->capa.max_match_sets,
   11959             :                                   drv->capa.max_remain_on_chan,
   11960             :                                   drv->capa.max_stations,
   11961             :                                   drv->capa.probe_resp_offloads,
   11962             :                                   drv->capa.max_acl_mac_addrs,
   11963             :                                   drv->capa.num_multichan_concurrent);
   11964           2 :                 if (res < 0 || res >= end - pos)
   11965           0 :                         return pos - buf;
   11966           2 :                 pos += res;
   11967             :         }
   11968             : 
   11969           2 :         return pos - buf;
   11970             : }
   11971             : 
   11972             : 
   11973           0 : static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
   11974             : {
   11975           0 :         if (settings->head)
   11976           0 :                 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
   11977             :                         settings->head_len, settings->head);
   11978             : 
   11979           0 :         if (settings->tail)
   11980           0 :                 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
   11981             :                         settings->tail_len, settings->tail);
   11982             : 
   11983           0 :         if (settings->beacon_ies)
   11984           0 :                 NLA_PUT(msg, NL80211_ATTR_IE,
   11985             :                         settings->beacon_ies_len, settings->beacon_ies);
   11986             : 
   11987           0 :         if (settings->proberesp_ies)
   11988           0 :                 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
   11989             :                         settings->proberesp_ies_len, settings->proberesp_ies);
   11990             : 
   11991           0 :         if (settings->assocresp_ies)
   11992           0 :                 NLA_PUT(msg,
   11993             :                         NL80211_ATTR_IE_ASSOC_RESP,
   11994             :                         settings->assocresp_ies_len, settings->assocresp_ies);
   11995             : 
   11996           0 :         if (settings->probe_resp)
   11997           0 :                 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
   11998             :                         settings->probe_resp_len, settings->probe_resp);
   11999             : 
   12000           0 :         return 0;
   12001             : 
   12002             : nla_put_failure:
   12003           0 :         return -ENOBUFS;
   12004             : }
   12005             : 
   12006             : 
   12007           1 : static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
   12008             : {
   12009             :         struct nl_msg *msg;
   12010           1 :         struct i802_bss *bss = priv;
   12011           1 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   12012             :         struct nlattr *beacon_csa;
   12013           1 :         int ret = -ENOBUFS;
   12014             : 
   12015           3 :         wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
   12016           2 :                    settings->cs_count, settings->block_tx,
   12017             :                    settings->freq_params.freq, settings->freq_params.bandwidth,
   12018             :                    settings->freq_params.center_freq1,
   12019             :                    settings->freq_params.center_freq2);
   12020             : 
   12021           1 :         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
   12022           1 :                 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
   12023           1 :                 return -EOPNOTSUPP;
   12024             :         }
   12025             : 
   12026           0 :         if ((drv->nlmode != NL80211_IFTYPE_AP) &&
   12027           0 :             (drv->nlmode != NL80211_IFTYPE_P2P_GO))
   12028           0 :                 return -EOPNOTSUPP;
   12029             : 
   12030             :         /* check settings validity */
   12031           0 :         if (!settings->beacon_csa.tail ||
   12032           0 :             ((settings->beacon_csa.tail_len <=
   12033           0 :               settings->counter_offset_beacon) ||
   12034           0 :              (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
   12035           0 :               settings->cs_count)))
   12036           0 :                 return -EINVAL;
   12037             : 
   12038           0 :         if (settings->beacon_csa.probe_resp &&
   12039           0 :             ((settings->beacon_csa.probe_resp_len <=
   12040           0 :               settings->counter_offset_presp) ||
   12041           0 :              (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
   12042           0 :               settings->cs_count)))
   12043           0 :                 return -EINVAL;
   12044             : 
   12045           0 :         msg = nlmsg_alloc();
   12046           0 :         if (!msg)
   12047           0 :                 return -ENOMEM;
   12048             : 
   12049           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
   12050           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   12051           0 :         NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
   12052           0 :         ret = nl80211_put_freq_params(msg, &settings->freq_params);
   12053           0 :         if (ret)
   12054           0 :                 goto error;
   12055             : 
   12056           0 :         if (settings->block_tx)
   12057           0 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
   12058             : 
   12059             :         /* beacon_after params */
   12060           0 :         ret = set_beacon_data(msg, &settings->beacon_after);
   12061           0 :         if (ret)
   12062           0 :                 goto error;
   12063             : 
   12064             :         /* beacon_csa params */
   12065           0 :         beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
   12066           0 :         if (!beacon_csa)
   12067           0 :                 goto nla_put_failure;
   12068             : 
   12069           0 :         ret = set_beacon_data(msg, &settings->beacon_csa);
   12070           0 :         if (ret)
   12071           0 :                 goto error;
   12072             : 
   12073           0 :         NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
   12074             :                     settings->counter_offset_beacon);
   12075             : 
   12076           0 :         if (settings->beacon_csa.probe_resp)
   12077           0 :                 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
   12078             :                             settings->counter_offset_presp);
   12079             : 
   12080           0 :         nla_nest_end(msg, beacon_csa);
   12081           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   12082           0 :         if (ret) {
   12083           0 :                 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
   12084             :                            ret, strerror(-ret));
   12085             :         }
   12086           0 :         return ret;
   12087             : 
   12088             : nla_put_failure:
   12089           0 :         ret = -ENOBUFS;
   12090             : error:
   12091           0 :         nlmsg_free(msg);
   12092           0 :         wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
   12093           0 :         return ret;
   12094             : }
   12095             : 
   12096             : 
   12097             : #ifdef CONFIG_TESTING_OPTIONS
   12098           0 : static int cmd_reply_handler(struct nl_msg *msg, void *arg)
   12099             : {
   12100           0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
   12101           0 :         struct wpabuf *buf = arg;
   12102             : 
   12103           0 :         if (!buf)
   12104           0 :                 return NL_SKIP;
   12105             : 
   12106           0 :         if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
   12107           0 :                 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
   12108           0 :                 return NL_SKIP;
   12109             :         }
   12110             : 
   12111           0 :         wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
   12112           0 :                         genlmsg_attrlen(gnlh, 0));
   12113             : 
   12114           0 :         return NL_SKIP;
   12115             : }
   12116             : #endif /* CONFIG_TESTING_OPTIONS */
   12117             : 
   12118             : 
   12119           0 : static int vendor_reply_handler(struct nl_msg *msg, void *arg)
   12120             : {
   12121             :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
   12122             :         struct nlattr *nl_vendor_reply, *nl;
   12123           0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
   12124           0 :         struct wpabuf *buf = arg;
   12125             :         int rem;
   12126             : 
   12127           0 :         if (!buf)
   12128           0 :                 return NL_SKIP;
   12129             : 
   12130           0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
   12131             :                   genlmsg_attrlen(gnlh, 0), NULL);
   12132           0 :         nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
   12133             : 
   12134           0 :         if (!nl_vendor_reply)
   12135           0 :                 return NL_SKIP;
   12136             : 
   12137           0 :         if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
   12138           0 :                 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
   12139           0 :                 return NL_SKIP;
   12140             :         }
   12141             : 
   12142           0 :         nla_for_each_nested(nl, nl_vendor_reply, rem) {
   12143           0 :                 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
   12144             :         }
   12145             : 
   12146           0 :         return NL_SKIP;
   12147             : }
   12148             : 
   12149             : 
   12150           0 : static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
   12151             :                               unsigned int subcmd, const u8 *data,
   12152             :                               size_t data_len, struct wpabuf *buf)
   12153             : {
   12154           0 :         struct i802_bss *bss = priv;
   12155           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   12156             :         struct nl_msg *msg;
   12157             :         int ret;
   12158             : 
   12159           0 :         msg = nlmsg_alloc();
   12160           0 :         if (!msg)
   12161           0 :                 return -ENOMEM;
   12162             : 
   12163             : #ifdef CONFIG_TESTING_OPTIONS
   12164           0 :         if (vendor_id == 0xffffffff) {
   12165           0 :                 nl80211_cmd(drv, msg, 0, subcmd);
   12166           0 :                 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
   12167             :                     0)
   12168           0 :                         goto nla_put_failure;
   12169           0 :                 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
   12170           0 :                 if (ret)
   12171           0 :                         wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
   12172             :                                    ret);
   12173           0 :                 return ret;
   12174             :         }
   12175             : #endif /* CONFIG_TESTING_OPTIONS */
   12176             : 
   12177           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
   12178           0 :         if (nl80211_set_iface_id(msg, bss) < 0)
   12179           0 :                 goto nla_put_failure;
   12180           0 :         NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, vendor_id);
   12181           0 :         NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd);
   12182           0 :         if (data)
   12183           0 :                 NLA_PUT(msg, NL80211_ATTR_VENDOR_DATA, data_len, data);
   12184             : 
   12185           0 :         ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
   12186           0 :         if (ret)
   12187           0 :                 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
   12188             :                            ret);
   12189           0 :         return ret;
   12190             : 
   12191             : nla_put_failure:
   12192           0 :         nlmsg_free(msg);
   12193           0 :         return -ENOBUFS;
   12194             : }
   12195             : 
   12196             : 
   12197           3 : static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
   12198             :                                u8 qos_map_set_len)
   12199             : {
   12200           3 :         struct i802_bss *bss = priv;
   12201           3 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   12202             :         struct nl_msg *msg;
   12203             :         int ret;
   12204             : 
   12205           3 :         msg = nlmsg_alloc();
   12206           3 :         if (!msg)
   12207           0 :                 return -ENOMEM;
   12208             : 
   12209           3 :         wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
   12210             :                     qos_map_set, qos_map_set_len);
   12211             : 
   12212           3 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
   12213           3 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   12214           3 :         NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
   12215             : 
   12216           3 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   12217           3 :         if (ret)
   12218           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
   12219             : 
   12220           3 :         return ret;
   12221             : 
   12222             : nla_put_failure:
   12223           0 :         nlmsg_free(msg);
   12224           0 :         return -ENOBUFS;
   12225             : }
   12226             : 
   12227             : 
   12228           0 : static int nl80211_set_wowlan(void *priv,
   12229             :                               const struct wowlan_triggers *triggers)
   12230             : {
   12231           0 :         struct i802_bss *bss = priv;
   12232           0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   12233             :         struct nl_msg *msg;
   12234             :         struct nlattr *wowlan_triggers;
   12235             :         int ret;
   12236             : 
   12237           0 :         msg = nlmsg_alloc();
   12238           0 :         if (!msg)
   12239           0 :                 return -ENOMEM;
   12240             : 
   12241           0 :         wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
   12242             : 
   12243           0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WOWLAN);
   12244           0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   12245             : 
   12246           0 :         wowlan_triggers = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
   12247           0 :         if (!wowlan_triggers)
   12248           0 :                 goto nla_put_failure;
   12249             : 
   12250           0 :         if (triggers->any)
   12251           0 :                 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
   12252           0 :         if (triggers->disconnect)
   12253           0 :                 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
   12254           0 :         if (triggers->magic_pkt)
   12255           0 :                 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
   12256           0 :         if (triggers->gtk_rekey_failure)
   12257           0 :                 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
   12258           0 :         if (triggers->eap_identity_req)
   12259           0 :                 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
   12260           0 :         if (triggers->four_way_handshake)
   12261           0 :                 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
   12262           0 :         if (triggers->rfkill_release)
   12263           0 :                 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
   12264             : 
   12265           0 :         nla_nest_end(msg, wowlan_triggers);
   12266             : 
   12267           0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   12268           0 :         if (ret)
   12269           0 :                 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
   12270             : 
   12271           0 :         return ret;
   12272             : 
   12273             : nla_put_failure:
   12274           0 :         nlmsg_free(msg);
   12275           0 :         return -ENOBUFS;
   12276             : }
   12277             : 
   12278             : 
   12279             : const struct wpa_driver_ops wpa_driver_nl80211_ops = {
   12280             :         .name = "nl80211",
   12281             :         .desc = "Linux nl80211/cfg80211",
   12282             :         .get_bssid = wpa_driver_nl80211_get_bssid,
   12283             :         .get_ssid = wpa_driver_nl80211_get_ssid,
   12284             :         .set_key = driver_nl80211_set_key,
   12285             :         .scan2 = driver_nl80211_scan2,
   12286             :         .sched_scan = wpa_driver_nl80211_sched_scan,
   12287             :         .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
   12288             :         .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
   12289             :         .deauthenticate = driver_nl80211_deauthenticate,
   12290             :         .authenticate = driver_nl80211_authenticate,
   12291             :         .associate = wpa_driver_nl80211_associate,
   12292             :         .global_init = nl80211_global_init,
   12293             :         .global_deinit = nl80211_global_deinit,
   12294             :         .init2 = wpa_driver_nl80211_init,
   12295             :         .deinit = driver_nl80211_deinit,
   12296             :         .get_capa = wpa_driver_nl80211_get_capa,
   12297             :         .set_operstate = wpa_driver_nl80211_set_operstate,
   12298             :         .set_supp_port = wpa_driver_nl80211_set_supp_port,
   12299             :         .set_country = wpa_driver_nl80211_set_country,
   12300             :         .get_country = wpa_driver_nl80211_get_country,
   12301             :         .set_ap = wpa_driver_nl80211_set_ap,
   12302             :         .set_acl = wpa_driver_nl80211_set_acl,
   12303             :         .if_add = wpa_driver_nl80211_if_add,
   12304             :         .if_remove = driver_nl80211_if_remove,
   12305             :         .send_mlme = driver_nl80211_send_mlme,
   12306             :         .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
   12307             :         .sta_add = wpa_driver_nl80211_sta_add,
   12308             :         .sta_remove = driver_nl80211_sta_remove,
   12309             :         .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
   12310             :         .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
   12311             :         .hapd_init = i802_init,
   12312             :         .hapd_deinit = i802_deinit,
   12313             :         .set_wds_sta = i802_set_wds_sta,
   12314             :         .get_seqnum = i802_get_seqnum,
   12315             :         .flush = i802_flush,
   12316             :         .get_inact_sec = i802_get_inact_sec,
   12317             :         .sta_clear_stats = i802_sta_clear_stats,
   12318             :         .set_rts = i802_set_rts,
   12319             :         .set_frag = i802_set_frag,
   12320             :         .set_tx_queue_params = i802_set_tx_queue_params,
   12321             :         .set_sta_vlan = driver_nl80211_set_sta_vlan,
   12322             :         .sta_deauth = i802_sta_deauth,
   12323             :         .sta_disassoc = i802_sta_disassoc,
   12324             :         .read_sta_data = driver_nl80211_read_sta_data,
   12325             :         .set_freq = i802_set_freq,
   12326             :         .send_action = driver_nl80211_send_action,
   12327             :         .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
   12328             :         .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
   12329             :         .cancel_remain_on_channel =
   12330             :         wpa_driver_nl80211_cancel_remain_on_channel,
   12331             :         .probe_req_report = driver_nl80211_probe_req_report,
   12332             :         .deinit_ap = wpa_driver_nl80211_deinit_ap,
   12333             :         .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
   12334             :         .resume = wpa_driver_nl80211_resume,
   12335             :         .send_ft_action = nl80211_send_ft_action,
   12336             :         .signal_monitor = nl80211_signal_monitor,
   12337             :         .signal_poll = nl80211_signal_poll,
   12338             :         .send_frame = nl80211_send_frame,
   12339             :         .shared_freq = wpa_driver_nl80211_shared_freq,
   12340             :         .set_param = nl80211_set_param,
   12341             :         .get_radio_name = nl80211_get_radio_name,
   12342             :         .add_pmkid = nl80211_add_pmkid,
   12343             :         .remove_pmkid = nl80211_remove_pmkid,
   12344             :         .flush_pmkid = nl80211_flush_pmkid,
   12345             :         .set_rekey_info = nl80211_set_rekey_info,
   12346             :         .poll_client = nl80211_poll_client,
   12347             :         .set_p2p_powersave = nl80211_set_p2p_powersave,
   12348             :         .start_dfs_cac = nl80211_start_radar_detection,
   12349             :         .stop_ap = wpa_driver_nl80211_stop_ap,
   12350             : #ifdef CONFIG_TDLS
   12351             :         .send_tdls_mgmt = nl80211_send_tdls_mgmt,
   12352             :         .tdls_oper = nl80211_tdls_oper,
   12353             : #endif /* CONFIG_TDLS */
   12354             :         .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
   12355             :         .get_mac_addr = wpa_driver_nl80211_get_macaddr,
   12356             :         .get_survey = wpa_driver_nl80211_get_survey,
   12357             :         .status = wpa_driver_nl80211_status,
   12358             :         .switch_channel = nl80211_switch_channel,
   12359             : #ifdef ANDROID_P2P
   12360             :         .set_noa = wpa_driver_set_p2p_noa,
   12361             :         .get_noa = wpa_driver_get_p2p_noa,
   12362             :         .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
   12363             : #endif /* ANDROID_P2P */
   12364             : #ifdef ANDROID
   12365             :         .driver_cmd = wpa_driver_nl80211_driver_cmd,
   12366             : #endif /* ANDROID */
   12367             :         .vendor_cmd = nl80211_vendor_cmd,
   12368             :         .set_qos_map = nl80211_set_qos_map,
   12369             :         .set_wowlan = nl80211_set_wowlan,
   12370             : };

Generated by: LCOV version 1.10