LCOV - code coverage report
Current view: top level - src/drivers - driver_nl80211.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1388338050 Lines: 3122 5727 54.5 %
Date: 2013-12-29 Functions: 209 286 73.1 %
Branches: 1517 3707 40.9 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Driver interaction with Linux nl80211/cfg80211
       3                 :            :  * Copyright (c) 2002-2012, 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/ieee802_11_defs.h"
      32                 :            : #include "common/ieee802_11_common.h"
      33                 :            : #include "l2_packet/l2_packet.h"
      34                 :            : #include "netlink.h"
      35                 :            : #include "linux_ioctl.h"
      36                 :            : #include "radiotap.h"
      37                 :            : #include "radiotap_iter.h"
      38                 :            : #include "rfkill.h"
      39                 :            : #include "driver.h"
      40                 :            : 
      41                 :            : #ifndef SO_WIFI_STATUS
      42                 :            : # if defined(__sparc__)
      43                 :            : #  define SO_WIFI_STATUS        0x0025
      44                 :            : # elif defined(__parisc__)
      45                 :            : #  define SO_WIFI_STATUS        0x4022
      46                 :            : # else
      47                 :            : #  define SO_WIFI_STATUS        41
      48                 :            : # endif
      49                 :            : 
      50                 :            : # define SCM_WIFI_STATUS        SO_WIFI_STATUS
      51                 :            : #endif
      52                 :            : 
      53                 :            : #ifndef SO_EE_ORIGIN_TXSTATUS
      54                 :            : #define SO_EE_ORIGIN_TXSTATUS   4
      55                 :            : #endif
      56                 :            : 
      57                 :            : #ifndef PACKET_TX_TIMESTAMP
      58                 :            : #define PACKET_TX_TIMESTAMP     16
      59                 :            : #endif
      60                 :            : 
      61                 :            : #ifdef ANDROID
      62                 :            : #include "android_drv.h"
      63                 :            : #endif /* ANDROID */
      64                 :            : #ifdef CONFIG_LIBNL20
      65                 :            : /* libnl 2.0 compatibility code */
      66                 :            : #define nl_handle nl_sock
      67                 :            : #define nl80211_handle_alloc nl_socket_alloc_cb
      68                 :            : #define nl80211_handle_destroy nl_socket_free
      69                 :            : #else
      70                 :            : /*
      71                 :            :  * libnl 1.1 has a bug, it tries to allocate socket numbers densely
      72                 :            :  * but when you free a socket again it will mess up its bitmap and
      73                 :            :  * and use the wrong number the next time it needs a socket ID.
      74                 :            :  * Therefore, we wrap the handle alloc/destroy and add our own pid
      75                 :            :  * accounting.
      76                 :            :  */
      77                 :            : static uint32_t port_bitmap[32] = { 0 };
      78                 :            : 
      79                 :            : static struct nl_handle *nl80211_handle_alloc(void *cb)
      80                 :            : {
      81                 :            :         struct nl_handle *handle;
      82                 :            :         uint32_t pid = getpid() & 0x3FFFFF;
      83                 :            :         int i;
      84                 :            : 
      85                 :            :         handle = nl_handle_alloc_cb(cb);
      86                 :            : 
      87                 :            :         for (i = 0; i < 1024; i++) {
      88                 :            :                 if (port_bitmap[i / 32] & (1 << (i % 32)))
      89                 :            :                         continue;
      90                 :            :                 port_bitmap[i / 32] |= 1 << (i % 32);
      91                 :            :                 pid += i << 22;
      92                 :            :                 break;
      93                 :            :         }
      94                 :            : 
      95                 :            :         nl_socket_set_local_port(handle, pid);
      96                 :            : 
      97                 :            :         return handle;
      98                 :            : }
      99                 :            : 
     100                 :            : static void nl80211_handle_destroy(struct nl_handle *handle)
     101                 :            : {
     102                 :            :         uint32_t port = nl_socket_get_local_port(handle);
     103                 :            : 
     104                 :            :         port >>= 22;
     105                 :            :         port_bitmap[port / 32] &= ~(1 << (port % 32));
     106                 :            : 
     107                 :            :         nl_handle_destroy(handle);
     108                 :            : }
     109                 :            : #endif /* CONFIG_LIBNL20 */
     110                 :            : 
     111                 :            : 
     112                 :            : #ifdef ANDROID
     113                 :            : /* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
     114                 :            : static int android_nl_socket_set_nonblocking(struct nl_handle *handle)
     115                 :            : {
     116                 :            :         return fcntl(nl_socket_get_fd(handle), F_SETFL, O_NONBLOCK);
     117                 :            : }
     118                 :            : #undef nl_socket_set_nonblocking
     119                 :            : #define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
     120                 :            : #endif /* ANDROID */
     121                 :            : 
     122                 :            : 
     123                 :       1187 : static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
     124                 :            : {
     125                 :            :         struct nl_handle *handle;
     126                 :            : 
     127                 :       1187 :         handle = nl80211_handle_alloc(cb);
     128         [ -  + ]:       1187 :         if (handle == NULL) {
     129                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
     130                 :            :                            "callbacks (%s)", dbg);
     131                 :          0 :                 return NULL;
     132                 :            :         }
     133                 :            : 
     134         [ -  + ]:       1187 :         if (genl_connect(handle)) {
     135                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
     136                 :            :                            "netlink (%s)", dbg);
     137                 :          0 :                 nl80211_handle_destroy(handle);
     138                 :          0 :                 return NULL;
     139                 :            :         }
     140                 :            : 
     141                 :       1187 :         return handle;
     142                 :            : }
     143                 :            : 
     144                 :            : 
     145                 :       1187 : static void nl_destroy_handles(struct nl_handle **handle)
     146                 :            : {
     147         [ -  + ]:       1187 :         if (*handle == NULL)
     148                 :       1187 :                 return;
     149                 :       1187 :         nl80211_handle_destroy(*handle);
     150                 :       1187 :         *handle = NULL;
     151                 :            : }
     152                 :            : 
     153                 :            : 
     154                 :            : #if __WORDSIZE == 64
     155                 :            : #define ELOOP_SOCKET_INVALID    (intptr_t) 0x8888888888888889ULL
     156                 :            : #else
     157                 :            : #define ELOOP_SOCKET_INVALID    (intptr_t) 0x88888889ULL
     158                 :            : #endif
     159                 :            : 
     160                 :       1183 : static void nl80211_register_eloop_read(struct nl_handle **handle,
     161                 :            :                                         eloop_sock_handler handler,
     162                 :            :                                         void *eloop_data)
     163                 :            : {
     164                 :       1183 :         nl_socket_set_nonblocking(*handle);
     165                 :       1183 :         eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
     166                 :            :                                  eloop_data, *handle);
     167                 :       1183 :         *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
     168                 :       1183 : }
     169                 :            : 
     170                 :            : 
     171                 :       1183 : static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
     172                 :            : {
     173                 :       1183 :         *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
     174                 :       1183 :         eloop_unregister_read_sock(nl_socket_get_fd(*handle));
     175                 :       1183 :         nl_destroy_handles(handle);
     176                 :       1183 : }
     177                 :            : 
     178                 :            : 
     179                 :            : #ifndef IFF_LOWER_UP
     180                 :            : #define IFF_LOWER_UP   0x10000         /* driver signals L1 up         */
     181                 :            : #endif
     182                 :            : #ifndef IFF_DORMANT
     183                 :            : #define IFF_DORMANT    0x20000         /* driver signals dormant       */
     184                 :            : #endif
     185                 :            : 
     186                 :            : #ifndef IF_OPER_DORMANT
     187                 :            : #define IF_OPER_DORMANT 5
     188                 :            : #endif
     189                 :            : #ifndef IF_OPER_UP
     190                 :            : #define IF_OPER_UP 6
     191                 :            : #endif
     192                 :            : 
     193                 :            : struct nl80211_global {
     194                 :            :         struct dl_list interfaces;
     195                 :            :         int if_add_ifindex;
     196                 :            :         u64 if_add_wdevid;
     197                 :            :         int if_add_wdevid_set;
     198                 :            :         struct netlink_data *netlink;
     199                 :            :         struct nl_cb *nl_cb;
     200                 :            :         struct nl_handle *nl;
     201                 :            :         int nl80211_id;
     202                 :            :         int ioctl_sock; /* socket for ioctl() use */
     203                 :            : 
     204                 :            :         struct nl_handle *nl_event;
     205                 :            : };
     206                 :            : 
     207                 :            : struct nl80211_wiphy_data {
     208                 :            :         struct dl_list list;
     209                 :            :         struct dl_list bsss;
     210                 :            :         struct dl_list drvs;
     211                 :            : 
     212                 :            :         struct nl_handle *nl_beacons;
     213                 :            :         struct nl_cb *nl_cb;
     214                 :            : 
     215                 :            :         int wiphy_idx;
     216                 :            : };
     217                 :            : 
     218                 :            : static void nl80211_global_deinit(void *priv);
     219                 :            : 
     220                 :            : struct i802_bss {
     221                 :            :         struct wpa_driver_nl80211_data *drv;
     222                 :            :         struct i802_bss *next;
     223                 :            :         int ifindex;
     224                 :            :         u64 wdev_id;
     225                 :            :         char ifname[IFNAMSIZ + 1];
     226                 :            :         char brname[IFNAMSIZ];
     227                 :            :         unsigned int beacon_set:1;
     228                 :            :         unsigned int added_if_into_bridge:1;
     229                 :            :         unsigned int added_bridge:1;
     230                 :            :         unsigned int in_deinit:1;
     231                 :            :         unsigned int wdev_id_set:1;
     232                 :            :         unsigned int added_if:1;
     233                 :            : 
     234                 :            :         u8 addr[ETH_ALEN];
     235                 :            : 
     236                 :            :         int freq;
     237                 :            :         int if_dynamic;
     238                 :            : 
     239                 :            :         void *ctx;
     240                 :            :         struct nl_handle *nl_preq, *nl_mgmt;
     241                 :            :         struct nl_cb *nl_cb;
     242                 :            : 
     243                 :            :         struct nl80211_wiphy_data *wiphy_data;
     244                 :            :         struct dl_list wiphy_list;
     245                 :            : };
     246                 :            : 
     247                 :            : struct wpa_driver_nl80211_data {
     248                 :            :         struct nl80211_global *global;
     249                 :            :         struct dl_list list;
     250                 :            :         struct dl_list wiphy_list;
     251                 :            :         char phyname[32];
     252                 :            :         void *ctx;
     253                 :            :         int ifindex;
     254                 :            :         int if_removed;
     255                 :            :         int if_disabled;
     256                 :            :         int ignore_if_down_event;
     257                 :            :         struct rfkill_data *rfkill;
     258                 :            :         struct wpa_driver_capa capa;
     259                 :            :         u8 *extended_capa, *extended_capa_mask;
     260                 :            :         unsigned int extended_capa_len;
     261                 :            :         int has_capability;
     262                 :            : 
     263                 :            :         int operstate;
     264                 :            : 
     265                 :            :         int scan_complete_events;
     266                 :            :         enum scan_states {
     267                 :            :                 NO_SCAN, SCAN_REQUESTED, SCAN_STARTED, SCAN_COMPLETED,
     268                 :            :                 SCAN_ABORTED, SCHED_SCAN_STARTED, SCHED_SCAN_STOPPED,
     269                 :            :                 SCHED_SCAN_RESULTS
     270                 :            :         } scan_state;
     271                 :            : 
     272                 :            :         struct nl_cb *nl_cb;
     273                 :            : 
     274                 :            :         u8 auth_bssid[ETH_ALEN];
     275                 :            :         u8 auth_attempt_bssid[ETH_ALEN];
     276                 :            :         u8 bssid[ETH_ALEN];
     277                 :            :         u8 prev_bssid[ETH_ALEN];
     278                 :            :         int associated;
     279                 :            :         u8 ssid[32];
     280                 :            :         size_t ssid_len;
     281                 :            :         enum nl80211_iftype nlmode;
     282                 :            :         enum nl80211_iftype ap_scan_as_station;
     283                 :            :         unsigned int assoc_freq;
     284                 :            : 
     285                 :            :         int monitor_sock;
     286                 :            :         int monitor_ifidx;
     287                 :            :         int monitor_refcount;
     288                 :            : 
     289                 :            :         unsigned int disabled_11b_rates:1;
     290                 :            :         unsigned int pending_remain_on_chan:1;
     291                 :            :         unsigned int in_interface_list:1;
     292                 :            :         unsigned int device_ap_sme:1;
     293                 :            :         unsigned int poll_command_supported:1;
     294                 :            :         unsigned int data_tx_status:1;
     295                 :            :         unsigned int scan_for_auth:1;
     296                 :            :         unsigned int retry_auth:1;
     297                 :            :         unsigned int use_monitor:1;
     298                 :            :         unsigned int ignore_next_local_disconnect:1;
     299                 :            :         unsigned int allow_p2p_device:1;
     300                 :            :         unsigned int hostapd:1;
     301                 :            :         unsigned int start_mode_ap:1;
     302                 :            :         unsigned int start_iface_up:1;
     303                 :            :         unsigned int channel_switch_supported:1;
     304                 :            : 
     305                 :            :         u64 remain_on_chan_cookie;
     306                 :            :         u64 send_action_cookie;
     307                 :            : 
     308                 :            :         unsigned int last_mgmt_freq;
     309                 :            : 
     310                 :            :         struct wpa_driver_scan_filter *filter_ssids;
     311                 :            :         size_t num_filter_ssids;
     312                 :            : 
     313                 :            :         struct i802_bss *first_bss;
     314                 :            : 
     315                 :            :         int eapol_tx_sock;
     316                 :            : 
     317                 :            :         int eapol_sock; /* socket for EAPOL frames */
     318                 :            : 
     319                 :            :         int default_if_indices[16];
     320                 :            :         int *if_indices;
     321                 :            :         int num_if_indices;
     322                 :            : 
     323                 :            :         /* From failed authentication command */
     324                 :            :         int auth_freq;
     325                 :            :         u8 auth_bssid_[ETH_ALEN];
     326                 :            :         u8 auth_ssid[32];
     327                 :            :         size_t auth_ssid_len;
     328                 :            :         int auth_alg;
     329                 :            :         u8 *auth_ie;
     330                 :            :         size_t auth_ie_len;
     331                 :            :         u8 auth_wep_key[4][16];
     332                 :            :         size_t auth_wep_key_len[4];
     333                 :            :         int auth_wep_tx_keyidx;
     334                 :            :         int auth_local_state_change;
     335                 :            :         int auth_p2p;
     336                 :            : };
     337                 :            : 
     338                 :            : 
     339                 :            : static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
     340                 :            : static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
     341                 :            :                                             void *timeout_ctx);
     342                 :            : static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
     343                 :            :                                        enum nl80211_iftype nlmode);
     344                 :            : static int
     345                 :            : wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
     346                 :            :                                    const u8 *set_addr, int first);
     347                 :            : static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
     348                 :            :                                    const u8 *addr, int cmd, u16 reason_code,
     349                 :            :                                    int local_state_change);
     350                 :            : static void nl80211_remove_monitor_interface(
     351                 :            :         struct wpa_driver_nl80211_data *drv);
     352                 :            : static int nl80211_send_frame_cmd(struct i802_bss *bss,
     353                 :            :                                   unsigned int freq, unsigned int wait,
     354                 :            :                                   const u8 *buf, size_t buf_len, u64 *cookie,
     355                 :            :                                   int no_cck, int no_ack, int offchanok);
     356                 :            : static int nl80211_register_frame(struct i802_bss *bss,
     357                 :            :                                   struct nl_handle *hl_handle,
     358                 :            :                                   u16 type, const u8 *match, size_t match_len);
     359                 :            : static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
     360                 :            :                                                int report);
     361                 :            : #ifdef ANDROID
     362                 :            : static int android_pno_start(struct i802_bss *bss,
     363                 :            :                              struct wpa_driver_scan_params *params);
     364                 :            : static int android_pno_stop(struct i802_bss *bss);
     365                 :            : extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
     366                 :            :                                          size_t buf_len);
     367                 :            : #endif /* ANDROID */
     368                 :            : #ifdef ANDROID_P2P
     369                 :            : int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
     370                 :            : int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
     371                 :            : int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
     372                 :            : int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
     373                 :            :                                  const struct wpabuf *proberesp,
     374                 :            :                                  const struct wpabuf *assocresp);
     375                 :            : #endif /* ANDROID_P2P */
     376                 :            : 
     377                 :            : static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
     378                 :            : static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
     379                 :            : static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
     380                 :            : static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
     381                 :            :                                         enum wpa_driver_if_type type,
     382                 :            :                                         const char *ifname);
     383                 :            : 
     384                 :            : static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
     385                 :            :                                        struct hostapd_freq_params *freq);
     386                 :            : static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
     387                 :            :                                      int ifindex, int disabled);
     388                 :            : 
     389                 :            : static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
     390                 :            : static int wpa_driver_nl80211_authenticate_retry(
     391                 :            :         struct wpa_driver_nl80211_data *drv);
     392                 :            : 
     393                 :            : static int i802_set_iface_flags(struct i802_bss *bss, int up);
     394                 :            : 
     395                 :            : 
     396                 :      16927 : static const char * nl80211_command_to_string(enum nl80211_commands cmd)
     397                 :            : {
     398                 :            : #define C2S(x) case x: return #x;
     399   [ -  -  -  -  :      16927 :         switch (cmd) {
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          +  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
          +  -  +  +  +  
          +  -  -  -  +  
          -  -  +  -  +  
          -  -  -  -  -  
          -  +  +  -  -  
          +  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
                   -  - ]
     400                 :          0 :         C2S(NL80211_CMD_UNSPEC)
     401                 :          0 :         C2S(NL80211_CMD_GET_WIPHY)
     402                 :          0 :         C2S(NL80211_CMD_SET_WIPHY)
     403                 :          0 :         C2S(NL80211_CMD_NEW_WIPHY)
     404                 :          0 :         C2S(NL80211_CMD_DEL_WIPHY)
     405                 :          0 :         C2S(NL80211_CMD_GET_INTERFACE)
     406                 :          0 :         C2S(NL80211_CMD_SET_INTERFACE)
     407                 :          0 :         C2S(NL80211_CMD_NEW_INTERFACE)
     408                 :          0 :         C2S(NL80211_CMD_DEL_INTERFACE)
     409                 :          0 :         C2S(NL80211_CMD_GET_KEY)
     410                 :          0 :         C2S(NL80211_CMD_SET_KEY)
     411                 :          0 :         C2S(NL80211_CMD_NEW_KEY)
     412                 :          0 :         C2S(NL80211_CMD_DEL_KEY)
     413                 :          0 :         C2S(NL80211_CMD_GET_BEACON)
     414                 :          0 :         C2S(NL80211_CMD_SET_BEACON)
     415                 :          0 :         C2S(NL80211_CMD_START_AP)
     416                 :          0 :         C2S(NL80211_CMD_STOP_AP)
     417                 :          0 :         C2S(NL80211_CMD_GET_STATION)
     418                 :          0 :         C2S(NL80211_CMD_SET_STATION)
     419                 :        888 :         C2S(NL80211_CMD_NEW_STATION)
     420                 :        863 :         C2S(NL80211_CMD_DEL_STATION)
     421                 :          0 :         C2S(NL80211_CMD_GET_MPATH)
     422                 :          0 :         C2S(NL80211_CMD_SET_MPATH)
     423                 :          0 :         C2S(NL80211_CMD_NEW_MPATH)
     424                 :          0 :         C2S(NL80211_CMD_DEL_MPATH)
     425                 :          0 :         C2S(NL80211_CMD_SET_BSS)
     426                 :          0 :         C2S(NL80211_CMD_SET_REG)
     427                 :          0 :         C2S(NL80211_CMD_REQ_SET_REG)
     428                 :          0 :         C2S(NL80211_CMD_GET_MESH_CONFIG)
     429                 :          0 :         C2S(NL80211_CMD_SET_MESH_CONFIG)
     430                 :          0 :         C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
     431                 :          0 :         C2S(NL80211_CMD_GET_REG)
     432                 :          0 :         C2S(NL80211_CMD_GET_SCAN)
     433                 :        899 :         C2S(NL80211_CMD_TRIGGER_SCAN)
     434                 :        894 :         C2S(NL80211_CMD_NEW_SCAN_RESULTS)
     435                 :          0 :         C2S(NL80211_CMD_SCAN_ABORTED)
     436                 :       1227 :         C2S(NL80211_CMD_REG_CHANGE)
     437                 :        847 :         C2S(NL80211_CMD_AUTHENTICATE)
     438                 :        816 :         C2S(NL80211_CMD_ASSOCIATE)
     439                 :        810 :         C2S(NL80211_CMD_DEAUTHENTICATE)
     440                 :          0 :         C2S(NL80211_CMD_DISASSOCIATE)
     441                 :          0 :         C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
     442                 :          0 :         C2S(NL80211_CMD_REG_BEACON_HINT)
     443                 :          6 :         C2S(NL80211_CMD_JOIN_IBSS)
     444                 :          0 :         C2S(NL80211_CMD_LEAVE_IBSS)
     445                 :          0 :         C2S(NL80211_CMD_TESTMODE)
     446                 :        408 :         C2S(NL80211_CMD_CONNECT)
     447                 :          0 :         C2S(NL80211_CMD_ROAM)
     448                 :        399 :         C2S(NL80211_CMD_DISCONNECT)
     449                 :          0 :         C2S(NL80211_CMD_SET_WIPHY_NETNS)
     450                 :          0 :         C2S(NL80211_CMD_GET_SURVEY)
     451                 :          0 :         C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
     452                 :          0 :         C2S(NL80211_CMD_SET_PMKSA)
     453                 :          0 :         C2S(NL80211_CMD_DEL_PMKSA)
     454                 :          0 :         C2S(NL80211_CMD_FLUSH_PMKSA)
     455                 :        494 :         C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
     456                 :        494 :         C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
     457                 :          0 :         C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
     458                 :          0 :         C2S(NL80211_CMD_REGISTER_FRAME)
     459                 :       4922 :         C2S(NL80211_CMD_FRAME)
     460                 :       2960 :         C2S(NL80211_CMD_FRAME_TX_STATUS)
     461                 :          0 :         C2S(NL80211_CMD_SET_POWER_SAVE)
     462                 :          0 :         C2S(NL80211_CMD_GET_POWER_SAVE)
     463                 :          0 :         C2S(NL80211_CMD_SET_CQM)
     464                 :          0 :         C2S(NL80211_CMD_NOTIFY_CQM)
     465                 :          0 :         C2S(NL80211_CMD_SET_CHANNEL)
     466                 :          0 :         C2S(NL80211_CMD_SET_WDS_PEER)
     467                 :          0 :         C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
     468                 :          0 :         C2S(NL80211_CMD_JOIN_MESH)
     469                 :          0 :         C2S(NL80211_CMD_LEAVE_MESH)
     470                 :          0 :         C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
     471                 :          0 :         C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
     472                 :          0 :         C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
     473                 :          0 :         C2S(NL80211_CMD_GET_WOWLAN)
     474                 :          0 :         C2S(NL80211_CMD_SET_WOWLAN)
     475                 :          0 :         C2S(NL80211_CMD_START_SCHED_SCAN)
     476                 :          0 :         C2S(NL80211_CMD_STOP_SCHED_SCAN)
     477                 :          0 :         C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
     478                 :          0 :         C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
     479                 :          0 :         C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
     480                 :          0 :         C2S(NL80211_CMD_PMKSA_CANDIDATE)
     481                 :          0 :         C2S(NL80211_CMD_TDLS_OPER)
     482                 :          0 :         C2S(NL80211_CMD_TDLS_MGMT)
     483                 :          0 :         C2S(NL80211_CMD_UNEXPECTED_FRAME)
     484                 :          0 :         C2S(NL80211_CMD_PROBE_CLIENT)
     485                 :          0 :         C2S(NL80211_CMD_REGISTER_BEACONS)
     486                 :          0 :         C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
     487                 :          0 :         C2S(NL80211_CMD_SET_NOACK_MAP)
     488                 :          0 :         C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
     489                 :          0 :         C2S(NL80211_CMD_START_P2P_DEVICE)
     490                 :          0 :         C2S(NL80211_CMD_STOP_P2P_DEVICE)
     491                 :          0 :         C2S(NL80211_CMD_CONN_FAILED)
     492                 :          0 :         C2S(NL80211_CMD_SET_MCAST_RATE)
     493                 :          0 :         C2S(NL80211_CMD_SET_MAC_ACL)
     494                 :          0 :         C2S(NL80211_CMD_RADAR_DETECT)
     495                 :          0 :         C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
     496                 :          0 :         C2S(NL80211_CMD_UPDATE_FT_IES)
     497                 :          0 :         C2S(NL80211_CMD_FT_EVENT)
     498                 :          0 :         C2S(NL80211_CMD_CRIT_PROTOCOL_START)
     499                 :          0 :         C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
     500                 :            :         default:
     501                 :      16927 :                 return "NL80211_CMD_UNKNOWN";
     502                 :            :         }
     503                 :            : #undef C2S
     504                 :            : }
     505                 :            : 
     506                 :            : 
     507                 :            : /* Converts nl80211_chan_width to a common format */
     508                 :          0 : static enum chan_width convert2width(int width)
     509                 :            : {
     510   [ #  #  #  #  :          0 :         switch (width) {
                #  #  # ]
     511                 :            :         case NL80211_CHAN_WIDTH_20_NOHT:
     512                 :          0 :                 return CHAN_WIDTH_20_NOHT;
     513                 :            :         case NL80211_CHAN_WIDTH_20:
     514                 :          0 :                 return CHAN_WIDTH_20;
     515                 :            :         case NL80211_CHAN_WIDTH_40:
     516                 :          0 :                 return CHAN_WIDTH_40;
     517                 :            :         case NL80211_CHAN_WIDTH_80:
     518                 :          0 :                 return CHAN_WIDTH_80;
     519                 :            :         case NL80211_CHAN_WIDTH_80P80:
     520                 :          0 :                 return CHAN_WIDTH_80P80;
     521                 :            :         case NL80211_CHAN_WIDTH_160:
     522                 :          0 :                 return CHAN_WIDTH_160;
     523                 :            :         }
     524                 :          0 :         return CHAN_WIDTH_UNKNOWN;
     525                 :            : }
     526                 :            : 
     527                 :            : 
     528                 :       6608 : static int is_ap_interface(enum nl80211_iftype nlmode)
     529                 :            : {
     530 [ +  + ][ +  + ]:       6608 :         return (nlmode == NL80211_IFTYPE_AP ||
     531                 :            :                 nlmode == NL80211_IFTYPE_P2P_GO);
     532                 :            : }
     533                 :            : 
     534                 :            : 
     535                 :       2641 : static int is_sta_interface(enum nl80211_iftype nlmode)
     536                 :            : {
     537 [ +  + ][ -  + ]:       2641 :         return (nlmode == NL80211_IFTYPE_STATION ||
     538                 :            :                 nlmode == NL80211_IFTYPE_P2P_CLIENT);
     539                 :            : }
     540                 :            : 
     541                 :            : 
     542                 :        674 : static int is_p2p_net_interface(enum nl80211_iftype nlmode)
     543                 :            : {
     544 [ +  + ][ +  + ]:        674 :         return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
     545                 :            :                 nlmode == NL80211_IFTYPE_P2P_GO);
     546                 :            : }
     547                 :            : 
     548                 :            : 
     549                 :       1619 : static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
     550                 :            : {
     551         [ +  + ]:       1619 :         if (drv->associated)
     552                 :        414 :                 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
     553                 :       1619 :         drv->associated = 0;
     554                 :       1619 :         os_memset(drv->bssid, 0, ETH_ALEN);
     555                 :       1619 : }
     556                 :            : 
     557                 :            : 
     558                 :            : struct nl80211_bss_info_arg {
     559                 :            :         struct wpa_driver_nl80211_data *drv;
     560                 :            :         struct wpa_scan_results *res;
     561                 :            :         unsigned int assoc_freq;
     562                 :            :         u8 assoc_bssid[ETH_ALEN];
     563                 :            : };
     564                 :            : 
     565                 :            : static int bss_info_handler(struct nl_msg *msg, void *arg);
     566                 :            : 
     567                 :            : 
     568                 :            : /* nl80211 code */
     569                 :      25588 : static int ack_handler(struct nl_msg *msg, void *arg)
     570                 :            : {
     571                 :      25588 :         int *err = arg;
     572                 :      25588 :         *err = 0;
     573                 :      25588 :         return NL_STOP;
     574                 :            : }
     575                 :            : 
     576                 :       3260 : static int finish_handler(struct nl_msg *msg, void *arg)
     577                 :            : {
     578                 :       3260 :         int *ret = arg;
     579                 :       3260 :         *ret = 0;
     580                 :       3260 :         return NL_SKIP;
     581                 :            : }
     582                 :            : 
     583                 :      13923 : static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
     584                 :            :                          void *arg)
     585                 :            : {
     586                 :      13923 :         int *ret = arg;
     587                 :      13923 :         *ret = err->error;
     588                 :      13923 :         return NL_SKIP;
     589                 :            : }
     590                 :            : 
     591                 :            : 
     592                 :     169917 : static int no_seq_check(struct nl_msg *msg, void *arg)
     593                 :            : {
     594                 :     169917 :         return NL_OK;
     595                 :            : }
     596                 :            : 
     597                 :            : 
     598                 :      42771 : static int send_and_recv(struct nl80211_global *global,
     599                 :            :                          struct nl_handle *nl_handle, struct nl_msg *msg,
     600                 :            :                          int (*valid_handler)(struct nl_msg *, void *),
     601                 :            :                          void *valid_data)
     602                 :            : {
     603                 :            :         struct nl_cb *cb;
     604                 :      42771 :         int err = -ENOMEM;
     605                 :            : 
     606                 :      42771 :         cb = nl_cb_clone(global->nl_cb);
     607         [ -  + ]:      42771 :         if (!cb)
     608                 :          0 :                 goto out;
     609                 :            : 
     610                 :      42771 :         err = nl_send_auto_complete(nl_handle, msg);
     611         [ -  + ]:      42771 :         if (err < 0)
     612                 :          0 :                 goto out;
     613                 :            : 
     614                 :      42771 :         err = 1;
     615                 :            : 
     616                 :      42771 :         nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
     617                 :      42771 :         nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
     618                 :      42771 :         nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
     619                 :            : 
     620         [ +  + ]:      42771 :         if (valid_handler)
     621                 :      10419 :                 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
     622                 :            :                           valid_handler, valid_data);
     623                 :            : 
     624         [ +  + ]:      91527 :         while (err > 0) {
     625                 :      48756 :                 int res = nl_recvmsgs(nl_handle, cb);
     626         [ -  + ]:      48756 :                 if (res) {
     627                 :          0 :                         wpa_printf(MSG_INFO,
     628                 :            :                                    "nl80211: %s->nl_recvmsgs failed: %d",
     629                 :            :                                    __func__, res);
     630                 :            :                 }
     631                 :            :         }
     632                 :            :  out:
     633                 :      42771 :         nl_cb_put(cb);
     634                 :      42771 :         nlmsg_free(msg);
     635                 :      42771 :         return err;
     636                 :            : }
     637                 :            : 
     638                 :            : 
     639                 :         12 : static int send_and_recv_msgs_global(struct nl80211_global *global,
     640                 :            :                                      struct nl_msg *msg,
     641                 :            :                                      int (*valid_handler)(struct nl_msg *, void *),
     642                 :            :                                      void *valid_data)
     643                 :            : {
     644                 :         12 :         return send_and_recv(global, global->nl, msg, valid_handler,
     645                 :            :                              valid_data);
     646                 :            : }
     647                 :            : 
     648                 :            : 
     649                 :      37785 : static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
     650                 :            :                               struct nl_msg *msg,
     651                 :            :                               int (*valid_handler)(struct nl_msg *, void *),
     652                 :            :                               void *valid_data)
     653                 :            : {
     654                 :      37785 :         return send_and_recv(drv->global, drv->global->nl, msg,
     655                 :            :                              valid_handler, valid_data);
     656                 :            : }
     657                 :            : 
     658                 :            : 
     659                 :            : struct family_data {
     660                 :            :         const char *group;
     661                 :            :         int id;
     662                 :            : };
     663                 :            : 
     664                 :            : 
     665                 :      10612 : static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
     666                 :            : {
     667         [ -  + ]:      10612 :         if (bss->wdev_id_set)
     668         [ #  # ]:          0 :                 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
     669                 :            :         else
     670         [ +  - ]:      10612 :                 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
     671                 :      10612 :         return 0;
     672                 :            : 
     673                 :            : nla_put_failure:
     674                 :      10612 :         return -1;
     675                 :            : }
     676                 :            : 
     677                 :            : 
     678                 :         12 : static int family_handler(struct nl_msg *msg, void *arg)
     679                 :            : {
     680                 :         12 :         struct family_data *res = arg;
     681                 :            :         struct nlattr *tb[CTRL_ATTR_MAX + 1];
     682                 :         12 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
     683                 :            :         struct nlattr *mcgrp;
     684                 :            :         int i;
     685                 :            : 
     686                 :         12 :         nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
     687                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
     688         [ -  + ]:         12 :         if (!tb[CTRL_ATTR_MCAST_GROUPS])
     689                 :          0 :                 return NL_SKIP;
     690                 :            : 
     691         [ +  - ]:         36 :         nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
     692                 :            :                 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
     693                 :         36 :                 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
     694                 :            :                           nla_len(mcgrp), NULL);
     695 [ +  - ][ +  - ]:         36 :                 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
     696         [ +  + ]:         36 :                     !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
     697                 :         36 :                     os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
     698                 :            :                                res->group,
     699                 :            :                                nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
     700                 :         24 :                         continue;
     701                 :         12 :                 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
     702                 :         12 :                 break;
     703                 :            :         };
     704                 :            : 
     705                 :         12 :         return NL_SKIP;
     706                 :            : }
     707                 :            : 
     708                 :            : 
     709                 :         12 : static int nl_get_multicast_id(struct nl80211_global *global,
     710                 :            :                                const char *family, const char *group)
     711                 :            : {
     712                 :            :         struct nl_msg *msg;
     713                 :         12 :         int ret = -1;
     714                 :         12 :         struct family_data res = { group, -ENOENT };
     715                 :            : 
     716                 :         12 :         msg = nlmsg_alloc();
     717         [ -  + ]:         12 :         if (!msg)
     718                 :          0 :                 return -ENOMEM;
     719                 :         12 :         genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
     720                 :            :                     0, 0, CTRL_CMD_GETFAMILY, 0);
     721         [ +  - ]:         12 :         NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
     722                 :            : 
     723                 :         12 :         ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
     724                 :         12 :         msg = NULL;
     725         [ +  - ]:         12 :         if (ret == 0)
     726                 :         12 :                 ret = res.id;
     727                 :            : 
     728                 :            : nla_put_failure:
     729                 :         12 :         nlmsg_free(msg);
     730                 :         12 :         return ret;
     731                 :            : }
     732                 :            : 
     733                 :            : 
     734                 :      42761 : static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
     735                 :            :                           struct nl_msg *msg, int flags, uint8_t cmd)
     736                 :            : {
     737                 :      42761 :         return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
     738                 :            :                            0, flags, cmd, 0);
     739                 :            : }
     740                 :            : 
     741                 :            : 
     742                 :            : struct wiphy_idx_data {
     743                 :            :         int wiphy_idx;
     744                 :            :         enum nl80211_iftype nlmode;
     745                 :            :         u8 *macaddr;
     746                 :            : };
     747                 :            : 
     748                 :            : 
     749                 :        714 : static int netdev_info_handler(struct nl_msg *msg, void *arg)
     750                 :            : {
     751                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
     752                 :        714 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
     753                 :        714 :         struct wiphy_idx_data *info = arg;
     754                 :            : 
     755                 :        714 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
     756                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
     757                 :            : 
     758         [ +  - ]:        714 :         if (tb[NL80211_ATTR_WIPHY])
     759                 :        714 :                 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
     760                 :            : 
     761         [ +  - ]:        714 :         if (tb[NL80211_ATTR_IFTYPE])
     762                 :        714 :                 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
     763                 :            : 
     764 [ +  - ][ -  + ]:        714 :         if (tb[NL80211_ATTR_MAC] && info->macaddr)
     765                 :          0 :                 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
     766                 :            :                           ETH_ALEN);
     767                 :            : 
     768                 :        714 :         return NL_SKIP;
     769                 :            : }
     770                 :            : 
     771                 :            : 
     772                 :        271 : static int nl80211_get_wiphy_index(struct i802_bss *bss)
     773                 :            : {
     774                 :            :         struct nl_msg *msg;
     775                 :        271 :         struct wiphy_idx_data data = {
     776                 :            :                 .wiphy_idx = -1,
     777                 :            :                 .macaddr = NULL,
     778                 :            :         };
     779                 :            : 
     780                 :        271 :         msg = nlmsg_alloc();
     781         [ -  + ]:        271 :         if (!msg)
     782                 :          0 :                 return NL80211_IFTYPE_UNSPECIFIED;
     783                 :            : 
     784                 :        271 :         nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
     785                 :            : 
     786         [ -  + ]:        271 :         if (nl80211_set_iface_id(msg, bss) < 0)
     787                 :          0 :                 goto nla_put_failure;
     788                 :            : 
     789         [ +  - ]:        271 :         if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
     790                 :        271 :                 return data.wiphy_idx;
     791                 :          0 :         msg = NULL;
     792                 :            : nla_put_failure:
     793                 :          0 :         nlmsg_free(msg);
     794                 :        271 :         return -1;
     795                 :            : }
     796                 :            : 
     797                 :            : 
     798                 :        443 : static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
     799                 :            : {
     800                 :            :         struct nl_msg *msg;
     801                 :        443 :         struct wiphy_idx_data data = {
     802                 :            :                 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
     803                 :            :                 .macaddr = NULL,
     804                 :            :         };
     805                 :            : 
     806                 :        443 :         msg = nlmsg_alloc();
     807         [ -  + ]:        443 :         if (!msg)
     808                 :          0 :                 return -1;
     809                 :            : 
     810                 :        443 :         nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
     811                 :            : 
     812         [ -  + ]:        443 :         if (nl80211_set_iface_id(msg, bss) < 0)
     813                 :          0 :                 goto nla_put_failure;
     814                 :            : 
     815         [ +  - ]:        443 :         if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
     816                 :        443 :                 return data.nlmode;
     817                 :          0 :         msg = NULL;
     818                 :            : nla_put_failure:
     819                 :          0 :         nlmsg_free(msg);
     820                 :        443 :         return NL80211_IFTYPE_UNSPECIFIED;
     821                 :            : }
     822                 :            : 
     823                 :            : 
     824                 :          0 : static int nl80211_get_macaddr(struct i802_bss *bss)
     825                 :            : {
     826                 :            :         struct nl_msg *msg;
     827                 :          0 :         struct wiphy_idx_data data = {
     828                 :          0 :                 .macaddr = bss->addr,
     829                 :            :         };
     830                 :            : 
     831                 :          0 :         msg = nlmsg_alloc();
     832         [ #  # ]:          0 :         if (!msg)
     833                 :          0 :                 return NL80211_IFTYPE_UNSPECIFIED;
     834                 :            : 
     835                 :          0 :         nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
     836         [ #  # ]:          0 :         if (nl80211_set_iface_id(msg, bss) < 0)
     837                 :          0 :                 goto nla_put_failure;
     838                 :            : 
     839                 :          0 :         return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
     840                 :            : 
     841                 :            : nla_put_failure:
     842                 :          0 :         nlmsg_free(msg);
     843                 :          0 :         return NL80211_IFTYPE_UNSPECIFIED;
     844                 :            : }
     845                 :            : 
     846                 :            : 
     847                 :        254 : static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
     848                 :            :                                     struct nl80211_wiphy_data *w)
     849                 :            : {
     850                 :            :         struct nl_msg *msg;
     851                 :        254 :         int ret = -1;
     852                 :            : 
     853                 :        254 :         msg = nlmsg_alloc();
     854         [ -  + ]:        254 :         if (!msg)
     855                 :          0 :                 return -1;
     856                 :            : 
     857                 :        254 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
     858                 :            : 
     859         [ +  - ]:        254 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
     860                 :            : 
     861                 :        254 :         ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
     862                 :        254 :         msg = NULL;
     863         [ -  + ]:        254 :         if (ret) {
     864                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
     865                 :            :                            "failed: ret=%d (%s)",
     866                 :            :                            ret, strerror(-ret));
     867                 :          0 :                 goto nla_put_failure;
     868                 :            :         }
     869                 :        254 :         ret = 0;
     870                 :            : nla_put_failure:
     871                 :        254 :         nlmsg_free(msg);
     872                 :        254 :         return ret;
     873                 :            : }
     874                 :            : 
     875                 :            : 
     876                 :       1404 : static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
     877                 :            : {
     878                 :       1404 :         struct nl80211_wiphy_data *w = eloop_ctx;
     879                 :            :         int res;
     880                 :            : 
     881                 :       1404 :         wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
     882                 :            : 
     883                 :       1404 :         res = nl_recvmsgs(handle, w->nl_cb);
     884         [ -  + ]:       1404 :         if (res) {
     885                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
     886                 :            :                            __func__, res);
     887                 :            :         }
     888                 :       1404 : }
     889                 :            : 
     890                 :            : 
     891                 :       1404 : static int process_beacon_event(struct nl_msg *msg, void *arg)
     892                 :            : {
     893                 :       1404 :         struct nl80211_wiphy_data *w = arg;
     894                 :            :         struct wpa_driver_nl80211_data *drv;
     895                 :       1404 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
     896                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
     897                 :            :         union wpa_event_data event;
     898                 :            : 
     899                 :       1404 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
     900                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
     901                 :            : 
     902         [ -  + ]:       1404 :         if (gnlh->cmd != NL80211_CMD_FRAME) {
     903                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
     904                 :          0 :                            gnlh->cmd);
     905                 :          0 :                 return NL_SKIP;
     906                 :            :         }
     907                 :            : 
     908         [ -  + ]:       1404 :         if (!tb[NL80211_ATTR_FRAME])
     909                 :          0 :                 return NL_SKIP;
     910                 :            : 
     911         [ +  + ]:       2808 :         dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
     912                 :            :                          wiphy_list) {
     913                 :       1404 :                 os_memset(&event, 0, sizeof(event));
     914                 :       1404 :                 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
     915                 :       1404 :                 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
     916                 :       1404 :                 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
     917                 :            :         }
     918                 :            : 
     919                 :       1404 :         return NL_SKIP;
     920                 :            : }
     921                 :            : 
     922                 :            : 
     923                 :            : static struct nl80211_wiphy_data *
     924                 :        271 : nl80211_get_wiphy_data_ap(struct i802_bss *bss)
     925                 :            : {
     926                 :            :         static DEFINE_DL_LIST(nl80211_wiphys);
     927                 :            :         struct nl80211_wiphy_data *w;
     928                 :        271 :         int wiphy_idx, found = 0;
     929                 :            :         struct i802_bss *tmp_bss;
     930                 :            : 
     931         [ -  + ]:        271 :         if (bss->wiphy_data != NULL)
     932                 :          0 :                 return bss->wiphy_data;
     933                 :            : 
     934                 :        271 :         wiphy_idx = nl80211_get_wiphy_index(bss);
     935                 :            : 
     936         [ +  + ]:        291 :         dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
     937         [ +  + ]:         37 :                 if (w->wiphy_idx == wiphy_idx)
     938                 :         17 :                         goto add;
     939                 :            :         }
     940                 :            : 
     941                 :            :         /* alloc new one */
     942                 :        254 :         w = os_zalloc(sizeof(*w));
     943         [ -  + ]:        254 :         if (w == NULL)
     944                 :          0 :                 return NULL;
     945                 :        254 :         w->wiphy_idx = wiphy_idx;
     946                 :        254 :         dl_list_init(&w->bsss);
     947                 :        254 :         dl_list_init(&w->drvs);
     948                 :            : 
     949                 :        254 :         w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
     950         [ -  + ]:        254 :         if (!w->nl_cb) {
     951                 :          0 :                 os_free(w);
     952                 :          0 :                 return NULL;
     953                 :            :         }
     954                 :        254 :         nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
     955                 :        254 :         nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
     956                 :            :                   w);
     957                 :            : 
     958                 :        254 :         w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
     959                 :            :                                          "wiphy beacons");
     960         [ -  + ]:        254 :         if (w->nl_beacons == NULL) {
     961                 :          0 :                 os_free(w);
     962                 :          0 :                 return NULL;
     963                 :            :         }
     964                 :            : 
     965         [ -  + ]:        254 :         if (nl80211_register_beacons(bss->drv, w)) {
     966                 :          0 :                 nl_destroy_handles(&w->nl_beacons);
     967                 :          0 :                 os_free(w);
     968                 :          0 :                 return NULL;
     969                 :            :         }
     970                 :            : 
     971                 :        254 :         nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
     972                 :            : 
     973                 :        254 :         dl_list_add(&nl80211_wiphys, &w->list);
     974                 :            : 
     975                 :            : add:
     976                 :            :         /* drv entry for this bss already there? */
     977         [ +  + ]:        271 :         dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
     978         [ +  - ]:         17 :                 if (tmp_bss->drv == bss->drv) {
     979                 :         17 :                         found = 1;
     980                 :         17 :                         break;
     981                 :            :                 }
     982                 :            :         }
     983                 :            :         /* if not add it */
     984         [ +  + ]:        271 :         if (!found)
     985                 :        254 :                 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
     986                 :            : 
     987                 :        271 :         dl_list_add(&w->bsss, &bss->wiphy_list);
     988                 :        271 :         bss->wiphy_data = w;
     989                 :        271 :         return w;
     990                 :            : }
     991                 :            : 
     992                 :            : 
     993                 :        443 : static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
     994                 :            : {
     995                 :        443 :         struct nl80211_wiphy_data *w = bss->wiphy_data;
     996                 :            :         struct i802_bss *tmp_bss;
     997                 :        443 :         int found = 0;
     998                 :            : 
     999         [ +  + ]:        443 :         if (w == NULL)
    1000                 :        172 :                 return;
    1001                 :        271 :         bss->wiphy_data = NULL;
    1002                 :        271 :         dl_list_del(&bss->wiphy_list);
    1003                 :            : 
    1004                 :            :         /* still any for this drv present? */
    1005         [ +  + ]:        271 :         dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
    1006         [ +  - ]:         17 :                 if (tmp_bss->drv == bss->drv) {
    1007                 :         17 :                         found = 1;
    1008                 :         17 :                         break;
    1009                 :            :                 }
    1010                 :            :         }
    1011                 :            :         /* if not remove it */
    1012         [ +  + ]:        271 :         if (!found)
    1013                 :        254 :                 dl_list_del(&bss->drv->wiphy_list);
    1014                 :            : 
    1015         [ +  + ]:        271 :         if (!dl_list_empty(&w->bsss))
    1016                 :         17 :                 return;
    1017                 :            : 
    1018                 :        254 :         nl80211_destroy_eloop_handle(&w->nl_beacons);
    1019                 :            : 
    1020                 :        254 :         nl_cb_put(w->nl_cb);
    1021                 :        254 :         dl_list_del(&w->list);
    1022                 :        443 :         os_free(w);
    1023                 :            : }
    1024                 :            : 
    1025                 :            : 
    1026                 :        714 : static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
    1027                 :            : {
    1028                 :        714 :         struct i802_bss *bss = priv;
    1029                 :        714 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    1030         [ -  + ]:        714 :         if (!drv->associated)
    1031                 :          0 :                 return -1;
    1032                 :        714 :         os_memcpy(bssid, drv->bssid, ETH_ALEN);
    1033                 :        714 :         return 0;
    1034                 :            : }
    1035                 :            : 
    1036                 :            : 
    1037                 :        277 : static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
    1038                 :            : {
    1039                 :        277 :         struct i802_bss *bss = priv;
    1040                 :        277 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    1041         [ -  + ]:        277 :         if (!drv->associated)
    1042                 :          0 :                 return -1;
    1043                 :        277 :         os_memcpy(ssid, drv->ssid, drv->ssid_len);
    1044                 :        277 :         return drv->ssid_len;
    1045                 :            : }
    1046                 :            : 
    1047                 :            : 
    1048                 :       1877 : static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
    1049                 :            :                                           char *buf, size_t len, int del)
    1050                 :            : {
    1051                 :            :         union wpa_event_data event;
    1052                 :            : 
    1053                 :       1877 :         os_memset(&event, 0, sizeof(event));
    1054         [ -  + ]:       1877 :         if (len > sizeof(event.interface_status.ifname))
    1055                 :          0 :                 len = sizeof(event.interface_status.ifname) - 1;
    1056                 :       1877 :         os_memcpy(event.interface_status.ifname, buf, len);
    1057                 :       1877 :         event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
    1058                 :            :                 EVENT_INTERFACE_ADDED;
    1059                 :            : 
    1060 [ -  + ][ -  + ]:       1877 :         wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
    1061                 :            :                    del ? "DEL" : "NEW",
    1062                 :            :                    event.interface_status.ifname,
    1063                 :            :                    del ? "removed" : "added");
    1064                 :            : 
    1065         [ +  + ]:       1877 :         if (os_strcmp(drv->first_bss->ifname, event.interface_status.ifname) ==
    1066                 :            :             0) {
    1067         [ -  + ]:       1843 :                 if (del) {
    1068         [ #  # ]:          0 :                         if (drv->if_removed) {
    1069                 :          0 :                                 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
    1070                 :            :                                            "already set - ignore event");
    1071                 :          0 :                                 return;
    1072                 :            :                         }
    1073                 :          0 :                         drv->if_removed = 1;
    1074                 :            :                 } else {
    1075         [ -  + ]:       1843 :                         if (if_nametoindex(drv->first_bss->ifname) == 0) {
    1076                 :          0 :                                 wpa_printf(MSG_DEBUG, "nl80211: Interface %s "
    1077                 :            :                                            "does not exist - ignore "
    1078                 :            :                                            "RTM_NEWLINK",
    1079                 :          0 :                                            drv->first_bss->ifname);
    1080                 :          0 :                                 return;
    1081                 :            :                         }
    1082         [ +  - ]:       1843 :                         if (!drv->if_removed) {
    1083                 :       1843 :                                 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
    1084                 :            :                                            "already cleared - ignore event");
    1085                 :       1843 :                                 return;
    1086                 :            :                         }
    1087                 :          0 :                         drv->if_removed = 0;
    1088                 :            :                 }
    1089                 :            :         }
    1090                 :            : 
    1091                 :       1877 :         wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
    1092                 :            : }
    1093                 :            : 
    1094                 :            : 
    1095                 :          0 : static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
    1096                 :            :                                          u8 *buf, size_t len)
    1097                 :            : {
    1098                 :            :         int attrlen, rta_len;
    1099                 :            :         struct rtattr *attr;
    1100                 :            : 
    1101                 :          0 :         attrlen = len;
    1102                 :          0 :         attr = (struct rtattr *) buf;
    1103                 :            : 
    1104                 :          0 :         rta_len = RTA_ALIGN(sizeof(struct rtattr));
    1105 [ #  # ][ #  # ]:          0 :         while (RTA_OK(attr, attrlen)) {
                 [ #  # ]
    1106         [ #  # ]:          0 :                 if (attr->rta_type == IFLA_IFNAME) {
    1107         [ #  # ]:          0 :                         if (os_strcmp(((char *) attr) + rta_len,
    1108                 :            :                                       drv->first_bss->ifname) == 0)
    1109                 :          0 :                                 return 1;
    1110                 :            :                         else
    1111                 :          0 :                                 break;
    1112                 :            :                 }
    1113                 :          0 :                 attr = RTA_NEXT(attr, attrlen);
    1114                 :            :         }
    1115                 :            : 
    1116                 :          0 :         return 0;
    1117                 :            : }
    1118                 :            : 
    1119                 :            : 
    1120                 :       8426 : static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
    1121                 :            :                                           int ifindex, u8 *buf, size_t len)
    1122                 :            : {
    1123         [ +  + ]:       8426 :         if (drv->ifindex == ifindex)
    1124                 :       1855 :                 return 1;
    1125                 :            : 
    1126 [ -  + ][ #  # ]:       6571 :         if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
    1127                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
    1128                 :            :                            "interface");
    1129                 :          0 :                 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0);
    1130                 :          0 :                 return 1;
    1131                 :            :         }
    1132                 :            : 
    1133                 :       8426 :         return 0;
    1134                 :            : }
    1135                 :            : 
    1136                 :            : 
    1137                 :            : static struct wpa_driver_nl80211_data *
    1138                 :       9008 : nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
    1139                 :            : {
    1140                 :            :         struct wpa_driver_nl80211_data *drv;
    1141         [ +  + ]:      15498 :         dl_list_for_each(drv, &global->interfaces,
    1142                 :            :                          struct wpa_driver_nl80211_data, list) {
    1143   [ +  +  +  + ]:      14997 :                 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
    1144                 :       6571 :                     have_ifidx(drv, idx))
    1145                 :       1936 :                         return drv;
    1146                 :            :         }
    1147                 :       9008 :         return NULL;
    1148                 :            : }
    1149                 :            : 
    1150                 :            : 
    1151                 :       8864 : static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
    1152                 :            :                                                  struct ifinfomsg *ifi,
    1153                 :            :                                                  u8 *buf, size_t len)
    1154                 :            : {
    1155                 :       8864 :         struct nl80211_global *global = ctx;
    1156                 :            :         struct wpa_driver_nl80211_data *drv;
    1157                 :            :         int attrlen, rta_len;
    1158                 :            :         struct rtattr *attr;
    1159                 :       8864 :         u32 brid = 0;
    1160                 :            :         char namebuf[IFNAMSIZ];
    1161                 :            : 
    1162                 :       8864 :         drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
    1163         [ +  + ]:       8864 :         if (!drv) {
    1164                 :       6928 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
    1165                 :            :                            "ifindex %d", ifi->ifi_index);
    1166                 :       6928 :                 return;
    1167                 :            :         }
    1168                 :            : 
    1169 [ -  + ][ +  + ]:       1936 :         wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
         [ +  + ][ +  + ]
    1170                 :            :                    "(%s%s%s%s)",
    1171                 :            :                    drv->operstate, ifi->ifi_flags,
    1172                 :       1936 :                    (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
    1173                 :       1936 :                    (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
    1174                 :       1936 :                    (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
    1175                 :       1936 :                    (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
    1176                 :            : 
    1177 [ +  + ][ +  - ]:       1936 :         if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
    1178   [ +  -  +  - ]:        118 :                 if (if_indextoname(ifi->ifi_index, namebuf) &&
    1179                 :         59 :                     linux_iface_up(drv->global->ioctl_sock,
    1180                 :         59 :                                    drv->first_bss->ifname) > 0) {
    1181                 :         59 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
    1182                 :            :                                    "event since interface %s is up", namebuf);
    1183                 :         59 :                         return;
    1184                 :            :                 }
    1185                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
    1186         [ #  # ]:          0 :                 if (drv->ignore_if_down_event) {
    1187                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
    1188                 :            :                                    "event generated by mode change");
    1189                 :          0 :                         drv->ignore_if_down_event = 0;
    1190                 :            :                 } else {
    1191                 :          0 :                         drv->if_disabled = 1;
    1192                 :          0 :                         wpa_supplicant_event(drv->ctx,
    1193                 :            :                                              EVENT_INTERFACE_DISABLED, NULL);
    1194                 :            :                 }
    1195                 :            :         }
    1196                 :            : 
    1197 [ -  + ][ #  # ]:       1877 :         if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
    1198   [ #  #  #  # ]:          0 :                 if (if_indextoname(ifi->ifi_index, namebuf) &&
    1199                 :          0 :                     linux_iface_up(drv->global->ioctl_sock,
    1200                 :          0 :                                    drv->first_bss->ifname) == 0) {
    1201                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
    1202                 :            :                                    "event since interface %s is down",
    1203                 :            :                                    namebuf);
    1204         [ #  # ]:          0 :                 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
    1205                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
    1206                 :            :                                    "event since interface %s does not exist",
    1207                 :          0 :                                    drv->first_bss->ifname);
    1208         [ #  # ]:          0 :                 } else if (drv->if_removed) {
    1209                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
    1210                 :            :                                    "event since interface %s is marked "
    1211                 :          0 :                                    "removed", drv->first_bss->ifname);
    1212                 :            :                 } else {
    1213                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Interface up");
    1214                 :          0 :                         drv->if_disabled = 0;
    1215                 :          0 :                         wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
    1216                 :            :                                              NULL);
    1217                 :            :                 }
    1218                 :            :         }
    1219                 :            : 
    1220                 :            :         /*
    1221                 :            :          * Some drivers send the association event before the operup event--in
    1222                 :            :          * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
    1223                 :            :          * fails. This will hit us when wpa_supplicant does not need to do
    1224                 :            :          * IEEE 802.1X authentication
    1225                 :            :          */
    1226 [ +  + ][ +  + ]:       1877 :         if (drv->operstate == 1 &&
    1227         [ +  + ]:        689 :             (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
    1228                 :        689 :             !(ifi->ifi_flags & IFF_RUNNING))
    1229                 :         72 :                 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
    1230                 :            :                                        -1, IF_OPER_UP);
    1231                 :            : 
    1232                 :       1877 :         attrlen = len;
    1233                 :       1877 :         attr = (struct rtattr *) buf;
    1234                 :       1877 :         rta_len = RTA_ALIGN(sizeof(struct rtattr));
    1235 [ +  + ][ +  - ]:      33786 :         while (RTA_OK(attr, attrlen)) {
                 [ +  - ]
    1236         [ +  + ]:      31909 :                 if (attr->rta_type == IFLA_IFNAME) {
    1237                 :       1877 :                         wpa_driver_nl80211_event_link(
    1238                 :            :                                 drv,
    1239                 :       1877 :                                 ((char *) attr) + rta_len,
    1240                 :       1877 :                                 attr->rta_len - rta_len, 0);
    1241         [ -  + ]:      30032 :                 } else if (attr->rta_type == IFLA_MASTER)
    1242                 :          0 :                         brid = nla_get_u32((struct nlattr *) attr);
    1243                 :      31909 :                 attr = RTA_NEXT(attr, attrlen);
    1244                 :            :         }
    1245                 :            : 
    1246 [ -  + ][ #  # ]:       1877 :         if (ifi->ifi_family == AF_BRIDGE && brid) {
    1247                 :            :                 /* device has been added to bridge */
    1248                 :          0 :                 if_indextoname(brid, namebuf);
    1249                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
    1250                 :            :                            brid, namebuf);
    1251                 :       8864 :                 add_ifidx(drv, brid);
    1252                 :            :         }
    1253                 :            : }
    1254                 :            : 
    1255                 :            : 
    1256                 :        144 : static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
    1257                 :            :                                                  struct ifinfomsg *ifi,
    1258                 :            :                                                  u8 *buf, size_t len)
    1259                 :            : {
    1260                 :        144 :         struct nl80211_global *global = ctx;
    1261                 :            :         struct wpa_driver_nl80211_data *drv;
    1262                 :            :         int attrlen, rta_len;
    1263                 :            :         struct rtattr *attr;
    1264                 :        144 :         u32 brid = 0;
    1265                 :            : 
    1266                 :        144 :         drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
    1267         [ +  - ]:        144 :         if (!drv) {
    1268                 :        144 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for "
    1269                 :            :                            "foreign ifindex %d", ifi->ifi_index);
    1270                 :        144 :                 return;
    1271                 :            :         }
    1272                 :            : 
    1273                 :          0 :         attrlen = len;
    1274                 :          0 :         attr = (struct rtattr *) buf;
    1275                 :            : 
    1276                 :          0 :         rta_len = RTA_ALIGN(sizeof(struct rtattr));
    1277 [ #  # ][ #  # ]:          0 :         while (RTA_OK(attr, attrlen)) {
                 [ #  # ]
    1278         [ #  # ]:          0 :                 if (attr->rta_type == IFLA_IFNAME) {
    1279                 :          0 :                         wpa_driver_nl80211_event_link(
    1280                 :            :                                 drv,
    1281                 :          0 :                                 ((char *) attr) + rta_len,
    1282                 :          0 :                                 attr->rta_len - rta_len, 1);
    1283         [ #  # ]:          0 :                 } else if (attr->rta_type == IFLA_MASTER)
    1284                 :          0 :                         brid = nla_get_u32((struct nlattr *) attr);
    1285                 :          0 :                 attr = RTA_NEXT(attr, attrlen);
    1286                 :            :         }
    1287                 :            : 
    1288 [ #  # ][ #  # ]:          0 :         if (ifi->ifi_family == AF_BRIDGE && brid) {
    1289                 :            :                 /* device has been removed from bridge */
    1290                 :            :                 char namebuf[IFNAMSIZ];
    1291                 :          0 :                 if_indextoname(brid, namebuf);
    1292                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
    1293                 :            :                            "%s", brid, namebuf);
    1294                 :          0 :                 del_ifidx(drv, brid);
    1295                 :            :         }
    1296                 :            : }
    1297                 :            : 
    1298                 :            : 
    1299                 :        422 : static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
    1300                 :            :                             const u8 *frame, size_t len)
    1301                 :            : {
    1302                 :            :         const struct ieee80211_mgmt *mgmt;
    1303                 :            :         union wpa_event_data event;
    1304                 :            : 
    1305                 :        422 :         wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
    1306                 :        422 :         mgmt = (const struct ieee80211_mgmt *) frame;
    1307         [ -  + ]:        422 :         if (len < 24 + sizeof(mgmt->u.auth)) {
    1308                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
    1309                 :            :                            "frame");
    1310                 :        422 :                 return;
    1311                 :            :         }
    1312                 :            : 
    1313                 :        422 :         os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
    1314                 :        422 :         os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
    1315                 :        422 :         os_memset(&event, 0, sizeof(event));
    1316                 :        422 :         os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
    1317                 :        422 :         event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
    1318                 :        422 :         event.auth.auth_transaction =
    1319                 :        422 :                 le_to_host16(mgmt->u.auth.auth_transaction);
    1320                 :        422 :         event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
    1321         [ +  + ]:        422 :         if (len > 24 + sizeof(mgmt->u.auth)) {
    1322                 :         43 :                 event.auth.ies = mgmt->u.auth.variable;
    1323                 :         43 :                 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
    1324                 :            :         }
    1325                 :            : 
    1326                 :        422 :         wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
    1327                 :            : }
    1328                 :            : 
    1329                 :            : 
    1330                 :          0 : static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
    1331                 :            : {
    1332                 :            :         struct nl_msg *msg;
    1333                 :            :         int ret;
    1334                 :            :         struct nl80211_bss_info_arg arg;
    1335                 :            : 
    1336                 :          0 :         os_memset(&arg, 0, sizeof(arg));
    1337                 :          0 :         msg = nlmsg_alloc();
    1338         [ #  # ]:          0 :         if (!msg)
    1339                 :          0 :                 goto nla_put_failure;
    1340                 :            : 
    1341                 :          0 :         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
    1342         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    1343                 :            : 
    1344                 :          0 :         arg.drv = drv;
    1345                 :          0 :         ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
    1346                 :          0 :         msg = NULL;
    1347         [ #  # ]:          0 :         if (ret == 0) {
    1348                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
    1349                 :            :                            "associated BSS from scan results: %u MHz",
    1350                 :            :                            arg.assoc_freq);
    1351         [ #  # ]:          0 :                 if (arg.assoc_freq)
    1352                 :          0 :                         drv->assoc_freq = arg.assoc_freq;
    1353                 :          0 :                 return drv->assoc_freq;
    1354                 :            :         }
    1355                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
    1356                 :            :                    "(%s)", ret, strerror(-ret));
    1357                 :            : nla_put_failure:
    1358                 :          0 :         nlmsg_free(msg);
    1359                 :          0 :         return drv->assoc_freq;
    1360                 :            : }
    1361                 :            : 
    1362                 :            : 
    1363                 :        408 : static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
    1364                 :            :                             const u8 *frame, size_t len)
    1365                 :            : {
    1366                 :            :         const struct ieee80211_mgmt *mgmt;
    1367                 :            :         union wpa_event_data event;
    1368                 :            :         u16 status;
    1369                 :            : 
    1370                 :        408 :         wpa_printf(MSG_DEBUG, "nl80211: Associate event");
    1371                 :        408 :         mgmt = (const struct ieee80211_mgmt *) frame;
    1372         [ -  + ]:        408 :         if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
    1373                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
    1374                 :            :                            "frame");
    1375                 :          0 :                 return;
    1376                 :            :         }
    1377                 :            : 
    1378                 :        408 :         status = le_to_host16(mgmt->u.assoc_resp.status_code);
    1379         [ -  + ]:        408 :         if (status != WLAN_STATUS_SUCCESS) {
    1380                 :          0 :                 os_memset(&event, 0, sizeof(event));
    1381                 :          0 :                 event.assoc_reject.bssid = mgmt->bssid;
    1382         [ #  # ]:          0 :                 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
    1383                 :          0 :                         event.assoc_reject.resp_ies =
    1384                 :          0 :                                 (u8 *) mgmt->u.assoc_resp.variable;
    1385                 :          0 :                         event.assoc_reject.resp_ies_len =
    1386                 :          0 :                                 len - 24 - sizeof(mgmt->u.assoc_resp);
    1387                 :            :                 }
    1388                 :          0 :                 event.assoc_reject.status_code = status;
    1389                 :            : 
    1390                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
    1391                 :          0 :                 return;
    1392                 :            :         }
    1393                 :            : 
    1394                 :        408 :         drv->associated = 1;
    1395                 :        408 :         os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
    1396                 :        408 :         os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
    1397                 :            : 
    1398                 :        408 :         os_memset(&event, 0, sizeof(event));
    1399         [ +  - ]:        408 :         if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
    1400                 :        408 :                 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
    1401                 :        408 :                 event.assoc_info.resp_ies_len =
    1402                 :        408 :                         len - 24 - sizeof(mgmt->u.assoc_resp);
    1403                 :            :         }
    1404                 :            : 
    1405                 :        408 :         event.assoc_info.freq = drv->assoc_freq;
    1406                 :            : 
    1407                 :        408 :         wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
    1408                 :            : }
    1409                 :            : 
    1410                 :            : 
    1411                 :        408 : static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
    1412                 :            :                                enum nl80211_commands cmd, struct nlattr *status,
    1413                 :            :                                struct nlattr *addr, struct nlattr *req_ie,
    1414                 :            :                                struct nlattr *resp_ie)
    1415                 :            : {
    1416                 :            :         union wpa_event_data event;
    1417                 :            : 
    1418         [ +  - ]:        408 :         if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
    1419                 :            :                 /*
    1420                 :            :                  * Avoid reporting two association events that would confuse
    1421                 :            :                  * the core code.
    1422                 :            :                  */
    1423                 :        408 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
    1424                 :            :                            "when using userspace SME", cmd);
    1425                 :        408 :                 return;
    1426                 :            :         }
    1427                 :            : 
    1428         [ #  # ]:          0 :         if (cmd == NL80211_CMD_CONNECT)
    1429                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
    1430         [ #  # ]:          0 :         else if (cmd == NL80211_CMD_ROAM)
    1431                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
    1432                 :            : 
    1433                 :          0 :         os_memset(&event, 0, sizeof(event));
    1434   [ #  #  #  # ]:          0 :         if (cmd == NL80211_CMD_CONNECT &&
    1435                 :          0 :             nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
    1436         [ #  # ]:          0 :                 if (addr)
    1437                 :          0 :                         event.assoc_reject.bssid = nla_data(addr);
    1438         [ #  # ]:          0 :                 if (resp_ie) {
    1439                 :          0 :                         event.assoc_reject.resp_ies = nla_data(resp_ie);
    1440                 :          0 :                         event.assoc_reject.resp_ies_len = nla_len(resp_ie);
    1441                 :            :                 }
    1442                 :          0 :                 event.assoc_reject.status_code = nla_get_u16(status);
    1443                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
    1444                 :          0 :                 return;
    1445                 :            :         }
    1446                 :            : 
    1447                 :          0 :         drv->associated = 1;
    1448         [ #  # ]:          0 :         if (addr) {
    1449                 :          0 :                 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
    1450                 :          0 :                 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
    1451                 :            :         }
    1452                 :            : 
    1453         [ #  # ]:          0 :         if (req_ie) {
    1454                 :          0 :                 event.assoc_info.req_ies = nla_data(req_ie);
    1455                 :          0 :                 event.assoc_info.req_ies_len = nla_len(req_ie);
    1456                 :            :         }
    1457         [ #  # ]:          0 :         if (resp_ie) {
    1458                 :          0 :                 event.assoc_info.resp_ies = nla_data(resp_ie);
    1459                 :          0 :                 event.assoc_info.resp_ies_len = nla_len(resp_ie);
    1460                 :            :         }
    1461                 :            : 
    1462                 :          0 :         event.assoc_info.freq = nl80211_get_assoc_freq(drv);
    1463                 :            : 
    1464                 :        408 :         wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
    1465                 :            : }
    1466                 :            : 
    1467                 :            : 
    1468                 :        399 : static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
    1469                 :            :                                   struct nlattr *reason, struct nlattr *addr,
    1470                 :            :                                   struct nlattr *by_ap)
    1471                 :            : {
    1472                 :            :         union wpa_event_data data;
    1473                 :        399 :         unsigned int locally_generated = by_ap == NULL;
    1474                 :            : 
    1475         [ +  - ]:        399 :         if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
    1476                 :            :                 /*
    1477                 :            :                  * Avoid reporting two disassociation events that could
    1478                 :            :                  * confuse the core code.
    1479                 :            :                  */
    1480                 :        399 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
    1481                 :            :                            "event when using userspace SME");
    1482                 :        399 :                 return;
    1483                 :            :         }
    1484                 :            : 
    1485         [ #  # ]:          0 :         if (drv->ignore_next_local_disconnect) {
    1486                 :          0 :                 drv->ignore_next_local_disconnect = 0;
    1487         [ #  # ]:          0 :                 if (locally_generated) {
    1488                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
    1489                 :            :                                    "event triggered during reassociation");
    1490                 :          0 :                         return;
    1491                 :            :                 }
    1492                 :          0 :                 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
    1493                 :            :                            "disconnect but got another disconnect "
    1494                 :            :                            "event first");
    1495                 :            :         }
    1496                 :            : 
    1497                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
    1498                 :          0 :         nl80211_mark_disconnected(drv);
    1499                 :          0 :         os_memset(&data, 0, sizeof(data));
    1500         [ #  # ]:          0 :         if (reason)
    1501                 :          0 :                 data.deauth_info.reason_code = nla_get_u16(reason);
    1502                 :          0 :         data.deauth_info.locally_generated = by_ap == NULL;
    1503                 :        399 :         wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
    1504                 :            : }
    1505                 :            : 
    1506                 :            : 
    1507                 :          0 : static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
    1508                 :            :                                  struct nlattr *ifindex, struct nlattr *freq,
    1509                 :            :                                  struct nlattr *type, struct nlattr *bw,
    1510                 :            :                                  struct nlattr *cf1, struct nlattr *cf2)
    1511                 :            : {
    1512                 :            :         struct i802_bss *bss;
    1513                 :            :         union wpa_event_data data;
    1514                 :          0 :         int ht_enabled = 1;
    1515                 :          0 :         int chan_offset = 0;
    1516                 :            :         int ifidx;
    1517                 :            : 
    1518                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
    1519                 :            : 
    1520         [ #  # ]:          0 :         if (!freq)
    1521                 :          0 :                 return;
    1522                 :            : 
    1523                 :          0 :         ifidx = nla_get_u32(ifindex);
    1524         [ #  # ]:          0 :         for (bss = drv->first_bss; bss; bss = bss->next)
    1525         [ #  # ]:          0 :                 if (bss->ifindex == ifidx)
    1526                 :          0 :                         break;
    1527                 :            : 
    1528         [ #  # ]:          0 :         if (bss == NULL) {
    1529                 :          0 :                 wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
    1530                 :            :                            ifidx);
    1531                 :          0 :                 return;
    1532                 :            :         }
    1533                 :            : 
    1534         [ #  # ]:          0 :         if (type) {
    1535   [ #  #  #  #  :          0 :                 switch (nla_get_u32(type)) {
                      # ]
    1536                 :            :                 case NL80211_CHAN_NO_HT:
    1537                 :          0 :                         ht_enabled = 0;
    1538                 :          0 :                         break;
    1539                 :            :                 case NL80211_CHAN_HT20:
    1540                 :          0 :                         break;
    1541                 :            :                 case NL80211_CHAN_HT40PLUS:
    1542                 :          0 :                         chan_offset = 1;
    1543                 :          0 :                         break;
    1544                 :            :                 case NL80211_CHAN_HT40MINUS:
    1545                 :          0 :                         chan_offset = -1;
    1546                 :          0 :                         break;
    1547                 :            :                 }
    1548                 :            :         }
    1549                 :            : 
    1550                 :          0 :         os_memset(&data, 0, sizeof(data));
    1551                 :          0 :         data.ch_switch.freq = nla_get_u32(freq);
    1552                 :          0 :         data.ch_switch.ht_enabled = ht_enabled;
    1553                 :          0 :         data.ch_switch.ch_offset = chan_offset;
    1554         [ #  # ]:          0 :         if (bw)
    1555                 :          0 :                 data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
    1556         [ #  # ]:          0 :         if (cf1)
    1557                 :          0 :                 data.ch_switch.cf1 = nla_get_u32(cf1);
    1558         [ #  # ]:          0 :         if (cf2)
    1559                 :          0 :                 data.ch_switch.cf2 = nla_get_u32(cf2);
    1560                 :            : 
    1561                 :          0 :         bss->freq = data.ch_switch.freq;
    1562                 :            : 
    1563                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
    1564                 :            : }
    1565                 :            : 
    1566                 :            : 
    1567                 :          3 : static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
    1568                 :            :                                enum nl80211_commands cmd, struct nlattr *addr)
    1569                 :            : {
    1570                 :            :         union wpa_event_data event;
    1571                 :            :         enum wpa_event_type ev;
    1572                 :            : 
    1573         [ -  + ]:          3 :         if (nla_len(addr) != ETH_ALEN)
    1574                 :          0 :                 return;
    1575                 :            : 
    1576                 :          3 :         wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
    1577                 :          3 :                    cmd, MAC2STR((u8 *) nla_data(addr)));
    1578                 :            : 
    1579         [ +  - ]:          3 :         if (cmd == NL80211_CMD_AUTHENTICATE)
    1580                 :          3 :                 ev = EVENT_AUTH_TIMED_OUT;
    1581         [ #  # ]:          0 :         else if (cmd == NL80211_CMD_ASSOCIATE)
    1582                 :          0 :                 ev = EVENT_ASSOC_TIMED_OUT;
    1583                 :            :         else
    1584                 :          0 :                 return;
    1585                 :            : 
    1586                 :          3 :         os_memset(&event, 0, sizeof(event));
    1587                 :          3 :         os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
    1588                 :          3 :         wpa_supplicant_event(drv->ctx, ev, &event);
    1589                 :            : }
    1590                 :            : 
    1591                 :            : 
    1592                 :       2461 : static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
    1593                 :            :                             struct nlattr *freq, struct nlattr *sig,
    1594                 :            :                             const u8 *frame, size_t len)
    1595                 :            : {
    1596                 :            :         const struct ieee80211_mgmt *mgmt;
    1597                 :            :         union wpa_event_data event;
    1598                 :            :         u16 fc, stype;
    1599                 :       2461 :         int ssi_signal = 0;
    1600                 :       2461 :         int rx_freq = 0;
    1601                 :            : 
    1602                 :       2461 :         wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
    1603                 :       2461 :         mgmt = (const struct ieee80211_mgmt *) frame;
    1604         [ -  + ]:       2461 :         if (len < 24) {
    1605                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
    1606                 :       2461 :                 return;
    1607                 :            :         }
    1608                 :            : 
    1609                 :       2461 :         fc = le_to_host16(mgmt->frame_control);
    1610                 :       2461 :         stype = WLAN_FC_GET_STYPE(fc);
    1611                 :            : 
    1612         [ +  - ]:       2461 :         if (sig)
    1613                 :       2461 :                 ssi_signal = (s32) nla_get_u32(sig);
    1614                 :            : 
    1615                 :       2461 :         os_memset(&event, 0, sizeof(event));
    1616         [ +  - ]:       2461 :         if (freq) {
    1617                 :       2461 :                 event.rx_mgmt.freq = nla_get_u32(freq);
    1618                 :       2461 :                 rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
    1619                 :            :         }
    1620                 :       2461 :         wpa_printf(MSG_DEBUG,
    1621                 :            :                    "nl80211: RX frame freq=%d ssi_signal=%d stype=%u len=%u",
    1622                 :            :                    rx_freq, ssi_signal, stype, (unsigned int) len);
    1623                 :       2461 :         event.rx_mgmt.frame = frame;
    1624                 :       2461 :         event.rx_mgmt.frame_len = len;
    1625                 :       2461 :         event.rx_mgmt.ssi_signal = ssi_signal;
    1626                 :       2461 :         wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
    1627                 :            : }
    1628                 :            : 
    1629                 :            : 
    1630                 :       1480 : static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
    1631                 :            :                                       struct nlattr *cookie, const u8 *frame,
    1632                 :            :                                       size_t len, struct nlattr *ack)
    1633                 :            : {
    1634                 :            :         union wpa_event_data event;
    1635                 :            :         const struct ieee80211_hdr *hdr;
    1636                 :            :         u16 fc;
    1637                 :            : 
    1638                 :       1480 :         wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
    1639         [ +  + ]:       1480 :         if (!is_ap_interface(drv->nlmode)) {
    1640                 :            :                 u64 cookie_val;
    1641                 :            : 
    1642         [ -  + ]:        342 :                 if (!cookie)
    1643                 :          0 :                         return;
    1644                 :            : 
    1645                 :        342 :                 cookie_val = nla_get_u64(cookie);
    1646         [ +  + ]:        342 :                 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
    1647                 :            :                            " cookie=0%llx%s (ack=%d)",
    1648                 :            :                            (long long unsigned int) cookie_val,
    1649                 :        342 :                            cookie_val == drv->send_action_cookie ?
    1650                 :            :                            " (match)" : " (unknown)", ack != NULL);
    1651         [ +  + ]:        342 :                 if (cookie_val != drv->send_action_cookie)
    1652                 :         57 :                         return;
    1653                 :            :         }
    1654                 :            : 
    1655                 :       1423 :         hdr = (const struct ieee80211_hdr *) frame;
    1656                 :       1423 :         fc = le_to_host16(hdr->frame_control);
    1657                 :            : 
    1658                 :       1423 :         os_memset(&event, 0, sizeof(event));
    1659                 :       1423 :         event.tx_status.type = WLAN_FC_GET_TYPE(fc);
    1660                 :       1423 :         event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
    1661                 :       1423 :         event.tx_status.dst = hdr->addr1;
    1662                 :       1423 :         event.tx_status.data = frame;
    1663                 :       1423 :         event.tx_status.data_len = len;
    1664                 :       1423 :         event.tx_status.ack = ack != NULL;
    1665                 :       1480 :         wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
    1666                 :            : }
    1667                 :            : 
    1668                 :            : 
    1669                 :        405 : static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
    1670                 :            :                                        enum wpa_event_type type,
    1671                 :            :                                        const u8 *frame, size_t len)
    1672                 :            : {
    1673                 :            :         const struct ieee80211_mgmt *mgmt;
    1674                 :            :         union wpa_event_data event;
    1675                 :        405 :         const u8 *bssid = NULL;
    1676                 :        405 :         u16 reason_code = 0;
    1677                 :            : 
    1678         [ +  - ]:        405 :         if (type == EVENT_DEAUTH)
    1679                 :        405 :                 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
    1680                 :            :         else
    1681                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
    1682                 :            : 
    1683                 :        405 :         mgmt = (const struct ieee80211_mgmt *) frame;
    1684         [ +  - ]:        405 :         if (len >= 24) {
    1685                 :        405 :                 bssid = mgmt->bssid;
    1686                 :            : 
    1687 [ +  - ][ +  + ]:        405 :                 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
    1688         [ +  + ]:        356 :                     !drv->associated &&
    1689         [ +  + ]:        103 :                     os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
    1690         [ +  - ]:         26 :                     os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
    1691                 :         26 :                     os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
    1692                 :            :                         /*
    1693                 :            :                          * Avoid issues with some roaming cases where
    1694                 :            :                          * disconnection event for the old AP may show up after
    1695                 :            :                          * we have started connection with the new AP.
    1696                 :            :                          */
    1697                 :         26 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
    1698                 :        156 :                                    MAC2STR(bssid),
    1699                 :        156 :                                    MAC2STR(drv->auth_attempt_bssid));
    1700                 :         26 :                         return;
    1701                 :            :                 }
    1702                 :            : 
    1703 [ +  + ][ -  + ]:        379 :                 if (drv->associated != 0 &&
    1704         [ #  # ]:          0 :                     os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
    1705                 :          0 :                     os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
    1706                 :            :                         /*
    1707                 :            :                          * We have presumably received this deauth as a
    1708                 :            :                          * response to a clear_state_mismatch() outgoing
    1709                 :            :                          * deauth.  Don't let it take us offline!
    1710                 :            :                          */
    1711                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
    1712                 :            :                                    "from Unknown BSSID " MACSTR " -- ignoring",
    1713                 :          0 :                                    MAC2STR(bssid));
    1714                 :          0 :                         return;
    1715                 :            :                 }
    1716                 :            :         }
    1717                 :            : 
    1718                 :        379 :         nl80211_mark_disconnected(drv);
    1719                 :        379 :         os_memset(&event, 0, sizeof(event));
    1720                 :            : 
    1721                 :            :         /* Note: Same offset for Reason Code in both frame subtypes */
    1722         [ +  - ]:        379 :         if (len >= 24 + sizeof(mgmt->u.deauth))
    1723                 :        379 :                 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
    1724                 :            : 
    1725         [ -  + ]:        379 :         if (type == EVENT_DISASSOC) {
    1726                 :          0 :                 event.disassoc_info.locally_generated =
    1727                 :          0 :                         !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
    1728                 :          0 :                 event.disassoc_info.addr = bssid;
    1729                 :          0 :                 event.disassoc_info.reason_code = reason_code;
    1730         [ #  # ]:          0 :                 if (frame + len > mgmt->u.disassoc.variable) {
    1731                 :          0 :                         event.disassoc_info.ie = mgmt->u.disassoc.variable;
    1732                 :          0 :                         event.disassoc_info.ie_len = frame + len -
    1733                 :            :                                 mgmt->u.disassoc.variable;
    1734                 :            :                 }
    1735                 :            :         } else {
    1736                 :        379 :                 event.deauth_info.locally_generated =
    1737                 :        379 :                         !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
    1738                 :        379 :                 event.deauth_info.addr = bssid;
    1739                 :        379 :                 event.deauth_info.reason_code = reason_code;
    1740         [ -  + ]:        379 :                 if (frame + len > mgmt->u.deauth.variable) {
    1741                 :          0 :                         event.deauth_info.ie = mgmt->u.deauth.variable;
    1742                 :          0 :                         event.deauth_info.ie_len = frame + len -
    1743                 :            :                                 mgmt->u.deauth.variable;
    1744                 :            :                 }
    1745                 :            :         }
    1746                 :            : 
    1747                 :        405 :         wpa_supplicant_event(drv->ctx, type, &event);
    1748                 :            : }
    1749                 :            : 
    1750                 :            : 
    1751                 :          0 : static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
    1752                 :            :                                          enum wpa_event_type type,
    1753                 :            :                                          const u8 *frame, size_t len)
    1754                 :            : {
    1755                 :            :         const struct ieee80211_mgmt *mgmt;
    1756                 :            :         union wpa_event_data event;
    1757                 :          0 :         u16 reason_code = 0;
    1758                 :            : 
    1759         [ #  # ]:          0 :         if (type == EVENT_UNPROT_DEAUTH)
    1760                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
    1761                 :            :         else
    1762                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
    1763                 :            : 
    1764         [ #  # ]:          0 :         if (len < 24)
    1765                 :          0 :                 return;
    1766                 :            : 
    1767                 :          0 :         mgmt = (const struct ieee80211_mgmt *) frame;
    1768                 :            : 
    1769                 :          0 :         os_memset(&event, 0, sizeof(event));
    1770                 :            :         /* Note: Same offset for Reason Code in both frame subtypes */
    1771         [ #  # ]:          0 :         if (len >= 24 + sizeof(mgmt->u.deauth))
    1772                 :          0 :                 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
    1773                 :            : 
    1774         [ #  # ]:          0 :         if (type == EVENT_UNPROT_DISASSOC) {
    1775                 :          0 :                 event.unprot_disassoc.sa = mgmt->sa;
    1776                 :          0 :                 event.unprot_disassoc.da = mgmt->da;
    1777                 :          0 :                 event.unprot_disassoc.reason_code = reason_code;
    1778                 :            :         } else {
    1779                 :          0 :                 event.unprot_deauth.sa = mgmt->sa;
    1780                 :          0 :                 event.unprot_deauth.da = mgmt->da;
    1781                 :          0 :                 event.unprot_deauth.reason_code = reason_code;
    1782                 :            :         }
    1783                 :            : 
    1784                 :          0 :         wpa_supplicant_event(drv->ctx, type, &event);
    1785                 :            : }
    1786                 :            : 
    1787                 :            : 
    1788                 :       5179 : static void mlme_event(struct i802_bss *bss,
    1789                 :            :                        enum nl80211_commands cmd, struct nlattr *frame,
    1790                 :            :                        struct nlattr *addr, struct nlattr *timed_out,
    1791                 :            :                        struct nlattr *freq, struct nlattr *ack,
    1792                 :            :                        struct nlattr *cookie, struct nlattr *sig)
    1793                 :            : {
    1794                 :       5179 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    1795                 :            :         const u8 *data;
    1796                 :            :         size_t len;
    1797                 :            : 
    1798 [ +  + ][ +  - ]:       5179 :         if (timed_out && addr) {
    1799                 :          3 :                 mlme_timeout_event(drv, cmd, addr);
    1800                 :          3 :                 return;
    1801                 :            :         }
    1802                 :            : 
    1803         [ -  + ]:       5176 :         if (frame == NULL) {
    1804                 :          0 :                 wpa_printf(MSG_DEBUG,
    1805                 :            :                            "nl80211: MLME event %d (%s) without frame data",
    1806                 :            :                            cmd, nl80211_command_to_string(cmd));
    1807                 :          0 :                 return;
    1808                 :            :         }
    1809                 :            : 
    1810                 :       5176 :         data = nla_data(frame);
    1811                 :       5176 :         len = nla_len(frame);
    1812         [ -  + ]:       5176 :         if (len < 4 + 2 * ETH_ALEN) {
    1813                 :          0 :                 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
    1814                 :            :                            MACSTR ") - too short",
    1815                 :          0 :                            cmd, nl80211_command_to_string(cmd), bss->ifname,
    1816                 :          0 :                            MAC2STR(bss->addr));
    1817                 :          0 :                 return;
    1818                 :            :         }
    1819                 :       5176 :         wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
    1820                 :            :                    ") A1=" MACSTR " A2=" MACSTR, cmd,
    1821                 :       5176 :                    nl80211_command_to_string(cmd), bss->ifname,
    1822                 :      62112 :                    MAC2STR(bss->addr), MAC2STR(data + 4),
    1823                 :      31056 :                    MAC2STR(data + 4 + ETH_ALEN));
    1824 [ +  + ][ +  + ]:       5176 :         if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
                 [ +  + ]
    1825         [ -  + ]:        352 :             os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
    1826                 :        352 :             os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
    1827                 :          0 :                 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
    1828                 :          0 :                            "for foreign address", bss->ifname);
    1829                 :          0 :                 return;
    1830                 :            :         }
    1831                 :       5176 :         wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
    1832                 :       5176 :                     nla_data(frame), nla_len(frame));
    1833                 :            : 
    1834   [ +  +  +  -  :       5176 :         switch (cmd) {
             +  +  -  -  
                      - ]
    1835                 :            :         case NL80211_CMD_AUTHENTICATE:
    1836                 :        422 :                 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
    1837                 :        422 :                 break;
    1838                 :            :         case NL80211_CMD_ASSOCIATE:
    1839                 :        408 :                 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
    1840                 :        408 :                 break;
    1841                 :            :         case NL80211_CMD_DEAUTHENTICATE:
    1842                 :        405 :                 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
    1843                 :        405 :                                            nla_data(frame), nla_len(frame));
    1844                 :        405 :                 break;
    1845                 :            :         case NL80211_CMD_DISASSOCIATE:
    1846                 :          0 :                 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
    1847                 :          0 :                                            nla_data(frame), nla_len(frame));
    1848                 :          0 :                 break;
    1849                 :            :         case NL80211_CMD_FRAME:
    1850                 :       2461 :                 mlme_event_mgmt(drv, freq, sig, nla_data(frame),
    1851                 :       2461 :                                 nla_len(frame));
    1852                 :       2461 :                 break;
    1853                 :            :         case NL80211_CMD_FRAME_TX_STATUS:
    1854                 :       1480 :                 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
    1855                 :       1480 :                                           nla_len(frame), ack);
    1856                 :       1480 :                 break;
    1857                 :            :         case NL80211_CMD_UNPROT_DEAUTHENTICATE:
    1858                 :          0 :                 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
    1859                 :          0 :                                              nla_data(frame), nla_len(frame));
    1860                 :          0 :                 break;
    1861                 :            :         case NL80211_CMD_UNPROT_DISASSOCIATE:
    1862                 :          0 :                 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
    1863                 :          0 :                                              nla_data(frame), nla_len(frame));
    1864                 :          0 :                 break;
    1865                 :            :         default:
    1866                 :       5179 :                 break;
    1867                 :            :         }
    1868                 :            : }
    1869                 :            : 
    1870                 :            : 
    1871                 :          0 : static void mlme_event_michael_mic_failure(struct i802_bss *bss,
    1872                 :            :                                            struct nlattr *tb[])
    1873                 :            : {
    1874                 :            :         union wpa_event_data data;
    1875                 :            : 
    1876                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
    1877                 :          0 :         os_memset(&data, 0, sizeof(data));
    1878         [ #  # ]:          0 :         if (tb[NL80211_ATTR_MAC]) {
    1879                 :          0 :                 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
    1880                 :          0 :                             nla_data(tb[NL80211_ATTR_MAC]),
    1881                 :          0 :                             nla_len(tb[NL80211_ATTR_MAC]));
    1882                 :          0 :                 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
    1883                 :            :         }
    1884         [ #  # ]:          0 :         if (tb[NL80211_ATTR_KEY_SEQ]) {
    1885                 :          0 :                 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
    1886                 :          0 :                             nla_data(tb[NL80211_ATTR_KEY_SEQ]),
    1887                 :          0 :                             nla_len(tb[NL80211_ATTR_KEY_SEQ]));
    1888                 :            :         }
    1889         [ #  # ]:          0 :         if (tb[NL80211_ATTR_KEY_TYPE]) {
    1890                 :          0 :                 enum nl80211_key_type key_type =
    1891                 :          0 :                         nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
    1892                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
    1893         [ #  # ]:          0 :                 if (key_type == NL80211_KEYTYPE_PAIRWISE)
    1894                 :          0 :                         data.michael_mic_failure.unicast = 1;
    1895                 :            :         } else
    1896                 :          0 :                 data.michael_mic_failure.unicast = 1;
    1897                 :            : 
    1898         [ #  # ]:          0 :         if (tb[NL80211_ATTR_KEY_IDX]) {
    1899                 :          0 :                 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
    1900                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
    1901                 :            :         }
    1902                 :            : 
    1903                 :          0 :         wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
    1904                 :          0 : }
    1905                 :            : 
    1906                 :            : 
    1907                 :          6 : static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
    1908                 :            :                                  struct nlattr *tb[])
    1909                 :            : {
    1910         [ -  + ]:          6 :         if (tb[NL80211_ATTR_MAC] == NULL) {
    1911                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
    1912                 :            :                            "event");
    1913                 :          6 :                 return;
    1914                 :            :         }
    1915                 :          6 :         os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
    1916                 :            : 
    1917                 :          6 :         drv->associated = 1;
    1918                 :          6 :         wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
    1919                 :         36 :                    MAC2STR(drv->bssid));
    1920                 :            : 
    1921                 :          6 :         wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
    1922                 :            : }
    1923                 :            : 
    1924                 :            : 
    1925                 :        988 : static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
    1926                 :            :                                          int cancel_event, struct nlattr *tb[])
    1927                 :            : {
    1928                 :            :         unsigned int freq, chan_type, duration;
    1929                 :            :         union wpa_event_data data;
    1930                 :            :         u64 cookie;
    1931                 :            : 
    1932         [ +  - ]:        988 :         if (tb[NL80211_ATTR_WIPHY_FREQ])
    1933                 :        988 :                 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
    1934                 :            :         else
    1935                 :          0 :                 freq = 0;
    1936                 :            : 
    1937         [ +  - ]:        988 :         if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
    1938                 :        988 :                 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
    1939                 :            :         else
    1940                 :          0 :                 chan_type = 0;
    1941                 :            : 
    1942         [ +  + ]:        988 :         if (tb[NL80211_ATTR_DURATION])
    1943                 :        494 :                 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
    1944                 :            :         else
    1945                 :        494 :                 duration = 0;
    1946                 :            : 
    1947         [ +  - ]:        988 :         if (tb[NL80211_ATTR_COOKIE])
    1948                 :        988 :                 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
    1949                 :            :         else
    1950                 :          0 :                 cookie = 0;
    1951                 :            : 
    1952         [ +  + ]:        988 :         wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
    1953                 :            :                    "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
    1954                 :            :                    cancel_event, freq, chan_type, duration,
    1955                 :            :                    (long long unsigned int) cookie,
    1956                 :        988 :                    cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
    1957                 :            : 
    1958         [ +  + ]:        988 :         if (cookie != drv->remain_on_chan_cookie)
    1959                 :        988 :                 return; /* not for us */
    1960                 :            : 
    1961         [ +  + ]:        986 :         if (cancel_event)
    1962                 :        493 :                 drv->pending_remain_on_chan = 0;
    1963                 :            : 
    1964                 :        986 :         os_memset(&data, 0, sizeof(data));
    1965                 :        986 :         data.remain_on_channel.freq = freq;
    1966                 :        986 :         data.remain_on_channel.duration = duration;
    1967         [ +  + ]:        986 :         wpa_supplicant_event(drv->ctx, cancel_event ?
    1968                 :            :                              EVENT_CANCEL_REMAIN_ON_CHANNEL :
    1969                 :            :                              EVENT_REMAIN_ON_CHANNEL, &data);
    1970                 :            : }
    1971                 :            : 
    1972                 :            : 
    1973                 :          0 : static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
    1974                 :            :                                 struct nlattr *tb[])
    1975                 :            : {
    1976                 :            :         union wpa_event_data data;
    1977                 :            : 
    1978                 :          0 :         os_memset(&data, 0, sizeof(data));
    1979                 :            : 
    1980         [ #  # ]:          0 :         if (tb[NL80211_ATTR_IE]) {
    1981                 :          0 :                 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
    1982                 :          0 :                 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
    1983                 :            :         }
    1984                 :            : 
    1985         [ #  # ]:          0 :         if (tb[NL80211_ATTR_IE_RIC]) {
    1986                 :          0 :                 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
    1987                 :          0 :                 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
    1988                 :            :         }
    1989                 :            : 
    1990         [ #  # ]:          0 :         if (tb[NL80211_ATTR_MAC])
    1991                 :          0 :                 os_memcpy(data.ft_ies.target_ap,
    1992                 :            :                           nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
    1993                 :            : 
    1994                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
    1995                 :          0 :                    MAC2STR(data.ft_ies.target_ap));
    1996                 :            : 
    1997                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
    1998                 :          0 : }
    1999                 :            : 
    2000                 :            : 
    2001                 :        894 : static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
    2002                 :            :                             struct nlattr *tb[])
    2003                 :            : {
    2004                 :            :         union wpa_event_data event;
    2005                 :            :         struct nlattr *nl;
    2006                 :            :         int rem;
    2007                 :            :         struct scan_info *info;
    2008                 :            : #define MAX_REPORT_FREQS 50
    2009                 :            :         int freqs[MAX_REPORT_FREQS];
    2010                 :        894 :         int num_freqs = 0;
    2011                 :            : 
    2012         [ -  + ]:        894 :         if (drv->scan_for_auth) {
    2013                 :          0 :                 drv->scan_for_auth = 0;
    2014                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
    2015                 :            :                            "cfg80211 BSS entry");
    2016                 :          0 :                 wpa_driver_nl80211_authenticate_retry(drv);
    2017                 :        894 :                 return;
    2018                 :            :         }
    2019                 :            : 
    2020                 :        894 :         os_memset(&event, 0, sizeof(event));
    2021                 :        894 :         info = &event.scan_info;
    2022                 :        894 :         info->aborted = aborted;
    2023                 :            : 
    2024         [ +  - ]:        894 :         if (tb[NL80211_ATTR_SCAN_SSIDS]) {
    2025         [ +  + ]:       1767 :                 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
    2026                 :        873 :                         struct wpa_driver_scan_ssid *s =
    2027                 :        873 :                                 &info->ssids[info->num_ssids];
    2028                 :        873 :                         s->ssid = nla_data(nl);
    2029                 :        873 :                         s->ssid_len = nla_len(nl);
    2030                 :        873 :                         wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
    2031                 :            :                                    wpa_ssid_txt(s->ssid, s->ssid_len));
    2032                 :        873 :                         info->num_ssids++;
    2033         [ -  + ]:        873 :                         if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
    2034                 :          0 :                                 break;
    2035                 :            :                 }
    2036                 :            :         }
    2037         [ +  - ]:        894 :         if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
    2038                 :            :                 char msg[200], *pos, *end;
    2039                 :            :                 int res;
    2040                 :            : 
    2041                 :        894 :                 pos = msg;
    2042                 :        894 :                 end = pos + sizeof(msg);
    2043                 :        894 :                 *pos = '\0';
    2044                 :            : 
    2045         [ +  + ]:       5950 :                 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
    2046                 :            :                 {
    2047                 :       5056 :                         freqs[num_freqs] = nla_get_u32(nl);
    2048                 :       5056 :                         res = os_snprintf(pos, end - pos, " %d",
    2049                 :            :                                           freqs[num_freqs]);
    2050 [ +  - ][ +  - ]:       5056 :                         if (res > 0 && end - pos > res)
    2051                 :       5056 :                                 pos += res;
    2052                 :       5056 :                         num_freqs++;
    2053         [ -  + ]:       5056 :                         if (num_freqs == MAX_REPORT_FREQS - 1)
    2054                 :          0 :                                 break;
    2055                 :            :                 }
    2056                 :        894 :                 info->freqs = freqs;
    2057                 :        894 :                 info->num_freqs = num_freqs;
    2058                 :        894 :                 wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
    2059                 :            :                            msg);
    2060                 :            :         }
    2061                 :        894 :         wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
    2062                 :            : }
    2063                 :            : 
    2064                 :            : 
    2065                 :          0 : static int get_link_signal(struct nl_msg *msg, void *arg)
    2066                 :            : {
    2067                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    2068                 :          0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    2069                 :            :         struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
    2070                 :            :         static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
    2071                 :            :                 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
    2072                 :            :                 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
    2073                 :            :         };
    2074                 :            :         struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
    2075                 :            :         static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
    2076                 :            :                 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
    2077                 :            :                 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
    2078                 :            :                 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
    2079                 :            :                 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
    2080                 :            :         };
    2081                 :          0 :         struct wpa_signal_info *sig_change = arg;
    2082                 :            : 
    2083                 :          0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    2084                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    2085   [ #  #  #  # ]:          0 :         if (!tb[NL80211_ATTR_STA_INFO] ||
    2086                 :          0 :             nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
    2087                 :            :                              tb[NL80211_ATTR_STA_INFO], policy))
    2088                 :          0 :                 return NL_SKIP;
    2089         [ #  # ]:          0 :         if (!sinfo[NL80211_STA_INFO_SIGNAL])
    2090                 :          0 :                 return NL_SKIP;
    2091                 :            : 
    2092                 :          0 :         sig_change->current_signal =
    2093                 :          0 :                 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
    2094                 :            : 
    2095         [ #  # ]:          0 :         if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
    2096                 :          0 :                 sig_change->avg_signal =
    2097                 :          0 :                         (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
    2098                 :            :         else
    2099                 :          0 :                 sig_change->avg_signal = 0;
    2100                 :            : 
    2101         [ #  # ]:          0 :         if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
    2102         [ #  # ]:          0 :                 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
    2103                 :            :                                      sinfo[NL80211_STA_INFO_TX_BITRATE],
    2104                 :            :                                      rate_policy)) {
    2105                 :          0 :                         sig_change->current_txrate = 0;
    2106                 :            :                 } else {
    2107         [ #  # ]:          0 :                         if (rinfo[NL80211_RATE_INFO_BITRATE]) {
    2108                 :          0 :                                 sig_change->current_txrate =
    2109                 :          0 :                                         nla_get_u16(rinfo[
    2110                 :          0 :                                              NL80211_RATE_INFO_BITRATE]) * 100;
    2111                 :            :                         }
    2112                 :            :                 }
    2113                 :            :         }
    2114                 :            : 
    2115                 :          0 :         return NL_SKIP;
    2116                 :            : }
    2117                 :            : 
    2118                 :            : 
    2119                 :          0 : static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
    2120                 :            :                                    struct wpa_signal_info *sig)
    2121                 :            : {
    2122                 :            :         struct nl_msg *msg;
    2123                 :            : 
    2124                 :          0 :         sig->current_signal = -9999;
    2125                 :          0 :         sig->current_txrate = 0;
    2126                 :            : 
    2127                 :          0 :         msg = nlmsg_alloc();
    2128         [ #  # ]:          0 :         if (!msg)
    2129                 :          0 :                 return -ENOMEM;
    2130                 :            : 
    2131                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
    2132                 :            : 
    2133         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    2134         [ #  # ]:          0 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
    2135                 :            : 
    2136                 :          0 :         return send_and_recv_msgs(drv, msg, get_link_signal, sig);
    2137                 :            :  nla_put_failure:
    2138                 :          0 :         nlmsg_free(msg);
    2139                 :          0 :         return -ENOBUFS;
    2140                 :            : }
    2141                 :            : 
    2142                 :            : 
    2143                 :          0 : static int get_link_noise(struct nl_msg *msg, void *arg)
    2144                 :            : {
    2145                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    2146                 :          0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    2147                 :            :         struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
    2148                 :            :         static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
    2149                 :            :                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
    2150                 :            :                 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
    2151                 :            :         };
    2152                 :          0 :         struct wpa_signal_info *sig_change = arg;
    2153                 :            : 
    2154                 :          0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    2155                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    2156                 :            : 
    2157         [ #  # ]:          0 :         if (!tb[NL80211_ATTR_SURVEY_INFO]) {
    2158                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
    2159                 :          0 :                 return NL_SKIP;
    2160                 :            :         }
    2161                 :            : 
    2162         [ #  # ]:          0 :         if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
    2163                 :            :                              tb[NL80211_ATTR_SURVEY_INFO],
    2164                 :            :                              survey_policy)) {
    2165                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
    2166                 :            :                            "attributes!");
    2167                 :          0 :                 return NL_SKIP;
    2168                 :            :         }
    2169                 :            : 
    2170         [ #  # ]:          0 :         if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
    2171                 :          0 :                 return NL_SKIP;
    2172                 :            : 
    2173         [ #  # ]:          0 :         if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
    2174                 :          0 :             sig_change->frequency)
    2175                 :          0 :                 return NL_SKIP;
    2176                 :            : 
    2177         [ #  # ]:          0 :         if (!sinfo[NL80211_SURVEY_INFO_NOISE])
    2178                 :          0 :                 return NL_SKIP;
    2179                 :            : 
    2180                 :          0 :         sig_change->current_noise =
    2181                 :          0 :                 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
    2182                 :            : 
    2183                 :          0 :         return NL_SKIP;
    2184                 :            : }
    2185                 :            : 
    2186                 :            : 
    2187                 :          0 : static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
    2188                 :            :                                   struct wpa_signal_info *sig_change)
    2189                 :            : {
    2190                 :            :         struct nl_msg *msg;
    2191                 :            : 
    2192                 :          0 :         sig_change->current_noise = 9999;
    2193                 :          0 :         sig_change->frequency = drv->assoc_freq;
    2194                 :            : 
    2195                 :          0 :         msg = nlmsg_alloc();
    2196         [ #  # ]:          0 :         if (!msg)
    2197                 :          0 :                 return -ENOMEM;
    2198                 :            : 
    2199                 :          0 :         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
    2200                 :            : 
    2201         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    2202                 :            : 
    2203                 :          0 :         return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
    2204                 :            :  nla_put_failure:
    2205                 :          0 :         nlmsg_free(msg);
    2206                 :          0 :         return -ENOBUFS;
    2207                 :            : }
    2208                 :            : 
    2209                 :            : 
    2210                 :        882 : static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
    2211                 :            : {
    2212                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    2213                 :        882 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    2214                 :            :         struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
    2215                 :            :         static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
    2216                 :            :                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
    2217                 :            :                 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
    2218                 :            :         };
    2219                 :        882 :         struct wpa_scan_results *scan_results = arg;
    2220                 :            :         struct wpa_scan_res *scan_res;
    2221                 :            :         size_t i;
    2222                 :            : 
    2223                 :        882 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    2224                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    2225                 :            : 
    2226         [ -  + ]:        882 :         if (!tb[NL80211_ATTR_SURVEY_INFO]) {
    2227                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
    2228                 :          0 :                 return NL_SKIP;
    2229                 :            :         }
    2230                 :            : 
    2231         [ -  + ]:        882 :         if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
    2232                 :            :                              tb[NL80211_ATTR_SURVEY_INFO],
    2233                 :            :                              survey_policy)) {
    2234                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
    2235                 :            :                            "attributes");
    2236                 :          0 :                 return NL_SKIP;
    2237                 :            :         }
    2238                 :            : 
    2239         [ -  + ]:        882 :         if (!sinfo[NL80211_SURVEY_INFO_NOISE])
    2240                 :          0 :                 return NL_SKIP;
    2241                 :            : 
    2242         [ -  + ]:        882 :         if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
    2243                 :          0 :                 return NL_SKIP;
    2244                 :            : 
    2245         [ +  + ]:       4278 :         for (i = 0; i < scan_results->num; ++i) {
    2246                 :       3396 :                 scan_res = scan_results->res[i];
    2247         [ -  + ]:       3396 :                 if (!scan_res)
    2248                 :          0 :                         continue;
    2249         [ +  + ]:       3396 :                 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
    2250                 :       3396 :                     scan_res->freq)
    2251                 :        964 :                         continue;
    2252         [ -  + ]:       2432 :                 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
    2253                 :          0 :                         continue;
    2254                 :       2432 :                 scan_res->noise = (s8)
    2255                 :       2432 :                         nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
    2256                 :       2432 :                 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
    2257                 :            :         }
    2258                 :            : 
    2259                 :        882 :         return NL_SKIP;
    2260                 :            : }
    2261                 :            : 
    2262                 :            : 
    2263                 :        882 : static int nl80211_get_noise_for_scan_results(
    2264                 :            :         struct wpa_driver_nl80211_data *drv,
    2265                 :            :         struct wpa_scan_results *scan_res)
    2266                 :            : {
    2267                 :            :         struct nl_msg *msg;
    2268                 :            : 
    2269                 :        882 :         msg = nlmsg_alloc();
    2270         [ -  + ]:        882 :         if (!msg)
    2271                 :          0 :                 return -ENOMEM;
    2272                 :            : 
    2273                 :        882 :         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
    2274                 :            : 
    2275         [ +  - ]:        882 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    2276                 :            : 
    2277                 :        882 :         return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
    2278                 :            :                                   scan_res);
    2279                 :            :  nla_put_failure:
    2280                 :          0 :         nlmsg_free(msg);
    2281                 :        882 :         return -ENOBUFS;
    2282                 :            : }
    2283                 :            : 
    2284                 :            : 
    2285                 :          0 : static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
    2286                 :            :                               struct nlattr *tb[])
    2287                 :            : {
    2288                 :            :         static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
    2289                 :            :                 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
    2290                 :            :                 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
    2291                 :            :                 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
    2292                 :            :                 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
    2293                 :            :         };
    2294                 :            :         struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
    2295                 :            :         enum nl80211_cqm_rssi_threshold_event event;
    2296                 :            :         union wpa_event_data ed;
    2297                 :            :         struct wpa_signal_info sig;
    2298                 :            :         int res;
    2299                 :            : 
    2300   [ #  #  #  # ]:          0 :         if (tb[NL80211_ATTR_CQM] == NULL ||
    2301                 :          0 :             nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
    2302                 :            :                              cqm_policy)) {
    2303                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
    2304                 :          0 :                 return;
    2305                 :            :         }
    2306                 :            : 
    2307                 :          0 :         os_memset(&ed, 0, sizeof(ed));
    2308                 :            : 
    2309         [ #  # ]:          0 :         if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
    2310         [ #  # ]:          0 :                 if (!tb[NL80211_ATTR_MAC])
    2311                 :          0 :                         return;
    2312                 :          0 :                 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
    2313                 :            :                           ETH_ALEN);
    2314                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
    2315                 :          0 :                 return;
    2316                 :            :         }
    2317                 :            : 
    2318         [ #  # ]:          0 :         if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
    2319                 :          0 :                 return;
    2320                 :          0 :         event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
    2321                 :            : 
    2322         [ #  # ]:          0 :         if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
    2323                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
    2324                 :            :                            "event: RSSI high");
    2325                 :          0 :                 ed.signal_change.above_threshold = 1;
    2326         [ #  # ]:          0 :         } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
    2327                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
    2328                 :            :                            "event: RSSI low");
    2329                 :          0 :                 ed.signal_change.above_threshold = 0;
    2330                 :            :         } else
    2331                 :          0 :                 return;
    2332                 :            : 
    2333                 :          0 :         res = nl80211_get_link_signal(drv, &sig);
    2334         [ #  # ]:          0 :         if (res == 0) {
    2335                 :          0 :                 ed.signal_change.current_signal = sig.current_signal;
    2336                 :          0 :                 ed.signal_change.current_txrate = sig.current_txrate;
    2337                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm  txrate: %d",
    2338                 :            :                            sig.current_signal, sig.current_txrate);
    2339                 :            :         }
    2340                 :            : 
    2341                 :          0 :         res = nl80211_get_link_noise(drv, &sig);
    2342         [ #  # ]:          0 :         if (res == 0) {
    2343                 :          0 :                 ed.signal_change.current_noise = sig.current_noise;
    2344                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
    2345                 :            :                            sig.current_noise);
    2346                 :            :         }
    2347                 :            : 
    2348                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
    2349                 :            : }
    2350                 :            : 
    2351                 :            : 
    2352                 :        888 : static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
    2353                 :            :                                       struct nlattr **tb)
    2354                 :            : {
    2355                 :            :         u8 *addr;
    2356                 :            :         union wpa_event_data data;
    2357                 :            : 
    2358         [ -  + ]:        888 :         if (tb[NL80211_ATTR_MAC] == NULL)
    2359                 :          0 :                 return;
    2360                 :        888 :         addr = nla_data(tb[NL80211_ATTR_MAC]);
    2361                 :        888 :         wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
    2362                 :            : 
    2363 [ -  + ][ +  + ]:        888 :         if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
    2364                 :          0 :                 u8 *ies = NULL;
    2365                 :          0 :                 size_t ies_len = 0;
    2366         [ #  # ]:          0 :                 if (tb[NL80211_ATTR_IE]) {
    2367                 :          0 :                         ies = nla_data(tb[NL80211_ATTR_IE]);
    2368                 :          0 :                         ies_len = nla_len(tb[NL80211_ATTR_IE]);
    2369                 :            :                 }
    2370                 :          0 :                 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
    2371                 :          0 :                 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
    2372                 :          0 :                 return;
    2373                 :            :         }
    2374                 :            : 
    2375         [ +  + ]:        888 :         if (drv->nlmode != NL80211_IFTYPE_ADHOC)
    2376                 :        882 :                 return;
    2377                 :            : 
    2378                 :          6 :         os_memset(&data, 0, sizeof(data));
    2379                 :          6 :         os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
    2380                 :        888 :         wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
    2381                 :            : }
    2382                 :            : 
    2383                 :            : 
    2384                 :        863 : static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
    2385                 :            :                                       struct nlattr **tb)
    2386                 :            : {
    2387                 :            :         u8 *addr;
    2388                 :            :         union wpa_event_data data;
    2389                 :            : 
    2390         [ -  + ]:        863 :         if (tb[NL80211_ATTR_MAC] == NULL)
    2391                 :          0 :                 return;
    2392                 :        863 :         addr = nla_data(tb[NL80211_ATTR_MAC]);
    2393                 :        863 :         wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
    2394                 :       5178 :                    MAC2STR(addr));
    2395                 :            : 
    2396 [ -  + ][ +  + ]:        863 :         if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
    2397                 :          0 :                 drv_event_disassoc(drv->ctx, addr);
    2398                 :          0 :                 return;
    2399                 :            :         }
    2400                 :            : 
    2401         [ +  - ]:        863 :         if (drv->nlmode != NL80211_IFTYPE_ADHOC)
    2402                 :        863 :                 return;
    2403                 :            : 
    2404                 :          0 :         os_memset(&data, 0, sizeof(data));
    2405                 :          0 :         os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
    2406                 :        863 :         wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
    2407                 :            : }
    2408                 :            : 
    2409                 :            : 
    2410                 :          0 : static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
    2411                 :            :                                         struct nlattr **tb)
    2412                 :            : {
    2413                 :            :         struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
    2414                 :            :         static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
    2415                 :            :                 [NL80211_REKEY_DATA_KEK] = {
    2416                 :            :                         .minlen = NL80211_KEK_LEN,
    2417                 :            :                         .maxlen = NL80211_KEK_LEN,
    2418                 :            :                 },
    2419                 :            :                 [NL80211_REKEY_DATA_KCK] = {
    2420                 :            :                         .minlen = NL80211_KCK_LEN,
    2421                 :            :                         .maxlen = NL80211_KCK_LEN,
    2422                 :            :                 },
    2423                 :            :                 [NL80211_REKEY_DATA_REPLAY_CTR] = {
    2424                 :            :                         .minlen = NL80211_REPLAY_CTR_LEN,
    2425                 :            :                         .maxlen = NL80211_REPLAY_CTR_LEN,
    2426                 :            :                 },
    2427                 :            :         };
    2428                 :            :         union wpa_event_data data;
    2429                 :            : 
    2430         [ #  # ]:          0 :         if (!tb[NL80211_ATTR_MAC])
    2431                 :          0 :                 return;
    2432         [ #  # ]:          0 :         if (!tb[NL80211_ATTR_REKEY_DATA])
    2433                 :          0 :                 return;
    2434         [ #  # ]:          0 :         if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
    2435                 :          0 :                              tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
    2436                 :          0 :                 return;
    2437         [ #  # ]:          0 :         if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
    2438                 :          0 :                 return;
    2439                 :            : 
    2440                 :          0 :         os_memset(&data, 0, sizeof(data));
    2441                 :          0 :         data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
    2442                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
    2443                 :          0 :                    MAC2STR(data.driver_gtk_rekey.bssid));
    2444                 :          0 :         data.driver_gtk_rekey.replay_ctr =
    2445                 :          0 :                 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
    2446                 :          0 :         wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
    2447                 :          0 :                     data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
    2448                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
    2449                 :            : }
    2450                 :            : 
    2451                 :            : 
    2452                 :          0 : static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
    2453                 :            :                                           struct nlattr **tb)
    2454                 :            : {
    2455                 :            :         struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
    2456                 :            :         static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
    2457                 :            :                 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
    2458                 :            :                 [NL80211_PMKSA_CANDIDATE_BSSID] = {
    2459                 :            :                         .minlen = ETH_ALEN,
    2460                 :            :                         .maxlen = ETH_ALEN,
    2461                 :            :                 },
    2462                 :            :                 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
    2463                 :            :         };
    2464                 :            :         union wpa_event_data data;
    2465                 :            : 
    2466                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
    2467                 :            : 
    2468         [ #  # ]:          0 :         if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
    2469                 :          0 :                 return;
    2470         [ #  # ]:          0 :         if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
    2471                 :          0 :                              tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
    2472                 :          0 :                 return;
    2473 [ #  # ][ #  # ]:          0 :         if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
    2474                 :          0 :             !cand[NL80211_PMKSA_CANDIDATE_BSSID])
    2475                 :          0 :                 return;
    2476                 :            : 
    2477                 :          0 :         os_memset(&data, 0, sizeof(data));
    2478                 :          0 :         os_memcpy(data.pmkid_candidate.bssid,
    2479                 :            :                   nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
    2480                 :          0 :         data.pmkid_candidate.index =
    2481                 :          0 :                 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
    2482                 :          0 :         data.pmkid_candidate.preauth =
    2483                 :          0 :                 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
    2484                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
    2485                 :            : }
    2486                 :            : 
    2487                 :            : 
    2488                 :          0 : static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
    2489                 :            :                                        struct nlattr **tb)
    2490                 :            : {
    2491                 :            :         union wpa_event_data data;
    2492                 :            : 
    2493                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
    2494                 :            : 
    2495 [ #  # ][ #  # ]:          0 :         if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
    2496                 :          0 :                 return;
    2497                 :            : 
    2498                 :          0 :         os_memset(&data, 0, sizeof(data));
    2499                 :          0 :         os_memcpy(data.client_poll.addr,
    2500                 :            :                   nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
    2501                 :            : 
    2502                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
    2503                 :            : }
    2504                 :            : 
    2505                 :            : 
    2506                 :          0 : static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
    2507                 :            :                                     struct nlattr **tb)
    2508                 :            : {
    2509                 :            :         union wpa_event_data data;
    2510                 :            : 
    2511                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
    2512                 :            : 
    2513 [ #  # ][ #  # ]:          0 :         if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
    2514                 :          0 :                 return;
    2515                 :            : 
    2516                 :          0 :         os_memset(&data, 0, sizeof(data));
    2517                 :          0 :         os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
    2518      [ #  #  # ]:          0 :         switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
    2519                 :            :         case NL80211_TDLS_SETUP:
    2520                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
    2521                 :          0 :                            MACSTR, MAC2STR(data.tdls.peer));
    2522                 :          0 :                 data.tdls.oper = TDLS_REQUEST_SETUP;
    2523                 :          0 :                 break;
    2524                 :            :         case NL80211_TDLS_TEARDOWN:
    2525                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
    2526                 :          0 :                            MACSTR, MAC2STR(data.tdls.peer));
    2527                 :          0 :                 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
    2528                 :          0 :                 break;
    2529                 :            :         default:
    2530                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
    2531                 :            :                            "event");
    2532                 :          0 :                 return;
    2533                 :            :         }
    2534         [ #  # ]:          0 :         if (tb[NL80211_ATTR_REASON_CODE]) {
    2535                 :          0 :                 data.tdls.reason_code =
    2536                 :          0 :                         nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
    2537                 :            :         }
    2538                 :            : 
    2539                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
    2540                 :            : }
    2541                 :            : 
    2542                 :            : 
    2543                 :          0 : static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
    2544                 :            :                             struct nlattr **tb)
    2545                 :            : {
    2546                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
    2547                 :          0 : }
    2548                 :            : 
    2549                 :            : 
    2550                 :          0 : static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
    2551                 :            :                                          struct nlattr **tb)
    2552                 :            : {
    2553                 :            :         union wpa_event_data data;
    2554                 :            :         u32 reason;
    2555                 :            : 
    2556                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
    2557                 :            : 
    2558 [ #  # ][ #  # ]:          0 :         if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
    2559                 :          0 :                 return;
    2560                 :            : 
    2561                 :          0 :         os_memset(&data, 0, sizeof(data));
    2562                 :          0 :         os_memcpy(data.connect_failed_reason.addr,
    2563                 :            :                   nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
    2564                 :            : 
    2565                 :          0 :         reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
    2566      [ #  #  # ]:          0 :         switch (reason) {
    2567                 :            :         case NL80211_CONN_FAIL_MAX_CLIENTS:
    2568                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
    2569                 :          0 :                 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
    2570                 :          0 :                 break;
    2571                 :            :         case NL80211_CONN_FAIL_BLOCKED_CLIENT:
    2572                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
    2573                 :            :                            " tried to connect",
    2574                 :          0 :                            MAC2STR(data.connect_failed_reason.addr));
    2575                 :          0 :                 data.connect_failed_reason.code = BLOCKED_CLIENT;
    2576                 :          0 :                 break;
    2577                 :            :         default:
    2578                 :          0 :                 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
    2579                 :            :                            "%u", reason);
    2580                 :          0 :                 return;
    2581                 :            :         }
    2582                 :            : 
    2583                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
    2584                 :            : }
    2585                 :            : 
    2586                 :            : 
    2587                 :          0 : static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
    2588                 :            :                                 struct nlattr **tb)
    2589                 :            : {
    2590                 :            :         union wpa_event_data data;
    2591                 :            :         enum nl80211_radar_event event_type;
    2592                 :            : 
    2593 [ #  # ][ #  # ]:          0 :         if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
    2594                 :          0 :                 return;
    2595                 :            : 
    2596                 :          0 :         os_memset(&data, 0, sizeof(data));
    2597                 :          0 :         data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
    2598                 :          0 :         event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
    2599                 :            : 
    2600                 :            :         /* Check HT params */
    2601         [ #  # ]:          0 :         if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
    2602                 :          0 :                 data.dfs_event.ht_enabled = 1;
    2603                 :          0 :                 data.dfs_event.chan_offset = 0;
    2604                 :            : 
    2605   [ #  #  #  #  :          0 :                 switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
                      # ]
    2606                 :            :                 case NL80211_CHAN_NO_HT:
    2607                 :          0 :                         data.dfs_event.ht_enabled = 0;
    2608                 :          0 :                         break;
    2609                 :            :                 case NL80211_CHAN_HT20:
    2610                 :          0 :                         break;
    2611                 :            :                 case NL80211_CHAN_HT40PLUS:
    2612                 :          0 :                         data.dfs_event.chan_offset = 1;
    2613                 :          0 :                         break;
    2614                 :            :                 case NL80211_CHAN_HT40MINUS:
    2615                 :          0 :                         data.dfs_event.chan_offset = -1;
    2616                 :          0 :                         break;
    2617                 :            :                 }
    2618                 :            :         }
    2619                 :            : 
    2620                 :            :         /* Get VHT params */
    2621         [ #  # ]:          0 :         if (tb[NL80211_ATTR_CHANNEL_WIDTH])
    2622                 :          0 :                 data.dfs_event.chan_width =
    2623                 :          0 :                         convert2width(nla_get_u32(
    2624                 :          0 :                                               tb[NL80211_ATTR_CHANNEL_WIDTH]));
    2625         [ #  # ]:          0 :         if (tb[NL80211_ATTR_CENTER_FREQ1])
    2626                 :          0 :                 data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
    2627         [ #  # ]:          0 :         if (tb[NL80211_ATTR_CENTER_FREQ2])
    2628                 :          0 :                 data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
    2629                 :            : 
    2630                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
    2631                 :            :                    data.dfs_event.freq, data.dfs_event.ht_enabled,
    2632                 :          0 :                    data.dfs_event.chan_offset, data.dfs_event.chan_width,
    2633                 :            :                    data.dfs_event.cf1, data.dfs_event.cf2);
    2634                 :            : 
    2635   [ #  #  #  #  :          0 :         switch (event_type) {
                      # ]
    2636                 :            :         case NL80211_RADAR_DETECTED:
    2637                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
    2638                 :          0 :                 break;
    2639                 :            :         case NL80211_RADAR_CAC_FINISHED:
    2640                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
    2641                 :          0 :                 break;
    2642                 :            :         case NL80211_RADAR_CAC_ABORTED:
    2643                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
    2644                 :          0 :                 break;
    2645                 :            :         case NL80211_RADAR_NOP_FINISHED:
    2646                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
    2647                 :          0 :                 break;
    2648                 :            :         default:
    2649                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
    2650                 :            :                            "received", event_type);
    2651                 :          0 :                 break;
    2652                 :            :         }
    2653                 :            : }
    2654                 :            : 
    2655                 :            : 
    2656                 :          0 : static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
    2657                 :            :                                    int wds)
    2658                 :            : {
    2659                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    2660                 :            :         union wpa_event_data event;
    2661                 :            : 
    2662         [ #  # ]:          0 :         if (!tb[NL80211_ATTR_MAC])
    2663                 :          0 :                 return;
    2664                 :            : 
    2665                 :          0 :         os_memset(&event, 0, sizeof(event));
    2666                 :          0 :         event.rx_from_unknown.bssid = bss->addr;
    2667                 :          0 :         event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
    2668                 :          0 :         event.rx_from_unknown.wds = wds;
    2669                 :            : 
    2670                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
    2671                 :            : }
    2672                 :            : 
    2673                 :            : 
    2674                 :       9290 : static void do_process_drv_event(struct i802_bss *bss, int cmd,
    2675                 :            :                                  struct nlattr **tb)
    2676                 :            : {
    2677                 :       9290 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    2678                 :            :         union wpa_event_data data;
    2679                 :            : 
    2680                 :       9290 :         wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
    2681                 :       9290 :                    cmd, nl80211_command_to_string(cmd), bss->ifname);
    2682                 :            : 
    2683 [ #  # ][ -  + ]:       9290 :         if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
    2684         [ #  # ]:          0 :             (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
    2685                 :            :              cmd == NL80211_CMD_SCAN_ABORTED)) {
    2686                 :          0 :                 wpa_driver_nl80211_set_mode(drv->first_bss,
    2687                 :            :                                             drv->ap_scan_as_station);
    2688                 :          0 :                 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
    2689                 :            :         }
    2690                 :            : 
    2691   [ +  -  -  +  :       9290 :         switch (cmd) {
          -  -  +  +  -  
          +  -  +  +  +  
          -  +  -  +  +  
          -  -  -  -  -  
             -  -  -  - ]
    2692                 :            :         case NL80211_CMD_TRIGGER_SCAN:
    2693                 :        899 :                 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
    2694                 :        899 :                 drv->scan_state = SCAN_STARTED;
    2695                 :        899 :                 wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
    2696                 :        899 :                 break;
    2697                 :            :         case NL80211_CMD_START_SCHED_SCAN:
    2698                 :          0 :                 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
    2699                 :          0 :                 drv->scan_state = SCHED_SCAN_STARTED;
    2700                 :          0 :                 break;
    2701                 :            :         case NL80211_CMD_SCHED_SCAN_STOPPED:
    2702                 :          0 :                 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
    2703                 :          0 :                 drv->scan_state = SCHED_SCAN_STOPPED;
    2704                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
    2705                 :          0 :                 break;
    2706                 :            :         case NL80211_CMD_NEW_SCAN_RESULTS:
    2707                 :        894 :                 wpa_dbg(drv->ctx, MSG_DEBUG,
    2708                 :            :                         "nl80211: New scan results available");
    2709                 :        894 :                 drv->scan_state = SCAN_COMPLETED;
    2710                 :        894 :                 drv->scan_complete_events = 1;
    2711                 :        894 :                 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
    2712                 :            :                                      drv->ctx);
    2713                 :        894 :                 send_scan_event(drv, 0, tb);
    2714                 :        894 :                 break;
    2715                 :            :         case NL80211_CMD_SCHED_SCAN_RESULTS:
    2716                 :          0 :                 wpa_dbg(drv->ctx, MSG_DEBUG,
    2717                 :            :                         "nl80211: New sched scan results available");
    2718                 :          0 :                 drv->scan_state = SCHED_SCAN_RESULTS;
    2719                 :          0 :                 send_scan_event(drv, 0, tb);
    2720                 :          0 :                 break;
    2721                 :            :         case NL80211_CMD_SCAN_ABORTED:
    2722                 :          0 :                 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
    2723                 :          0 :                 drv->scan_state = SCAN_ABORTED;
    2724                 :            :                 /*
    2725                 :            :                  * Need to indicate that scan results are available in order
    2726                 :            :                  * not to make wpa_supplicant stop its scanning.
    2727                 :            :                  */
    2728                 :          0 :                 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
    2729                 :            :                                      drv->ctx);
    2730                 :          0 :                 send_scan_event(drv, 1, tb);
    2731                 :          0 :                 break;
    2732                 :            :         case NL80211_CMD_AUTHENTICATE:
    2733                 :            :         case NL80211_CMD_ASSOCIATE:
    2734                 :            :         case NL80211_CMD_DEAUTHENTICATE:
    2735                 :            :         case NL80211_CMD_DISASSOCIATE:
    2736                 :            :         case NL80211_CMD_FRAME_TX_STATUS:
    2737                 :            :         case NL80211_CMD_UNPROT_DEAUTHENTICATE:
    2738                 :            :         case NL80211_CMD_UNPROT_DISASSOCIATE:
    2739                 :       2718 :                 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
    2740                 :       5436 :                            tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
    2741                 :       5436 :                            tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
    2742                 :       2718 :                            tb[NL80211_ATTR_COOKIE],
    2743                 :       2718 :                            tb[NL80211_ATTR_RX_SIGNAL_DBM]);
    2744                 :       2718 :                 break;
    2745                 :            :         case NL80211_CMD_CONNECT:
    2746                 :            :         case NL80211_CMD_ROAM:
    2747                 :        408 :                 mlme_event_connect(drv, cmd,
    2748                 :        408 :                                    tb[NL80211_ATTR_STATUS_CODE],
    2749                 :        408 :                                    tb[NL80211_ATTR_MAC],
    2750                 :        408 :                                    tb[NL80211_ATTR_REQ_IE],
    2751                 :        408 :                                    tb[NL80211_ATTR_RESP_IE]);
    2752                 :        408 :                 break;
    2753                 :            :         case NL80211_CMD_CH_SWITCH_NOTIFY:
    2754                 :          0 :                 mlme_event_ch_switch(drv,
    2755                 :          0 :                                      tb[NL80211_ATTR_IFINDEX],
    2756                 :          0 :                                      tb[NL80211_ATTR_WIPHY_FREQ],
    2757                 :          0 :                                      tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
    2758                 :          0 :                                      tb[NL80211_ATTR_CHANNEL_WIDTH],
    2759                 :          0 :                                      tb[NL80211_ATTR_CENTER_FREQ1],
    2760                 :          0 :                                      tb[NL80211_ATTR_CENTER_FREQ2]);
    2761                 :          0 :                 break;
    2762                 :            :         case NL80211_CMD_DISCONNECT:
    2763                 :        399 :                 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
    2764                 :        399 :                                       tb[NL80211_ATTR_MAC],
    2765                 :        399 :                                       tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
    2766                 :        399 :                 break;
    2767                 :            :         case NL80211_CMD_MICHAEL_MIC_FAILURE:
    2768                 :          0 :                 mlme_event_michael_mic_failure(bss, tb);
    2769                 :          0 :                 break;
    2770                 :            :         case NL80211_CMD_JOIN_IBSS:
    2771                 :          6 :                 mlme_event_join_ibss(drv, tb);
    2772                 :          6 :                 break;
    2773                 :            :         case NL80211_CMD_REMAIN_ON_CHANNEL:
    2774                 :        494 :                 mlme_event_remain_on_channel(drv, 0, tb);
    2775                 :        494 :                 break;
    2776                 :            :         case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
    2777                 :        494 :                 mlme_event_remain_on_channel(drv, 1, tb);
    2778                 :        494 :                 break;
    2779                 :            :         case NL80211_CMD_NOTIFY_CQM:
    2780                 :          0 :                 nl80211_cqm_event(drv, tb);
    2781                 :          0 :                 break;
    2782                 :            :         case NL80211_CMD_REG_CHANGE:
    2783                 :       1227 :                 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
    2784         [ -  + ]:       1227 :                 if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
    2785                 :          0 :                         break;
    2786                 :       1227 :                 os_memset(&data, 0, sizeof(data));
    2787   [ +  -  -  -  :       1227 :                 switch (nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])) {
                      - ]
    2788                 :            :                 case NL80211_REGDOM_SET_BY_CORE:
    2789                 :       1227 :                         data.channel_list_changed.initiator =
    2790                 :            :                                 REGDOM_SET_BY_CORE;
    2791                 :       1227 :                         break;
    2792                 :            :                 case NL80211_REGDOM_SET_BY_USER:
    2793                 :          0 :                         data.channel_list_changed.initiator =
    2794                 :            :                                 REGDOM_SET_BY_USER;
    2795                 :          0 :                         break;
    2796                 :            :                 case NL80211_REGDOM_SET_BY_DRIVER:
    2797                 :          0 :                         data.channel_list_changed.initiator =
    2798                 :            :                                 REGDOM_SET_BY_DRIVER;
    2799                 :          0 :                         break;
    2800                 :            :                 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
    2801                 :          0 :                         data.channel_list_changed.initiator =
    2802                 :            :                                 REGDOM_SET_BY_COUNTRY_IE;
    2803                 :          0 :                         break;
    2804                 :            :                 default:
    2805                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Unknown reg change initiator %d received",
    2806                 :          0 :                                    nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]));
    2807                 :          0 :                         break;
    2808                 :            :                 }
    2809                 :       1227 :                 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
    2810                 :            :                                      &data);
    2811                 :       1227 :                 break;
    2812                 :            :         case NL80211_CMD_REG_BEACON_HINT:
    2813                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
    2814                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
    2815                 :            :                                      NULL);
    2816                 :          0 :                 break;
    2817                 :            :         case NL80211_CMD_NEW_STATION:
    2818                 :        888 :                 nl80211_new_station_event(drv, tb);
    2819                 :        888 :                 break;
    2820                 :            :         case NL80211_CMD_DEL_STATION:
    2821                 :        863 :                 nl80211_del_station_event(drv, tb);
    2822                 :        863 :                 break;
    2823                 :            :         case NL80211_CMD_SET_REKEY_OFFLOAD:
    2824                 :          0 :                 nl80211_rekey_offload_event(drv, tb);
    2825                 :          0 :                 break;
    2826                 :            :         case NL80211_CMD_PMKSA_CANDIDATE:
    2827                 :          0 :                 nl80211_pmksa_candidate_event(drv, tb);
    2828                 :          0 :                 break;
    2829                 :            :         case NL80211_CMD_PROBE_CLIENT:
    2830                 :          0 :                 nl80211_client_probe_event(drv, tb);
    2831                 :          0 :                 break;
    2832                 :            :         case NL80211_CMD_TDLS_OPER:
    2833                 :          0 :                 nl80211_tdls_oper_event(drv, tb);
    2834                 :          0 :                 break;
    2835                 :            :         case NL80211_CMD_CONN_FAILED:
    2836                 :          0 :                 nl80211_connect_failed_event(drv, tb);
    2837                 :          0 :                 break;
    2838                 :            :         case NL80211_CMD_FT_EVENT:
    2839                 :          0 :                 mlme_event_ft_event(drv, tb);
    2840                 :          0 :                 break;
    2841                 :            :         case NL80211_CMD_RADAR_DETECT:
    2842                 :          0 :                 nl80211_radar_event(drv, tb);
    2843                 :          0 :                 break;
    2844                 :            :         case NL80211_CMD_STOP_AP:
    2845                 :          0 :                 nl80211_stop_ap(drv, tb);
    2846                 :          0 :                 break;
    2847                 :            :         default:
    2848                 :          0 :                 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
    2849                 :            :                         "(cmd=%d)", cmd);
    2850                 :          0 :                 break;
    2851                 :            :         }
    2852                 :       9290 : }
    2853                 :            : 
    2854                 :            : 
    2855                 :          0 : static int process_drv_event(struct nl_msg *msg, void *arg)
    2856                 :            : {
    2857                 :          0 :         struct wpa_driver_nl80211_data *drv = arg;
    2858                 :          0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    2859                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    2860                 :            :         struct i802_bss *bss;
    2861                 :          0 :         int ifidx = -1;
    2862                 :            : 
    2863                 :          0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    2864                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    2865                 :            : 
    2866         [ #  # ]:          0 :         if (tb[NL80211_ATTR_IFINDEX]) {
    2867                 :          0 :                 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
    2868                 :            : 
    2869         [ #  # ]:          0 :                 for (bss = drv->first_bss; bss; bss = bss->next)
    2870 [ #  # ][ #  # ]:          0 :                         if (ifidx == -1 || ifidx == bss->ifindex) {
    2871                 :          0 :                                 do_process_drv_event(bss, gnlh->cmd, tb);
    2872                 :          0 :                                 return NL_SKIP;
    2873                 :            :                         }
    2874                 :          0 :                 wpa_printf(MSG_DEBUG,
    2875                 :            :                            "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
    2876                 :          0 :                            gnlh->cmd, ifidx);
    2877         [ #  # ]:          0 :         } else if (tb[NL80211_ATTR_WDEV]) {
    2878                 :          0 :                 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
    2879                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
    2880         [ #  # ]:          0 :                 for (bss = drv->first_bss; bss; bss = bss->next) {
    2881 [ #  # ][ #  # ]:          0 :                         if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
    2882                 :          0 :                                 do_process_drv_event(bss, gnlh->cmd, tb);
    2883                 :          0 :                                 return NL_SKIP;
    2884                 :            :                         }
    2885                 :            :                 }
    2886                 :          0 :                 wpa_printf(MSG_DEBUG,
    2887                 :            :                            "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
    2888                 :          0 :                            gnlh->cmd, (long long unsigned int) wdev_id);
    2889                 :            :         }
    2890                 :            : 
    2891                 :          0 :         return NL_SKIP;
    2892                 :            : }
    2893                 :            : 
    2894                 :            : 
    2895                 :      34570 : static int process_global_event(struct nl_msg *msg, void *arg)
    2896                 :            : {
    2897                 :      34570 :         struct nl80211_global *global = arg;
    2898                 :      34570 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    2899                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    2900                 :            :         struct wpa_driver_nl80211_data *drv, *tmp;
    2901                 :      34570 :         int ifidx = -1;
    2902                 :            :         struct i802_bss *bss;
    2903                 :      34570 :         u64 wdev_id = 0;
    2904                 :      34570 :         int wdev_id_set = 0;
    2905                 :            : 
    2906                 :      34570 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    2907                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    2908                 :            : 
    2909         [ +  + ]:      34570 :         if (tb[NL80211_ATTR_IFINDEX])
    2910                 :      33254 :                 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
    2911         [ -  + ]:       1316 :         else if (tb[NL80211_ATTR_WDEV]) {
    2912                 :          0 :                 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
    2913                 :          0 :                 wdev_id_set = 1;
    2914                 :            :         }
    2915                 :            : 
    2916         [ +  + ]:      57126 :         dl_list_for_each_safe(drv, tmp, &global->interfaces,
    2917                 :            :                               struct wpa_driver_nl80211_data, list) {
    2918         [ +  + ]:      55226 :                 for (bss = drv->first_bss; bss; bss = bss->next) {
    2919 [ +  + ][ -  + ]:      32670 :                         if ((ifidx == -1 && !wdev_id_set) ||
                 [ +  + ]
    2920         [ -  + ]:      23380 :                             ifidx == bss->ifindex ||
    2921 [ #  # ][ #  # ]:          0 :                             (wdev_id_set && bss->wdev_id_set &&
    2922                 :          0 :                              wdev_id == bss->wdev_id)) {
    2923                 :       9290 :                                 do_process_drv_event(bss, gnlh->cmd, tb);
    2924                 :       9290 :                                 return NL_SKIP;
    2925                 :            :                         }
    2926                 :            :                 }
    2927                 :            :         }
    2928                 :            : 
    2929                 :      34570 :         return NL_SKIP;
    2930                 :            : }
    2931                 :            : 
    2932                 :            : 
    2933                 :       2461 : static int process_bss_event(struct nl_msg *msg, void *arg)
    2934                 :            : {
    2935                 :       2461 :         struct i802_bss *bss = arg;
    2936                 :       2461 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    2937                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    2938                 :            : 
    2939                 :       2461 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    2940                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    2941                 :            : 
    2942                 :       2461 :         wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
    2943                 :       4922 :                    gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
    2944                 :       2461 :                    bss->ifname);
    2945                 :            : 
    2946   [ +  -  -  - ]:       2461 :         switch (gnlh->cmd) {
    2947                 :            :         case NL80211_CMD_FRAME:
    2948                 :            :         case NL80211_CMD_FRAME_TX_STATUS:
    2949                 :       2461 :                 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
    2950                 :            :                            tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
    2951                 :            :                            tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
    2952                 :            :                            tb[NL80211_ATTR_COOKIE],
    2953                 :            :                            tb[NL80211_ATTR_RX_SIGNAL_DBM]);
    2954                 :       2461 :                 break;
    2955                 :            :         case NL80211_CMD_UNEXPECTED_FRAME:
    2956                 :          0 :                 nl80211_spurious_frame(bss, tb, 0);
    2957                 :          0 :                 break;
    2958                 :            :         case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
    2959                 :          0 :                 nl80211_spurious_frame(bss, tb, 1);
    2960                 :          0 :                 break;
    2961                 :            :         default:
    2962                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
    2963                 :          0 :                            "(cmd=%d)", gnlh->cmd);
    2964                 :          0 :                 break;
    2965                 :            :         }
    2966                 :            : 
    2967                 :       2461 :         return NL_SKIP;
    2968                 :            : }
    2969                 :            : 
    2970                 :            : 
    2971                 :      36997 : static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
    2972                 :            :                                              void *handle)
    2973                 :            : {
    2974                 :      36997 :         struct nl_cb *cb = eloop_ctx;
    2975                 :            :         int res;
    2976                 :            : 
    2977                 :      36997 :         wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
    2978                 :            : 
    2979                 :      36997 :         res = nl_recvmsgs(handle, cb);
    2980         [ -  + ]:      36997 :         if (res) {
    2981                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
    2982                 :            :                            __func__, res);
    2983                 :            :         }
    2984                 :      36997 : }
    2985                 :            : 
    2986                 :            : 
    2987                 :            : /**
    2988                 :            :  * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
    2989                 :            :  * @priv: driver_nl80211 private data
    2990                 :            :  * @alpha2_arg: country to which to switch to
    2991                 :            :  * Returns: 0 on success, -1 on failure
    2992                 :            :  *
    2993                 :            :  * This asks nl80211 to set the regulatory domain for given
    2994                 :            :  * country ISO / IEC alpha2.
    2995                 :            :  */
    2996                 :          0 : static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
    2997                 :            : {
    2998                 :          0 :         struct i802_bss *bss = priv;
    2999                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    3000                 :            :         char alpha2[3];
    3001                 :            :         struct nl_msg *msg;
    3002                 :            : 
    3003                 :          0 :         msg = nlmsg_alloc();
    3004         [ #  # ]:          0 :         if (!msg)
    3005                 :          0 :                 return -ENOMEM;
    3006                 :            : 
    3007                 :          0 :         alpha2[0] = alpha2_arg[0];
    3008                 :          0 :         alpha2[1] = alpha2_arg[1];
    3009                 :          0 :         alpha2[2] = '\0';
    3010                 :            : 
    3011                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
    3012                 :            : 
    3013         [ #  # ]:          0 :         NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
    3014         [ #  # ]:          0 :         if (send_and_recv_msgs(drv, msg, NULL, NULL))
    3015                 :          0 :                 return -EINVAL;
    3016                 :          0 :         return 0;
    3017                 :            : nla_put_failure:
    3018                 :          0 :         nlmsg_free(msg);
    3019                 :          0 :         return -EINVAL;
    3020                 :            : }
    3021                 :            : 
    3022                 :            : 
    3023                 :          0 : static int nl80211_get_country(struct nl_msg *msg, void *arg)
    3024                 :            : {
    3025                 :          0 :         char *alpha2 = arg;
    3026                 :            :         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
    3027                 :          0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    3028                 :            : 
    3029                 :          0 :         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    3030                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    3031         [ #  # ]:          0 :         if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
    3032                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
    3033                 :          0 :                 return NL_SKIP;
    3034                 :            :         }
    3035                 :          0 :         os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
    3036                 :          0 :         return NL_SKIP;
    3037                 :            : }
    3038                 :            : 
    3039                 :            : 
    3040                 :          0 : static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
    3041                 :            : {
    3042                 :          0 :         struct i802_bss *bss = priv;
    3043                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    3044                 :            :         struct nl_msg *msg;
    3045                 :            :         int ret;
    3046                 :            : 
    3047                 :          0 :         msg = nlmsg_alloc();
    3048         [ #  # ]:          0 :         if (!msg)
    3049                 :          0 :                 return -ENOMEM;
    3050                 :            : 
    3051                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
    3052                 :          0 :         alpha2[0] = '\0';
    3053                 :          0 :         ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
    3054         [ #  # ]:          0 :         if (!alpha2[0])
    3055                 :          0 :                 ret = -1;
    3056                 :            : 
    3057                 :          0 :         return ret;
    3058                 :            : }
    3059                 :            : 
    3060                 :            : 
    3061                 :       1481 : static int protocol_feature_handler(struct nl_msg *msg, void *arg)
    3062                 :            : {
    3063                 :       1481 :         u32 *feat = arg;
    3064                 :            :         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
    3065                 :       1481 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    3066                 :            : 
    3067                 :       1481 :         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    3068                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    3069                 :            : 
    3070         [ +  - ]:       1481 :         if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
    3071                 :       1481 :                 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
    3072                 :            : 
    3073                 :       1481 :         return NL_SKIP;
    3074                 :            : }
    3075                 :            : 
    3076                 :            : 
    3077                 :       1481 : static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
    3078                 :            : {
    3079                 :       1481 :         u32 feat = 0;
    3080                 :            :         struct nl_msg *msg;
    3081                 :            : 
    3082                 :       1481 :         msg = nlmsg_alloc();
    3083         [ -  + ]:       1481 :         if (!msg)
    3084                 :          0 :                 goto nla_put_failure;
    3085                 :            : 
    3086                 :       1481 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
    3087         [ +  - ]:       1481 :         if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
    3088                 :       1481 :                 return feat;
    3089                 :            : 
    3090                 :          0 :         msg = NULL;
    3091                 :            : nla_put_failure:
    3092                 :          0 :         nlmsg_free(msg);
    3093                 :       1481 :         return 0;
    3094                 :            : }
    3095                 :            : 
    3096                 :            : 
    3097                 :            : struct wiphy_info_data {
    3098                 :            :         struct wpa_driver_nl80211_data *drv;
    3099                 :            :         struct wpa_driver_capa *capa;
    3100                 :            : 
    3101                 :            :         unsigned int num_multichan_concurrent;
    3102                 :            : 
    3103                 :            :         unsigned int error:1;
    3104                 :            :         unsigned int device_ap_sme:1;
    3105                 :            :         unsigned int poll_command_supported:1;
    3106                 :            :         unsigned int data_tx_status:1;
    3107                 :            :         unsigned int monitor_supported:1;
    3108                 :            :         unsigned int auth_supported:1;
    3109                 :            :         unsigned int connect_supported:1;
    3110                 :            :         unsigned int p2p_go_supported:1;
    3111                 :            :         unsigned int p2p_client_supported:1;
    3112                 :            :         unsigned int p2p_concurrent:1;
    3113                 :            :         unsigned int channel_switch_supported:1;
    3114                 :            :         unsigned int set_qos_map_supported:1;
    3115                 :            : };
    3116                 :            : 
    3117                 :            : 
    3118                 :          0 : static unsigned int probe_resp_offload_support(int supp_protocols)
    3119                 :            : {
    3120                 :          0 :         unsigned int prot = 0;
    3121                 :            : 
    3122         [ #  # ]:          0 :         if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
    3123                 :          0 :                 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
    3124         [ #  # ]:          0 :         if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
    3125                 :          0 :                 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
    3126         [ #  # ]:          0 :         if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
    3127                 :          0 :                 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
    3128         [ #  # ]:          0 :         if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
    3129                 :          0 :                 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
    3130                 :            : 
    3131                 :          0 :         return prot;
    3132                 :            : }
    3133                 :            : 
    3134                 :            : 
    3135                 :      11236 : static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
    3136                 :            :                                          struct nlattr *tb)
    3137                 :            : {
    3138                 :            :         struct nlattr *nl_mode;
    3139                 :            :         int i;
    3140                 :            : 
    3141         [ +  + ]:      11236 :         if (tb == NULL)
    3142                 :      11236 :                 return;
    3143                 :            : 
    3144         [ +  + ]:       1908 :         nla_for_each_nested(nl_mode, tb, i) {
    3145   [ +  +  +  +  :       1696 :                 switch (nla_type(nl_mode)) {
                +  +  + ]
    3146                 :            :                 case NL80211_IFTYPE_AP:
    3147                 :        212 :                         info->capa->flags |= WPA_DRIVER_FLAGS_AP;
    3148                 :        212 :                         break;
    3149                 :            :                 case NL80211_IFTYPE_ADHOC:
    3150                 :        212 :                         info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
    3151                 :        212 :                         break;
    3152                 :            :                 case NL80211_IFTYPE_P2P_DEVICE:
    3153                 :        212 :                         info->capa->flags |=
    3154                 :            :                                 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
    3155                 :        212 :                         break;
    3156                 :            :                 case NL80211_IFTYPE_P2P_GO:
    3157                 :        212 :                         info->p2p_go_supported = 1;
    3158                 :        212 :                         break;
    3159                 :            :                 case NL80211_IFTYPE_P2P_CLIENT:
    3160                 :        212 :                         info->p2p_client_supported = 1;
    3161                 :        212 :                         break;
    3162                 :            :                 case NL80211_IFTYPE_MONITOR:
    3163                 :        212 :                         info->monitor_supported = 1;
    3164                 :        212 :                         break;
    3165                 :            :                 }
    3166                 :            :         }
    3167                 :            : }
    3168                 :            : 
    3169                 :            : 
    3170                 :        212 : static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
    3171                 :            :                                          struct nlattr *nl_combi)
    3172                 :            : {
    3173                 :            :         struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
    3174                 :            :         struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
    3175                 :            :         struct nlattr *nl_limit, *nl_mode;
    3176                 :            :         int err, rem_limit, rem_mode;
    3177                 :        212 :         int combination_has_p2p = 0, combination_has_mgd = 0;
    3178                 :            :         static struct nla_policy
    3179                 :            :         iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
    3180                 :            :                 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
    3181                 :            :                 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
    3182                 :            :                 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
    3183                 :            :                 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
    3184                 :            :                 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
    3185                 :            :         },
    3186                 :            :         iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
    3187                 :            :                 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
    3188                 :            :                 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
    3189                 :            :         };
    3190                 :            : 
    3191                 :        212 :         err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
    3192                 :            :                                nl_combi, iface_combination_policy);
    3193 [ +  - ][ +  - ]:        212 :         if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
                 [ +  - ]
    3194         [ -  + ]:        212 :             !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
    3195                 :        212 :             !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
    3196                 :          0 :                 return 0; /* broken combination */
    3197                 :            : 
    3198         [ +  - ]:        212 :         if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
    3199                 :        212 :                 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
    3200                 :            : 
    3201         [ +  - ]:        424 :         nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
    3202                 :            :                             rem_limit) {
    3203                 :        424 :                 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
    3204                 :            :                                        nl_limit, iface_limit_policy);
    3205 [ -  + ][ +  - ]:        424 :                 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
    3206                 :          0 :                         return 0; /* broken combination */
    3207                 :            : 
    3208         [ +  + ]:       1484 :                 nla_for_each_nested(nl_mode,
    3209                 :            :                                     tb_limit[NL80211_IFACE_LIMIT_TYPES],
    3210                 :            :                                     rem_mode) {
    3211                 :       1060 :                         int ift = nla_type(nl_mode);
    3212 [ +  + ][ +  + ]:       1060 :                         if (ift == NL80211_IFTYPE_P2P_GO ||
    3213                 :            :                             ift == NL80211_IFTYPE_P2P_CLIENT)
    3214                 :        424 :                                 combination_has_p2p = 1;
    3215         [ +  + ]:       1060 :                         if (ift == NL80211_IFTYPE_STATION)
    3216                 :        212 :                                 combination_has_mgd = 1;
    3217                 :            :                 }
    3218 [ +  + ][ +  - ]:        424 :                 if (combination_has_p2p && combination_has_mgd)
    3219                 :        212 :                         break;
    3220                 :            :         }
    3221                 :            : 
    3222 [ +  - ][ +  - ]:        212 :         if (combination_has_p2p && combination_has_mgd) {
    3223                 :        212 :                 info->p2p_concurrent = 1;
    3224                 :        212 :                 info->num_multichan_concurrent =
    3225                 :        212 :                         nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
    3226                 :        212 :                 return 1;
    3227                 :            :         }
    3228                 :            : 
    3229                 :        212 :         return 0;
    3230                 :            : }
    3231                 :            : 
    3232                 :            : 
    3233                 :      11236 : static void wiphy_info_iface_comb(struct wiphy_info_data *info,
    3234                 :            :                                   struct nlattr *tb)
    3235                 :            : {
    3236                 :            :         struct nlattr *nl_combi;
    3237                 :            :         int rem_combi;
    3238                 :            : 
    3239         [ +  + ]:      11236 :         if (tb == NULL)
    3240                 :      11236 :                 return;
    3241                 :            : 
    3242         [ +  - ]:        212 :         nla_for_each_nested(nl_combi, tb, rem_combi) {
    3243         [ +  - ]:        212 :                 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
    3244                 :        212 :                         break;
    3245                 :            :         }
    3246                 :            : }
    3247                 :            : 
    3248                 :            : 
    3249                 :      11236 : static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
    3250                 :            :                                  struct nlattr *tb)
    3251                 :            : {
    3252                 :            :         struct nlattr *nl_cmd;
    3253                 :            :         int i;
    3254                 :            : 
    3255         [ +  + ]:      11236 :         if (tb == NULL)
    3256                 :      11236 :                 return;
    3257                 :            : 
    3258         [ +  + ]:       6148 :         nla_for_each_nested(nl_cmd, tb, i) {
    3259   [ +  +  -  +  :       5936 :                 switch (nla_get_u32(nl_cmd)) {
                -  +  + ]
    3260                 :            :                 case NL80211_CMD_AUTHENTICATE:
    3261                 :        212 :                         info->auth_supported = 1;
    3262                 :        212 :                         break;
    3263                 :            :                 case NL80211_CMD_CONNECT:
    3264                 :        212 :                         info->connect_supported = 1;
    3265                 :        212 :                         break;
    3266                 :            :                 case NL80211_CMD_START_SCHED_SCAN:
    3267                 :          0 :                         info->capa->sched_scan_supported = 1;
    3268                 :          0 :                         break;
    3269                 :            :                 case NL80211_CMD_PROBE_CLIENT:
    3270                 :        212 :                         info->poll_command_supported = 1;
    3271                 :        212 :                         break;
    3272                 :            :                 case NL80211_CMD_CHANNEL_SWITCH:
    3273                 :          0 :                         info->channel_switch_supported = 1;
    3274                 :          0 :                         break;
    3275                 :            :                 case NL80211_CMD_SET_QOS_MAP:
    3276                 :        212 :                         info->set_qos_map_supported = 1;
    3277                 :        212 :                         break;
    3278                 :            :                 }
    3279                 :            :         }
    3280                 :            : }
    3281                 :            : 
    3282                 :            : 
    3283                 :      11236 : static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
    3284                 :            :                                      struct nlattr *tb)
    3285                 :            : {
    3286                 :            :         int i, num;
    3287                 :            :         u32 *ciphers;
    3288                 :            : 
    3289         [ +  + ]:      11236 :         if (tb == NULL)
    3290                 :      11236 :                 return;
    3291                 :            : 
    3292                 :        212 :         num = nla_len(tb) / sizeof(u32);
    3293                 :        212 :         ciphers = nla_data(tb);
    3294         [ +  + ]:       2544 :         for (i = 0; i < num; i++) {
    3295                 :       2332 :                 u32 c = ciphers[i];
    3296                 :            : 
    3297                 :       2332 :                 wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
    3298                 :       2332 :                            c >> 24, (c >> 16) & 0xff,
    3299                 :       2332 :                            (c >> 8) & 0xff, c & 0xff);
    3300   [ +  +  +  +  :       2332 :                 switch (c) {
          +  +  +  +  +  
                +  +  - ]
    3301                 :            :                 case WLAN_CIPHER_SUITE_CCMP_256:
    3302                 :        212 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
    3303                 :        212 :                         break;
    3304                 :            :                 case WLAN_CIPHER_SUITE_GCMP_256:
    3305                 :        212 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
    3306                 :        212 :                         break;
    3307                 :            :                 case WLAN_CIPHER_SUITE_CCMP:
    3308                 :        212 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
    3309                 :        212 :                         break;
    3310                 :            :                 case WLAN_CIPHER_SUITE_GCMP:
    3311                 :        212 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
    3312                 :        212 :                         break;
    3313                 :            :                 case WLAN_CIPHER_SUITE_TKIP:
    3314                 :        212 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
    3315                 :        212 :                         break;
    3316                 :            :                 case WLAN_CIPHER_SUITE_WEP104:
    3317                 :        212 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
    3318                 :        212 :                         break;
    3319                 :            :                 case WLAN_CIPHER_SUITE_WEP40:
    3320                 :        212 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
    3321                 :        212 :                         break;
    3322                 :            :                 case WLAN_CIPHER_SUITE_AES_CMAC:
    3323                 :        212 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
    3324                 :        212 :                         break;
    3325                 :            :                 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
    3326                 :        212 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
    3327                 :        212 :                         break;
    3328                 :            :                 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
    3329                 :        212 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
    3330                 :        212 :                         break;
    3331                 :            :                 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
    3332                 :        212 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
    3333                 :        212 :                         break;
    3334                 :            :                 }
    3335                 :            :         }
    3336                 :            : }
    3337                 :            : 
    3338                 :            : 
    3339                 :      11236 : static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
    3340                 :            :                                struct nlattr *tb)
    3341                 :            : {
    3342         [ +  + ]:      11236 :         if (tb)
    3343                 :        212 :                 capa->max_remain_on_chan = nla_get_u32(tb);
    3344                 :      11236 : }
    3345                 :            : 
    3346                 :            : 
    3347                 :      11236 : static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
    3348                 :            :                             struct nlattr *ext_setup)
    3349                 :            : {
    3350         [ +  + ]:      11236 :         if (tdls == NULL)
    3351                 :      11236 :                 return;
    3352                 :            : 
    3353                 :        212 :         wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
    3354                 :        212 :         capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
    3355                 :            : 
    3356         [ +  - ]:        212 :         if (ext_setup) {
    3357                 :        212 :                 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
    3358                 :        212 :                 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
    3359                 :            :         }
    3360                 :            : }
    3361                 :            : 
    3362                 :            : 
    3363                 :      11236 : static void wiphy_info_feature_flags(struct wiphy_info_data *info,
    3364                 :            :                                      struct nlattr *tb)
    3365                 :            : {
    3366                 :            :         u32 flags;
    3367                 :      11236 :         struct wpa_driver_capa *capa = info->capa;
    3368                 :            : 
    3369         [ +  + ]:      11236 :         if (tb == NULL)
    3370                 :      11236 :                 return;
    3371                 :            : 
    3372                 :        212 :         flags = nla_get_u32(tb);
    3373                 :            : 
    3374         [ +  - ]:        212 :         if (flags & NL80211_FEATURE_SK_TX_STATUS)
    3375                 :        212 :                 info->data_tx_status = 1;
    3376                 :            : 
    3377         [ -  + ]:        212 :         if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
    3378                 :          0 :                 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
    3379                 :            : 
    3380         [ +  - ]:        212 :         if (flags & NL80211_FEATURE_SAE)
    3381                 :        212 :                 capa->flags |= WPA_DRIVER_FLAGS_SAE;
    3382                 :            : 
    3383         [ -  + ]:        212 :         if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
    3384                 :          0 :                 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
    3385                 :            : }
    3386                 :            : 
    3387                 :            : 
    3388                 :      11236 : static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
    3389                 :            :                                           struct nlattr *tb)
    3390                 :            : {
    3391                 :            :         u32 protocols;
    3392                 :            : 
    3393         [ +  - ]:      11236 :         if (tb == NULL)
    3394                 :      11236 :                 return;
    3395                 :            : 
    3396                 :          0 :         protocols = nla_get_u32(tb);
    3397                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
    3398                 :            :                    "mode");
    3399                 :          0 :         capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
    3400                 :          0 :         capa->probe_resp_offloads = probe_resp_offload_support(protocols);
    3401                 :            : }
    3402                 :            : 
    3403                 :            : 
    3404                 :      11236 : static int wiphy_info_handler(struct nl_msg *msg, void *arg)
    3405                 :            : {
    3406                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    3407                 :      11236 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    3408                 :      11236 :         struct wiphy_info_data *info = arg;
    3409                 :      11236 :         struct wpa_driver_capa *capa = info->capa;
    3410                 :      11236 :         struct wpa_driver_nl80211_data *drv = info->drv;
    3411                 :            : 
    3412                 :      11236 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    3413                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    3414                 :            : 
    3415         [ +  - ]:      11236 :         if (tb[NL80211_ATTR_WIPHY_NAME])
    3416                 :      11236 :                 os_strlcpy(drv->phyname,
    3417                 :      11236 :                            nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
    3418                 :            :                            sizeof(drv->phyname));
    3419         [ +  + ]:      11236 :         if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
    3420                 :        212 :                 capa->max_scan_ssids =
    3421                 :        212 :                         nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
    3422                 :            : 
    3423         [ +  + ]:      11236 :         if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
    3424                 :        212 :                 capa->max_sched_scan_ssids =
    3425                 :        212 :                         nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
    3426                 :            : 
    3427         [ +  + ]:      11236 :         if (tb[NL80211_ATTR_MAX_MATCH_SETS])
    3428                 :        212 :                 capa->max_match_sets =
    3429                 :        212 :                         nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
    3430                 :            : 
    3431         [ -  + ]:      11236 :         if (tb[NL80211_ATTR_MAC_ACL_MAX])
    3432                 :          0 :                 capa->max_acl_mac_addrs =
    3433                 :          0 :                         nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
    3434                 :            : 
    3435                 :      11236 :         wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
    3436                 :      11236 :         wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
    3437                 :      11236 :         wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
    3438                 :      11236 :         wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
    3439                 :            : 
    3440         [ +  + ]:      11236 :         if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
    3441                 :        212 :                 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
    3442                 :            :                            "off-channel TX");
    3443                 :        212 :                 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
    3444                 :            :         }
    3445                 :            : 
    3446         [ -  + ]:      11236 :         if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
    3447                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
    3448                 :          0 :                 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
    3449                 :            :         }
    3450                 :            : 
    3451                 :      11236 :         wiphy_info_max_roc(capa,
    3452                 :            :                            tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
    3453                 :            : 
    3454         [ +  + ]:      11236 :         if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
    3455                 :        212 :                 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
    3456                 :            : 
    3457                 :      11236 :         wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
    3458                 :            :                         tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
    3459                 :            : 
    3460         [ -  + ]:      11236 :         if (tb[NL80211_ATTR_DEVICE_AP_SME])
    3461                 :          0 :                 info->device_ap_sme = 1;
    3462                 :            : 
    3463                 :      11236 :         wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
    3464                 :      11236 :         wiphy_info_probe_resp_offload(capa,
    3465                 :            :                                       tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
    3466                 :            : 
    3467 [ +  - ][ +  - ]:      11236 :         if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
                 [ +  + ]
    3468                 :        212 :             drv->extended_capa == NULL) {
    3469                 :        212 :                 drv->extended_capa =
    3470                 :        212 :                         os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
    3471         [ +  - ]:        212 :                 if (drv->extended_capa) {
    3472                 :        212 :                         os_memcpy(drv->extended_capa,
    3473                 :            :                                   nla_data(tb[NL80211_ATTR_EXT_CAPA]),
    3474                 :            :                                   nla_len(tb[NL80211_ATTR_EXT_CAPA]));
    3475                 :        212 :                         drv->extended_capa_len =
    3476                 :        212 :                                 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
    3477                 :            :                 }
    3478                 :        212 :                 drv->extended_capa_mask =
    3479                 :        212 :                         os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
    3480         [ +  - ]:        212 :                 if (drv->extended_capa_mask) {
    3481                 :        212 :                         os_memcpy(drv->extended_capa_mask,
    3482                 :            :                                   nla_data(tb[NL80211_ATTR_EXT_CAPA]),
    3483                 :            :                                   nla_len(tb[NL80211_ATTR_EXT_CAPA]));
    3484                 :            :                 } else {
    3485                 :          0 :                         os_free(drv->extended_capa);
    3486                 :          0 :                         drv->extended_capa = NULL;
    3487                 :          0 :                         drv->extended_capa_len = 0;
    3488                 :            :                 }
    3489                 :            :         }
    3490                 :            : 
    3491                 :      11236 :         return NL_SKIP;
    3492                 :            : }
    3493                 :            : 
    3494                 :            : 
    3495                 :        212 : static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
    3496                 :            :                                        struct wiphy_info_data *info)
    3497                 :            : {
    3498                 :            :         u32 feat;
    3499                 :            :         struct nl_msg *msg;
    3500                 :            : 
    3501                 :        212 :         os_memset(info, 0, sizeof(*info));
    3502                 :        212 :         info->capa = &drv->capa;
    3503                 :        212 :         info->drv = drv;
    3504                 :            : 
    3505                 :        212 :         msg = nlmsg_alloc();
    3506         [ -  + ]:        212 :         if (!msg)
    3507                 :          0 :                 return -1;
    3508                 :            : 
    3509                 :        212 :         feat = get_nl80211_protocol_features(drv);
    3510         [ +  - ]:        212 :         if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
    3511                 :        212 :                 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
    3512                 :            :         else
    3513                 :          0 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
    3514                 :            : 
    3515         [ +  - ]:        212 :         NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
    3516         [ -  + ]:        212 :         if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
    3517                 :          0 :                 goto nla_put_failure;
    3518                 :            : 
    3519         [ -  + ]:        212 :         if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
    3520                 :          0 :                 return -1;
    3521                 :            : 
    3522         [ +  - ]:        212 :         if (info->auth_supported)
    3523                 :        212 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
    3524         [ #  # ]:          0 :         else if (!info->connect_supported) {
    3525                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
    3526                 :            :                            "authentication/association or connect commands");
    3527                 :          0 :                 info->error = 1;
    3528                 :            :         }
    3529                 :            : 
    3530 [ +  - ][ +  - ]:        212 :         if (info->p2p_go_supported && info->p2p_client_supported)
    3531                 :        212 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
    3532         [ +  - ]:        212 :         if (info->p2p_concurrent) {
    3533                 :        212 :                 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
    3534                 :            :                            "interface (driver advertised support)");
    3535                 :        212 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
    3536                 :        212 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
    3537                 :            :         }
    3538         [ -  + ]:        212 :         if (info->num_multichan_concurrent > 1) {
    3539                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
    3540                 :            :                            "concurrent (driver advertised support)");
    3541                 :          0 :                 drv->capa.num_multichan_concurrent =
    3542                 :          0 :                         info->num_multichan_concurrent;
    3543                 :            :         }
    3544                 :            : 
    3545                 :            :         /* default to 5000 since early versions of mac80211 don't set it */
    3546         [ -  + ]:        212 :         if (!drv->capa.max_remain_on_chan)
    3547                 :          0 :                 drv->capa.max_remain_on_chan = 5000;
    3548                 :            : 
    3549                 :        212 :         return 0;
    3550                 :            : nla_put_failure:
    3551                 :          0 :         nlmsg_free(msg);
    3552                 :        212 :         return -1;
    3553                 :            : }
    3554                 :            : 
    3555                 :            : 
    3556                 :        212 : static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
    3557                 :            : {
    3558                 :            :         struct wiphy_info_data info;
    3559         [ -  + ]:        212 :         if (wpa_driver_nl80211_get_info(drv, &info))
    3560                 :          0 :                 return -1;
    3561                 :            : 
    3562         [ -  + ]:        212 :         if (info.error)
    3563                 :          0 :                 return -1;
    3564                 :            : 
    3565                 :        212 :         drv->has_capability = 1;
    3566                 :        212 :         drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
    3567                 :            :                 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
    3568                 :            :                 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
    3569                 :            :                 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
    3570                 :        212 :         drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
    3571                 :            :                 WPA_DRIVER_AUTH_SHARED |
    3572                 :            :                 WPA_DRIVER_AUTH_LEAP;
    3573                 :            : 
    3574                 :        212 :         drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
    3575                 :        212 :         drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
    3576                 :        212 :         drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
    3577                 :            : 
    3578         [ +  - ]:        212 :         if (!info.device_ap_sme) {
    3579                 :        212 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
    3580                 :            : 
    3581                 :            :                 /*
    3582                 :            :                  * No AP SME is currently assumed to also indicate no AP MLME
    3583                 :            :                  * in the driver/firmware.
    3584                 :            :                  */
    3585                 :        212 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
    3586                 :            :         }
    3587                 :            : 
    3588                 :        212 :         drv->device_ap_sme = info.device_ap_sme;
    3589                 :        212 :         drv->poll_command_supported = info.poll_command_supported;
    3590                 :        212 :         drv->data_tx_status = info.data_tx_status;
    3591                 :        212 :         drv->channel_switch_supported = info.channel_switch_supported;
    3592         [ +  - ]:        212 :         if (info.set_qos_map_supported)
    3593                 :        212 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
    3594                 :            : 
    3595                 :            :         /*
    3596                 :            :          * If poll command and tx status are supported, mac80211 is new enough
    3597                 :            :          * to have everything we need to not need monitor interfaces.
    3598                 :            :          */
    3599 [ +  - ][ -  + ]:        212 :         drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
    3600                 :            : 
    3601 [ -  + ][ #  # ]:        212 :         if (drv->device_ap_sme && drv->use_monitor) {
    3602                 :            :                 /*
    3603                 :            :                  * Non-mac80211 drivers may not support monitor interface.
    3604                 :            :                  * Make sure we do not get stuck with incorrect capability here
    3605                 :            :                  * by explicitly testing this.
    3606                 :            :                  */
    3607         [ #  # ]:          0 :                 if (!info.monitor_supported) {
    3608                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
    3609                 :            :                                    "with device_ap_sme since no monitor mode "
    3610                 :            :                                    "support detected");
    3611                 :          0 :                         drv->use_monitor = 0;
    3612                 :            :                 }
    3613                 :            :         }
    3614                 :            : 
    3615                 :            :         /*
    3616                 :            :          * If we aren't going to use monitor interfaces, but the
    3617                 :            :          * driver doesn't support data TX status, we won't get TX
    3618                 :            :          * status for EAPOL frames.
    3619                 :            :          */
    3620 [ +  - ][ -  + ]:        212 :         if (!drv->use_monitor && !info.data_tx_status)
    3621                 :          0 :                 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
    3622                 :            : 
    3623                 :        212 :         return 0;
    3624                 :            : }
    3625                 :            : 
    3626                 :            : 
    3627                 :            : #ifdef ANDROID
    3628                 :            : static int android_genl_ctrl_resolve(struct nl_handle *handle,
    3629                 :            :                                      const char *name)
    3630                 :            : {
    3631                 :            :         /*
    3632                 :            :          * Android ICS has very minimal genl_ctrl_resolve() implementation, so
    3633                 :            :          * need to work around that.
    3634                 :            :          */
    3635                 :            :         struct nl_cache *cache = NULL;
    3636                 :            :         struct genl_family *nl80211 = NULL;
    3637                 :            :         int id = -1;
    3638                 :            : 
    3639                 :            :         if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
    3640                 :            :                 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
    3641                 :            :                            "netlink cache");
    3642                 :            :                 goto fail;
    3643                 :            :         }
    3644                 :            : 
    3645                 :            :         nl80211 = genl_ctrl_search_by_name(cache, name);
    3646                 :            :         if (nl80211 == NULL)
    3647                 :            :                 goto fail;
    3648                 :            : 
    3649                 :            :         id = genl_family_get_id(nl80211);
    3650                 :            : 
    3651                 :            : fail:
    3652                 :            :         if (nl80211)
    3653                 :            :                 genl_family_put(nl80211);
    3654                 :            :         if (cache)
    3655                 :            :                 nl_cache_free(cache);
    3656                 :            : 
    3657                 :            :         return id;
    3658                 :            : }
    3659                 :            : #define genl_ctrl_resolve android_genl_ctrl_resolve
    3660                 :            : #endif /* ANDROID */
    3661                 :            : 
    3662                 :            : 
    3663                 :          4 : static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
    3664                 :            : {
    3665                 :            :         int ret;
    3666                 :            : 
    3667                 :          4 :         global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
    3668         [ -  + ]:          4 :         if (global->nl_cb == NULL) {
    3669                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
    3670                 :            :                            "callbacks");
    3671                 :          0 :                 return -1;
    3672                 :            :         }
    3673                 :            : 
    3674                 :          4 :         global->nl = nl_create_handle(global->nl_cb, "nl");
    3675         [ -  + ]:          4 :         if (global->nl == NULL)
    3676                 :          0 :                 goto err;
    3677                 :            : 
    3678                 :          4 :         global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
    3679         [ -  + ]:          4 :         if (global->nl80211_id < 0) {
    3680                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
    3681                 :            :                            "found");
    3682                 :          0 :                 goto err;
    3683                 :            :         }
    3684                 :            : 
    3685                 :          4 :         global->nl_event = nl_create_handle(global->nl_cb, "event");
    3686         [ -  + ]:          4 :         if (global->nl_event == NULL)
    3687                 :          0 :                 goto err;
    3688                 :            : 
    3689                 :          4 :         ret = nl_get_multicast_id(global, "nl80211", "scan");
    3690         [ +  - ]:          4 :         if (ret >= 0)
    3691                 :          4 :                 ret = nl_socket_add_membership(global->nl_event, ret);
    3692         [ -  + ]:          4 :         if (ret < 0) {
    3693                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
    3694                 :            :                            "membership for scan events: %d (%s)",
    3695                 :            :                            ret, strerror(-ret));
    3696                 :          0 :                 goto err;
    3697                 :            :         }
    3698                 :            : 
    3699                 :          4 :         ret = nl_get_multicast_id(global, "nl80211", "mlme");
    3700         [ +  - ]:          4 :         if (ret >= 0)
    3701                 :          4 :                 ret = nl_socket_add_membership(global->nl_event, ret);
    3702         [ -  + ]:          4 :         if (ret < 0) {
    3703                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
    3704                 :            :                            "membership for mlme events: %d (%s)",
    3705                 :            :                            ret, strerror(-ret));
    3706                 :          0 :                 goto err;
    3707                 :            :         }
    3708                 :            : 
    3709                 :          4 :         ret = nl_get_multicast_id(global, "nl80211", "regulatory");
    3710         [ +  - ]:          4 :         if (ret >= 0)
    3711                 :          4 :                 ret = nl_socket_add_membership(global->nl_event, ret);
    3712         [ -  + ]:          4 :         if (ret < 0) {
    3713                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
    3714                 :            :                            "membership for regulatory events: %d (%s)",
    3715                 :            :                            ret, strerror(-ret));
    3716                 :            :                 /* Continue without regulatory events */
    3717                 :            :         }
    3718                 :            : 
    3719                 :          4 :         nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
    3720                 :            :                   no_seq_check, NULL);
    3721                 :          4 :         nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
    3722                 :            :                   process_global_event, global);
    3723                 :            : 
    3724                 :          4 :         nl80211_register_eloop_read(&global->nl_event,
    3725                 :            :                                     wpa_driver_nl80211_event_receive,
    3726                 :          4 :                                     global->nl_cb);
    3727                 :            : 
    3728                 :          4 :         return 0;
    3729                 :            : 
    3730                 :            : err:
    3731                 :          0 :         nl_destroy_handles(&global->nl_event);
    3732                 :          0 :         nl_destroy_handles(&global->nl);
    3733                 :          0 :         nl_cb_put(global->nl_cb);
    3734                 :          0 :         global->nl_cb = NULL;
    3735                 :          4 :         return -1;
    3736                 :            : }
    3737                 :            : 
    3738                 :            : 
    3739                 :        212 : static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
    3740                 :            : {
    3741                 :        212 :         drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
    3742         [ -  + ]:        212 :         if (!drv->nl_cb) {
    3743                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
    3744                 :          0 :                 return -1;
    3745                 :            :         }
    3746                 :            : 
    3747                 :        212 :         nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
    3748                 :            :                   no_seq_check, NULL);
    3749                 :        212 :         nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
    3750                 :            :                   process_drv_event, drv);
    3751                 :            : 
    3752                 :        212 :         return 0;
    3753                 :            : }
    3754                 :            : 
    3755                 :            : 
    3756                 :          0 : static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
    3757                 :            : {
    3758                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
    3759                 :            :         /*
    3760                 :            :          * This may be for any interface; use ifdown event to disable
    3761                 :            :          * interface.
    3762                 :            :          */
    3763                 :          0 : }
    3764                 :            : 
    3765                 :            : 
    3766                 :          0 : static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
    3767                 :            : {
    3768                 :          0 :         struct wpa_driver_nl80211_data *drv = ctx;
    3769                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
    3770         [ #  # ]:          0 :         if (i802_set_iface_flags(drv->first_bss, 1)) {
    3771                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
    3772                 :            :                            "after rfkill unblock");
    3773                 :          0 :                 return;
    3774                 :            :         }
    3775                 :            :         /* rtnetlink ifup handler will report interface as enabled */
    3776                 :            : }
    3777                 :            : 
    3778                 :            : 
    3779                 :       1667 : static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
    3780                 :            :                                                       void *eloop_ctx,
    3781                 :            :                                                       void *handle)
    3782                 :            : {
    3783                 :       1667 :         struct wpa_driver_nl80211_data *drv = eloop_ctx;
    3784                 :            :         u8 data[2048];
    3785                 :            :         struct msghdr msg;
    3786                 :            :         struct iovec entry;
    3787                 :            :         u8 control[512];
    3788                 :            :         struct cmsghdr *cmsg;
    3789                 :       1667 :         int res, found_ee = 0, found_wifi = 0, acked = 0;
    3790                 :            :         union wpa_event_data event;
    3791                 :            : 
    3792                 :       1667 :         memset(&msg, 0, sizeof(msg));
    3793                 :       1667 :         msg.msg_iov = &entry;
    3794                 :       1667 :         msg.msg_iovlen = 1;
    3795                 :       1667 :         entry.iov_base = data;
    3796                 :       1667 :         entry.iov_len = sizeof(data);
    3797                 :       1667 :         msg.msg_control = &control;
    3798                 :       1667 :         msg.msg_controllen = sizeof(control);
    3799                 :            : 
    3800                 :       1667 :         res = recvmsg(sock, &msg, MSG_ERRQUEUE);
    3801                 :            :         /* if error or not fitting 802.3 header, return */
    3802         [ -  + ]:       1667 :         if (res < 14)
    3803                 :          0 :                 return;
    3804                 :            : 
    3805 [ +  - ][ +  + ]:       5001 :         for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
    3806                 :            :         {
    3807 [ +  + ][ +  - ]:       3334 :                 if (cmsg->cmsg_level == SOL_SOCKET &&
    3808                 :       1667 :                     cmsg->cmsg_type == SCM_WIFI_STATUS) {
    3809                 :            :                         int *ack;
    3810                 :            : 
    3811                 :       1667 :                         found_wifi = 1;
    3812                 :       1667 :                         ack = (void *)CMSG_DATA(cmsg);
    3813                 :       1667 :                         acked = *ack;
    3814                 :            :                 }
    3815                 :            : 
    3816 [ +  + ][ +  - ]:       3334 :                 if (cmsg->cmsg_level == SOL_PACKET &&
    3817                 :       1667 :                     cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
    3818                 :       1667 :                         struct sock_extended_err *err =
    3819                 :            :                                 (struct sock_extended_err *)CMSG_DATA(cmsg);
    3820                 :            : 
    3821         [ +  - ]:       1667 :                         if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
    3822                 :       1667 :                                 found_ee = 1;
    3823                 :            :                 }
    3824                 :            :         }
    3825                 :            : 
    3826 [ +  - ][ -  + ]:       1667 :         if (!found_ee || !found_wifi)
    3827                 :          0 :                 return;
    3828                 :            : 
    3829                 :       1667 :         memset(&event, 0, sizeof(event));
    3830                 :       1667 :         event.eapol_tx_status.dst = data;
    3831                 :       1667 :         event.eapol_tx_status.data = data + 14;
    3832                 :       1667 :         event.eapol_tx_status.data_len = res - 14;
    3833                 :       1667 :         event.eapol_tx_status.ack = acked;
    3834                 :       1667 :         wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
    3835                 :            : }
    3836                 :            : 
    3837                 :            : 
    3838                 :        229 : static int nl80211_init_bss(struct i802_bss *bss)
    3839                 :            : {
    3840                 :        229 :         bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
    3841         [ -  + ]:        229 :         if (!bss->nl_cb)
    3842                 :          0 :                 return -1;
    3843                 :            : 
    3844                 :        229 :         nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
    3845                 :            :                   no_seq_check, NULL);
    3846                 :        229 :         nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
    3847                 :            :                   process_bss_event, bss);
    3848                 :            : 
    3849                 :        229 :         return 0;
    3850                 :            : }
    3851                 :            : 
    3852                 :            : 
    3853                 :        229 : static void nl80211_destroy_bss(struct i802_bss *bss)
    3854                 :            : {
    3855                 :        229 :         nl_cb_put(bss->nl_cb);
    3856                 :        229 :         bss->nl_cb = NULL;
    3857                 :        229 : }
    3858                 :            : 
    3859                 :            : 
    3860                 :        212 : static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
    3861                 :            :                                           void *global_priv, int hostapd,
    3862                 :            :                                           const u8 *set_addr)
    3863                 :            : {
    3864                 :            :         struct wpa_driver_nl80211_data *drv;
    3865                 :            :         struct rfkill_config *rcfg;
    3866                 :            :         struct i802_bss *bss;
    3867                 :            : 
    3868         [ -  + ]:        212 :         if (global_priv == NULL)
    3869                 :          0 :                 return NULL;
    3870                 :        212 :         drv = os_zalloc(sizeof(*drv));
    3871         [ -  + ]:        212 :         if (drv == NULL)
    3872                 :          0 :                 return NULL;
    3873                 :        212 :         drv->global = global_priv;
    3874                 :        212 :         drv->ctx = ctx;
    3875                 :        212 :         drv->hostapd = !!hostapd;
    3876                 :        212 :         drv->eapol_sock = -1;
    3877                 :        212 :         drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
    3878                 :        212 :         drv->if_indices = drv->default_if_indices;
    3879                 :            : 
    3880                 :        212 :         drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
    3881         [ -  + ]:        212 :         if (!drv->first_bss) {
    3882                 :          0 :                 os_free(drv);
    3883                 :          0 :                 return NULL;
    3884                 :            :         }
    3885                 :        212 :         bss = drv->first_bss;
    3886                 :        212 :         bss->drv = drv;
    3887                 :        212 :         bss->ctx = ctx;
    3888                 :            : 
    3889                 :        212 :         os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
    3890                 :        212 :         drv->monitor_ifidx = -1;
    3891                 :        212 :         drv->monitor_sock = -1;
    3892                 :        212 :         drv->eapol_tx_sock = -1;
    3893                 :        212 :         drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
    3894                 :            : 
    3895         [ -  + ]:        212 :         if (wpa_driver_nl80211_init_nl(drv)) {
    3896                 :          0 :                 os_free(drv);
    3897                 :          0 :                 return NULL;
    3898                 :            :         }
    3899                 :            : 
    3900         [ -  + ]:        212 :         if (nl80211_init_bss(bss))
    3901                 :          0 :                 goto failed;
    3902                 :            : 
    3903                 :        212 :         rcfg = os_zalloc(sizeof(*rcfg));
    3904         [ -  + ]:        212 :         if (rcfg == NULL)
    3905                 :          0 :                 goto failed;
    3906                 :        212 :         rcfg->ctx = drv;
    3907                 :        212 :         os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
    3908                 :        212 :         rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
    3909                 :        212 :         rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
    3910                 :        212 :         drv->rfkill = rfkill_init(rcfg);
    3911         [ -  + ]:        212 :         if (drv->rfkill == NULL) {
    3912                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
    3913                 :          0 :                 os_free(rcfg);
    3914                 :            :         }
    3915                 :            : 
    3916         [ -  + ]:        212 :         if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
    3917                 :          0 :                 drv->start_iface_up = 1;
    3918                 :            : 
    3919         [ -  + ]:        212 :         if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1))
    3920                 :          0 :                 goto failed;
    3921                 :            : 
    3922                 :        212 :         drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
    3923         [ -  + ]:        212 :         if (drv->eapol_tx_sock < 0)
    3924                 :          0 :                 goto failed;
    3925                 :            : 
    3926         [ +  - ]:        212 :         if (drv->data_tx_status) {
    3927                 :        212 :                 int enabled = 1;
    3928                 :            : 
    3929         [ -  + ]:        212 :                 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
    3930                 :            :                                &enabled, sizeof(enabled)) < 0) {
    3931                 :          0 :                         wpa_printf(MSG_DEBUG,
    3932                 :            :                                 "nl80211: wifi status sockopt failed\n");
    3933                 :          0 :                         drv->data_tx_status = 0;
    3934         [ #  # ]:          0 :                         if (!drv->use_monitor)
    3935                 :          0 :                                 drv->capa.flags &=
    3936                 :            :                                         ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
    3937                 :            :                 } else {
    3938                 :        212 :                         eloop_register_read_sock(drv->eapol_tx_sock,
    3939                 :            :                                 wpa_driver_nl80211_handle_eapol_tx_status,
    3940                 :            :                                 drv, NULL);
    3941                 :            :                 }
    3942                 :            :         }
    3943                 :            : 
    3944         [ +  - ]:        212 :         if (drv->global) {
    3945                 :        212 :                 dl_list_add(&drv->global->interfaces, &drv->list);
    3946                 :        212 :                 drv->in_interface_list = 1;
    3947                 :            :         }
    3948                 :            : 
    3949                 :        212 :         return bss;
    3950                 :            : 
    3951                 :            : failed:
    3952                 :          0 :         wpa_driver_nl80211_deinit(bss);
    3953                 :        212 :         return NULL;
    3954                 :            : }
    3955                 :            : 
    3956                 :            : 
    3957                 :            : /**
    3958                 :            :  * wpa_driver_nl80211_init - Initialize nl80211 driver interface
    3959                 :            :  * @ctx: context to be used when calling wpa_supplicant functions,
    3960                 :            :  * e.g., wpa_supplicant_event()
    3961                 :            :  * @ifname: interface name, e.g., wlan0
    3962                 :            :  * @global_priv: private driver global data from global_init()
    3963                 :            :  * Returns: Pointer to private data, %NULL on failure
    3964                 :            :  */
    3965                 :         22 : static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
    3966                 :            :                                       void *global_priv)
    3967                 :            : {
    3968                 :         22 :         return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL);
    3969                 :            : }
    3970                 :            : 
    3971                 :            : 
    3972                 :       4449 : static int nl80211_register_frame(struct i802_bss *bss,
    3973                 :            :                                   struct nl_handle *nl_handle,
    3974                 :            :                                   u16 type, const u8 *match, size_t match_len)
    3975                 :            : {
    3976                 :       4449 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    3977                 :            :         struct nl_msg *msg;
    3978                 :       4449 :         int ret = -1;
    3979                 :            : 
    3980                 :       4449 :         msg = nlmsg_alloc();
    3981         [ -  + ]:       4449 :         if (!msg)
    3982                 :          0 :                 return -1;
    3983                 :            : 
    3984                 :       4449 :         wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p",
    3985                 :            :                    type, nl_handle);
    3986                 :       4449 :         wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
    3987                 :            :                     match, match_len);
    3988                 :            : 
    3989                 :       4449 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
    3990                 :            : 
    3991         [ -  + ]:       4449 :         if (nl80211_set_iface_id(msg, bss) < 0)
    3992                 :          0 :                 goto nla_put_failure;
    3993                 :            : 
    3994         [ +  - ]:       4449 :         NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
    3995         [ +  - ]:       4449 :         NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
    3996                 :            : 
    3997                 :       4449 :         ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
    3998                 :       4449 :         msg = NULL;
    3999         [ -  + ]:       4449 :         if (ret) {
    4000                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
    4001                 :            :                            "failed (type=%u): ret=%d (%s)",
    4002                 :            :                            type, ret, strerror(-ret));
    4003                 :          0 :                 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
    4004                 :            :                             match, match_len);
    4005                 :          0 :                 goto nla_put_failure;
    4006                 :            :         }
    4007                 :       4449 :         ret = 0;
    4008                 :            : nla_put_failure:
    4009                 :       4449 :         nlmsg_free(msg);
    4010                 :       4449 :         return ret;
    4011                 :            : }
    4012                 :            : 
    4013                 :            : 
    4014                 :        443 : static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
    4015                 :            : {
    4016                 :        443 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4017                 :            : 
    4018         [ -  + ]:        443 :         if (bss->nl_mgmt) {
    4019                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
    4020                 :            :                            "already on! (nl_mgmt=%p)", bss->nl_mgmt);
    4021                 :          0 :                 return -1;
    4022                 :            :         }
    4023                 :            : 
    4024                 :        443 :         bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
    4025         [ -  + ]:        443 :         if (bss->nl_mgmt == NULL)
    4026                 :          0 :                 return -1;
    4027                 :            : 
    4028                 :        443 :         return 0;
    4029                 :            : }
    4030                 :            : 
    4031                 :            : 
    4032                 :        443 : static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
    4033                 :            : {
    4034                 :        443 :         nl80211_register_eloop_read(&bss->nl_mgmt,
    4035                 :            :                                     wpa_driver_nl80211_event_receive,
    4036                 :        443 :                                     bss->nl_cb);
    4037                 :        443 : }
    4038                 :            : 
    4039                 :            : 
    4040                 :       2064 : static int nl80211_register_action_frame(struct i802_bss *bss,
    4041                 :            :                                          const u8 *match, size_t match_len)
    4042                 :            : {
    4043                 :       2064 :         u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
    4044                 :       2064 :         return nl80211_register_frame(bss, bss->nl_mgmt,
    4045                 :            :                                       type, match, match_len);
    4046                 :            : }
    4047                 :            : 
    4048                 :            : 
    4049                 :        172 : static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
    4050                 :            : {
    4051                 :        172 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4052                 :            : 
    4053         [ -  + ]:        172 :         if (nl80211_alloc_mgmt_handle(bss))
    4054                 :          0 :                 return -1;
    4055                 :        172 :         wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
    4056                 :            :                    "handle %p", bss->nl_mgmt);
    4057                 :            : 
    4058         [ +  + ]:        172 :         if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
    4059                 :          6 :                 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
    4060                 :            : 
    4061                 :            :                 /* register for any AUTH message */
    4062                 :          6 :                 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
    4063                 :            :         }
    4064                 :            : 
    4065                 :            : #ifdef CONFIG_INTERWORKING
    4066                 :            :         /* QoS Map Configure */
    4067         [ -  + ]:        172 :         if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
    4068                 :          0 :                 return -1;
    4069                 :            : #endif /* CONFIG_INTERWORKING */
    4070                 :            : #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
    4071                 :            :         /* GAS Initial Request */
    4072         [ -  + ]:        172 :         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
    4073                 :          0 :                 return -1;
    4074                 :            :         /* GAS Initial Response */
    4075         [ -  + ]:        172 :         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
    4076                 :          0 :                 return -1;
    4077                 :            :         /* GAS Comeback Request */
    4078         [ -  + ]:        172 :         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
    4079                 :          0 :                 return -1;
    4080                 :            :         /* GAS Comeback Response */
    4081         [ -  + ]:        172 :         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
    4082                 :          0 :                 return -1;
    4083                 :            : #endif /* CONFIG_P2P || CONFIG_INTERWORKING */
    4084                 :            : #ifdef CONFIG_P2P
    4085                 :            :         /* P2P Public Action */
    4086         [ -  + ]:        172 :         if (nl80211_register_action_frame(bss,
    4087                 :            :                                           (u8 *) "\x04\x09\x50\x6f\x9a\x09",
    4088                 :            :                                           6) < 0)
    4089                 :          0 :                 return -1;
    4090                 :            :         /* P2P Action */
    4091         [ -  + ]:        172 :         if (nl80211_register_action_frame(bss,
    4092                 :            :                                           (u8 *) "\x7f\x50\x6f\x9a\x09",
    4093                 :            :                                           5) < 0)
    4094                 :          0 :                 return -1;
    4095                 :            : #endif /* CONFIG_P2P */
    4096                 :            : #ifdef CONFIG_IEEE80211W
    4097                 :            :         /* SA Query Response */
    4098         [ -  + ]:        172 :         if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
    4099                 :          0 :                 return -1;
    4100                 :            : #endif /* CONFIG_IEEE80211W */
    4101                 :            : #ifdef CONFIG_TDLS
    4102         [ +  - ]:        172 :         if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
    4103                 :            :                 /* TDLS Discovery Response */
    4104         [ -  + ]:        172 :                 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
    4105                 :            :                     0)
    4106                 :          0 :                         return -1;
    4107                 :            :         }
    4108                 :            : #endif /* CONFIG_TDLS */
    4109                 :            : 
    4110                 :            :         /* FT Action frames */
    4111         [ -  + ]:        172 :         if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
    4112                 :          0 :                 return -1;
    4113                 :            :         else
    4114                 :        172 :                 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
    4115                 :            :                         WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
    4116                 :            : 
    4117                 :            :         /* WNM - BSS Transition Management Request */
    4118         [ -  + ]:        172 :         if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
    4119                 :          0 :                 return -1;
    4120                 :            :         /* WNM-Sleep Mode Response */
    4121         [ -  + ]:        172 :         if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
    4122                 :          0 :                 return -1;
    4123                 :            : 
    4124                 :        172 :         nl80211_mgmt_handle_register_eloop(bss);
    4125                 :            : 
    4126                 :        172 :         return 0;
    4127                 :            : }
    4128                 :            : 
    4129                 :            : 
    4130                 :        271 : static int nl80211_register_spurious_class3(struct i802_bss *bss)
    4131                 :            : {
    4132                 :        271 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4133                 :            :         struct nl_msg *msg;
    4134                 :        271 :         int ret = -1;
    4135                 :            : 
    4136                 :        271 :         msg = nlmsg_alloc();
    4137         [ -  + ]:        271 :         if (!msg)
    4138                 :          0 :                 return -1;
    4139                 :            : 
    4140                 :        271 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
    4141                 :            : 
    4142         [ +  - ]:        271 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
    4143                 :            : 
    4144                 :        271 :         ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
    4145                 :        271 :         msg = NULL;
    4146         [ -  + ]:        271 :         if (ret) {
    4147                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
    4148                 :            :                            "failed: ret=%d (%s)",
    4149                 :            :                            ret, strerror(-ret));
    4150                 :          0 :                 goto nla_put_failure;
    4151                 :            :         }
    4152                 :        271 :         ret = 0;
    4153                 :            : nla_put_failure:
    4154                 :        271 :         nlmsg_free(msg);
    4155                 :        271 :         return ret;
    4156                 :            : }
    4157                 :            : 
    4158                 :            : 
    4159                 :        271 : static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
    4160                 :            : {
    4161                 :            :         static const int stypes[] = {
    4162                 :            :                 WLAN_FC_STYPE_AUTH,
    4163                 :            :                 WLAN_FC_STYPE_ASSOC_REQ,
    4164                 :            :                 WLAN_FC_STYPE_REASSOC_REQ,
    4165                 :            :                 WLAN_FC_STYPE_DISASSOC,
    4166                 :            :                 WLAN_FC_STYPE_DEAUTH,
    4167                 :            :                 WLAN_FC_STYPE_ACTION,
    4168                 :            :                 WLAN_FC_STYPE_PROBE_REQ,
    4169                 :            : /* Beacon doesn't work as mac80211 doesn't currently allow
    4170                 :            :  * it, but it wouldn't really be the right thing anyway as
    4171                 :            :  * it isn't per interface ... maybe just dump the scan
    4172                 :            :  * results periodically for OLBC?
    4173                 :            :  */
    4174                 :            : //              WLAN_FC_STYPE_BEACON,
    4175                 :            :         };
    4176                 :            :         unsigned int i;
    4177                 :            : 
    4178         [ -  + ]:        271 :         if (nl80211_alloc_mgmt_handle(bss))
    4179                 :          0 :                 return -1;
    4180                 :        271 :         wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
    4181                 :            :                    "handle %p", bss->nl_mgmt);
    4182                 :            : 
    4183         [ +  + ]:       2168 :         for (i = 0; i < ARRAY_SIZE(stypes); i++) {
    4184         [ -  + ]:       1897 :                 if (nl80211_register_frame(bss, bss->nl_mgmt,
    4185                 :            :                                            (WLAN_FC_TYPE_MGMT << 2) |
    4186                 :       1897 :                                            (stypes[i] << 4),
    4187                 :            :                                            NULL, 0) < 0) {
    4188                 :          0 :                         goto out_err;
    4189                 :            :                 }
    4190                 :            :         }
    4191                 :            : 
    4192         [ -  + ]:        271 :         if (nl80211_register_spurious_class3(bss))
    4193                 :          0 :                 goto out_err;
    4194                 :            : 
    4195         [ -  + ]:        271 :         if (nl80211_get_wiphy_data_ap(bss) == NULL)
    4196                 :          0 :                 goto out_err;
    4197                 :            : 
    4198                 :        271 :         nl80211_mgmt_handle_register_eloop(bss);
    4199                 :        271 :         return 0;
    4200                 :            : 
    4201                 :            : out_err:
    4202                 :          0 :         nl_destroy_handles(&bss->nl_mgmt);
    4203                 :        271 :         return -1;
    4204                 :            : }
    4205                 :            : 
    4206                 :            : 
    4207                 :          0 : static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
    4208                 :            : {
    4209         [ #  # ]:          0 :         if (nl80211_alloc_mgmt_handle(bss))
    4210                 :          0 :                 return -1;
    4211                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
    4212                 :            :                    "handle %p (device SME)", bss->nl_mgmt);
    4213                 :            : 
    4214         [ #  # ]:          0 :         if (nl80211_register_frame(bss, bss->nl_mgmt,
    4215                 :            :                                    (WLAN_FC_TYPE_MGMT << 2) |
    4216                 :            :                                    (WLAN_FC_STYPE_ACTION << 4),
    4217                 :            :                                    NULL, 0) < 0)
    4218                 :          0 :                 goto out_err;
    4219                 :            : 
    4220                 :          0 :         nl80211_mgmt_handle_register_eloop(bss);
    4221                 :          0 :         return 0;
    4222                 :            : 
    4223                 :            : out_err:
    4224                 :          0 :         nl_destroy_handles(&bss->nl_mgmt);
    4225                 :          0 :         return -1;
    4226                 :            : }
    4227                 :            : 
    4228                 :            : 
    4229                 :        867 : static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
    4230                 :            : {
    4231         [ +  + ]:        867 :         if (bss->nl_mgmt == NULL)
    4232                 :        867 :                 return;
    4233                 :        443 :         wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
    4234                 :            :                    "(%s)", bss->nl_mgmt, reason);
    4235                 :        443 :         nl80211_destroy_eloop_handle(&bss->nl_mgmt);
    4236                 :            : 
    4237                 :        443 :         nl80211_put_wiphy_data_ap(bss);
    4238                 :            : }
    4239                 :            : 
    4240                 :            : 
    4241                 :          0 : static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
    4242                 :            : {
    4243                 :          0 :         wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
    4244                 :          0 : }
    4245                 :            : 
    4246                 :            : 
    4247                 :          0 : static void nl80211_del_p2pdev(struct i802_bss *bss)
    4248                 :            : {
    4249                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4250                 :            :         struct nl_msg *msg;
    4251                 :            :         int ret;
    4252                 :            : 
    4253                 :          0 :         msg = nlmsg_alloc();
    4254         [ #  # ]:          0 :         if (!msg)
    4255                 :          0 :                 return;
    4256                 :            : 
    4257                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
    4258         [ #  # ]:          0 :         NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
    4259                 :            : 
    4260                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    4261                 :          0 :         msg = NULL;
    4262                 :            : 
    4263                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
    4264                 :          0 :                    bss->ifname, (long long unsigned int) bss->wdev_id,
    4265                 :            :                    strerror(-ret));
    4266                 :            : 
    4267                 :            : nla_put_failure:
    4268                 :          0 :         nlmsg_free(msg);
    4269                 :            : }
    4270                 :            : 
    4271                 :            : 
    4272                 :          0 : static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
    4273                 :            : {
    4274                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4275                 :            :         struct nl_msg *msg;
    4276                 :          0 :         int ret = -1;
    4277                 :            : 
    4278                 :          0 :         msg = nlmsg_alloc();
    4279         [ #  # ]:          0 :         if (!msg)
    4280                 :          0 :                 return -1;
    4281                 :            : 
    4282         [ #  # ]:          0 :         if (start)
    4283                 :          0 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
    4284                 :            :         else
    4285                 :          0 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
    4286                 :            : 
    4287         [ #  # ]:          0 :         NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
    4288                 :            : 
    4289                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    4290                 :          0 :         msg = NULL;
    4291                 :            : 
    4292         [ #  # ]:          0 :         wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
    4293                 :            :                    start ? "Start" : "Stop",
    4294                 :          0 :                    bss->ifname, (long long unsigned int) bss->wdev_id,
    4295                 :            :                    strerror(-ret));
    4296                 :            : 
    4297                 :            : nla_put_failure:
    4298                 :          0 :         nlmsg_free(msg);
    4299                 :          0 :         return ret;
    4300                 :            : }
    4301                 :            : 
    4302                 :            : 
    4303                 :        212 : static int i802_set_iface_flags(struct i802_bss *bss, int up)
    4304                 :            : {
    4305                 :            :         enum nl80211_iftype nlmode;
    4306                 :            : 
    4307                 :        212 :         nlmode = nl80211_get_ifmode(bss);
    4308         [ +  - ]:        212 :         if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
    4309                 :        212 :                 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
    4310                 :        212 :                                              bss->ifname, up);
    4311                 :            :         }
    4312                 :            : 
    4313                 :            :         /* P2P Device has start/stop which is equivalent */
    4314                 :        212 :         return nl80211_set_p2pdev(bss, up);
    4315                 :            : }
    4316                 :            : 
    4317                 :            : 
    4318                 :            : static int
    4319                 :        212 : wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
    4320                 :            :                                    const u8 *set_addr, int first)
    4321                 :            : {
    4322                 :        212 :         struct i802_bss *bss = drv->first_bss;
    4323                 :        212 :         int send_rfkill_event = 0;
    4324                 :            :         enum nl80211_iftype nlmode;
    4325                 :            : 
    4326                 :        212 :         drv->ifindex = if_nametoindex(bss->ifname);
    4327                 :        212 :         bss->ifindex = drv->ifindex;
    4328                 :        212 :         bss->wdev_id = drv->global->if_add_wdevid;
    4329                 :        212 :         bss->wdev_id_set = drv->global->if_add_wdevid_set;
    4330                 :            : 
    4331                 :        212 :         bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
    4332 [ -  + ][ +  + ]:        212 :         bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
    4333                 :        212 :         drv->global->if_add_wdevid_set = 0;
    4334                 :            : 
    4335         [ -  + ]:        212 :         if (wpa_driver_nl80211_capa(drv))
    4336                 :          0 :                 return -1;
    4337                 :            : 
    4338                 :        212 :         wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
    4339                 :        212 :                    bss->ifname, drv->phyname);
    4340                 :            : 
    4341   [ +  +  +  - ]:        218 :         if (set_addr &&
    4342         [ -  + ]:         12 :             (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
    4343                 :          6 :              linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
    4344                 :            :                                 set_addr)))
    4345                 :          0 :                 return -1;
    4346                 :            : 
    4347 [ +  - ][ -  + ]:        212 :         if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
    4348                 :          0 :                 drv->start_mode_ap = 1;
    4349                 :            : 
    4350         [ +  + ]:        212 :         if (drv->hostapd)
    4351                 :        190 :                 nlmode = NL80211_IFTYPE_AP;
    4352         [ +  + ]:         22 :         else if (bss->if_dynamic)
    4353                 :         19 :                 nlmode = nl80211_get_ifmode(bss);
    4354                 :            :         else
    4355                 :          3 :                 nlmode = NL80211_IFTYPE_STATION;
    4356                 :            : 
    4357         [ -  + ]:        212 :         if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
    4358                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
    4359                 :          0 :                 return -1;
    4360                 :            :         }
    4361                 :            : 
    4362         [ -  + ]:        212 :         if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
    4363                 :          0 :                 int ret = nl80211_set_p2pdev(bss, 1);
    4364         [ #  # ]:          0 :                 if (ret < 0)
    4365                 :          0 :                         wpa_printf(MSG_ERROR, "nl80211: Could not start P2P device");
    4366                 :          0 :                 nl80211_get_macaddr(bss);
    4367                 :          0 :                 return ret;
    4368                 :            :         }
    4369                 :            : 
    4370         [ -  + ]:        212 :         if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
    4371         [ #  # ]:          0 :                 if (rfkill_is_blocked(drv->rfkill)) {
    4372                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
    4373                 :            :                                    "interface '%s' due to rfkill",
    4374                 :          0 :                                    bss->ifname);
    4375                 :          0 :                         drv->if_disabled = 1;
    4376                 :          0 :                         send_rfkill_event = 1;
    4377                 :            :                 } else {
    4378                 :          0 :                         wpa_printf(MSG_ERROR, "nl80211: Could not set "
    4379                 :          0 :                                    "interface '%s' UP", bss->ifname);
    4380                 :          0 :                         return -1;
    4381                 :            :                 }
    4382                 :            :         }
    4383                 :            : 
    4384         [ +  + ]:        212 :         if (!drv->hostapd)
    4385                 :         22 :                 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
    4386                 :            :                                        1, IF_OPER_DORMANT);
    4387                 :            : 
    4388         [ -  + ]:        212 :         if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
    4389                 :        212 :                                bss->addr))
    4390                 :          0 :                 return -1;
    4391                 :            : 
    4392         [ -  + ]:        212 :         if (send_rfkill_event) {
    4393                 :          0 :                 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
    4394                 :            :                                        drv, drv->ctx);
    4395                 :            :         }
    4396                 :            : 
    4397                 :        212 :         return 0;
    4398                 :            : }
    4399                 :            : 
    4400                 :            : 
    4401                 :        255 : static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
    4402                 :            : {
    4403                 :            :         struct nl_msg *msg;
    4404                 :            : 
    4405                 :        255 :         msg = nlmsg_alloc();
    4406         [ -  + ]:        255 :         if (!msg)
    4407                 :          0 :                 return -ENOMEM;
    4408                 :            : 
    4409                 :        255 :         wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
    4410                 :            :                    drv->ifindex);
    4411                 :        255 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
    4412         [ +  - ]:        255 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    4413                 :            : 
    4414                 :        255 :         return send_and_recv_msgs(drv, msg, NULL, NULL);
    4415                 :            :  nla_put_failure:
    4416                 :          0 :         nlmsg_free(msg);
    4417                 :        255 :         return -ENOBUFS;
    4418                 :            : }
    4419                 :            : 
    4420                 :            : 
    4421                 :            : /**
    4422                 :            :  * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
    4423                 :            :  * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
    4424                 :            :  *
    4425                 :            :  * Shut down driver interface and processing of driver events. Free
    4426                 :            :  * private data buffer if one was allocated in wpa_driver_nl80211_init().
    4427                 :            :  */
    4428                 :        212 : static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
    4429                 :            : {
    4430                 :        212 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4431                 :            : 
    4432                 :        212 :         bss->in_deinit = 1;
    4433         [ +  - ]:        212 :         if (drv->data_tx_status)
    4434                 :        212 :                 eloop_unregister_read_sock(drv->eapol_tx_sock);
    4435         [ +  - ]:        212 :         if (drv->eapol_tx_sock >= 0)
    4436                 :        212 :                 close(drv->eapol_tx_sock);
    4437                 :            : 
    4438         [ -  + ]:        212 :         if (bss->nl_preq)
    4439                 :          0 :                 wpa_driver_nl80211_probe_req_report(bss, 0);
    4440         [ -  + ]:        212 :         if (bss->added_if_into_bridge) {
    4441         [ #  # ]:          0 :                 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
    4442                 :          0 :                                     bss->ifname) < 0)
    4443                 :          0 :                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
    4444                 :            :                                    "interface %s from bridge %s: %s",
    4445                 :          0 :                                    bss->ifname, bss->brname, strerror(errno));
    4446                 :            :         }
    4447         [ -  + ]:        212 :         if (bss->added_bridge) {
    4448         [ #  # ]:          0 :                 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
    4449                 :          0 :                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
    4450                 :            :                                    "bridge %s: %s",
    4451                 :          0 :                                    bss->brname, strerror(errno));
    4452                 :            :         }
    4453                 :            : 
    4454                 :        212 :         nl80211_remove_monitor_interface(drv);
    4455                 :            : 
    4456         [ +  + ]:        212 :         if (is_ap_interface(drv->nlmode))
    4457                 :        200 :                 wpa_driver_nl80211_del_beacon(drv);
    4458                 :            : 
    4459         [ +  + ]:        212 :         if (drv->eapol_sock >= 0) {
    4460                 :        190 :                 eloop_unregister_read_sock(drv->eapol_sock);
    4461                 :        190 :                 close(drv->eapol_sock);
    4462                 :            :         }
    4463                 :            : 
    4464         [ -  + ]:        212 :         if (drv->if_indices != drv->default_if_indices)
    4465                 :          0 :                 os_free(drv->if_indices);
    4466                 :            : 
    4467         [ +  + ]:        212 :         if (drv->disabled_11b_rates)
    4468                 :         10 :                 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
    4469                 :            : 
    4470                 :        212 :         netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
    4471                 :            :                                IF_OPER_UP);
    4472                 :        212 :         rfkill_deinit(drv->rfkill);
    4473                 :            : 
    4474                 :        212 :         eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
    4475                 :            : 
    4476         [ +  - ]:        212 :         if (!drv->start_iface_up)
    4477                 :        212 :                 (void) i802_set_iface_flags(bss, 0);
    4478         [ +  - ]:        212 :         if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
    4479 [ +  + ][ +  - ]:        212 :                 if (!drv->hostapd || !drv->start_mode_ap)
    4480                 :        212 :                         wpa_driver_nl80211_set_mode(bss,
    4481                 :            :                                                     NL80211_IFTYPE_STATION);
    4482                 :        212 :                 nl80211_mgmt_unsubscribe(bss, "deinit");
    4483                 :            :         } else {
    4484                 :          0 :                 nl80211_mgmt_unsubscribe(bss, "deinit");
    4485                 :          0 :                 nl80211_del_p2pdev(bss);
    4486                 :            :         }
    4487                 :        212 :         nl_cb_put(drv->nl_cb);
    4488                 :            : 
    4489                 :        212 :         nl80211_destroy_bss(drv->first_bss);
    4490                 :            : 
    4491                 :        212 :         os_free(drv->filter_ssids);
    4492                 :            : 
    4493                 :        212 :         os_free(drv->auth_ie);
    4494                 :            : 
    4495         [ +  - ]:        212 :         if (drv->in_interface_list)
    4496                 :        212 :                 dl_list_del(&drv->list);
    4497                 :            : 
    4498                 :        212 :         os_free(drv->extended_capa);
    4499                 :        212 :         os_free(drv->extended_capa_mask);
    4500                 :        212 :         os_free(drv->first_bss);
    4501                 :        212 :         os_free(drv);
    4502                 :        212 : }
    4503                 :            : 
    4504                 :            : 
    4505                 :            : /**
    4506                 :            :  * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
    4507                 :            :  * @eloop_ctx: Driver private data
    4508                 :            :  * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
    4509                 :            :  *
    4510                 :            :  * This function can be used as registered timeout when starting a scan to
    4511                 :            :  * generate a scan completed event if the driver does not report this.
    4512                 :            :  */
    4513                 :          0 : static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
    4514                 :            : {
    4515                 :          0 :         struct wpa_driver_nl80211_data *drv = eloop_ctx;
    4516         [ #  # ]:          0 :         if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
    4517                 :          0 :                 wpa_driver_nl80211_set_mode(drv->first_bss,
    4518                 :            :                                             drv->ap_scan_as_station);
    4519                 :          0 :                 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
    4520                 :            :         }
    4521                 :          0 :         wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
    4522                 :          0 :         wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
    4523                 :          0 : }
    4524                 :            : 
    4525                 :            : 
    4526                 :            : static struct nl_msg *
    4527                 :        905 : nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
    4528                 :            :                     struct wpa_driver_scan_params *params, u64 *wdev_id)
    4529                 :            : {
    4530                 :            :         struct nl_msg *msg;
    4531                 :            :         size_t i;
    4532                 :            : 
    4533                 :        905 :         msg = nlmsg_alloc();
    4534         [ -  + ]:        905 :         if (!msg)
    4535                 :          0 :                 return NULL;
    4536                 :            : 
    4537                 :        905 :         nl80211_cmd(drv, msg, 0, cmd);
    4538                 :            : 
    4539         [ +  - ]:        905 :         if (!wdev_id)
    4540         [ +  - ]:        905 :                 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    4541                 :            :         else
    4542         [ #  # ]:          0 :                 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
    4543                 :            : 
    4544         [ +  + ]:        905 :         if (params->num_ssids) {
    4545                 :            :                 struct nlattr *ssids;
    4546                 :            : 
    4547                 :        879 :                 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
    4548         [ -  + ]:        879 :                 if (ssids == NULL)
    4549                 :          0 :                         goto fail;
    4550         [ +  + ]:       1758 :                 for (i = 0; i < params->num_ssids; i++) {
    4551                 :        879 :                         wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
    4552                 :        879 :                                           params->ssids[i].ssid,
    4553                 :            :                                           params->ssids[i].ssid_len);
    4554         [ -  + ]:        879 :                         if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
    4555                 :        879 :                                     params->ssids[i].ssid) < 0)
    4556                 :          0 :                                 goto fail;
    4557                 :            :                 }
    4558                 :        879 :                 nla_nest_end(msg, ssids);
    4559                 :            :         }
    4560                 :            : 
    4561         [ +  + ]:        905 :         if (params->extra_ies) {
    4562                 :        880 :                 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
    4563                 :        880 :                             params->extra_ies, params->extra_ies_len);
    4564         [ -  + ]:        880 :                 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
    4565                 :        880 :                             params->extra_ies) < 0)
    4566                 :          0 :                         goto fail;
    4567                 :            :         }
    4568                 :            : 
    4569         [ +  + ]:        905 :         if (params->freqs) {
    4570                 :            :                 struct nlattr *freqs;
    4571                 :        761 :                 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
    4572         [ -  + ]:        761 :                 if (freqs == NULL)
    4573                 :          0 :                         goto fail;
    4574         [ +  + ]:       2589 :                 for (i = 0; params->freqs[i]; i++) {
    4575                 :       1828 :                         wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
    4576                 :       1828 :                                    "MHz", params->freqs[i]);
    4577         [ -  + ]:       1828 :                         if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
    4578                 :          0 :                                 goto fail;
    4579                 :            :                 }
    4580                 :        761 :                 nla_nest_end(msg, freqs);
    4581                 :            :         }
    4582                 :            : 
    4583                 :        905 :         os_free(drv->filter_ssids);
    4584                 :        905 :         drv->filter_ssids = params->filter_ssids;
    4585                 :        905 :         params->filter_ssids = NULL;
    4586                 :        905 :         drv->num_filter_ssids = params->num_filter_ssids;
    4587                 :            : 
    4588                 :        905 :         return msg;
    4589                 :            : 
    4590                 :            : fail:
    4591                 :            : nla_put_failure:
    4592                 :          0 :         nlmsg_free(msg);
    4593                 :        905 :         return NULL;
    4594                 :            : }
    4595                 :            : 
    4596                 :            : 
    4597                 :            : /**
    4598                 :            :  * wpa_driver_nl80211_scan - Request the driver to initiate scan
    4599                 :            :  * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
    4600                 :            :  * @params: Scan parameters
    4601                 :            :  * Returns: 0 on success, -1 on failure
    4602                 :            :  */
    4603                 :        905 : static int wpa_driver_nl80211_scan(struct i802_bss *bss,
    4604                 :            :                                    struct wpa_driver_scan_params *params)
    4605                 :            : {
    4606                 :        905 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4607                 :        905 :         int ret = -1, timeout;
    4608                 :        905 :         struct nl_msg *msg = NULL;
    4609                 :            : 
    4610                 :        905 :         wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
    4611                 :        905 :         drv->scan_for_auth = 0;
    4612                 :            : 
    4613         [ -  + ]:        905 :         msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
    4614                 :        905 :                                   bss->wdev_id_set ? &bss->wdev_id : NULL);
    4615         [ -  + ]:        905 :         if (!msg)
    4616                 :          0 :                 return -1;
    4617                 :            : 
    4618         [ +  + ]:        905 :         if (params->p2p_probe) {
    4619                 :            :                 struct nlattr *rates;
    4620                 :            : 
    4621                 :        511 :                 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
    4622                 :            : 
    4623                 :        511 :                 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
    4624         [ -  + ]:        511 :                 if (rates == NULL)
    4625                 :          0 :                         goto nla_put_failure;
    4626                 :            : 
    4627                 :            :                 /*
    4628                 :            :                  * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
    4629                 :            :                  * by masking out everything else apart from the OFDM rates 6,
    4630                 :            :                  * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
    4631                 :            :                  * rates are left enabled.
    4632                 :            :                  */
    4633         [ +  - ]:        511 :                 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
    4634                 :            :                         "\x0c\x12\x18\x24\x30\x48\x60\x6c");
    4635                 :        511 :                 nla_nest_end(msg, rates);
    4636                 :            : 
    4637         [ +  - ]:        511 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
    4638                 :            :         }
    4639                 :            : 
    4640                 :        905 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    4641                 :        905 :         msg = NULL;
    4642         [ +  + ]:        905 :         if (ret) {
    4643                 :          6 :                 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
    4644                 :            :                            "(%s)", ret, strerror(-ret));
    4645 [ #  # ][ -  + ]:          6 :                 if (drv->hostapd && is_ap_interface(drv->nlmode)) {
    4646                 :            :                         /*
    4647                 :            :                          * mac80211 does not allow scan requests in AP mode, so
    4648                 :            :                          * try to do this in station mode.
    4649                 :            :                          */
    4650         [ #  # ]:          0 :                         if (wpa_driver_nl80211_set_mode(
    4651                 :            :                                     bss, NL80211_IFTYPE_STATION))
    4652                 :          0 :                                 goto nla_put_failure;
    4653                 :            : 
    4654         [ #  # ]:          0 :                         if (wpa_driver_nl80211_scan(bss, params)) {
    4655                 :          0 :                                 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
    4656                 :          0 :                                 goto nla_put_failure;
    4657                 :            :                         }
    4658                 :            : 
    4659                 :            :                         /* Restore AP mode when processing scan results */
    4660                 :          0 :                         drv->ap_scan_as_station = drv->nlmode;
    4661                 :          0 :                         ret = 0;
    4662                 :            :                 } else
    4663                 :            :                         goto nla_put_failure;
    4664                 :            :         }
    4665                 :            : 
    4666                 :        899 :         drv->scan_state = SCAN_REQUESTED;
    4667                 :            :         /* Not all drivers generate "scan completed" wireless event, so try to
    4668                 :            :          * read results after a timeout. */
    4669                 :        899 :         timeout = 10;
    4670         [ +  + ]:        899 :         if (drv->scan_complete_events) {
    4671                 :            :                 /*
    4672                 :            :                  * The driver seems to deliver events to notify when scan is
    4673                 :            :                  * complete, so use longer timeout to avoid race conditions
    4674                 :            :                  * with scanning and following association request.
    4675                 :            :                  */
    4676                 :        875 :                 timeout = 30;
    4677                 :            :         }
    4678                 :        899 :         wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
    4679                 :            :                    "seconds", ret, timeout);
    4680                 :        899 :         eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
    4681                 :        899 :         eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
    4682                 :            :                                drv, drv->ctx);
    4683                 :            : 
    4684                 :            : nla_put_failure:
    4685                 :        905 :         nlmsg_free(msg);
    4686                 :        905 :         return ret;
    4687                 :            : }
    4688                 :            : 
    4689                 :            : 
    4690                 :            : /**
    4691                 :            :  * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
    4692                 :            :  * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
    4693                 :            :  * @params: Scan parameters
    4694                 :            :  * @interval: Interval between scan cycles in milliseconds
    4695                 :            :  * Returns: 0 on success, -1 on failure or if not supported
    4696                 :            :  */
    4697                 :          0 : static int wpa_driver_nl80211_sched_scan(void *priv,
    4698                 :            :                                          struct wpa_driver_scan_params *params,
    4699                 :            :                                          u32 interval)
    4700                 :            : {
    4701                 :          0 :         struct i802_bss *bss = priv;
    4702                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4703                 :          0 :         int ret = -1;
    4704                 :            :         struct nl_msg *msg;
    4705                 :            :         size_t i;
    4706                 :            : 
    4707                 :          0 :         wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
    4708                 :            : 
    4709                 :            : #ifdef ANDROID
    4710                 :            :         if (!drv->capa.sched_scan_supported)
    4711                 :            :                 return android_pno_start(bss, params);
    4712                 :            : #endif /* ANDROID */
    4713                 :            : 
    4714         [ #  # ]:          0 :         msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
    4715                 :          0 :                                   bss->wdev_id_set ? &bss->wdev_id : NULL);
    4716         [ #  # ]:          0 :         if (!msg)
    4717                 :          0 :                 goto nla_put_failure;
    4718                 :            : 
    4719         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
    4720                 :            : 
    4721 [ #  # ][ #  # ]:          0 :         if ((drv->num_filter_ssids &&
    4722         [ #  # ]:          0 :             (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
    4723                 :          0 :             params->filter_rssi) {
    4724                 :            :                 struct nlattr *match_sets;
    4725                 :          0 :                 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
    4726         [ #  # ]:          0 :                 if (match_sets == NULL)
    4727                 :          0 :                         goto nla_put_failure;
    4728                 :            : 
    4729         [ #  # ]:          0 :                 for (i = 0; i < drv->num_filter_ssids; i++) {
    4730                 :            :                         struct nlattr *match_set_ssid;
    4731                 :          0 :                         wpa_hexdump_ascii(MSG_MSGDUMP,
    4732                 :            :                                           "nl80211: Sched scan filter SSID",
    4733                 :          0 :                                           drv->filter_ssids[i].ssid,
    4734                 :          0 :                                           drv->filter_ssids[i].ssid_len);
    4735                 :            : 
    4736                 :          0 :                         match_set_ssid = nla_nest_start(msg, i + 1);
    4737         [ #  # ]:          0 :                         if (match_set_ssid == NULL)
    4738                 :          0 :                                 goto nla_put_failure;
    4739         [ #  # ]:          0 :                         NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
    4740                 :            :                                 drv->filter_ssids[i].ssid_len,
    4741                 :            :                                 drv->filter_ssids[i].ssid);
    4742                 :            : 
    4743                 :          0 :                         nla_nest_end(msg, match_set_ssid);
    4744                 :            :                 }
    4745                 :            : 
    4746         [ #  # ]:          0 :                 if (params->filter_rssi) {
    4747                 :            :                         struct nlattr *match_set_rssi;
    4748                 :          0 :                         match_set_rssi = nla_nest_start(msg, 0);
    4749         [ #  # ]:          0 :                         if (match_set_rssi == NULL)
    4750                 :          0 :                                 goto nla_put_failure;
    4751         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
    4752                 :            :                                     params->filter_rssi);
    4753                 :          0 :                         wpa_printf(MSG_MSGDUMP,
    4754                 :            :                                    "nl80211: Sched scan RSSI filter %d dBm",
    4755                 :            :                                    params->filter_rssi);
    4756                 :          0 :                         nla_nest_end(msg, match_set_rssi);
    4757                 :            :                 }
    4758                 :            : 
    4759                 :          0 :                 nla_nest_end(msg, match_sets);
    4760                 :            :         }
    4761                 :            : 
    4762                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    4763                 :            : 
    4764                 :            :         /* TODO: if we get an error here, we should fall back to normal scan */
    4765                 :            : 
    4766                 :          0 :         msg = NULL;
    4767         [ #  # ]:          0 :         if (ret) {
    4768                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
    4769                 :            :                            "ret=%d (%s)", ret, strerror(-ret));
    4770                 :          0 :                 goto nla_put_failure;
    4771                 :            :         }
    4772                 :            : 
    4773                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
    4774                 :            :                    "scan interval %d msec", ret, interval);
    4775                 :            : 
    4776                 :            : nla_put_failure:
    4777                 :          0 :         nlmsg_free(msg);
    4778                 :          0 :         return ret;
    4779                 :            : }
    4780                 :            : 
    4781                 :            : 
    4782                 :            : /**
    4783                 :            :  * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
    4784                 :            :  * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
    4785                 :            :  * Returns: 0 on success, -1 on failure or if not supported
    4786                 :            :  */
    4787                 :          0 : static int wpa_driver_nl80211_stop_sched_scan(void *priv)
    4788                 :            : {
    4789                 :          0 :         struct i802_bss *bss = priv;
    4790                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4791                 :          0 :         int ret = 0;
    4792                 :            :         struct nl_msg *msg;
    4793                 :            : 
    4794                 :            : #ifdef ANDROID
    4795                 :            :         if (!drv->capa.sched_scan_supported)
    4796                 :            :                 return android_pno_stop(bss);
    4797                 :            : #endif /* ANDROID */
    4798                 :            : 
    4799                 :          0 :         msg = nlmsg_alloc();
    4800         [ #  # ]:          0 :         if (!msg)
    4801                 :          0 :                 return -1;
    4802                 :            : 
    4803                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
    4804                 :            : 
    4805         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    4806                 :            : 
    4807                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    4808                 :          0 :         msg = NULL;
    4809         [ #  # ]:          0 :         if (ret) {
    4810                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
    4811                 :            :                            "ret=%d (%s)", ret, strerror(-ret));
    4812                 :          0 :                 goto nla_put_failure;
    4813                 :            :         }
    4814                 :            : 
    4815                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
    4816                 :            : 
    4817                 :            : nla_put_failure:
    4818                 :          0 :         nlmsg_free(msg);
    4819                 :          0 :         return ret;
    4820                 :            : }
    4821                 :            : 
    4822                 :            : 
    4823                 :       6404 : static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
    4824                 :            : {
    4825                 :            :         const u8 *end, *pos;
    4826                 :            : 
    4827         [ -  + ]:       6404 :         if (ies == NULL)
    4828                 :          0 :                 return NULL;
    4829                 :            : 
    4830                 :       6404 :         pos = ies;
    4831                 :       6404 :         end = ies + ies_len;
    4832                 :            : 
    4833         [ +  - ]:       6404 :         while (pos + 1 < end) {
    4834         [ -  + ]:       6404 :                 if (pos + 2 + pos[1] > end)
    4835                 :          0 :                         break;
    4836         [ +  - ]:       6404 :                 if (pos[0] == ie)
    4837                 :       6404 :                         return pos;
    4838                 :          0 :                 pos += 2 + pos[1];
    4839                 :            :         }
    4840                 :            : 
    4841                 :       6404 :         return NULL;
    4842                 :            : }
    4843                 :            : 
    4844                 :            : 
    4845                 :       3396 : static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
    4846                 :            :                                  const u8 *ie, size_t ie_len)
    4847                 :            : {
    4848                 :            :         const u8 *ssid;
    4849                 :            :         size_t i;
    4850                 :            : 
    4851         [ +  - ]:       3396 :         if (drv->filter_ssids == NULL)
    4852                 :       3396 :                 return 0;
    4853                 :            : 
    4854                 :          0 :         ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
    4855         [ #  # ]:          0 :         if (ssid == NULL)
    4856                 :          0 :                 return 1;
    4857                 :            : 
    4858         [ #  # ]:          0 :         for (i = 0; i < drv->num_filter_ssids; i++) {
    4859 [ #  # ][ #  # ]:          0 :                 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
    4860                 :          0 :                     os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
    4861                 :            :                     0)
    4862                 :          0 :                         return 0;
    4863                 :            :         }
    4864                 :            : 
    4865                 :       3396 :         return 1;
    4866                 :            : }
    4867                 :            : 
    4868                 :            : 
    4869                 :       3396 : static int bss_info_handler(struct nl_msg *msg, void *arg)
    4870                 :            : {
    4871                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    4872                 :       3396 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    4873                 :            :         struct nlattr *bss[NL80211_BSS_MAX + 1];
    4874                 :            :         static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
    4875                 :            :                 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
    4876                 :            :                 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
    4877                 :            :                 [NL80211_BSS_TSF] = { .type = NLA_U64 },
    4878                 :            :                 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
    4879                 :            :                 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
    4880                 :            :                 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
    4881                 :            :                 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
    4882                 :            :                 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
    4883                 :            :                 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
    4884                 :            :                 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
    4885                 :            :                 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
    4886                 :            :         };
    4887                 :       3396 :         struct nl80211_bss_info_arg *_arg = arg;
    4888                 :       3396 :         struct wpa_scan_results *res = _arg->res;
    4889                 :            :         struct wpa_scan_res **tmp;
    4890                 :            :         struct wpa_scan_res *r;
    4891                 :            :         const u8 *ie, *beacon_ie;
    4892                 :            :         size_t ie_len, beacon_ie_len;
    4893                 :            :         u8 *pos;
    4894                 :            :         size_t i;
    4895                 :            : 
    4896                 :       3396 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    4897                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    4898         [ -  + ]:       3396 :         if (!tb[NL80211_ATTR_BSS])
    4899                 :          0 :                 return NL_SKIP;
    4900         [ -  + ]:       3396 :         if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
    4901                 :            :                              bss_policy))
    4902                 :          0 :                 return NL_SKIP;
    4903         [ +  + ]:       3396 :         if (bss[NL80211_BSS_STATUS]) {
    4904                 :            :                 enum nl80211_bss_status status;
    4905                 :         23 :                 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
    4906 [ +  - ][ +  + ]:         23 :                 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
    4907                 :         19 :                     bss[NL80211_BSS_FREQUENCY]) {
    4908                 :         19 :                         _arg->assoc_freq =
    4909                 :         19 :                                 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
    4910                 :         19 :                         wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
    4911                 :            :                                    _arg->assoc_freq);
    4912                 :            :                 }
    4913 [ +  + ][ +  - ]:         23 :                 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
    4914                 :         19 :                     bss[NL80211_BSS_BSSID]) {
    4915                 :         19 :                         os_memcpy(_arg->assoc_bssid,
    4916                 :            :                                   nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
    4917                 :         19 :                         wpa_printf(MSG_DEBUG, "nl80211: Associated with "
    4918                 :        114 :                                    MACSTR, MAC2STR(_arg->assoc_bssid));
    4919                 :            :                 }
    4920                 :            :         }
    4921         [ -  + ]:       3396 :         if (!res)
    4922                 :          0 :                 return NL_SKIP;
    4923         [ +  - ]:       3396 :         if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
    4924                 :       3396 :                 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
    4925                 :       3396 :                 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
    4926                 :            :         } else {
    4927                 :          0 :                 ie = NULL;
    4928                 :          0 :                 ie_len = 0;
    4929                 :            :         }
    4930         [ +  + ]:       3396 :         if (bss[NL80211_BSS_BEACON_IES]) {
    4931                 :       2590 :                 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
    4932                 :       2590 :                 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
    4933                 :            :         } else {
    4934                 :        806 :                 beacon_ie = NULL;
    4935                 :        806 :                 beacon_ie_len = 0;
    4936                 :            :         }
    4937                 :            : 
    4938 [ +  - ][ +  - ]:       3396 :         if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
                 [ -  + ]
    4939                 :            :                                   ie ? ie_len : beacon_ie_len))
    4940                 :          0 :                 return NL_SKIP;
    4941                 :            : 
    4942                 :       3396 :         r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
    4943         [ -  + ]:       3396 :         if (r == NULL)
    4944                 :          0 :                 return NL_SKIP;
    4945         [ +  - ]:       3396 :         if (bss[NL80211_BSS_BSSID])
    4946                 :       3396 :                 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
    4947                 :            :                           ETH_ALEN);
    4948         [ +  - ]:       3396 :         if (bss[NL80211_BSS_FREQUENCY])
    4949                 :       3396 :                 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
    4950         [ +  - ]:       3396 :         if (bss[NL80211_BSS_BEACON_INTERVAL])
    4951                 :       3396 :                 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
    4952         [ +  - ]:       3396 :         if (bss[NL80211_BSS_CAPABILITY])
    4953                 :       3396 :                 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
    4954                 :       3396 :         r->flags |= WPA_SCAN_NOISE_INVALID;
    4955         [ +  - ]:       3396 :         if (bss[NL80211_BSS_SIGNAL_MBM]) {
    4956                 :       3396 :                 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
    4957                 :       3396 :                 r->level /= 100; /* mBm to dBm */
    4958                 :       3396 :                 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
    4959         [ #  # ]:          0 :         } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
    4960                 :          0 :                 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
    4961                 :          0 :                 r->flags |= WPA_SCAN_QUAL_INVALID;
    4962                 :            :         } else
    4963                 :          0 :                 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
    4964         [ +  - ]:       3396 :         if (bss[NL80211_BSS_TSF])
    4965                 :       3396 :                 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
    4966         [ +  - ]:       3396 :         if (bss[NL80211_BSS_SEEN_MS_AGO])
    4967                 :       3396 :                 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
    4968                 :       3396 :         r->ie_len = ie_len;
    4969                 :       3396 :         pos = (u8 *) (r + 1);
    4970         [ +  - ]:       3396 :         if (ie) {
    4971                 :       3396 :                 os_memcpy(pos, ie, ie_len);
    4972                 :       3396 :                 pos += ie_len;
    4973                 :            :         }
    4974                 :       3396 :         r->beacon_ie_len = beacon_ie_len;
    4975         [ +  + ]:       3396 :         if (beacon_ie)
    4976                 :       2590 :                 os_memcpy(pos, beacon_ie, beacon_ie_len);
    4977                 :            : 
    4978         [ +  + ]:       3396 :         if (bss[NL80211_BSS_STATUS]) {
    4979                 :            :                 enum nl80211_bss_status status;
    4980                 :         23 :                 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
    4981      [ -  +  + ]:         23 :                 switch (status) {
    4982                 :            :                 case NL80211_BSS_STATUS_AUTHENTICATED:
    4983                 :          0 :                         r->flags |= WPA_SCAN_AUTHENTICATED;
    4984                 :          0 :                         break;
    4985                 :            :                 case NL80211_BSS_STATUS_ASSOCIATED:
    4986                 :         19 :                         r->flags |= WPA_SCAN_ASSOCIATED;
    4987                 :         19 :                         break;
    4988                 :            :                 default:
    4989                 :          4 :                         break;
    4990                 :            :                 }
    4991                 :            :         }
    4992                 :            : 
    4993                 :            :         /*
    4994                 :            :          * cfg80211 maintains separate BSS table entries for APs if the same
    4995                 :            :          * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
    4996                 :            :          * not use frequency as a separate key in the BSS table, so filter out
    4997                 :            :          * duplicated entries. Prefer associated BSS entry in such a case in
    4998                 :            :          * order to get the correct frequency into the BSS table.
    4999                 :            :          */
    5000         [ +  + ]:       9982 :         for (i = 0; i < res->num; i++) {
    5001                 :            :                 const u8 *s1, *s2;
    5002         [ +  + ]:       6586 :                 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
    5003                 :       3384 :                         continue;
    5004                 :            : 
    5005                 :       3202 :                 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
    5006                 :       3202 :                                     res->res[i]->ie_len, WLAN_EID_SSID);
    5007                 :       3202 :                 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
    5008 [ +  - ][ +  + ]:       3202 :                 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
         [ +  - ][ +  - ]
    5009                 :        896 :                     os_memcmp(s1, s2, 2 + s1[1]) != 0)
    5010                 :       3202 :                         continue;
    5011                 :            : 
    5012                 :            :                 /* Same BSSID,SSID was already included in scan results */
    5013                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
    5014                 :          0 :                            "for " MACSTR, MAC2STR(r->bssid));
    5015                 :            : 
    5016 [ #  # ][ #  # ]:          0 :                 if ((r->flags & WPA_SCAN_ASSOCIATED) &&
    5017                 :          0 :                     !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
    5018                 :          0 :                         os_free(res->res[i]);
    5019                 :          0 :                         res->res[i] = r;
    5020                 :            :                 } else
    5021                 :          0 :                         os_free(r);
    5022                 :          0 :                 return NL_SKIP;
    5023                 :            :         }
    5024                 :            : 
    5025                 :       3396 :         tmp = os_realloc_array(res->res, res->num + 1,
    5026                 :            :                                sizeof(struct wpa_scan_res *));
    5027         [ -  + ]:       3396 :         if (tmp == NULL) {
    5028                 :          0 :                 os_free(r);
    5029                 :          0 :                 return NL_SKIP;
    5030                 :            :         }
    5031                 :       3396 :         tmp[res->num++] = r;
    5032                 :       3396 :         res->res = tmp;
    5033                 :            : 
    5034                 :       3396 :         return NL_SKIP;
    5035                 :            : }
    5036                 :            : 
    5037                 :            : 
    5038                 :          0 : static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
    5039                 :            :                                  const u8 *addr)
    5040                 :            : {
    5041         [ #  # ]:          0 :         if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
    5042                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
    5043                 :          0 :                            "mismatch (" MACSTR ")", MAC2STR(addr));
    5044                 :          0 :                 wpa_driver_nl80211_mlme(drv, addr,
    5045                 :            :                                         NL80211_CMD_DEAUTHENTICATE,
    5046                 :            :                                         WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
    5047                 :            :         }
    5048                 :          0 : }
    5049                 :            : 
    5050                 :            : 
    5051                 :        882 : static void wpa_driver_nl80211_check_bss_status(
    5052                 :            :         struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
    5053                 :            : {
    5054                 :            :         size_t i;
    5055                 :            : 
    5056         [ +  + ]:       4278 :         for (i = 0; i < res->num; i++) {
    5057                 :       3396 :                 struct wpa_scan_res *r = res->res[i];
    5058         [ -  + ]:       3396 :                 if (r->flags & WPA_SCAN_AUTHENTICATED) {
    5059                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Scan results "
    5060                 :            :                                    "indicates BSS status with " MACSTR
    5061                 :            :                                    " as authenticated",
    5062                 :          0 :                                    MAC2STR(r->bssid));
    5063 [ #  # ][ #  # ]:          0 :                         if (is_sta_interface(drv->nlmode) &&
    5064         [ #  # ]:          0 :                             os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
    5065                 :          0 :                             os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
    5066                 :            :                             0) {
    5067                 :          0 :                                 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
    5068                 :            :                                            " in local state (auth=" MACSTR
    5069                 :            :                                            " assoc=" MACSTR ")",
    5070                 :          0 :                                            MAC2STR(drv->auth_bssid),
    5071                 :          0 :                                            MAC2STR(drv->bssid));
    5072                 :          0 :                                 clear_state_mismatch(drv, r->bssid);
    5073                 :            :                         }
    5074                 :            :                 }
    5075                 :            : 
    5076         [ +  + ]:       3396 :                 if (r->flags & WPA_SCAN_ASSOCIATED) {
    5077                 :         19 :                         wpa_printf(MSG_DEBUG, "nl80211: Scan results "
    5078                 :            :                                    "indicate BSS status with " MACSTR
    5079                 :            :                                    " as associated",
    5080                 :        114 :                                    MAC2STR(r->bssid));
    5081 [ -  + ][ +  - ]:         19 :                         if (is_sta_interface(drv->nlmode) &&
    5082                 :         19 :                             !drv->associated) {
    5083                 :          0 :                                 wpa_printf(MSG_DEBUG, "nl80211: Local state "
    5084                 :            :                                            "(not associated) does not match "
    5085                 :            :                                            "with BSS state");
    5086                 :          0 :                                 clear_state_mismatch(drv, r->bssid);
    5087 [ +  - ][ -  + ]:         19 :                         } else if (is_sta_interface(drv->nlmode) &&
    5088                 :         19 :                                    os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
    5089                 :            :                                    0) {
    5090                 :          0 :                                 wpa_printf(MSG_DEBUG, "nl80211: Local state "
    5091                 :            :                                            "(associated with " MACSTR ") does "
    5092                 :            :                                            "not match with BSS state",
    5093                 :          0 :                                            MAC2STR(drv->bssid));
    5094                 :          0 :                                 clear_state_mismatch(drv, r->bssid);
    5095                 :          0 :                                 clear_state_mismatch(drv, drv->bssid);
    5096                 :            :                         }
    5097                 :            :                 }
    5098                 :            :         }
    5099                 :        882 : }
    5100                 :            : 
    5101                 :            : 
    5102                 :            : static struct wpa_scan_results *
    5103                 :        882 : nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
    5104                 :            : {
    5105                 :            :         struct nl_msg *msg;
    5106                 :            :         struct wpa_scan_results *res;
    5107                 :            :         int ret;
    5108                 :            :         struct nl80211_bss_info_arg arg;
    5109                 :            : 
    5110                 :        882 :         res = os_zalloc(sizeof(*res));
    5111         [ -  + ]:        882 :         if (res == NULL)
    5112                 :          0 :                 return NULL;
    5113                 :        882 :         msg = nlmsg_alloc();
    5114         [ -  + ]:        882 :         if (!msg)
    5115                 :          0 :                 goto nla_put_failure;
    5116                 :            : 
    5117                 :        882 :         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
    5118         [ -  + ]:        882 :         if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
    5119                 :          0 :                 goto nla_put_failure;
    5120                 :            : 
    5121                 :        882 :         arg.drv = drv;
    5122                 :        882 :         arg.res = res;
    5123                 :        882 :         ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
    5124                 :        882 :         msg = NULL;
    5125         [ +  - ]:        882 :         if (ret == 0) {
    5126                 :        882 :                 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
    5127                 :            :                            "BSSes)", (unsigned long) res->num);
    5128                 :        882 :                 nl80211_get_noise_for_scan_results(drv, res);
    5129                 :        882 :                 return res;
    5130                 :            :         }
    5131                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
    5132                 :            :                    "(%s)", ret, strerror(-ret));
    5133                 :            : nla_put_failure:
    5134                 :          0 :         nlmsg_free(msg);
    5135                 :          0 :         wpa_scan_results_free(res);
    5136                 :        882 :         return NULL;
    5137                 :            : }
    5138                 :            : 
    5139                 :            : 
    5140                 :            : /**
    5141                 :            :  * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
    5142                 :            :  * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
    5143                 :            :  * Returns: Scan results on success, -1 on failure
    5144                 :            :  */
    5145                 :            : static struct wpa_scan_results *
    5146                 :        882 : wpa_driver_nl80211_get_scan_results(void *priv)
    5147                 :            : {
    5148                 :        882 :         struct i802_bss *bss = priv;
    5149                 :        882 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    5150                 :            :         struct wpa_scan_results *res;
    5151                 :            : 
    5152                 :        882 :         res = nl80211_get_scan_results(drv);
    5153         [ +  - ]:        882 :         if (res)
    5154                 :        882 :                 wpa_driver_nl80211_check_bss_status(drv, res);
    5155                 :        882 :         return res;
    5156                 :            : }
    5157                 :            : 
    5158                 :            : 
    5159                 :          0 : static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
    5160                 :            : {
    5161                 :            :         struct wpa_scan_results *res;
    5162                 :            :         size_t i;
    5163                 :            : 
    5164                 :          0 :         res = nl80211_get_scan_results(drv);
    5165         [ #  # ]:          0 :         if (res == NULL) {
    5166                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
    5167                 :          0 :                 return;
    5168                 :            :         }
    5169                 :            : 
    5170                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
    5171         [ #  # ]:          0 :         for (i = 0; i < res->num; i++) {
    5172                 :          0 :                 struct wpa_scan_res *r = res->res[i];
    5173 [ #  # ][ #  # ]:          0 :                 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
    5174                 :          0 :                            (int) i, (int) res->num, MAC2STR(r->bssid),
    5175                 :          0 :                            r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
    5176                 :          0 :                            r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
    5177                 :            :         }
    5178                 :            : 
    5179                 :          0 :         wpa_scan_results_free(res);
    5180                 :            : }
    5181                 :            : 
    5182                 :            : 
    5183                 :      10121 : static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
    5184                 :            :                                       enum wpa_alg alg, const u8 *addr,
    5185                 :            :                                       int key_idx, int set_tx,
    5186                 :            :                                       const u8 *seq, size_t seq_len,
    5187                 :            :                                       const u8 *key, size_t key_len)
    5188                 :            : {
    5189                 :      10121 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    5190                 :            :         int ifindex;
    5191                 :            :         struct nl_msg *msg;
    5192                 :            :         int ret;
    5193                 :      10121 :         int tdls = 0;
    5194                 :            : 
    5195                 :            :         /* Ignore for P2P Device */
    5196         [ -  + ]:      10121 :         if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
    5197                 :          0 :                 return 0;
    5198                 :            : 
    5199                 :      10121 :         ifindex = if_nametoindex(ifname);
    5200                 :      10121 :         wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
    5201                 :            :                    "set_tx=%d seq_len=%lu key_len=%lu",
    5202                 :            :                    __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
    5203                 :            :                    (unsigned long) seq_len, (unsigned long) key_len);
    5204                 :            : #ifdef CONFIG_TDLS
    5205         [ +  + ]:       6640 :         if (key_idx == -1) {
    5206                 :         68 :                 key_idx = 0;
    5207                 :         68 :                 tdls = 1;
    5208                 :            :         }
    5209                 :            : #endif /* CONFIG_TDLS */
    5210                 :            : 
    5211                 :      10121 :         msg = nlmsg_alloc();
    5212         [ -  + ]:      10121 :         if (!msg)
    5213                 :          0 :                 return -ENOMEM;
    5214                 :            : 
    5215         [ +  + ]:      10121 :         if (alg == WPA_ALG_NONE) {
    5216                 :       8705 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
    5217                 :            :         } else {
    5218                 :       1416 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
    5219         [ +  - ]:       1416 :                 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
    5220   [ +  +  +  +  :       1416 :                 switch (alg) {
          +  +  +  -  -  
             -  -  -  + ]
    5221                 :            :                 case WPA_ALG_WEP:
    5222         [ +  - ]:          5 :                         if (key_len == 5)
    5223         [ +  - ]:          5 :                                 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
    5224                 :            :                                             WLAN_CIPHER_SUITE_WEP40);
    5225                 :            :                         else
    5226         [ #  # ]:          0 :                                 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
    5227                 :            :                                             WLAN_CIPHER_SUITE_WEP104);
    5228                 :          5 :                         break;
    5229                 :            :                 case WPA_ALG_TKIP:
    5230         [ +  - ]:         73 :                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
    5231                 :            :                                     WLAN_CIPHER_SUITE_TKIP);
    5232                 :         73 :                         break;
    5233                 :            :                 case WPA_ALG_CCMP:
    5234         [ +  - ]:       1219 :                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
    5235                 :            :                                     WLAN_CIPHER_SUITE_CCMP);
    5236                 :       1219 :                         break;
    5237                 :            :                 case WPA_ALG_GCMP:
    5238         [ +  - ]:          5 :                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
    5239                 :            :                                     WLAN_CIPHER_SUITE_GCMP);
    5240                 :          5 :                         break;
    5241                 :            :                 case WPA_ALG_CCMP_256:
    5242         [ +  - ]:          5 :                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
    5243                 :            :                                     WLAN_CIPHER_SUITE_CCMP_256);
    5244                 :          5 :                         break;
    5245                 :            :                 case WPA_ALG_GCMP_256:
    5246         [ +  - ]:          5 :                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
    5247                 :            :                                     WLAN_CIPHER_SUITE_GCMP_256);
    5248                 :          5 :                         break;
    5249                 :            :                 case WPA_ALG_IGTK:
    5250         [ +  - ]:        102 :                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
    5251                 :            :                                     WLAN_CIPHER_SUITE_AES_CMAC);
    5252                 :        102 :                         break;
    5253                 :            :                 case WPA_ALG_BIP_GMAC_128:
    5254         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
    5255                 :            :                                     WLAN_CIPHER_SUITE_BIP_GMAC_128);
    5256                 :          0 :                         break;
    5257                 :            :                 case WPA_ALG_BIP_GMAC_256:
    5258         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
    5259                 :            :                                     WLAN_CIPHER_SUITE_BIP_GMAC_256);
    5260                 :          0 :                         break;
    5261                 :            :                 case WPA_ALG_BIP_CMAC_256:
    5262         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
    5263                 :            :                                     WLAN_CIPHER_SUITE_BIP_CMAC_256);
    5264                 :          0 :                         break;
    5265                 :            :                 case WPA_ALG_SMS4:
    5266         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
    5267                 :            :                                     WLAN_CIPHER_SUITE_SMS4);
    5268                 :          0 :                         break;
    5269                 :            :                 case WPA_ALG_KRK:
    5270         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
    5271                 :            :                                     WLAN_CIPHER_SUITE_KRK);
    5272                 :          0 :                         break;
    5273                 :            :                 default:
    5274                 :          2 :                         wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
    5275                 :            :                                    "algorithm %d", __func__, alg);
    5276                 :          2 :                         nlmsg_free(msg);
    5277                 :          2 :                         return -1;
    5278                 :            :                 }
    5279                 :            :         }
    5280                 :            : 
    5281 [ +  + ][ +  - ]:      10119 :         if (seq && seq_len)
    5282         [ +  - ]:        660 :                 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
    5283                 :            : 
    5284 [ +  + ][ +  + ]:      10119 :         if (addr && !is_broadcast_ether_addr(addr)) {
    5285                 :       2665 :                 wpa_printf(MSG_DEBUG, "   addr=" MACSTR, MAC2STR(addr));
    5286         [ +  - ]:       2665 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    5287                 :            : 
    5288 [ +  - ][ +  + ]:       2671 :                 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
                 [ +  - ]
    5289                 :          6 :                         wpa_printf(MSG_DEBUG, "   RSN IBSS RX GTK");
    5290         [ +  - ]:          6 :                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
    5291                 :            :                                     NL80211_KEYTYPE_GROUP);
    5292                 :            :                 }
    5293 [ +  + ][ +  - ]:       7454 :         } else if (addr && is_broadcast_ether_addr(addr)) {
    5294                 :            :                 struct nlattr *types;
    5295                 :            : 
    5296                 :        762 :                 wpa_printf(MSG_DEBUG, "   broadcast key");
    5297                 :            : 
    5298                 :        762 :                 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
    5299         [ -  + ]:        762 :                 if (!types)
    5300                 :          0 :                         goto nla_put_failure;
    5301         [ +  - ]:        762 :                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
    5302                 :        762 :                 nla_nest_end(msg, types);
    5303                 :            :         }
    5304         [ +  - ]:      10119 :         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
    5305         [ +  - ]:      10119 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
    5306                 :            : 
    5307                 :      10119 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    5308 [ +  + ][ +  + ]:      10119 :         if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
                 [ +  + ]
    5309                 :       8187 :                 ret = 0;
    5310         [ +  + ]:      10119 :         if (ret)
    5311                 :         74 :                 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
    5312                 :            :                            ret, strerror(-ret));
    5313                 :            : 
    5314                 :            :         /*
    5315                 :            :          * If we failed or don't need to set the default TX key (below),
    5316                 :            :          * we're done here.
    5317                 :            :          */
    5318 [ +  + ][ +  + ]:      10119 :         if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
         [ +  + ][ +  + ]
    5319                 :       9091 :                 return ret;
    5320         [ +  + ]:       1775 :         if (is_ap_interface(drv->nlmode) && addr &&
           [ +  +  +  + ]
    5321                 :        747 :             !is_broadcast_ether_addr(addr))
    5322                 :        271 :                 return ret;
    5323                 :            : 
    5324                 :        757 :         msg = nlmsg_alloc();
    5325         [ -  + ]:        757 :         if (!msg)
    5326                 :          0 :                 return -ENOMEM;
    5327                 :            : 
    5328                 :        757 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
    5329         [ +  - ]:        757 :         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
    5330         [ +  - ]:        757 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
    5331         [ +  + ]:        757 :         if (alg == WPA_ALG_IGTK)
    5332         [ +  - ]:         86 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
    5333                 :            :         else
    5334         [ +  - ]:        671 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
    5335 [ +  + ][ +  + ]:       1239 :         if (addr && is_broadcast_ether_addr(addr)) {
    5336                 :            :                 struct nlattr *types;
    5337                 :            : 
    5338                 :        482 :                 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
    5339         [ -  + ]:        482 :                 if (!types)
    5340                 :          0 :                         goto nla_put_failure;
    5341         [ +  - ]:        482 :                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
    5342                 :        482 :                 nla_nest_end(msg, types);
    5343         [ +  + ]:        275 :         } else if (addr) {
    5344                 :            :                 struct nlattr *types;
    5345                 :            : 
    5346                 :        269 :                 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
    5347         [ -  + ]:        269 :                 if (!types)
    5348                 :          0 :                         goto nla_put_failure;
    5349         [ +  - ]:        269 :                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
    5350                 :        269 :                 nla_nest_end(msg, types);
    5351                 :            :         }
    5352                 :            : 
    5353                 :        757 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    5354         [ -  + ]:        757 :         if (ret == -ENOENT)
    5355                 :          0 :                 ret = 0;
    5356         [ -  + ]:        757 :         if (ret)
    5357                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
    5358                 :            :                            "err=%d %s)", ret, strerror(-ret));
    5359                 :        757 :         return ret;
    5360                 :            : 
    5361                 :            : nla_put_failure:
    5362                 :          0 :         nlmsg_free(msg);
    5363                 :      10121 :         return -ENOBUFS;
    5364                 :            : }
    5365                 :            : 
    5366                 :            : 
    5367                 :          2 : static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
    5368                 :            :                       int key_idx, int defkey,
    5369                 :            :                       const u8 *seq, size_t seq_len,
    5370                 :            :                       const u8 *key, size_t key_len)
    5371                 :            : {
    5372                 :          2 :         struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
    5373         [ -  + ]:          2 :         if (!key_attr)
    5374                 :          0 :                 return -1;
    5375                 :            : 
    5376 [ +  - ][ -  + ]:          2 :         if (defkey && alg == WPA_ALG_IGTK)
    5377         [ #  # ]:          0 :                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
    5378         [ +  - ]:          2 :         else if (defkey)
    5379         [ +  - ]:          2 :                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
    5380                 :            : 
    5381         [ +  - ]:          2 :         NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
    5382                 :            : 
    5383   [ +  -  -  -  :          2 :         switch (alg) {
          -  -  -  -  -  
                   -  - ]
    5384                 :            :         case WPA_ALG_WEP:
    5385         [ +  - ]:          2 :                 if (key_len == 5)
    5386         [ +  - ]:          2 :                         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
    5387                 :            :                                     WLAN_CIPHER_SUITE_WEP40);
    5388                 :            :                 else
    5389         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
    5390                 :            :                                     WLAN_CIPHER_SUITE_WEP104);
    5391                 :          2 :                 break;
    5392                 :            :         case WPA_ALG_TKIP:
    5393         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
    5394                 :          0 :                 break;
    5395                 :            :         case WPA_ALG_CCMP:
    5396         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
    5397                 :          0 :                 break;
    5398                 :            :         case WPA_ALG_GCMP:
    5399         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_GCMP);
    5400                 :          0 :                 break;
    5401                 :            :         case WPA_ALG_CCMP_256:
    5402         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
    5403                 :            :                             WLAN_CIPHER_SUITE_CCMP_256);
    5404                 :          0 :                 break;
    5405                 :            :         case WPA_ALG_GCMP_256:
    5406         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
    5407                 :            :                             WLAN_CIPHER_SUITE_GCMP_256);
    5408                 :          0 :                 break;
    5409                 :            :         case WPA_ALG_IGTK:
    5410         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
    5411                 :            :                             WLAN_CIPHER_SUITE_AES_CMAC);
    5412                 :          0 :                 break;
    5413                 :            :         case WPA_ALG_BIP_GMAC_128:
    5414         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
    5415                 :            :                             WLAN_CIPHER_SUITE_BIP_GMAC_128);
    5416                 :          0 :                 break;
    5417                 :            :         case WPA_ALG_BIP_GMAC_256:
    5418         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
    5419                 :            :                             WLAN_CIPHER_SUITE_BIP_GMAC_256);
    5420                 :          0 :                 break;
    5421                 :            :         case WPA_ALG_BIP_CMAC_256:
    5422         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
    5423                 :            :                             WLAN_CIPHER_SUITE_BIP_CMAC_256);
    5424                 :          0 :                 break;
    5425                 :            :         default:
    5426                 :          0 :                 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
    5427                 :            :                            "algorithm %d", __func__, alg);
    5428                 :          0 :                 return -1;
    5429                 :            :         }
    5430                 :            : 
    5431 [ -  + ][ #  # ]:          2 :         if (seq && seq_len)
    5432         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
    5433                 :            : 
    5434         [ +  - ]:          2 :         NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
    5435                 :            : 
    5436                 :          2 :         nla_nest_end(msg, key_attr);
    5437                 :            : 
    5438                 :          2 :         return 0;
    5439                 :            :  nla_put_failure:
    5440                 :          2 :         return -1;
    5441                 :            : }
    5442                 :            : 
    5443                 :            : 
    5444                 :          6 : static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
    5445                 :            :                                  struct nl_msg *msg)
    5446                 :            : {
    5447                 :          6 :         int i, privacy = 0;
    5448                 :            :         struct nlattr *nl_keys, *nl_key;
    5449                 :            : 
    5450         [ +  + ]:         30 :         for (i = 0; i < 4; i++) {
    5451         [ +  - ]:         24 :                 if (!params->wep_key[i])
    5452                 :         24 :                         continue;
    5453                 :          0 :                 privacy = 1;
    5454                 :          0 :                 break;
    5455                 :            :         }
    5456         [ -  + ]:          6 :         if (params->wps == WPS_MODE_PRIVACY)
    5457                 :          0 :                 privacy = 1;
    5458 [ +  - ][ +  - ]:          6 :         if (params->pairwise_suite &&
    5459                 :          6 :             params->pairwise_suite != WPA_CIPHER_NONE)
    5460                 :          6 :                 privacy = 1;
    5461                 :            : 
    5462         [ -  + ]:          6 :         if (!privacy)
    5463                 :          0 :                 return 0;
    5464                 :            : 
    5465         [ +  - ]:          6 :         NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
    5466                 :            : 
    5467                 :          6 :         nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
    5468         [ -  + ]:          6 :         if (!nl_keys)
    5469                 :          0 :                 goto nla_put_failure;
    5470                 :            : 
    5471         [ +  + ]:         30 :         for (i = 0; i < 4; i++) {
    5472         [ +  - ]:         24 :                 if (!params->wep_key[i])
    5473                 :         24 :                         continue;
    5474                 :            : 
    5475                 :          0 :                 nl_key = nla_nest_start(msg, i);
    5476         [ #  # ]:          0 :                 if (!nl_key)
    5477                 :          0 :                         goto nla_put_failure;
    5478                 :            : 
    5479         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
    5480                 :            :                         params->wep_key[i]);
    5481         [ #  # ]:          0 :                 if (params->wep_key_len[i] == 5)
    5482         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
    5483                 :            :                                     WLAN_CIPHER_SUITE_WEP40);
    5484                 :            :                 else
    5485         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
    5486                 :            :                                     WLAN_CIPHER_SUITE_WEP104);
    5487                 :            : 
    5488         [ #  # ]:          0 :                 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
    5489                 :            : 
    5490         [ #  # ]:          0 :                 if (i == params->wep_tx_keyidx)
    5491         [ #  # ]:          0 :                         NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
    5492                 :            : 
    5493                 :          0 :                 nla_nest_end(msg, nl_key);
    5494                 :            :         }
    5495                 :          6 :         nla_nest_end(msg, nl_keys);
    5496                 :            : 
    5497                 :          6 :         return 0;
    5498                 :            : 
    5499                 :            : nla_put_failure:
    5500                 :          6 :         return -ENOBUFS;
    5501                 :            : }
    5502                 :            : 
    5503                 :            : 
    5504                 :        385 : static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
    5505                 :            :                                    const u8 *addr, int cmd, u16 reason_code,
    5506                 :            :                                    int local_state_change)
    5507                 :            : {
    5508                 :        385 :         int ret = -1;
    5509                 :            :         struct nl_msg *msg;
    5510                 :            : 
    5511                 :        385 :         msg = nlmsg_alloc();
    5512         [ -  + ]:        385 :         if (!msg)
    5513                 :          0 :                 return -1;
    5514                 :            : 
    5515                 :        385 :         nl80211_cmd(drv, msg, 0, cmd);
    5516                 :            : 
    5517         [ +  - ]:        385 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    5518         [ +  - ]:        385 :         NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
    5519         [ +  - ]:        385 :         if (addr)
    5520         [ +  - ]:        385 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    5521         [ -  + ]:        385 :         if (local_state_change)
    5522         [ #  # ]:          0 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
    5523                 :            : 
    5524                 :        385 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    5525                 :        385 :         msg = NULL;
    5526         [ +  + ]:        385 :         if (ret) {
    5527                 :         10 :                 wpa_dbg(drv->ctx, MSG_DEBUG,
    5528                 :            :                         "nl80211: MLME command failed: reason=%u ret=%d (%s)",
    5529                 :            :                         reason_code, ret, strerror(-ret));
    5530                 :         10 :                 goto nla_put_failure;
    5531                 :            :         }
    5532                 :        375 :         ret = 0;
    5533                 :            : 
    5534                 :            : nla_put_failure:
    5535                 :        385 :         nlmsg_free(msg);
    5536                 :        385 :         return ret;
    5537                 :            : }
    5538                 :            : 
    5539                 :            : 
    5540                 :          0 : static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
    5541                 :            :                                          int reason_code)
    5542                 :            : {
    5543                 :            :         int ret;
    5544                 :            : 
    5545                 :          0 :         wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
    5546                 :          0 :         nl80211_mark_disconnected(drv);
    5547                 :            :         /* Disconnect command doesn't need BSSID - it uses cached value */
    5548                 :          0 :         ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
    5549                 :            :                                       reason_code, 0);
    5550                 :            :         /*
    5551                 :            :          * For locally generated disconnect, supplicant already generates a
    5552                 :            :          * DEAUTH event, so ignore the event from NL80211.
    5553                 :            :          */
    5554                 :          0 :         drv->ignore_next_local_disconnect = ret == 0;
    5555                 :            : 
    5556                 :          0 :         return ret;
    5557                 :            : }
    5558                 :            : 
    5559                 :            : 
    5560                 :        391 : static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
    5561                 :            :                                              const u8 *addr, int reason_code)
    5562                 :            : {
    5563                 :        391 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    5564         [ -  + ]:        391 :         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
    5565                 :          0 :                 return wpa_driver_nl80211_disconnect(drv, reason_code);
    5566                 :        391 :         wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
    5567                 :       2346 :                    __func__, MAC2STR(addr), reason_code);
    5568                 :        391 :         nl80211_mark_disconnected(drv);
    5569         [ +  + ]:        391 :         if (drv->nlmode == NL80211_IFTYPE_ADHOC)
    5570                 :          6 :                 return nl80211_leave_ibss(drv);
    5571                 :        391 :         return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
    5572                 :            :                                        reason_code, 0);
    5573                 :            : }
    5574                 :            : 
    5575                 :            : 
    5576                 :          0 : static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
    5577                 :            :                                      struct wpa_driver_auth_params *params)
    5578                 :            : {
    5579                 :            :         int i;
    5580                 :            : 
    5581                 :          0 :         drv->auth_freq = params->freq;
    5582                 :          0 :         drv->auth_alg = params->auth_alg;
    5583                 :          0 :         drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
    5584                 :          0 :         drv->auth_local_state_change = params->local_state_change;
    5585                 :          0 :         drv->auth_p2p = params->p2p;
    5586                 :            : 
    5587         [ #  # ]:          0 :         if (params->bssid)
    5588                 :          0 :                 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
    5589                 :            :         else
    5590                 :          0 :                 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
    5591                 :            : 
    5592         [ #  # ]:          0 :         if (params->ssid) {
    5593                 :          0 :                 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
    5594                 :          0 :                 drv->auth_ssid_len = params->ssid_len;
    5595                 :            :         } else
    5596                 :          0 :                 drv->auth_ssid_len = 0;
    5597                 :            : 
    5598                 :            : 
    5599                 :          0 :         os_free(drv->auth_ie);
    5600                 :          0 :         drv->auth_ie = NULL;
    5601                 :          0 :         drv->auth_ie_len = 0;
    5602         [ #  # ]:          0 :         if (params->ie) {
    5603                 :          0 :                 drv->auth_ie = os_malloc(params->ie_len);
    5604         [ #  # ]:          0 :                 if (drv->auth_ie) {
    5605                 :          0 :                         os_memcpy(drv->auth_ie, params->ie, params->ie_len);
    5606                 :          0 :                         drv->auth_ie_len = params->ie_len;
    5607                 :            :                 }
    5608                 :            :         }
    5609                 :            : 
    5610         [ #  # ]:          0 :         for (i = 0; i < 4; i++) {
    5611 [ #  # ][ #  # ]:          0 :                 if (params->wep_key[i] && params->wep_key_len[i] &&
                 [ #  # ]
    5612                 :          0 :                     params->wep_key_len[i] <= 16) {
    5613                 :          0 :                         os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
    5614                 :            :                                   params->wep_key_len[i]);
    5615                 :          0 :                         drv->auth_wep_key_len[i] = params->wep_key_len[i];
    5616                 :            :                 } else
    5617                 :          0 :                         drv->auth_wep_key_len[i] = 0;
    5618                 :            :         }
    5619                 :          0 : }
    5620                 :            : 
    5621                 :            : 
    5622                 :        441 : static int wpa_driver_nl80211_authenticate(
    5623                 :            :         struct i802_bss *bss, struct wpa_driver_auth_params *params)
    5624                 :            : {
    5625                 :        441 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    5626                 :        441 :         int ret = -1, i;
    5627                 :            :         struct nl_msg *msg;
    5628                 :            :         enum nl80211_auth_type type;
    5629                 :            :         enum nl80211_iftype nlmode;
    5630                 :        441 :         int count = 0;
    5631                 :            :         int is_retry;
    5632                 :            : 
    5633                 :        441 :         is_retry = drv->retry_auth;
    5634                 :        441 :         drv->retry_auth = 0;
    5635                 :            : 
    5636                 :        441 :         nl80211_mark_disconnected(drv);
    5637                 :        441 :         os_memset(drv->auth_bssid, 0, ETH_ALEN);
    5638         [ +  - ]:        441 :         if (params->bssid)
    5639                 :        441 :                 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
    5640                 :            :         else
    5641                 :          0 :                 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
    5642                 :            :         /* FIX: IBSS mode */
    5643         [ +  + ]:        441 :         nlmode = params->p2p ?
    5644                 :            :                 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
    5645   [ +  +  -  + ]:        493 :         if (drv->nlmode != nlmode &&
    5646                 :         52 :             wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
    5647                 :          0 :                 return -1;
    5648                 :            : 
    5649                 :            : retry:
    5650                 :        441 :         msg = nlmsg_alloc();
    5651         [ -  + ]:        441 :         if (!msg)
    5652                 :          0 :                 return -1;
    5653                 :            : 
    5654                 :        441 :         wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
    5655                 :            :                    drv->ifindex);
    5656                 :            : 
    5657                 :        441 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
    5658                 :            : 
    5659         [ +  + ]:       2205 :         for (i = 0; i < 4; i++) {
    5660         [ +  + ]:       1764 :                 if (!params->wep_key[i])
    5661                 :       1762 :                         continue;
    5662                 :          2 :                 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
    5663                 :            :                                            NULL, i,
    5664                 :          2 :                                            i == params->wep_tx_keyidx, NULL, 0,
    5665                 :            :                                            params->wep_key[i],
    5666                 :            :                                            params->wep_key_len[i]);
    5667         [ -  + ]:          2 :                 if (params->wep_tx_keyidx != i)
    5668                 :          0 :                         continue;
    5669         [ -  + ]:          2 :                 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
    5670                 :            :                                params->wep_key[i], params->wep_key_len[i])) {
    5671                 :          0 :                         nlmsg_free(msg);
    5672                 :          0 :                         return -1;
    5673                 :            :                 }
    5674                 :            :         }
    5675                 :            : 
    5676         [ +  - ]:        441 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    5677         [ +  - ]:        441 :         if (params->bssid) {
    5678                 :        441 :                 wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
    5679                 :       2646 :                            MAC2STR(params->bssid));
    5680         [ +  - ]:        441 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
    5681                 :            :         }
    5682         [ +  - ]:        441 :         if (params->freq) {
    5683                 :        441 :                 wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
    5684         [ +  - ]:        441 :                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
    5685                 :            :         }
    5686         [ +  - ]:        441 :         if (params->ssid) {
    5687                 :        441 :                 wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
    5688                 :        441 :                                   params->ssid, params->ssid_len);
    5689         [ +  - ]:        441 :                 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
    5690                 :            :                         params->ssid);
    5691                 :            :         }
    5692                 :        441 :         wpa_hexdump(MSG_DEBUG, "  * IEs", params->ie, params->ie_len);
    5693         [ +  + ]:        441 :         if (params->ie)
    5694         [ +  - ]:         10 :                 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
    5695         [ +  + ]:        441 :         if (params->sae_data) {
    5696                 :         36 :                 wpa_hexdump(MSG_DEBUG, "  * SAE data", params->sae_data,
    5697                 :            :                             params->sae_data_len);
    5698         [ +  - ]:         36 :                 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
    5699                 :            :                         params->sae_data);
    5700                 :            :         }
    5701         [ +  + ]:        441 :         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
    5702                 :        379 :                 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
    5703         [ -  + ]:         62 :         else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
    5704                 :          0 :                 type = NL80211_AUTHTYPE_SHARED_KEY;
    5705         [ -  + ]:         62 :         else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
    5706                 :          0 :                 type = NL80211_AUTHTYPE_NETWORK_EAP;
    5707         [ +  + ]:         62 :         else if (params->auth_alg & WPA_AUTH_ALG_FT)
    5708                 :         26 :                 type = NL80211_AUTHTYPE_FT;
    5709         [ +  - ]:         36 :         else if (params->auth_alg & WPA_AUTH_ALG_SAE)
    5710                 :         36 :                 type = NL80211_AUTHTYPE_SAE;
    5711                 :            :         else
    5712                 :          0 :                 goto nla_put_failure;
    5713                 :        441 :         wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
    5714         [ +  - ]:        441 :         NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
    5715         [ +  + ]:        441 :         if (params->local_state_change) {
    5716                 :         16 :                 wpa_printf(MSG_DEBUG, "  * Local state change only");
    5717         [ +  - ]:         16 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
    5718                 :            :         }
    5719                 :            : 
    5720                 :        441 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    5721                 :        441 :         msg = NULL;
    5722         [ -  + ]:        441 :         if (ret) {
    5723                 :          0 :                 wpa_dbg(drv->ctx, MSG_DEBUG,
    5724                 :            :                         "nl80211: MLME command failed (auth): ret=%d (%s)",
    5725                 :            :                         ret, strerror(-ret));
    5726                 :          0 :                 count++;
    5727 [ #  # ][ #  # ]:          0 :                 if (ret == -EALREADY && count == 1 && params->bssid &&
         [ #  # ][ #  # ]
    5728                 :          0 :                     !params->local_state_change) {
    5729                 :            :                         /*
    5730                 :            :                          * mac80211 does not currently accept new
    5731                 :            :                          * authentication if we are already authenticated. As a
    5732                 :            :                          * workaround, force deauthentication and try again.
    5733                 :            :                          */
    5734                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
    5735                 :            :                                    "after forced deauthentication");
    5736                 :          0 :                         wpa_driver_nl80211_deauthenticate(
    5737                 :            :                                 bss, params->bssid,
    5738                 :            :                                 WLAN_REASON_PREV_AUTH_NOT_VALID);
    5739                 :          0 :                         nlmsg_free(msg);
    5740                 :          0 :                         goto retry;
    5741                 :            :                 }
    5742                 :            : 
    5743 [ #  # ][ #  # ]:          0 :                 if (ret == -ENOENT && params->freq && !is_retry) {
                 [ #  # ]
    5744                 :            :                         /*
    5745                 :            :                          * cfg80211 has likely expired the BSS entry even
    5746                 :            :                          * though it was previously available in our internal
    5747                 :            :                          * BSS table. To recover quickly, start a single
    5748                 :            :                          * channel scan on the specified channel.
    5749                 :            :                          */
    5750                 :            :                         struct wpa_driver_scan_params scan;
    5751                 :            :                         int freqs[2];
    5752                 :            : 
    5753                 :          0 :                         os_memset(&scan, 0, sizeof(scan));
    5754                 :          0 :                         scan.num_ssids = 1;
    5755         [ #  # ]:          0 :                         if (params->ssid) {
    5756                 :          0 :                                 scan.ssids[0].ssid = params->ssid;
    5757                 :          0 :                                 scan.ssids[0].ssid_len = params->ssid_len;
    5758                 :            :                         }
    5759                 :          0 :                         freqs[0] = params->freq;
    5760                 :          0 :                         freqs[1] = 0;
    5761                 :          0 :                         scan.freqs = freqs;
    5762                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
    5763                 :            :                                    "channel scan to refresh cfg80211 BSS "
    5764                 :            :                                    "entry");
    5765                 :          0 :                         ret = wpa_driver_nl80211_scan(bss, &scan);
    5766         [ #  # ]:          0 :                         if (ret == 0) {
    5767                 :          0 :                                 nl80211_copy_auth_params(drv, params);
    5768                 :          0 :                                 drv->scan_for_auth = 1;
    5769                 :            :                         }
    5770         [ #  # ]:          0 :                 } else if (is_retry) {
    5771                 :            :                         /*
    5772                 :            :                          * Need to indicate this with an event since the return
    5773                 :            :                          * value from the retry is not delivered to core code.
    5774                 :            :                          */
    5775                 :            :                         union wpa_event_data event;
    5776                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
    5777                 :            :                                    "failed");
    5778                 :          0 :                         os_memset(&event, 0, sizeof(event));
    5779                 :          0 :                         os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
    5780                 :            :                                   ETH_ALEN);
    5781                 :          0 :                         wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
    5782                 :            :                                              &event);
    5783                 :            :                 }
    5784                 :            : 
    5785                 :          0 :                 goto nla_put_failure;
    5786                 :            :         }
    5787                 :        441 :         ret = 0;
    5788                 :        441 :         wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
    5789                 :            :                    "successfully");
    5790                 :            : 
    5791                 :            : nla_put_failure:
    5792                 :        441 :         nlmsg_free(msg);
    5793                 :        441 :         return ret;
    5794                 :            : }
    5795                 :            : 
    5796                 :            : 
    5797                 :          0 : static int wpa_driver_nl80211_authenticate_retry(
    5798                 :            :         struct wpa_driver_nl80211_data *drv)
    5799                 :            : {
    5800                 :            :         struct wpa_driver_auth_params params;
    5801                 :          0 :         struct i802_bss *bss = drv->first_bss;
    5802                 :            :         int i;
    5803                 :            : 
    5804                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
    5805                 :            : 
    5806                 :          0 :         os_memset(&params, 0, sizeof(params));
    5807                 :          0 :         params.freq = drv->auth_freq;
    5808                 :          0 :         params.auth_alg = drv->auth_alg;
    5809                 :          0 :         params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
    5810                 :          0 :         params.local_state_change = drv->auth_local_state_change;
    5811                 :          0 :         params.p2p = drv->auth_p2p;
    5812                 :            : 
    5813         [ #  # ]:          0 :         if (!is_zero_ether_addr(drv->auth_bssid_))
    5814                 :          0 :                 params.bssid = drv->auth_bssid_;
    5815                 :            : 
    5816         [ #  # ]:          0 :         if (drv->auth_ssid_len) {
    5817                 :          0 :                 params.ssid = drv->auth_ssid;
    5818                 :          0 :                 params.ssid_len = drv->auth_ssid_len;
    5819                 :            :         }
    5820                 :            : 
    5821                 :          0 :         params.ie = drv->auth_ie;
    5822                 :          0 :         params.ie_len = drv->auth_ie_len;
    5823                 :            : 
    5824         [ #  # ]:          0 :         for (i = 0; i < 4; i++) {
    5825         [ #  # ]:          0 :                 if (drv->auth_wep_key_len[i]) {
    5826                 :          0 :                         params.wep_key[i] = drv->auth_wep_key[i];
    5827                 :          0 :                         params.wep_key_len[i] = drv->auth_wep_key_len[i];
    5828                 :            :                 }
    5829                 :            :         }
    5830                 :            : 
    5831                 :          0 :         drv->retry_auth = 1;
    5832                 :          0 :         return wpa_driver_nl80211_authenticate(bss, &params);
    5833                 :            : }
    5834                 :            : 
    5835                 :            : 
    5836                 :            : struct phy_info_arg {
    5837                 :            :         u16 *num_modes;
    5838                 :            :         struct hostapd_hw_modes *modes;
    5839                 :            :         int last_mode, last_chan_idx;
    5840                 :            : };
    5841                 :            : 
    5842                 :      53298 : static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
    5843                 :            :                              struct nlattr *ampdu_factor,
    5844                 :            :                              struct nlattr *ampdu_density,
    5845                 :            :                              struct nlattr *mcs_set)
    5846                 :            : {
    5847         [ +  + ]:      53298 :         if (capa)
    5848                 :       2538 :                 mode->ht_capab = nla_get_u16(capa);
    5849                 :            : 
    5850         [ +  + ]:      53298 :         if (ampdu_factor)
    5851                 :       2538 :                 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
    5852                 :            : 
    5853         [ +  + ]:      53298 :         if (ampdu_density)
    5854                 :       2538 :                 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
    5855                 :            : 
    5856 [ +  + ][ +  - ]:      53298 :         if (mcs_set && nla_len(mcs_set) >= 16) {
    5857                 :            :                 u8 *mcs;
    5858                 :       2538 :                 mcs = nla_data(mcs_set);
    5859                 :       2538 :                 os_memcpy(mode->mcs_set, mcs, 16);
    5860                 :            :         }
    5861                 :      53298 : }
    5862                 :            : 
    5863                 :            : 
    5864                 :      53298 : static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
    5865                 :            :                               struct nlattr *capa,
    5866                 :            :                               struct nlattr *mcs_set)
    5867                 :            : {
    5868         [ +  + ]:      53298 :         if (capa)
    5869                 :       2538 :                 mode->vht_capab = nla_get_u32(capa);
    5870                 :            : 
    5871 [ +  + ][ +  - ]:      53298 :         if (mcs_set && nla_len(mcs_set) >= 8) {
    5872                 :            :                 u8 *mcs;
    5873                 :       2538 :                 mcs = nla_data(mcs_set);
    5874                 :       2538 :                 os_memcpy(mode->vht_mcs_set, mcs, 8);
    5875                 :            :         }
    5876                 :      53298 : }
    5877                 :            : 
    5878                 :            : 
    5879                 :      48222 : static void phy_info_freq(struct hostapd_hw_modes *mode,
    5880                 :            :                           struct hostapd_channel_data *chan,
    5881                 :            :                           struct nlattr *tb_freq[])
    5882                 :            : {
    5883                 :            :         u8 channel;
    5884                 :      48222 :         chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
    5885                 :      48222 :         chan->flag = 0;
    5886         [ +  - ]:      48222 :         if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
    5887                 :      48222 :                 chan->chan = channel;
    5888                 :            : 
    5889         [ +  + ]:      48222 :         if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
    5890                 :      19035 :                 chan->flag |= HOSTAPD_CHAN_DISABLED;
    5891         [ +  + ]:      48222 :         if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
    5892                 :      34263 :                 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
    5893         [ +  + ]:      48222 :         if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
    5894                 :      19035 :                 chan->flag |= HOSTAPD_CHAN_RADAR;
    5895                 :            : 
    5896         [ +  + ]:      48222 :         if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
    5897                 :      19035 :                 enum nl80211_dfs_state state =
    5898                 :      19035 :                         nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
    5899                 :            : 
    5900   [ +  -  -  - ]:      19035 :                 switch (state) {
    5901                 :            :                 case NL80211_DFS_USABLE:
    5902                 :      19035 :                         chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
    5903                 :      19035 :                         break;
    5904                 :            :                 case NL80211_DFS_AVAILABLE:
    5905                 :          0 :                         chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
    5906                 :          0 :                         break;
    5907                 :            :                 case NL80211_DFS_UNAVAILABLE:
    5908                 :          0 :                         chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
    5909                 :          0 :                         break;
    5910                 :            :                 }
    5911                 :            :         }
    5912                 :      48222 : }
    5913                 :            : 
    5914                 :            : 
    5915                 :      53298 : static int phy_info_freqs(struct phy_info_arg *phy_info,
    5916                 :            :                           struct hostapd_hw_modes *mode, struct nlattr *tb)
    5917                 :            : {
    5918                 :            :         static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
    5919                 :            :                 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
    5920                 :            :                 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
    5921                 :            :                 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
    5922                 :            :                 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
    5923                 :            :                 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
    5924                 :            :                 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
    5925                 :            :         };
    5926                 :      53298 :         int new_channels = 0;
    5927                 :            :         struct hostapd_channel_data *channel;
    5928                 :            :         struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
    5929                 :            :         struct nlattr *nl_freq;
    5930                 :            :         int rem_freq, idx;
    5931                 :            : 
    5932         [ +  + ]:      53298 :         if (tb == NULL)
    5933                 :       2538 :                 return NL_OK;
    5934                 :            : 
    5935         [ +  + ]:      98982 :         nla_for_each_nested(nl_freq, tb, rem_freq) {
    5936                 :      96444 :                 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
    5937                 :      48222 :                           nla_data(nl_freq), nla_len(nl_freq), freq_policy);
    5938         [ -  + ]:      48222 :                 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
    5939                 :          0 :                         continue;
    5940                 :      48222 :                 new_channels++;
    5941                 :            :         }
    5942                 :            : 
    5943                 :      50760 :         channel = os_realloc_array(mode->channels,
    5944                 :      50760 :                                    mode->num_channels + new_channels,
    5945                 :            :                                    sizeof(struct hostapd_channel_data));
    5946         [ -  + ]:      50760 :         if (!channel)
    5947                 :          0 :                 return NL_SKIP;
    5948                 :            : 
    5949                 :      50760 :         mode->channels = channel;
    5950                 :      50760 :         mode->num_channels += new_channels;
    5951                 :            : 
    5952                 :      50760 :         idx = phy_info->last_chan_idx;
    5953                 :            : 
    5954         [ +  + ]:      98982 :         nla_for_each_nested(nl_freq, tb, rem_freq) {
    5955                 :      96444 :                 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
    5956                 :      48222 :                           nla_data(nl_freq), nla_len(nl_freq), freq_policy);
    5957         [ -  + ]:      48222 :                 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
    5958                 :          0 :                         continue;
    5959                 :      48222 :                 phy_info_freq(mode, &mode->channels[idx], tb_freq);
    5960                 :      48222 :                 idx++;
    5961                 :            :         }
    5962                 :      50760 :         phy_info->last_chan_idx = idx;
    5963                 :            : 
    5964                 :      53298 :         return NL_OK;
    5965                 :            : }
    5966                 :            : 
    5967                 :            : 
    5968                 :      53298 : static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
    5969                 :            : {
    5970                 :            :         static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
    5971                 :            :                 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
    5972                 :            :                 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
    5973                 :            :                 { .type = NLA_FLAG },
    5974                 :            :         };
    5975                 :            :         struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
    5976                 :            :         struct nlattr *nl_rate;
    5977                 :            :         int rem_rate, idx;
    5978                 :            : 
    5979         [ +  + ]:      53298 :         if (tb == NULL)
    5980                 :      50760 :                 return NL_OK;
    5981                 :            : 
    5982         [ +  + ]:      27918 :         nla_for_each_nested(nl_rate, tb, rem_rate) {
    5983                 :      50760 :                 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
    5984                 :      25380 :                           nla_data(nl_rate), nla_len(nl_rate),
    5985                 :            :                           rate_policy);
    5986         [ -  + ]:      25380 :                 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
    5987                 :          0 :                         continue;
    5988                 :      25380 :                 mode->num_rates++;
    5989                 :            :         }
    5990                 :            : 
    5991                 :       2538 :         mode->rates = os_calloc(mode->num_rates, sizeof(int));
    5992         [ -  + ]:       2538 :         if (!mode->rates)
    5993                 :          0 :                 return NL_SKIP;
    5994                 :            : 
    5995                 :       2538 :         idx = 0;
    5996                 :            : 
    5997         [ +  + ]:      27918 :         nla_for_each_nested(nl_rate, tb, rem_rate) {
    5998                 :      50760 :                 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
    5999                 :      25380 :                           nla_data(nl_rate), nla_len(nl_rate),
    6000                 :            :                           rate_policy);
    6001         [ -  + ]:      25380 :                 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
    6002                 :          0 :                         continue;
    6003                 :      25380 :                 mode->rates[idx] = nla_get_u32(
    6004                 :            :                         tb_rate[NL80211_BITRATE_ATTR_RATE]);
    6005                 :      25380 :                 idx++;
    6006                 :            :         }
    6007                 :            : 
    6008                 :      53298 :         return NL_OK;
    6009                 :            : }
    6010                 :            : 
    6011                 :            : 
    6012                 :      53298 : static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
    6013                 :            : {
    6014                 :            :         struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
    6015                 :            :         struct hostapd_hw_modes *mode;
    6016                 :            :         int ret;
    6017                 :            : 
    6018         [ +  + ]:      53298 :         if (phy_info->last_mode != nl_band->nla_type) {
    6019                 :       2538 :                 mode = os_realloc_array(phy_info->modes,
    6020                 :       2538 :                                         *phy_info->num_modes + 1,
    6021                 :            :                                         sizeof(*mode));
    6022         [ -  + ]:       2538 :                 if (!mode)
    6023                 :          0 :                         return NL_SKIP;
    6024                 :       2538 :                 phy_info->modes = mode;
    6025                 :            : 
    6026                 :       2538 :                 mode = &phy_info->modes[*(phy_info->num_modes)];
    6027                 :       2538 :                 os_memset(mode, 0, sizeof(*mode));
    6028                 :       2538 :                 mode->mode = NUM_HOSTAPD_MODES;
    6029                 :       2538 :                 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
    6030                 :            :                         HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
    6031                 :            : 
    6032                 :            :                 /*
    6033                 :            :                  * Unsupported VHT MCS stream is defined as value 3, so the VHT
    6034                 :            :                  * MCS RX/TX map must be initialized with 0xffff to mark all 8
    6035                 :            :                  * possible streams as unsupported. This will be overridden if
    6036                 :            :                  * driver advertises VHT support.
    6037                 :            :                  */
    6038                 :       2538 :                 mode->vht_mcs_set[0] = 0xff;
    6039                 :       2538 :                 mode->vht_mcs_set[1] = 0xff;
    6040                 :       2538 :                 mode->vht_mcs_set[4] = 0xff;
    6041                 :       2538 :                 mode->vht_mcs_set[5] = 0xff;
    6042                 :            : 
    6043                 :       2538 :                 *(phy_info->num_modes) += 1;
    6044                 :       2538 :                 phy_info->last_mode = nl_band->nla_type;
    6045                 :       2538 :                 phy_info->last_chan_idx = 0;
    6046                 :            :         } else
    6047                 :      50760 :                 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
    6048                 :            : 
    6049                 :      53298 :         nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
    6050                 :            :                   nla_len(nl_band), NULL);
    6051                 :            : 
    6052                 :      53298 :         phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
    6053                 :            :                          tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
    6054                 :            :                          tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
    6055                 :            :                          tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
    6056                 :      53298 :         phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
    6057                 :            :                           tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
    6058                 :      53298 :         ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
    6059         [ -  + ]:      53298 :         if (ret != NL_OK)
    6060                 :          0 :                 return ret;
    6061                 :      53298 :         ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
    6062         [ -  + ]:      53298 :         if (ret != NL_OK)
    6063                 :          0 :                 return ret;
    6064                 :            : 
    6065                 :      53298 :         return NL_OK;
    6066                 :            : }
    6067                 :            : 
    6068                 :            : 
    6069                 :      67257 : static int phy_info_handler(struct nl_msg *msg, void *arg)
    6070                 :            : {
    6071                 :            :         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
    6072                 :      67257 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    6073                 :      67257 :         struct phy_info_arg *phy_info = arg;
    6074                 :            :         struct nlattr *nl_band;
    6075                 :            :         int rem_band;
    6076                 :            : 
    6077                 :      67257 :         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    6078                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    6079                 :            : 
    6080         [ +  + ]:      67257 :         if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
    6081                 :      12690 :                 return NL_SKIP;
    6082                 :            : 
    6083         [ +  + ]:     107865 :         nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
    6084                 :            :         {
    6085                 :      53298 :                 int res = phy_info_band(phy_info, nl_band);
    6086         [ -  + ]:      53298 :                 if (res != NL_OK)
    6087                 :          0 :                         return res;
    6088                 :            :         }
    6089                 :            : 
    6090                 :      67257 :         return NL_SKIP;
    6091                 :            : }
    6092                 :            : 
    6093                 :            : 
    6094                 :            : static struct hostapd_hw_modes *
    6095                 :       1269 : wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
    6096                 :            :                                      u16 *num_modes)
    6097                 :            : {
    6098                 :            :         u16 m;
    6099                 :       1269 :         struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
    6100                 :       1269 :         int i, mode11g_idx = -1;
    6101                 :            : 
    6102                 :            :         /* heuristic to set up modes */
    6103         [ +  + ]:       3807 :         for (m = 0; m < *num_modes; m++) {
    6104         [ -  + ]:       2538 :                 if (!modes[m].num_channels)
    6105                 :          0 :                         continue;
    6106         [ +  + ]:       2538 :                 if (modes[m].channels[0].freq < 4000) {
    6107                 :       1269 :                         modes[m].mode = HOSTAPD_MODE_IEEE80211B;
    6108         [ +  - ]:      12690 :                         for (i = 0; i < modes[m].num_rates; i++) {
    6109         [ +  + ]:      11421 :                                 if (modes[m].rates[i] > 200) {
    6110                 :       1269 :                                         modes[m].mode = HOSTAPD_MODE_IEEE80211G;
    6111                 :       1269 :                                         break;
    6112                 :            :                                 }
    6113                 :            :                         }
    6114         [ -  + ]:       1269 :                 } else if (modes[m].channels[0].freq > 50000)
    6115                 :          0 :                         modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
    6116                 :            :                 else
    6117                 :       1269 :                         modes[m].mode = HOSTAPD_MODE_IEEE80211A;
    6118                 :            :         }
    6119                 :            : 
    6120                 :            :         /* If only 802.11g mode is included, use it to construct matching
    6121                 :            :          * 802.11b mode data. */
    6122                 :            : 
    6123         [ +  + ]:       3807 :         for (m = 0; m < *num_modes; m++) {
    6124         [ -  + ]:       2538 :                 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
    6125                 :          0 :                         return modes; /* 802.11b already included */
    6126         [ +  + ]:       2538 :                 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
    6127                 :       1269 :                         mode11g_idx = m;
    6128                 :            :         }
    6129                 :            : 
    6130         [ -  + ]:       1269 :         if (mode11g_idx < 0)
    6131                 :          0 :                 return modes; /* 2.4 GHz band not supported at all */
    6132                 :            : 
    6133                 :       1269 :         nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
    6134         [ -  + ]:       1269 :         if (nmodes == NULL)
    6135                 :          0 :                 return modes; /* Could not add 802.11b mode */
    6136                 :            : 
    6137                 :       1269 :         mode = &nmodes[*num_modes];
    6138                 :       1269 :         os_memset(mode, 0, sizeof(*mode));
    6139                 :       1269 :         (*num_modes)++;
    6140                 :       1269 :         modes = nmodes;
    6141                 :            : 
    6142                 :       1269 :         mode->mode = HOSTAPD_MODE_IEEE80211B;
    6143                 :            : 
    6144                 :       1269 :         mode11g = &modes[mode11g_idx];
    6145                 :       1269 :         mode->num_channels = mode11g->num_channels;
    6146                 :       1269 :         mode->channels = os_malloc(mode11g->num_channels *
    6147                 :            :                                    sizeof(struct hostapd_channel_data));
    6148         [ -  + ]:       1269 :         if (mode->channels == NULL) {
    6149                 :          0 :                 (*num_modes)--;
    6150                 :          0 :                 return modes; /* Could not add 802.11b mode */
    6151                 :            :         }
    6152                 :       1269 :         os_memcpy(mode->channels, mode11g->channels,
    6153                 :            :                   mode11g->num_channels * sizeof(struct hostapd_channel_data));
    6154                 :            : 
    6155                 :       1269 :         mode->num_rates = 0;
    6156                 :       1269 :         mode->rates = os_malloc(4 * sizeof(int));
    6157         [ -  + ]:       1269 :         if (mode->rates == NULL) {
    6158                 :          0 :                 os_free(mode->channels);
    6159                 :          0 :                 (*num_modes)--;
    6160                 :          0 :                 return modes; /* Could not add 802.11b mode */
    6161                 :            :         }
    6162                 :            : 
    6163         [ +  - ]:       5076 :         for (i = 0; i < mode11g->num_rates; i++) {
    6164 [ +  + ][ +  + ]:       5076 :                 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
                 [ +  + ]
    6165         [ -  + ]:       1269 :                     mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
    6166                 :          0 :                         continue;
    6167                 :       5076 :                 mode->rates[mode->num_rates] = mode11g->rates[i];
    6168                 :       5076 :                 mode->num_rates++;
    6169         [ +  + ]:       5076 :                 if (mode->num_rates == 4)
    6170                 :       1269 :                         break;
    6171                 :            :         }
    6172                 :            : 
    6173         [ -  + ]:       1269 :         if (mode->num_rates == 0) {
    6174                 :          0 :                 os_free(mode->channels);
    6175                 :          0 :                 os_free(mode->rates);
    6176                 :          0 :                 (*num_modes)--;
    6177                 :          0 :                 return modes; /* No 802.11b rates */
    6178                 :            :         }
    6179                 :            : 
    6180                 :       1269 :         wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
    6181                 :            :                    "information");
    6182                 :            : 
    6183                 :       1269 :         return modes;
    6184                 :            : }
    6185                 :            : 
    6186                 :            : 
    6187                 :       7614 : static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
    6188                 :            :                                   int end)
    6189                 :            : {
    6190                 :            :         int c;
    6191                 :            : 
    6192         [ +  + ]:     152280 :         for (c = 0; c < mode->num_channels; c++) {
    6193                 :     144666 :                 struct hostapd_channel_data *chan = &mode->channels[c];
    6194 [ +  + ][ +  + ]:     144666 :                 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
    6195                 :      25380 :                         chan->flag |= HOSTAPD_CHAN_HT40;
    6196                 :            :         }
    6197                 :       7614 : }
    6198                 :            : 
    6199                 :            : 
    6200                 :      12690 : static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
    6201                 :            :                                       int end)
    6202                 :            : {
    6203                 :            :         int c;
    6204                 :            : 
    6205         [ +  + ]:     253800 :         for (c = 0; c < mode->num_channels; c++) {
    6206                 :     241110 :                 struct hostapd_channel_data *chan = &mode->channels[c];
    6207         [ +  + ]:     241110 :                 if (!(chan->flag & HOSTAPD_CHAN_HT40))
    6208                 :     114210 :                         continue;
    6209 [ +  + ][ +  + ]:     126900 :                 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
    6210                 :      17766 :                         chan->flag |= HOSTAPD_CHAN_HT40MINUS;
    6211 [ +  + ][ +  + ]:     126900 :                 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
    6212                 :      20304 :                         chan->flag |= HOSTAPD_CHAN_HT40PLUS;
    6213                 :            :         }
    6214                 :      12690 : }
    6215                 :            : 
    6216                 :            : 
    6217                 :       6345 : static void nl80211_reg_rule_max_eirp(struct nlattr *tb[],
    6218                 :            :                                       struct phy_info_arg *results)
    6219                 :            : {
    6220                 :            :         u32 start, end, max_eirp;
    6221                 :            :         u16 m;
    6222                 :            : 
    6223 [ +  - ][ +  - ]:       6345 :         if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
    6224         [ -  + ]:       6345 :             tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
    6225                 :       6345 :             tb[NL80211_ATTR_POWER_RULE_MAX_EIRP] == NULL)
    6226                 :       6345 :                 return;
    6227                 :            : 
    6228                 :       6345 :         start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
    6229                 :       6345 :         end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
    6230                 :       6345 :         max_eirp = nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
    6231                 :            : 
    6232                 :       6345 :         wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u mBm",
    6233                 :            :                    start, end, max_eirp);
    6234                 :            : 
    6235         [ +  + ]:      19035 :         for (m = 0; m < *results->num_modes; m++) {
    6236                 :            :                 int c;
    6237                 :      12690 :                 struct hostapd_hw_modes *mode = &results->modes[m];
    6238                 :            : 
    6239         [ +  + ]:     253800 :                 for (c = 0; c < mode->num_channels; c++) {
    6240                 :     241110 :                         struct hostapd_channel_data *chan = &mode->channels[c];
    6241 [ +  + ][ +  + ]:     241110 :                         if ((u32) chan->freq - 10 >= start &&
    6242                 :     151011 :                             (u32) chan->freq + 10 <= end)
    6243                 :      29187 :                                 chan->max_tx_power = max_eirp;
    6244                 :            :                 }
    6245                 :            :         }
    6246                 :            : }
    6247                 :            : 
    6248                 :            : 
    6249                 :       6345 : static void nl80211_reg_rule_ht40(struct nlattr *tb[],
    6250                 :            :                                   struct phy_info_arg *results)
    6251                 :            : {
    6252                 :            :         u32 start, end, max_bw;
    6253                 :            :         u16 m;
    6254                 :            : 
    6255 [ +  - ][ +  - ]:       6345 :         if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
    6256         [ -  + ]:       6345 :             tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
    6257                 :       6345 :             tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
    6258                 :          0 :                 return;
    6259                 :            : 
    6260                 :       6345 :         start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
    6261                 :       6345 :         end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
    6262                 :       6345 :         max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
    6263                 :            : 
    6264                 :       6345 :         wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
    6265                 :            :                    start, end, max_bw);
    6266         [ +  + ]:       6345 :         if (max_bw < 40)
    6267                 :       2538 :                 return;
    6268                 :            : 
    6269         [ +  + ]:      13959 :         for (m = 0; m < *results->num_modes; m++) {
    6270         [ -  + ]:       7614 :                 if (!(results->modes[m].ht_capab &
    6271                 :            :                       HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
    6272                 :          0 :                         continue;
    6273                 :       7614 :                 nl80211_set_ht40_mode(&results->modes[m], start, end);
    6274                 :            :         }
    6275                 :            : }
    6276                 :            : 
    6277                 :            : 
    6278                 :       6345 : static void nl80211_reg_rule_sec(struct nlattr *tb[],
    6279                 :            :                                  struct phy_info_arg *results)
    6280                 :            : {
    6281                 :            :         u32 start, end, max_bw;
    6282                 :            :         u16 m;
    6283                 :            : 
    6284 [ +  - ][ +  - ]:       6345 :         if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
    6285         [ -  + ]:       6345 :             tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
    6286                 :       6345 :             tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
    6287                 :          0 :                 return;
    6288                 :            : 
    6289                 :       6345 :         start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
    6290                 :       6345 :         end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
    6291                 :       6345 :         max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
    6292                 :            : 
    6293         [ -  + ]:       6345 :         if (max_bw < 20)
    6294                 :          0 :                 return;
    6295                 :            : 
    6296         [ +  + ]:      19035 :         for (m = 0; m < *results->num_modes; m++) {
    6297         [ -  + ]:      12690 :                 if (!(results->modes[m].ht_capab &
    6298                 :            :                       HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
    6299                 :          0 :                         continue;
    6300                 :      12690 :                 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
    6301                 :            :         }
    6302                 :            : }
    6303                 :            : 
    6304                 :            : 
    6305                 :          0 : static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
    6306                 :            :                                  int end)
    6307                 :            : {
    6308                 :            :         int c;
    6309                 :            : 
    6310         [ #  # ]:          0 :         for (c = 0; c < mode->num_channels; c++) {
    6311                 :          0 :                 struct hostapd_channel_data *chan = &mode->channels[c];
    6312 [ #  # ][ #  # ]:          0 :                 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
    6313                 :          0 :                         chan->flag |= HOSTAPD_CHAN_VHT_10_70;
    6314                 :            : 
    6315 [ #  # ][ #  # ]:          0 :                 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
    6316                 :          0 :                         chan->flag |= HOSTAPD_CHAN_VHT_30_50;
    6317                 :            : 
    6318 [ #  # ][ #  # ]:          0 :                 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
    6319                 :          0 :                         chan->flag |= HOSTAPD_CHAN_VHT_50_30;
    6320                 :            : 
    6321 [ #  # ][ #  # ]:          0 :                 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
    6322                 :          0 :                         chan->flag |= HOSTAPD_CHAN_VHT_70_10;
    6323                 :            :         }
    6324                 :          0 : }
    6325                 :            : 
    6326                 :            : 
    6327                 :       6345 : static void nl80211_reg_rule_vht(struct nlattr *tb[],
    6328                 :            :                                  struct phy_info_arg *results)
    6329                 :            : {
    6330                 :            :         u32 start, end, max_bw;
    6331                 :            :         u16 m;
    6332                 :            : 
    6333 [ +  - ][ +  - ]:       6345 :         if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
    6334         [ -  + ]:       6345 :             tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
    6335                 :       6345 :             tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
    6336                 :          0 :                 return;
    6337                 :            : 
    6338                 :       6345 :         start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
    6339                 :       6345 :         end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
    6340                 :       6345 :         max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
    6341                 :            : 
    6342         [ +  - ]:       6345 :         if (max_bw < 80)
    6343                 :       6345 :                 return;
    6344                 :            : 
    6345         [ #  # ]:       6345 :         for (m = 0; m < *results->num_modes; m++) {
    6346         [ #  # ]:          0 :                 if (!(results->modes[m].ht_capab &
    6347                 :            :                       HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
    6348                 :          0 :                         continue;
    6349                 :            :                 /* TODO: use a real VHT support indication */
    6350         [ #  # ]:          0 :                 if (!results->modes[m].vht_capab)
    6351                 :          0 :                         continue;
    6352                 :            : 
    6353                 :          0 :                 nl80211_set_vht_mode(&results->modes[m], start, end);
    6354                 :            :         }
    6355                 :            : }
    6356                 :            : 
    6357                 :            : 
    6358                 :       1269 : static int nl80211_get_reg(struct nl_msg *msg, void *arg)
    6359                 :            : {
    6360                 :       1269 :         struct phy_info_arg *results = arg;
    6361                 :            :         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
    6362                 :       1269 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    6363                 :            :         struct nlattr *nl_rule;
    6364                 :            :         struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
    6365                 :            :         int rem_rule;
    6366                 :            :         static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
    6367                 :            :                 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
    6368                 :            :                 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
    6369                 :            :                 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
    6370                 :            :                 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
    6371                 :            :                 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
    6372                 :            :                 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
    6373                 :            :         };
    6374                 :            : 
    6375                 :       1269 :         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    6376                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    6377 [ -  + ][ +  - ]:       1269 :         if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
    6378                 :       1269 :             !tb_msg[NL80211_ATTR_REG_RULES]) {
    6379                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
    6380                 :            :                            "available");
    6381                 :          0 :                 return NL_SKIP;
    6382                 :            :         }
    6383                 :            : 
    6384                 :       1269 :         wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
    6385                 :       1269 :                    (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
    6386                 :            : 
    6387         [ +  + ]:       7614 :         nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
    6388                 :            :         {
    6389                 :      12690 :                 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
    6390                 :       6345 :                           nla_data(nl_rule), nla_len(nl_rule), reg_policy);
    6391                 :       6345 :                 nl80211_reg_rule_ht40(tb_rule, results);
    6392                 :       6345 :                 nl80211_reg_rule_max_eirp(tb_rule, results);
    6393                 :            :         }
    6394                 :            : 
    6395         [ +  + ]:       7614 :         nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
    6396                 :            :         {
    6397                 :      12690 :                 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
    6398                 :       6345 :                           nla_data(nl_rule), nla_len(nl_rule), reg_policy);
    6399                 :       6345 :                 nl80211_reg_rule_sec(tb_rule, results);
    6400                 :            :         }
    6401                 :            : 
    6402         [ +  + ]:       7614 :         nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
    6403                 :            :         {
    6404                 :      12690 :                 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
    6405                 :       6345 :                           nla_data(nl_rule), nla_len(nl_rule), reg_policy);
    6406                 :       6345 :                 nl80211_reg_rule_vht(tb_rule, results);
    6407                 :            :         }
    6408                 :            : 
    6409                 :       1269 :         return NL_SKIP;
    6410                 :            : }
    6411                 :            : 
    6412                 :            : 
    6413                 :       1269 : static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
    6414                 :            :                                         struct phy_info_arg *results)
    6415                 :            : {
    6416                 :            :         struct nl_msg *msg;
    6417                 :            : 
    6418                 :       1269 :         msg = nlmsg_alloc();
    6419         [ -  + ]:       1269 :         if (!msg)
    6420                 :          0 :                 return -ENOMEM;
    6421                 :            : 
    6422                 :       1269 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
    6423                 :       1269 :         return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
    6424                 :            : }
    6425                 :            : 
    6426                 :            : 
    6427                 :            : static struct hostapd_hw_modes *
    6428                 :       1269 : wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
    6429                 :            : {
    6430                 :            :         u32 feat;
    6431                 :       1269 :         struct i802_bss *bss = priv;
    6432                 :       1269 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    6433                 :            :         struct nl_msg *msg;
    6434                 :       1269 :         struct phy_info_arg result = {
    6435                 :            :                 .num_modes = num_modes,
    6436                 :            :                 .modes = NULL,
    6437                 :            :                 .last_mode = -1,
    6438                 :            :         };
    6439                 :            : 
    6440                 :       1269 :         *num_modes = 0;
    6441                 :       1269 :         *flags = 0;
    6442                 :            : 
    6443                 :       1269 :         msg = nlmsg_alloc();
    6444         [ -  + ]:       1269 :         if (!msg)
    6445                 :          0 :                 return NULL;
    6446                 :            : 
    6447                 :       1269 :         feat = get_nl80211_protocol_features(drv);
    6448         [ +  - ]:       1269 :         if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
    6449                 :       1269 :                 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
    6450                 :            :         else
    6451                 :          0 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
    6452                 :            : 
    6453         [ +  - ]:       1269 :         NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
    6454         [ +  - ]:       1269 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    6455                 :            : 
    6456         [ +  - ]:       1269 :         if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
    6457                 :       1269 :                 nl80211_set_regulatory_flags(drv, &result);
    6458                 :       1269 :                 return wpa_driver_nl80211_postprocess_modes(result.modes,
    6459                 :            :                                                             num_modes);
    6460                 :            :         }
    6461                 :          0 :         msg = NULL;
    6462                 :            :  nla_put_failure:
    6463                 :          0 :         nlmsg_free(msg);
    6464                 :       1269 :         return NULL;
    6465                 :            : }
    6466                 :            : 
    6467                 :            : 
    6468                 :          0 : static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
    6469                 :            :                                         const void *data, size_t len,
    6470                 :            :                                         int encrypt, int noack)
    6471                 :            : {
    6472                 :          0 :         __u8 rtap_hdr[] = {
    6473                 :            :                 0x00, 0x00, /* radiotap version */
    6474                 :            :                 0x0e, 0x00, /* radiotap length */
    6475                 :            :                 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
    6476                 :            :                 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
    6477                 :            :                 0x00,       /* padding */
    6478                 :            :                 0x00, 0x00, /* RX and TX flags to indicate that */
    6479                 :            :                 0x00, 0x00, /* this is the injected frame directly */
    6480                 :            :         };
    6481                 :          0 :         struct iovec iov[2] = {
    6482                 :            :                 {
    6483                 :            :                         .iov_base = &rtap_hdr,
    6484                 :            :                         .iov_len = sizeof(rtap_hdr),
    6485                 :            :                 },
    6486                 :            :                 {
    6487                 :            :                         .iov_base = (void *) data,
    6488                 :            :                         .iov_len = len,
    6489                 :            :                 }
    6490                 :            :         };
    6491                 :          0 :         struct msghdr msg = {
    6492                 :            :                 .msg_name = NULL,
    6493                 :            :                 .msg_namelen = 0,
    6494                 :            :                 .msg_iov = iov,
    6495                 :            :                 .msg_iovlen = 2,
    6496                 :            :                 .msg_control = NULL,
    6497                 :            :                 .msg_controllen = 0,
    6498                 :            :                 .msg_flags = 0,
    6499                 :            :         };
    6500                 :            :         int res;
    6501                 :          0 :         u16 txflags = 0;
    6502                 :            : 
    6503         [ #  # ]:          0 :         if (encrypt)
    6504                 :          0 :                 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
    6505                 :            : 
    6506         [ #  # ]:          0 :         if (drv->monitor_sock < 0) {
    6507                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
    6508                 :            :                            "for %s", __func__);
    6509                 :          0 :                 return -1;
    6510                 :            :         }
    6511                 :            : 
    6512         [ #  # ]:          0 :         if (noack)
    6513                 :          0 :                 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
    6514                 :          0 :         WPA_PUT_LE16(&rtap_hdr[12], txflags);
    6515                 :            : 
    6516                 :          0 :         res = sendmsg(drv->monitor_sock, &msg, 0);
    6517         [ #  # ]:          0 :         if (res < 0) {
    6518                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
    6519                 :          0 :                 return -1;
    6520                 :            :         }
    6521                 :          0 :         return 0;
    6522                 :            : }
    6523                 :            : 
    6524                 :            : 
    6525                 :       2374 : static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
    6526                 :            :                                          const void *data, size_t len,
    6527                 :            :                                          int encrypt, int noack,
    6528                 :            :                                          unsigned int freq, int no_cck,
    6529                 :            :                                          int offchanok, unsigned int wait_time)
    6530                 :            : {
    6531                 :       2374 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    6532                 :            :         u64 cookie;
    6533                 :            :         int res;
    6534                 :            : 
    6535         [ +  + ]:       2374 :         if (freq == 0) {
    6536                 :       2279 :                 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
    6537                 :            :                            bss->freq);
    6538                 :       2279 :                 freq = bss->freq;
    6539                 :            :         }
    6540                 :            : 
    6541         [ -  + ]:       2374 :         if (drv->use_monitor) {
    6542                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
    6543                 :            :                            freq, bss->freq);
    6544                 :          0 :                 return wpa_driver_nl80211_send_mntr(drv, data, len,
    6545                 :            :                                                     encrypt, noack);
    6546                 :            :         }
    6547                 :            : 
    6548                 :       2374 :         wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
    6549                 :       2374 :         res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
    6550                 :            :                                      &cookie, no_cck, noack, offchanok);
    6551 [ +  + ][ +  + ]:       2374 :         if (res == 0 && !noack) {
    6552                 :            :                 const struct ieee80211_mgmt *mgmt;
    6553                 :            :                 u16 fc;
    6554                 :            : 
    6555                 :       1405 :                 mgmt = (const struct ieee80211_mgmt *) data;
    6556                 :       1405 :                 fc = le_to_host16(mgmt->frame_control);
    6557 [ +  - ][ +  + ]:       1405 :                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
    6558                 :       1405 :                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
    6559                 :        121 :                         wpa_printf(MSG_MSGDUMP,
    6560                 :            :                                    "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
    6561                 :            :                                    (long long unsigned int)
    6562                 :        121 :                                    drv->send_action_cookie,
    6563                 :            :                                    (long long unsigned int) cookie);
    6564                 :        121 :                         drv->send_action_cookie = cookie;
    6565                 :            :                 }
    6566                 :            :         }
    6567                 :            : 
    6568                 :       2374 :         return res;
    6569                 :            : }
    6570                 :            : 
    6571                 :            : 
    6572                 :       2603 : static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
    6573                 :            :                                         size_t data_len, int noack,
    6574                 :            :                                         unsigned int freq, int no_cck,
    6575                 :            :                                         int offchanok,
    6576                 :            :                                         unsigned int wait_time)
    6577                 :            : {
    6578                 :       2603 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    6579                 :            :         struct ieee80211_mgmt *mgmt;
    6580                 :       2603 :         int encrypt = 1;
    6581                 :            :         u16 fc;
    6582                 :            : 
    6583                 :       2603 :         mgmt = (struct ieee80211_mgmt *) data;
    6584                 :       2603 :         fc = le_to_host16(mgmt->frame_control);
    6585                 :       2603 :         wpa_printf(MSG_DEBUG, "nl80211: send_mlme - noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x nlmode=%d",
    6586                 :       2603 :                    noack, freq, no_cck, offchanok, wait_time, fc, drv->nlmode);
    6587                 :            : 
    6588 [ -  + ][ +  + ]:       2603 :         if ((is_sta_interface(drv->nlmode) ||
    6589         [ +  - ]:        241 :              drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
    6590         [ +  - ]:        241 :             WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
    6591                 :        241 :             WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
    6592                 :            :                 /*
    6593                 :            :                  * The use of last_mgmt_freq is a bit of a hack,
    6594                 :            :                  * but it works due to the single-threaded nature
    6595                 :            :                  * of wpa_supplicant.
    6596                 :            :                  */
    6597         [ +  - ]:        241 :                 if (freq == 0) {
    6598                 :        241 :                         wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
    6599                 :            :                                    drv->last_mgmt_freq);
    6600                 :        241 :                         freq = drv->last_mgmt_freq;
    6601                 :            :                 }
    6602                 :        241 :                 return nl80211_send_frame_cmd(bss, freq, 0,
    6603                 :            :                                               data, data_len, NULL, 1, noack,
    6604                 :            :                                               1);
    6605                 :            :         }
    6606                 :            : 
    6607 [ -  + ][ #  # ]:       2362 :         if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
    6608         [ #  # ]:          0 :                 if (freq == 0) {
    6609                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
    6610                 :            :                                    bss->freq);
    6611                 :          0 :                         freq = bss->freq;
    6612                 :            :                 }
    6613         [ #  # ]:          0 :                 return nl80211_send_frame_cmd(bss, freq,
    6614                 :          0 :                                               (int) freq == bss->freq ? 0 :
    6615                 :            :                                               wait_time,
    6616                 :            :                                               data, data_len,
    6617                 :            :                                               &drv->send_action_cookie,
    6618                 :            :                                               no_cck, noack, offchanok);
    6619                 :            :         }
    6620                 :            : 
    6621 [ +  - ][ +  + ]:       2362 :         if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
    6622                 :       2362 :             WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
    6623                 :            :                 /*
    6624                 :            :                  * Only one of the authentication frame types is encrypted.
    6625                 :            :                  * In order for static WEP encryption to work properly (i.e.,
    6626                 :            :                  * to not encrypt the frame), we need to tell mac80211 about
    6627                 :            :                  * the frames that must not be encrypted.
    6628                 :            :                  */
    6629                 :        422 :                 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
    6630                 :        422 :                 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
    6631 [ -  + ][ #  # ]:        422 :                 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
    6632                 :        422 :                         encrypt = 0;
    6633                 :            :         }
    6634                 :            : 
    6635                 :       2362 :         wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
    6636                 :       2603 :         return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
    6637                 :            :                                              noack, freq, no_cck, offchanok,
    6638                 :            :                                              wait_time);
    6639                 :            : }
    6640                 :            : 
    6641                 :            : 
    6642                 :       1082 : static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
    6643                 :            :                            int slot, int ht_opmode, int ap_isolate,
    6644                 :            :                            int *basic_rates)
    6645                 :            : {
    6646                 :       1082 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    6647                 :            :         struct nl_msg *msg;
    6648                 :            : 
    6649                 :       1082 :         msg = nlmsg_alloc();
    6650         [ -  + ]:       1082 :         if (!msg)
    6651                 :          0 :                 return -ENOMEM;
    6652                 :            : 
    6653                 :       1082 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
    6654                 :            : 
    6655         [ +  - ]:       1082 :         if (cts >= 0)
    6656         [ +  - ]:       1082 :                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
    6657         [ +  - ]:       1082 :         if (preamble >= 0)
    6658         [ +  - ]:       1082 :                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
    6659         [ +  - ]:       1082 :         if (slot >= 0)
    6660         [ +  - ]:       1082 :                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
    6661         [ +  + ]:       1082 :         if (ht_opmode >= 0)
    6662         [ +  - ]:        530 :                 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
    6663         [ +  - ]:       1082 :         if (ap_isolate >= 0)
    6664         [ +  - ]:       1082 :                 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
    6665                 :            : 
    6666         [ +  - ]:       1082 :         if (basic_rates) {
    6667                 :            :                 u8 rates[NL80211_MAX_SUPP_RATES];
    6668                 :       1082 :                 u8 rates_len = 0;
    6669                 :            :                 int i;
    6670                 :            : 
    6671 [ +  - ][ +  + ]:       4986 :                 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
    6672                 :       3904 :                      i++)
    6673                 :       3904 :                         rates[rates_len++] = basic_rates[i] / 5;
    6674                 :            : 
    6675         [ +  - ]:       1082 :                 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
    6676                 :            :         }
    6677                 :            : 
    6678         [ +  - ]:       1082 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
    6679                 :            : 
    6680                 :       1082 :         return send_and_recv_msgs(drv, msg, NULL, NULL);
    6681                 :            :  nla_put_failure:
    6682                 :          0 :         nlmsg_free(msg);
    6683                 :       1082 :         return -ENOBUFS;
    6684                 :            : }
    6685                 :            : 
    6686                 :            : 
    6687                 :          0 : static int wpa_driver_nl80211_set_acl(void *priv,
    6688                 :            :                                       struct hostapd_acl_params *params)
    6689                 :            : {
    6690                 :          0 :         struct i802_bss *bss = priv;
    6691                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    6692                 :            :         struct nl_msg *msg;
    6693                 :            :         struct nlattr *acl;
    6694                 :            :         unsigned int i;
    6695                 :          0 :         int ret = 0;
    6696                 :            : 
    6697         [ #  # ]:          0 :         if (!(drv->capa.max_acl_mac_addrs))
    6698                 :          0 :                 return -ENOTSUP;
    6699                 :            : 
    6700         [ #  # ]:          0 :         if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
    6701                 :          0 :                 return -ENOTSUP;
    6702                 :            : 
    6703                 :          0 :         msg = nlmsg_alloc();
    6704         [ #  # ]:          0 :         if (!msg)
    6705                 :          0 :                 return -ENOMEM;
    6706                 :            : 
    6707         [ #  # ]:          0 :         wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
    6708                 :          0 :                    params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
    6709                 :            : 
    6710                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
    6711                 :            : 
    6712         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    6713                 :            : 
    6714         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
    6715                 :            :                     NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
    6716                 :            :                     NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
    6717                 :            : 
    6718                 :          0 :         acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
    6719         [ #  # ]:          0 :         if (acl == NULL)
    6720                 :          0 :                 goto nla_put_failure;
    6721                 :            : 
    6722         [ #  # ]:          0 :         for (i = 0; i < params->num_mac_acl; i++)
    6723         [ #  # ]:          0 :                 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
    6724                 :            : 
    6725                 :          0 :         nla_nest_end(msg, acl);
    6726                 :            : 
    6727                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    6728                 :          0 :         msg = NULL;
    6729         [ #  # ]:          0 :         if (ret) {
    6730                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
    6731                 :            :                            ret, strerror(-ret));
    6732                 :            :         }
    6733                 :            : 
    6734                 :            : nla_put_failure:
    6735                 :          0 :         nlmsg_free(msg);
    6736                 :            : 
    6737                 :          0 :         return ret;
    6738                 :            : }
    6739                 :            : 
    6740                 :            : 
    6741                 :       1082 : static int wpa_driver_nl80211_set_ap(void *priv,
    6742                 :            :                                      struct wpa_driver_ap_params *params)
    6743                 :            : {
    6744                 :       1082 :         struct i802_bss *bss = priv;
    6745                 :       1082 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    6746                 :            :         struct nl_msg *msg;
    6747                 :       1082 :         u8 cmd = NL80211_CMD_NEW_BEACON;
    6748                 :            :         int ret;
    6749                 :            :         int beacon_set;
    6750                 :       1082 :         int ifindex = if_nametoindex(bss->ifname);
    6751                 :            :         int num_suites;
    6752                 :            :         u32 suites[10];
    6753                 :            :         u32 ver;
    6754                 :            : 
    6755                 :       1082 :         beacon_set = bss->beacon_set;
    6756                 :            : 
    6757                 :       1082 :         msg = nlmsg_alloc();
    6758         [ -  + ]:       1082 :         if (!msg)
    6759                 :          0 :                 return -ENOMEM;
    6760                 :            : 
    6761                 :       1082 :         wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
    6762                 :            :                    beacon_set);
    6763         [ +  + ]:       1082 :         if (beacon_set)
    6764                 :        827 :                 cmd = NL80211_CMD_SET_BEACON;
    6765                 :            : 
    6766                 :       1082 :         nl80211_cmd(drv, msg, 0, cmd);
    6767                 :       1082 :         wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
    6768                 :       1082 :                     params->head, params->head_len);
    6769         [ +  - ]:       1082 :         NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
    6770                 :       1082 :         wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
    6771                 :       1082 :                     params->tail, params->tail_len);
    6772         [ +  - ]:       1082 :         NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
    6773                 :       1082 :         wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
    6774         [ +  - ]:       1082 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
    6775                 :       1082 :         wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
    6776         [ +  - ]:       1082 :         NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
    6777                 :       1082 :         wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
    6778         [ +  - ]:       1082 :         NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
    6779                 :       1082 :         wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
    6780                 :       1082 :                           params->ssid, params->ssid_len);
    6781         [ +  - ]:       1082 :         NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
    6782                 :            :                 params->ssid);
    6783 [ -  + ][ #  # ]:       1082 :         if (params->proberesp && params->proberesp_len) {
    6784                 :          0 :                 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
    6785                 :          0 :                             params->proberesp, params->proberesp_len);
    6786         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
    6787                 :            :                         params->proberesp);
    6788                 :            :         }
    6789   [ +  -  -  - ]:       1082 :         switch (params->hide_ssid) {
    6790                 :            :         case NO_SSID_HIDING:
    6791                 :       1082 :                 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
    6792         [ +  - ]:       1082 :                 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
    6793                 :            :                             NL80211_HIDDEN_SSID_NOT_IN_USE);
    6794                 :       1082 :                 break;
    6795                 :            :         case HIDDEN_SSID_ZERO_LEN:
    6796                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
    6797         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
    6798                 :            :                             NL80211_HIDDEN_SSID_ZERO_LEN);
    6799                 :          0 :                 break;
    6800                 :            :         case HIDDEN_SSID_ZERO_CONTENTS:
    6801                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
    6802         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
    6803                 :            :                             NL80211_HIDDEN_SSID_ZERO_CONTENTS);
    6804                 :          0 :                 break;
    6805                 :            :         }
    6806                 :       1082 :         wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
    6807         [ +  + ]:       1082 :         if (params->privacy)
    6808         [ +  - ]:       1018 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
    6809                 :       1082 :         wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
    6810         [ +  + ]:       1082 :         if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
    6811                 :            :             (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
    6812                 :            :                 /* Leave out the attribute */
    6813         [ -  + ]:        471 :         } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
    6814         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
    6815                 :            :                             NL80211_AUTHTYPE_SHARED_KEY);
    6816                 :            :         else
    6817         [ +  - ]:        471 :                 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
    6818                 :            :                             NL80211_AUTHTYPE_OPEN_SYSTEM);
    6819                 :            : 
    6820                 :       1082 :         wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
    6821                 :       1082 :         ver = 0;
    6822         [ +  + ]:       1082 :         if (params->wpa_version & WPA_PROTO_WPA)
    6823                 :         53 :                 ver |= NL80211_WPA_VERSION_1;
    6824         [ +  + ]:       1082 :         if (params->wpa_version & WPA_PROTO_RSN)
    6825                 :       1006 :                 ver |= NL80211_WPA_VERSION_2;
    6826         [ +  + ]:       1082 :         if (ver)
    6827         [ +  - ]:       1015 :                 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
    6828                 :            : 
    6829                 :       1082 :         wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
    6830                 :            :                    params->key_mgmt_suites);
    6831                 :       1082 :         num_suites = 0;
    6832         [ +  + ]:       1082 :         if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
    6833                 :        210 :                 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
    6834         [ +  + ]:       1082 :         if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
    6835                 :        774 :                 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
    6836         [ +  + ]:       1082 :         if (num_suites) {
    6837         [ +  - ]:        984 :                 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
    6838                 :            :                         num_suites * sizeof(u32), suites);
    6839                 :            :         }
    6840                 :            : 
    6841 [ +  + ][ -  + ]:       1082 :         if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
    6842                 :        210 :             params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
    6843         [ #  # ]:          0 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
    6844                 :            : 
    6845                 :       1082 :         wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
    6846                 :            :                    params->pairwise_ciphers);
    6847                 :       1082 :         num_suites = 0;
    6848         [ +  + ]:       1082 :         if (params->pairwise_ciphers & WPA_CIPHER_CCMP_256)
    6849                 :          3 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
    6850         [ +  + ]:       1082 :         if (params->pairwise_ciphers & WPA_CIPHER_GCMP_256)
    6851                 :          3 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
    6852         [ +  + ]:       1082 :         if (params->pairwise_ciphers & WPA_CIPHER_CCMP)
    6853                 :        994 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
    6854         [ +  + ]:       1082 :         if (params->pairwise_ciphers & WPA_CIPHER_GCMP)
    6855                 :          3 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
    6856         [ +  + ]:       1082 :         if (params->pairwise_ciphers & WPA_CIPHER_TKIP)
    6857                 :         32 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
    6858         [ -  + ]:       1082 :         if (params->pairwise_ciphers & WPA_CIPHER_WEP104)
    6859                 :          0 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
    6860         [ +  + ]:       1082 :         if (params->pairwise_ciphers & WPA_CIPHER_WEP40)
    6861                 :          3 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
    6862         [ +  + ]:       1082 :         if (num_suites) {
    6863         [ +  - ]:       1012 :                 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
    6864                 :            :                         num_suites * sizeof(u32), suites);
    6865                 :            :         }
    6866                 :            : 
    6867                 :       1082 :         wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
    6868                 :            :                    params->group_cipher);
    6869   [ +  +  +  +  :       1082 :         switch (params->group_cipher) {
             +  -  +  + ]
    6870                 :            :         case WPA_CIPHER_CCMP_256:
    6871         [ +  - ]:          3 :                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
    6872                 :            :                             WLAN_CIPHER_SUITE_CCMP_256);
    6873                 :          3 :                 break;
    6874                 :            :         case WPA_CIPHER_GCMP_256:
    6875         [ +  - ]:          3 :                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
    6876                 :            :                             WLAN_CIPHER_SUITE_GCMP_256);
    6877                 :          3 :                 break;
    6878                 :            :         case WPA_CIPHER_CCMP:
    6879         [ +  - ]:        950 :                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
    6880                 :            :                             WLAN_CIPHER_SUITE_CCMP);
    6881                 :        950 :                 break;
    6882                 :            :         case WPA_CIPHER_GCMP:
    6883         [ +  - ]:          3 :                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
    6884                 :            :                             WLAN_CIPHER_SUITE_GCMP);
    6885                 :          3 :                 break;
    6886                 :            :         case WPA_CIPHER_TKIP:
    6887         [ +  - ]:         56 :                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
    6888                 :            :                             WLAN_CIPHER_SUITE_TKIP);
    6889                 :         56 :                 break;
    6890                 :            :         case WPA_CIPHER_WEP104:
    6891         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
    6892                 :            :                             WLAN_CIPHER_SUITE_WEP104);
    6893                 :          0 :                 break;
    6894                 :            :         case WPA_CIPHER_WEP40:
    6895         [ +  - ]:          3 :                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
    6896                 :            :                             WLAN_CIPHER_SUITE_WEP40);
    6897                 :          3 :                 break;
    6898                 :            :         }
    6899                 :            : 
    6900         [ +  - ]:       1082 :         if (params->beacon_ies) {
    6901                 :       1082 :                 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
    6902                 :            :                                 params->beacon_ies);
    6903         [ +  - ]:       1082 :                 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
    6904                 :            :                         wpabuf_head(params->beacon_ies));
    6905                 :            :         }
    6906         [ +  - ]:       1082 :         if (params->proberesp_ies) {
    6907                 :       1082 :                 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
    6908                 :            :                                 params->proberesp_ies);
    6909         [ +  - ]:       1082 :                 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
    6910                 :            :                         wpabuf_len(params->proberesp_ies),
    6911                 :            :                         wpabuf_head(params->proberesp_ies));
    6912                 :            :         }
    6913         [ +  - ]:       1082 :         if (params->assocresp_ies) {
    6914                 :       1082 :                 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
    6915                 :            :                                 params->assocresp_ies);
    6916         [ +  - ]:       1082 :                 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
    6917                 :            :                         wpabuf_len(params->assocresp_ies),
    6918                 :            :                         wpabuf_head(params->assocresp_ies));
    6919                 :            :         }
    6920                 :            : 
    6921         [ -  + ]:       1082 :         if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)  {
    6922                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
    6923                 :            :                            params->ap_max_inactivity);
    6924         [ #  # ]:          0 :                 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
    6925                 :            :                             params->ap_max_inactivity);
    6926                 :            :         }
    6927                 :            : 
    6928                 :       1082 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    6929         [ -  + ]:       1082 :         if (ret) {
    6930                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
    6931                 :            :                            ret, strerror(-ret));
    6932                 :            :         } else {
    6933                 :       1082 :                 bss->beacon_set = 1;
    6934                 :       1082 :                 nl80211_set_bss(bss, params->cts_protect, params->preamble,
    6935                 :            :                                 params->short_slot_time, params->ht_opmode,
    6936                 :            :                                 params->isolate, params->basic_rates);
    6937                 :            :         }
    6938                 :       1082 :         return ret;
    6939                 :            :  nla_put_failure:
    6940                 :          0 :         nlmsg_free(msg);
    6941                 :       1082 :         return -ENOBUFS;
    6942                 :            : }
    6943                 :            : 
    6944                 :            : 
    6945                 :        293 : static int nl80211_put_freq_params(struct nl_msg *msg,
    6946                 :            :                                    struct hostapd_freq_params *freq)
    6947                 :            : {
    6948         [ +  - ]:        293 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
    6949         [ -  + ]:        293 :         if (freq->vht_enabled) {
    6950   [ #  #  #  #  :          0 :                 switch (freq->bandwidth) {
                      # ]
    6951                 :            :                 case 20:
    6952         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
    6953                 :            :                                     NL80211_CHAN_WIDTH_20);
    6954                 :          0 :                         break;
    6955                 :            :                 case 40:
    6956         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
    6957                 :            :                                     NL80211_CHAN_WIDTH_40);
    6958                 :          0 :                         break;
    6959                 :            :                 case 80:
    6960         [ #  # ]:          0 :                         if (freq->center_freq2)
    6961         [ #  # ]:          0 :                                 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
    6962                 :            :                                             NL80211_CHAN_WIDTH_80P80);
    6963                 :            :                         else
    6964         [ #  # ]:          0 :                                 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
    6965                 :            :                                             NL80211_CHAN_WIDTH_80);
    6966                 :          0 :                         break;
    6967                 :            :                 case 160:
    6968         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
    6969                 :            :                                     NL80211_CHAN_WIDTH_160);
    6970                 :          0 :                         break;
    6971                 :            :                 default:
    6972                 :          0 :                         return -EINVAL;
    6973                 :            :                 }
    6974         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
    6975         [ #  # ]:          0 :                 if (freq->center_freq2)
    6976         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
    6977                 :            :                                     freq->center_freq2);
    6978         [ +  + ]:        293 :         } else if (freq->ht_enabled) {
    6979      [ +  +  + ]:        238 :                 switch (freq->sec_channel_offset) {
    6980                 :            :                 case -1:
    6981         [ +  - ]:          1 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
    6982                 :            :                                     NL80211_CHAN_HT40MINUS);
    6983                 :          1 :                         break;
    6984                 :            :                 case 1:
    6985         [ +  - ]:          3 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
    6986                 :            :                                     NL80211_CHAN_HT40PLUS);
    6987                 :          3 :                         break;
    6988                 :            :                 default:
    6989         [ +  - ]:        234 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
    6990                 :            :                                     NL80211_CHAN_HT20);
    6991                 :        234 :                         break;
    6992                 :            :                 }
    6993                 :            :         }
    6994                 :        293 :         return 0;
    6995                 :            : 
    6996                 :            : nla_put_failure:
    6997                 :        293 :         return -ENOBUFS;
    6998                 :            : }
    6999                 :            : 
    7000                 :            : 
    7001                 :        293 : static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
    7002                 :            :                                        struct hostapd_freq_params *freq)
    7003                 :            : {
    7004                 :        293 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7005                 :            :         struct nl_msg *msg;
    7006                 :            :         int ret;
    7007                 :            : 
    7008                 :        293 :         wpa_printf(MSG_DEBUG,
    7009                 :            :                    "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
    7010                 :            :                    freq->freq, freq->ht_enabled, freq->vht_enabled,
    7011                 :            :                    freq->bandwidth, freq->center_freq1, freq->center_freq2);
    7012                 :        293 :         msg = nlmsg_alloc();
    7013         [ -  + ]:        293 :         if (!msg)
    7014                 :          0 :                 return -1;
    7015                 :            : 
    7016                 :        293 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
    7017                 :            : 
    7018         [ +  - ]:        293 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    7019         [ -  + ]:        293 :         if (nl80211_put_freq_params(msg, freq) < 0)
    7020                 :          0 :                 goto nla_put_failure;
    7021                 :            : 
    7022                 :        293 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    7023                 :        293 :         msg = NULL;
    7024         [ +  - ]:        293 :         if (ret == 0) {
    7025                 :        293 :                 bss->freq = freq->freq;
    7026                 :        293 :                 return 0;
    7027                 :            :         }
    7028                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
    7029                 :            :                    "%d (%s)", freq->freq, ret, strerror(-ret));
    7030                 :            : nla_put_failure:
    7031                 :          0 :         nlmsg_free(msg);
    7032                 :        293 :         return -1;
    7033                 :            : }
    7034                 :            : 
    7035                 :            : 
    7036                 :       2863 : static u32 sta_flags_nl80211(int flags)
    7037                 :            : {
    7038                 :       2863 :         u32 f = 0;
    7039                 :            : 
    7040         [ +  + ]:       2863 :         if (flags & WPA_STA_AUTHORIZED)
    7041                 :       1398 :                 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
    7042         [ +  + ]:       2863 :         if (flags & WPA_STA_WMM)
    7043                 :       1224 :                 f |= BIT(NL80211_STA_FLAG_WME);
    7044         [ +  + ]:       2863 :         if (flags & WPA_STA_SHORT_PREAMBLE)
    7045                 :       1224 :                 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
    7046         [ +  + ]:       2863 :         if (flags & WPA_STA_MFP)
    7047                 :        438 :                 f |= BIT(NL80211_STA_FLAG_MFP);
    7048         [ +  + ]:       2863 :         if (flags & WPA_STA_TDLS_PEER)
    7049                 :         81 :                 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
    7050                 :            : 
    7051                 :       2863 :         return f;
    7052                 :            : }
    7053                 :            : 
    7054                 :            : 
    7055                 :        489 : static int wpa_driver_nl80211_sta_add(void *priv,
    7056                 :            :                                       struct hostapd_sta_add_params *params)
    7057                 :            : {
    7058                 :        489 :         struct i802_bss *bss = priv;
    7059                 :        489 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7060                 :            :         struct nl_msg *msg;
    7061                 :            :         struct nl80211_sta_flag_update upd;
    7062                 :        489 :         int ret = -ENOBUFS;
    7063                 :            : 
    7064 [ +  + ][ -  + ]:        489 :         if ((params->flags & WPA_STA_TDLS_PEER) &&
    7065                 :         81 :             !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
    7066                 :          0 :                 return -EOPNOTSUPP;
    7067                 :            : 
    7068                 :        489 :         msg = nlmsg_alloc();
    7069         [ -  + ]:        489 :         if (!msg)
    7070                 :          0 :                 return -ENOMEM;
    7071                 :            : 
    7072         [ +  + ]:        489 :         wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
    7073                 :       3423 :                    params->set ? "Set" : "Add", MAC2STR(params->addr));
    7074         [ +  + ]:        489 :         nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
    7075                 :            :                     NL80211_CMD_NEW_STATION);
    7076                 :            : 
    7077         [ +  - ]:        489 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
    7078         [ +  - ]:        489 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
    7079         [ +  - ]:        489 :         NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
    7080                 :            :                 params->supp_rates);
    7081                 :        489 :         wpa_hexdump(MSG_DEBUG, "  * supported rates", params->supp_rates,
    7082                 :            :                     params->supp_rates_len);
    7083         [ +  + ]:        489 :         if (!params->set) {
    7084         [ +  + ]:        451 :                 if (params->aid) {
    7085                 :        408 :                         wpa_printf(MSG_DEBUG, "  * aid=%u", params->aid);
    7086         [ +  - ]:        408 :                         NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
    7087                 :            :                 } else {
    7088                 :            :                         /*
    7089                 :            :                          * cfg80211 validates that AID is non-zero, so we have
    7090                 :            :                          * to make this a non-zero value for the TDLS case where
    7091                 :            :                          * a dummy STA entry is used for now.
    7092                 :            :                          */
    7093                 :         43 :                         wpa_printf(MSG_DEBUG, "  * aid=1 (TDLS workaround)");
    7094         [ +  - ]:         43 :                         NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
    7095                 :            :                 }
    7096                 :        451 :                 wpa_printf(MSG_DEBUG, "  * listen_interval=%u",
    7097                 :        451 :                            params->listen_interval);
    7098         [ +  - ]:        451 :                 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
    7099                 :            :                             params->listen_interval);
    7100 [ -  + ][ #  # ]:         38 :         } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
    7101                 :          0 :                 wpa_printf(MSG_DEBUG, "  * peer_aid=%u", params->aid);
    7102         [ #  # ]:          0 :                 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
    7103                 :            :         }
    7104         [ +  + ]:        489 :         if (params->ht_capabilities) {
    7105                 :        207 :                 wpa_hexdump(MSG_DEBUG, "  * ht_capabilities",
    7106                 :        207 :                             (u8 *) params->ht_capabilities,
    7107                 :            :                             sizeof(*params->ht_capabilities));
    7108         [ +  - ]:        207 :                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
    7109                 :            :                         sizeof(*params->ht_capabilities),
    7110                 :            :                         params->ht_capabilities);
    7111                 :            :         }
    7112                 :            : 
    7113         [ -  + ]:        489 :         if (params->vht_capabilities) {
    7114                 :          0 :                 wpa_hexdump(MSG_DEBUG, "  * vht_capabilities",
    7115                 :          0 :                             (u8 *) params->vht_capabilities,
    7116                 :            :                             sizeof(*params->vht_capabilities));
    7117         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
    7118                 :            :                         sizeof(*params->vht_capabilities),
    7119                 :            :                         params->vht_capabilities);
    7120                 :            :         }
    7121                 :            : 
    7122                 :        489 :         wpa_printf(MSG_DEBUG, "  * capability=0x%x", params->capability);
    7123         [ +  - ]:        489 :         NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
    7124                 :            : 
    7125         [ +  + ]:        489 :         if (params->ext_capab) {
    7126                 :         38 :                 wpa_hexdump(MSG_DEBUG, "  * ext_capab",
    7127                 :         38 :                             params->ext_capab, params->ext_capab_len);
    7128         [ +  - ]:         38 :                 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
    7129                 :            :                         params->ext_capab_len, params->ext_capab);
    7130                 :            :         }
    7131                 :            : 
    7132                 :        489 :         os_memset(&upd, 0, sizeof(upd));
    7133                 :        489 :         upd.mask = sta_flags_nl80211(params->flags);
    7134                 :        489 :         upd.set = upd.mask;
    7135                 :        489 :         wpa_printf(MSG_DEBUG, "  * flags set=0x%x mask=0x%x",
    7136                 :            :                    upd.set, upd.mask);
    7137         [ +  - ]:        489 :         NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
    7138                 :            : 
    7139         [ +  + ]:        489 :         if (params->flags & WPA_STA_WMM) {
    7140                 :        408 :                 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
    7141                 :            : 
    7142         [ -  + ]:        408 :                 if (!wme)
    7143                 :          0 :                         goto nla_put_failure;
    7144                 :            : 
    7145                 :        408 :                 wpa_printf(MSG_DEBUG, "  * qosinfo=0x%x", params->qosinfo);
    7146         [ +  - ]:        408 :                 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
    7147                 :            :                                 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
    7148         [ +  - ]:        408 :                 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
    7149                 :            :                                 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
    7150                 :            :                                 WMM_QOSINFO_STA_SP_MASK);
    7151                 :        408 :                 nla_nest_end(msg, wme);
    7152                 :            :         }
    7153                 :            : 
    7154                 :        489 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    7155                 :        489 :         msg = NULL;
    7156         [ -  + ]:        489 :         if (ret)
    7157         [ #  # ]:          0 :                 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
    7158                 :          0 :                            "result: %d (%s)", params->set ? "SET" : "NEW", ret,
    7159                 :            :                            strerror(-ret));
    7160         [ -  + ]:        489 :         if (ret == -EEXIST)
    7161                 :          0 :                 ret = 0;
    7162                 :            :  nla_put_failure:
    7163                 :        489 :         nlmsg_free(msg);
    7164                 :        489 :         return ret;
    7165                 :            : }
    7166                 :            : 
    7167                 :            : 
    7168                 :        812 : static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
    7169                 :            : {
    7170                 :        812 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7171                 :            :         struct nl_msg *msg;
    7172                 :            :         int ret;
    7173                 :            : 
    7174                 :        812 :         msg = nlmsg_alloc();
    7175         [ -  + ]:        812 :         if (!msg)
    7176                 :          0 :                 return -ENOMEM;
    7177                 :            : 
    7178                 :        812 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
    7179                 :            : 
    7180         [ +  - ]:        812 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
    7181                 :            :                     if_nametoindex(bss->ifname));
    7182         [ +  - ]:        812 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    7183                 :            : 
    7184                 :        812 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    7185                 :        812 :         wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
    7186                 :            :                    " --> %d (%s)",
    7187                 :       5684 :                    bss->ifname, MAC2STR(addr), ret, strerror(-ret));
    7188         [ +  + ]:        812 :         if (ret == -ENOENT)
    7189                 :        405 :                 return 0;
    7190                 :        407 :         return ret;
    7191                 :            :  nla_put_failure:
    7192                 :          0 :         nlmsg_free(msg);
    7193                 :        812 :         return -ENOBUFS;
    7194                 :            : }
    7195                 :            : 
    7196                 :            : 
    7197                 :         36 : static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
    7198                 :            :                                  int ifidx)
    7199                 :            : {
    7200                 :            :         struct nl_msg *msg;
    7201                 :            : 
    7202                 :         36 :         wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
    7203                 :            : 
    7204                 :            :         /* stop listening for EAPOL on this interface */
    7205                 :         36 :         del_ifidx(drv, ifidx);
    7206                 :            : 
    7207                 :         36 :         msg = nlmsg_alloc();
    7208         [ -  + ]:         36 :         if (!msg)
    7209                 :          0 :                 goto nla_put_failure;
    7210                 :            : 
    7211                 :         36 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
    7212         [ +  - ]:         36 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
    7213                 :            : 
    7214         [ +  - ]:         36 :         if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
    7215                 :         36 :                 return;
    7216                 :          0 :         msg = NULL;
    7217                 :            :  nla_put_failure:
    7218                 :          0 :         nlmsg_free(msg);
    7219                 :          0 :         wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
    7220                 :            : }
    7221                 :            : 
    7222                 :            : 
    7223                 :        674 : static const char * nl80211_iftype_str(enum nl80211_iftype mode)
    7224                 :            : {
    7225   [ +  +  +  -  :        674 :         switch (mode) {
          -  -  -  +  +  
                   -  - ]
    7226                 :            :         case NL80211_IFTYPE_ADHOC:
    7227                 :          6 :                 return "ADHOC";
    7228                 :            :         case NL80211_IFTYPE_STATION:
    7229                 :        317 :                 return "STATION";
    7230                 :            :         case NL80211_IFTYPE_AP:
    7231                 :        207 :                 return "AP";
    7232                 :            :         case NL80211_IFTYPE_AP_VLAN:
    7233                 :          0 :                 return "AP_VLAN";
    7234                 :            :         case NL80211_IFTYPE_WDS:
    7235                 :          0 :                 return "WDS";
    7236                 :            :         case NL80211_IFTYPE_MONITOR:
    7237                 :          0 :                 return "MONITOR";
    7238                 :            :         case NL80211_IFTYPE_MESH_POINT:
    7239                 :          0 :                 return "MESH_POINT";
    7240                 :            :         case NL80211_IFTYPE_P2P_CLIENT:
    7241                 :         71 :                 return "P2P_CLIENT";
    7242                 :            :         case NL80211_IFTYPE_P2P_GO:
    7243                 :         73 :                 return "P2P_GO";
    7244                 :            :         case NL80211_IFTYPE_P2P_DEVICE:
    7245                 :          0 :                 return "P2P_DEVICE";
    7246                 :            :         default:
    7247                 :        674 :                 return "unknown";
    7248                 :            :         }
    7249                 :            : }
    7250                 :            : 
    7251                 :            : 
    7252                 :         36 : static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
    7253                 :            :                                      const char *ifname,
    7254                 :            :                                      enum nl80211_iftype iftype,
    7255                 :            :                                      const u8 *addr, int wds,
    7256                 :            :                                      int (*handler)(struct nl_msg *, void *),
    7257                 :            :                                      void *arg)
    7258                 :            : {
    7259                 :            :         struct nl_msg *msg;
    7260                 :            :         int ifidx;
    7261                 :         36 :         int ret = -ENOBUFS;
    7262                 :            : 
    7263                 :         36 :         wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
    7264                 :            :                    iftype, nl80211_iftype_str(iftype));
    7265                 :            : 
    7266                 :         36 :         msg = nlmsg_alloc();
    7267         [ -  + ]:         36 :         if (!msg)
    7268                 :          0 :                 return -1;
    7269                 :            : 
    7270                 :         36 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
    7271         [ -  + ]:         36 :         if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
    7272                 :          0 :                 goto nla_put_failure;
    7273         [ +  - ]:         36 :         NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
    7274         [ +  - ]:         36 :         NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
    7275                 :            : 
    7276         [ -  + ]:         36 :         if (iftype == NL80211_IFTYPE_MONITOR) {
    7277                 :            :                 struct nlattr *flags;
    7278                 :            : 
    7279                 :          0 :                 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
    7280         [ #  # ]:          0 :                 if (!flags)
    7281                 :          0 :                         goto nla_put_failure;
    7282                 :            : 
    7283         [ #  # ]:          0 :                 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
    7284                 :            : 
    7285                 :          0 :                 nla_nest_end(msg, flags);
    7286         [ -  + ]:         36 :         } else if (wds) {
    7287         [ #  # ]:          0 :                 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
    7288                 :            :         }
    7289                 :            : 
    7290                 :         36 :         ret = send_and_recv_msgs(drv, msg, handler, arg);
    7291                 :         36 :         msg = NULL;
    7292         [ -  + ]:         36 :         if (ret) {
    7293                 :            :  nla_put_failure:
    7294                 :          0 :                 nlmsg_free(msg);
    7295                 :          0 :                 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
    7296                 :            :                            ifname, ret, strerror(-ret));
    7297                 :          0 :                 return ret;
    7298                 :            :         }
    7299                 :            : 
    7300         [ -  + ]:         36 :         if (iftype == NL80211_IFTYPE_P2P_DEVICE)
    7301                 :          0 :                 return 0;
    7302                 :            : 
    7303                 :         36 :         ifidx = if_nametoindex(ifname);
    7304                 :         36 :         wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
    7305                 :            :                    ifname, ifidx);
    7306                 :            : 
    7307         [ -  + ]:         36 :         if (ifidx <= 0)
    7308                 :          0 :                 return -1;
    7309                 :            : 
    7310                 :            :         /* start listening for EAPOL on this interface */
    7311                 :         36 :         add_ifidx(drv, ifidx);
    7312                 :            : 
    7313   [ +  -  -  + ]:         53 :         if (addr && iftype != NL80211_IFTYPE_MONITOR &&
                 [ +  + ]
    7314                 :         17 :             linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
    7315                 :          0 :                 nl80211_remove_iface(drv, ifidx);
    7316                 :          0 :                 return -1;
    7317                 :            :         }
    7318                 :            : 
    7319                 :         36 :         return ifidx;
    7320                 :            : }
    7321                 :            : 
    7322                 :            : 
    7323                 :         36 : static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
    7324                 :            :                                 const char *ifname, enum nl80211_iftype iftype,
    7325                 :            :                                 const u8 *addr, int wds,
    7326                 :            :                                 int (*handler)(struct nl_msg *, void *),
    7327                 :            :                                 void *arg, int use_existing)
    7328                 :            : {
    7329                 :            :         int ret;
    7330                 :            : 
    7331                 :         36 :         ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
    7332                 :            :                                         arg);
    7333                 :            : 
    7334                 :            :         /* if error occurred and interface exists already */
    7335 [ #  # ][ -  + ]:         36 :         if (ret == -ENFILE && if_nametoindex(ifname)) {
    7336         [ #  # ]:          0 :                 if (use_existing) {
    7337                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
    7338                 :            :                                    ifname);
    7339                 :          0 :                         return -ENFILE;
    7340                 :            :                 }
    7341                 :          0 :                 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
    7342                 :            : 
    7343                 :            :                 /* Try to remove the interface that was already there. */
    7344                 :          0 :                 nl80211_remove_iface(drv, if_nametoindex(ifname));
    7345                 :            : 
    7346                 :            :                 /* Try to create the interface again */
    7347                 :          0 :                 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
    7348                 :            :                                                 wds, handler, arg);
    7349                 :            :         }
    7350                 :            : 
    7351 [ +  - ][ +  + ]:         36 :         if (ret >= 0 && is_p2p_net_interface(iftype))
    7352                 :         19 :                 nl80211_disable_11b_rates(drv, ret, 1);
    7353                 :            : 
    7354                 :         36 :         return ret;
    7355                 :            : }
    7356                 :            : 
    7357                 :            : 
    7358                 :          0 : static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
    7359                 :            : {
    7360                 :            :         struct ieee80211_hdr *hdr;
    7361                 :            :         u16 fc;
    7362                 :            :         union wpa_event_data event;
    7363                 :            : 
    7364                 :          0 :         hdr = (struct ieee80211_hdr *) buf;
    7365                 :          0 :         fc = le_to_host16(hdr->frame_control);
    7366                 :            : 
    7367                 :          0 :         os_memset(&event, 0, sizeof(event));
    7368                 :          0 :         event.tx_status.type = WLAN_FC_GET_TYPE(fc);
    7369                 :          0 :         event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
    7370                 :          0 :         event.tx_status.dst = hdr->addr1;
    7371                 :          0 :         event.tx_status.data = buf;
    7372                 :          0 :         event.tx_status.data_len = len;
    7373                 :          0 :         event.tx_status.ack = ok;
    7374                 :          0 :         wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
    7375                 :          0 : }
    7376                 :            : 
    7377                 :            : 
    7378                 :          0 : static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
    7379                 :            :                              u8 *buf, size_t len)
    7380                 :            : {
    7381                 :          0 :         struct ieee80211_hdr *hdr = (void *)buf;
    7382                 :            :         u16 fc;
    7383                 :            :         union wpa_event_data event;
    7384                 :            : 
    7385         [ #  # ]:          0 :         if (len < sizeof(*hdr))
    7386                 :          0 :                 return;
    7387                 :            : 
    7388                 :          0 :         fc = le_to_host16(hdr->frame_control);
    7389                 :            : 
    7390                 :          0 :         os_memset(&event, 0, sizeof(event));
    7391                 :          0 :         event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
    7392                 :          0 :         event.rx_from_unknown.addr = hdr->addr2;
    7393                 :          0 :         event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
    7394                 :            :                 (WLAN_FC_FROMDS | WLAN_FC_TODS);
    7395                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
    7396                 :            : }
    7397                 :            : 
    7398                 :            : 
    7399                 :          0 : static void handle_frame(struct wpa_driver_nl80211_data *drv,
    7400                 :            :                          u8 *buf, size_t len, int datarate, int ssi_signal)
    7401                 :            : {
    7402                 :            :         struct ieee80211_hdr *hdr;
    7403                 :            :         u16 fc;
    7404                 :            :         union wpa_event_data event;
    7405                 :            : 
    7406                 :          0 :         hdr = (struct ieee80211_hdr *) buf;
    7407                 :          0 :         fc = le_to_host16(hdr->frame_control);
    7408                 :            : 
    7409   [ #  #  #  # ]:          0 :         switch (WLAN_FC_GET_TYPE(fc)) {
    7410                 :            :         case WLAN_FC_TYPE_MGMT:
    7411                 :          0 :                 os_memset(&event, 0, sizeof(event));
    7412                 :          0 :                 event.rx_mgmt.frame = buf;
    7413                 :          0 :                 event.rx_mgmt.frame_len = len;
    7414                 :          0 :                 event.rx_mgmt.datarate = datarate;
    7415                 :          0 :                 event.rx_mgmt.ssi_signal = ssi_signal;
    7416                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
    7417                 :          0 :                 break;
    7418                 :            :         case WLAN_FC_TYPE_CTRL:
    7419                 :            :                 /* can only get here with PS-Poll frames */
    7420                 :          0 :                 wpa_printf(MSG_DEBUG, "CTRL");
    7421                 :          0 :                 from_unknown_sta(drv, buf, len);
    7422                 :          0 :                 break;
    7423                 :            :         case WLAN_FC_TYPE_DATA:
    7424                 :          0 :                 from_unknown_sta(drv, buf, len);
    7425                 :          0 :                 break;
    7426                 :            :         }
    7427                 :          0 : }
    7428                 :            : 
    7429                 :            : 
    7430                 :          0 : static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
    7431                 :            : {
    7432                 :          0 :         struct wpa_driver_nl80211_data *drv = eloop_ctx;
    7433                 :            :         int len;
    7434                 :            :         unsigned char buf[3000];
    7435                 :            :         struct ieee80211_radiotap_iterator iter;
    7436                 :            :         int ret;
    7437                 :          0 :         int datarate = 0, ssi_signal = 0;
    7438                 :          0 :         int injected = 0, failed = 0, rxflags = 0;
    7439                 :            : 
    7440                 :          0 :         len = recv(sock, buf, sizeof(buf), 0);
    7441         [ #  # ]:          0 :         if (len < 0) {
    7442                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
    7443                 :          0 :                            strerror(errno));
    7444                 :          0 :                 return;
    7445                 :            :         }
    7446                 :            : 
    7447         [ #  # ]:          0 :         if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
    7448                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
    7449                 :          0 :                 return;
    7450                 :            :         }
    7451                 :            : 
    7452                 :            :         while (1) {
    7453                 :          0 :                 ret = ieee80211_radiotap_iterator_next(&iter);
    7454         [ #  # ]:          0 :                 if (ret == -ENOENT)
    7455                 :          0 :                         break;
    7456         [ #  # ]:          0 :                 if (ret) {
    7457                 :          0 :                         wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
    7458                 :            :                                    ret);
    7459                 :          0 :                         return;
    7460                 :            :                 }
    7461   [ #  #  #  #  :          0 :                 switch (iter.this_arg_index) {
             #  #  #  # ]
    7462                 :            :                 case IEEE80211_RADIOTAP_FLAGS:
    7463         [ #  # ]:          0 :                         if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
    7464                 :          0 :                                 len -= 4;
    7465                 :          0 :                         break;
    7466                 :            :                 case IEEE80211_RADIOTAP_RX_FLAGS:
    7467                 :          0 :                         rxflags = 1;
    7468                 :          0 :                         break;
    7469                 :            :                 case IEEE80211_RADIOTAP_TX_FLAGS:
    7470                 :          0 :                         injected = 1;
    7471                 :          0 :                         failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
    7472                 :            :                                         IEEE80211_RADIOTAP_F_TX_FAIL;
    7473                 :          0 :                         break;
    7474                 :            :                 case IEEE80211_RADIOTAP_DATA_RETRIES:
    7475                 :          0 :                         break;
    7476                 :            :                 case IEEE80211_RADIOTAP_CHANNEL:
    7477                 :            :                         /* TODO: convert from freq/flags to channel number */
    7478                 :          0 :                         break;
    7479                 :            :                 case IEEE80211_RADIOTAP_RATE:
    7480                 :          0 :                         datarate = *iter.this_arg * 5;
    7481                 :          0 :                         break;
    7482                 :            :                 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
    7483                 :          0 :                         ssi_signal = (s8) *iter.this_arg;
    7484                 :          0 :                         break;
    7485                 :            :                 }
    7486                 :          0 :         }
    7487                 :            : 
    7488 [ #  # ][ #  # ]:          0 :         if (rxflags && injected)
    7489                 :          0 :                 return;
    7490                 :            : 
    7491         [ #  # ]:          0 :         if (!injected)
    7492                 :          0 :                 handle_frame(drv, buf + iter.max_length,
    7493                 :          0 :                              len - iter.max_length, datarate, ssi_signal);
    7494                 :            :         else
    7495                 :          0 :                 handle_tx_callback(drv->ctx, buf + iter.max_length,
    7496                 :          0 :                                    len - iter.max_length, !failed);
    7497                 :            : }
    7498                 :            : 
    7499                 :            : 
    7500                 :            : /*
    7501                 :            :  * we post-process the filter code later and rewrite
    7502                 :            :  * this to the offset to the last instruction
    7503                 :            :  */
    7504                 :            : #define PASS    0xFF
    7505                 :            : #define FAIL    0xFE
    7506                 :            : 
    7507                 :            : static struct sock_filter msock_filter_insns[] = {
    7508                 :            :         /*
    7509                 :            :          * do a little-endian load of the radiotap length field
    7510                 :            :          */
    7511                 :            :         /* load lower byte into A */
    7512                 :            :         BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 2),
    7513                 :            :         /* put it into X (== index register) */
    7514                 :            :         BPF_STMT(BPF_MISC| BPF_TAX, 0),
    7515                 :            :         /* load upper byte into A */
    7516                 :            :         BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 3),
    7517                 :            :         /* left-shift it by 8 */
    7518                 :            :         BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
    7519                 :            :         /* or with X */
    7520                 :            :         BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
    7521                 :            :         /* put result into X */
    7522                 :            :         BPF_STMT(BPF_MISC| BPF_TAX, 0),
    7523                 :            : 
    7524                 :            :         /*
    7525                 :            :          * Allow management frames through, this also gives us those
    7526                 :            :          * management frames that we sent ourselves with status
    7527                 :            :          */
    7528                 :            :         /* load the lower byte of the IEEE 802.11 frame control field */
    7529                 :            :         BPF_STMT(BPF_LD  | BPF_B | BPF_IND, 0),
    7530                 :            :         /* mask off frame type and version */
    7531                 :            :         BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
    7532                 :            :         /* accept frame if it's both 0, fall through otherwise */
    7533                 :            :         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
    7534                 :            : 
    7535                 :            :         /*
    7536                 :            :          * TODO: add a bit to radiotap RX flags that indicates
    7537                 :            :          * that the sending station is not associated, then
    7538                 :            :          * add a filter here that filters on our DA and that flag
    7539                 :            :          * to allow us to deauth frames to that bad station.
    7540                 :            :          *
    7541                 :            :          * For now allow all To DS data frames through.
    7542                 :            :          */
    7543                 :            :         /* load the IEEE 802.11 frame control field */
    7544                 :            :         BPF_STMT(BPF_LD  | BPF_H | BPF_IND, 0),
    7545                 :            :         /* mask off frame type, version and DS status */
    7546                 :            :         BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
    7547                 :            :         /* accept frame if version 0, type 2 and To DS, fall through otherwise
    7548                 :            :          */
    7549                 :            :         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
    7550                 :            : 
    7551                 :            : #if 0
    7552                 :            :         /*
    7553                 :            :          * drop non-data frames
    7554                 :            :          */
    7555                 :            :         /* load the lower byte of the frame control field */
    7556                 :            :         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
    7557                 :            :         /* mask off QoS bit */
    7558                 :            :         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x0c),
    7559                 :            :         /* drop non-data frames */
    7560                 :            :         BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 8, 0, FAIL),
    7561                 :            : #endif
    7562                 :            :         /* load the upper byte of the frame control field */
    7563                 :            :         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 1),
    7564                 :            :         /* mask off toDS/fromDS */
    7565                 :            :         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x03),
    7566                 :            :         /* accept WDS frames */
    7567                 :            :         BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 3, PASS, 0),
    7568                 :            : 
    7569                 :            :         /*
    7570                 :            :          * add header length to index
    7571                 :            :          */
    7572                 :            :         /* load the lower byte of the frame control field */
    7573                 :            :         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
    7574                 :            :         /* mask off QoS bit */
    7575                 :            :         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x80),
    7576                 :            :         /* right shift it by 6 to give 0 or 2 */
    7577                 :            :         BPF_STMT(BPF_ALU  | BPF_RSH | BPF_K, 6),
    7578                 :            :         /* add data frame header length */
    7579                 :            :         BPF_STMT(BPF_ALU  | BPF_ADD | BPF_K, 24),
    7580                 :            :         /* add index, was start of 802.11 header */
    7581                 :            :         BPF_STMT(BPF_ALU  | BPF_ADD | BPF_X, 0),
    7582                 :            :         /* move to index, now start of LL header */
    7583                 :            :         BPF_STMT(BPF_MISC | BPF_TAX, 0),
    7584                 :            : 
    7585                 :            :         /*
    7586                 :            :          * Accept empty data frames, we use those for
    7587                 :            :          * polling activity.
    7588                 :            :          */
    7589                 :            :         BPF_STMT(BPF_LD  | BPF_W | BPF_LEN, 0),
    7590                 :            :         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
    7591                 :            : 
    7592                 :            :         /*
    7593                 :            :          * Accept EAPOL frames
    7594                 :            :          */
    7595                 :            :         BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 0),
    7596                 :            :         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
    7597                 :            :         BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 4),
    7598                 :            :         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
    7599                 :            : 
    7600                 :            :         /* keep these last two statements or change the code below */
    7601                 :            :         /* return 0 == "DROP" */
    7602                 :            :         BPF_STMT(BPF_RET | BPF_K, 0),
    7603                 :            :         /* return ~0 == "keep all" */
    7604                 :            :         BPF_STMT(BPF_RET | BPF_K, ~0),
    7605                 :            : };
    7606                 :            : 
    7607                 :            : static struct sock_fprog msock_filter = {
    7608                 :            :         .len = ARRAY_SIZE(msock_filter_insns),
    7609                 :            :         .filter = msock_filter_insns,
    7610                 :            : };
    7611                 :            : 
    7612                 :            : 
    7613                 :          0 : static int add_monitor_filter(int s)
    7614                 :            : {
    7615                 :            :         int idx;
    7616                 :            : 
    7617                 :            :         /* rewrite all PASS/FAIL jump offsets */
    7618         [ #  # ]:          0 :         for (idx = 0; idx < msock_filter.len; idx++) {
    7619                 :          0 :                 struct sock_filter *insn = &msock_filter_insns[idx];
    7620                 :            : 
    7621         [ #  # ]:          0 :                 if (BPF_CLASS(insn->code) == BPF_JMP) {
    7622         [ #  # ]:          0 :                         if (insn->code == (BPF_JMP|BPF_JA)) {
    7623         [ #  # ]:          0 :                                 if (insn->k == PASS)
    7624                 :          0 :                                         insn->k = msock_filter.len - idx - 2;
    7625         [ #  # ]:          0 :                                 else if (insn->k == FAIL)
    7626                 :          0 :                                         insn->k = msock_filter.len - idx - 3;
    7627                 :            :                         }
    7628                 :            : 
    7629         [ #  # ]:          0 :                         if (insn->jt == PASS)
    7630                 :          0 :                                 insn->jt = msock_filter.len - idx - 2;
    7631         [ #  # ]:          0 :                         else if (insn->jt == FAIL)
    7632                 :          0 :                                 insn->jt = msock_filter.len - idx - 3;
    7633                 :            : 
    7634         [ #  # ]:          0 :                         if (insn->jf == PASS)
    7635                 :          0 :                                 insn->jf = msock_filter.len - idx - 2;
    7636         [ #  # ]:          0 :                         else if (insn->jf == FAIL)
    7637                 :          0 :                                 insn->jf = msock_filter.len - idx - 3;
    7638                 :            :                 }
    7639                 :            :         }
    7640                 :            : 
    7641         [ #  # ]:          0 :         if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
    7642                 :            :                        &msock_filter, sizeof(msock_filter))) {
    7643                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
    7644                 :          0 :                            strerror(errno));
    7645                 :          0 :                 return -1;
    7646                 :            :         }
    7647                 :            : 
    7648                 :          0 :         return 0;
    7649                 :            : }
    7650                 :            : 
    7651                 :            : 
    7652                 :        212 : static void nl80211_remove_monitor_interface(
    7653                 :            :         struct wpa_driver_nl80211_data *drv)
    7654                 :            : {
    7655         [ -  + ]:        212 :         if (drv->monitor_refcount > 0)
    7656                 :          0 :                 drv->monitor_refcount--;
    7657                 :        212 :         wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
    7658                 :            :                    drv->monitor_refcount);
    7659         [ -  + ]:        212 :         if (drv->monitor_refcount > 0)
    7660                 :        212 :                 return;
    7661                 :            : 
    7662         [ -  + ]:        212 :         if (drv->monitor_ifidx >= 0) {
    7663                 :          0 :                 nl80211_remove_iface(drv, drv->monitor_ifidx);
    7664                 :          0 :                 drv->monitor_ifidx = -1;
    7665                 :            :         }
    7666         [ -  + ]:        212 :         if (drv->monitor_sock >= 0) {
    7667                 :          0 :                 eloop_unregister_read_sock(drv->monitor_sock);
    7668                 :          0 :                 close(drv->monitor_sock);
    7669                 :          0 :                 drv->monitor_sock = -1;
    7670                 :            :         }
    7671                 :            : }
    7672                 :            : 
    7673                 :            : 
    7674                 :            : static int
    7675                 :          0 : nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
    7676                 :            : {
    7677                 :            :         char buf[IFNAMSIZ];
    7678                 :            :         struct sockaddr_ll ll;
    7679                 :            :         int optval;
    7680                 :            :         socklen_t optlen;
    7681                 :            : 
    7682         [ #  # ]:          0 :         if (drv->monitor_ifidx >= 0) {
    7683                 :          0 :                 drv->monitor_refcount++;
    7684                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
    7685                 :            :                            drv->monitor_refcount);
    7686                 :          0 :                 return 0;
    7687                 :            :         }
    7688                 :            : 
    7689         [ #  # ]:          0 :         if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
    7690                 :            :                 /*
    7691                 :            :                  * P2P interface name is of the format p2p-%s-%d. For monitor
    7692                 :            :                  * interface name corresponding to P2P GO, replace "p2p-" with
    7693                 :            :                  * "mon-" to retain the same interface name length and to
    7694                 :            :                  * indicate that it is a monitor interface.
    7695                 :            :                  */
    7696                 :          0 :                 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
    7697                 :            :         } else {
    7698                 :            :                 /* Non-P2P interface with AP functionality. */
    7699                 :          0 :                 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
    7700                 :            :         }
    7701                 :            : 
    7702                 :          0 :         buf[IFNAMSIZ - 1] = '\0';
    7703                 :            : 
    7704                 :          0 :         drv->monitor_ifidx =
    7705                 :          0 :                 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
    7706                 :            :                                      0, NULL, NULL, 0);
    7707                 :            : 
    7708         [ #  # ]:          0 :         if (drv->monitor_ifidx == -EOPNOTSUPP) {
    7709                 :            :                 /*
    7710                 :            :                  * This is backward compatibility for a few versions of
    7711                 :            :                  * the kernel only that didn't advertise the right
    7712                 :            :                  * attributes for the only driver that then supported
    7713                 :            :                  * AP mode w/o monitor -- ath6kl.
    7714                 :            :                  */
    7715                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
    7716                 :            :                            "monitor interface type - try to run without it");
    7717                 :          0 :                 drv->device_ap_sme = 1;
    7718                 :            :         }
    7719                 :            : 
    7720         [ #  # ]:          0 :         if (drv->monitor_ifidx < 0)
    7721                 :          0 :                 return -1;
    7722                 :            : 
    7723         [ #  # ]:          0 :         if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
    7724                 :          0 :                 goto error;
    7725                 :            : 
    7726                 :          0 :         memset(&ll, 0, sizeof(ll));
    7727                 :          0 :         ll.sll_family = AF_PACKET;
    7728                 :          0 :         ll.sll_ifindex = drv->monitor_ifidx;
    7729                 :          0 :         drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    7730         [ #  # ]:          0 :         if (drv->monitor_sock < 0) {
    7731                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
    7732                 :          0 :                            strerror(errno));
    7733                 :          0 :                 goto error;
    7734                 :            :         }
    7735                 :            : 
    7736         [ #  # ]:          0 :         if (add_monitor_filter(drv->monitor_sock)) {
    7737                 :          0 :                 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
    7738                 :            :                            "interface; do filtering in user space");
    7739                 :            :                 /* This works, but will cost in performance. */
    7740                 :            :         }
    7741                 :            : 
    7742         [ #  # ]:          0 :         if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
    7743                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
    7744                 :          0 :                            strerror(errno));
    7745                 :          0 :                 goto error;
    7746                 :            :         }
    7747                 :            : 
    7748                 :          0 :         optlen = sizeof(optval);
    7749                 :          0 :         optval = 20;
    7750         [ #  # ]:          0 :         if (setsockopt
    7751                 :          0 :             (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
    7752                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
    7753                 :          0 :                            strerror(errno));
    7754                 :          0 :                 goto error;
    7755                 :            :         }
    7756                 :            : 
    7757         [ #  # ]:          0 :         if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
    7758                 :            :                                      drv, NULL)) {
    7759                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
    7760                 :          0 :                 goto error;
    7761                 :            :         }
    7762                 :            : 
    7763                 :          0 :         drv->monitor_refcount++;
    7764                 :          0 :         return 0;
    7765                 :            :  error:
    7766                 :          0 :         nl80211_remove_monitor_interface(drv);
    7767                 :          0 :         return -1;
    7768                 :            : }
    7769                 :            : 
    7770                 :            : 
    7771                 :        271 : static int nl80211_setup_ap(struct i802_bss *bss)
    7772                 :            : {
    7773                 :        271 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7774                 :            : 
    7775                 :        271 :         wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
    7776                 :        813 :                    bss->ifname, drv->device_ap_sme, drv->use_monitor);
    7777                 :            : 
    7778                 :            :         /*
    7779                 :            :          * Disable Probe Request reporting unless we need it in this way for
    7780                 :            :          * devices that include the AP SME, in the other case (unless using
    7781                 :            :          * monitor iface) we'll get it through the nl_mgmt socket instead.
    7782                 :            :          */
    7783         [ +  - ]:        271 :         if (!drv->device_ap_sme)
    7784                 :        271 :                 wpa_driver_nl80211_probe_req_report(bss, 0);
    7785                 :            : 
    7786 [ +  - ][ +  - ]:        271 :         if (!drv->device_ap_sme && !drv->use_monitor)
    7787         [ -  + ]:        271 :                 if (nl80211_mgmt_subscribe_ap(bss))
    7788                 :          0 :                         return -1;
    7789                 :            : 
    7790 [ -  + ][ #  # ]:        271 :         if (drv->device_ap_sme && !drv->use_monitor)
    7791         [ #  # ]:          0 :                 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
    7792                 :          0 :                         return -1;
    7793                 :            : 
    7794         [ +  - ]:        271 :         if (!drv->device_ap_sme && drv->use_monitor &&
           [ -  +  #  # ]
    7795         [ #  # ]:          0 :             nl80211_create_monitor_interface(drv) &&
    7796                 :          0 :             !drv->device_ap_sme)
    7797                 :          0 :                 return -1;
    7798                 :            : 
    7799   [ -  +  #  # ]:        271 :         if (drv->device_ap_sme &&
    7800                 :          0 :             wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
    7801                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
    7802                 :            :                            "Probe Request frame reporting in AP mode");
    7803                 :            :                 /* Try to survive without this */
    7804                 :            :         }
    7805                 :            : 
    7806                 :        271 :         return 0;
    7807                 :            : }
    7808                 :            : 
    7809                 :            : 
    7810                 :        262 : static void nl80211_teardown_ap(struct i802_bss *bss)
    7811                 :            : {
    7812                 :        262 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7813                 :            : 
    7814                 :        262 :         wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
    7815                 :        786 :                    bss->ifname, drv->device_ap_sme, drv->use_monitor);
    7816         [ -  + ]:        262 :         if (drv->device_ap_sme) {
    7817                 :          0 :                 wpa_driver_nl80211_probe_req_report(bss, 0);
    7818         [ #  # ]:          0 :                 if (!drv->use_monitor)
    7819                 :          0 :                         nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
    7820         [ -  + ]:        262 :         } else if (drv->use_monitor)
    7821                 :          0 :                 nl80211_remove_monitor_interface(drv);
    7822                 :            :         else
    7823                 :        262 :                 nl80211_mgmt_unsubscribe(bss, "AP teardown");
    7824                 :            : 
    7825                 :        262 :         bss->beacon_set = 0;
    7826                 :        262 : }
    7827                 :            : 
    7828                 :            : 
    7829                 :       1667 : static int nl80211_send_eapol_data(struct i802_bss *bss,
    7830                 :            :                                    const u8 *addr, const u8 *data,
    7831                 :            :                                    size_t data_len)
    7832                 :            : {
    7833                 :            :         struct sockaddr_ll ll;
    7834                 :            :         int ret;
    7835                 :            : 
    7836         [ -  + ]:       1667 :         if (bss->drv->eapol_tx_sock < 0) {
    7837                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
    7838                 :          0 :                 return -1;
    7839                 :            :         }
    7840                 :            : 
    7841                 :       1667 :         os_memset(&ll, 0, sizeof(ll));
    7842                 :       1667 :         ll.sll_family = AF_PACKET;
    7843                 :       1667 :         ll.sll_ifindex = bss->ifindex;
    7844                 :       1667 :         ll.sll_protocol = htons(ETH_P_PAE);
    7845                 :       1667 :         ll.sll_halen = ETH_ALEN;
    7846                 :       1667 :         os_memcpy(ll.sll_addr, addr, ETH_ALEN);
    7847                 :       1667 :         ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
    7848                 :            :                      (struct sockaddr *) &ll, sizeof(ll));
    7849         [ -  + ]:       1667 :         if (ret < 0)
    7850                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
    7851                 :          0 :                            strerror(errno));
    7852                 :            : 
    7853                 :       1667 :         return ret;
    7854                 :            : }
    7855                 :            : 
    7856                 :            : 
    7857                 :            : static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
    7858                 :            : 
    7859                 :       1667 : static int wpa_driver_nl80211_hapd_send_eapol(
    7860                 :            :         void *priv, const u8 *addr, const u8 *data,
    7861                 :            :         size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
    7862                 :            : {
    7863                 :       1667 :         struct i802_bss *bss = priv;
    7864                 :       1667 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7865                 :            :         struct ieee80211_hdr *hdr;
    7866                 :            :         size_t len;
    7867                 :            :         u8 *pos;
    7868                 :            :         int res;
    7869                 :       1667 :         int qos = flags & WPA_STA_WMM;
    7870                 :            : 
    7871 [ +  - ][ +  - ]:       1667 :         if (drv->device_ap_sme || !drv->use_monitor)
    7872                 :       1667 :                 return nl80211_send_eapol_data(bss, addr, data, data_len);
    7873                 :            : 
    7874         [ #  # ]:          0 :         len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
    7875                 :            :                 data_len;
    7876                 :          0 :         hdr = os_zalloc(len);
    7877         [ #  # ]:          0 :         if (hdr == NULL) {
    7878                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
    7879                 :            :                            (unsigned long) len);
    7880                 :          0 :                 return -1;
    7881                 :            :         }
    7882                 :            : 
    7883                 :          0 :         hdr->frame_control =
    7884                 :            :                 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
    7885                 :          0 :         hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
    7886         [ #  # ]:          0 :         if (encrypt)
    7887                 :          0 :                 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
    7888         [ #  # ]:          0 :         if (qos) {
    7889                 :          0 :                 hdr->frame_control |=
    7890                 :            :                         host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
    7891                 :            :         }
    7892                 :            : 
    7893                 :          0 :         memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
    7894                 :          0 :         memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
    7895                 :          0 :         memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
    7896                 :          0 :         pos = (u8 *) (hdr + 1);
    7897                 :            : 
    7898         [ #  # ]:          0 :         if (qos) {
    7899                 :            :                 /* Set highest priority in QoS header */
    7900                 :          0 :                 pos[0] = 7;
    7901                 :          0 :                 pos[1] = 0;
    7902                 :          0 :                 pos += 2;
    7903                 :            :         }
    7904                 :            : 
    7905                 :          0 :         memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
    7906                 :          0 :         pos += sizeof(rfc1042_header);
    7907                 :          0 :         WPA_PUT_BE16(pos, ETH_P_PAE);
    7908                 :          0 :         pos += 2;
    7909                 :          0 :         memcpy(pos, data, data_len);
    7910                 :            : 
    7911                 :          0 :         res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
    7912                 :            :                                             0, 0, 0, 0);
    7913         [ #  # ]:          0 :         if (res < 0) {
    7914                 :          0 :                 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
    7915                 :            :                            "failed: %d (%s)",
    7916                 :          0 :                            (unsigned long) len, errno, strerror(errno));
    7917                 :            :         }
    7918                 :          0 :         os_free(hdr);
    7919                 :            : 
    7920                 :       1667 :         return res;
    7921                 :            : }
    7922                 :            : 
    7923                 :            : 
    7924                 :       1187 : static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
    7925                 :            :                                             int total_flags,
    7926                 :            :                                             int flags_or, int flags_and)
    7927                 :            : {
    7928                 :       1187 :         struct i802_bss *bss = priv;
    7929                 :       1187 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7930                 :            :         struct nl_msg *msg;
    7931                 :            :         struct nlattr *flags;
    7932                 :            :         struct nl80211_sta_flag_update upd;
    7933                 :            : 
    7934                 :       1187 :         msg = nlmsg_alloc();
    7935         [ -  + ]:       1187 :         if (!msg)
    7936                 :          0 :                 return -ENOMEM;
    7937                 :            : 
    7938                 :       1187 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
    7939                 :            : 
    7940         [ +  - ]:       1187 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
    7941                 :            :                     if_nametoindex(bss->ifname));
    7942         [ +  - ]:       1187 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    7943                 :            : 
    7944                 :            :         /*
    7945                 :            :          * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
    7946                 :            :          * can be removed eventually.
    7947                 :            :          */
    7948                 :       1187 :         flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
    7949         [ -  + ]:       1187 :         if (!flags)
    7950                 :          0 :                 goto nla_put_failure;
    7951         [ +  + ]:       1187 :         if (total_flags & WPA_STA_AUTHORIZED)
    7952         [ +  - ]:        348 :                 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
    7953                 :            : 
    7954         [ +  + ]:       1187 :         if (total_flags & WPA_STA_WMM)
    7955         [ +  - ]:       1175 :                 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
    7956                 :            : 
    7957         [ +  + ]:       1187 :         if (total_flags & WPA_STA_SHORT_PREAMBLE)
    7958         [ +  - ]:       1175 :                 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
    7959                 :            : 
    7960         [ +  + ]:       1187 :         if (total_flags & WPA_STA_MFP)
    7961         [ +  - ]:         48 :                 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
    7962                 :            : 
    7963         [ -  + ]:       1187 :         if (total_flags & WPA_STA_TDLS_PEER)
    7964         [ #  # ]:          0 :                 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
    7965                 :            : 
    7966                 :       1187 :         nla_nest_end(msg, flags);
    7967                 :            : 
    7968                 :       1187 :         os_memset(&upd, 0, sizeof(upd));
    7969                 :       1187 :         upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
    7970                 :       1187 :         upd.set = sta_flags_nl80211(flags_or);
    7971         [ +  - ]:       1187 :         NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
    7972                 :            : 
    7973                 :       1187 :         return send_and_recv_msgs(drv, msg, NULL, NULL);
    7974                 :            :  nla_put_failure:
    7975                 :          0 :         nlmsg_free(msg);
    7976                 :       1187 :         return -ENOBUFS;
    7977                 :            : }
    7978                 :            : 
    7979                 :            : 
    7980                 :         55 : static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
    7981                 :            :                                  struct wpa_driver_associate_params *params)
    7982                 :            : {
    7983                 :            :         enum nl80211_iftype nlmode, old_mode;
    7984                 :        110 :         struct hostapd_freq_params freq = {
    7985                 :         55 :                 .freq = params->freq,
    7986                 :            :         };
    7987                 :            : 
    7988         [ +  - ]:         55 :         if (params->p2p) {
    7989                 :         55 :                 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
    7990                 :            :                            "group (GO)");
    7991                 :         55 :                 nlmode = NL80211_IFTYPE_P2P_GO;
    7992                 :            :         } else
    7993                 :          0 :                 nlmode = NL80211_IFTYPE_AP;
    7994                 :            : 
    7995                 :         55 :         old_mode = drv->nlmode;
    7996         [ -  + ]:         55 :         if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
    7997                 :          0 :                 nl80211_remove_monitor_interface(drv);
    7998                 :          0 :                 return -1;
    7999                 :            :         }
    8000                 :            : 
    8001         [ -  + ]:         55 :         if (wpa_driver_nl80211_set_freq(drv->first_bss, &freq)) {
    8002         [ #  # ]:          0 :                 if (old_mode != nlmode)
    8003                 :          0 :                         wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
    8004                 :          0 :                 nl80211_remove_monitor_interface(drv);
    8005                 :          0 :                 return -1;
    8006                 :            :         }
    8007                 :            : 
    8008                 :         55 :         return 0;
    8009                 :            : }
    8010                 :            : 
    8011                 :            : 
    8012                 :          6 : static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
    8013                 :            : {
    8014                 :            :         struct nl_msg *msg;
    8015                 :          6 :         int ret = -1;
    8016                 :            : 
    8017                 :          6 :         msg = nlmsg_alloc();
    8018         [ -  + ]:          6 :         if (!msg)
    8019                 :          0 :                 return -1;
    8020                 :            : 
    8021                 :          6 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
    8022         [ +  - ]:          6 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    8023                 :          6 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    8024                 :          6 :         msg = NULL;
    8025         [ -  + ]:          6 :         if (ret) {
    8026                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
    8027                 :            :                            "(%s)", ret, strerror(-ret));
    8028                 :          0 :                 goto nla_put_failure;
    8029                 :            :         }
    8030                 :            : 
    8031                 :          6 :         ret = 0;
    8032                 :          6 :         wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
    8033                 :            : 
    8034                 :            : nla_put_failure:
    8035         [ -  + ]:          6 :         if (wpa_driver_nl80211_set_mode(drv->first_bss,
    8036                 :            :                                         NL80211_IFTYPE_STATION)) {
    8037                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
    8038                 :            :                            "station mode");
    8039                 :            :         }
    8040                 :            : 
    8041                 :          6 :         nlmsg_free(msg);
    8042                 :          6 :         return ret;
    8043                 :            : }
    8044                 :            : 
    8045                 :            : 
    8046                 :          6 : static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
    8047                 :            :                                    struct wpa_driver_associate_params *params)
    8048                 :            : {
    8049                 :            :         struct nl_msg *msg;
    8050                 :          6 :         int ret = -1;
    8051                 :          6 :         int count = 0;
    8052                 :            : 
    8053                 :          6 :         wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
    8054                 :            : 
    8055         [ -  + ]:          6 :         if (wpa_driver_nl80211_set_mode(drv->first_bss,
    8056                 :            :                                         NL80211_IFTYPE_ADHOC)) {
    8057                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
    8058                 :            :                            "IBSS mode");
    8059                 :          0 :                 return -1;
    8060                 :            :         }
    8061                 :            : 
    8062                 :            : retry:
    8063                 :          6 :         msg = nlmsg_alloc();
    8064         [ -  + ]:          6 :         if (!msg)
    8065                 :          0 :                 return -1;
    8066                 :            : 
    8067                 :          6 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
    8068         [ +  - ]:          6 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    8069                 :            : 
    8070 [ +  - ][ +  - ]:          6 :         if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
    8071                 :            :                 goto nla_put_failure;
    8072                 :            : 
    8073                 :          6 :         wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
    8074                 :          6 :                           params->ssid, params->ssid_len);
    8075         [ +  - ]:          6 :         NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
    8076                 :            :                 params->ssid);
    8077                 :          6 :         os_memcpy(drv->ssid, params->ssid, params->ssid_len);
    8078                 :          6 :         drv->ssid_len = params->ssid_len;
    8079                 :            : 
    8080                 :          6 :         wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
    8081         [ +  - ]:          6 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
    8082                 :            : 
    8083                 :          6 :         ret = nl80211_set_conn_keys(params, msg);
    8084         [ -  + ]:          6 :         if (ret)
    8085                 :          0 :                 goto nla_put_failure;
    8086                 :            : 
    8087 [ -  + ][ #  # ]:          6 :         if (params->bssid && params->fixed_bssid) {
    8088                 :          0 :                 wpa_printf(MSG_DEBUG, "  * BSSID=" MACSTR,
    8089                 :          0 :                            MAC2STR(params->bssid));
    8090         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
    8091                 :            :         }
    8092                 :            : 
    8093 [ +  - ][ +  + ]:          6 :         if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
    8094         [ +  - ]:          3 :             params->key_mgmt_suite == KEY_MGMT_PSK ||
    8095         [ -  + ]:          3 :             params->key_mgmt_suite == KEY_MGMT_802_1X_SHA256 ||
    8096                 :          3 :             params->key_mgmt_suite == KEY_MGMT_PSK_SHA256) {
    8097                 :          3 :                 wpa_printf(MSG_DEBUG, "  * control port");
    8098         [ +  - ]:          3 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
    8099                 :            :         }
    8100                 :            : 
    8101         [ +  - ]:          6 :         if (params->wpa_ie) {
    8102                 :          6 :                 wpa_hexdump(MSG_DEBUG,
    8103                 :            :                             "  * Extra IEs for Beacon/Probe Response frames",
    8104                 :          6 :                             params->wpa_ie, params->wpa_ie_len);
    8105         [ +  - ]:          6 :                 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
    8106                 :            :                         params->wpa_ie);
    8107                 :            :         }
    8108                 :            : 
    8109                 :          6 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    8110                 :          6 :         msg = NULL;
    8111         [ -  + ]:          6 :         if (ret) {
    8112                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
    8113                 :            :                            ret, strerror(-ret));
    8114                 :          0 :                 count++;
    8115 [ #  # ][ #  # ]:          0 :                 if (ret == -EALREADY && count == 1) {
    8116                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
    8117                 :            :                                    "forced leave");
    8118                 :          0 :                         nl80211_leave_ibss(drv);
    8119                 :          0 :                         nlmsg_free(msg);
    8120                 :          0 :                         goto retry;
    8121                 :            :                 }
    8122                 :            : 
    8123                 :          0 :                 goto nla_put_failure;
    8124                 :            :         }
    8125                 :          6 :         ret = 0;
    8126                 :          6 :         wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
    8127                 :            : 
    8128                 :            : nla_put_failure:
    8129                 :          6 :         nlmsg_free(msg);
    8130                 :          6 :         return ret;
    8131                 :            : }
    8132                 :            : 
    8133                 :            : 
    8134                 :          0 : static int wpa_driver_nl80211_try_connect(
    8135                 :            :         struct wpa_driver_nl80211_data *drv,
    8136                 :            :         struct wpa_driver_associate_params *params)
    8137                 :            : {
    8138                 :            :         struct nl_msg *msg;
    8139                 :            :         enum nl80211_auth_type type;
    8140                 :          0 :         int ret = 0;
    8141                 :            :         int algs;
    8142                 :            : 
    8143                 :          0 :         msg = nlmsg_alloc();
    8144         [ #  # ]:          0 :         if (!msg)
    8145                 :          0 :                 return -1;
    8146                 :            : 
    8147                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
    8148                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
    8149                 :            : 
    8150         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    8151         [ #  # ]:          0 :         if (params->bssid) {
    8152                 :          0 :                 wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
    8153                 :          0 :                            MAC2STR(params->bssid));
    8154         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
    8155                 :            :         }
    8156         [ #  # ]:          0 :         if (params->freq) {
    8157                 :          0 :                 wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
    8158         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
    8159                 :          0 :                 drv->assoc_freq = params->freq;
    8160                 :            :         } else
    8161                 :          0 :                 drv->assoc_freq = 0;
    8162         [ #  # ]:          0 :         if (params->bg_scan_period >= 0) {
    8163                 :          0 :                 wpa_printf(MSG_DEBUG, "  * bg scan period=%d",
    8164                 :            :                            params->bg_scan_period);
    8165         [ #  # ]:          0 :                 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
    8166                 :            :                             params->bg_scan_period);
    8167                 :            :         }
    8168         [ #  # ]:          0 :         if (params->ssid) {
    8169                 :          0 :                 wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
    8170                 :          0 :                                   params->ssid, params->ssid_len);
    8171         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
    8172                 :            :                         params->ssid);
    8173         [ #  # ]:          0 :                 if (params->ssid_len > sizeof(drv->ssid))
    8174                 :          0 :                         goto nla_put_failure;
    8175                 :          0 :                 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
    8176                 :          0 :                 drv->ssid_len = params->ssid_len;
    8177                 :            :         }
    8178                 :          0 :         wpa_hexdump(MSG_DEBUG, "  * IEs", params->wpa_ie, params->wpa_ie_len);
    8179         [ #  # ]:          0 :         if (params->wpa_ie)
    8180         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
    8181                 :            :                         params->wpa_ie);
    8182                 :            : 
    8183                 :          0 :         algs = 0;
    8184         [ #  # ]:          0 :         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
    8185                 :          0 :                 algs++;
    8186         [ #  # ]:          0 :         if (params->auth_alg & WPA_AUTH_ALG_SHARED)
    8187                 :          0 :                 algs++;
    8188         [ #  # ]:          0 :         if (params->auth_alg & WPA_AUTH_ALG_LEAP)
    8189                 :          0 :                 algs++;
    8190         [ #  # ]:          0 :         if (algs > 1) {
    8191                 :          0 :                 wpa_printf(MSG_DEBUG, "  * Leave out Auth Type for automatic "
    8192                 :            :                            "selection");
    8193                 :          0 :                 goto skip_auth_type;
    8194                 :            :         }
    8195                 :            : 
    8196         [ #  # ]:          0 :         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
    8197                 :          0 :                 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
    8198         [ #  # ]:          0 :         else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
    8199                 :          0 :                 type = NL80211_AUTHTYPE_SHARED_KEY;
    8200         [ #  # ]:          0 :         else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
    8201                 :          0 :                 type = NL80211_AUTHTYPE_NETWORK_EAP;
    8202         [ #  # ]:          0 :         else if (params->auth_alg & WPA_AUTH_ALG_FT)
    8203                 :          0 :                 type = NL80211_AUTHTYPE_FT;
    8204                 :            :         else
    8205                 :          0 :                 goto nla_put_failure;
    8206                 :            : 
    8207                 :          0 :         wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
    8208         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
    8209                 :            : 
    8210                 :            : skip_auth_type:
    8211         [ #  # ]:          0 :         if (params->wpa_proto) {
    8212                 :          0 :                 enum nl80211_wpa_versions ver = 0;
    8213                 :            : 
    8214         [ #  # ]:          0 :                 if (params->wpa_proto & WPA_PROTO_WPA)
    8215                 :          0 :                         ver |= NL80211_WPA_VERSION_1;
    8216         [ #  # ]:          0 :                 if (params->wpa_proto & WPA_PROTO_RSN)
    8217                 :          0 :                         ver |= NL80211_WPA_VERSION_2;
    8218                 :            : 
    8219                 :          0 :                 wpa_printf(MSG_DEBUG, "  * WPA Versions 0x%x", ver);
    8220         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
    8221                 :            :         }
    8222                 :            : 
    8223         [ #  # ]:          0 :         if (params->pairwise_suite != CIPHER_NONE) {
    8224                 :            :                 int cipher;
    8225                 :            : 
    8226   [ #  #  #  #  :          0 :                 switch (params->pairwise_suite) {
             #  #  #  # ]
    8227                 :            :                 case CIPHER_SMS4:
    8228                 :          0 :                         cipher = WLAN_CIPHER_SUITE_SMS4;
    8229                 :          0 :                         break;
    8230                 :            :                 case CIPHER_WEP40:
    8231                 :          0 :                         cipher = WLAN_CIPHER_SUITE_WEP40;
    8232                 :          0 :                         break;
    8233                 :            :                 case CIPHER_WEP104:
    8234                 :          0 :                         cipher = WLAN_CIPHER_SUITE_WEP104;
    8235                 :          0 :                         break;
    8236                 :            :                 case CIPHER_CCMP:
    8237                 :          0 :                         cipher = WLAN_CIPHER_SUITE_CCMP;
    8238                 :          0 :                         break;
    8239                 :            :                 case CIPHER_GCMP:
    8240                 :          0 :                         cipher = WLAN_CIPHER_SUITE_GCMP;
    8241                 :          0 :                         break;
    8242                 :            :                 case CIPHER_CCMP_256:
    8243                 :          0 :                         cipher = WLAN_CIPHER_SUITE_CCMP_256;
    8244                 :          0 :                         break;
    8245                 :            :                 case CIPHER_GCMP_256:
    8246                 :          0 :                         cipher = WLAN_CIPHER_SUITE_GCMP_256;
    8247                 :          0 :                         break;
    8248                 :            :                 case CIPHER_TKIP:
    8249                 :            :                 default:
    8250                 :          0 :                         cipher = WLAN_CIPHER_SUITE_TKIP;
    8251                 :          0 :                         break;
    8252                 :            :                 }
    8253         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
    8254                 :            :         }
    8255                 :            : 
    8256         [ #  # ]:          0 :         if (params->group_suite != CIPHER_NONE) {
    8257                 :            :                 int cipher;
    8258                 :            : 
    8259   [ #  #  #  #  :          0 :                 switch (params->group_suite) {
             #  #  #  # ]
    8260                 :            :                 case CIPHER_SMS4:
    8261                 :          0 :                         cipher = WLAN_CIPHER_SUITE_SMS4;
    8262                 :          0 :                         break;
    8263                 :            :                 case CIPHER_WEP40:
    8264                 :          0 :                         cipher = WLAN_CIPHER_SUITE_WEP40;
    8265                 :          0 :                         break;
    8266                 :            :                 case CIPHER_WEP104:
    8267                 :          0 :                         cipher = WLAN_CIPHER_SUITE_WEP104;
    8268                 :          0 :                         break;
    8269                 :            :                 case CIPHER_CCMP:
    8270                 :          0 :                         cipher = WLAN_CIPHER_SUITE_CCMP;
    8271                 :          0 :                         break;
    8272                 :            :                 case CIPHER_GCMP:
    8273                 :          0 :                         cipher = WLAN_CIPHER_SUITE_GCMP;
    8274                 :          0 :                         break;
    8275                 :            :                 case CIPHER_CCMP_256:
    8276                 :          0 :                         cipher = WLAN_CIPHER_SUITE_CCMP_256;
    8277                 :          0 :                         break;
    8278                 :            :                 case CIPHER_GCMP_256:
    8279                 :          0 :                         cipher = WLAN_CIPHER_SUITE_GCMP_256;
    8280                 :          0 :                         break;
    8281                 :            :                 case CIPHER_TKIP:
    8282                 :            :                 default:
    8283                 :          0 :                         cipher = WLAN_CIPHER_SUITE_TKIP;
    8284                 :          0 :                         break;
    8285                 :            :                 }
    8286         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
    8287                 :            :         }
    8288                 :            : 
    8289 [ #  # ][ #  # ]:          0 :         if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
    8290         [ #  # ]:          0 :             params->key_mgmt_suite == KEY_MGMT_PSK ||
    8291         [ #  # ]:          0 :             params->key_mgmt_suite == KEY_MGMT_FT_802_1X ||
    8292         [ #  # ]:          0 :             params->key_mgmt_suite == KEY_MGMT_FT_PSK ||
    8293                 :          0 :             params->key_mgmt_suite == KEY_MGMT_CCKM) {
    8294                 :          0 :                 int mgmt = WLAN_AKM_SUITE_PSK;
    8295                 :            : 
    8296   [ #  #  #  #  :          0 :                 switch (params->key_mgmt_suite) {
                      # ]
    8297                 :            :                 case KEY_MGMT_CCKM:
    8298                 :          0 :                         mgmt = WLAN_AKM_SUITE_CCKM;
    8299                 :          0 :                         break;
    8300                 :            :                 case KEY_MGMT_802_1X:
    8301                 :          0 :                         mgmt = WLAN_AKM_SUITE_8021X;
    8302                 :          0 :                         break;
    8303                 :            :                 case KEY_MGMT_FT_802_1X:
    8304                 :          0 :                         mgmt = WLAN_AKM_SUITE_FT_8021X;
    8305                 :          0 :                         break;
    8306                 :            :                 case KEY_MGMT_FT_PSK:
    8307                 :          0 :                         mgmt = WLAN_AKM_SUITE_FT_PSK;
    8308                 :          0 :                         break;
    8309                 :            :                 case KEY_MGMT_PSK:
    8310                 :            :                 default:
    8311                 :          0 :                         mgmt = WLAN_AKM_SUITE_PSK;
    8312                 :          0 :                         break;
    8313                 :            :                 }
    8314         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
    8315                 :            :         }
    8316                 :            : 
    8317                 :            : #ifdef CONFIG_IEEE80211W
    8318         [ #  # ]:          0 :         if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
    8319         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
    8320                 :            : #endif /* CONFIG_IEEE80211W */
    8321                 :            : 
    8322         [ #  # ]:          0 :         if (params->disable_ht)
    8323         [ #  # ]:          0 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
    8324                 :            : 
    8325 [ #  # ][ #  # ]:          0 :         if (params->htcaps && params->htcaps_mask) {
    8326                 :          0 :                 int sz = sizeof(struct ieee80211_ht_capabilities);
    8327         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
    8328         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
    8329                 :            :                         params->htcaps_mask);
    8330                 :            :         }
    8331                 :            : 
    8332                 :            : #ifdef CONFIG_VHT_OVERRIDES
    8333                 :            :         if (params->disable_vht) {
    8334                 :            :                 wpa_printf(MSG_DEBUG, "  * VHT disabled");
    8335                 :            :                 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
    8336                 :            :         }
    8337                 :            : 
    8338                 :            :         if (params->vhtcaps && params->vhtcaps_mask) {
    8339                 :            :                 int sz = sizeof(struct ieee80211_vht_capabilities);
    8340                 :            :                 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
    8341                 :            :                 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
    8342                 :            :                         params->vhtcaps_mask);
    8343                 :            :         }
    8344                 :            : #endif /* CONFIG_VHT_OVERRIDES */
    8345                 :            : 
    8346                 :          0 :         ret = nl80211_set_conn_keys(params, msg);
    8347         [ #  # ]:          0 :         if (ret)
    8348                 :          0 :                 goto nla_put_failure;
    8349                 :            : 
    8350                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    8351                 :          0 :         msg = NULL;
    8352         [ #  # ]:          0 :         if (ret) {
    8353                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
    8354                 :            :                            "(%s)", ret, strerror(-ret));
    8355                 :          0 :                 goto nla_put_failure;
    8356                 :            :         }
    8357                 :          0 :         ret = 0;
    8358                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
    8359                 :            : 
    8360                 :            : nla_put_failure:
    8361                 :          0 :         nlmsg_free(msg);
    8362                 :          0 :         return ret;
    8363                 :            : 
    8364                 :            : }
    8365                 :            : 
    8366                 :            : 
    8367                 :          0 : static int wpa_driver_nl80211_connect(
    8368                 :            :         struct wpa_driver_nl80211_data *drv,
    8369                 :            :         struct wpa_driver_associate_params *params)
    8370                 :            : {
    8371                 :          0 :         int ret = wpa_driver_nl80211_try_connect(drv, params);
    8372         [ #  # ]:          0 :         if (ret == -EALREADY) {
    8373                 :            :                 /*
    8374                 :            :                  * cfg80211 does not currently accept new connections if
    8375                 :            :                  * we are already connected. As a workaround, force
    8376                 :            :                  * disconnection and try again.
    8377                 :            :                  */
    8378                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
    8379                 :            :                            "disconnecting before reassociation "
    8380                 :            :                            "attempt");
    8381         [ #  # ]:          0 :                 if (wpa_driver_nl80211_disconnect(
    8382                 :            :                             drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
    8383                 :          0 :                         return -1;
    8384                 :          0 :                 ret = wpa_driver_nl80211_try_connect(drv, params);
    8385                 :            :         }
    8386                 :          0 :         return ret;
    8387                 :            : }
    8388                 :            : 
    8389                 :            : 
    8390                 :        469 : static int wpa_driver_nl80211_associate(
    8391                 :            :         void *priv, struct wpa_driver_associate_params *params)
    8392                 :            : {
    8393                 :        469 :         struct i802_bss *bss = priv;
    8394                 :        469 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8395                 :        469 :         int ret = -1;
    8396                 :            :         struct nl_msg *msg;
    8397                 :            : 
    8398         [ +  + ]:        469 :         if (params->mode == IEEE80211_MODE_AP)
    8399                 :         55 :                 return wpa_driver_nl80211_ap(drv, params);
    8400                 :            : 
    8401         [ +  + ]:        414 :         if (params->mode == IEEE80211_MODE_IBSS)
    8402                 :          6 :                 return wpa_driver_nl80211_ibss(drv, params);
    8403                 :            : 
    8404         [ -  + ]:        408 :         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
    8405         [ #  # ]:          0 :                 enum nl80211_iftype nlmode = params->p2p ?
    8406                 :            :                         NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
    8407                 :            : 
    8408         [ #  # ]:          0 :                 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
    8409                 :          0 :                         return -1;
    8410                 :          0 :                 return wpa_driver_nl80211_connect(drv, params);
    8411                 :            :         }
    8412                 :            : 
    8413                 :        408 :         nl80211_mark_disconnected(drv);
    8414                 :            : 
    8415                 :        408 :         msg = nlmsg_alloc();
    8416         [ -  + ]:        408 :         if (!msg)
    8417                 :          0 :                 return -1;
    8418                 :            : 
    8419                 :        408 :         wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
    8420                 :            :                    drv->ifindex);
    8421                 :        408 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
    8422                 :            : 
    8423         [ +  - ]:        408 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    8424         [ +  - ]:        408 :         if (params->bssid) {
    8425                 :        408 :                 wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
    8426                 :       2448 :                            MAC2STR(params->bssid));
    8427         [ +  - ]:        408 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
    8428                 :            :         }
    8429         [ +  - ]:        408 :         if (params->freq) {
    8430                 :        408 :                 wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
    8431         [ +  - ]:        408 :                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
    8432                 :        408 :                 drv->assoc_freq = params->freq;
    8433                 :            :         } else
    8434                 :          0 :                 drv->assoc_freq = 0;
    8435         [ -  + ]:        408 :         if (params->bg_scan_period >= 0) {
    8436                 :          0 :                 wpa_printf(MSG_DEBUG, "  * bg scan period=%d",
    8437                 :            :                            params->bg_scan_period);
    8438         [ #  # ]:          0 :                 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
    8439                 :            :                             params->bg_scan_period);
    8440                 :            :         }
    8441         [ +  - ]:        408 :         if (params->ssid) {
    8442                 :        408 :                 wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
    8443                 :        408 :                                   params->ssid, params->ssid_len);
    8444         [ +  - ]:        408 :                 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
    8445                 :            :                         params->ssid);
    8446         [ -  + ]:        408 :                 if (params->ssid_len > sizeof(drv->ssid))
    8447                 :          0 :                         goto nla_put_failure;
    8448                 :        408 :                 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
    8449                 :        408 :                 drv->ssid_len = params->ssid_len;
    8450                 :            :         }
    8451                 :        408 :         wpa_hexdump(MSG_DEBUG, "  * IEs", params->wpa_ie, params->wpa_ie_len);
    8452         [ +  - ]:        408 :         if (params->wpa_ie)
    8453         [ +  - ]:        408 :                 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
    8454                 :            :                         params->wpa_ie);
    8455                 :            : 
    8456         [ +  + ]:        408 :         if (params->pairwise_suite != CIPHER_NONE) {
    8457                 :            :                 int cipher;
    8458                 :            : 
    8459   [ +  -  +  +  :        252 :                 switch (params->pairwise_suite) {
                +  +  + ]
    8460                 :            :                 case CIPHER_WEP40:
    8461                 :          2 :                         cipher = WLAN_CIPHER_SUITE_WEP40;
    8462                 :          2 :                         break;
    8463                 :            :                 case CIPHER_WEP104:
    8464                 :          0 :                         cipher = WLAN_CIPHER_SUITE_WEP104;
    8465                 :          0 :                         break;
    8466                 :            :                 case CIPHER_CCMP:
    8467                 :        240 :                         cipher = WLAN_CIPHER_SUITE_CCMP;
    8468                 :        240 :                         break;
    8469                 :            :                 case CIPHER_GCMP:
    8470                 :          1 :                         cipher = WLAN_CIPHER_SUITE_GCMP;
    8471                 :          1 :                         break;
    8472                 :            :                 case CIPHER_CCMP_256:
    8473                 :          1 :                         cipher = WLAN_CIPHER_SUITE_CCMP_256;
    8474                 :          1 :                         break;
    8475                 :            :                 case CIPHER_GCMP_256:
    8476                 :          1 :                         cipher = WLAN_CIPHER_SUITE_GCMP_256;
    8477                 :          1 :                         break;
    8478                 :            :                 case CIPHER_TKIP:
    8479                 :            :                 default:
    8480                 :          7 :                         cipher = WLAN_CIPHER_SUITE_TKIP;
    8481                 :          7 :                         break;
    8482                 :            :                 }
    8483                 :        252 :                 wpa_printf(MSG_DEBUG, "  * pairwise=0x%x", cipher);
    8484         [ +  - ]:        252 :                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
    8485                 :            :         }
    8486                 :            : 
    8487         [ +  + ]:        408 :         if (params->group_suite != CIPHER_NONE) {
    8488                 :            :                 int cipher;
    8489                 :            : 
    8490   [ +  -  +  +  :        252 :                 switch (params->group_suite) {
                +  +  + ]
    8491                 :            :                 case CIPHER_WEP40:
    8492                 :          2 :                         cipher = WLAN_CIPHER_SUITE_WEP40;
    8493                 :          2 :                         break;
    8494                 :            :                 case CIPHER_WEP104:
    8495                 :          0 :                         cipher = WLAN_CIPHER_SUITE_WEP104;
    8496                 :          0 :                         break;
    8497                 :            :                 case CIPHER_CCMP:
    8498                 :        229 :                         cipher = WLAN_CIPHER_SUITE_CCMP;
    8499                 :        229 :                         break;
    8500                 :            :                 case CIPHER_GCMP:
    8501                 :          1 :                         cipher = WLAN_CIPHER_SUITE_GCMP;
    8502                 :          1 :                         break;
    8503                 :            :                 case CIPHER_CCMP_256:
    8504                 :          1 :                         cipher = WLAN_CIPHER_SUITE_CCMP_256;
    8505                 :          1 :                         break;
    8506                 :            :                 case CIPHER_GCMP_256:
    8507                 :          1 :                         cipher = WLAN_CIPHER_SUITE_GCMP_256;
    8508                 :          1 :                         break;
    8509                 :            :                 case CIPHER_TKIP:
    8510                 :            :                 default:
    8511                 :         18 :                         cipher = WLAN_CIPHER_SUITE_TKIP;
    8512                 :         18 :                         break;
    8513                 :            :                 }
    8514                 :        252 :                 wpa_printf(MSG_DEBUG, "  * group=0x%x", cipher);
    8515         [ +  - ]:        252 :                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
    8516                 :            :         }
    8517                 :            : 
    8518                 :            : #ifdef CONFIG_IEEE80211W
    8519         [ +  + ]:        408 :         if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
    8520         [ +  - ]:         15 :                 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
    8521                 :            : #endif /* CONFIG_IEEE80211W */
    8522                 :            : 
    8523         [ +  - ]:        408 :         NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
    8524                 :            : 
    8525         [ +  + ]:        408 :         if (params->prev_bssid) {
    8526                 :         26 :                 wpa_printf(MSG_DEBUG, "  * prev_bssid=" MACSTR,
    8527                 :        156 :                            MAC2STR(params->prev_bssid));
    8528         [ +  - ]:         26 :                 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
    8529                 :            :                         params->prev_bssid);
    8530                 :            :         }
    8531                 :            : 
    8532         [ -  + ]:        408 :         if (params->disable_ht)
    8533         [ #  # ]:          0 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
    8534                 :            : 
    8535 [ -  + ][ #  # ]:        408 :         if (params->htcaps && params->htcaps_mask) {
    8536                 :          0 :                 int sz = sizeof(struct ieee80211_ht_capabilities);
    8537         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
    8538         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
    8539                 :            :                         params->htcaps_mask);
    8540                 :            :         }
    8541                 :            : 
    8542                 :            : #ifdef CONFIG_VHT_OVERRIDES
    8543                 :            :         if (params->disable_vht) {
    8544                 :            :                 wpa_printf(MSG_DEBUG, "  * VHT disabled");
    8545                 :            :                 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
    8546                 :            :         }
    8547                 :            : 
    8548                 :            :         if (params->vhtcaps && params->vhtcaps_mask) {
    8549                 :            :                 int sz = sizeof(struct ieee80211_vht_capabilities);
    8550                 :            :                 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
    8551                 :            :                 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
    8552                 :            :                         params->vhtcaps_mask);
    8553                 :            :         }
    8554                 :            : #endif /* CONFIG_VHT_OVERRIDES */
    8555                 :            : 
    8556         [ +  + ]:        408 :         if (params->p2p)
    8557                 :        112 :                 wpa_printf(MSG_DEBUG, "  * P2P group");
    8558                 :            : 
    8559                 :        408 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    8560                 :        408 :         msg = NULL;
    8561         [ -  + ]:        408 :         if (ret) {
    8562                 :          0 :                 wpa_dbg(drv->ctx, MSG_DEBUG,
    8563                 :            :                         "nl80211: MLME command failed (assoc): ret=%d (%s)",
    8564                 :            :                         ret, strerror(-ret));
    8565                 :          0 :                 nl80211_dump_scan(drv);
    8566                 :          0 :                 goto nla_put_failure;
    8567                 :            :         }
    8568                 :        408 :         ret = 0;
    8569                 :        408 :         wpa_printf(MSG_DEBUG, "nl80211: Association request send "
    8570                 :            :                    "successfully");
    8571                 :            : 
    8572                 :            : nla_put_failure:
    8573                 :        408 :         nlmsg_free(msg);
    8574                 :        469 :         return ret;
    8575                 :            : }
    8576                 :            : 
    8577                 :            : 
    8578                 :        638 : static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
    8579                 :            :                             int ifindex, enum nl80211_iftype mode)
    8580                 :            : {
    8581                 :            :         struct nl_msg *msg;
    8582                 :        638 :         int ret = -ENOBUFS;
    8583                 :            : 
    8584                 :        638 :         wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
    8585                 :            :                    ifindex, mode, nl80211_iftype_str(mode));
    8586                 :            : 
    8587                 :        638 :         msg = nlmsg_alloc();
    8588         [ -  + ]:        638 :         if (!msg)
    8589                 :          0 :                 return -ENOMEM;
    8590                 :            : 
    8591                 :        638 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
    8592         [ -  + ]:        638 :         if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
    8593                 :          0 :                 goto nla_put_failure;
    8594         [ +  - ]:        638 :         NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
    8595                 :            : 
    8596                 :        638 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    8597                 :        638 :         msg = NULL;
    8598         [ +  - ]:        638 :         if (!ret)
    8599                 :        638 :                 return 0;
    8600                 :            : nla_put_failure:
    8601                 :          0 :         nlmsg_free(msg);
    8602                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
    8603                 :            :                    " %d (%s)", ifindex, mode, ret, strerror(-ret));
    8604                 :        638 :         return ret;
    8605                 :            : }
    8606                 :            : 
    8607                 :            : 
    8608                 :        638 : static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
    8609                 :            :                                        enum nl80211_iftype nlmode)
    8610                 :            : {
    8611                 :        638 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8612                 :        638 :         int ret = -1;
    8613                 :            :         int i;
    8614                 :        638 :         int was_ap = is_ap_interface(drv->nlmode);
    8615                 :            :         int res;
    8616                 :            : 
    8617                 :        638 :         res = nl80211_set_mode(drv, drv->ifindex, nlmode);
    8618 [ #  # ][ -  + ]:        638 :         if (res && nlmode == nl80211_get_ifmode(bss))
    8619                 :          0 :                 res = 0;
    8620                 :            : 
    8621         [ +  - ]:        638 :         if (res == 0) {
    8622                 :        638 :                 drv->nlmode = nlmode;
    8623                 :        638 :                 ret = 0;
    8624                 :        638 :                 goto done;
    8625                 :            :         }
    8626                 :            : 
    8627         [ #  # ]:          0 :         if (res == -ENODEV)
    8628                 :          0 :                 return -1;
    8629                 :            : 
    8630         [ #  # ]:          0 :         if (nlmode == drv->nlmode) {
    8631                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
    8632                 :            :                            "requested mode - ignore error");
    8633                 :          0 :                 ret = 0;
    8634                 :          0 :                 goto done; /* Already in the requested mode */
    8635                 :            :         }
    8636                 :            : 
    8637                 :            :         /* mac80211 doesn't allow mode changes while the device is up, so
    8638                 :            :          * take the device down, try to set the mode again, and bring the
    8639                 :            :          * device back up.
    8640                 :            :          */
    8641                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
    8642                 :            :                    "interface down");
    8643         [ #  # ]:          0 :         for (i = 0; i < 10; i++) {
    8644                 :          0 :                 res = i802_set_iface_flags(bss, 0);
    8645 [ #  # ][ #  # ]:          0 :                 if (res == -EACCES || res == -ENODEV)
    8646                 :            :                         break;
    8647         [ #  # ]:          0 :                 if (res == 0) {
    8648                 :            :                         /* Try to set the mode again while the interface is
    8649                 :            :                          * down */
    8650                 :          0 :                         ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
    8651         [ #  # ]:          0 :                         if (ret == -EACCES)
    8652                 :          0 :                                 break;
    8653                 :          0 :                         res = i802_set_iface_flags(bss, 1);
    8654 [ #  # ][ #  # ]:          0 :                         if (res && !ret)
    8655                 :          0 :                                 ret = -1;
    8656         [ #  # ]:          0 :                         else if (ret != -EBUSY)
    8657                 :          0 :                                 break;
    8658                 :            :                 } else
    8659                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
    8660                 :            :                                    "interface down");
    8661                 :          0 :                 os_sleep(0, 100000);
    8662                 :            :         }
    8663                 :            : 
    8664         [ #  # ]:          0 :         if (!ret) {
    8665                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
    8666                 :            :                            "interface is down");
    8667                 :          0 :                 drv->nlmode = nlmode;
    8668                 :          0 :                 drv->ignore_if_down_event = 1;
    8669                 :            :         }
    8670                 :            : 
    8671                 :            : done:
    8672         [ -  + ]:        638 :         if (ret) {
    8673                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
    8674                 :          0 :                            "from %d failed", nlmode, drv->nlmode);
    8675                 :          0 :                 return ret;
    8676                 :            :         }
    8677                 :            : 
    8678         [ +  + ]:        638 :         if (is_p2p_net_interface(nlmode))
    8679                 :        125 :                 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
    8680         [ +  + ]:        513 :         else if (drv->disabled_11b_rates)
    8681                 :         96 :                 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
    8682                 :            : 
    8683         [ +  + ]:        638 :         if (is_ap_interface(nlmode)) {
    8684                 :        254 :                 nl80211_mgmt_unsubscribe(bss, "start AP");
    8685                 :            :                 /* Setup additional AP mode functionality if needed */
    8686         [ -  + ]:        254 :                 if (nl80211_setup_ap(bss))
    8687                 :          0 :                         return -1;
    8688         [ +  + ]:        384 :         } else if (was_ap) {
    8689                 :            :                 /* Remove additional AP mode functionality */
    8690                 :        245 :                 nl80211_teardown_ap(bss);
    8691                 :            :         } else {
    8692                 :        139 :                 nl80211_mgmt_unsubscribe(bss, "mode change");
    8693                 :            :         }
    8694                 :            : 
    8695         [ +  + ]:        810 :         if (!bss->in_deinit && !is_ap_interface(nlmode) &&
           [ +  +  -  + ]
    8696                 :        172 :             nl80211_mgmt_subscribe_non_ap(bss) < 0)
    8697                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
    8698                 :            :                            "frame processing - ignore for now");
    8699                 :            : 
    8700                 :        638 :         return 0;
    8701                 :            : }
    8702                 :            : 
    8703                 :            : 
    8704                 :        297 : static int wpa_driver_nl80211_get_capa(void *priv,
    8705                 :            :                                        struct wpa_driver_capa *capa)
    8706                 :            : {
    8707                 :        297 :         struct i802_bss *bss = priv;
    8708                 :        297 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8709         [ -  + ]:        297 :         if (!drv->has_capability)
    8710                 :          0 :                 return -1;
    8711                 :        297 :         os_memcpy(capa, &drv->capa, sizeof(*capa));
    8712 [ +  - ][ +  - ]:        297 :         if (drv->extended_capa && drv->extended_capa_mask) {
    8713                 :        297 :                 capa->extended_capa = drv->extended_capa;
    8714                 :        297 :                 capa->extended_capa_mask = drv->extended_capa_mask;
    8715                 :        297 :                 capa->extended_capa_len = drv->extended_capa_len;
    8716                 :            :         }
    8717                 :            : 
    8718 [ +  - ][ +  - ]:        297 :         if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
    8719                 :        297 :             !drv->allow_p2p_device) {
    8720                 :        297 :                 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
    8721                 :        297 :                 capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
    8722                 :            :         }
    8723                 :            : 
    8724                 :        297 :         return 0;
    8725                 :            : }
    8726                 :            : 
    8727                 :            : 
    8728                 :       2781 : static int wpa_driver_nl80211_set_operstate(void *priv, int state)
    8729                 :            : {
    8730                 :       2781 :         struct i802_bss *bss = priv;
    8731                 :       2781 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8732                 :            : 
    8733         [ +  + ]:       2781 :         wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
    8734                 :            :                    __func__, drv->operstate, state, state ? "UP" : "DORMANT");
    8735                 :       2781 :         drv->operstate = state;
    8736         [ +  + ]:       2781 :         return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
    8737                 :            :                                       state ? IF_OPER_UP : IF_OPER_DORMANT);
    8738                 :            : }
    8739                 :            : 
    8740                 :            : 
    8741                 :       5782 : static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
    8742                 :            : {
    8743                 :       5782 :         struct i802_bss *bss = priv;
    8744                 :       5782 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8745                 :            :         struct nl_msg *msg;
    8746                 :            :         struct nl80211_sta_flag_update upd;
    8747                 :            : 
    8748         [ +  + ]:       5782 :         wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
    8749                 :      34692 :                    MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
    8750                 :            : 
    8751                 :       5782 :         msg = nlmsg_alloc();
    8752         [ -  + ]:       5782 :         if (!msg)
    8753                 :          0 :                 return -ENOMEM;
    8754                 :            : 
    8755                 :       5782 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
    8756                 :            : 
    8757         [ +  - ]:       5782 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
    8758                 :            :                     if_nametoindex(bss->ifname));
    8759         [ +  - ]:       5782 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
    8760                 :            : 
    8761                 :       5782 :         os_memset(&upd, 0, sizeof(upd));
    8762                 :       5782 :         upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
    8763         [ +  + ]:       5782 :         if (authorized)
    8764                 :        340 :                 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
    8765         [ +  - ]:       5782 :         NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
    8766                 :            : 
    8767                 :       5782 :         return send_and_recv_msgs(drv, msg, NULL, NULL);
    8768                 :            :  nla_put_failure:
    8769                 :          0 :         nlmsg_free(msg);
    8770                 :       5782 :         return -ENOBUFS;
    8771                 :            : }
    8772                 :            : 
    8773                 :            : 
    8774                 :            : /* Set kernel driver on given frequency (MHz) */
    8775                 :        238 : static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
    8776                 :            : {
    8777                 :        238 :         struct i802_bss *bss = priv;
    8778                 :        238 :         return wpa_driver_nl80211_set_freq(bss, freq);
    8779                 :            : }
    8780                 :            : 
    8781                 :            : 
    8782                 :        286 : static inline int min_int(int a, int b)
    8783                 :            : {
    8784         [ -  + ]:        286 :         if (a < b)
    8785                 :          0 :                 return a;
    8786                 :        286 :         return b;
    8787                 :            : }
    8788                 :            : 
    8789                 :            : 
    8790                 :        289 : static int get_key_handler(struct nl_msg *msg, void *arg)
    8791                 :            : {
    8792                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    8793                 :        289 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    8794                 :            : 
    8795                 :        289 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    8796                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    8797                 :            : 
    8798                 :            :         /*
    8799                 :            :          * TODO: validate the key index and mac address!
    8800                 :            :          * Otherwise, there's a race condition as soon as
    8801                 :            :          * the kernel starts sending key notifications.
    8802                 :            :          */
    8803                 :            : 
    8804         [ +  + ]:        289 :         if (tb[NL80211_ATTR_KEY_SEQ])
    8805                 :        286 :                 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
    8806                 :        286 :                        min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
    8807                 :        289 :         return NL_SKIP;
    8808                 :            : }
    8809                 :            : 
    8810                 :            : 
    8811                 :        289 : static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
    8812                 :            :                            int idx, u8 *seq)
    8813                 :            : {
    8814                 :        289 :         struct i802_bss *bss = priv;
    8815                 :        289 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8816                 :            :         struct nl_msg *msg;
    8817                 :            : 
    8818                 :        289 :         msg = nlmsg_alloc();
    8819         [ -  + ]:        289 :         if (!msg)
    8820                 :          0 :                 return -ENOMEM;
    8821                 :            : 
    8822                 :        289 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
    8823                 :            : 
    8824         [ -  + ]:        289 :         if (addr)
    8825         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    8826         [ +  - ]:        289 :         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
    8827         [ +  - ]:        289 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
    8828                 :            : 
    8829                 :        289 :         memset(seq, 0, 6);
    8830                 :            : 
    8831                 :        289 :         return send_and_recv_msgs(drv, msg, get_key_handler, seq);
    8832                 :            :  nla_put_failure:
    8833                 :          0 :         nlmsg_free(msg);
    8834                 :        289 :         return -ENOBUFS;
    8835                 :            : }
    8836                 :            : 
    8837                 :            : 
    8838                 :          0 : static int i802_set_rts(void *priv, int rts)
    8839                 :            : {
    8840                 :          0 :         struct i802_bss *bss = priv;
    8841                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8842                 :            :         struct nl_msg *msg;
    8843                 :          0 :         int ret = -ENOBUFS;
    8844                 :            :         u32 val;
    8845                 :            : 
    8846                 :          0 :         msg = nlmsg_alloc();
    8847         [ #  # ]:          0 :         if (!msg)
    8848                 :          0 :                 return -ENOMEM;
    8849                 :            : 
    8850         [ #  # ]:          0 :         if (rts >= 2347)
    8851                 :          0 :                 val = (u32) -1;
    8852                 :            :         else
    8853                 :          0 :                 val = rts;
    8854                 :            : 
    8855                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
    8856         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    8857         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
    8858                 :            : 
    8859                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    8860                 :          0 :         msg = NULL;
    8861         [ #  # ]:          0 :         if (!ret)
    8862                 :          0 :                 return 0;
    8863                 :            : nla_put_failure:
    8864                 :          0 :         nlmsg_free(msg);
    8865                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
    8866                 :            :                    "%d (%s)", rts, ret, strerror(-ret));
    8867                 :          0 :         return ret;
    8868                 :            : }
    8869                 :            : 
    8870                 :            : 
    8871                 :          0 : static int i802_set_frag(void *priv, int frag)
    8872                 :            : {
    8873                 :          0 :         struct i802_bss *bss = priv;
    8874                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8875                 :            :         struct nl_msg *msg;
    8876                 :          0 :         int ret = -ENOBUFS;
    8877                 :            :         u32 val;
    8878                 :            : 
    8879                 :          0 :         msg = nlmsg_alloc();
    8880         [ #  # ]:          0 :         if (!msg)
    8881                 :          0 :                 return -ENOMEM;
    8882                 :            : 
    8883         [ #  # ]:          0 :         if (frag >= 2346)
    8884                 :          0 :                 val = (u32) -1;
    8885                 :            :         else
    8886                 :          0 :                 val = frag;
    8887                 :            : 
    8888                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
    8889         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    8890         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
    8891                 :            : 
    8892                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    8893                 :          0 :         msg = NULL;
    8894         [ #  # ]:          0 :         if (!ret)
    8895                 :          0 :                 return 0;
    8896                 :            : nla_put_failure:
    8897                 :          0 :         nlmsg_free(msg);
    8898                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
    8899                 :            :                    "%d: %d (%s)", frag, ret, strerror(-ret));
    8900                 :          0 :         return ret;
    8901                 :            : }
    8902                 :            : 
    8903                 :            : 
    8904                 :        531 : static int i802_flush(void *priv)
    8905                 :            : {
    8906                 :        531 :         struct i802_bss *bss = priv;
    8907                 :        531 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8908                 :            :         struct nl_msg *msg;
    8909                 :            :         int res;
    8910                 :            : 
    8911                 :        531 :         msg = nlmsg_alloc();
    8912         [ -  + ]:        531 :         if (!msg)
    8913                 :          0 :                 return -1;
    8914                 :            : 
    8915                 :        531 :         wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
    8916                 :        531 :                    bss->ifname);
    8917                 :        531 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
    8918                 :            : 
    8919                 :            :         /*
    8920                 :            :          * XXX: FIX! this needs to flush all VLANs too
    8921                 :            :          */
    8922         [ +  - ]:        531 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
    8923                 :            :                     if_nametoindex(bss->ifname));
    8924                 :            : 
    8925                 :        531 :         res = send_and_recv_msgs(drv, msg, NULL, NULL);
    8926         [ -  + ]:        531 :         if (res) {
    8927                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
    8928                 :            :                            "(%s)", res, strerror(-res));
    8929                 :            :         }
    8930                 :        531 :         return res;
    8931                 :            :  nla_put_failure:
    8932                 :          0 :         nlmsg_free(msg);
    8933                 :        531 :         return -ENOBUFS;
    8934                 :            : }
    8935                 :            : 
    8936                 :            : 
    8937                 :          0 : static int get_sta_handler(struct nl_msg *msg, void *arg)
    8938                 :            : {
    8939                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    8940                 :          0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    8941                 :          0 :         struct hostap_sta_driver_data *data = arg;
    8942                 :            :         struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
    8943                 :            :         static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
    8944                 :            :                 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
    8945                 :            :                 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
    8946                 :            :                 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
    8947                 :            :                 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
    8948                 :            :                 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
    8949                 :            :                 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
    8950                 :            :         };
    8951                 :            : 
    8952                 :          0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    8953                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    8954                 :            : 
    8955                 :            :         /*
    8956                 :            :          * TODO: validate the interface and mac address!
    8957                 :            :          * Otherwise, there's a race condition as soon as
    8958                 :            :          * the kernel starts sending station notifications.
    8959                 :            :          */
    8960                 :            : 
    8961         [ #  # ]:          0 :         if (!tb[NL80211_ATTR_STA_INFO]) {
    8962                 :          0 :                 wpa_printf(MSG_DEBUG, "sta stats missing!");
    8963                 :          0 :                 return NL_SKIP;
    8964                 :            :         }
    8965         [ #  # ]:          0 :         if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
    8966                 :            :                              tb[NL80211_ATTR_STA_INFO],
    8967                 :            :                              stats_policy)) {
    8968                 :          0 :                 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
    8969                 :          0 :                 return NL_SKIP;
    8970                 :            :         }
    8971                 :            : 
    8972         [ #  # ]:          0 :         if (stats[NL80211_STA_INFO_INACTIVE_TIME])
    8973                 :          0 :                 data->inactive_msec =
    8974                 :          0 :                         nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
    8975         [ #  # ]:          0 :         if (stats[NL80211_STA_INFO_RX_BYTES])
    8976                 :          0 :                 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
    8977         [ #  # ]:          0 :         if (stats[NL80211_STA_INFO_TX_BYTES])
    8978                 :          0 :                 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
    8979         [ #  # ]:          0 :         if (stats[NL80211_STA_INFO_RX_PACKETS])
    8980                 :          0 :                 data->rx_packets =
    8981                 :          0 :                         nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
    8982         [ #  # ]:          0 :         if (stats[NL80211_STA_INFO_TX_PACKETS])
    8983                 :          0 :                 data->tx_packets =
    8984                 :          0 :                         nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
    8985         [ #  # ]:          0 :         if (stats[NL80211_STA_INFO_TX_FAILED])
    8986                 :          0 :                 data->tx_retry_failed =
    8987                 :          0 :                         nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
    8988                 :            : 
    8989                 :          0 :         return NL_SKIP;
    8990                 :            : }
    8991                 :            : 
    8992                 :          0 : static int i802_read_sta_data(struct i802_bss *bss,
    8993                 :            :                               struct hostap_sta_driver_data *data,
    8994                 :            :                               const u8 *addr)
    8995                 :            : {
    8996                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8997                 :            :         struct nl_msg *msg;
    8998                 :            : 
    8999                 :          0 :         os_memset(data, 0, sizeof(*data));
    9000                 :          0 :         msg = nlmsg_alloc();
    9001         [ #  # ]:          0 :         if (!msg)
    9002                 :          0 :                 return -ENOMEM;
    9003                 :            : 
    9004                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
    9005                 :            : 
    9006         [ #  # ]:          0 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    9007         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
    9008                 :            : 
    9009                 :          0 :         return send_and_recv_msgs(drv, msg, get_sta_handler, data);
    9010                 :            :  nla_put_failure:
    9011                 :          0 :         nlmsg_free(msg);
    9012                 :          0 :         return -ENOBUFS;
    9013                 :            : }
    9014                 :            : 
    9015                 :            : 
    9016                 :        952 : static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
    9017                 :            :                                     int cw_min, int cw_max, int burst_time)
    9018                 :            : {
    9019                 :        952 :         struct i802_bss *bss = priv;
    9020                 :        952 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9021                 :            :         struct nl_msg *msg;
    9022                 :            :         struct nlattr *txq, *params;
    9023                 :            : 
    9024                 :        952 :         msg = nlmsg_alloc();
    9025         [ -  + ]:        952 :         if (!msg)
    9026                 :          0 :                 return -1;
    9027                 :            : 
    9028                 :        952 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
    9029                 :            : 
    9030         [ +  - ]:        952 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
    9031                 :            : 
    9032                 :        952 :         txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
    9033         [ -  + ]:        952 :         if (!txq)
    9034                 :          0 :                 goto nla_put_failure;
    9035                 :            : 
    9036                 :            :         /* We are only sending parameters for a single TXQ at a time */
    9037                 :        952 :         params = nla_nest_start(msg, 1);
    9038         [ -  + ]:        952 :         if (!params)
    9039                 :          0 :                 goto nla_put_failure;
    9040                 :            : 
    9041   [ +  +  +  +  :        952 :         switch (queue) {
                      - ]
    9042                 :            :         case 0:
    9043         [ +  - ]:        238 :                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
    9044                 :        238 :                 break;
    9045                 :            :         case 1:
    9046         [ +  - ]:        238 :                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
    9047                 :        238 :                 break;
    9048                 :            :         case 2:
    9049         [ +  - ]:        238 :                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
    9050                 :        238 :                 break;
    9051                 :            :         case 3:
    9052         [ +  - ]:        238 :                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
    9053                 :        238 :                 break;
    9054                 :            :         }
    9055                 :            :         /* Burst time is configured in units of 0.1 msec and TXOP parameter in
    9056                 :            :          * 32 usec, so need to convert the value here. */
    9057         [ +  - ]:        952 :         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
    9058         [ +  - ]:        952 :         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
    9059         [ +  - ]:        952 :         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
    9060         [ +  - ]:        952 :         NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
    9061                 :            : 
    9062                 :        952 :         nla_nest_end(msg, params);
    9063                 :            : 
    9064                 :        952 :         nla_nest_end(msg, txq);
    9065                 :            : 
    9066         [ +  - ]:        952 :         if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
    9067                 :        952 :                 return 0;
    9068                 :          0 :         msg = NULL;
    9069                 :            :  nla_put_failure:
    9070                 :          0 :         nlmsg_free(msg);
    9071                 :        952 :         return -1;
    9072                 :            : }
    9073                 :            : 
    9074                 :            : 
    9075                 :          0 : static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
    9076                 :            :                              const char *ifname, int vlan_id)
    9077                 :            : {
    9078                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9079                 :            :         struct nl_msg *msg;
    9080                 :          0 :         int ret = -ENOBUFS;
    9081                 :            : 
    9082                 :          0 :         msg = nlmsg_alloc();
    9083         [ #  # ]:          0 :         if (!msg)
    9084                 :          0 :                 return -ENOMEM;
    9085                 :            : 
    9086                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
    9087                 :            :                    ", ifname=%s[%d], vlan_id=%d)",
    9088                 :          0 :                    bss->ifname, if_nametoindex(bss->ifname),
    9089                 :          0 :                    MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
    9090                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
    9091                 :            : 
    9092         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
    9093                 :            :                     if_nametoindex(bss->ifname));
    9094         [ #  # ]:          0 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    9095         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
    9096                 :            :                     if_nametoindex(ifname));
    9097                 :            : 
    9098                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    9099                 :          0 :         msg = NULL;
    9100         [ #  # ]:          0 :         if (ret < 0) {
    9101                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
    9102                 :            :                            MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
    9103                 :          0 :                            MAC2STR(addr), ifname, vlan_id, ret,
    9104                 :            :                            strerror(-ret));
    9105                 :            :         }
    9106                 :            :  nla_put_failure:
    9107                 :          0 :         nlmsg_free(msg);
    9108                 :          0 :         return ret;
    9109                 :            : }
    9110                 :            : 
    9111                 :            : 
    9112                 :          0 : static int i802_get_inact_sec(void *priv, const u8 *addr)
    9113                 :            : {
    9114                 :            :         struct hostap_sta_driver_data data;
    9115                 :            :         int ret;
    9116                 :            : 
    9117                 :          0 :         data.inactive_msec = (unsigned long) -1;
    9118                 :          0 :         ret = i802_read_sta_data(priv, &data, addr);
    9119 [ #  # ][ #  # ]:          0 :         if (ret || data.inactive_msec == (unsigned long) -1)
    9120                 :          0 :                 return -1;
    9121                 :          0 :         return data.inactive_msec / 1000;
    9122                 :            : }
    9123                 :            : 
    9124                 :            : 
    9125                 :        266 : static int i802_sta_clear_stats(void *priv, const u8 *addr)
    9126                 :            : {
    9127                 :            : #if 0
    9128                 :            :         /* TODO */
    9129                 :            : #endif
    9130                 :        266 :         return 0;
    9131                 :            : }
    9132                 :            : 
    9133                 :            : 
    9134                 :        631 : static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
    9135                 :            :                            int reason)
    9136                 :            : {
    9137                 :        631 :         struct i802_bss *bss = priv;
    9138                 :        631 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9139                 :            :         struct ieee80211_mgmt mgmt;
    9140                 :            : 
    9141         [ -  + ]:        631 :         if (drv->device_ap_sme)
    9142                 :          0 :                 return wpa_driver_nl80211_sta_remove(bss, addr);
    9143                 :            : 
    9144                 :        631 :         memset(&mgmt, 0, sizeof(mgmt));
    9145                 :        631 :         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
    9146                 :            :                                           WLAN_FC_STYPE_DEAUTH);
    9147                 :        631 :         memcpy(mgmt.da, addr, ETH_ALEN);
    9148                 :        631 :         memcpy(mgmt.sa, own_addr, ETH_ALEN);
    9149                 :        631 :         memcpy(mgmt.bssid, own_addr, ETH_ALEN);
    9150                 :        631 :         mgmt.u.deauth.reason_code = host_to_le16(reason);
    9151                 :        631 :         return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
    9152                 :            :                                             IEEE80211_HDRLEN +
    9153                 :            :                                             sizeof(mgmt.u.deauth), 0, 0, 0, 0,
    9154                 :            :                                             0);
    9155                 :            : }
    9156                 :            : 
    9157                 :            : 
    9158                 :          0 : static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
    9159                 :            :                              int reason)
    9160                 :            : {
    9161                 :          0 :         struct i802_bss *bss = priv;
    9162                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9163                 :            :         struct ieee80211_mgmt mgmt;
    9164                 :            : 
    9165         [ #  # ]:          0 :         if (drv->device_ap_sme)
    9166                 :          0 :                 return wpa_driver_nl80211_sta_remove(bss, addr);
    9167                 :            : 
    9168                 :          0 :         memset(&mgmt, 0, sizeof(mgmt));
    9169                 :          0 :         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
    9170                 :            :                                           WLAN_FC_STYPE_DISASSOC);
    9171                 :          0 :         memcpy(mgmt.da, addr, ETH_ALEN);
    9172                 :          0 :         memcpy(mgmt.sa, own_addr, ETH_ALEN);
    9173                 :          0 :         memcpy(mgmt.bssid, own_addr, ETH_ALEN);
    9174                 :          0 :         mgmt.u.disassoc.reason_code = host_to_le16(reason);
    9175                 :          0 :         return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
    9176                 :            :                                             IEEE80211_HDRLEN +
    9177                 :            :                                             sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
    9178                 :            :                                             0);
    9179                 :            : }
    9180                 :            : 
    9181                 :            : 
    9182                 :        226 : static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
    9183                 :            : {
    9184                 :            :         int i;
    9185                 :            :         int *old;
    9186                 :            : 
    9187                 :        226 :         wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
    9188                 :            :                    ifidx);
    9189         [ +  - ]:        251 :         for (i = 0; i < drv->num_if_indices; i++) {
    9190         [ +  + ]:        251 :                 if (drv->if_indices[i] == 0) {
    9191                 :        226 :                         drv->if_indices[i] = ifidx;
    9192                 :        226 :                         return;
    9193                 :            :                 }
    9194                 :            :         }
    9195                 :            : 
    9196         [ #  # ]:          0 :         if (drv->if_indices != drv->default_if_indices)
    9197                 :          0 :                 old = drv->if_indices;
    9198                 :            :         else
    9199                 :          0 :                 old = NULL;
    9200                 :            : 
    9201                 :          0 :         drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
    9202                 :            :                                            sizeof(int));
    9203         [ #  # ]:          0 :         if (!drv->if_indices) {
    9204         [ #  # ]:          0 :                 if (!old)
    9205                 :          0 :                         drv->if_indices = drv->default_if_indices;
    9206                 :            :                 else
    9207                 :          0 :                         drv->if_indices = old;
    9208                 :          0 :                 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
    9209                 :            :                            "interfaces");
    9210                 :          0 :                 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
    9211                 :          0 :                 return;
    9212         [ #  # ]:          0 :         } else if (!old)
    9213                 :          0 :                 os_memcpy(drv->if_indices, drv->default_if_indices,
    9214                 :            :                           sizeof(drv->default_if_indices));
    9215                 :          0 :         drv->if_indices[drv->num_if_indices] = ifidx;
    9216                 :        226 :         drv->num_if_indices++;
    9217                 :            : }
    9218                 :            : 
    9219                 :            : 
    9220                 :         36 : static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
    9221                 :            : {
    9222                 :            :         int i;
    9223                 :            : 
    9224         [ +  - ]:         61 :         for (i = 0; i < drv->num_if_indices; i++) {
    9225         [ +  + ]:         61 :                 if (drv->if_indices[i] == ifidx) {
    9226                 :         36 :                         drv->if_indices[i] = 0;
    9227                 :         36 :                         break;
    9228                 :            :                 }
    9229                 :            :         }
    9230                 :         36 : }
    9231                 :            : 
    9232                 :            : 
    9233                 :       9120 : static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
    9234                 :            : {
    9235                 :            :         int i;
    9236                 :            : 
    9237         [ +  + ]:     136699 :         for (i = 0; i < drv->num_if_indices; i++)
    9238         [ +  + ]:     128732 :                 if (drv->if_indices[i] == ifidx)
    9239                 :       1153 :                         return 1;
    9240                 :            : 
    9241                 :       9120 :         return 0;
    9242                 :            : }
    9243                 :            : 
    9244                 :            : 
    9245                 :          0 : static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
    9246                 :            :                             const char *bridge_ifname, char *ifname_wds)
    9247                 :            : {
    9248                 :          0 :         struct i802_bss *bss = priv;
    9249                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9250                 :            :         char name[IFNAMSIZ + 1];
    9251                 :            : 
    9252                 :          0 :         os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
    9253         [ #  # ]:          0 :         if (ifname_wds)
    9254                 :          0 :                 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
    9255                 :            : 
    9256                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
    9257                 :          0 :                    " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
    9258         [ #  # ]:          0 :         if (val) {
    9259         [ #  # ]:          0 :                 if (!if_nametoindex(name)) {
    9260         [ #  # ]:          0 :                         if (nl80211_create_iface(drv, name,
    9261                 :            :                                                  NL80211_IFTYPE_AP_VLAN,
    9262                 :          0 :                                                  bss->addr, 1, NULL, NULL, 0) <
    9263                 :            :                             0)
    9264                 :          0 :                                 return -1;
    9265   [ #  #  #  # ]:          0 :                         if (bridge_ifname &&
    9266                 :          0 :                             linux_br_add_if(drv->global->ioctl_sock,
    9267                 :            :                                             bridge_ifname, name) < 0)
    9268                 :          0 :                                 return -1;
    9269                 :            :                 }
    9270         [ #  # ]:          0 :                 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
    9271                 :          0 :                         wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
    9272                 :            :                                    "interface %s up", name);
    9273                 :            :                 }
    9274                 :          0 :                 return i802_set_sta_vlan(priv, addr, name, 0);
    9275                 :            :         } else {
    9276         [ #  # ]:          0 :                 if (bridge_ifname)
    9277                 :          0 :                         linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
    9278                 :            :                                         name);
    9279                 :            : 
    9280                 :          0 :                 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
    9281                 :          0 :                 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
    9282                 :            :                                                     name);
    9283                 :            :         }
    9284                 :            : }
    9285                 :            : 
    9286                 :            : 
    9287                 :       2549 : static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
    9288                 :            : {
    9289                 :       2549 :         struct wpa_driver_nl80211_data *drv = eloop_ctx;
    9290                 :            :         struct sockaddr_ll lladdr;
    9291                 :            :         unsigned char buf[3000];
    9292                 :            :         int len;
    9293                 :       2549 :         socklen_t fromlen = sizeof(lladdr);
    9294                 :            : 
    9295                 :       2549 :         len = recvfrom(sock, buf, sizeof(buf), 0,
    9296                 :            :                        (struct sockaddr *)&lladdr, &fromlen);
    9297         [ -  + ]:       2549 :         if (len < 0) {
    9298                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
    9299                 :          0 :                            strerror(errno));
    9300                 :       2549 :                 return;
    9301                 :            :         }
    9302                 :            : 
    9303         [ +  + ]:       2549 :         if (have_ifidx(drv, lladdr.sll_ifindex))
    9304                 :       1072 :                 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
    9305                 :            : }
    9306                 :            : 
    9307                 :            : 
    9308                 :          0 : static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
    9309                 :            :                              struct i802_bss *bss,
    9310                 :            :                              const char *brname, const char *ifname)
    9311                 :            : {
    9312                 :            :         int ifindex;
    9313                 :            :         char in_br[IFNAMSIZ];
    9314                 :            : 
    9315                 :          0 :         os_strlcpy(bss->brname, brname, IFNAMSIZ);
    9316                 :          0 :         ifindex = if_nametoindex(brname);
    9317         [ #  # ]:          0 :         if (ifindex == 0) {
    9318                 :            :                 /*
    9319                 :            :                  * Bridge was configured, but the bridge device does
    9320                 :            :                  * not exist. Try to add it now.
    9321                 :            :                  */
    9322         [ #  # ]:          0 :                 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
    9323                 :          0 :                         wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
    9324                 :            :                                    "bridge interface %s: %s",
    9325                 :          0 :                                    brname, strerror(errno));
    9326                 :          0 :                         return -1;
    9327                 :            :                 }
    9328                 :          0 :                 bss->added_bridge = 1;
    9329                 :          0 :                 add_ifidx(drv, if_nametoindex(brname));
    9330                 :            :         }
    9331                 :            : 
    9332         [ #  # ]:          0 :         if (linux_br_get(in_br, ifname) == 0) {
    9333         [ #  # ]:          0 :                 if (os_strcmp(in_br, brname) == 0)
    9334                 :          0 :                         return 0; /* already in the bridge */
    9335                 :            : 
    9336                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
    9337                 :            :                            "bridge %s", ifname, in_br);
    9338         [ #  # ]:          0 :                 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
    9339                 :            :                     0) {
    9340                 :          0 :                         wpa_printf(MSG_ERROR, "nl80211: Failed to "
    9341                 :            :                                    "remove interface %s from bridge "
    9342                 :            :                                    "%s: %s",
    9343                 :          0 :                                    ifname, brname, strerror(errno));
    9344                 :          0 :                         return -1;
    9345                 :            :                 }
    9346                 :            :         }
    9347                 :            : 
    9348                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
    9349                 :            :                    ifname, brname);
    9350         [ #  # ]:          0 :         if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
    9351                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
    9352                 :            :                            "into bridge %s: %s",
    9353                 :          0 :                            ifname, brname, strerror(errno));
    9354                 :          0 :                 return -1;
    9355                 :            :         }
    9356                 :          0 :         bss->added_if_into_bridge = 1;
    9357                 :            : 
    9358                 :          0 :         return 0;
    9359                 :            : }
    9360                 :            : 
    9361                 :            : 
    9362                 :        190 : static void *i802_init(struct hostapd_data *hapd,
    9363                 :            :                        struct wpa_init_params *params)
    9364                 :            : {
    9365                 :            :         struct wpa_driver_nl80211_data *drv;
    9366                 :            :         struct i802_bss *bss;
    9367                 :            :         size_t i;
    9368                 :            :         char brname[IFNAMSIZ];
    9369                 :            :         int ifindex, br_ifindex;
    9370                 :        190 :         int br_added = 0;
    9371                 :            : 
    9372                 :        190 :         bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
    9373                 :            :                                           params->global_priv, 1,
    9374                 :            :                                           params->bssid);
    9375         [ -  + ]:        190 :         if (bss == NULL)
    9376                 :          0 :                 return NULL;
    9377                 :            : 
    9378                 :        190 :         drv = bss->drv;
    9379                 :            : 
    9380         [ -  + ]:        190 :         if (linux_br_get(brname, params->ifname) == 0) {
    9381                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
    9382                 :            :                            params->ifname, brname);
    9383                 :          0 :                 br_ifindex = if_nametoindex(brname);
    9384                 :            :         } else {
    9385                 :        190 :                 brname[0] = '\0';
    9386                 :        190 :                 br_ifindex = 0;
    9387                 :            :         }
    9388                 :            : 
    9389         [ +  + ]:        386 :         for (i = 0; i < params->num_bridge; i++) {
    9390         [ -  + ]:        196 :                 if (params->bridge[i]) {
    9391                 :          0 :                         ifindex = if_nametoindex(params->bridge[i]);
    9392         [ #  # ]:          0 :                         if (ifindex)
    9393                 :          0 :                                 add_ifidx(drv, ifindex);
    9394         [ #  # ]:          0 :                         if (ifindex == br_ifindex)
    9395                 :          0 :                                 br_added = 1;
    9396                 :            :                 }
    9397                 :            :         }
    9398 [ +  - ][ -  + ]:        190 :         if (!br_added && br_ifindex &&
                 [ #  # ]
    9399         [ #  # ]:          0 :             (params->num_bridge == 0 || !params->bridge[0]))
    9400                 :          0 :                 add_ifidx(drv, br_ifindex);
    9401                 :            : 
    9402                 :            :         /* start listening for EAPOL on the default AP interface */
    9403                 :        190 :         add_ifidx(drv, drv->ifindex);
    9404                 :            : 
    9405   [ -  +  #  # ]:        190 :         if (params->num_bridge && params->bridge[0] &&
                 [ +  - ]
    9406                 :          0 :             i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
    9407                 :          0 :                 goto failed;
    9408                 :            : 
    9409                 :        190 :         drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
    9410         [ -  + ]:        190 :         if (drv->eapol_sock < 0) {
    9411                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
    9412                 :          0 :                            strerror(errno));
    9413                 :          0 :                 goto failed;
    9414                 :            :         }
    9415                 :            : 
    9416         [ -  + ]:        190 :         if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
    9417                 :            :         {
    9418                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
    9419                 :          0 :                 goto failed;
    9420                 :            :         }
    9421                 :            : 
    9422         [ -  + ]:        190 :         if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
    9423                 :            :                                params->own_addr))
    9424                 :          0 :                 goto failed;
    9425                 :            : 
    9426                 :        190 :         memcpy(bss->addr, params->own_addr, ETH_ALEN);
    9427                 :            : 
    9428                 :        190 :         return bss;
    9429                 :            : 
    9430                 :            : failed:
    9431                 :          0 :         wpa_driver_nl80211_deinit(bss);
    9432                 :        190 :         return NULL;
    9433                 :            : }
    9434                 :            : 
    9435                 :            : 
    9436                 :        190 : static void i802_deinit(void *priv)
    9437                 :            : {
    9438                 :        190 :         struct i802_bss *bss = priv;
    9439                 :        190 :         wpa_driver_nl80211_deinit(bss);
    9440                 :        190 : }
    9441                 :            : 
    9442                 :            : 
    9443                 :         36 : static enum nl80211_iftype wpa_driver_nl80211_if_type(
    9444                 :            :         enum wpa_driver_if_type type)
    9445                 :            : {
    9446   [ -  +  -  +  :         36 :         switch (type) {
                +  -  - ]
    9447                 :            :         case WPA_IF_STATION:
    9448                 :          0 :                 return NL80211_IFTYPE_STATION;
    9449                 :            :         case WPA_IF_P2P_CLIENT:
    9450                 :            :         case WPA_IF_P2P_GROUP:
    9451                 :         10 :                 return NL80211_IFTYPE_P2P_CLIENT;
    9452                 :            :         case WPA_IF_AP_VLAN:
    9453                 :          0 :                 return NL80211_IFTYPE_AP_VLAN;
    9454                 :            :         case WPA_IF_AP_BSS:
    9455                 :         17 :                 return NL80211_IFTYPE_AP;
    9456                 :            :         case WPA_IF_P2P_GO:
    9457                 :          9 :                 return NL80211_IFTYPE_P2P_GO;
    9458                 :            :         case WPA_IF_P2P_DEVICE:
    9459                 :          0 :                 return NL80211_IFTYPE_P2P_DEVICE;
    9460                 :            :         }
    9461                 :         36 :         return -1;
    9462                 :            : }
    9463                 :            : 
    9464                 :            : 
    9465                 :            : #ifdef CONFIG_P2P
    9466                 :            : 
    9467                 :         19 : static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
    9468                 :            : {
    9469                 :            :         struct wpa_driver_nl80211_data *drv;
    9470         [ +  + ]:         38 :         dl_list_for_each(drv, &global->interfaces,
    9471                 :            :                          struct wpa_driver_nl80211_data, list) {
    9472         [ -  + ]:         19 :                 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
    9473                 :          0 :                         return 1;
    9474                 :            :         }
    9475                 :         19 :         return 0;
    9476                 :            : }
    9477                 :            : 
    9478                 :            : 
    9479                 :          0 : static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
    9480                 :            :                                       u8 *new_addr)
    9481                 :            : {
    9482                 :            :         unsigned int idx;
    9483                 :            : 
    9484         [ #  # ]:          0 :         if (!drv->global)
    9485                 :          0 :                 return -1;
    9486                 :            : 
    9487                 :          0 :         os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
    9488         [ #  # ]:          0 :         for (idx = 0; idx < 64; idx++) {
    9489                 :          0 :                 new_addr[0] = drv->first_bss->addr[0] | 0x02;
    9490                 :          0 :                 new_addr[0] ^= idx << 2;
    9491         [ #  # ]:          0 :                 if (!nl80211_addr_in_use(drv->global, new_addr))
    9492                 :          0 :                         break;
    9493                 :            :         }
    9494         [ #  # ]:          0 :         if (idx == 64)
    9495                 :          0 :                 return -1;
    9496                 :            : 
    9497                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
    9498                 :          0 :                    MACSTR, MAC2STR(new_addr));
    9499                 :            : 
    9500                 :          0 :         return 0;
    9501                 :            : }
    9502                 :            : 
    9503                 :            : #endif /* CONFIG_P2P */
    9504                 :            : 
    9505                 :            : 
    9506                 :            : struct wdev_info {
    9507                 :            :         u64 wdev_id;
    9508                 :            :         int wdev_id_set;
    9509                 :            :         u8 macaddr[ETH_ALEN];
    9510                 :            : };
    9511                 :            : 
    9512                 :          0 : static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
    9513                 :            : {
    9514                 :          0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    9515                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    9516                 :          0 :         struct wdev_info *wi = arg;
    9517                 :            : 
    9518                 :          0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    9519                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    9520         [ #  # ]:          0 :         if (tb[NL80211_ATTR_WDEV]) {
    9521                 :          0 :                 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
    9522                 :          0 :                 wi->wdev_id_set = 1;
    9523                 :            :         }
    9524                 :            : 
    9525         [ #  # ]:          0 :         if (tb[NL80211_ATTR_MAC])
    9526                 :          0 :                 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
    9527                 :            :                           ETH_ALEN);
    9528                 :            : 
    9529                 :          0 :         return NL_SKIP;
    9530                 :            : }
    9531                 :            : 
    9532                 :            : 
    9533                 :         36 : static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
    9534                 :            :                                      const char *ifname, const u8 *addr,
    9535                 :            :                                      void *bss_ctx, void **drv_priv,
    9536                 :            :                                      char *force_ifname, u8 *if_addr,
    9537                 :            :                                      const char *bridge, int use_existing)
    9538                 :            : {
    9539                 :            :         enum nl80211_iftype nlmode;
    9540                 :         36 :         struct i802_bss *bss = priv;
    9541                 :         36 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9542                 :            :         int ifidx;
    9543                 :         36 :         int added = 1;
    9544                 :            : 
    9545         [ +  + ]:         36 :         if (addr)
    9546                 :         17 :                 os_memcpy(if_addr, addr, ETH_ALEN);
    9547                 :         36 :         nlmode = wpa_driver_nl80211_if_type(type);
    9548         [ -  + ]:         36 :         if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
    9549                 :            :                 struct wdev_info p2pdev_info;
    9550                 :            : 
    9551                 :          0 :                 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
    9552                 :          0 :                 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
    9553                 :            :                                              0, nl80211_wdev_handler,
    9554                 :            :                                              &p2pdev_info, use_existing);
    9555 [ #  # ][ #  # ]:          0 :                 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
    9556                 :          0 :                         wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
    9557                 :            :                                    ifname);
    9558                 :          0 :                         return -1;
    9559                 :            :                 }
    9560                 :            : 
    9561                 :          0 :                 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
    9562                 :          0 :                 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
    9563         [ #  # ]:          0 :                 if (!is_zero_ether_addr(p2pdev_info.macaddr))
    9564                 :          0 :                         os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
    9565                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
    9566                 :            :                            ifname,
    9567                 :          0 :                            (long long unsigned int) p2pdev_info.wdev_id);
    9568                 :            :         } else {
    9569                 :         36 :                 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
    9570                 :            :                                              0, NULL, NULL, use_existing);
    9571 [ -  + ][ +  + ]:         36 :                 if (use_existing && ifidx == -ENFILE) {
    9572                 :          0 :                         added = 0;
    9573                 :          0 :                         ifidx = if_nametoindex(ifname);
    9574         [ -  + ]:         36 :                 } else if (ifidx < 0) {
    9575                 :          0 :                         return -1;
    9576                 :            :                 }
    9577                 :            :         }
    9578                 :            : 
    9579         [ +  + ]:         36 :         if (!addr) {
    9580         [ -  + ]:         19 :                 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
    9581                 :          0 :                         os_memcpy(if_addr, bss->addr, ETH_ALEN);
    9582         [ -  + ]:         19 :                 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
    9583                 :         19 :                                             bss->ifname, if_addr) < 0) {
    9584         [ #  # ]:          0 :                         if (added)
    9585                 :          0 :                                 nl80211_remove_iface(drv, ifidx);
    9586                 :          0 :                         return -1;
    9587                 :            :                 }
    9588                 :            :         }
    9589                 :            : 
    9590                 :            : #ifdef CONFIG_P2P
    9591 [ +  - ][ +  + ]:         19 :         if (!addr &&
    9592 [ +  + ][ +  - ]:         16 :             (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
    9593                 :            :              type == WPA_IF_P2P_GO)) {
    9594                 :            :                 /* Enforce unique P2P Interface Address */
    9595                 :            :                 u8 new_addr[ETH_ALEN];
    9596                 :            : 
    9597         [ -  + ]:         19 :                 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
    9598                 :            :                                        new_addr) < 0) {
    9599                 :          0 :                         nl80211_remove_iface(drv, ifidx);
    9600                 :          0 :                         return -1;
    9601                 :            :                 }
    9602         [ -  + ]:         19 :                 if (nl80211_addr_in_use(drv->global, new_addr)) {
    9603                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
    9604                 :            :                                    "for P2P group interface");
    9605         [ #  # ]:          0 :                         if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
    9606                 :          0 :                                 nl80211_remove_iface(drv, ifidx);
    9607                 :          0 :                                 return -1;
    9608                 :            :                         }
    9609         [ #  # ]:          0 :                         if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
    9610                 :            :                                                new_addr) < 0) {
    9611                 :          0 :                                 nl80211_remove_iface(drv, ifidx);
    9612                 :          0 :                                 return -1;
    9613                 :            :                         }
    9614                 :            :                 }
    9615                 :         19 :                 os_memcpy(if_addr, new_addr, ETH_ALEN);
    9616                 :            :         }
    9617                 :            : #endif /* CONFIG_P2P */
    9618                 :            : 
    9619         [ +  + ]:         36 :         if (type == WPA_IF_AP_BSS) {
    9620                 :         17 :                 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
    9621         [ -  + ]:         17 :                 if (new_bss == NULL) {
    9622         [ #  # ]:          0 :                         if (added)
    9623                 :          0 :                                 nl80211_remove_iface(drv, ifidx);
    9624                 :          0 :                         return -1;
    9625                 :            :                 }
    9626                 :            : 
    9627   [ -  +  #  # ]:         17 :                 if (bridge &&
    9628                 :          0 :                     i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
    9629                 :          0 :                         wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
    9630                 :            :                                    "interface %s to a bridge %s",
    9631                 :            :                                    ifname, bridge);
    9632         [ #  # ]:          0 :                         if (added)
    9633                 :          0 :                                 nl80211_remove_iface(drv, ifidx);
    9634                 :          0 :                         os_free(new_bss);
    9635                 :          0 :                         return -1;
    9636                 :            :                 }
    9637                 :            : 
    9638         [ -  + ]:         17 :                 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
    9639                 :            :                 {
    9640                 :          0 :                         nl80211_remove_iface(drv, ifidx);
    9641                 :          0 :                         os_free(new_bss);
    9642                 :          0 :                         return -1;
    9643                 :            :                 }
    9644                 :         17 :                 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
    9645                 :         17 :                 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
    9646                 :         17 :                 new_bss->ifindex = ifidx;
    9647                 :         17 :                 new_bss->drv = drv;
    9648                 :         17 :                 new_bss->next = drv->first_bss->next;
    9649                 :         17 :                 new_bss->freq = drv->first_bss->freq;
    9650                 :         17 :                 new_bss->ctx = bss_ctx;
    9651                 :         17 :                 new_bss->added_if = added;
    9652                 :         17 :                 drv->first_bss->next = new_bss;
    9653         [ +  - ]:         17 :                 if (drv_priv)
    9654                 :         17 :                         *drv_priv = new_bss;
    9655                 :         17 :                 nl80211_init_bss(new_bss);
    9656                 :            : 
    9657                 :            :                 /* Subscribe management frames for this WPA_IF_AP_BSS */
    9658         [ -  + ]:         17 :                 if (nl80211_setup_ap(new_bss))
    9659                 :          0 :                         return -1;
    9660                 :            :         }
    9661                 :            : 
    9662         [ +  - ]:         36 :         if (drv->global)
    9663                 :         36 :                 drv->global->if_add_ifindex = ifidx;
    9664                 :            : 
    9665                 :         36 :         return 0;
    9666                 :            : }
    9667                 :            : 
    9668                 :            : 
    9669                 :         36 : static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
    9670                 :            :                                         enum wpa_driver_if_type type,
    9671                 :            :                                         const char *ifname)
    9672                 :            : {
    9673                 :         36 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9674                 :         36 :         int ifindex = if_nametoindex(ifname);
    9675                 :            : 
    9676                 :         36 :         wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
    9677                 :         36 :                    __func__, type, ifname, ifindex, bss->added_if);
    9678 [ +  + ][ +  - ]:         36 :         if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
                 [ +  - ]
    9679                 :         36 :                 nl80211_remove_iface(drv, ifindex);
    9680                 :            : 
    9681         [ +  + ]:         36 :         if (type != WPA_IF_AP_BSS)
    9682                 :         19 :                 return 0;
    9683                 :            : 
    9684         [ -  + ]:         17 :         if (bss->added_if_into_bridge) {
    9685         [ #  # ]:          0 :                 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
    9686                 :          0 :                                     bss->ifname) < 0)
    9687                 :          0 :                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
    9688                 :            :                                    "interface %s from bridge %s: %s",
    9689                 :          0 :                                    bss->ifname, bss->brname, strerror(errno));
    9690                 :            :         }
    9691         [ -  + ]:         17 :         if (bss->added_bridge) {
    9692         [ #  # ]:          0 :                 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
    9693                 :          0 :                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
    9694                 :            :                                    "bridge %s: %s",
    9695                 :          0 :                                    bss->brname, strerror(errno));
    9696                 :            :         }
    9697                 :            : 
    9698         [ +  - ]:         17 :         if (bss != drv->first_bss) {
    9699                 :            :                 struct i802_bss *tbss;
    9700                 :            : 
    9701                 :         17 :                 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
    9702         [ +  - ]:         20 :                 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
    9703         [ +  + ]:         20 :                         if (tbss->next == bss) {
    9704                 :         17 :                                 tbss->next = bss->next;
    9705                 :            :                                 /* Unsubscribe management frames */
    9706                 :         17 :                                 nl80211_teardown_ap(bss);
    9707                 :         17 :                                 nl80211_destroy_bss(bss);
    9708                 :         17 :                                 os_free(bss);
    9709                 :         17 :                                 bss = NULL;
    9710                 :         17 :                                 break;
    9711                 :            :                         }
    9712                 :            :                 }
    9713         [ -  + ]:         17 :                 if (bss)
    9714                 :          0 :                         wpa_printf(MSG_INFO, "nl80211: %s - could not find "
    9715                 :            :                                    "BSS %p in the list", __func__, bss);
    9716                 :            :         } else {
    9717                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
    9718                 :          0 :                 nl80211_teardown_ap(bss);
    9719 [ #  # ][ #  # ]:          0 :                 if (!bss->added_if && !drv->first_bss->next)
    9720                 :          0 :                         wpa_driver_nl80211_del_beacon(drv);
    9721                 :          0 :                 nl80211_destroy_bss(bss);
    9722         [ #  # ]:          0 :                 if (!bss->added_if)
    9723                 :          0 :                         i802_set_iface_flags(bss, 0);
    9724         [ #  # ]:          0 :                 if (drv->first_bss->next) {
    9725                 :          0 :                         drv->first_bss = drv->first_bss->next;
    9726                 :          0 :                         drv->ctx = drv->first_bss->ctx;
    9727                 :          0 :                         os_free(bss);
    9728                 :            :                 } else {
    9729                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
    9730                 :            :                 }
    9731                 :            :         }
    9732                 :            : 
    9733                 :         36 :         return 0;
    9734                 :            : }
    9735                 :            : 
    9736                 :            : 
    9737                 :       2184 : static int cookie_handler(struct nl_msg *msg, void *arg)
    9738                 :            : {
    9739                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    9740                 :       2184 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    9741                 :       2184 :         u64 *cookie = arg;
    9742                 :       2184 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    9743                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    9744         [ +  - ]:       2184 :         if (tb[NL80211_ATTR_COOKIE])
    9745                 :       2184 :                 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
    9746                 :       2184 :         return NL_SKIP;
    9747                 :            : }
    9748                 :            : 
    9749                 :            : 
    9750                 :       2900 : static int nl80211_send_frame_cmd(struct i802_bss *bss,
    9751                 :            :                                   unsigned int freq, unsigned int wait,
    9752                 :            :                                   const u8 *buf, size_t buf_len,
    9753                 :            :                                   u64 *cookie_out, int no_cck, int no_ack,
    9754                 :            :                                   int offchanok)
    9755                 :            : {
    9756                 :       2900 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9757                 :            :         struct nl_msg *msg;
    9758                 :            :         u64 cookie;
    9759                 :       2900 :         int ret = -1;
    9760                 :            : 
    9761                 :       2900 :         msg = nlmsg_alloc();
    9762         [ -  + ]:       2900 :         if (!msg)
    9763                 :          0 :                 return -1;
    9764                 :            : 
    9765                 :       2900 :         wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
    9766                 :            :                    "no_ack=%d offchanok=%d",
    9767                 :            :                    freq, wait, no_cck, no_ack, offchanok);
    9768                 :       2900 :         wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
    9769                 :       2900 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
    9770                 :            : 
    9771         [ -  + ]:       2900 :         if (nl80211_set_iface_id(msg, bss) < 0)
    9772                 :          0 :                 goto nla_put_failure;
    9773         [ +  + ]:       2900 :         if (freq)
    9774         [ +  - ]:       2878 :                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
    9775         [ +  + ]:       2900 :         if (wait)
    9776         [ +  - ]:        255 :                 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
    9777 [ +  + ][ +  - ]:       2900 :         if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
    9778         [ +  - ]:        621 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
    9779         [ +  + ]:       2900 :         if (no_cck)
    9780         [ +  - ]:        456 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
    9781         [ +  + ]:       2900 :         if (no_ack)
    9782         [ +  - ]:        948 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
    9783                 :            : 
    9784         [ +  - ]:       2900 :         NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
    9785                 :            : 
    9786                 :       2900 :         cookie = 0;
    9787                 :       2900 :         ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
    9788                 :       2900 :         msg = NULL;
    9789         [ +  + ]:       2900 :         if (ret) {
    9790                 :        265 :                 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
    9791                 :            :                            "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
    9792                 :            :                            freq, wait);
    9793                 :        265 :                 goto nla_put_failure;
    9794                 :            :         }
    9795         [ +  + ]:       2635 :         wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
    9796                 :            :                    "cookie 0x%llx", no_ack ? " (no ACK)" : "",
    9797                 :            :                    (long long unsigned int) cookie);
    9798                 :            : 
    9799         [ +  + ]:       2635 :         if (cookie_out)
    9800         [ +  + ]:       2394 :                 *cookie_out = no_ack ? (u64) -1 : cookie;
    9801                 :            : 
    9802                 :            : nla_put_failure:
    9803                 :       2900 :         nlmsg_free(msg);
    9804                 :       2900 :         return ret;
    9805                 :            : }
    9806                 :            : 
    9807                 :            : 
    9808                 :        380 : static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
    9809                 :            :                                           unsigned int freq,
    9810                 :            :                                           unsigned int wait_time,
    9811                 :            :                                           const u8 *dst, const u8 *src,
    9812                 :            :                                           const u8 *bssid,
    9813                 :            :                                           const u8 *data, size_t data_len,
    9814                 :            :                                           int no_cck)
    9815                 :            : {
    9816                 :        380 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9817                 :        380 :         int ret = -1;
    9818                 :            :         u8 *buf;
    9819                 :            :         struct ieee80211_hdr *hdr;
    9820                 :            : 
    9821                 :        380 :         wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
    9822                 :            :                    "freq=%u MHz wait=%d ms no_cck=%d)",
    9823                 :            :                    drv->ifindex, freq, wait_time, no_cck);
    9824                 :            : 
    9825                 :        380 :         buf = os_zalloc(24 + data_len);
    9826         [ -  + ]:        380 :         if (buf == NULL)
    9827                 :          0 :                 return ret;
    9828                 :        380 :         os_memcpy(buf + 24, data, data_len);
    9829                 :        380 :         hdr = (struct ieee80211_hdr *) buf;
    9830                 :        380 :         hdr->frame_control =
    9831                 :            :                 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
    9832                 :        380 :         os_memcpy(hdr->addr1, dst, ETH_ALEN);
    9833                 :        380 :         os_memcpy(hdr->addr2, src, ETH_ALEN);
    9834                 :        380 :         os_memcpy(hdr->addr3, bssid, ETH_ALEN);
    9835                 :            : 
    9836 [ +  + ][ +  - ]:        380 :         if (is_ap_interface(drv->nlmode) &&
    9837         [ +  + ]:         95 :             (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
    9838 [ +  - ][ +  - ]:          2 :              (int) freq == bss->freq || drv->device_ap_sme ||
    9839                 :          2 :              !drv->use_monitor))
    9840                 :         95 :                 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
    9841                 :            :                                                    0, freq, no_cck, 1,
    9842                 :            :                                                    wait_time);
    9843                 :            :         else
    9844                 :        285 :                 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
    9845                 :            :                                              24 + data_len,
    9846                 :            :                                              &drv->send_action_cookie,
    9847                 :            :                                              no_cck, 0, 1);
    9848                 :            : 
    9849                 :        380 :         os_free(buf);
    9850                 :        380 :         return ret;
    9851                 :            : }
    9852                 :            : 
    9853                 :            : 
    9854                 :        134 : static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
    9855                 :            : {
    9856                 :        134 :         struct i802_bss *bss = priv;
    9857                 :        134 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9858                 :            :         struct nl_msg *msg;
    9859                 :            :         int ret;
    9860                 :            : 
    9861                 :        134 :         msg = nlmsg_alloc();
    9862         [ -  + ]:        134 :         if (!msg)
    9863                 :        134 :                 return;
    9864                 :            : 
    9865                 :        134 :         wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
    9866                 :        134 :                    (long long unsigned int) drv->send_action_cookie);
    9867                 :        134 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
    9868                 :            : 
    9869         [ -  + ]:        134 :         if (nl80211_set_iface_id(msg, bss) < 0)
    9870                 :          0 :                 goto nla_put_failure;
    9871         [ +  - ]:        134 :         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
    9872                 :            : 
    9873                 :        134 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    9874                 :        134 :         msg = NULL;
    9875         [ +  + ]:        134 :         if (ret)
    9876                 :          9 :                 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
    9877                 :            :                            "(%s)", ret, strerror(-ret));
    9878                 :            : 
    9879                 :            :  nla_put_failure:
    9880                 :        134 :         nlmsg_free(msg);
    9881                 :            : }
    9882                 :            : 
    9883                 :            : 
    9884                 :        494 : static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
    9885                 :            :                                                 unsigned int duration)
    9886                 :            : {
    9887                 :        494 :         struct i802_bss *bss = priv;
    9888                 :        494 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9889                 :            :         struct nl_msg *msg;
    9890                 :            :         int ret;
    9891                 :            :         u64 cookie;
    9892                 :            : 
    9893                 :        494 :         msg = nlmsg_alloc();
    9894         [ -  + ]:        494 :         if (!msg)
    9895                 :          0 :                 return -1;
    9896                 :            : 
    9897                 :        494 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
    9898                 :            : 
    9899         [ -  + ]:        494 :         if (nl80211_set_iface_id(msg, bss) < 0)
    9900                 :          0 :                 goto nla_put_failure;
    9901                 :            : 
    9902         [ +  - ]:        494 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
    9903         [ +  - ]:        494 :         NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
    9904                 :            : 
    9905                 :        494 :         cookie = 0;
    9906                 :        494 :         ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
    9907                 :        494 :         msg = NULL;
    9908         [ +  - ]:        494 :         if (ret == 0) {
    9909                 :        494 :                 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
    9910                 :            :                            "0x%llx for freq=%u MHz duration=%u",
    9911                 :            :                            (long long unsigned int) cookie, freq, duration);
    9912                 :        494 :                 drv->remain_on_chan_cookie = cookie;
    9913                 :        494 :                 drv->pending_remain_on_chan = 1;
    9914                 :        494 :                 return 0;
    9915                 :            :         }
    9916                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
    9917                 :            :                    "(freq=%d duration=%u): %d (%s)",
    9918                 :            :                    freq, duration, ret, strerror(-ret));
    9919                 :            : nla_put_failure:
    9920                 :          0 :         nlmsg_free(msg);
    9921                 :        494 :         return -1;
    9922                 :            : }
    9923                 :            : 
    9924                 :            : 
    9925                 :        154 : static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
    9926                 :            : {
    9927                 :        154 :         struct i802_bss *bss = priv;
    9928                 :        154 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9929                 :            :         struct nl_msg *msg;
    9930                 :            :         int ret;
    9931                 :            : 
    9932         [ +  + ]:        154 :         if (!drv->pending_remain_on_chan) {
    9933                 :          1 :                 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
    9934                 :            :                            "to cancel");
    9935                 :          1 :                 return -1;
    9936                 :            :         }
    9937                 :            : 
    9938                 :        153 :         wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
    9939                 :            :                    "0x%llx",
    9940                 :        153 :                    (long long unsigned int) drv->remain_on_chan_cookie);
    9941                 :            : 
    9942                 :        153 :         msg = nlmsg_alloc();
    9943         [ -  + ]:        153 :         if (!msg)
    9944                 :          0 :                 return -1;
    9945                 :            : 
    9946                 :        153 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
    9947                 :            : 
    9948         [ -  + ]:        153 :         if (nl80211_set_iface_id(msg, bss) < 0)
    9949                 :          0 :                 goto nla_put_failure;
    9950                 :            : 
    9951         [ +  - ]:        153 :         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
    9952                 :            : 
    9953                 :        153 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    9954                 :        153 :         msg = NULL;
    9955         [ +  + ]:        153 :         if (ret == 0)
    9956                 :        151 :                 return 0;
    9957                 :          2 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
    9958                 :            :                    "%d (%s)", ret, strerror(-ret));
    9959                 :            : nla_put_failure:
    9960                 :          2 :         nlmsg_free(msg);
    9961                 :        154 :         return -1;
    9962                 :            : }
    9963                 :            : 
    9964                 :            : 
    9965                 :       2729 : static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
    9966                 :            : {
    9967                 :       2729 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9968                 :            : 
    9969         [ +  + ]:       2729 :         if (!report) {
    9970         [ +  + ]:       2235 :                 if (bss->nl_preq && drv->device_ap_sme &&
           [ -  +  #  # ]
    9971                 :          0 :                     is_ap_interface(drv->nlmode)) {
    9972                 :            :                         /*
    9973                 :            :                          * Do not disable Probe Request reporting that was
    9974                 :            :                          * enabled in nl80211_setup_ap().
    9975                 :            :                          */
    9976                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
    9977                 :            :                                    "Probe Request reporting nl_preq=%p while "
    9978                 :            :                                    "in AP mode", bss->nl_preq);
    9979         [ +  + ]:       2235 :                 } else if (bss->nl_preq) {
    9980                 :        482 :                         wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
    9981                 :            :                                    "reporting nl_preq=%p", bss->nl_preq);
    9982                 :        482 :                         nl80211_destroy_eloop_handle(&bss->nl_preq);
    9983                 :            :                 }
    9984                 :       2235 :                 return 0;
    9985                 :            :         }
    9986                 :            : 
    9987         [ +  + ]:        494 :         if (bss->nl_preq) {
    9988                 :         12 :                 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
    9989                 :            :                            "already on! nl_preq=%p", bss->nl_preq);
    9990                 :         12 :                 return 0;
    9991                 :            :         }
    9992                 :            : 
    9993                 :        482 :         bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
    9994         [ -  + ]:        482 :         if (bss->nl_preq == NULL)
    9995                 :          0 :                 return -1;
    9996                 :        482 :         wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
    9997                 :            :                    "reporting nl_preq=%p", bss->nl_preq);
    9998                 :            : 
    9999         [ -  + ]:        482 :         if (nl80211_register_frame(bss, bss->nl_preq,
   10000                 :            :                                    (WLAN_FC_TYPE_MGMT << 2) |
   10001                 :            :                                    (WLAN_FC_STYPE_PROBE_REQ << 4),
   10002                 :            :                                    NULL, 0) < 0)
   10003                 :          0 :                 goto out_err;
   10004                 :            : 
   10005                 :        482 :         nl80211_register_eloop_read(&bss->nl_preq,
   10006                 :            :                                     wpa_driver_nl80211_event_receive,
   10007                 :        482 :                                     bss->nl_cb);
   10008                 :            : 
   10009                 :        482 :         return 0;
   10010                 :            : 
   10011                 :            :  out_err:
   10012                 :          0 :         nl_destroy_handles(&bss->nl_preq);
   10013                 :       2729 :         return -1;
   10014                 :            : }
   10015                 :            : 
   10016                 :            : 
   10017                 :        250 : static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
   10018                 :            :                                      int ifindex, int disabled)
   10019                 :            : {
   10020                 :            :         struct nl_msg *msg;
   10021                 :            :         struct nlattr *bands, *band;
   10022                 :            :         int ret;
   10023                 :            : 
   10024                 :        250 :         msg = nlmsg_alloc();
   10025         [ -  + ]:        250 :         if (!msg)
   10026                 :          0 :                 return -1;
   10027                 :            : 
   10028                 :        250 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
   10029         [ +  - ]:        250 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
   10030                 :            : 
   10031                 :        250 :         bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
   10032         [ -  + ]:        250 :         if (!bands)
   10033                 :          0 :                 goto nla_put_failure;
   10034                 :            : 
   10035                 :            :         /*
   10036                 :            :          * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
   10037                 :            :          * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
   10038                 :            :          * rates. All 5 GHz rates are left enabled.
   10039                 :            :          */
   10040                 :        250 :         band = nla_nest_start(msg, NL80211_BAND_2GHZ);
   10041         [ -  + ]:        250 :         if (!band)
   10042                 :          0 :                 goto nla_put_failure;
   10043         [ +  + ]:        250 :         if (disabled) {
   10044         [ +  - ]:        144 :                 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
   10045                 :            :                         "\x0c\x12\x18\x24\x30\x48\x60\x6c");
   10046                 :            :         }
   10047                 :        250 :         nla_nest_end(msg, band);
   10048                 :            : 
   10049                 :        250 :         nla_nest_end(msg, bands);
   10050                 :            : 
   10051                 :        250 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   10052                 :        250 :         msg = NULL;
   10053         [ +  + ]:        250 :         if (ret) {
   10054                 :         38 :                 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
   10055                 :            :                            "(%s)", ret, strerror(-ret));
   10056                 :            :         } else
   10057                 :        212 :                 drv->disabled_11b_rates = disabled;
   10058                 :            : 
   10059                 :        250 :         return ret;
   10060                 :            : 
   10061                 :            : nla_put_failure:
   10062                 :          0 :         nlmsg_free(msg);
   10063                 :        250 :         return -1;
   10064                 :            : }
   10065                 :            : 
   10066                 :            : 
   10067                 :         55 : static int wpa_driver_nl80211_deinit_ap(void *priv)
   10068                 :            : {
   10069                 :         55 :         struct i802_bss *bss = priv;
   10070                 :         55 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10071         [ -  + ]:         55 :         if (!is_ap_interface(drv->nlmode))
   10072                 :          0 :                 return -1;
   10073                 :         55 :         wpa_driver_nl80211_del_beacon(drv);
   10074                 :            : 
   10075                 :            :         /*
   10076                 :            :          * If the P2P GO interface was dynamically added, then it is
   10077                 :            :          * possible that the interface change to station is not possible.
   10078                 :            :          */
   10079 [ +  + ][ +  - ]:         55 :         if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
   10080                 :         10 :                 return 0;
   10081                 :            : 
   10082                 :         55 :         return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
   10083                 :            : }
   10084                 :            : 
   10085                 :            : 
   10086                 :          0 : static int wpa_driver_nl80211_stop_ap(void *priv)
   10087                 :            : {
   10088                 :          0 :         struct i802_bss *bss = priv;
   10089                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10090         [ #  # ]:          0 :         if (!is_ap_interface(drv->nlmode))
   10091                 :          0 :                 return -1;
   10092                 :          0 :         wpa_driver_nl80211_del_beacon(drv);
   10093                 :          0 :         bss->beacon_set = 0;
   10094                 :          0 :         return 0;
   10095                 :            : }
   10096                 :            : 
   10097                 :            : 
   10098                 :         50 : static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
   10099                 :            : {
   10100                 :         50 :         struct i802_bss *bss = priv;
   10101                 :         50 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10102         [ -  + ]:         50 :         if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
   10103                 :          0 :                 return -1;
   10104                 :            : 
   10105                 :            :         /*
   10106                 :            :          * If the P2P Client interface was dynamically added, then it is
   10107                 :            :          * possible that the interface change to station is not possible.
   10108                 :            :          */
   10109         [ -  + ]:         50 :         if (bss->if_dynamic)
   10110                 :          0 :                 return 0;
   10111                 :            : 
   10112                 :         50 :         return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
   10113                 :            : }
   10114                 :            : 
   10115                 :            : 
   10116                 :          0 : static void wpa_driver_nl80211_resume(void *priv)
   10117                 :            : {
   10118                 :          0 :         struct i802_bss *bss = priv;
   10119                 :            : 
   10120         [ #  # ]:          0 :         if (i802_set_iface_flags(bss, 1))
   10121                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
   10122                 :          0 : }
   10123                 :            : 
   10124                 :            : 
   10125                 :          6 : static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
   10126                 :            :                                   const u8 *ies, size_t ies_len)
   10127                 :            : {
   10128                 :          6 :         struct i802_bss *bss = priv;
   10129                 :          6 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10130                 :            :         int ret;
   10131                 :            :         u8 *data, *pos;
   10132                 :            :         size_t data_len;
   10133                 :          6 :         const u8 *own_addr = bss->addr;
   10134                 :            : 
   10135         [ -  + ]:          6 :         if (action != 1) {
   10136                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
   10137                 :            :                            "action %d", action);
   10138                 :          0 :                 return -1;
   10139                 :            :         }
   10140                 :            : 
   10141                 :            :         /*
   10142                 :            :          * Action frame payload:
   10143                 :            :          * Category[1] = 6 (Fast BSS Transition)
   10144                 :            :          * Action[1] = 1 (Fast BSS Transition Request)
   10145                 :            :          * STA Address
   10146                 :            :          * Target AP Address
   10147                 :            :          * FT IEs
   10148                 :            :          */
   10149                 :            : 
   10150                 :          6 :         data_len = 2 + 2 * ETH_ALEN + ies_len;
   10151                 :          6 :         data = os_malloc(data_len);
   10152         [ -  + ]:          6 :         if (data == NULL)
   10153                 :          0 :                 return -1;
   10154                 :          6 :         pos = data;
   10155                 :          6 :         *pos++ = 0x06; /* FT Action category */
   10156                 :          6 :         *pos++ = action;
   10157                 :          6 :         os_memcpy(pos, own_addr, ETH_ALEN);
   10158                 :          6 :         pos += ETH_ALEN;
   10159                 :          6 :         os_memcpy(pos, target_ap, ETH_ALEN);
   10160                 :          6 :         pos += ETH_ALEN;
   10161                 :          6 :         os_memcpy(pos, ies, ies_len);
   10162                 :            : 
   10163                 :          6 :         ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
   10164                 :          6 :                                              drv->bssid, own_addr, drv->bssid,
   10165                 :            :                                              data, data_len, 0);
   10166                 :          6 :         os_free(data);
   10167                 :            : 
   10168                 :          6 :         return ret;
   10169                 :            : }
   10170                 :            : 
   10171                 :            : 
   10172                 :          0 : static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
   10173                 :            : {
   10174                 :          0 :         struct i802_bss *bss = priv;
   10175                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10176                 :            :         struct nl_msg *msg;
   10177                 :            :         struct nlattr *cqm;
   10178                 :          0 :         int ret = -1;
   10179                 :            : 
   10180                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
   10181                 :            :                    "hysteresis=%d", threshold, hysteresis);
   10182                 :            : 
   10183                 :          0 :         msg = nlmsg_alloc();
   10184         [ #  # ]:          0 :         if (!msg)
   10185                 :          0 :                 return -1;
   10186                 :            : 
   10187                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
   10188                 :            : 
   10189         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
   10190                 :            : 
   10191                 :          0 :         cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
   10192         [ #  # ]:          0 :         if (cqm == NULL)
   10193                 :          0 :                 goto nla_put_failure;
   10194                 :            : 
   10195         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
   10196         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
   10197                 :          0 :         nla_nest_end(msg, cqm);
   10198                 :            : 
   10199                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   10200                 :          0 :         msg = NULL;
   10201                 :            : 
   10202                 :            : nla_put_failure:
   10203                 :          0 :         nlmsg_free(msg);
   10204                 :          0 :         return ret;
   10205                 :            : }
   10206                 :            : 
   10207                 :            : 
   10208                 :          0 : static int get_channel_width(struct nl_msg *msg, void *arg)
   10209                 :            : {
   10210                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
   10211                 :          0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
   10212                 :          0 :         struct wpa_signal_info *sig_change = arg;
   10213                 :            : 
   10214                 :          0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
   10215                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
   10216                 :            : 
   10217                 :          0 :         sig_change->center_frq1 = -1;
   10218                 :          0 :         sig_change->center_frq2 = -1;
   10219                 :          0 :         sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
   10220                 :            : 
   10221         [ #  # ]:          0 :         if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
   10222                 :          0 :                 sig_change->chanwidth = convert2width(
   10223                 :          0 :                         nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
   10224         [ #  # ]:          0 :                 if (tb[NL80211_ATTR_CENTER_FREQ1])
   10225                 :          0 :                         sig_change->center_frq1 =
   10226                 :          0 :                                 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
   10227         [ #  # ]:          0 :                 if (tb[NL80211_ATTR_CENTER_FREQ2])
   10228                 :          0 :                         sig_change->center_frq2 =
   10229                 :          0 :                                 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
   10230                 :            :         }
   10231                 :            : 
   10232                 :          0 :         return NL_SKIP;
   10233                 :            : }
   10234                 :            : 
   10235                 :            : 
   10236                 :          0 : static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
   10237                 :            :                                      struct wpa_signal_info *sig)
   10238                 :            : {
   10239                 :            :         struct nl_msg *msg;
   10240                 :            : 
   10241                 :          0 :         msg = nlmsg_alloc();
   10242         [ #  # ]:          0 :         if (!msg)
   10243                 :          0 :                 return -ENOMEM;
   10244                 :            : 
   10245                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
   10246         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   10247                 :            : 
   10248                 :          0 :         return send_and_recv_msgs(drv, msg, get_channel_width, sig);
   10249                 :            : 
   10250                 :            : nla_put_failure:
   10251                 :          0 :         nlmsg_free(msg);
   10252                 :          0 :         return -ENOBUFS;
   10253                 :            : }
   10254                 :            : 
   10255                 :            : 
   10256                 :          0 : static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
   10257                 :            : {
   10258                 :          0 :         struct i802_bss *bss = priv;
   10259                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10260                 :            :         int res;
   10261                 :            : 
   10262                 :          0 :         os_memset(si, 0, sizeof(*si));
   10263                 :          0 :         res = nl80211_get_link_signal(drv, si);
   10264         [ #  # ]:          0 :         if (res != 0)
   10265                 :          0 :                 return res;
   10266                 :            : 
   10267                 :          0 :         res = nl80211_get_channel_width(drv, si);
   10268         [ #  # ]:          0 :         if (res != 0)
   10269                 :          0 :                 return res;
   10270                 :            : 
   10271                 :          0 :         return nl80211_get_link_noise(drv, si);
   10272                 :            : }
   10273                 :            : 
   10274                 :            : 
   10275                 :          6 : static int wpa_driver_nl80211_shared_freq(void *priv)
   10276                 :            : {
   10277                 :          6 :         struct i802_bss *bss = priv;
   10278                 :          6 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10279                 :            :         struct wpa_driver_nl80211_data *driver;
   10280                 :          6 :         int freq = 0;
   10281                 :            : 
   10282                 :            :         /*
   10283                 :            :          * If the same PHY is in connected state with some other interface,
   10284                 :            :          * then retrieve the assoc freq.
   10285                 :            :          */
   10286                 :          6 :         wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
   10287                 :          6 :                    drv->phyname);
   10288                 :            : 
   10289         [ +  + ]:         12 :         dl_list_for_each(driver, &drv->global->interfaces,
   10290                 :            :                          struct wpa_driver_nl80211_data, list) {
   10291 [ -  + ][ #  # ]:          6 :                 if (drv == driver ||
   10292         [ #  # ]:          0 :                     os_strcmp(drv->phyname, driver->phyname) != 0 ||
   10293                 :          0 :                     !driver->associated)
   10294                 :          6 :                         continue;
   10295                 :            : 
   10296                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
   10297                 :            :                            MACSTR,
   10298                 :          0 :                            driver->phyname, driver->first_bss->ifname,
   10299                 :          0 :                            MAC2STR(driver->first_bss->addr));
   10300         [ #  # ]:          0 :                 if (is_ap_interface(driver->nlmode))
   10301                 :          0 :                         freq = driver->first_bss->freq;
   10302                 :            :                 else
   10303                 :          0 :                         freq = nl80211_get_assoc_freq(driver);
   10304                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
   10305                 :          0 :                            drv->phyname, freq);
   10306                 :            :         }
   10307                 :            : 
   10308         [ +  - ]:          6 :         if (!freq)
   10309                 :          6 :                 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
   10310                 :          6 :                            "PHY (%s) in associated state", drv->phyname);
   10311                 :            : 
   10312                 :          6 :         return freq;
   10313                 :            : }
   10314                 :            : 
   10315                 :            : 
   10316                 :         12 : static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
   10317                 :            :                               int encrypt)
   10318                 :            : {
   10319                 :         12 :         struct i802_bss *bss = priv;
   10320                 :         12 :         return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
   10321                 :            :                                              0, 0, 0, 0);
   10322                 :            : }
   10323                 :            : 
   10324                 :            : 
   10325                 :         22 : static int nl80211_set_param(void *priv, const char *param)
   10326                 :            : {
   10327                 :         22 :         wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
   10328         [ +  - ]:         22 :         if (param == NULL)
   10329                 :         22 :                 return 0;
   10330                 :            : 
   10331                 :            : #ifdef CONFIG_P2P
   10332         [ #  # ]:          0 :         if (os_strstr(param, "use_p2p_group_interface=1")) {
   10333                 :          0 :                 struct i802_bss *bss = priv;
   10334                 :          0 :                 struct wpa_driver_nl80211_data *drv = bss->drv;
   10335                 :            : 
   10336                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
   10337                 :            :                            "interface");
   10338                 :          0 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
   10339                 :          0 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
   10340                 :            :         }
   10341                 :            : 
   10342         [ #  # ]:          0 :         if (os_strstr(param, "p2p_device=1")) {
   10343                 :          0 :                 struct i802_bss *bss = priv;
   10344                 :          0 :                 struct wpa_driver_nl80211_data *drv = bss->drv;
   10345                 :          0 :                 drv->allow_p2p_device = 1;
   10346                 :            :         }
   10347                 :            : #endif /* CONFIG_P2P */
   10348                 :            : 
   10349                 :         22 :         return 0;
   10350                 :            : }
   10351                 :            : 
   10352                 :            : 
   10353                 :          4 : static void * nl80211_global_init(void)
   10354                 :            : {
   10355                 :            :         struct nl80211_global *global;
   10356                 :            :         struct netlink_config *cfg;
   10357                 :            : 
   10358                 :          4 :         global = os_zalloc(sizeof(*global));
   10359         [ -  + ]:          4 :         if (global == NULL)
   10360                 :          0 :                 return NULL;
   10361                 :          4 :         global->ioctl_sock = -1;
   10362                 :          4 :         dl_list_init(&global->interfaces);
   10363                 :          4 :         global->if_add_ifindex = -1;
   10364                 :            : 
   10365                 :          4 :         cfg = os_zalloc(sizeof(*cfg));
   10366         [ -  + ]:          4 :         if (cfg == NULL)
   10367                 :          0 :                 goto err;
   10368                 :            : 
   10369                 :          4 :         cfg->ctx = global;
   10370                 :          4 :         cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
   10371                 :          4 :         cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
   10372                 :          4 :         global->netlink = netlink_init(cfg);
   10373         [ -  + ]:          4 :         if (global->netlink == NULL) {
   10374                 :          0 :                 os_free(cfg);
   10375                 :          0 :                 goto err;
   10376                 :            :         }
   10377                 :            : 
   10378         [ -  + ]:          4 :         if (wpa_driver_nl80211_init_nl_global(global) < 0)
   10379                 :          0 :                 goto err;
   10380                 :            : 
   10381                 :          4 :         global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
   10382         [ -  + ]:          4 :         if (global->ioctl_sock < 0) {
   10383                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
   10384                 :          0 :                            strerror(errno));
   10385                 :          0 :                 goto err;
   10386                 :            :         }
   10387                 :            : 
   10388                 :          4 :         return global;
   10389                 :            : 
   10390                 :            : err:
   10391                 :          0 :         nl80211_global_deinit(global);
   10392                 :          4 :         return NULL;
   10393                 :            : }
   10394                 :            : 
   10395                 :            : 
   10396                 :          4 : static void nl80211_global_deinit(void *priv)
   10397                 :            : {
   10398                 :          4 :         struct nl80211_global *global = priv;
   10399         [ -  + ]:          4 :         if (global == NULL)
   10400                 :          4 :                 return;
   10401         [ -  + ]:          4 :         if (!dl_list_empty(&global->interfaces)) {
   10402                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
   10403                 :            :                            "nl80211_global_deinit",
   10404                 :            :                            dl_list_len(&global->interfaces));
   10405                 :            :         }
   10406                 :            : 
   10407         [ +  - ]:          4 :         if (global->netlink)
   10408                 :          4 :                 netlink_deinit(global->netlink);
   10409                 :            : 
   10410                 :          4 :         nl_destroy_handles(&global->nl);
   10411                 :            : 
   10412         [ +  - ]:          4 :         if (global->nl_event)
   10413                 :          4 :                 nl80211_destroy_eloop_handle(&global->nl_event);
   10414                 :            : 
   10415                 :          4 :         nl_cb_put(global->nl_cb);
   10416                 :            : 
   10417         [ +  - ]:          4 :         if (global->ioctl_sock >= 0)
   10418                 :          4 :                 close(global->ioctl_sock);
   10419                 :            : 
   10420                 :          4 :         os_free(global);
   10421                 :            : }
   10422                 :            : 
   10423                 :            : 
   10424                 :        260 : static const char * nl80211_get_radio_name(void *priv)
   10425                 :            : {
   10426                 :        260 :         struct i802_bss *bss = priv;
   10427                 :        260 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10428                 :        260 :         return drv->phyname;
   10429                 :            : }
   10430                 :            : 
   10431                 :            : 
   10432                 :        184 : static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
   10433                 :            :                          const u8 *pmkid)
   10434                 :            : {
   10435                 :            :         struct nl_msg *msg;
   10436                 :            : 
   10437                 :        184 :         msg = nlmsg_alloc();
   10438         [ -  + ]:        184 :         if (!msg)
   10439                 :          0 :                 return -ENOMEM;
   10440                 :            : 
   10441                 :        184 :         nl80211_cmd(bss->drv, msg, 0, cmd);
   10442                 :            : 
   10443         [ +  - ]:        184 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
   10444         [ +  + ]:        184 :         if (pmkid)
   10445         [ +  - ]:        162 :                 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
   10446         [ +  + ]:        184 :         if (bssid)
   10447         [ +  - ]:        162 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
   10448                 :            : 
   10449                 :        184 :         return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
   10450                 :            :  nla_put_failure:
   10451                 :          0 :         nlmsg_free(msg);
   10452                 :        184 :         return -ENOBUFS;
   10453                 :            : }
   10454                 :            : 
   10455                 :            : 
   10456                 :         81 : static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
   10457                 :            : {
   10458                 :         81 :         struct i802_bss *bss = priv;
   10459                 :         81 :         wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
   10460                 :         81 :         return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
   10461                 :            : }
   10462                 :            : 
   10463                 :            : 
   10464                 :         81 : static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
   10465                 :            : {
   10466                 :         81 :         struct i802_bss *bss = priv;
   10467                 :         81 :         wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
   10468                 :        486 :                    MAC2STR(bssid));
   10469                 :         81 :         return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
   10470                 :            : }
   10471                 :            : 
   10472                 :            : 
   10473                 :         22 : static int nl80211_flush_pmkid(void *priv)
   10474                 :            : {
   10475                 :         22 :         struct i802_bss *bss = priv;
   10476                 :         22 :         wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
   10477                 :         22 :         return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
   10478                 :            : }
   10479                 :            : 
   10480                 :            : 
   10481                 :         15 : static void clean_survey_results(struct survey_results *survey_results)
   10482                 :            : {
   10483                 :            :         struct freq_survey *survey, *tmp;
   10484                 :            : 
   10485         [ +  - ]:         15 :         if (dl_list_empty(&survey_results->survey_list))
   10486                 :         15 :                 return;
   10487                 :            : 
   10488         [ #  # ]:          0 :         dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
   10489                 :            :                               struct freq_survey, list) {
   10490                 :          0 :                 dl_list_del(&survey->list);
   10491                 :          0 :                 os_free(survey);
   10492                 :            :         }
   10493                 :            : }
   10494                 :            : 
   10495                 :            : 
   10496                 :         15 : static void add_survey(struct nlattr **sinfo, u32 ifidx,
   10497                 :            :                        struct dl_list *survey_list)
   10498                 :            : {
   10499                 :            :         struct freq_survey *survey;
   10500                 :            : 
   10501                 :         15 :         survey = os_zalloc(sizeof(struct freq_survey));
   10502         [ -  + ]:         15 :         if  (!survey)
   10503                 :         15 :                 return;
   10504                 :            : 
   10505                 :         15 :         survey->ifidx = ifidx;
   10506                 :         15 :         survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
   10507                 :         15 :         survey->filled = 0;
   10508                 :            : 
   10509         [ +  - ]:         15 :         if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
   10510                 :         15 :                 survey->nf = (int8_t)
   10511                 :         15 :                         nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
   10512                 :         15 :                 survey->filled |= SURVEY_HAS_NF;
   10513                 :            :         }
   10514                 :            : 
   10515         [ -  + ]:         15 :         if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
   10516                 :          0 :                 survey->channel_time =
   10517                 :          0 :                         nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
   10518                 :          0 :                 survey->filled |= SURVEY_HAS_CHAN_TIME;
   10519                 :            :         }
   10520                 :            : 
   10521         [ -  + ]:         15 :         if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
   10522                 :          0 :                 survey->channel_time_busy =
   10523                 :          0 :                         nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
   10524                 :          0 :                 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
   10525                 :            :         }
   10526                 :            : 
   10527         [ -  + ]:         15 :         if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
   10528                 :          0 :                 survey->channel_time_rx =
   10529                 :          0 :                         nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
   10530                 :          0 :                 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
   10531                 :            :         }
   10532                 :            : 
   10533         [ -  + ]:         15 :         if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
   10534                 :          0 :                 survey->channel_time_tx =
   10535                 :          0 :                         nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
   10536                 :          0 :                 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
   10537                 :            :         }
   10538                 :            : 
   10539                 :         15 :         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)",
   10540                 :            :                    survey->freq,
   10541                 :         15 :                    survey->nf,
   10542                 :            :                    (unsigned long int) survey->channel_time,
   10543                 :            :                    (unsigned long int) survey->channel_time_busy,
   10544                 :            :                    (unsigned long int) survey->channel_time_tx,
   10545                 :            :                    (unsigned long int) survey->channel_time_rx,
   10546                 :            :                    survey->filled);
   10547                 :            : 
   10548                 :         15 :         dl_list_add_tail(survey_list, &survey->list);
   10549                 :            : }
   10550                 :            : 
   10551                 :            : 
   10552                 :         15 : static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
   10553                 :            :                            unsigned int freq_filter)
   10554                 :            : {
   10555         [ +  - ]:         15 :         if (!freq_filter)
   10556                 :         15 :                 return 1;
   10557                 :            : 
   10558                 :         15 :         return freq_filter == surveyed_freq;
   10559                 :            : }
   10560                 :            : 
   10561                 :            : 
   10562                 :         15 : static int survey_handler(struct nl_msg *msg, void *arg)
   10563                 :            : {
   10564                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
   10565                 :         15 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
   10566                 :            :         struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
   10567                 :            :         struct survey_results *survey_results;
   10568                 :         15 :         u32 surveyed_freq = 0;
   10569                 :            :         u32 ifidx;
   10570                 :            : 
   10571                 :            :         static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
   10572                 :            :                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
   10573                 :            :                 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
   10574                 :            :         };
   10575                 :            : 
   10576                 :         15 :         survey_results = (struct survey_results *) arg;
   10577                 :            : 
   10578                 :         15 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
   10579                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
   10580                 :            : 
   10581                 :         15 :         ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
   10582                 :            : 
   10583         [ -  + ]:         15 :         if (!tb[NL80211_ATTR_SURVEY_INFO])
   10584                 :          0 :                 return NL_SKIP;
   10585                 :            : 
   10586         [ -  + ]:         15 :         if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
   10587                 :            :                              tb[NL80211_ATTR_SURVEY_INFO],
   10588                 :            :                              survey_policy))
   10589                 :          0 :                 return NL_SKIP;
   10590                 :            : 
   10591         [ -  + ]:         15 :         if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
   10592                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
   10593                 :          0 :                 return NL_SKIP;
   10594                 :            :         }
   10595                 :            : 
   10596                 :         15 :         surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
   10597                 :            : 
   10598         [ -  + ]:         15 :         if (!check_survey_ok(sinfo, surveyed_freq,
   10599                 :            :                              survey_results->freq_filter))
   10600                 :          0 :                 return NL_SKIP;
   10601                 :            : 
   10602 [ -  + ][ #  # ]:         15 :         if (survey_results->freq_filter &&
   10603                 :          0 :             survey_results->freq_filter != surveyed_freq) {
   10604                 :          0 :                 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
   10605                 :            :                            surveyed_freq);
   10606                 :          0 :                 return NL_SKIP;
   10607                 :            :         }
   10608                 :            : 
   10609                 :         15 :         add_survey(sinfo, ifidx, &survey_results->survey_list);
   10610                 :            : 
   10611                 :         15 :         return NL_SKIP;
   10612                 :            : }
   10613                 :            : 
   10614                 :            : 
   10615                 :         15 : static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
   10616                 :            : {
   10617                 :         15 :         struct i802_bss *bss = priv;
   10618                 :         15 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10619                 :            :         struct nl_msg *msg;
   10620                 :         15 :         int err = -ENOBUFS;
   10621                 :            :         union wpa_event_data data;
   10622                 :            :         struct survey_results *survey_results;
   10623                 :            : 
   10624                 :         15 :         os_memset(&data, 0, sizeof(data));
   10625                 :         15 :         survey_results = &data.survey_results;
   10626                 :            : 
   10627                 :         15 :         dl_list_init(&survey_results->survey_list);
   10628                 :            : 
   10629                 :         15 :         msg = nlmsg_alloc();
   10630         [ -  + ]:         15 :         if (!msg)
   10631                 :          0 :                 goto nla_put_failure;
   10632                 :            : 
   10633                 :         15 :         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
   10634                 :            : 
   10635         [ +  - ]:         15 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   10636                 :            : 
   10637         [ -  + ]:         15 :         if (freq)
   10638                 :          0 :                 data.survey_results.freq_filter = freq;
   10639                 :            : 
   10640                 :            :         do {
   10641                 :         15 :                 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
   10642                 :         15 :                 err = send_and_recv_msgs(drv, msg, survey_handler,
   10643                 :            :                                          survey_results);
   10644         [ -  + ]:         15 :         } while (err > 0);
   10645                 :            : 
   10646         [ -  + ]:         15 :         if (err) {
   10647                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
   10648                 :          0 :                 goto out_clean;
   10649                 :            :         }
   10650                 :            : 
   10651                 :         15 :         wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
   10652                 :            : 
   10653                 :            : out_clean:
   10654                 :         15 :         clean_survey_results(survey_results);
   10655                 :            : nla_put_failure:
   10656                 :         15 :         return err;
   10657                 :            : }
   10658                 :            : 
   10659                 :            : 
   10660                 :        248 : static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
   10661                 :            :                                    const u8 *replay_ctr)
   10662                 :            : {
   10663                 :        248 :         struct i802_bss *bss = priv;
   10664                 :        248 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10665                 :            :         struct nlattr *replay_nested;
   10666                 :            :         struct nl_msg *msg;
   10667                 :            : 
   10668                 :        248 :         msg = nlmsg_alloc();
   10669         [ -  + ]:        248 :         if (!msg)
   10670                 :          0 :                 return;
   10671                 :            : 
   10672                 :        248 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
   10673                 :            : 
   10674         [ +  - ]:        248 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
   10675                 :            : 
   10676                 :        248 :         replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
   10677         [ -  + ]:        248 :         if (!replay_nested)
   10678                 :          0 :                 goto nla_put_failure;
   10679                 :            : 
   10680         [ +  - ]:        248 :         NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
   10681         [ +  - ]:        248 :         NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
   10682         [ +  - ]:        248 :         NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
   10683                 :            :                 replay_ctr);
   10684                 :            : 
   10685                 :        248 :         nla_nest_end(msg, replay_nested);
   10686                 :            : 
   10687                 :        248 :         send_and_recv_msgs(drv, msg, NULL, NULL);
   10688                 :        248 :         return;
   10689                 :            :  nla_put_failure:
   10690                 :        248 :         nlmsg_free(msg);
   10691                 :            : }
   10692                 :            : 
   10693                 :            : 
   10694                 :          0 : static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
   10695                 :            :                                     const u8 *addr, int qos)
   10696                 :            : {
   10697                 :            :         /* send data frame to poll STA and check whether
   10698                 :            :          * this frame is ACKed */
   10699                 :            :         struct {
   10700                 :            :                 struct ieee80211_hdr hdr;
   10701                 :            :                 u16 qos_ctl;
   10702                 :            :         } STRUCT_PACKED nulldata;
   10703                 :            :         size_t size;
   10704                 :            : 
   10705                 :            :         /* Send data frame to poll STA and check whether this frame is ACKed */
   10706                 :            : 
   10707                 :          0 :         os_memset(&nulldata, 0, sizeof(nulldata));
   10708                 :            : 
   10709         [ #  # ]:          0 :         if (qos) {
   10710                 :          0 :                 nulldata.hdr.frame_control =
   10711                 :            :                         IEEE80211_FC(WLAN_FC_TYPE_DATA,
   10712                 :            :                                      WLAN_FC_STYPE_QOS_NULL);
   10713                 :          0 :                 size = sizeof(nulldata);
   10714                 :            :         } else {
   10715                 :          0 :                 nulldata.hdr.frame_control =
   10716                 :            :                         IEEE80211_FC(WLAN_FC_TYPE_DATA,
   10717                 :            :                                      WLAN_FC_STYPE_NULLFUNC);
   10718                 :          0 :                 size = sizeof(struct ieee80211_hdr);
   10719                 :            :         }
   10720                 :            : 
   10721                 :          0 :         nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
   10722                 :          0 :         os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
   10723                 :          0 :         os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
   10724                 :          0 :         os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
   10725                 :            : 
   10726         [ #  # ]:          0 :         if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
   10727                 :            :                                          0, 0) < 0)
   10728                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
   10729                 :            :                            "send poll frame");
   10730                 :          0 : }
   10731                 :            : 
   10732                 :          0 : static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
   10733                 :            :                                 int qos)
   10734                 :            : {
   10735                 :          0 :         struct i802_bss *bss = priv;
   10736                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10737                 :            :         struct nl_msg *msg;
   10738                 :            : 
   10739         [ #  # ]:          0 :         if (!drv->poll_command_supported) {
   10740                 :          0 :                 nl80211_send_null_frame(bss, own_addr, addr, qos);
   10741                 :          0 :                 return;
   10742                 :            :         }
   10743                 :            : 
   10744                 :          0 :         msg = nlmsg_alloc();
   10745         [ #  # ]:          0 :         if (!msg)
   10746                 :          0 :                 return;
   10747                 :            : 
   10748                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
   10749                 :            : 
   10750         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
   10751         [ #  # ]:          0 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
   10752                 :            : 
   10753                 :          0 :         send_and_recv_msgs(drv, msg, NULL, NULL);
   10754                 :          0 :         return;
   10755                 :            :  nla_put_failure:
   10756                 :          0 :         nlmsg_free(msg);
   10757                 :            : }
   10758                 :            : 
   10759                 :            : 
   10760                 :          0 : static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
   10761                 :            : {
   10762                 :            :         struct nl_msg *msg;
   10763                 :            : 
   10764                 :          0 :         msg = nlmsg_alloc();
   10765         [ #  # ]:          0 :         if (!msg)
   10766                 :          0 :                 return -ENOMEM;
   10767                 :            : 
   10768                 :          0 :         nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
   10769         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
   10770         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
   10771                 :            :                     enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
   10772                 :          0 :         return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
   10773                 :            : nla_put_failure:
   10774                 :          0 :         nlmsg_free(msg);
   10775                 :          0 :         return -ENOBUFS;
   10776                 :            : }
   10777                 :            : 
   10778                 :            : 
   10779                 :          0 : static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
   10780                 :            :                                      int ctwindow)
   10781                 :            : {
   10782                 :          0 :         struct i802_bss *bss = priv;
   10783                 :            : 
   10784                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
   10785                 :            :                    "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
   10786                 :            : 
   10787 [ #  # ][ #  # ]:          0 :         if (opp_ps != -1 || ctwindow != -1) {
   10788                 :            : #ifdef ANDROID_P2P
   10789                 :            :                 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
   10790                 :            : #else /* ANDROID_P2P */
   10791                 :          0 :                 return -1; /* Not yet supported */
   10792                 :            : #endif /* ANDROID_P2P */
   10793                 :            :         }
   10794                 :            : 
   10795         [ #  # ]:          0 :         if (legacy_ps == -1)
   10796                 :          0 :                 return 0;
   10797 [ #  # ][ #  # ]:          0 :         if (legacy_ps != 0 && legacy_ps != 1)
   10798                 :          0 :                 return -1; /* Not yet supported */
   10799                 :            : 
   10800                 :          0 :         return nl80211_set_power_save(bss, legacy_ps);
   10801                 :            : }
   10802                 :            : 
   10803                 :            : 
   10804                 :          0 : static int nl80211_start_radar_detection(void *priv,
   10805                 :            :                                          struct hostapd_freq_params *freq)
   10806                 :            : {
   10807                 :          0 :         struct i802_bss *bss = priv;
   10808                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10809                 :            :         struct nl_msg *msg;
   10810                 :            :         int ret;
   10811                 :            : 
   10812                 :          0 :         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)",
   10813                 :            :                    freq->freq, freq->ht_enabled, freq->vht_enabled,
   10814                 :            :                    freq->bandwidth, freq->center_freq1, freq->center_freq2);
   10815                 :            : 
   10816         [ #  # ]:          0 :         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
   10817                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
   10818                 :            :                            "detection");
   10819                 :          0 :                 return -1;
   10820                 :            :         }
   10821                 :            : 
   10822                 :          0 :         msg = nlmsg_alloc();
   10823         [ #  # ]:          0 :         if (!msg)
   10824                 :          0 :                 return -1;
   10825                 :            : 
   10826                 :          0 :         nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
   10827         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   10828         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
   10829                 :            : 
   10830         [ #  # ]:          0 :         if (freq->vht_enabled) {
   10831   [ #  #  #  #  :          0 :                 switch (freq->bandwidth) {
                      # ]
   10832                 :            :                 case 20:
   10833         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
   10834                 :            :                                     NL80211_CHAN_WIDTH_20);
   10835                 :          0 :                         break;
   10836                 :            :                 case 40:
   10837         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
   10838                 :            :                                     NL80211_CHAN_WIDTH_40);
   10839                 :          0 :                         break;
   10840                 :            :                 case 80:
   10841         [ #  # ]:          0 :                         if (freq->center_freq2)
   10842         [ #  # ]:          0 :                                 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
   10843                 :            :                                             NL80211_CHAN_WIDTH_80P80);
   10844                 :            :                         else
   10845         [ #  # ]:          0 :                                 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
   10846                 :            :                                             NL80211_CHAN_WIDTH_80);
   10847                 :          0 :                         break;
   10848                 :            :                 case 160:
   10849         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
   10850                 :            :                                     NL80211_CHAN_WIDTH_160);
   10851                 :          0 :                         break;
   10852                 :            :                 default:
   10853                 :          0 :                         return -1;
   10854                 :            :                 }
   10855         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
   10856         [ #  # ]:          0 :                 if (freq->center_freq2)
   10857         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
   10858                 :            :                                     freq->center_freq2);
   10859         [ #  # ]:          0 :         } else if (freq->ht_enabled) {
   10860      [ #  #  # ]:          0 :                 switch (freq->sec_channel_offset) {
   10861                 :            :                 case -1:
   10862         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
   10863                 :            :                                     NL80211_CHAN_HT40MINUS);
   10864                 :          0 :                         break;
   10865                 :            :                 case 1:
   10866         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
   10867                 :            :                                     NL80211_CHAN_HT40PLUS);
   10868                 :          0 :                         break;
   10869                 :            :                 default:
   10870         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
   10871                 :            :                                     NL80211_CHAN_HT20);
   10872                 :          0 :                         break;
   10873                 :            :                 }
   10874                 :            :         }
   10875                 :            : 
   10876                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   10877         [ #  # ]:          0 :         if (ret == 0)
   10878                 :          0 :                 return 0;
   10879                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
   10880                 :            :                    "%d (%s)", ret, strerror(-ret));
   10881                 :            : nla_put_failure:
   10882                 :          0 :         return -1;
   10883                 :            : }
   10884                 :            : 
   10885                 :            : #ifdef CONFIG_TDLS
   10886                 :            : 
   10887                 :        105 : static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
   10888                 :            :                                   u8 dialog_token, u16 status_code,
   10889                 :            :                                   const u8 *buf, size_t len)
   10890                 :            : {
   10891                 :        105 :         struct i802_bss *bss = priv;
   10892                 :        105 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10893                 :            :         struct nl_msg *msg;
   10894                 :            : 
   10895         [ -  + ]:        105 :         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
   10896                 :          0 :                 return -EOPNOTSUPP;
   10897                 :            : 
   10898         [ -  + ]:        105 :         if (!dst)
   10899                 :          0 :                 return -EINVAL;
   10900                 :            : 
   10901                 :        105 :         msg = nlmsg_alloc();
   10902         [ -  + ]:        105 :         if (!msg)
   10903                 :          0 :                 return -ENOMEM;
   10904                 :            : 
   10905                 :        105 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
   10906         [ +  - ]:        105 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   10907         [ +  - ]:        105 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
   10908         [ +  - ]:        105 :         NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
   10909         [ +  - ]:        105 :         NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
   10910         [ +  - ]:        105 :         NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
   10911         [ +  - ]:        105 :         NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
   10912                 :            : 
   10913                 :        105 :         return send_and_recv_msgs(drv, msg, NULL, NULL);
   10914                 :            : 
   10915                 :            : nla_put_failure:
   10916                 :          0 :         nlmsg_free(msg);
   10917                 :        105 :         return -ENOBUFS;
   10918                 :            : }
   10919                 :            : 
   10920                 :            : 
   10921                 :        753 : static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
   10922                 :            : {
   10923                 :        753 :         struct i802_bss *bss = priv;
   10924                 :        753 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10925                 :            :         struct nl_msg *msg;
   10926                 :            :         enum nl80211_tdls_operation nl80211_oper;
   10927                 :            : 
   10928         [ -  + ]:        753 :         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
   10929                 :          0 :                 return -EOPNOTSUPP;
   10930                 :            : 
   10931   [ -  -  -  +  :        753 :         switch (oper) {
             +  +  -  - ]
   10932                 :            :         case TDLS_DISCOVERY_REQ:
   10933                 :          0 :                 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
   10934                 :          0 :                 break;
   10935                 :            :         case TDLS_SETUP:
   10936                 :          0 :                 nl80211_oper = NL80211_TDLS_SETUP;
   10937                 :          0 :                 break;
   10938                 :            :         case TDLS_TEARDOWN:
   10939                 :          0 :                 nl80211_oper = NL80211_TDLS_TEARDOWN;
   10940                 :          0 :                 break;
   10941                 :            :         case TDLS_ENABLE_LINK:
   10942                 :         38 :                 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
   10943                 :         38 :                 break;
   10944                 :            :         case TDLS_DISABLE_LINK:
   10945                 :         60 :                 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
   10946                 :         60 :                 break;
   10947                 :            :         case TDLS_ENABLE:
   10948                 :        655 :                 return 0;
   10949                 :            :         case TDLS_DISABLE:
   10950                 :          0 :                 return 0;
   10951                 :            :         default:
   10952                 :          0 :                 return -EINVAL;
   10953                 :            :         }
   10954                 :            : 
   10955                 :         98 :         msg = nlmsg_alloc();
   10956         [ -  + ]:         98 :         if (!msg)
   10957                 :          0 :                 return -ENOMEM;
   10958                 :            : 
   10959                 :         98 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
   10960         [ +  - ]:         98 :         NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
   10961         [ +  - ]:         98 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   10962         [ +  - ]:         98 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
   10963                 :            : 
   10964                 :         98 :         return send_and_recv_msgs(drv, msg, NULL, NULL);
   10965                 :            : 
   10966                 :            : nla_put_failure:
   10967                 :          0 :         nlmsg_free(msg);
   10968                 :        753 :         return -ENOBUFS;
   10969                 :            : }
   10970                 :            : 
   10971                 :            : #endif /* CONFIG TDLS */
   10972                 :            : 
   10973                 :            : 
   10974                 :            : #ifdef ANDROID
   10975                 :            : 
   10976                 :            : typedef struct android_wifi_priv_cmd {
   10977                 :            :         char *buf;
   10978                 :            :         int used_len;
   10979                 :            :         int total_len;
   10980                 :            : } android_wifi_priv_cmd;
   10981                 :            : 
   10982                 :            : static int drv_errors = 0;
   10983                 :            : 
   10984                 :            : static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
   10985                 :            : {
   10986                 :            :         drv_errors++;
   10987                 :            :         if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
   10988                 :            :                 drv_errors = 0;
   10989                 :            :                 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
   10990                 :            :         }
   10991                 :            : }
   10992                 :            : 
   10993                 :            : 
   10994                 :            : static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
   10995                 :            : {
   10996                 :            :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10997                 :            :         struct ifreq ifr;
   10998                 :            :         android_wifi_priv_cmd priv_cmd;
   10999                 :            :         char buf[MAX_DRV_CMD_SIZE];
   11000                 :            :         int ret;
   11001                 :            : 
   11002                 :            :         os_memset(&ifr, 0, sizeof(ifr));
   11003                 :            :         os_memset(&priv_cmd, 0, sizeof(priv_cmd));
   11004                 :            :         os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
   11005                 :            : 
   11006                 :            :         os_memset(buf, 0, sizeof(buf));
   11007                 :            :         os_strlcpy(buf, cmd, sizeof(buf));
   11008                 :            : 
   11009                 :            :         priv_cmd.buf = buf;
   11010                 :            :         priv_cmd.used_len = sizeof(buf);
   11011                 :            :         priv_cmd.total_len = sizeof(buf);
   11012                 :            :         ifr.ifr_data = &priv_cmd;
   11013                 :            : 
   11014                 :            :         ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
   11015                 :            :         if (ret < 0) {
   11016                 :            :                 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
   11017                 :            :                            __func__);
   11018                 :            :                 wpa_driver_send_hang_msg(drv);
   11019                 :            :                 return ret;
   11020                 :            :         }
   11021                 :            : 
   11022                 :            :         drv_errors = 0;
   11023                 :            :         return 0;
   11024                 :            : }
   11025                 :            : 
   11026                 :            : 
   11027                 :            : static int android_pno_start(struct i802_bss *bss,
   11028                 :            :                              struct wpa_driver_scan_params *params)
   11029                 :            : {
   11030                 :            :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11031                 :            :         struct ifreq ifr;
   11032                 :            :         android_wifi_priv_cmd priv_cmd;
   11033                 :            :         int ret = 0, i = 0, bp;
   11034                 :            :         char buf[WEXT_PNO_MAX_COMMAND_SIZE];
   11035                 :            : 
   11036                 :            :         bp = WEXT_PNOSETUP_HEADER_SIZE;
   11037                 :            :         os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
   11038                 :            :         buf[bp++] = WEXT_PNO_TLV_PREFIX;
   11039                 :            :         buf[bp++] = WEXT_PNO_TLV_VERSION;
   11040                 :            :         buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
   11041                 :            :         buf[bp++] = WEXT_PNO_TLV_RESERVED;
   11042                 :            : 
   11043                 :            :         while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
   11044                 :            :                 /* Check that there is enough space needed for 1 more SSID, the
   11045                 :            :                  * other sections and null termination */
   11046                 :            :                 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
   11047                 :            :                      WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
   11048                 :            :                         break;
   11049                 :            :                 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
   11050                 :            :                                   params->ssids[i].ssid,
   11051                 :            :                                   params->ssids[i].ssid_len);
   11052                 :            :                 buf[bp++] = WEXT_PNO_SSID_SECTION;
   11053                 :            :                 buf[bp++] = params->ssids[i].ssid_len;
   11054                 :            :                 os_memcpy(&buf[bp], params->ssids[i].ssid,
   11055                 :            :                           params->ssids[i].ssid_len);
   11056                 :            :                 bp += params->ssids[i].ssid_len;
   11057                 :            :                 i++;
   11058                 :            :         }
   11059                 :            : 
   11060                 :            :         buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
   11061                 :            :         os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
   11062                 :            :                     WEXT_PNO_SCAN_INTERVAL);
   11063                 :            :         bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
   11064                 :            : 
   11065                 :            :         buf[bp++] = WEXT_PNO_REPEAT_SECTION;
   11066                 :            :         os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
   11067                 :            :                     WEXT_PNO_REPEAT);
   11068                 :            :         bp += WEXT_PNO_REPEAT_LENGTH;
   11069                 :            : 
   11070                 :            :         buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
   11071                 :            :         os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
   11072                 :            :                     WEXT_PNO_MAX_REPEAT);
   11073                 :            :         bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
   11074                 :            : 
   11075                 :            :         memset(&ifr, 0, sizeof(ifr));
   11076                 :            :         memset(&priv_cmd, 0, sizeof(priv_cmd));
   11077                 :            :         os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
   11078                 :            : 
   11079                 :            :         priv_cmd.buf = buf;
   11080                 :            :         priv_cmd.used_len = bp;
   11081                 :            :         priv_cmd.total_len = bp;
   11082                 :            :         ifr.ifr_data = &priv_cmd;
   11083                 :            : 
   11084                 :            :         ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
   11085                 :            : 
   11086                 :            :         if (ret < 0) {
   11087                 :            :                 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
   11088                 :            :                            ret);
   11089                 :            :                 wpa_driver_send_hang_msg(drv);
   11090                 :            :                 return ret;
   11091                 :            :         }
   11092                 :            : 
   11093                 :            :         drv_errors = 0;
   11094                 :            : 
   11095                 :            :         return android_priv_cmd(bss, "PNOFORCE 1");
   11096                 :            : }
   11097                 :            : 
   11098                 :            : 
   11099                 :            : static int android_pno_stop(struct i802_bss *bss)
   11100                 :            : {
   11101                 :            :         return android_priv_cmd(bss, "PNOFORCE 0");
   11102                 :            : }
   11103                 :            : 
   11104                 :            : #endif /* ANDROID */
   11105                 :            : 
   11106                 :            : 
   11107                 :      10119 : static int driver_nl80211_set_key(const char *ifname, void *priv,
   11108                 :            :                                   enum wpa_alg alg, const u8 *addr,
   11109                 :            :                                   int key_idx, int set_tx,
   11110                 :            :                                   const u8 *seq, size_t seq_len,
   11111                 :            :                                   const u8 *key, size_t key_len)
   11112                 :            : {
   11113                 :      10119 :         struct i802_bss *bss = priv;
   11114                 :      10119 :         return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
   11115                 :            :                                           set_tx, seq, seq_len, key, key_len);
   11116                 :            : }
   11117                 :            : 
   11118                 :            : 
   11119                 :        905 : static int driver_nl80211_scan2(void *priv,
   11120                 :            :                                 struct wpa_driver_scan_params *params)
   11121                 :            : {
   11122                 :        905 :         struct i802_bss *bss = priv;
   11123                 :        905 :         return wpa_driver_nl80211_scan(bss, params);
   11124                 :            : }
   11125                 :            : 
   11126                 :            : 
   11127                 :        391 : static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
   11128                 :            :                                          int reason_code)
   11129                 :            : {
   11130                 :        391 :         struct i802_bss *bss = priv;
   11131                 :        391 :         return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
   11132                 :            : }
   11133                 :            : 
   11134                 :            : 
   11135                 :        441 : static int driver_nl80211_authenticate(void *priv,
   11136                 :            :                                        struct wpa_driver_auth_params *params)
   11137                 :            : {
   11138                 :        441 :         struct i802_bss *bss = priv;
   11139                 :        441 :         return wpa_driver_nl80211_authenticate(bss, params);
   11140                 :            : }
   11141                 :            : 
   11142                 :            : 
   11143                 :         22 : static void driver_nl80211_deinit(void *priv)
   11144                 :            : {
   11145                 :         22 :         struct i802_bss *bss = priv;
   11146                 :         22 :         wpa_driver_nl80211_deinit(bss);
   11147                 :         22 : }
   11148                 :            : 
   11149                 :            : 
   11150                 :         36 : static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
   11151                 :            :                                     const char *ifname)
   11152                 :            : {
   11153                 :         36 :         struct i802_bss *bss = priv;
   11154                 :         36 :         return wpa_driver_nl80211_if_remove(bss, type, ifname);
   11155                 :            : }
   11156                 :            : 
   11157                 :            : 
   11158                 :       1877 : static int driver_nl80211_send_mlme(void *priv, const u8 *data,
   11159                 :            :                                     size_t data_len, int noack)
   11160                 :            : {
   11161                 :       1877 :         struct i802_bss *bss = priv;
   11162                 :       1877 :         return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
   11163                 :            :                                             0, 0, 0, 0);
   11164                 :            : }
   11165                 :            : 
   11166                 :            : 
   11167                 :        812 : static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
   11168                 :            : {
   11169                 :        812 :         struct i802_bss *bss = priv;
   11170                 :        812 :         return wpa_driver_nl80211_sta_remove(bss, addr);
   11171                 :            : }
   11172                 :            : 
   11173                 :            : 
   11174                 :          0 : static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
   11175                 :            :                                        const char *ifname, int vlan_id)
   11176                 :            : {
   11177                 :          0 :         struct i802_bss *bss = priv;
   11178                 :          0 :         return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
   11179                 :            : }
   11180                 :            : 
   11181                 :            : 
   11182                 :          0 : static int driver_nl80211_read_sta_data(void *priv,
   11183                 :            :                                         struct hostap_sta_driver_data *data,
   11184                 :            :                                         const u8 *addr)
   11185                 :            : {
   11186                 :          0 :         struct i802_bss *bss = priv;
   11187                 :          0 :         return i802_read_sta_data(bss, data, addr);
   11188                 :            : }
   11189                 :            : 
   11190                 :            : 
   11191                 :        374 : static int driver_nl80211_send_action(void *priv, unsigned int freq,
   11192                 :            :                                       unsigned int wait_time,
   11193                 :            :                                       const u8 *dst, const u8 *src,
   11194                 :            :                                       const u8 *bssid,
   11195                 :            :                                       const u8 *data, size_t data_len,
   11196                 :            :                                       int no_cck)
   11197                 :            : {
   11198                 :        374 :         struct i802_bss *bss = priv;
   11199                 :        374 :         return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
   11200                 :            :                                               bssid, data, data_len, no_cck);
   11201                 :            : }
   11202                 :            : 
   11203                 :            : 
   11204                 :       2458 : static int driver_nl80211_probe_req_report(void *priv, int report)
   11205                 :            : {
   11206                 :       2458 :         struct i802_bss *bss = priv;
   11207                 :       2458 :         return wpa_driver_nl80211_probe_req_report(bss, report);
   11208                 :            : }
   11209                 :            : 
   11210                 :            : 
   11211                 :          0 : static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
   11212                 :            :                                             const u8 *ies, size_t ies_len)
   11213                 :            : {
   11214                 :            :         int ret;
   11215                 :            :         struct nl_msg *msg;
   11216                 :          0 :         struct i802_bss *bss = priv;
   11217                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11218                 :          0 :         u16 mdid = WPA_GET_LE16(md);
   11219                 :            : 
   11220                 :          0 :         msg = nlmsg_alloc();
   11221         [ #  # ]:          0 :         if (!msg)
   11222                 :          0 :                 return -ENOMEM;
   11223                 :            : 
   11224                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
   11225                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
   11226         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   11227         [ #  # ]:          0 :         NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
   11228         [ #  # ]:          0 :         NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
   11229                 :            : 
   11230                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   11231         [ #  # ]:          0 :         if (ret) {
   11232                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
   11233                 :            :                            "err=%d (%s)", ret, strerror(-ret));
   11234                 :            :         }
   11235                 :            : 
   11236                 :          0 :         return ret;
   11237                 :            : 
   11238                 :            : nla_put_failure:
   11239                 :          0 :         nlmsg_free(msg);
   11240                 :          0 :         return -ENOBUFS;
   11241                 :            : }
   11242                 :            : 
   11243                 :            : 
   11244                 :         22 : const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
   11245                 :            : {
   11246                 :         22 :         struct i802_bss *bss = priv;
   11247                 :         22 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11248                 :            : 
   11249         [ +  - ]:         22 :         if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
   11250                 :         22 :                 return NULL;
   11251                 :            : 
   11252                 :         22 :         return bss->addr;
   11253                 :            : }
   11254                 :            : 
   11255                 :            : 
   11256                 :        668 : static const char * scan_state_str(enum scan_states scan_state)
   11257                 :            : {
   11258   [ +  -  +  +  :        668 :         switch (scan_state) {
             -  -  -  -  
                      - ]
   11259                 :            :         case NO_SCAN:
   11260                 :          3 :                 return "NO_SCAN";
   11261                 :            :         case SCAN_REQUESTED:
   11262                 :          0 :                 return "SCAN_REQUESTED";
   11263                 :            :         case SCAN_STARTED:
   11264                 :         13 :                 return "SCAN_STARTED";
   11265                 :            :         case SCAN_COMPLETED:
   11266                 :        652 :                 return "SCAN_COMPLETED";
   11267                 :            :         case SCAN_ABORTED:
   11268                 :          0 :                 return "SCAN_ABORTED";
   11269                 :            :         case SCHED_SCAN_STARTED:
   11270                 :          0 :                 return "SCHED_SCAN_STARTED";
   11271                 :            :         case SCHED_SCAN_STOPPED:
   11272                 :          0 :                 return "SCHED_SCAN_STOPPED";
   11273                 :            :         case SCHED_SCAN_RESULTS:
   11274                 :          0 :                 return "SCHED_SCAN_RESULTS";
   11275                 :            :         }
   11276                 :            : 
   11277                 :        668 :         return "??";
   11278                 :            : }
   11279                 :            : 
   11280                 :            : 
   11281                 :        668 : static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
   11282                 :            : {
   11283                 :        668 :         struct i802_bss *bss = priv;
   11284                 :        668 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11285                 :            :         int res;
   11286                 :            :         char *pos, *end;
   11287                 :            : 
   11288                 :        668 :         pos = buf;
   11289                 :        668 :         end = buf + buflen;
   11290                 :            : 
   11291 [ -  + ][ -  + ]:        668 :         res = os_snprintf(pos, end - pos,
         [ -  + ][ -  + ]
                 [ -  + ]
   11292                 :            :                           "ifindex=%d\n"
   11293                 :            :                           "ifname=%s\n"
   11294                 :            :                           "brname=%s\n"
   11295                 :            :                           "addr=" MACSTR "\n"
   11296                 :            :                           "freq=%d\n"
   11297                 :            :                           "%s%s%s%s%s",
   11298                 :            :                           bss->ifindex,
   11299                 :        668 :                           bss->ifname,
   11300                 :        668 :                           bss->brname,
   11301                 :       4008 :                           MAC2STR(bss->addr),
   11302                 :            :                           bss->freq,
   11303                 :        668 :                           bss->beacon_set ? "beacon_set=1\n" : "",
   11304                 :        668 :                           bss->added_if_into_bridge ?
   11305                 :            :                           "added_if_into_bridge=1\n" : "",
   11306                 :        668 :                           bss->added_bridge ? "added_bridge=1\n" : "",
   11307                 :        668 :                           bss->in_deinit ? "in_deinit=1\n" : "",
   11308                 :        668 :                           bss->if_dynamic ? "if_dynamic=1\n" : "");
   11309 [ +  - ][ -  + ]:        668 :         if (res < 0 || res >= end - pos)
   11310                 :          0 :                 return pos - buf;
   11311                 :        668 :         pos += res;
   11312                 :            : 
   11313         [ -  + ]:        668 :         if (bss->wdev_id_set) {
   11314                 :          0 :                 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
   11315                 :          0 :                                   (unsigned long long) bss->wdev_id);
   11316 [ #  # ][ #  # ]:          0 :                 if (res < 0 || res >= end - pos)
   11317                 :          0 :                         return pos - buf;
   11318                 :          0 :                 pos += res;
   11319                 :            :         }
   11320                 :            : 
   11321 [ -  + ][ -  + ]:        668 :         res = os_snprintf(pos, end - pos,
         [ -  + ][ -  + ]
         [ -  + ][ +  - ]
         [ +  - ][ -  + ]
         [ +  - ][ -  + ]
         [ +  + ][ +  + ]
                 [ -  + ]
   11322                 :            :                           "phyname=%s\n"
   11323                 :            :                           "drv_ifindex=%d\n"
   11324                 :            :                           "operstate=%d\n"
   11325                 :            :                           "scan_state=%s\n"
   11326                 :            :                           "auth_bssid=" MACSTR "\n"
   11327                 :            :                           "auth_attempt_bssid=" MACSTR "\n"
   11328                 :            :                           "bssid=" MACSTR "\n"
   11329                 :            :                           "prev_bssid=" MACSTR "\n"
   11330                 :            :                           "associated=%d\n"
   11331                 :            :                           "assoc_freq=%u\n"
   11332                 :            :                           "monitor_sock=%d\n"
   11333                 :            :                           "monitor_ifidx=%d\n"
   11334                 :            :                           "monitor_refcount=%d\n"
   11335                 :            :                           "last_mgmt_freq=%u\n"
   11336                 :            :                           "eapol_tx_sock=%d\n"
   11337                 :            :                           "%s%s%s%s%s%s%s%s%s%s%s%s%s",
   11338                 :        668 :                           drv->phyname,
   11339                 :            :                           drv->ifindex,
   11340                 :            :                           drv->operstate,
   11341                 :            :                           scan_state_str(drv->scan_state),
   11342                 :       4008 :                           MAC2STR(drv->auth_bssid),
   11343                 :       4008 :                           MAC2STR(drv->auth_attempt_bssid),
   11344                 :       4008 :                           MAC2STR(drv->bssid),
   11345                 :       4008 :                           MAC2STR(drv->prev_bssid),
   11346                 :            :                           drv->associated,
   11347                 :            :                           drv->assoc_freq,
   11348                 :            :                           drv->monitor_sock,
   11349                 :            :                           drv->monitor_ifidx,
   11350                 :            :                           drv->monitor_refcount,
   11351                 :            :                           drv->last_mgmt_freq,
   11352                 :            :                           drv->eapol_tx_sock,
   11353                 :        668 :                           drv->ignore_if_down_event ?
   11354                 :            :                           "ignore_if_down_event=1\n" : "",
   11355                 :        668 :                           drv->scan_complete_events ?
   11356                 :            :                           "scan_complete_events=1\n" : "",
   11357                 :        668 :                           drv->disabled_11b_rates ?
   11358                 :            :                           "disabled_11b_rates=1\n" : "",
   11359                 :        668 :                           drv->pending_remain_on_chan ?
   11360                 :            :                           "pending_remain_on_chan=1\n" : "",
   11361                 :        668 :                           drv->in_interface_list ? "in_interface_list=1\n" : "",
   11362                 :        668 :                           drv->device_ap_sme ? "device_ap_sme=1\n" : "",
   11363                 :        668 :                           drv->poll_command_supported ?
   11364                 :            :                           "poll_command_supported=1\n" : "",
   11365                 :        668 :                           drv->data_tx_status ? "data_tx_status=1\n" : "",
   11366                 :        668 :                           drv->scan_for_auth ? "scan_for_auth=1\n" : "",
   11367                 :        668 :                           drv->retry_auth ? "retry_auth=1\n" : "",
   11368                 :        668 :                           drv->use_monitor ? "use_monitor=1\n" : "",
   11369                 :        668 :                           drv->ignore_next_local_disconnect ?
   11370                 :            :                           "ignore_next_local_disconnect=1\n" : "",
   11371                 :        668 :                           drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
   11372 [ -  + ][ +  - ]:        668 :         if (res < 0 || res >= end - pos)
   11373                 :          0 :                 return pos - buf;
   11374                 :        668 :         pos += res;
   11375                 :            : 
   11376         [ +  - ]:        668 :         if (drv->has_capability) {
   11377                 :        668 :                 res = os_snprintf(pos, end - pos,
   11378                 :            :                                   "capa.key_mgmt=0x%x\n"
   11379                 :            :                                   "capa.enc=0x%x\n"
   11380                 :            :                                   "capa.auth=0x%x\n"
   11381                 :            :                                   "capa.flags=0x%x\n"
   11382                 :            :                                   "capa.max_scan_ssids=%d\n"
   11383                 :            :                                   "capa.max_sched_scan_ssids=%d\n"
   11384                 :            :                                   "capa.sched_scan_supported=%d\n"
   11385                 :            :                                   "capa.max_match_sets=%d\n"
   11386                 :            :                                   "capa.max_remain_on_chan=%u\n"
   11387                 :            :                                   "capa.max_stations=%u\n"
   11388                 :            :                                   "capa.probe_resp_offloads=0x%x\n"
   11389                 :            :                                   "capa.max_acl_mac_addrs=%u\n"
   11390                 :            :                                   "capa.num_multichan_concurrent=%u\n",
   11391                 :            :                                   drv->capa.key_mgmt,
   11392                 :            :                                   drv->capa.enc,
   11393                 :            :                                   drv->capa.auth,
   11394                 :            :                                   drv->capa.flags,
   11395                 :            :                                   drv->capa.max_scan_ssids,
   11396                 :            :                                   drv->capa.max_sched_scan_ssids,
   11397                 :            :                                   drv->capa.sched_scan_supported,
   11398                 :            :                                   drv->capa.max_match_sets,
   11399                 :            :                                   drv->capa.max_remain_on_chan,
   11400                 :            :                                   drv->capa.max_stations,
   11401                 :            :                                   drv->capa.probe_resp_offloads,
   11402                 :            :                                   drv->capa.max_acl_mac_addrs,
   11403                 :            :                                   drv->capa.num_multichan_concurrent);
   11404 [ +  - ][ -  + ]:        668 :                 if (res < 0 || res >= end - pos)
   11405                 :          0 :                         return pos - buf;
   11406                 :        668 :                 pos += res;
   11407                 :            :         }
   11408                 :            : 
   11409                 :        668 :         return pos - buf;
   11410                 :            : }
   11411                 :            : 
   11412                 :            : 
   11413                 :          0 : static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
   11414                 :            : {
   11415         [ #  # ]:          0 :         if (settings->head)
   11416         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
   11417                 :            :                         settings->head_len, settings->head);
   11418                 :            : 
   11419         [ #  # ]:          0 :         if (settings->tail)
   11420         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
   11421                 :            :                         settings->tail_len, settings->tail);
   11422                 :            : 
   11423         [ #  # ]:          0 :         if (settings->beacon_ies)
   11424         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_IE,
   11425                 :            :                         settings->beacon_ies_len, settings->beacon_ies);
   11426                 :            : 
   11427         [ #  # ]:          0 :         if (settings->proberesp_ies)
   11428         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
   11429                 :            :                         settings->proberesp_ies_len, settings->proberesp_ies);
   11430                 :            : 
   11431         [ #  # ]:          0 :         if (settings->assocresp_ies)
   11432         [ #  # ]:          0 :                 NLA_PUT(msg,
   11433                 :            :                         NL80211_ATTR_IE_ASSOC_RESP,
   11434                 :            :                         settings->assocresp_ies_len, settings->assocresp_ies);
   11435                 :            : 
   11436         [ #  # ]:          0 :         if (settings->probe_resp)
   11437         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
   11438                 :            :                         settings->probe_resp_len, settings->probe_resp);
   11439                 :            : 
   11440                 :          0 :         return 0;
   11441                 :            : 
   11442                 :            : nla_put_failure:
   11443                 :          0 :         return -ENOBUFS;
   11444                 :            : }
   11445                 :            : 
   11446                 :            : 
   11447                 :          1 : static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
   11448                 :            : {
   11449                 :            :         struct nl_msg *msg;
   11450                 :          1 :         struct i802_bss *bss = priv;
   11451                 :          1 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11452                 :            :         struct nlattr *beacon_csa;
   11453                 :          1 :         int ret = -ENOBUFS;
   11454                 :            : 
   11455                 :          1 :         wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
   11456                 :          2 :                    settings->cs_count, settings->block_tx,
   11457                 :            :                    settings->freq_params.freq, settings->freq_params.bandwidth,
   11458                 :            :                    settings->freq_params.center_freq1,
   11459                 :            :                    settings->freq_params.center_freq2);
   11460                 :            : 
   11461         [ +  - ]:          1 :         if (!drv->channel_switch_supported) {
   11462                 :          1 :                 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
   11463                 :          1 :                 return -EOPNOTSUPP;
   11464                 :            :         }
   11465                 :            : 
   11466 [ #  # ][ #  # ]:          0 :         if ((drv->nlmode != NL80211_IFTYPE_AP) &&
   11467                 :          0 :             (drv->nlmode != NL80211_IFTYPE_P2P_GO))
   11468                 :          0 :                 return -EOPNOTSUPP;
   11469                 :            : 
   11470                 :            :         /* check settings validity */
   11471 [ #  # ][ #  # ]:          0 :         if (!settings->beacon_csa.tail ||
   11472                 :          0 :             ((settings->beacon_csa.tail_len <=
   11473         [ #  # ]:          0 :               settings->counter_offset_beacon) ||
   11474                 :          0 :              (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
   11475                 :          0 :               settings->cs_count)))
   11476                 :          0 :                 return -EINVAL;
   11477                 :            : 
   11478 [ #  # ][ #  # ]:          0 :         if (settings->beacon_csa.probe_resp &&
   11479                 :          0 :             ((settings->beacon_csa.probe_resp_len <=
   11480         [ #  # ]:          0 :               settings->counter_offset_presp) ||
   11481                 :          0 :              (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
   11482                 :          0 :               settings->cs_count)))
   11483                 :          0 :                 return -EINVAL;
   11484                 :            : 
   11485                 :          0 :         msg = nlmsg_alloc();
   11486         [ #  # ]:          0 :         if (!msg)
   11487                 :          0 :                 return -ENOMEM;
   11488                 :            : 
   11489                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
   11490         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   11491         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
   11492                 :          0 :         ret = nl80211_put_freq_params(msg, &settings->freq_params);
   11493         [ #  # ]:          0 :         if (ret)
   11494                 :          0 :                 goto error;
   11495                 :            : 
   11496         [ #  # ]:          0 :         if (settings->block_tx)
   11497         [ #  # ]:          0 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
   11498                 :            : 
   11499                 :            :         /* beacon_after params */
   11500                 :          0 :         ret = set_beacon_data(msg, &settings->beacon_after);
   11501         [ #  # ]:          0 :         if (ret)
   11502                 :          0 :                 goto error;
   11503                 :            : 
   11504                 :            :         /* beacon_csa params */
   11505                 :          0 :         beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
   11506         [ #  # ]:          0 :         if (!beacon_csa)
   11507                 :          0 :                 goto nla_put_failure;
   11508                 :            : 
   11509                 :          0 :         ret = set_beacon_data(msg, &settings->beacon_csa);
   11510         [ #  # ]:          0 :         if (ret)
   11511                 :          0 :                 goto error;
   11512                 :            : 
   11513         [ #  # ]:          0 :         NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
   11514                 :            :                     settings->counter_offset_beacon);
   11515                 :            : 
   11516         [ #  # ]:          0 :         if (settings->beacon_csa.probe_resp)
   11517         [ #  # ]:          0 :                 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
   11518                 :            :                             settings->counter_offset_presp);
   11519                 :            : 
   11520                 :          0 :         nla_nest_end(msg, beacon_csa);
   11521                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   11522         [ #  # ]:          0 :         if (ret) {
   11523                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
   11524                 :            :                            ret, strerror(-ret));
   11525                 :            :         }
   11526                 :          0 :         return ret;
   11527                 :            : 
   11528                 :            : nla_put_failure:
   11529                 :          0 :         ret = -ENOBUFS;
   11530                 :            : error:
   11531                 :          0 :         nlmsg_free(msg);
   11532                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
   11533                 :          1 :         return ret;
   11534                 :            : }
   11535                 :            : 
   11536                 :            : 
   11537                 :          4 : static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
   11538                 :            :                                u8 qos_map_set_len)
   11539                 :            : {
   11540                 :          4 :         struct i802_bss *bss = priv;
   11541                 :          4 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11542                 :            :         struct nl_msg *msg;
   11543                 :            :         int ret;
   11544                 :            : 
   11545                 :          4 :         msg = nlmsg_alloc();
   11546         [ -  + ]:          4 :         if (!msg)
   11547                 :          0 :                 return -ENOMEM;
   11548                 :            : 
   11549                 :          4 :         wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
   11550                 :            :                     qos_map_set, qos_map_set_len);
   11551                 :            : 
   11552                 :          4 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
   11553         [ +  - ]:          4 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   11554         [ +  - ]:          4 :         NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
   11555                 :            : 
   11556                 :          4 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   11557         [ -  + ]:          4 :         if (ret)
   11558                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
   11559                 :            : 
   11560                 :          4 :         return ret;
   11561                 :            : 
   11562                 :            : nla_put_failure:
   11563                 :          0 :         nlmsg_free(msg);
   11564                 :          4 :         return -ENOBUFS;
   11565                 :            : }
   11566                 :            : 
   11567                 :            : 
   11568                 :            : const struct wpa_driver_ops wpa_driver_nl80211_ops = {
   11569                 :            :         .name = "nl80211",
   11570                 :            :         .desc = "Linux nl80211/cfg80211",
   11571                 :            :         .get_bssid = wpa_driver_nl80211_get_bssid,
   11572                 :            :         .get_ssid = wpa_driver_nl80211_get_ssid,
   11573                 :            :         .set_key = driver_nl80211_set_key,
   11574                 :            :         .scan2 = driver_nl80211_scan2,
   11575                 :            :         .sched_scan = wpa_driver_nl80211_sched_scan,
   11576                 :            :         .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
   11577                 :            :         .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
   11578                 :            :         .deauthenticate = driver_nl80211_deauthenticate,
   11579                 :            :         .authenticate = driver_nl80211_authenticate,
   11580                 :            :         .associate = wpa_driver_nl80211_associate,
   11581                 :            :         .global_init = nl80211_global_init,
   11582                 :            :         .global_deinit = nl80211_global_deinit,
   11583                 :            :         .init2 = wpa_driver_nl80211_init,
   11584                 :            :         .deinit = driver_nl80211_deinit,
   11585                 :            :         .get_capa = wpa_driver_nl80211_get_capa,
   11586                 :            :         .set_operstate = wpa_driver_nl80211_set_operstate,
   11587                 :            :         .set_supp_port = wpa_driver_nl80211_set_supp_port,
   11588                 :            :         .set_country = wpa_driver_nl80211_set_country,
   11589                 :            :         .get_country = wpa_driver_nl80211_get_country,
   11590                 :            :         .set_ap = wpa_driver_nl80211_set_ap,
   11591                 :            :         .set_acl = wpa_driver_nl80211_set_acl,
   11592                 :            :         .if_add = wpa_driver_nl80211_if_add,
   11593                 :            :         .if_remove = driver_nl80211_if_remove,
   11594                 :            :         .send_mlme = driver_nl80211_send_mlme,
   11595                 :            :         .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
   11596                 :            :         .sta_add = wpa_driver_nl80211_sta_add,
   11597                 :            :         .sta_remove = driver_nl80211_sta_remove,
   11598                 :            :         .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
   11599                 :            :         .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
   11600                 :            :         .hapd_init = i802_init,
   11601                 :            :         .hapd_deinit = i802_deinit,
   11602                 :            :         .set_wds_sta = i802_set_wds_sta,
   11603                 :            :         .get_seqnum = i802_get_seqnum,
   11604                 :            :         .flush = i802_flush,
   11605                 :            :         .get_inact_sec = i802_get_inact_sec,
   11606                 :            :         .sta_clear_stats = i802_sta_clear_stats,
   11607                 :            :         .set_rts = i802_set_rts,
   11608                 :            :         .set_frag = i802_set_frag,
   11609                 :            :         .set_tx_queue_params = i802_set_tx_queue_params,
   11610                 :            :         .set_sta_vlan = driver_nl80211_set_sta_vlan,
   11611                 :            :         .sta_deauth = i802_sta_deauth,
   11612                 :            :         .sta_disassoc = i802_sta_disassoc,
   11613                 :            :         .read_sta_data = driver_nl80211_read_sta_data,
   11614                 :            :         .set_freq = i802_set_freq,
   11615                 :            :         .send_action = driver_nl80211_send_action,
   11616                 :            :         .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
   11617                 :            :         .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
   11618                 :            :         .cancel_remain_on_channel =
   11619                 :            :         wpa_driver_nl80211_cancel_remain_on_channel,
   11620                 :            :         .probe_req_report = driver_nl80211_probe_req_report,
   11621                 :            :         .deinit_ap = wpa_driver_nl80211_deinit_ap,
   11622                 :            :         .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
   11623                 :            :         .resume = wpa_driver_nl80211_resume,
   11624                 :            :         .send_ft_action = nl80211_send_ft_action,
   11625                 :            :         .signal_monitor = nl80211_signal_monitor,
   11626                 :            :         .signal_poll = nl80211_signal_poll,
   11627                 :            :         .send_frame = nl80211_send_frame,
   11628                 :            :         .shared_freq = wpa_driver_nl80211_shared_freq,
   11629                 :            :         .set_param = nl80211_set_param,
   11630                 :            :         .get_radio_name = nl80211_get_radio_name,
   11631                 :            :         .add_pmkid = nl80211_add_pmkid,
   11632                 :            :         .remove_pmkid = nl80211_remove_pmkid,
   11633                 :            :         .flush_pmkid = nl80211_flush_pmkid,
   11634                 :            :         .set_rekey_info = nl80211_set_rekey_info,
   11635                 :            :         .poll_client = nl80211_poll_client,
   11636                 :            :         .set_p2p_powersave = nl80211_set_p2p_powersave,
   11637                 :            :         .start_dfs_cac = nl80211_start_radar_detection,
   11638                 :            :         .stop_ap = wpa_driver_nl80211_stop_ap,
   11639                 :            : #ifdef CONFIG_TDLS
   11640                 :            :         .send_tdls_mgmt = nl80211_send_tdls_mgmt,
   11641                 :            :         .tdls_oper = nl80211_tdls_oper,
   11642                 :            : #endif /* CONFIG_TDLS */
   11643                 :            :         .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
   11644                 :            :         .get_mac_addr = wpa_driver_nl80211_get_macaddr,
   11645                 :            :         .get_survey = wpa_driver_nl80211_get_survey,
   11646                 :            :         .status = wpa_driver_nl80211_status,
   11647                 :            :         .switch_channel = nl80211_switch_channel,
   11648                 :            : #ifdef ANDROID_P2P
   11649                 :            :         .set_noa = wpa_driver_set_p2p_noa,
   11650                 :            :         .get_noa = wpa_driver_get_p2p_noa,
   11651                 :            :         .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
   11652                 :            : #endif /* ANDROID_P2P */
   11653                 :            : #ifdef ANDROID
   11654                 :            :         .driver_cmd = wpa_driver_nl80211_driver_cmd,
   11655                 :            : #endif /* ANDROID */
   11656                 :            :         .set_qos_map = nl80211_set_qos_map,
   11657                 :            : };

Generated by: LCOV version 1.9