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 1393793999 Lines: 3520 5822 60.5 %
Date: 2014-03-02 Functions: 231 296 78.0 %
Branches: 1712 3737 45.8 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Driver interaction with Linux nl80211/cfg80211
       3                 :            :  * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
       4                 :            :  * Copyright (c) 2003-2004, Instant802 Networks, Inc.
       5                 :            :  * Copyright (c) 2005-2006, Devicescape Software, Inc.
       6                 :            :  * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
       7                 :            :  * Copyright (c) 2009-2010, Atheros Communications
       8                 :            :  *
       9                 :            :  * This software may be distributed under the terms of the BSD license.
      10                 :            :  * See README for more details.
      11                 :            :  */
      12                 :            : 
      13                 :            : #include "includes.h"
      14                 :            : #include <sys/ioctl.h>
      15                 :            : #include <sys/types.h>
      16                 :            : #include <sys/stat.h>
      17                 :            : #include <fcntl.h>
      18                 :            : #include <net/if.h>
      19                 :            : #include <netlink/genl/genl.h>
      20                 :            : #include <netlink/genl/family.h>
      21                 :            : #include <netlink/genl/ctrl.h>
      22                 :            : #include <linux/rtnetlink.h>
      23                 :            : #include <netpacket/packet.h>
      24                 :            : #include <linux/filter.h>
      25                 :            : #include <linux/errqueue.h>
      26                 :            : #include "nl80211_copy.h"
      27                 :            : 
      28                 :            : #include "common.h"
      29                 :            : #include "eloop.h"
      30                 :            : #include "utils/list.h"
      31                 :            : #include "common/qca-vendor.h"
      32                 :            : #include "common/ieee802_11_defs.h"
      33                 :            : #include "common/ieee802_11_common.h"
      34                 :            : #include "l2_packet/l2_packet.h"
      35                 :            : #include "netlink.h"
      36                 :            : #include "linux_ioctl.h"
      37                 :            : #include "radiotap.h"
      38                 :            : #include "radiotap_iter.h"
      39                 :            : #include "rfkill.h"
      40                 :            : #include "driver.h"
      41                 :            : 
      42                 :            : #ifndef SO_WIFI_STATUS
      43                 :            : # if defined(__sparc__)
      44                 :            : #  define SO_WIFI_STATUS        0x0025
      45                 :            : # elif defined(__parisc__)
      46                 :            : #  define SO_WIFI_STATUS        0x4022
      47                 :            : # else
      48                 :            : #  define SO_WIFI_STATUS        41
      49                 :            : # endif
      50                 :            : 
      51                 :            : # define SCM_WIFI_STATUS        SO_WIFI_STATUS
      52                 :            : #endif
      53                 :            : 
      54                 :            : #ifndef SO_EE_ORIGIN_TXSTATUS
      55                 :            : #define SO_EE_ORIGIN_TXSTATUS   4
      56                 :            : #endif
      57                 :            : 
      58                 :            : #ifndef PACKET_TX_TIMESTAMP
      59                 :            : #define PACKET_TX_TIMESTAMP     16
      60                 :            : #endif
      61                 :            : 
      62                 :            : #ifdef ANDROID
      63                 :            : #include "android_drv.h"
      64                 :            : #endif /* ANDROID */
      65                 :            : #ifdef CONFIG_LIBNL20
      66                 :            : /* libnl 2.0 compatibility code */
      67                 :            : #define nl_handle nl_sock
      68                 :            : #define nl80211_handle_alloc nl_socket_alloc_cb
      69                 :            : #define nl80211_handle_destroy nl_socket_free
      70                 :            : #else
      71                 :            : /*
      72                 :            :  * libnl 1.1 has a bug, it tries to allocate socket numbers densely
      73                 :            :  * but when you free a socket again it will mess up its bitmap and
      74                 :            :  * and use the wrong number the next time it needs a socket ID.
      75                 :            :  * Therefore, we wrap the handle alloc/destroy and add our own pid
      76                 :            :  * accounting.
      77                 :            :  */
      78                 :            : static uint32_t port_bitmap[32] = { 0 };
      79                 :            : 
      80                 :            : static struct nl_handle *nl80211_handle_alloc(void *cb)
      81                 :            : {
      82                 :            :         struct nl_handle *handle;
      83                 :            :         uint32_t pid = getpid() & 0x3FFFFF;
      84                 :            :         int i;
      85                 :            : 
      86                 :            :         handle = nl_handle_alloc_cb(cb);
      87                 :            : 
      88                 :            :         for (i = 0; i < 1024; i++) {
      89                 :            :                 if (port_bitmap[i / 32] & (1 << (i % 32)))
      90                 :            :                         continue;
      91                 :            :                 port_bitmap[i / 32] |= 1 << (i % 32);
      92                 :            :                 pid += i << 22;
      93                 :            :                 break;
      94                 :            :         }
      95                 :            : 
      96                 :            :         nl_socket_set_local_port(handle, pid);
      97                 :            : 
      98                 :            :         return handle;
      99                 :            : }
     100                 :            : 
     101                 :            : static void nl80211_handle_destroy(struct nl_handle *handle)
     102                 :            : {
     103                 :            :         uint32_t port = nl_socket_get_local_port(handle);
     104                 :            : 
     105                 :            :         port >>= 22;
     106                 :            :         port_bitmap[port / 32] &= ~(1 << (port % 32));
     107                 :            : 
     108                 :            :         nl_handle_destroy(handle);
     109                 :            : }
     110                 :            : #endif /* CONFIG_LIBNL20 */
     111                 :            : 
     112                 :            : 
     113                 :            : #ifdef ANDROID
     114                 :            : /* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
     115                 :            : static int android_nl_socket_set_nonblocking(struct nl_handle *handle)
     116                 :            : {
     117                 :            :         return fcntl(nl_socket_get_fd(handle), F_SETFL, O_NONBLOCK);
     118                 :            : }
     119                 :            : #undef nl_socket_set_nonblocking
     120                 :            : #define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
     121                 :            : #endif /* ANDROID */
     122                 :            : 
     123                 :            : 
     124                 :       1884 : static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
     125                 :            : {
     126                 :            :         struct nl_handle *handle;
     127                 :            : 
     128                 :       1884 :         handle = nl80211_handle_alloc(cb);
     129         [ -  + ]:       1884 :         if (handle == NULL) {
     130                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
     131                 :            :                            "callbacks (%s)", dbg);
     132                 :          0 :                 return NULL;
     133                 :            :         }
     134                 :            : 
     135         [ -  + ]:       1884 :         if (genl_connect(handle)) {
     136                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
     137                 :            :                            "netlink (%s)", dbg);
     138                 :          0 :                 nl80211_handle_destroy(handle);
     139                 :          0 :                 return NULL;
     140                 :            :         }
     141                 :            : 
     142                 :       1884 :         return handle;
     143                 :            : }
     144                 :            : 
     145                 :            : 
     146                 :       1884 : static void nl_destroy_handles(struct nl_handle **handle)
     147                 :            : {
     148         [ -  + ]:       1884 :         if (*handle == NULL)
     149                 :       1884 :                 return;
     150                 :       1884 :         nl80211_handle_destroy(*handle);
     151                 :       1884 :         *handle = NULL;
     152                 :            : }
     153                 :            : 
     154                 :            : 
     155                 :            : #if __WORDSIZE == 64
     156                 :            : #define ELOOP_SOCKET_INVALID    (intptr_t) 0x8888888888888889ULL
     157                 :            : #else
     158                 :            : #define ELOOP_SOCKET_INVALID    (intptr_t) 0x88888889ULL
     159                 :            : #endif
     160                 :            : 
     161                 :       1879 : static void nl80211_register_eloop_read(struct nl_handle **handle,
     162                 :            :                                         eloop_sock_handler handler,
     163                 :            :                                         void *eloop_data)
     164                 :            : {
     165                 :       1879 :         nl_socket_set_nonblocking(*handle);
     166                 :       1879 :         eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
     167                 :            :                                  eloop_data, *handle);
     168                 :       1879 :         *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
     169                 :       1879 : }
     170                 :            : 
     171                 :            : 
     172                 :       1879 : static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
     173                 :            : {
     174                 :       1879 :         *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
     175                 :       1879 :         eloop_unregister_read_sock(nl_socket_get_fd(*handle));
     176                 :       1879 :         nl_destroy_handles(handle);
     177                 :       1879 : }
     178                 :            : 
     179                 :            : 
     180                 :            : #ifndef IFF_LOWER_UP
     181                 :            : #define IFF_LOWER_UP   0x10000         /* driver signals L1 up         */
     182                 :            : #endif
     183                 :            : #ifndef IFF_DORMANT
     184                 :            : #define IFF_DORMANT    0x20000         /* driver signals dormant       */
     185                 :            : #endif
     186                 :            : 
     187                 :            : #ifndef IF_OPER_DORMANT
     188                 :            : #define IF_OPER_DORMANT 5
     189                 :            : #endif
     190                 :            : #ifndef IF_OPER_UP
     191                 :            : #define IF_OPER_UP 6
     192                 :            : #endif
     193                 :            : 
     194                 :            : struct nl80211_global {
     195                 :            :         struct dl_list interfaces;
     196                 :            :         int if_add_ifindex;
     197                 :            :         u64 if_add_wdevid;
     198                 :            :         int if_add_wdevid_set;
     199                 :            :         struct netlink_data *netlink;
     200                 :            :         struct nl_cb *nl_cb;
     201                 :            :         struct nl_handle *nl;
     202                 :            :         int nl80211_id;
     203                 :            :         int ioctl_sock; /* socket for ioctl() use */
     204                 :            : 
     205                 :            :         struct nl_handle *nl_event;
     206                 :            : };
     207                 :            : 
     208                 :            : struct nl80211_wiphy_data {
     209                 :            :         struct dl_list list;
     210                 :            :         struct dl_list bsss;
     211                 :            :         struct dl_list drvs;
     212                 :            : 
     213                 :            :         struct nl_handle *nl_beacons;
     214                 :            :         struct nl_cb *nl_cb;
     215                 :            : 
     216                 :            :         int wiphy_idx;
     217                 :            : };
     218                 :            : 
     219                 :            : static void nl80211_global_deinit(void *priv);
     220                 :            : 
     221                 :            : struct i802_bss {
     222                 :            :         struct wpa_driver_nl80211_data *drv;
     223                 :            :         struct i802_bss *next;
     224                 :            :         int ifindex;
     225                 :            :         u64 wdev_id;
     226                 :            :         char ifname[IFNAMSIZ + 1];
     227                 :            :         char brname[IFNAMSIZ];
     228                 :            :         unsigned int beacon_set:1;
     229                 :            :         unsigned int added_if_into_bridge:1;
     230                 :            :         unsigned int added_bridge:1;
     231                 :            :         unsigned int in_deinit:1;
     232                 :            :         unsigned int wdev_id_set:1;
     233                 :            :         unsigned int added_if:1;
     234                 :            : 
     235                 :            :         u8 addr[ETH_ALEN];
     236                 :            : 
     237                 :            :         int freq;
     238                 :            :         int if_dynamic;
     239                 :            : 
     240                 :            :         void *ctx;
     241                 :            :         struct nl_handle *nl_preq, *nl_mgmt;
     242                 :            :         struct nl_cb *nl_cb;
     243                 :            : 
     244                 :            :         struct nl80211_wiphy_data *wiphy_data;
     245                 :            :         struct dl_list wiphy_list;
     246                 :            : };
     247                 :            : 
     248                 :            : struct wpa_driver_nl80211_data {
     249                 :            :         struct nl80211_global *global;
     250                 :            :         struct dl_list list;
     251                 :            :         struct dl_list wiphy_list;
     252                 :            :         char phyname[32];
     253                 :            :         void *ctx;
     254                 :            :         int ifindex;
     255                 :            :         int if_removed;
     256                 :            :         int if_disabled;
     257                 :            :         int ignore_if_down_event;
     258                 :            :         struct rfkill_data *rfkill;
     259                 :            :         struct wpa_driver_capa capa;
     260                 :            :         u8 *extended_capa, *extended_capa_mask;
     261                 :            :         unsigned int extended_capa_len;
     262                 :            :         int has_capability;
     263                 :            : 
     264                 :            :         int operstate;
     265                 :            : 
     266                 :            :         int scan_complete_events;
     267                 :            :         enum scan_states {
     268                 :            :                 NO_SCAN, SCAN_REQUESTED, SCAN_STARTED, SCAN_COMPLETED,
     269                 :            :                 SCAN_ABORTED, SCHED_SCAN_STARTED, SCHED_SCAN_STOPPED,
     270                 :            :                 SCHED_SCAN_RESULTS
     271                 :            :         } scan_state;
     272                 :            : 
     273                 :            :         struct nl_cb *nl_cb;
     274                 :            : 
     275                 :            :         u8 auth_bssid[ETH_ALEN];
     276                 :            :         u8 auth_attempt_bssid[ETH_ALEN];
     277                 :            :         u8 bssid[ETH_ALEN];
     278                 :            :         u8 prev_bssid[ETH_ALEN];
     279                 :            :         int associated;
     280                 :            :         u8 ssid[32];
     281                 :            :         size_t ssid_len;
     282                 :            :         enum nl80211_iftype nlmode;
     283                 :            :         enum nl80211_iftype ap_scan_as_station;
     284                 :            :         unsigned int assoc_freq;
     285                 :            : 
     286                 :            :         int monitor_sock;
     287                 :            :         int monitor_ifidx;
     288                 :            :         int monitor_refcount;
     289                 :            : 
     290                 :            :         unsigned int disabled_11b_rates:1;
     291                 :            :         unsigned int pending_remain_on_chan:1;
     292                 :            :         unsigned int in_interface_list:1;
     293                 :            :         unsigned int device_ap_sme:1;
     294                 :            :         unsigned int poll_command_supported:1;
     295                 :            :         unsigned int data_tx_status:1;
     296                 :            :         unsigned int scan_for_auth:1;
     297                 :            :         unsigned int retry_auth:1;
     298                 :            :         unsigned int use_monitor:1;
     299                 :            :         unsigned int ignore_next_local_disconnect:1;
     300                 :            :         unsigned int allow_p2p_device:1;
     301                 :            :         unsigned int hostapd:1;
     302                 :            :         unsigned int start_mode_ap:1;
     303                 :            :         unsigned int start_iface_up:1;
     304                 :            :         unsigned int test_use_roc_tx:1;
     305                 :            : 
     306                 :            :         u64 remain_on_chan_cookie;
     307                 :            :         u64 send_action_cookie;
     308                 :            : 
     309                 :            :         unsigned int last_mgmt_freq;
     310                 :            : 
     311                 :            :         struct wpa_driver_scan_filter *filter_ssids;
     312                 :            :         size_t num_filter_ssids;
     313                 :            : 
     314                 :            :         struct i802_bss *first_bss;
     315                 :            : 
     316                 :            :         int eapol_tx_sock;
     317                 :            : 
     318                 :            :         int eapol_sock; /* socket for EAPOL frames */
     319                 :            : 
     320                 :            :         int default_if_indices[16];
     321                 :            :         int *if_indices;
     322                 :            :         int num_if_indices;
     323                 :            : 
     324                 :            :         /* From failed authentication command */
     325                 :            :         int auth_freq;
     326                 :            :         u8 auth_bssid_[ETH_ALEN];
     327                 :            :         u8 auth_ssid[32];
     328                 :            :         size_t auth_ssid_len;
     329                 :            :         int auth_alg;
     330                 :            :         u8 *auth_ie;
     331                 :            :         size_t auth_ie_len;
     332                 :            :         u8 auth_wep_key[4][16];
     333                 :            :         size_t auth_wep_key_len[4];
     334                 :            :         int auth_wep_tx_keyidx;
     335                 :            :         int auth_local_state_change;
     336                 :            :         int auth_p2p;
     337                 :            : };
     338                 :            : 
     339                 :            : 
     340                 :            : static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
     341                 :            : static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
     342                 :            :                                             void *timeout_ctx);
     343                 :            : static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
     344                 :            :                                        enum nl80211_iftype nlmode);
     345                 :            : static int
     346                 :            : wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
     347                 :            :                                    const u8 *set_addr, int first);
     348                 :            : static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
     349                 :            :                                    const u8 *addr, int cmd, u16 reason_code,
     350                 :            :                                    int local_state_change);
     351                 :            : static void nl80211_remove_monitor_interface(
     352                 :            :         struct wpa_driver_nl80211_data *drv);
     353                 :            : static int nl80211_send_frame_cmd(struct i802_bss *bss,
     354                 :            :                                   unsigned int freq, unsigned int wait,
     355                 :            :                                   const u8 *buf, size_t buf_len, u64 *cookie,
     356                 :            :                                   int no_cck, int no_ack, int offchanok);
     357                 :            : static int nl80211_register_frame(struct i802_bss *bss,
     358                 :            :                                   struct nl_handle *hl_handle,
     359                 :            :                                   u16 type, const u8 *match, size_t match_len);
     360                 :            : static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
     361                 :            :                                                int report);
     362                 :            : #ifdef ANDROID
     363                 :            : static int android_pno_start(struct i802_bss *bss,
     364                 :            :                              struct wpa_driver_scan_params *params);
     365                 :            : static int android_pno_stop(struct i802_bss *bss);
     366                 :            : extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
     367                 :            :                                          size_t buf_len);
     368                 :            : #endif /* ANDROID */
     369                 :            : #ifdef ANDROID_P2P
     370                 :            : int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
     371                 :            : int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
     372                 :            : int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
     373                 :            : int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
     374                 :            :                                  const struct wpabuf *proberesp,
     375                 :            :                                  const struct wpabuf *assocresp);
     376                 :            : #endif /* ANDROID_P2P */
     377                 :            : 
     378                 :            : static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
     379                 :            : static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
     380                 :            : static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
     381                 :            : static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
     382                 :            :                                         enum wpa_driver_if_type type,
     383                 :            :                                         const char *ifname);
     384                 :            : 
     385                 :            : static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
     386                 :            :                                        struct hostapd_freq_params *freq);
     387                 :            : static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
     388                 :            :                                      int ifindex, int disabled);
     389                 :            : 
     390                 :            : static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
     391                 :            : static int wpa_driver_nl80211_authenticate_retry(
     392                 :            :         struct wpa_driver_nl80211_data *drv);
     393                 :            : 
     394                 :            : static int i802_set_iface_flags(struct i802_bss *bss, int up);
     395                 :            : 
     396                 :            : 
     397                 :      29677 : static const char * nl80211_command_to_string(enum nl80211_commands cmd)
     398                 :            : {
     399                 :            : #define C2S(x) case x: return #x;
     400   [ -  -  -  -  :      29677 :         switch (cmd) {
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  +  -  -  
          +  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
          +  -  +  +  +  
          +  +  -  -  +  
          -  -  +  -  +  
          -  -  -  -  -  
          -  +  +  -  -  
          +  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
                   -  - ]
     401                 :          0 :         C2S(NL80211_CMD_UNSPEC)
     402                 :          0 :         C2S(NL80211_CMD_GET_WIPHY)
     403                 :          0 :         C2S(NL80211_CMD_SET_WIPHY)
     404                 :          0 :         C2S(NL80211_CMD_NEW_WIPHY)
     405                 :          0 :         C2S(NL80211_CMD_DEL_WIPHY)
     406                 :          0 :         C2S(NL80211_CMD_GET_INTERFACE)
     407                 :          0 :         C2S(NL80211_CMD_SET_INTERFACE)
     408                 :          0 :         C2S(NL80211_CMD_NEW_INTERFACE)
     409                 :          0 :         C2S(NL80211_CMD_DEL_INTERFACE)
     410                 :          0 :         C2S(NL80211_CMD_GET_KEY)
     411                 :          0 :         C2S(NL80211_CMD_SET_KEY)
     412                 :          0 :         C2S(NL80211_CMD_NEW_KEY)
     413                 :          0 :         C2S(NL80211_CMD_DEL_KEY)
     414                 :          0 :         C2S(NL80211_CMD_GET_BEACON)
     415                 :          0 :         C2S(NL80211_CMD_SET_BEACON)
     416                 :          0 :         C2S(NL80211_CMD_START_AP)
     417                 :         71 :         C2S(NL80211_CMD_STOP_AP)
     418                 :          0 :         C2S(NL80211_CMD_GET_STATION)
     419                 :          0 :         C2S(NL80211_CMD_SET_STATION)
     420                 :       1423 :         C2S(NL80211_CMD_NEW_STATION)
     421                 :       1388 :         C2S(NL80211_CMD_DEL_STATION)
     422                 :          0 :         C2S(NL80211_CMD_GET_MPATH)
     423                 :          0 :         C2S(NL80211_CMD_SET_MPATH)
     424                 :          0 :         C2S(NL80211_CMD_NEW_MPATH)
     425                 :          0 :         C2S(NL80211_CMD_DEL_MPATH)
     426                 :          0 :         C2S(NL80211_CMD_SET_BSS)
     427                 :          0 :         C2S(NL80211_CMD_SET_REG)
     428                 :          0 :         C2S(NL80211_CMD_REQ_SET_REG)
     429                 :          0 :         C2S(NL80211_CMD_GET_MESH_CONFIG)
     430                 :          0 :         C2S(NL80211_CMD_SET_MESH_CONFIG)
     431                 :          0 :         C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
     432                 :          0 :         C2S(NL80211_CMD_GET_REG)
     433                 :          0 :         C2S(NL80211_CMD_GET_SCAN)
     434                 :       1381 :         C2S(NL80211_CMD_TRIGGER_SCAN)
     435                 :       1376 :         C2S(NL80211_CMD_NEW_SCAN_RESULTS)
     436                 :          0 :         C2S(NL80211_CMD_SCAN_ABORTED)
     437                 :       2053 :         C2S(NL80211_CMD_REG_CHANGE)
     438                 :       1389 :         C2S(NL80211_CMD_AUTHENTICATE)
     439                 :       1348 :         C2S(NL80211_CMD_ASSOCIATE)
     440                 :       1332 :         C2S(NL80211_CMD_DEAUTHENTICATE)
     441                 :          2 :         C2S(NL80211_CMD_DISASSOCIATE)
     442                 :          0 :         C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
     443                 :          0 :         C2S(NL80211_CMD_REG_BEACON_HINT)
     444                 :          5 :         C2S(NL80211_CMD_JOIN_IBSS)
     445                 :          0 :         C2S(NL80211_CMD_LEAVE_IBSS)
     446                 :          0 :         C2S(NL80211_CMD_TESTMODE)
     447                 :        674 :         C2S(NL80211_CMD_CONNECT)
     448                 :          0 :         C2S(NL80211_CMD_ROAM)
     449                 :        661 :         C2S(NL80211_CMD_DISCONNECT)
     450                 :          0 :         C2S(NL80211_CMD_SET_WIPHY_NETNS)
     451                 :          0 :         C2S(NL80211_CMD_GET_SURVEY)
     452                 :          0 :         C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
     453                 :          0 :         C2S(NL80211_CMD_SET_PMKSA)
     454                 :          0 :         C2S(NL80211_CMD_DEL_PMKSA)
     455                 :          0 :         C2S(NL80211_CMD_FLUSH_PMKSA)
     456                 :        775 :         C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
     457                 :        775 :         C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
     458                 :          0 :         C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
     459                 :          0 :         C2S(NL80211_CMD_REGISTER_FRAME)
     460                 :       8812 :         C2S(NL80211_CMD_FRAME)
     461                 :       6212 :         C2S(NL80211_CMD_FRAME_TX_STATUS)
     462                 :          0 :         C2S(NL80211_CMD_SET_POWER_SAVE)
     463                 :          0 :         C2S(NL80211_CMD_GET_POWER_SAVE)
     464                 :          0 :         C2S(NL80211_CMD_SET_CQM)
     465                 :          0 :         C2S(NL80211_CMD_NOTIFY_CQM)
     466                 :          0 :         C2S(NL80211_CMD_SET_CHANNEL)
     467                 :          0 :         C2S(NL80211_CMD_SET_WDS_PEER)
     468                 :          0 :         C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
     469                 :          0 :         C2S(NL80211_CMD_JOIN_MESH)
     470                 :          0 :         C2S(NL80211_CMD_LEAVE_MESH)
     471                 :          0 :         C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
     472                 :          0 :         C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
     473                 :          0 :         C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
     474                 :          0 :         C2S(NL80211_CMD_GET_WOWLAN)
     475                 :          0 :         C2S(NL80211_CMD_SET_WOWLAN)
     476                 :          0 :         C2S(NL80211_CMD_START_SCHED_SCAN)
     477                 :          0 :         C2S(NL80211_CMD_STOP_SCHED_SCAN)
     478                 :          0 :         C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
     479                 :          0 :         C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
     480                 :          0 :         C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
     481                 :          0 :         C2S(NL80211_CMD_PMKSA_CANDIDATE)
     482                 :          0 :         C2S(NL80211_CMD_TDLS_OPER)
     483                 :          0 :         C2S(NL80211_CMD_TDLS_MGMT)
     484                 :          0 :         C2S(NL80211_CMD_UNEXPECTED_FRAME)
     485                 :          0 :         C2S(NL80211_CMD_PROBE_CLIENT)
     486                 :          0 :         C2S(NL80211_CMD_REGISTER_BEACONS)
     487                 :          0 :         C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
     488                 :          0 :         C2S(NL80211_CMD_SET_NOACK_MAP)
     489                 :          0 :         C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
     490                 :          0 :         C2S(NL80211_CMD_START_P2P_DEVICE)
     491                 :          0 :         C2S(NL80211_CMD_STOP_P2P_DEVICE)
     492                 :          0 :         C2S(NL80211_CMD_CONN_FAILED)
     493                 :          0 :         C2S(NL80211_CMD_SET_MCAST_RATE)
     494                 :          0 :         C2S(NL80211_CMD_SET_MAC_ACL)
     495                 :          0 :         C2S(NL80211_CMD_RADAR_DETECT)
     496                 :          0 :         C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
     497                 :          0 :         C2S(NL80211_CMD_UPDATE_FT_IES)
     498                 :          0 :         C2S(NL80211_CMD_FT_EVENT)
     499                 :          0 :         C2S(NL80211_CMD_CRIT_PROTOCOL_START)
     500                 :          0 :         C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
     501                 :          0 :         C2S(NL80211_CMD_GET_COALESCE)
     502                 :          0 :         C2S(NL80211_CMD_SET_COALESCE)
     503                 :          0 :         C2S(NL80211_CMD_CHANNEL_SWITCH)
     504                 :          0 :         C2S(NL80211_CMD_VENDOR)
     505                 :          0 :         C2S(NL80211_CMD_SET_QOS_MAP)
     506                 :            :         default:
     507                 :      29677 :                 return "NL80211_CMD_UNKNOWN";
     508                 :            :         }
     509                 :            : #undef C2S
     510                 :            : }
     511                 :            : 
     512                 :            : 
     513                 :            : /* Converts nl80211_chan_width to a common format */
     514                 :          0 : static enum chan_width convert2width(int width)
     515                 :            : {
     516   [ #  #  #  #  :          0 :         switch (width) {
                #  #  # ]
     517                 :            :         case NL80211_CHAN_WIDTH_20_NOHT:
     518                 :          0 :                 return CHAN_WIDTH_20_NOHT;
     519                 :            :         case NL80211_CHAN_WIDTH_20:
     520                 :          0 :                 return CHAN_WIDTH_20;
     521                 :            :         case NL80211_CHAN_WIDTH_40:
     522                 :          0 :                 return CHAN_WIDTH_40;
     523                 :            :         case NL80211_CHAN_WIDTH_80:
     524                 :          0 :                 return CHAN_WIDTH_80;
     525                 :            :         case NL80211_CHAN_WIDTH_80P80:
     526                 :          0 :                 return CHAN_WIDTH_80P80;
     527                 :            :         case NL80211_CHAN_WIDTH_160:
     528                 :          0 :                 return CHAN_WIDTH_160;
     529                 :            :         }
     530                 :          0 :         return CHAN_WIDTH_UNKNOWN;
     531                 :            : }
     532                 :            : 
     533                 :            : 
     534                 :      11983 : static int is_ap_interface(enum nl80211_iftype nlmode)
     535                 :            : {
     536 [ +  + ][ +  + ]:      11983 :         return (nlmode == NL80211_IFTYPE_AP ||
     537                 :            :                 nlmode == NL80211_IFTYPE_P2P_GO);
     538                 :            : }
     539                 :            : 
     540                 :            : 
     541                 :       4415 : static int is_sta_interface(enum nl80211_iftype nlmode)
     542                 :            : {
     543 [ +  + ][ -  + ]:       4415 :         return (nlmode == NL80211_IFTYPE_STATION ||
     544                 :            :                 nlmode == NL80211_IFTYPE_P2P_CLIENT);
     545                 :            : }
     546                 :            : 
     547                 :            : 
     548                 :       1114 : static int is_p2p_net_interface(enum nl80211_iftype nlmode)
     549                 :            : {
     550 [ +  + ][ +  + ]:       1114 :         return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
     551                 :            :                 nlmode == NL80211_IFTYPE_P2P_GO);
     552                 :            : }
     553                 :            : 
     554                 :            : 
     555                 :       2647 : static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
     556                 :            : {
     557         [ +  + ]:       2647 :         if (drv->associated)
     558                 :        679 :                 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
     559                 :       2647 :         drv->associated = 0;
     560                 :       2647 :         os_memset(drv->bssid, 0, ETH_ALEN);
     561                 :       2647 : }
     562                 :            : 
     563                 :            : 
     564                 :            : struct nl80211_bss_info_arg {
     565                 :            :         struct wpa_driver_nl80211_data *drv;
     566                 :            :         struct wpa_scan_results *res;
     567                 :            :         unsigned int assoc_freq;
     568                 :            :         u8 assoc_bssid[ETH_ALEN];
     569                 :            : };
     570                 :            : 
     571                 :            : static int bss_info_handler(struct nl_msg *msg, void *arg);
     572                 :            : 
     573                 :            : 
     574                 :            : /* nl80211 code */
     575                 :      40955 : static int ack_handler(struct nl_msg *msg, void *arg)
     576                 :            : {
     577                 :      40955 :         int *err = arg;
     578                 :      40955 :         *err = 0;
     579                 :      40955 :         return NL_STOP;
     580                 :            : }
     581                 :            : 
     582                 :       5253 : static int finish_handler(struct nl_msg *msg, void *arg)
     583                 :            : {
     584                 :       5253 :         int *ret = arg;
     585                 :       5253 :         *ret = 0;
     586                 :       5253 :         return NL_SKIP;
     587                 :            : }
     588                 :            : 
     589                 :       9144 : static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
     590                 :            :                          void *arg)
     591                 :            : {
     592                 :       9144 :         int *ret = arg;
     593                 :       9144 :         *ret = err->error;
     594                 :       9144 :         return NL_SKIP;
     595                 :            : }
     596                 :            : 
     597                 :            : 
     598                 :     281756 : static int no_seq_check(struct nl_msg *msg, void *arg)
     599                 :            : {
     600                 :     281756 :         return NL_OK;
     601                 :            : }
     602                 :            : 
     603                 :            : 
     604                 :      55352 : static int send_and_recv(struct nl80211_global *global,
     605                 :            :                          struct nl_handle *nl_handle, struct nl_msg *msg,
     606                 :            :                          int (*valid_handler)(struct nl_msg *, void *),
     607                 :            :                          void *valid_data)
     608                 :            : {
     609                 :            :         struct nl_cb *cb;
     610                 :      55352 :         int err = -ENOMEM;
     611                 :            : 
     612                 :      55352 :         cb = nl_cb_clone(global->nl_cb);
     613         [ -  + ]:      55352 :         if (!cb)
     614                 :          0 :                 goto out;
     615                 :            : 
     616                 :      55352 :         err = nl_send_auto_complete(nl_handle, msg);
     617         [ -  + ]:      55352 :         if (err < 0)
     618                 :          0 :                 goto out;
     619                 :            : 
     620                 :      55352 :         err = 1;
     621                 :            : 
     622                 :      55352 :         nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
     623                 :      55352 :         nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
     624                 :      55352 :         nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
     625                 :            : 
     626         [ +  + ]:      55352 :         if (valid_handler)
     627                 :      17557 :                 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
     628                 :            :                           valid_handler, valid_data);
     629                 :            : 
     630         [ +  + ]:     121329 :         while (err > 0) {
     631                 :      65977 :                 int res = nl_recvmsgs(nl_handle, cb);
     632         [ -  + ]:      65977 :                 if (res) {
     633                 :          0 :                         wpa_printf(MSG_INFO,
     634                 :            :                                    "nl80211: %s->nl_recvmsgs failed: %d",
     635                 :            :                                    __func__, res);
     636                 :            :                 }
     637                 :            :         }
     638                 :            :  out:
     639                 :      55352 :         nl_cb_put(cb);
     640                 :      55352 :         nlmsg_free(msg);
     641                 :      55352 :         return err;
     642                 :            : }
     643                 :            : 
     644                 :            : 
     645                 :         20 : static int send_and_recv_msgs_global(struct nl80211_global *global,
     646                 :            :                                      struct nl_msg *msg,
     647                 :            :                                      int (*valid_handler)(struct nl_msg *, void *),
     648                 :            :                                      void *valid_data)
     649                 :            : {
     650                 :         20 :         return send_and_recv(global, global->nl, msg, valid_handler,
     651                 :            :                              valid_data);
     652                 :            : }
     653                 :            : 
     654                 :            : 
     655                 :      46009 : static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
     656                 :            :                               struct nl_msg *msg,
     657                 :            :                               int (*valid_handler)(struct nl_msg *, void *),
     658                 :            :                               void *valid_data)
     659                 :            : {
     660                 :      46009 :         return send_and_recv(drv->global, drv->global->nl, msg,
     661                 :            :                              valid_handler, valid_data);
     662                 :            : }
     663                 :            : 
     664                 :            : 
     665                 :            : struct family_data {
     666                 :            :         const char *group;
     667                 :            :         int id;
     668                 :            : };
     669                 :            : 
     670                 :            : 
     671                 :      21317 : static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
     672                 :            : {
     673         [ -  + ]:      21317 :         if (bss->wdev_id_set)
     674         [ #  # ]:          0 :                 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
     675                 :            :         else
     676         [ +  - ]:      21317 :                 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
     677                 :      21317 :         return 0;
     678                 :            : 
     679                 :            : nla_put_failure:
     680                 :      21317 :         return -1;
     681                 :            : }
     682                 :            : 
     683                 :            : 
     684                 :         20 : static int family_handler(struct nl_msg *msg, void *arg)
     685                 :            : {
     686                 :         20 :         struct family_data *res = arg;
     687                 :            :         struct nlattr *tb[CTRL_ATTR_MAX + 1];
     688                 :         20 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
     689                 :            :         struct nlattr *mcgrp;
     690                 :            :         int i;
     691                 :            : 
     692                 :         20 :         nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
     693                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
     694         [ -  + ]:         20 :         if (!tb[CTRL_ATTR_MCAST_GROUPS])
     695                 :          0 :                 return NL_SKIP;
     696                 :            : 
     697         [ +  - ]:         70 :         nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
     698                 :            :                 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
     699                 :         70 :                 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
     700                 :            :                           nla_len(mcgrp), NULL);
     701 [ +  - ][ +  - ]:         70 :                 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
     702         [ +  + ]:         70 :                     !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
     703                 :         70 :                     os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
     704                 :            :                                res->group,
     705                 :            :                                nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
     706                 :         50 :                         continue;
     707                 :         20 :                 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
     708                 :         20 :                 break;
     709                 :            :         };
     710                 :            : 
     711                 :         20 :         return NL_SKIP;
     712                 :            : }
     713                 :            : 
     714                 :            : 
     715                 :         20 : static int nl_get_multicast_id(struct nl80211_global *global,
     716                 :            :                                const char *family, const char *group)
     717                 :            : {
     718                 :            :         struct nl_msg *msg;
     719                 :         20 :         int ret = -1;
     720                 :         20 :         struct family_data res = { group, -ENOENT };
     721                 :            : 
     722                 :         20 :         msg = nlmsg_alloc();
     723         [ -  + ]:         20 :         if (!msg)
     724                 :          0 :                 return -ENOMEM;
     725                 :         20 :         genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
     726                 :            :                     0, 0, CTRL_CMD_GETFAMILY, 0);
     727         [ +  - ]:         20 :         NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
     728                 :            : 
     729                 :         20 :         ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
     730                 :         20 :         msg = NULL;
     731         [ +  - ]:         20 :         if (ret == 0)
     732                 :         20 :                 ret = res.id;
     733                 :            : 
     734                 :            : nla_put_failure:
     735                 :         20 :         nlmsg_free(msg);
     736                 :         20 :         return ret;
     737                 :            : }
     738                 :            : 
     739                 :            : 
     740                 :      55332 : static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
     741                 :            :                           struct nl_msg *msg, int flags, uint8_t cmd)
     742                 :            : {
     743                 :      55332 :         return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
     744                 :            :                            0, flags, cmd, 0);
     745                 :            : }
     746                 :            : 
     747                 :            : 
     748                 :            : struct wiphy_idx_data {
     749                 :            :         int wiphy_idx;
     750                 :            :         enum nl80211_iftype nlmode;
     751                 :            :         u8 *macaddr;
     752                 :            : };
     753                 :            : 
     754                 :            : 
     755                 :       1210 : static int netdev_info_handler(struct nl_msg *msg, void *arg)
     756                 :            : {
     757                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
     758                 :       1210 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
     759                 :       1210 :         struct wiphy_idx_data *info = arg;
     760                 :            : 
     761                 :       1210 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
     762                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
     763                 :            : 
     764         [ +  - ]:       1210 :         if (tb[NL80211_ATTR_WIPHY])
     765                 :       1210 :                 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
     766                 :            : 
     767         [ +  - ]:       1210 :         if (tb[NL80211_ATTR_IFTYPE])
     768                 :       1210 :                 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
     769                 :            : 
     770 [ +  - ][ -  + ]:       1210 :         if (tb[NL80211_ATTR_MAC] && info->macaddr)
     771                 :          0 :                 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
     772                 :            :                           ETH_ALEN);
     773                 :            : 
     774                 :       1210 :         return NL_SKIP;
     775                 :            : }
     776                 :            : 
     777                 :            : 
     778                 :        444 : static int nl80211_get_wiphy_index(struct i802_bss *bss)
     779                 :            : {
     780                 :            :         struct nl_msg *msg;
     781                 :        444 :         struct wiphy_idx_data data = {
     782                 :            :                 .wiphy_idx = -1,
     783                 :            :                 .macaddr = NULL,
     784                 :            :         };
     785                 :            : 
     786                 :        444 :         msg = nlmsg_alloc();
     787         [ -  + ]:        444 :         if (!msg)
     788                 :          0 :                 return NL80211_IFTYPE_UNSPECIFIED;
     789                 :            : 
     790                 :        444 :         nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
     791                 :            : 
     792         [ -  + ]:        444 :         if (nl80211_set_iface_id(msg, bss) < 0)
     793                 :          0 :                 goto nla_put_failure;
     794                 :            : 
     795         [ +  - ]:        444 :         if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
     796                 :        444 :                 return data.wiphy_idx;
     797                 :          0 :         msg = NULL;
     798                 :            : nla_put_failure:
     799                 :          0 :         nlmsg_free(msg);
     800                 :        444 :         return -1;
     801                 :            : }
     802                 :            : 
     803                 :            : 
     804                 :        766 : static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
     805                 :            : {
     806                 :            :         struct nl_msg *msg;
     807                 :        766 :         struct wiphy_idx_data data = {
     808                 :            :                 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
     809                 :            :                 .macaddr = NULL,
     810                 :            :         };
     811                 :            : 
     812                 :        766 :         msg = nlmsg_alloc();
     813         [ -  + ]:        766 :         if (!msg)
     814                 :          0 :                 return -1;
     815                 :            : 
     816                 :        766 :         nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
     817                 :            : 
     818         [ -  + ]:        766 :         if (nl80211_set_iface_id(msg, bss) < 0)
     819                 :          0 :                 goto nla_put_failure;
     820                 :            : 
     821         [ +  - ]:        766 :         if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
     822                 :        766 :                 return data.nlmode;
     823                 :          0 :         msg = NULL;
     824                 :            : nla_put_failure:
     825                 :          0 :         nlmsg_free(msg);
     826                 :        766 :         return NL80211_IFTYPE_UNSPECIFIED;
     827                 :            : }
     828                 :            : 
     829                 :            : 
     830                 :          0 : static int nl80211_get_macaddr(struct i802_bss *bss)
     831                 :            : {
     832                 :            :         struct nl_msg *msg;
     833                 :          0 :         struct wiphy_idx_data data = {
     834                 :          0 :                 .macaddr = bss->addr,
     835                 :            :         };
     836                 :            : 
     837                 :          0 :         msg = nlmsg_alloc();
     838         [ #  # ]:          0 :         if (!msg)
     839                 :          0 :                 return NL80211_IFTYPE_UNSPECIFIED;
     840                 :            : 
     841                 :          0 :         nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
     842         [ #  # ]:          0 :         if (nl80211_set_iface_id(msg, bss) < 0)
     843                 :          0 :                 goto nla_put_failure;
     844                 :            : 
     845                 :          0 :         return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
     846                 :            : 
     847                 :            : nla_put_failure:
     848                 :          0 :         nlmsg_free(msg);
     849                 :          0 :         return NL80211_IFTYPE_UNSPECIFIED;
     850                 :            : }
     851                 :            : 
     852                 :            : 
     853                 :        427 : static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
     854                 :            :                                     struct nl80211_wiphy_data *w)
     855                 :            : {
     856                 :            :         struct nl_msg *msg;
     857                 :        427 :         int ret = -1;
     858                 :            : 
     859                 :        427 :         msg = nlmsg_alloc();
     860         [ -  + ]:        427 :         if (!msg)
     861                 :          0 :                 return -1;
     862                 :            : 
     863                 :        427 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
     864                 :            : 
     865         [ +  - ]:        427 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
     866                 :            : 
     867                 :        427 :         ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
     868                 :        427 :         msg = NULL;
     869         [ -  + ]:        427 :         if (ret) {
     870                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
     871                 :            :                            "failed: ret=%d (%s)",
     872                 :            :                            ret, strerror(-ret));
     873                 :          0 :                 goto nla_put_failure;
     874                 :            :         }
     875                 :        427 :         ret = 0;
     876                 :            : nla_put_failure:
     877                 :        427 :         nlmsg_free(msg);
     878                 :        427 :         return ret;
     879                 :            : }
     880                 :            : 
     881                 :            : 
     882                 :       1350 : static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
     883                 :            : {
     884                 :       1350 :         struct nl80211_wiphy_data *w = eloop_ctx;
     885                 :            :         int res;
     886                 :            : 
     887                 :       1350 :         wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
     888                 :            : 
     889                 :       1350 :         res = nl_recvmsgs(handle, w->nl_cb);
     890         [ -  + ]:       1350 :         if (res) {
     891                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
     892                 :            :                            __func__, res);
     893                 :            :         }
     894                 :       1350 : }
     895                 :            : 
     896                 :            : 
     897                 :       1349 : static int process_beacon_event(struct nl_msg *msg, void *arg)
     898                 :            : {
     899                 :       1349 :         struct nl80211_wiphy_data *w = arg;
     900                 :            :         struct wpa_driver_nl80211_data *drv;
     901                 :       1349 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
     902                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
     903                 :            :         union wpa_event_data event;
     904                 :            : 
     905                 :       1349 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
     906                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
     907                 :            : 
     908         [ -  + ]:       1349 :         if (gnlh->cmd != NL80211_CMD_FRAME) {
     909                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
     910                 :          0 :                            gnlh->cmd);
     911                 :          0 :                 return NL_SKIP;
     912                 :            :         }
     913                 :            : 
     914         [ -  + ]:       1349 :         if (!tb[NL80211_ATTR_FRAME])
     915                 :          0 :                 return NL_SKIP;
     916                 :            : 
     917         [ +  + ]:       2698 :         dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
     918                 :            :                          wiphy_list) {
     919                 :       1349 :                 os_memset(&event, 0, sizeof(event));
     920                 :       1349 :                 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
     921                 :       1349 :                 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
     922                 :       1349 :                 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
     923                 :            :         }
     924                 :            : 
     925                 :       1349 :         return NL_SKIP;
     926                 :            : }
     927                 :            : 
     928                 :            : 
     929                 :            : static struct nl80211_wiphy_data *
     930                 :        444 : nl80211_get_wiphy_data_ap(struct i802_bss *bss)
     931                 :            : {
     932                 :            :         static DEFINE_DL_LIST(nl80211_wiphys);
     933                 :            :         struct nl80211_wiphy_data *w;
     934                 :        444 :         int wiphy_idx, found = 0;
     935                 :            :         struct i802_bss *tmp_bss;
     936                 :            : 
     937         [ -  + ]:        444 :         if (bss->wiphy_data != NULL)
     938                 :          0 :                 return bss->wiphy_data;
     939                 :            : 
     940                 :        444 :         wiphy_idx = nl80211_get_wiphy_index(bss);
     941                 :            : 
     942         [ +  + ]:        484 :         dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
     943         [ +  + ]:         57 :                 if (w->wiphy_idx == wiphy_idx)
     944                 :         17 :                         goto add;
     945                 :            :         }
     946                 :            : 
     947                 :            :         /* alloc new one */
     948                 :        427 :         w = os_zalloc(sizeof(*w));
     949         [ -  + ]:        427 :         if (w == NULL)
     950                 :          0 :                 return NULL;
     951                 :        427 :         w->wiphy_idx = wiphy_idx;
     952                 :        427 :         dl_list_init(&w->bsss);
     953                 :        427 :         dl_list_init(&w->drvs);
     954                 :            : 
     955                 :        427 :         w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
     956         [ -  + ]:        427 :         if (!w->nl_cb) {
     957                 :          0 :                 os_free(w);
     958                 :          0 :                 return NULL;
     959                 :            :         }
     960                 :        427 :         nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
     961                 :        427 :         nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
     962                 :            :                   w);
     963                 :            : 
     964                 :        427 :         w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
     965                 :            :                                          "wiphy beacons");
     966         [ -  + ]:        427 :         if (w->nl_beacons == NULL) {
     967                 :          0 :                 os_free(w);
     968                 :          0 :                 return NULL;
     969                 :            :         }
     970                 :            : 
     971         [ -  + ]:        427 :         if (nl80211_register_beacons(bss->drv, w)) {
     972                 :          0 :                 nl_destroy_handles(&w->nl_beacons);
     973                 :          0 :                 os_free(w);
     974                 :          0 :                 return NULL;
     975                 :            :         }
     976                 :            : 
     977                 :        427 :         nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
     978                 :            : 
     979                 :        427 :         dl_list_add(&nl80211_wiphys, &w->list);
     980                 :            : 
     981                 :            : add:
     982                 :            :         /* drv entry for this bss already there? */
     983         [ +  + ]:        444 :         dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
     984         [ +  - ]:         17 :                 if (tmp_bss->drv == bss->drv) {
     985                 :         17 :                         found = 1;
     986                 :         17 :                         break;
     987                 :            :                 }
     988                 :            :         }
     989                 :            :         /* if not add it */
     990         [ +  + ]:        444 :         if (!found)
     991                 :        427 :                 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
     992                 :            : 
     993                 :        444 :         dl_list_add(&w->bsss, &bss->wiphy_list);
     994                 :        444 :         bss->wiphy_data = w;
     995                 :        444 :         return w;
     996                 :            : }
     997                 :            : 
     998                 :            : 
     999                 :        715 : static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
    1000                 :            : {
    1001                 :        715 :         struct nl80211_wiphy_data *w = bss->wiphy_data;
    1002                 :            :         struct i802_bss *tmp_bss;
    1003                 :        715 :         int found = 0;
    1004                 :            : 
    1005         [ +  + ]:        715 :         if (w == NULL)
    1006                 :        271 :                 return;
    1007                 :        444 :         bss->wiphy_data = NULL;
    1008                 :        444 :         dl_list_del(&bss->wiphy_list);
    1009                 :            : 
    1010                 :            :         /* still any for this drv present? */
    1011         [ +  + ]:        444 :         dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
    1012         [ +  - ]:         17 :                 if (tmp_bss->drv == bss->drv) {
    1013                 :         17 :                         found = 1;
    1014                 :         17 :                         break;
    1015                 :            :                 }
    1016                 :            :         }
    1017                 :            :         /* if not remove it */
    1018         [ +  + ]:        444 :         if (!found)
    1019                 :        427 :                 dl_list_del(&bss->drv->wiphy_list);
    1020                 :            : 
    1021         [ +  + ]:        444 :         if (!dl_list_empty(&w->bsss))
    1022                 :         17 :                 return;
    1023                 :            : 
    1024                 :        427 :         nl80211_destroy_eloop_handle(&w->nl_beacons);
    1025                 :            : 
    1026                 :        427 :         nl_cb_put(w->nl_cb);
    1027                 :        427 :         dl_list_del(&w->list);
    1028                 :        715 :         os_free(w);
    1029                 :            : }
    1030                 :            : 
    1031                 :            : 
    1032                 :       1141 : static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
    1033                 :            : {
    1034                 :       1141 :         struct i802_bss *bss = priv;
    1035                 :       1141 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    1036         [ -  + ]:       1141 :         if (!drv->associated)
    1037                 :          0 :                 return -1;
    1038                 :       1141 :         os_memcpy(bssid, drv->bssid, ETH_ALEN);
    1039                 :       1141 :         return 0;
    1040                 :            : }
    1041                 :            : 
    1042                 :            : 
    1043                 :        433 : static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
    1044                 :            : {
    1045                 :        433 :         struct i802_bss *bss = priv;
    1046                 :        433 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    1047         [ -  + ]:        433 :         if (!drv->associated)
    1048                 :          0 :                 return -1;
    1049                 :        433 :         os_memcpy(ssid, drv->ssid, drv->ssid_len);
    1050                 :        433 :         return drv->ssid_len;
    1051                 :            : }
    1052                 :            : 
    1053                 :            : 
    1054                 :       2982 : static void wpa_driver_nl80211_event_newlink(
    1055                 :            :         struct wpa_driver_nl80211_data *drv, char *ifname)
    1056                 :            : {
    1057                 :            :         union wpa_event_data event;
    1058                 :            : 
    1059         [ +  + ]:       2982 :         if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
    1060         [ -  + ]:       2945 :                 if (if_nametoindex(drv->first_bss->ifname) == 0) {
    1061                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
    1062                 :          0 :                                    drv->first_bss->ifname);
    1063                 :          0 :                         return;
    1064                 :            :                 }
    1065         [ +  - ]:       2945 :                 if (!drv->if_removed)
    1066                 :       2945 :                         return;
    1067                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
    1068                 :          0 :                            drv->first_bss->ifname);
    1069                 :          0 :                 drv->if_removed = 0;
    1070                 :            :         }
    1071                 :            : 
    1072                 :         37 :         os_memset(&event, 0, sizeof(event));
    1073                 :         37 :         os_strlcpy(event.interface_status.ifname, ifname,
    1074                 :            :                    sizeof(event.interface_status.ifname));
    1075                 :         37 :         event.interface_status.ievent = EVENT_INTERFACE_ADDED;
    1076                 :       2982 :         wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
    1077                 :            : }
    1078                 :            : 
    1079                 :            : 
    1080                 :          0 : static void wpa_driver_nl80211_event_dellink(
    1081                 :            :         struct wpa_driver_nl80211_data *drv, char *ifname)
    1082                 :            : {
    1083                 :            :         union wpa_event_data event;
    1084                 :            : 
    1085         [ #  # ]:          0 :         if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
    1086         [ #  # ]:          0 :                 if (drv->if_removed) {
    1087                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
    1088                 :            :                                    ifname);
    1089                 :          0 :                         return;
    1090                 :            :                 }
    1091                 :          0 :                 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
    1092                 :            :                            ifname);
    1093                 :          0 :                 drv->if_removed = 1;
    1094                 :            :         } else {
    1095                 :          0 :                 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
    1096                 :            :                            ifname);
    1097                 :            :         }
    1098                 :            : 
    1099                 :          0 :         os_memset(&event, 0, sizeof(event));
    1100                 :          0 :         os_strlcpy(event.interface_status.ifname, ifname,
    1101                 :            :                    sizeof(event.interface_status.ifname));
    1102                 :          0 :         event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
    1103                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
    1104                 :            : }
    1105                 :            : 
    1106                 :            : 
    1107                 :          0 : static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
    1108                 :            :                                          u8 *buf, size_t len)
    1109                 :            : {
    1110                 :            :         int attrlen, rta_len;
    1111                 :            :         struct rtattr *attr;
    1112                 :            : 
    1113                 :          0 :         attrlen = len;
    1114                 :          0 :         attr = (struct rtattr *) buf;
    1115                 :            : 
    1116                 :          0 :         rta_len = RTA_ALIGN(sizeof(struct rtattr));
    1117 [ #  # ][ #  # ]:          0 :         while (RTA_OK(attr, attrlen)) {
                 [ #  # ]
    1118         [ #  # ]:          0 :                 if (attr->rta_type == IFLA_IFNAME) {
    1119         [ #  # ]:          0 :                         if (os_strcmp(((char *) attr) + rta_len,
    1120                 :            :                                       drv->first_bss->ifname) == 0)
    1121                 :          0 :                                 return 1;
    1122                 :            :                         else
    1123                 :          0 :                                 break;
    1124                 :            :                 }
    1125                 :          0 :                 attr = RTA_NEXT(attr, attrlen);
    1126                 :            :         }
    1127                 :            : 
    1128                 :          0 :         return 0;
    1129                 :            : }
    1130                 :            : 
    1131                 :            : 
    1132                 :      13350 : static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
    1133                 :            :                                           int ifindex, u8 *buf, size_t len)
    1134                 :            : {
    1135         [ +  + ]:      13350 :         if (drv->ifindex == ifindex)
    1136                 :       2957 :                 return 1;
    1137                 :            : 
    1138 [ -  + ][ #  # ]:      10393 :         if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
    1139                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
    1140                 :            :                            "interface");
    1141                 :          0 :                 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0);
    1142                 :          0 :                 return 1;
    1143                 :            :         }
    1144                 :            : 
    1145                 :      13350 :         return 0;
    1146                 :            : }
    1147                 :            : 
    1148                 :            : 
    1149                 :            : static struct wpa_driver_nl80211_data *
    1150                 :      17430 : nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
    1151                 :            : {
    1152                 :            :         struct wpa_driver_nl80211_data *drv;
    1153         [ +  + ]:      27732 :         dl_list_for_each(drv, &global->interfaces,
    1154                 :            :                          struct wpa_driver_nl80211_data, list) {
    1155   [ +  +  +  + ]:      23743 :                 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
    1156                 :      10393 :                     have_ifidx(drv, idx))
    1157                 :       3048 :                         return drv;
    1158                 :            :         }
    1159                 :      17430 :         return NULL;
    1160                 :            : }
    1161                 :            : 
    1162                 :            : 
    1163                 :      17224 : static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
    1164                 :            :                                                  struct ifinfomsg *ifi,
    1165                 :            :                                                  u8 *buf, size_t len)
    1166                 :            : {
    1167                 :      17224 :         struct nl80211_global *global = ctx;
    1168                 :            :         struct wpa_driver_nl80211_data *drv;
    1169                 :            :         int attrlen;
    1170                 :            :         struct rtattr *attr;
    1171                 :      17224 :         u32 brid = 0;
    1172                 :            :         char namebuf[IFNAMSIZ];
    1173                 :            :         char ifname[IFNAMSIZ + 1];
    1174                 :            :         char extra[100], *pos, *end;
    1175                 :            : 
    1176                 :      17224 :         drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
    1177         [ +  + ]:      17224 :         if (!drv) {
    1178                 :      14176 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
    1179                 :            :                            ifi->ifi_index);
    1180                 :      14176 :                 return;
    1181                 :            :         }
    1182                 :            : 
    1183                 :       3048 :         extra[0] = '\0';
    1184                 :       3048 :         pos = extra;
    1185                 :       3048 :         end = pos + sizeof(extra);
    1186                 :       3048 :         ifname[0] = '\0';
    1187                 :            : 
    1188                 :       3048 :         attrlen = len;
    1189                 :       3048 :         attr = (struct rtattr *) buf;
    1190 [ +  + ][ +  - ]:      54864 :         while (RTA_OK(attr, attrlen)) {
                 [ +  - ]
    1191   [ +  -  -  +  :      51816 :                 switch (attr->rta_type) {
                   +  + ]
    1192                 :            :                 case IFLA_IFNAME:
    1193         [ -  + ]:       3048 :                         if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
    1194                 :          0 :                                 break;
    1195                 :       3048 :                         os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
    1196                 :       3048 :                         ifname[RTA_PAYLOAD(attr)] = '\0';
    1197                 :       3048 :                         break;
    1198                 :            :                 case IFLA_MASTER:
    1199                 :          0 :                         brid = nla_get_u32((struct nlattr *) attr);
    1200                 :          0 :                         pos += os_snprintf(pos, end - pos, " master=%u", brid);
    1201                 :          0 :                         break;
    1202                 :            :                 case IFLA_WIRELESS:
    1203                 :          0 :                         pos += os_snprintf(pos, end - pos, " wext");
    1204                 :          0 :                         break;
    1205                 :            :                 case IFLA_OPERSTATE:
    1206                 :       3048 :                         pos += os_snprintf(pos, end - pos, " operstate=%u",
    1207                 :            :                                            nla_get_u32((struct nlattr *) attr));
    1208                 :       3048 :                         break;
    1209                 :            :                 case IFLA_LINKMODE:
    1210                 :       3048 :                         pos += os_snprintf(pos, end - pos, " linkmode=%u",
    1211                 :            :                                            nla_get_u32((struct nlattr *) attr));
    1212                 :       3048 :                         break;
    1213                 :            :                 }
    1214                 :      51816 :                 attr = RTA_NEXT(attr, attrlen);
    1215                 :            :         }
    1216                 :       3048 :         extra[sizeof(extra) - 1] = '\0';
    1217                 :            : 
    1218 [ -  + ][ +  + ]:       3048 :         wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_flags=0x%x (%s%s%s%s)",
         [ +  + ][ +  + ]
    1219                 :            :                    ifi->ifi_index, ifname, extra, ifi->ifi_flags,
    1220                 :       3048 :                    (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
    1221                 :       3048 :                    (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
    1222                 :       3048 :                    (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
    1223                 :       3048 :                    (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
    1224                 :            : 
    1225 [ +  + ][ +  - ]:       3048 :         if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
    1226   [ +  -  +  - ]:        132 :                 if (if_indextoname(ifi->ifi_index, namebuf) &&
    1227                 :         66 :                     linux_iface_up(drv->global->ioctl_sock,
    1228                 :         66 :                                    drv->first_bss->ifname) > 0) {
    1229                 :         66 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
    1230                 :            :                                    "event since interface %s is up", namebuf);
    1231                 :         66 :                         return;
    1232                 :            :                 }
    1233                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
    1234         [ #  # ]:          0 :                 if (drv->ignore_if_down_event) {
    1235                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
    1236                 :            :                                    "event generated by mode change");
    1237                 :          0 :                         drv->ignore_if_down_event = 0;
    1238                 :            :                 } else {
    1239                 :          0 :                         drv->if_disabled = 1;
    1240                 :          0 :                         wpa_supplicant_event(drv->ctx,
    1241                 :            :                                              EVENT_INTERFACE_DISABLED, NULL);
    1242                 :            :                 }
    1243                 :            :         }
    1244                 :            : 
    1245 [ -  + ][ #  # ]:       2982 :         if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
    1246   [ #  #  #  # ]:          0 :                 if (if_indextoname(ifi->ifi_index, namebuf) &&
    1247                 :          0 :                     linux_iface_up(drv->global->ioctl_sock,
    1248                 :          0 :                                    drv->first_bss->ifname) == 0) {
    1249                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
    1250                 :            :                                    "event since interface %s is down",
    1251                 :            :                                    namebuf);
    1252         [ #  # ]:          0 :                 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
    1253                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
    1254                 :            :                                    "event since interface %s does not exist",
    1255                 :          0 :                                    drv->first_bss->ifname);
    1256         [ #  # ]:          0 :                 } else if (drv->if_removed) {
    1257                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
    1258                 :            :                                    "event since interface %s is marked "
    1259                 :          0 :                                    "removed", drv->first_bss->ifname);
    1260                 :            :                 } else {
    1261                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Interface up");
    1262                 :          0 :                         drv->if_disabled = 0;
    1263                 :          0 :                         wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
    1264                 :            :                                              NULL);
    1265                 :            :                 }
    1266                 :            :         }
    1267                 :            : 
    1268                 :            :         /*
    1269                 :            :          * Some drivers send the association event before the operup event--in
    1270                 :            :          * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
    1271                 :            :          * fails. This will hit us when wpa_supplicant does not need to do
    1272                 :            :          * IEEE 802.1X authentication
    1273                 :            :          */
    1274 [ +  + ][ +  + ]:       2982 :         if (drv->operstate == 1 &&
    1275         [ +  + ]:       1110 :             (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
    1276                 :       1110 :             !(ifi->ifi_flags & IFF_RUNNING)) {
    1277                 :        121 :                 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
    1278                 :        121 :                 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
    1279                 :            :                                        -1, IF_OPER_UP);
    1280                 :            :         }
    1281                 :            : 
    1282         [ +  - ]:       2982 :         if (ifname[0])
    1283                 :       2982 :                 wpa_driver_nl80211_event_newlink(drv, ifname);
    1284                 :            : 
    1285 [ -  + ][ #  # ]:       2982 :         if (ifi->ifi_family == AF_BRIDGE && brid) {
    1286                 :            :                 /* device has been added to bridge */
    1287                 :          0 :                 if_indextoname(brid, namebuf);
    1288                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
    1289                 :            :                            brid, namebuf);
    1290                 :      17224 :                 add_ifidx(drv, brid);
    1291                 :            :         }
    1292                 :            : }
    1293                 :            : 
    1294                 :            : 
    1295                 :        206 : static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
    1296                 :            :                                                  struct ifinfomsg *ifi,
    1297                 :            :                                                  u8 *buf, size_t len)
    1298                 :            : {
    1299                 :        206 :         struct nl80211_global *global = ctx;
    1300                 :            :         struct wpa_driver_nl80211_data *drv;
    1301                 :            :         int attrlen;
    1302                 :            :         struct rtattr *attr;
    1303                 :        206 :         u32 brid = 0;
    1304                 :            :         char ifname[IFNAMSIZ + 1];
    1305                 :            : 
    1306                 :        206 :         drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
    1307         [ +  - ]:        206 :         if (!drv) {
    1308                 :        206 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
    1309                 :            :                            ifi->ifi_index);
    1310                 :        206 :                 return;
    1311                 :            :         }
    1312                 :            : 
    1313                 :          0 :         ifname[0] = '\0';
    1314                 :            : 
    1315                 :          0 :         attrlen = len;
    1316                 :          0 :         attr = (struct rtattr *) buf;
    1317 [ #  # ][ #  # ]:          0 :         while (RTA_OK(attr, attrlen)) {
                 [ #  # ]
    1318      [ #  #  # ]:          0 :                 switch (attr->rta_type) {
    1319                 :            :                 case IFLA_IFNAME:
    1320         [ #  # ]:          0 :                         if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
    1321                 :          0 :                                 break;
    1322                 :          0 :                         os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
    1323                 :          0 :                         ifname[RTA_PAYLOAD(attr)] = '\0';
    1324                 :          0 :                         break;
    1325                 :            :                 case IFLA_MASTER:
    1326                 :          0 :                         brid = nla_get_u32((struct nlattr *) attr);
    1327                 :          0 :                         break;
    1328                 :            :                 }
    1329                 :          0 :                 attr = RTA_NEXT(attr, attrlen);
    1330                 :            :         }
    1331                 :            : 
    1332         [ #  # ]:          0 :         if (ifname[0])
    1333                 :          0 :                 wpa_driver_nl80211_event_dellink(drv, ifname);
    1334                 :            : 
    1335 [ #  # ][ #  # ]:          0 :         if (ifi->ifi_family == AF_BRIDGE && brid) {
    1336                 :            :                 /* device has been removed from bridge */
    1337                 :            :                 char namebuf[IFNAMSIZ];
    1338                 :          0 :                 if_indextoname(brid, namebuf);
    1339                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
    1340                 :            :                            "%s", brid, namebuf);
    1341                 :          0 :                 del_ifidx(drv, brid);
    1342                 :            :         }
    1343                 :            : }
    1344                 :            : 
    1345                 :            : 
    1346                 :        693 : static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
    1347                 :            :                             const u8 *frame, size_t len)
    1348                 :            : {
    1349                 :            :         const struct ieee80211_mgmt *mgmt;
    1350                 :            :         union wpa_event_data event;
    1351                 :            : 
    1352                 :        693 :         wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
    1353                 :        693 :         mgmt = (const struct ieee80211_mgmt *) frame;
    1354         [ -  + ]:        693 :         if (len < 24 + sizeof(mgmt->u.auth)) {
    1355                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
    1356                 :            :                            "frame");
    1357                 :        693 :                 return;
    1358                 :            :         }
    1359                 :            : 
    1360                 :        693 :         os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
    1361                 :        693 :         os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
    1362                 :        693 :         os_memset(&event, 0, sizeof(event));
    1363                 :        693 :         os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
    1364                 :        693 :         event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
    1365                 :        693 :         event.auth.auth_transaction =
    1366                 :        693 :                 le_to_host16(mgmt->u.auth.auth_transaction);
    1367                 :        693 :         event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
    1368         [ +  + ]:        693 :         if (len > 24 + sizeof(mgmt->u.auth)) {
    1369                 :         43 :                 event.auth.ies = mgmt->u.auth.variable;
    1370                 :         43 :                 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
    1371                 :            :         }
    1372                 :            : 
    1373                 :        693 :         wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
    1374                 :            : }
    1375                 :            : 
    1376                 :            : 
    1377                 :          4 : static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
    1378                 :            : {
    1379                 :            :         struct nl_msg *msg;
    1380                 :            :         int ret;
    1381                 :            :         struct nl80211_bss_info_arg arg;
    1382                 :            : 
    1383                 :          4 :         os_memset(&arg, 0, sizeof(arg));
    1384                 :          4 :         msg = nlmsg_alloc();
    1385         [ -  + ]:          4 :         if (!msg)
    1386                 :          0 :                 goto nla_put_failure;
    1387                 :            : 
    1388                 :          4 :         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
    1389         [ +  - ]:          4 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    1390                 :            : 
    1391                 :          4 :         arg.drv = drv;
    1392                 :          4 :         ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
    1393                 :          4 :         msg = NULL;
    1394         [ +  - ]:          4 :         if (ret == 0) {
    1395                 :          4 :                 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
    1396                 :            :                            "associated BSS from scan results: %u MHz",
    1397                 :            :                            arg.assoc_freq);
    1398         [ +  - ]:          4 :                 if (arg.assoc_freq)
    1399                 :          4 :                         drv->assoc_freq = arg.assoc_freq;
    1400                 :          4 :                 return drv->assoc_freq;
    1401                 :            :         }
    1402                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
    1403                 :            :                    "(%s)", ret, strerror(-ret));
    1404                 :            : nla_put_failure:
    1405                 :          0 :         nlmsg_free(msg);
    1406                 :          4 :         return drv->assoc_freq;
    1407                 :            : }
    1408                 :            : 
    1409                 :            : 
    1410                 :        674 : static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
    1411                 :            :                             const u8 *frame, size_t len)
    1412                 :            : {
    1413                 :            :         const struct ieee80211_mgmt *mgmt;
    1414                 :            :         union wpa_event_data event;
    1415                 :            :         u16 status;
    1416                 :            : 
    1417                 :        674 :         wpa_printf(MSG_DEBUG, "nl80211: Associate event");
    1418                 :        674 :         mgmt = (const struct ieee80211_mgmt *) frame;
    1419         [ -  + ]:        674 :         if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
    1420                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
    1421                 :            :                            "frame");
    1422                 :          0 :                 return;
    1423                 :            :         }
    1424                 :            : 
    1425                 :        674 :         status = le_to_host16(mgmt->u.assoc_resp.status_code);
    1426         [ -  + ]:        674 :         if (status != WLAN_STATUS_SUCCESS) {
    1427                 :          0 :                 os_memset(&event, 0, sizeof(event));
    1428                 :          0 :                 event.assoc_reject.bssid = mgmt->bssid;
    1429         [ #  # ]:          0 :                 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
    1430                 :          0 :                         event.assoc_reject.resp_ies =
    1431                 :          0 :                                 (u8 *) mgmt->u.assoc_resp.variable;
    1432                 :          0 :                         event.assoc_reject.resp_ies_len =
    1433                 :          0 :                                 len - 24 - sizeof(mgmt->u.assoc_resp);
    1434                 :            :                 }
    1435                 :          0 :                 event.assoc_reject.status_code = status;
    1436                 :            : 
    1437                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
    1438                 :          0 :                 return;
    1439                 :            :         }
    1440                 :            : 
    1441                 :        674 :         drv->associated = 1;
    1442                 :        674 :         os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
    1443                 :        674 :         os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
    1444                 :            : 
    1445                 :        674 :         os_memset(&event, 0, sizeof(event));
    1446         [ +  - ]:        674 :         if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
    1447                 :        674 :                 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
    1448                 :        674 :                 event.assoc_info.resp_ies_len =
    1449                 :        674 :                         len - 24 - sizeof(mgmt->u.assoc_resp);
    1450                 :            :         }
    1451                 :            : 
    1452                 :        674 :         event.assoc_info.freq = drv->assoc_freq;
    1453                 :            : 
    1454                 :        674 :         wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
    1455                 :            : }
    1456                 :            : 
    1457                 :            : 
    1458                 :        674 : static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
    1459                 :            :                                enum nl80211_commands cmd, struct nlattr *status,
    1460                 :            :                                struct nlattr *addr, struct nlattr *req_ie,
    1461                 :            :                                struct nlattr *resp_ie)
    1462                 :            : {
    1463                 :            :         union wpa_event_data event;
    1464                 :            : 
    1465         [ +  + ]:        674 :         if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
    1466                 :            :                 /*
    1467                 :            :                  * Avoid reporting two association events that would confuse
    1468                 :            :                  * the core code.
    1469                 :            :                  */
    1470                 :        670 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
    1471                 :            :                            "when using userspace SME", cmd);
    1472                 :        670 :                 return;
    1473                 :            :         }
    1474                 :            : 
    1475         [ +  - ]:          4 :         if (cmd == NL80211_CMD_CONNECT)
    1476                 :          4 :                 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
    1477         [ #  # ]:          0 :         else if (cmd == NL80211_CMD_ROAM)
    1478                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
    1479                 :            : 
    1480                 :          4 :         os_memset(&event, 0, sizeof(event));
    1481   [ +  -  -  + ]:          8 :         if (cmd == NL80211_CMD_CONNECT &&
    1482                 :          4 :             nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
    1483         [ #  # ]:          0 :                 if (addr)
    1484                 :          0 :                         event.assoc_reject.bssid = nla_data(addr);
    1485         [ #  # ]:          0 :                 if (resp_ie) {
    1486                 :          0 :                         event.assoc_reject.resp_ies = nla_data(resp_ie);
    1487                 :          0 :                         event.assoc_reject.resp_ies_len = nla_len(resp_ie);
    1488                 :            :                 }
    1489                 :          0 :                 event.assoc_reject.status_code = nla_get_u16(status);
    1490                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
    1491                 :          0 :                 return;
    1492                 :            :         }
    1493                 :            : 
    1494                 :          4 :         drv->associated = 1;
    1495         [ +  - ]:          4 :         if (addr) {
    1496                 :          4 :                 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
    1497                 :          4 :                 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
    1498                 :            :         }
    1499                 :            : 
    1500         [ -  + ]:          4 :         if (req_ie) {
    1501                 :          0 :                 event.assoc_info.req_ies = nla_data(req_ie);
    1502                 :          0 :                 event.assoc_info.req_ies_len = nla_len(req_ie);
    1503                 :            :         }
    1504         [ +  - ]:          4 :         if (resp_ie) {
    1505                 :          4 :                 event.assoc_info.resp_ies = nla_data(resp_ie);
    1506                 :          4 :                 event.assoc_info.resp_ies_len = nla_len(resp_ie);
    1507                 :            :         }
    1508                 :            : 
    1509                 :          4 :         event.assoc_info.freq = nl80211_get_assoc_freq(drv);
    1510                 :            : 
    1511                 :        674 :         wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
    1512                 :            : }
    1513                 :            : 
    1514                 :            : 
    1515                 :        661 : static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
    1516                 :            :                                   struct nlattr *reason, struct nlattr *addr,
    1517                 :            :                                   struct nlattr *by_ap)
    1518                 :            : {
    1519                 :            :         union wpa_event_data data;
    1520                 :        661 :         unsigned int locally_generated = by_ap == NULL;
    1521                 :            : 
    1522         [ +  + ]:        661 :         if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
    1523                 :            :                 /*
    1524                 :            :                  * Avoid reporting two disassociation events that could
    1525                 :            :                  * confuse the core code.
    1526                 :            :                  */
    1527                 :        658 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
    1528                 :            :                            "event when using userspace SME");
    1529                 :        658 :                 return;
    1530                 :            :         }
    1531                 :            : 
    1532         [ +  - ]:          3 :         if (drv->ignore_next_local_disconnect) {
    1533                 :          3 :                 drv->ignore_next_local_disconnect = 0;
    1534         [ +  - ]:          3 :                 if (locally_generated) {
    1535                 :          3 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
    1536                 :            :                                    "event triggered during reassociation");
    1537                 :          3 :                         return;
    1538                 :            :                 }
    1539                 :          0 :                 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
    1540                 :            :                            "disconnect but got another disconnect "
    1541                 :            :                            "event first");
    1542                 :            :         }
    1543                 :            : 
    1544                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
    1545                 :          0 :         nl80211_mark_disconnected(drv);
    1546                 :          0 :         os_memset(&data, 0, sizeof(data));
    1547         [ #  # ]:          0 :         if (reason)
    1548                 :          0 :                 data.deauth_info.reason_code = nla_get_u16(reason);
    1549                 :          0 :         data.deauth_info.locally_generated = by_ap == NULL;
    1550                 :        661 :         wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
    1551                 :            : }
    1552                 :            : 
    1553                 :            : 
    1554                 :          0 : static int calculate_chan_offset(int width, int freq, int cf1, int cf2)
    1555                 :            : {
    1556                 :          0 :         int freq1 = 0;
    1557                 :            : 
    1558   [ #  #  #  #  :          0 :         switch (convert2width(width)) {
                   #  # ]
    1559                 :            :         case CHAN_WIDTH_20_NOHT:
    1560                 :            :         case CHAN_WIDTH_20:
    1561                 :          0 :                 return 0;
    1562                 :            :         case CHAN_WIDTH_40:
    1563                 :          0 :                 freq1 = cf1 - 10;
    1564                 :          0 :                 break;
    1565                 :            :         case CHAN_WIDTH_80:
    1566                 :          0 :                 freq1 = cf1 - 30;
    1567                 :          0 :                 break;
    1568                 :            :         case CHAN_WIDTH_160:
    1569                 :          0 :                 freq1 = cf1 - 70;
    1570                 :          0 :                 break;
    1571                 :            :         case CHAN_WIDTH_UNKNOWN:
    1572                 :            :         case CHAN_WIDTH_80P80:
    1573                 :            :                 /* FIXME: implement this */
    1574                 :          0 :                 return 0;
    1575                 :            :         }
    1576                 :            : 
    1577         [ #  # ]:          0 :         return (abs(freq - freq1) / 20) % 2 == 0 ? 1 : -1;
    1578                 :            : }
    1579                 :            : 
    1580                 :            : 
    1581                 :          0 : static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
    1582                 :            :                                  struct nlattr *ifindex, struct nlattr *freq,
    1583                 :            :                                  struct nlattr *type, struct nlattr *bw,
    1584                 :            :                                  struct nlattr *cf1, struct nlattr *cf2)
    1585                 :            : {
    1586                 :            :         struct i802_bss *bss;
    1587                 :            :         union wpa_event_data data;
    1588                 :          0 :         int ht_enabled = 1;
    1589                 :          0 :         int chan_offset = 0;
    1590                 :            :         int ifidx;
    1591                 :            : 
    1592                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
    1593                 :            : 
    1594         [ #  # ]:          0 :         if (!freq)
    1595                 :          0 :                 return;
    1596                 :            : 
    1597                 :          0 :         ifidx = nla_get_u32(ifindex);
    1598         [ #  # ]:          0 :         for (bss = drv->first_bss; bss; bss = bss->next)
    1599         [ #  # ]:          0 :                 if (bss->ifindex == ifidx)
    1600                 :          0 :                         break;
    1601                 :            : 
    1602         [ #  # ]:          0 :         if (bss == NULL) {
    1603                 :          0 :                 wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
    1604                 :            :                            ifidx);
    1605                 :          0 :                 return;
    1606                 :            :         }
    1607                 :            : 
    1608         [ #  # ]:          0 :         if (type) {
    1609   [ #  #  #  #  :          0 :                 switch (nla_get_u32(type)) {
                      # ]
    1610                 :            :                 case NL80211_CHAN_NO_HT:
    1611                 :          0 :                         ht_enabled = 0;
    1612                 :          0 :                         break;
    1613                 :            :                 case NL80211_CHAN_HT20:
    1614                 :          0 :                         break;
    1615                 :            :                 case NL80211_CHAN_HT40PLUS:
    1616                 :          0 :                         chan_offset = 1;
    1617                 :          0 :                         break;
    1618                 :            :                 case NL80211_CHAN_HT40MINUS:
    1619                 :          0 :                         chan_offset = -1;
    1620                 :          0 :                         break;
    1621                 :            :                 }
    1622 [ #  # ][ #  # ]:          0 :         } else if (bw && cf1) {
    1623                 :            :                 /* This can happen for example with VHT80 ch switch */
    1624         [ #  # ]:          0 :                 chan_offset = calculate_chan_offset(nla_get_u32(bw),
    1625                 :          0 :                                                     nla_get_u32(freq),
    1626                 :          0 :                                                     nla_get_u32(cf1),
    1627                 :          0 :                                                     cf2 ? nla_get_u32(cf2) : 0);
    1628                 :            :         } else {
    1629                 :          0 :                 wpa_printf(MSG_WARNING, "nl80211: Unknown secondary channel information - following channel definition calculations may fail");
    1630                 :            :         }
    1631                 :            : 
    1632                 :          0 :         os_memset(&data, 0, sizeof(data));
    1633                 :          0 :         data.ch_switch.freq = nla_get_u32(freq);
    1634                 :          0 :         data.ch_switch.ht_enabled = ht_enabled;
    1635                 :          0 :         data.ch_switch.ch_offset = chan_offset;
    1636         [ #  # ]:          0 :         if (bw)
    1637                 :          0 :                 data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
    1638         [ #  # ]:          0 :         if (cf1)
    1639                 :          0 :                 data.ch_switch.cf1 = nla_get_u32(cf1);
    1640         [ #  # ]:          0 :         if (cf2)
    1641                 :          0 :                 data.ch_switch.cf2 = nla_get_u32(cf2);
    1642                 :            : 
    1643                 :          0 :         bss->freq = data.ch_switch.freq;
    1644                 :            : 
    1645                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
    1646                 :            : }
    1647                 :            : 
    1648                 :            : 
    1649                 :          3 : static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
    1650                 :            :                                enum nl80211_commands cmd, struct nlattr *addr)
    1651                 :            : {
    1652                 :            :         union wpa_event_data event;
    1653                 :            :         enum wpa_event_type ev;
    1654                 :            : 
    1655         [ -  + ]:          3 :         if (nla_len(addr) != ETH_ALEN)
    1656                 :          0 :                 return;
    1657                 :            : 
    1658                 :          3 :         wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
    1659                 :          3 :                    cmd, MAC2STR((u8 *) nla_data(addr)));
    1660                 :            : 
    1661         [ +  - ]:          3 :         if (cmd == NL80211_CMD_AUTHENTICATE)
    1662                 :          3 :                 ev = EVENT_AUTH_TIMED_OUT;
    1663         [ #  # ]:          0 :         else if (cmd == NL80211_CMD_ASSOCIATE)
    1664                 :          0 :                 ev = EVENT_ASSOC_TIMED_OUT;
    1665                 :            :         else
    1666                 :          0 :                 return;
    1667                 :            : 
    1668                 :          3 :         os_memset(&event, 0, sizeof(event));
    1669                 :          3 :         os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
    1670                 :          3 :         wpa_supplicant_event(drv->ctx, ev, &event);
    1671                 :            : }
    1672                 :            : 
    1673                 :            : 
    1674                 :       4406 : static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
    1675                 :            :                             struct nlattr *freq, struct nlattr *sig,
    1676                 :            :                             const u8 *frame, size_t len)
    1677                 :            : {
    1678                 :            :         const struct ieee80211_mgmt *mgmt;
    1679                 :            :         union wpa_event_data event;
    1680                 :            :         u16 fc, stype;
    1681                 :       4406 :         int ssi_signal = 0;
    1682                 :       4406 :         int rx_freq = 0;
    1683                 :            : 
    1684                 :       4406 :         wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
    1685                 :       4406 :         mgmt = (const struct ieee80211_mgmt *) frame;
    1686         [ -  + ]:       4406 :         if (len < 24) {
    1687                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
    1688                 :       4406 :                 return;
    1689                 :            :         }
    1690                 :            : 
    1691                 :       4406 :         fc = le_to_host16(mgmt->frame_control);
    1692                 :       4406 :         stype = WLAN_FC_GET_STYPE(fc);
    1693                 :            : 
    1694         [ +  - ]:       4406 :         if (sig)
    1695                 :       4406 :                 ssi_signal = (s32) nla_get_u32(sig);
    1696                 :            : 
    1697                 :       4406 :         os_memset(&event, 0, sizeof(event));
    1698         [ +  - ]:       4406 :         if (freq) {
    1699                 :       4406 :                 event.rx_mgmt.freq = nla_get_u32(freq);
    1700                 :       4406 :                 rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
    1701                 :            :         }
    1702                 :       4406 :         wpa_printf(MSG_DEBUG,
    1703                 :            :                    "nl80211: RX frame freq=%d ssi_signal=%d stype=%u len=%u",
    1704                 :            :                    rx_freq, ssi_signal, stype, (unsigned int) len);
    1705                 :       4406 :         event.rx_mgmt.frame = frame;
    1706                 :       4406 :         event.rx_mgmt.frame_len = len;
    1707                 :       4406 :         event.rx_mgmt.ssi_signal = ssi_signal;
    1708                 :       4406 :         wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
    1709                 :            : }
    1710                 :            : 
    1711                 :            : 
    1712                 :       3106 : static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
    1713                 :            :                                       struct nlattr *cookie, const u8 *frame,
    1714                 :            :                                       size_t len, struct nlattr *ack)
    1715                 :            : {
    1716                 :            :         union wpa_event_data event;
    1717                 :            :         const struct ieee80211_hdr *hdr;
    1718                 :            :         u16 fc;
    1719                 :            : 
    1720                 :       3106 :         wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
    1721         [ +  + ]:       3106 :         if (!is_ap_interface(drv->nlmode)) {
    1722                 :            :                 u64 cookie_val;
    1723                 :            : 
    1724         [ -  + ]:        930 :                 if (!cookie)
    1725                 :          0 :                         return;
    1726                 :            : 
    1727                 :        930 :                 cookie_val = nla_get_u64(cookie);
    1728         [ +  + ]:        930 :                 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
    1729                 :            :                            " cookie=0%llx%s (ack=%d)",
    1730                 :            :                            (long long unsigned int) cookie_val,
    1731                 :        930 :                            cookie_val == drv->send_action_cookie ?
    1732                 :            :                            " (match)" : " (unknown)", ack != NULL);
    1733         [ +  + ]:        930 :                 if (cookie_val != drv->send_action_cookie)
    1734                 :         74 :                         return;
    1735                 :            :         }
    1736                 :            : 
    1737                 :       3032 :         hdr = (const struct ieee80211_hdr *) frame;
    1738                 :       3032 :         fc = le_to_host16(hdr->frame_control);
    1739                 :            : 
    1740                 :       3032 :         os_memset(&event, 0, sizeof(event));
    1741                 :       3032 :         event.tx_status.type = WLAN_FC_GET_TYPE(fc);
    1742                 :       3032 :         event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
    1743                 :       3032 :         event.tx_status.dst = hdr->addr1;
    1744                 :       3032 :         event.tx_status.data = frame;
    1745                 :       3032 :         event.tx_status.data_len = len;
    1746                 :       3032 :         event.tx_status.ack = ack != NULL;
    1747                 :       3106 :         wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
    1748                 :            : }
    1749                 :            : 
    1750                 :            : 
    1751                 :        667 : static void mlme_event_deauth_disassoc(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                 :        667 :         const u8 *bssid = NULL;
    1758                 :        667 :         u16 reason_code = 0;
    1759                 :            : 
    1760         [ +  + ]:        667 :         if (type == EVENT_DEAUTH)
    1761                 :        666 :                 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
    1762                 :            :         else
    1763                 :          1 :                 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
    1764                 :            : 
    1765                 :        667 :         mgmt = (const struct ieee80211_mgmt *) frame;
    1766         [ +  - ]:        667 :         if (len >= 24) {
    1767                 :        667 :                 bssid = mgmt->bssid;
    1768                 :            : 
    1769 [ +  + ][ +  + ]:        667 :                 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
    1770         [ +  + ]:        571 :                     !drv->associated &&
    1771         [ +  + ]:        152 :                     os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
    1772         [ +  - ]:         31 :                     os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
    1773                 :         31 :                     os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
    1774                 :            :                         /*
    1775                 :            :                          * Avoid issues with some roaming cases where
    1776                 :            :                          * disconnection event for the old AP may show up after
    1777                 :            :                          * we have started connection with the new AP.
    1778                 :            :                          */
    1779                 :         31 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
    1780                 :        186 :                                    MAC2STR(bssid),
    1781                 :        186 :                                    MAC2STR(drv->auth_attempt_bssid));
    1782                 :         31 :                         return;
    1783                 :            :                 }
    1784                 :            : 
    1785 [ +  + ][ -  + ]:        636 :                 if (drv->associated != 0 &&
    1786         [ #  # ]:          0 :                     os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
    1787                 :          0 :                     os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
    1788                 :            :                         /*
    1789                 :            :                          * We have presumably received this deauth as a
    1790                 :            :                          * response to a clear_state_mismatch() outgoing
    1791                 :            :                          * deauth.  Don't let it take us offline!
    1792                 :            :                          */
    1793                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
    1794                 :            :                                    "from Unknown BSSID " MACSTR " -- ignoring",
    1795                 :          0 :                                    MAC2STR(bssid));
    1796                 :          0 :                         return;
    1797                 :            :                 }
    1798                 :            :         }
    1799                 :            : 
    1800                 :        636 :         nl80211_mark_disconnected(drv);
    1801                 :        636 :         os_memset(&event, 0, sizeof(event));
    1802                 :            : 
    1803                 :            :         /* Note: Same offset for Reason Code in both frame subtypes */
    1804         [ +  - ]:        636 :         if (len >= 24 + sizeof(mgmt->u.deauth))
    1805                 :        636 :                 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
    1806                 :            : 
    1807         [ +  + ]:        636 :         if (type == EVENT_DISASSOC) {
    1808                 :          1 :                 event.disassoc_info.locally_generated =
    1809                 :          1 :                         !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
    1810                 :          1 :                 event.disassoc_info.addr = bssid;
    1811                 :          1 :                 event.disassoc_info.reason_code = reason_code;
    1812         [ -  + ]:          1 :                 if (frame + len > mgmt->u.disassoc.variable) {
    1813                 :          0 :                         event.disassoc_info.ie = mgmt->u.disassoc.variable;
    1814                 :          0 :                         event.disassoc_info.ie_len = frame + len -
    1815                 :            :                                 mgmt->u.disassoc.variable;
    1816                 :            :                 }
    1817                 :            :         } else {
    1818                 :        635 :                 event.deauth_info.locally_generated =
    1819                 :        635 :                         !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
    1820                 :        635 :                 event.deauth_info.addr = bssid;
    1821                 :        635 :                 event.deauth_info.reason_code = reason_code;
    1822         [ -  + ]:        635 :                 if (frame + len > mgmt->u.deauth.variable) {
    1823                 :          0 :                         event.deauth_info.ie = mgmt->u.deauth.variable;
    1824                 :          0 :                         event.deauth_info.ie_len = frame + len -
    1825                 :            :                                 mgmt->u.deauth.variable;
    1826                 :            :                 }
    1827                 :            :         }
    1828                 :            : 
    1829                 :        667 :         wpa_supplicant_event(drv->ctx, type, &event);
    1830                 :            : }
    1831                 :            : 
    1832                 :            : 
    1833                 :          0 : static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
    1834                 :            :                                          enum wpa_event_type type,
    1835                 :            :                                          const u8 *frame, size_t len)
    1836                 :            : {
    1837                 :            :         const struct ieee80211_mgmt *mgmt;
    1838                 :            :         union wpa_event_data event;
    1839                 :          0 :         u16 reason_code = 0;
    1840                 :            : 
    1841         [ #  # ]:          0 :         if (type == EVENT_UNPROT_DEAUTH)
    1842                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
    1843                 :            :         else
    1844                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
    1845                 :            : 
    1846         [ #  # ]:          0 :         if (len < 24)
    1847                 :          0 :                 return;
    1848                 :            : 
    1849                 :          0 :         mgmt = (const struct ieee80211_mgmt *) frame;
    1850                 :            : 
    1851                 :          0 :         os_memset(&event, 0, sizeof(event));
    1852                 :            :         /* Note: Same offset for Reason Code in both frame subtypes */
    1853         [ #  # ]:          0 :         if (len >= 24 + sizeof(mgmt->u.deauth))
    1854                 :          0 :                 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
    1855                 :            : 
    1856         [ #  # ]:          0 :         if (type == EVENT_UNPROT_DISASSOC) {
    1857                 :          0 :                 event.unprot_disassoc.sa = mgmt->sa;
    1858                 :          0 :                 event.unprot_disassoc.da = mgmt->da;
    1859                 :          0 :                 event.unprot_disassoc.reason_code = reason_code;
    1860                 :            :         } else {
    1861                 :          0 :                 event.unprot_deauth.sa = mgmt->sa;
    1862                 :          0 :                 event.unprot_deauth.da = mgmt->da;
    1863                 :          0 :                 event.unprot_deauth.reason_code = reason_code;
    1864                 :            :         }
    1865                 :            : 
    1866                 :          0 :         wpa_supplicant_event(drv->ctx, type, &event);
    1867                 :            : }
    1868                 :            : 
    1869                 :            : 
    1870                 :       9549 : static void mlme_event(struct i802_bss *bss,
    1871                 :            :                        enum nl80211_commands cmd, struct nlattr *frame,
    1872                 :            :                        struct nlattr *addr, struct nlattr *timed_out,
    1873                 :            :                        struct nlattr *freq, struct nlattr *ack,
    1874                 :            :                        struct nlattr *cookie, struct nlattr *sig)
    1875                 :            : {
    1876                 :       9549 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    1877                 :            :         const u8 *data;
    1878                 :            :         size_t len;
    1879                 :            : 
    1880 [ +  + ][ +  - ]:       9549 :         if (timed_out && addr) {
    1881                 :          3 :                 mlme_timeout_event(drv, cmd, addr);
    1882                 :          3 :                 return;
    1883                 :            :         }
    1884                 :            : 
    1885         [ -  + ]:       9546 :         if (frame == NULL) {
    1886                 :          0 :                 wpa_printf(MSG_DEBUG,
    1887                 :            :                            "nl80211: MLME event %d (%s) without frame data",
    1888                 :            :                            cmd, nl80211_command_to_string(cmd));
    1889                 :          0 :                 return;
    1890                 :            :         }
    1891                 :            : 
    1892                 :       9546 :         data = nla_data(frame);
    1893                 :       9546 :         len = nla_len(frame);
    1894         [ -  + ]:       9546 :         if (len < 4 + 2 * ETH_ALEN) {
    1895                 :          0 :                 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
    1896                 :            :                            MACSTR ") - too short",
    1897                 :          0 :                            cmd, nl80211_command_to_string(cmd), bss->ifname,
    1898                 :          0 :                            MAC2STR(bss->addr));
    1899                 :          0 :                 return;
    1900                 :            :         }
    1901                 :       9546 :         wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
    1902                 :            :                    ") A1=" MACSTR " A2=" MACSTR, cmd,
    1903                 :       9546 :                    nl80211_command_to_string(cmd), bss->ifname,
    1904                 :     114552 :                    MAC2STR(bss->addr), MAC2STR(data + 4),
    1905                 :      57276 :                    MAC2STR(data + 4 + ETH_ALEN));
    1906 [ +  + ][ +  + ]:       9546 :         if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
                 [ +  + ]
    1907         [ -  + ]:        567 :             os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
    1908                 :        567 :             os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
    1909                 :          0 :                 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
    1910                 :          0 :                            "for foreign address", bss->ifname);
    1911                 :          0 :                 return;
    1912                 :            :         }
    1913                 :       9546 :         wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
    1914                 :       9546 :                     nla_data(frame), nla_len(frame));
    1915                 :            : 
    1916   [ +  +  +  +  :       9546 :         switch (cmd) {
             +  +  -  -  
                      - ]
    1917                 :            :         case NL80211_CMD_AUTHENTICATE:
    1918                 :        693 :                 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
    1919                 :        693 :                 break;
    1920                 :            :         case NL80211_CMD_ASSOCIATE:
    1921                 :        674 :                 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
    1922                 :        674 :                 break;
    1923                 :            :         case NL80211_CMD_DEAUTHENTICATE:
    1924                 :        666 :                 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
    1925                 :        666 :                                            nla_data(frame), nla_len(frame));
    1926                 :        666 :                 break;
    1927                 :            :         case NL80211_CMD_DISASSOCIATE:
    1928                 :          1 :                 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
    1929                 :          1 :                                            nla_data(frame), nla_len(frame));
    1930                 :          1 :                 break;
    1931                 :            :         case NL80211_CMD_FRAME:
    1932                 :       4406 :                 mlme_event_mgmt(drv, freq, sig, nla_data(frame),
    1933                 :       4406 :                                 nla_len(frame));
    1934                 :       4406 :                 break;
    1935                 :            :         case NL80211_CMD_FRAME_TX_STATUS:
    1936                 :       3106 :                 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
    1937                 :       3106 :                                           nla_len(frame), ack);
    1938                 :       3106 :                 break;
    1939                 :            :         case NL80211_CMD_UNPROT_DEAUTHENTICATE:
    1940                 :          0 :                 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
    1941                 :          0 :                                              nla_data(frame), nla_len(frame));
    1942                 :          0 :                 break;
    1943                 :            :         case NL80211_CMD_UNPROT_DISASSOCIATE:
    1944                 :          0 :                 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
    1945                 :          0 :                                              nla_data(frame), nla_len(frame));
    1946                 :          0 :                 break;
    1947                 :            :         default:
    1948                 :       9549 :                 break;
    1949                 :            :         }
    1950                 :            : }
    1951                 :            : 
    1952                 :            : 
    1953                 :          0 : static void mlme_event_michael_mic_failure(struct i802_bss *bss,
    1954                 :            :                                            struct nlattr *tb[])
    1955                 :            : {
    1956                 :            :         union wpa_event_data data;
    1957                 :            : 
    1958                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
    1959                 :          0 :         os_memset(&data, 0, sizeof(data));
    1960         [ #  # ]:          0 :         if (tb[NL80211_ATTR_MAC]) {
    1961                 :          0 :                 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
    1962                 :          0 :                             nla_data(tb[NL80211_ATTR_MAC]),
    1963                 :          0 :                             nla_len(tb[NL80211_ATTR_MAC]));
    1964                 :          0 :                 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
    1965                 :            :         }
    1966         [ #  # ]:          0 :         if (tb[NL80211_ATTR_KEY_SEQ]) {
    1967                 :          0 :                 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
    1968                 :          0 :                             nla_data(tb[NL80211_ATTR_KEY_SEQ]),
    1969                 :          0 :                             nla_len(tb[NL80211_ATTR_KEY_SEQ]));
    1970                 :            :         }
    1971         [ #  # ]:          0 :         if (tb[NL80211_ATTR_KEY_TYPE]) {
    1972                 :          0 :                 enum nl80211_key_type key_type =
    1973                 :          0 :                         nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
    1974                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
    1975         [ #  # ]:          0 :                 if (key_type == NL80211_KEYTYPE_PAIRWISE)
    1976                 :          0 :                         data.michael_mic_failure.unicast = 1;
    1977                 :            :         } else
    1978                 :          0 :                 data.michael_mic_failure.unicast = 1;
    1979                 :            : 
    1980         [ #  # ]:          0 :         if (tb[NL80211_ATTR_KEY_IDX]) {
    1981                 :          0 :                 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
    1982                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
    1983                 :            :         }
    1984                 :            : 
    1985                 :          0 :         wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
    1986                 :          0 : }
    1987                 :            : 
    1988                 :            : 
    1989                 :          5 : static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
    1990                 :            :                                  struct nlattr *tb[])
    1991                 :            : {
    1992         [ -  + ]:          5 :         if (tb[NL80211_ATTR_MAC] == NULL) {
    1993                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
    1994                 :            :                            "event");
    1995                 :          5 :                 return;
    1996                 :            :         }
    1997                 :          5 :         os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
    1998                 :            : 
    1999                 :          5 :         drv->associated = 1;
    2000                 :          5 :         wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
    2001                 :         30 :                    MAC2STR(drv->bssid));
    2002                 :            : 
    2003                 :          5 :         wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
    2004                 :            : }
    2005                 :            : 
    2006                 :            : 
    2007                 :       1550 : static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
    2008                 :            :                                          int cancel_event, struct nlattr *tb[])
    2009                 :            : {
    2010                 :            :         unsigned int freq, chan_type, duration;
    2011                 :            :         union wpa_event_data data;
    2012                 :            :         u64 cookie;
    2013                 :            : 
    2014         [ +  - ]:       1550 :         if (tb[NL80211_ATTR_WIPHY_FREQ])
    2015                 :       1550 :                 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
    2016                 :            :         else
    2017                 :          0 :                 freq = 0;
    2018                 :            : 
    2019         [ +  - ]:       1550 :         if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
    2020                 :       1550 :                 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
    2021                 :            :         else
    2022                 :          0 :                 chan_type = 0;
    2023                 :            : 
    2024         [ +  + ]:       1550 :         if (tb[NL80211_ATTR_DURATION])
    2025                 :        775 :                 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
    2026                 :            :         else
    2027                 :        775 :                 duration = 0;
    2028                 :            : 
    2029         [ +  - ]:       1550 :         if (tb[NL80211_ATTR_COOKIE])
    2030                 :       1550 :                 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
    2031                 :            :         else
    2032                 :          0 :                 cookie = 0;
    2033                 :            : 
    2034         [ +  + ]:       1550 :         wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
    2035                 :            :                    "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
    2036                 :            :                    cancel_event, freq, chan_type, duration,
    2037                 :            :                    (long long unsigned int) cookie,
    2038                 :       1550 :                    cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
    2039                 :            : 
    2040         [ +  + ]:       1550 :         if (cookie != drv->remain_on_chan_cookie)
    2041                 :       1550 :                 return; /* not for us */
    2042                 :            : 
    2043         [ +  + ]:       1544 :         if (cancel_event)
    2044                 :        769 :                 drv->pending_remain_on_chan = 0;
    2045                 :            : 
    2046                 :       1544 :         os_memset(&data, 0, sizeof(data));
    2047                 :       1544 :         data.remain_on_channel.freq = freq;
    2048                 :       1544 :         data.remain_on_channel.duration = duration;
    2049         [ +  + ]:       1544 :         wpa_supplicant_event(drv->ctx, cancel_event ?
    2050                 :            :                              EVENT_CANCEL_REMAIN_ON_CHANNEL :
    2051                 :            :                              EVENT_REMAIN_ON_CHANNEL, &data);
    2052                 :            : }
    2053                 :            : 
    2054                 :            : 
    2055                 :          0 : static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
    2056                 :            :                                 struct nlattr *tb[])
    2057                 :            : {
    2058                 :            :         union wpa_event_data data;
    2059                 :            : 
    2060                 :          0 :         os_memset(&data, 0, sizeof(data));
    2061                 :            : 
    2062         [ #  # ]:          0 :         if (tb[NL80211_ATTR_IE]) {
    2063                 :          0 :                 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
    2064                 :          0 :                 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
    2065                 :            :         }
    2066                 :            : 
    2067         [ #  # ]:          0 :         if (tb[NL80211_ATTR_IE_RIC]) {
    2068                 :          0 :                 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
    2069                 :          0 :                 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
    2070                 :            :         }
    2071                 :            : 
    2072         [ #  # ]:          0 :         if (tb[NL80211_ATTR_MAC])
    2073                 :          0 :                 os_memcpy(data.ft_ies.target_ap,
    2074                 :            :                           nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
    2075                 :            : 
    2076                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
    2077                 :          0 :                    MAC2STR(data.ft_ies.target_ap));
    2078                 :            : 
    2079                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
    2080                 :          0 : }
    2081                 :            : 
    2082                 :            : 
    2083                 :       1376 : static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
    2084                 :            :                             struct nlattr *tb[])
    2085                 :            : {
    2086                 :            :         union wpa_event_data event;
    2087                 :            :         struct nlattr *nl;
    2088                 :            :         int rem;
    2089                 :            :         struct scan_info *info;
    2090                 :            : #define MAX_REPORT_FREQS 50
    2091                 :            :         int freqs[MAX_REPORT_FREQS];
    2092                 :       1376 :         int num_freqs = 0;
    2093                 :            : 
    2094         [ -  + ]:       1376 :         if (drv->scan_for_auth) {
    2095                 :          0 :                 drv->scan_for_auth = 0;
    2096                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
    2097                 :            :                            "cfg80211 BSS entry");
    2098                 :          0 :                 wpa_driver_nl80211_authenticate_retry(drv);
    2099                 :       1376 :                 return;
    2100                 :            :         }
    2101                 :            : 
    2102                 :       1376 :         os_memset(&event, 0, sizeof(event));
    2103                 :       1376 :         info = &event.scan_info;
    2104                 :       1376 :         info->aborted = aborted;
    2105                 :            : 
    2106         [ +  - ]:       1376 :         if (tb[NL80211_ATTR_SCAN_SSIDS]) {
    2107         [ +  + ]:       2731 :                 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
    2108                 :       1355 :                         struct wpa_driver_scan_ssid *s =
    2109                 :       1355 :                                 &info->ssids[info->num_ssids];
    2110                 :       1355 :                         s->ssid = nla_data(nl);
    2111                 :       1355 :                         s->ssid_len = nla_len(nl);
    2112                 :       1355 :                         wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
    2113                 :            :                                    wpa_ssid_txt(s->ssid, s->ssid_len));
    2114                 :       1355 :                         info->num_ssids++;
    2115         [ -  + ]:       1355 :                         if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
    2116                 :          0 :                                 break;
    2117                 :            :                 }
    2118                 :            :         }
    2119         [ +  - ]:       1376 :         if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
    2120                 :            :                 char msg[200], *pos, *end;
    2121                 :            :                 int res;
    2122                 :            : 
    2123                 :       1376 :                 pos = msg;
    2124                 :       1376 :                 end = pos + sizeof(msg);
    2125                 :       1376 :                 *pos = '\0';
    2126                 :            : 
    2127         [ +  + ]:       7431 :                 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
    2128                 :            :                 {
    2129                 :       6055 :                         freqs[num_freqs] = nla_get_u32(nl);
    2130                 :       6055 :                         res = os_snprintf(pos, end - pos, " %d",
    2131                 :            :                                           freqs[num_freqs]);
    2132 [ +  - ][ +  - ]:       6055 :                         if (res > 0 && end - pos > res)
    2133                 :       6055 :                                 pos += res;
    2134                 :       6055 :                         num_freqs++;
    2135         [ -  + ]:       6055 :                         if (num_freqs == MAX_REPORT_FREQS - 1)
    2136                 :          0 :                                 break;
    2137                 :            :                 }
    2138                 :       1376 :                 info->freqs = freqs;
    2139                 :       1376 :                 info->num_freqs = num_freqs;
    2140                 :       1376 :                 wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
    2141                 :            :                            msg);
    2142                 :            :         }
    2143                 :       1376 :         wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
    2144                 :            : }
    2145                 :            : 
    2146                 :            : 
    2147                 :          0 : static int get_link_signal(struct nl_msg *msg, void *arg)
    2148                 :            : {
    2149                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    2150                 :          0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    2151                 :            :         struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
    2152                 :            :         static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
    2153                 :            :                 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
    2154                 :            :                 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
    2155                 :            :         };
    2156                 :            :         struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
    2157                 :            :         static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
    2158                 :            :                 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
    2159                 :            :                 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
    2160                 :            :                 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
    2161                 :            :                 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
    2162                 :            :         };
    2163                 :          0 :         struct wpa_signal_info *sig_change = arg;
    2164                 :            : 
    2165                 :          0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    2166                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    2167   [ #  #  #  # ]:          0 :         if (!tb[NL80211_ATTR_STA_INFO] ||
    2168                 :          0 :             nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
    2169                 :            :                              tb[NL80211_ATTR_STA_INFO], policy))
    2170                 :          0 :                 return NL_SKIP;
    2171         [ #  # ]:          0 :         if (!sinfo[NL80211_STA_INFO_SIGNAL])
    2172                 :          0 :                 return NL_SKIP;
    2173                 :            : 
    2174                 :          0 :         sig_change->current_signal =
    2175                 :          0 :                 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
    2176                 :            : 
    2177         [ #  # ]:          0 :         if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
    2178                 :          0 :                 sig_change->avg_signal =
    2179                 :          0 :                         (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
    2180                 :            :         else
    2181                 :          0 :                 sig_change->avg_signal = 0;
    2182                 :            : 
    2183         [ #  # ]:          0 :         if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
    2184         [ #  # ]:          0 :                 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
    2185                 :            :                                      sinfo[NL80211_STA_INFO_TX_BITRATE],
    2186                 :            :                                      rate_policy)) {
    2187                 :          0 :                         sig_change->current_txrate = 0;
    2188                 :            :                 } else {
    2189         [ #  # ]:          0 :                         if (rinfo[NL80211_RATE_INFO_BITRATE]) {
    2190                 :          0 :                                 sig_change->current_txrate =
    2191                 :          0 :                                         nla_get_u16(rinfo[
    2192                 :          0 :                                              NL80211_RATE_INFO_BITRATE]) * 100;
    2193                 :            :                         }
    2194                 :            :                 }
    2195                 :            :         }
    2196                 :            : 
    2197                 :          0 :         return NL_SKIP;
    2198                 :            : }
    2199                 :            : 
    2200                 :            : 
    2201                 :          0 : static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
    2202                 :            :                                    struct wpa_signal_info *sig)
    2203                 :            : {
    2204                 :            :         struct nl_msg *msg;
    2205                 :            : 
    2206                 :          0 :         sig->current_signal = -9999;
    2207                 :          0 :         sig->current_txrate = 0;
    2208                 :            : 
    2209                 :          0 :         msg = nlmsg_alloc();
    2210         [ #  # ]:          0 :         if (!msg)
    2211                 :          0 :                 return -ENOMEM;
    2212                 :            : 
    2213                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
    2214                 :            : 
    2215         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    2216         [ #  # ]:          0 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
    2217                 :            : 
    2218                 :          0 :         return send_and_recv_msgs(drv, msg, get_link_signal, sig);
    2219                 :            :  nla_put_failure:
    2220                 :          0 :         nlmsg_free(msg);
    2221                 :          0 :         return -ENOBUFS;
    2222                 :            : }
    2223                 :            : 
    2224                 :            : 
    2225                 :          0 : static int get_link_noise(struct nl_msg *msg, void *arg)
    2226                 :            : {
    2227                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    2228                 :          0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    2229                 :            :         struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
    2230                 :            :         static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
    2231                 :            :                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
    2232                 :            :                 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
    2233                 :            :         };
    2234                 :          0 :         struct wpa_signal_info *sig_change = arg;
    2235                 :            : 
    2236                 :          0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    2237                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    2238                 :            : 
    2239         [ #  # ]:          0 :         if (!tb[NL80211_ATTR_SURVEY_INFO]) {
    2240                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
    2241                 :          0 :                 return NL_SKIP;
    2242                 :            :         }
    2243                 :            : 
    2244         [ #  # ]:          0 :         if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
    2245                 :            :                              tb[NL80211_ATTR_SURVEY_INFO],
    2246                 :            :                              survey_policy)) {
    2247                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
    2248                 :            :                            "attributes!");
    2249                 :          0 :                 return NL_SKIP;
    2250                 :            :         }
    2251                 :            : 
    2252         [ #  # ]:          0 :         if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
    2253                 :          0 :                 return NL_SKIP;
    2254                 :            : 
    2255         [ #  # ]:          0 :         if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
    2256                 :          0 :             sig_change->frequency)
    2257                 :          0 :                 return NL_SKIP;
    2258                 :            : 
    2259         [ #  # ]:          0 :         if (!sinfo[NL80211_SURVEY_INFO_NOISE])
    2260                 :          0 :                 return NL_SKIP;
    2261                 :            : 
    2262                 :          0 :         sig_change->current_noise =
    2263                 :          0 :                 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
    2264                 :            : 
    2265                 :          0 :         return NL_SKIP;
    2266                 :            : }
    2267                 :            : 
    2268                 :            : 
    2269                 :          0 : static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
    2270                 :            :                                   struct wpa_signal_info *sig_change)
    2271                 :            : {
    2272                 :            :         struct nl_msg *msg;
    2273                 :            : 
    2274                 :          0 :         sig_change->current_noise = 9999;
    2275                 :          0 :         sig_change->frequency = drv->assoc_freq;
    2276                 :            : 
    2277                 :          0 :         msg = nlmsg_alloc();
    2278         [ #  # ]:          0 :         if (!msg)
    2279                 :          0 :                 return -ENOMEM;
    2280                 :            : 
    2281                 :          0 :         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
    2282                 :            : 
    2283         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    2284                 :            : 
    2285                 :          0 :         return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
    2286                 :            :  nla_put_failure:
    2287                 :          0 :         nlmsg_free(msg);
    2288                 :          0 :         return -ENOBUFS;
    2289                 :            : }
    2290                 :            : 
    2291                 :            : 
    2292                 :       1363 : static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
    2293                 :            : {
    2294                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    2295                 :       1363 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    2296                 :            :         struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
    2297                 :            :         static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
    2298                 :            :                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
    2299                 :            :                 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
    2300                 :            :         };
    2301                 :       1363 :         struct wpa_scan_results *scan_results = arg;
    2302                 :            :         struct wpa_scan_res *scan_res;
    2303                 :            :         size_t i;
    2304                 :            : 
    2305                 :       1363 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    2306                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    2307                 :            : 
    2308         [ -  + ]:       1363 :         if (!tb[NL80211_ATTR_SURVEY_INFO]) {
    2309                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
    2310                 :          0 :                 return NL_SKIP;
    2311                 :            :         }
    2312                 :            : 
    2313         [ -  + ]:       1363 :         if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
    2314                 :            :                              tb[NL80211_ATTR_SURVEY_INFO],
    2315                 :            :                              survey_policy)) {
    2316                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
    2317                 :            :                            "attributes");
    2318                 :          0 :                 return NL_SKIP;
    2319                 :            :         }
    2320                 :            : 
    2321         [ -  + ]:       1363 :         if (!sinfo[NL80211_SURVEY_INFO_NOISE])
    2322                 :          0 :                 return NL_SKIP;
    2323                 :            : 
    2324         [ -  + ]:       1363 :         if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
    2325                 :          0 :                 return NL_SKIP;
    2326                 :            : 
    2327         [ +  + ]:       3656 :         for (i = 0; i < scan_results->num; ++i) {
    2328                 :       2293 :                 scan_res = scan_results->res[i];
    2329         [ -  + ]:       2293 :                 if (!scan_res)
    2330                 :          0 :                         continue;
    2331         [ +  + ]:       2293 :                 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
    2332                 :       2293 :                     scan_res->freq)
    2333                 :        650 :                         continue;
    2334         [ -  + ]:       1643 :                 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
    2335                 :          0 :                         continue;
    2336                 :       1643 :                 scan_res->noise = (s8)
    2337                 :       1643 :                         nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
    2338                 :       1643 :                 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
    2339                 :            :         }
    2340                 :            : 
    2341                 :       1363 :         return NL_SKIP;
    2342                 :            : }
    2343                 :            : 
    2344                 :            : 
    2345                 :       1363 : static int nl80211_get_noise_for_scan_results(
    2346                 :            :         struct wpa_driver_nl80211_data *drv,
    2347                 :            :         struct wpa_scan_results *scan_res)
    2348                 :            : {
    2349                 :            :         struct nl_msg *msg;
    2350                 :            : 
    2351                 :       1363 :         msg = nlmsg_alloc();
    2352         [ -  + ]:       1363 :         if (!msg)
    2353                 :          0 :                 return -ENOMEM;
    2354                 :            : 
    2355                 :       1363 :         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
    2356                 :            : 
    2357         [ +  - ]:       1363 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    2358                 :            : 
    2359                 :       1363 :         return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
    2360                 :            :                                   scan_res);
    2361                 :            :  nla_put_failure:
    2362                 :          0 :         nlmsg_free(msg);
    2363                 :       1363 :         return -ENOBUFS;
    2364                 :            : }
    2365                 :            : 
    2366                 :            : 
    2367                 :          0 : static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
    2368                 :            :                               struct nlattr *tb[])
    2369                 :            : {
    2370                 :            :         static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
    2371                 :            :                 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
    2372                 :            :                 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
    2373                 :            :                 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
    2374                 :            :                 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
    2375                 :            :         };
    2376                 :            :         struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
    2377                 :            :         enum nl80211_cqm_rssi_threshold_event event;
    2378                 :            :         union wpa_event_data ed;
    2379                 :            :         struct wpa_signal_info sig;
    2380                 :            :         int res;
    2381                 :            : 
    2382   [ #  #  #  # ]:          0 :         if (tb[NL80211_ATTR_CQM] == NULL ||
    2383                 :          0 :             nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
    2384                 :            :                              cqm_policy)) {
    2385                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
    2386                 :          0 :                 return;
    2387                 :            :         }
    2388                 :            : 
    2389                 :          0 :         os_memset(&ed, 0, sizeof(ed));
    2390                 :            : 
    2391         [ #  # ]:          0 :         if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
    2392         [ #  # ]:          0 :                 if (!tb[NL80211_ATTR_MAC])
    2393                 :          0 :                         return;
    2394                 :          0 :                 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
    2395                 :            :                           ETH_ALEN);
    2396                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
    2397                 :          0 :                 return;
    2398                 :            :         }
    2399                 :            : 
    2400         [ #  # ]:          0 :         if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
    2401                 :          0 :                 return;
    2402                 :          0 :         event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
    2403                 :            : 
    2404         [ #  # ]:          0 :         if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
    2405                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
    2406                 :            :                            "event: RSSI high");
    2407                 :          0 :                 ed.signal_change.above_threshold = 1;
    2408         [ #  # ]:          0 :         } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
    2409                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
    2410                 :            :                            "event: RSSI low");
    2411                 :          0 :                 ed.signal_change.above_threshold = 0;
    2412                 :            :         } else
    2413                 :          0 :                 return;
    2414                 :            : 
    2415                 :          0 :         res = nl80211_get_link_signal(drv, &sig);
    2416         [ #  # ]:          0 :         if (res == 0) {
    2417                 :          0 :                 ed.signal_change.current_signal = sig.current_signal;
    2418                 :          0 :                 ed.signal_change.current_txrate = sig.current_txrate;
    2419                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm  txrate: %d",
    2420                 :            :                            sig.current_signal, sig.current_txrate);
    2421                 :            :         }
    2422                 :            : 
    2423                 :          0 :         res = nl80211_get_link_noise(drv, &sig);
    2424         [ #  # ]:          0 :         if (res == 0) {
    2425                 :          0 :                 ed.signal_change.current_noise = sig.current_noise;
    2426                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
    2427                 :            :                            sig.current_noise);
    2428                 :            :         }
    2429                 :            : 
    2430                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
    2431                 :            : }
    2432                 :            : 
    2433                 :            : 
    2434                 :       1423 : static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
    2435                 :            :                                       struct nlattr **tb)
    2436                 :            : {
    2437                 :            :         u8 *addr;
    2438                 :            :         union wpa_event_data data;
    2439                 :            : 
    2440         [ -  + ]:       1423 :         if (tb[NL80211_ATTR_MAC] == NULL)
    2441                 :          0 :                 return;
    2442                 :       1423 :         addr = nla_data(tb[NL80211_ATTR_MAC]);
    2443                 :       1423 :         wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
    2444                 :            : 
    2445 [ -  + ][ +  + ]:       1423 :         if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
    2446                 :          0 :                 u8 *ies = NULL;
    2447                 :          0 :                 size_t ies_len = 0;
    2448         [ #  # ]:          0 :                 if (tb[NL80211_ATTR_IE]) {
    2449                 :          0 :                         ies = nla_data(tb[NL80211_ATTR_IE]);
    2450                 :          0 :                         ies_len = nla_len(tb[NL80211_ATTR_IE]);
    2451                 :            :                 }
    2452                 :          0 :                 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
    2453                 :          0 :                 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
    2454                 :          0 :                 return;
    2455                 :            :         }
    2456                 :            : 
    2457         [ +  + ]:       1423 :         if (drv->nlmode != NL80211_IFTYPE_ADHOC)
    2458                 :       1419 :                 return;
    2459                 :            : 
    2460                 :          4 :         os_memset(&data, 0, sizeof(data));
    2461                 :          4 :         os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
    2462                 :       1423 :         wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
    2463                 :            : }
    2464                 :            : 
    2465                 :            : 
    2466                 :       1388 : static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
    2467                 :            :                                       struct nlattr **tb)
    2468                 :            : {
    2469                 :            :         u8 *addr;
    2470                 :            :         union wpa_event_data data;
    2471                 :            : 
    2472         [ -  + ]:       1388 :         if (tb[NL80211_ATTR_MAC] == NULL)
    2473                 :          0 :                 return;
    2474                 :       1388 :         addr = nla_data(tb[NL80211_ATTR_MAC]);
    2475                 :       1388 :         wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
    2476                 :       8328 :                    MAC2STR(addr));
    2477                 :            : 
    2478 [ -  + ][ +  + ]:       1388 :         if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
    2479                 :          0 :                 drv_event_disassoc(drv->ctx, addr);
    2480                 :          0 :                 return;
    2481                 :            :         }
    2482                 :            : 
    2483         [ +  - ]:       1388 :         if (drv->nlmode != NL80211_IFTYPE_ADHOC)
    2484                 :       1388 :                 return;
    2485                 :            : 
    2486                 :          0 :         os_memset(&data, 0, sizeof(data));
    2487                 :          0 :         os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
    2488                 :       1388 :         wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
    2489                 :            : }
    2490                 :            : 
    2491                 :            : 
    2492                 :          0 : static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
    2493                 :            :                                         struct nlattr **tb)
    2494                 :            : {
    2495                 :            :         struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
    2496                 :            :         static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
    2497                 :            :                 [NL80211_REKEY_DATA_KEK] = {
    2498                 :            :                         .minlen = NL80211_KEK_LEN,
    2499                 :            :                         .maxlen = NL80211_KEK_LEN,
    2500                 :            :                 },
    2501                 :            :                 [NL80211_REKEY_DATA_KCK] = {
    2502                 :            :                         .minlen = NL80211_KCK_LEN,
    2503                 :            :                         .maxlen = NL80211_KCK_LEN,
    2504                 :            :                 },
    2505                 :            :                 [NL80211_REKEY_DATA_REPLAY_CTR] = {
    2506                 :            :                         .minlen = NL80211_REPLAY_CTR_LEN,
    2507                 :            :                         .maxlen = NL80211_REPLAY_CTR_LEN,
    2508                 :            :                 },
    2509                 :            :         };
    2510                 :            :         union wpa_event_data data;
    2511                 :            : 
    2512         [ #  # ]:          0 :         if (!tb[NL80211_ATTR_MAC])
    2513                 :          0 :                 return;
    2514         [ #  # ]:          0 :         if (!tb[NL80211_ATTR_REKEY_DATA])
    2515                 :          0 :                 return;
    2516         [ #  # ]:          0 :         if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
    2517                 :          0 :                              tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
    2518                 :          0 :                 return;
    2519         [ #  # ]:          0 :         if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
    2520                 :          0 :                 return;
    2521                 :            : 
    2522                 :          0 :         os_memset(&data, 0, sizeof(data));
    2523                 :          0 :         data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
    2524                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
    2525                 :          0 :                    MAC2STR(data.driver_gtk_rekey.bssid));
    2526                 :          0 :         data.driver_gtk_rekey.replay_ctr =
    2527                 :          0 :                 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
    2528                 :          0 :         wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
    2529                 :          0 :                     data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
    2530                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
    2531                 :            : }
    2532                 :            : 
    2533                 :            : 
    2534                 :          0 : static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
    2535                 :            :                                           struct nlattr **tb)
    2536                 :            : {
    2537                 :            :         struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
    2538                 :            :         static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
    2539                 :            :                 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
    2540                 :            :                 [NL80211_PMKSA_CANDIDATE_BSSID] = {
    2541                 :            :                         .minlen = ETH_ALEN,
    2542                 :            :                         .maxlen = ETH_ALEN,
    2543                 :            :                 },
    2544                 :            :                 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
    2545                 :            :         };
    2546                 :            :         union wpa_event_data data;
    2547                 :            : 
    2548                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
    2549                 :            : 
    2550         [ #  # ]:          0 :         if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
    2551                 :          0 :                 return;
    2552         [ #  # ]:          0 :         if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
    2553                 :          0 :                              tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
    2554                 :          0 :                 return;
    2555 [ #  # ][ #  # ]:          0 :         if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
    2556                 :          0 :             !cand[NL80211_PMKSA_CANDIDATE_BSSID])
    2557                 :          0 :                 return;
    2558                 :            : 
    2559                 :          0 :         os_memset(&data, 0, sizeof(data));
    2560                 :          0 :         os_memcpy(data.pmkid_candidate.bssid,
    2561                 :            :                   nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
    2562                 :          0 :         data.pmkid_candidate.index =
    2563                 :          0 :                 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
    2564                 :          0 :         data.pmkid_candidate.preauth =
    2565                 :          0 :                 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
    2566                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
    2567                 :            : }
    2568                 :            : 
    2569                 :            : 
    2570                 :          0 : static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
    2571                 :            :                                        struct nlattr **tb)
    2572                 :            : {
    2573                 :            :         union wpa_event_data data;
    2574                 :            : 
    2575                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
    2576                 :            : 
    2577 [ #  # ][ #  # ]:          0 :         if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
    2578                 :          0 :                 return;
    2579                 :            : 
    2580                 :          0 :         os_memset(&data, 0, sizeof(data));
    2581                 :          0 :         os_memcpy(data.client_poll.addr,
    2582                 :            :                   nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
    2583                 :            : 
    2584                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
    2585                 :            : }
    2586                 :            : 
    2587                 :            : 
    2588                 :          0 : static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
    2589                 :            :                                     struct nlattr **tb)
    2590                 :            : {
    2591                 :            :         union wpa_event_data data;
    2592                 :            : 
    2593                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
    2594                 :            : 
    2595 [ #  # ][ #  # ]:          0 :         if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
    2596                 :          0 :                 return;
    2597                 :            : 
    2598                 :          0 :         os_memset(&data, 0, sizeof(data));
    2599                 :          0 :         os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
    2600      [ #  #  # ]:          0 :         switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
    2601                 :            :         case NL80211_TDLS_SETUP:
    2602                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
    2603                 :          0 :                            MACSTR, MAC2STR(data.tdls.peer));
    2604                 :          0 :                 data.tdls.oper = TDLS_REQUEST_SETUP;
    2605                 :          0 :                 break;
    2606                 :            :         case NL80211_TDLS_TEARDOWN:
    2607                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
    2608                 :          0 :                            MACSTR, MAC2STR(data.tdls.peer));
    2609                 :          0 :                 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
    2610                 :          0 :                 break;
    2611                 :            :         default:
    2612                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
    2613                 :            :                            "event");
    2614                 :          0 :                 return;
    2615                 :            :         }
    2616         [ #  # ]:          0 :         if (tb[NL80211_ATTR_REASON_CODE]) {
    2617                 :          0 :                 data.tdls.reason_code =
    2618                 :          0 :                         nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
    2619                 :            :         }
    2620                 :            : 
    2621                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
    2622                 :            : }
    2623                 :            : 
    2624                 :            : 
    2625                 :         71 : static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
    2626                 :            :                             struct nlattr **tb)
    2627                 :            : {
    2628                 :         71 :         wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
    2629                 :         71 : }
    2630                 :            : 
    2631                 :            : 
    2632                 :          0 : static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
    2633                 :            :                                          struct nlattr **tb)
    2634                 :            : {
    2635                 :            :         union wpa_event_data data;
    2636                 :            :         u32 reason;
    2637                 :            : 
    2638                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
    2639                 :            : 
    2640 [ #  # ][ #  # ]:          0 :         if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
    2641                 :          0 :                 return;
    2642                 :            : 
    2643                 :          0 :         os_memset(&data, 0, sizeof(data));
    2644                 :          0 :         os_memcpy(data.connect_failed_reason.addr,
    2645                 :            :                   nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
    2646                 :            : 
    2647                 :          0 :         reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
    2648      [ #  #  # ]:          0 :         switch (reason) {
    2649                 :            :         case NL80211_CONN_FAIL_MAX_CLIENTS:
    2650                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
    2651                 :          0 :                 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
    2652                 :          0 :                 break;
    2653                 :            :         case NL80211_CONN_FAIL_BLOCKED_CLIENT:
    2654                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
    2655                 :            :                            " tried to connect",
    2656                 :          0 :                            MAC2STR(data.connect_failed_reason.addr));
    2657                 :          0 :                 data.connect_failed_reason.code = BLOCKED_CLIENT;
    2658                 :          0 :                 break;
    2659                 :            :         default:
    2660                 :          0 :                 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
    2661                 :            :                            "%u", reason);
    2662                 :          0 :                 return;
    2663                 :            :         }
    2664                 :            : 
    2665                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
    2666                 :            : }
    2667                 :            : 
    2668                 :            : 
    2669                 :          0 : static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
    2670                 :            :                                 struct nlattr **tb)
    2671                 :            : {
    2672                 :            :         union wpa_event_data data;
    2673                 :            :         enum nl80211_radar_event event_type;
    2674                 :            : 
    2675 [ #  # ][ #  # ]:          0 :         if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
    2676                 :          0 :                 return;
    2677                 :            : 
    2678                 :          0 :         os_memset(&data, 0, sizeof(data));
    2679                 :          0 :         data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
    2680                 :          0 :         event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
    2681                 :            : 
    2682                 :            :         /* Check HT params */
    2683         [ #  # ]:          0 :         if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
    2684                 :          0 :                 data.dfs_event.ht_enabled = 1;
    2685                 :          0 :                 data.dfs_event.chan_offset = 0;
    2686                 :            : 
    2687   [ #  #  #  #  :          0 :                 switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
                      # ]
    2688                 :            :                 case NL80211_CHAN_NO_HT:
    2689                 :          0 :                         data.dfs_event.ht_enabled = 0;
    2690                 :          0 :                         break;
    2691                 :            :                 case NL80211_CHAN_HT20:
    2692                 :          0 :                         break;
    2693                 :            :                 case NL80211_CHAN_HT40PLUS:
    2694                 :          0 :                         data.dfs_event.chan_offset = 1;
    2695                 :          0 :                         break;
    2696                 :            :                 case NL80211_CHAN_HT40MINUS:
    2697                 :          0 :                         data.dfs_event.chan_offset = -1;
    2698                 :          0 :                         break;
    2699                 :            :                 }
    2700                 :            :         }
    2701                 :            : 
    2702                 :            :         /* Get VHT params */
    2703         [ #  # ]:          0 :         if (tb[NL80211_ATTR_CHANNEL_WIDTH])
    2704                 :          0 :                 data.dfs_event.chan_width =
    2705                 :          0 :                         convert2width(nla_get_u32(
    2706                 :          0 :                                               tb[NL80211_ATTR_CHANNEL_WIDTH]));
    2707         [ #  # ]:          0 :         if (tb[NL80211_ATTR_CENTER_FREQ1])
    2708                 :          0 :                 data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
    2709         [ #  # ]:          0 :         if (tb[NL80211_ATTR_CENTER_FREQ2])
    2710                 :          0 :                 data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
    2711                 :            : 
    2712                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
    2713                 :            :                    data.dfs_event.freq, data.dfs_event.ht_enabled,
    2714                 :          0 :                    data.dfs_event.chan_offset, data.dfs_event.chan_width,
    2715                 :            :                    data.dfs_event.cf1, data.dfs_event.cf2);
    2716                 :            : 
    2717   [ #  #  #  #  :          0 :         switch (event_type) {
                      # ]
    2718                 :            :         case NL80211_RADAR_DETECTED:
    2719                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
    2720                 :          0 :                 break;
    2721                 :            :         case NL80211_RADAR_CAC_FINISHED:
    2722                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
    2723                 :          0 :                 break;
    2724                 :            :         case NL80211_RADAR_CAC_ABORTED:
    2725                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
    2726                 :          0 :                 break;
    2727                 :            :         case NL80211_RADAR_NOP_FINISHED:
    2728                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
    2729                 :          0 :                 break;
    2730                 :            :         default:
    2731                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
    2732                 :            :                            "received", event_type);
    2733                 :          0 :                 break;
    2734                 :            :         }
    2735                 :            : }
    2736                 :            : 
    2737                 :            : 
    2738                 :          0 : static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
    2739                 :            :                                    int wds)
    2740                 :            : {
    2741                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    2742                 :            :         union wpa_event_data event;
    2743                 :            : 
    2744         [ #  # ]:          0 :         if (!tb[NL80211_ATTR_MAC])
    2745                 :          0 :                 return;
    2746                 :            : 
    2747                 :          0 :         os_memset(&event, 0, sizeof(event));
    2748                 :          0 :         event.rx_from_unknown.bssid = bss->addr;
    2749                 :          0 :         event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
    2750                 :          0 :         event.rx_from_unknown.wds = wds;
    2751                 :            : 
    2752                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
    2753                 :            : }
    2754                 :            : 
    2755                 :            : 
    2756                 :          0 : static void qca_nl80211_avoid_freq(struct wpa_driver_nl80211_data *drv,
    2757                 :            :                                    const u8 *data, size_t len)
    2758                 :            : {
    2759                 :            :         u32 i, count;
    2760                 :            :         union wpa_event_data event;
    2761                 :          0 :         struct wpa_freq_range *range = NULL;
    2762                 :            :         const struct qca_avoid_freq_list *freq_range;
    2763                 :            : 
    2764                 :          0 :         freq_range = (const struct qca_avoid_freq_list *) data;
    2765         [ #  # ]:          0 :         if (len < sizeof(freq_range->count))
    2766                 :          0 :                 return;
    2767                 :            : 
    2768                 :          0 :         count = freq_range->count;
    2769         [ #  # ]:          0 :         if (len < sizeof(freq_range->count) +
    2770                 :          0 :             count * sizeof(struct qca_avoid_freq_range)) {
    2771                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignored too short avoid frequency list (len=%u)",
    2772                 :            :                            (unsigned int) len);
    2773                 :          0 :                 return;
    2774                 :            :         }
    2775                 :            : 
    2776         [ #  # ]:          0 :         if (count > 0) {
    2777                 :          0 :                 range = os_calloc(count, sizeof(struct wpa_freq_range));
    2778         [ #  # ]:          0 :                 if (range == NULL)
    2779                 :          0 :                         return;
    2780                 :            :         }
    2781                 :            : 
    2782                 :          0 :         os_memset(&event, 0, sizeof(event));
    2783         [ #  # ]:          0 :         for (i = 0; i < count; i++) {
    2784                 :          0 :                 unsigned int idx = event.freq_range.num;
    2785                 :          0 :                 range[idx].min = freq_range->range[i].start_freq;
    2786                 :          0 :                 range[idx].max = freq_range->range[i].end_freq;
    2787                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Avoid frequency range: %u-%u",
    2788                 :          0 :                            range[idx].min, range[idx].max);
    2789         [ #  # ]:          0 :                 if (range[idx].min > range[idx].max) {
    2790                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid frequency range");
    2791                 :          0 :                         continue;
    2792                 :            :                 }
    2793                 :          0 :                 event.freq_range.num++;
    2794                 :            :         }
    2795                 :          0 :         event.freq_range.range = range;
    2796                 :            : 
    2797                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_AVOID_FREQUENCIES, &event);
    2798                 :            : 
    2799                 :          0 :         os_free(range);
    2800                 :            : }
    2801                 :            : 
    2802                 :            : 
    2803                 :          0 : static void nl80211_vendor_event_qca(struct wpa_driver_nl80211_data *drv,
    2804                 :            :                                      u32 subcmd, u8 *data, size_t len)
    2805                 :            : {
    2806         [ #  # ]:          0 :         switch (subcmd) {
    2807                 :            :         case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
    2808                 :          0 :                 qca_nl80211_avoid_freq(drv, data, len);
    2809                 :          0 :                 break;
    2810                 :            :         default:
    2811                 :          0 :                 wpa_printf(MSG_DEBUG,
    2812                 :            :                            "nl80211: Ignore unsupported QCA vendor event %u",
    2813                 :            :                            subcmd);
    2814                 :          0 :                 break;
    2815                 :            :         }
    2816                 :          0 : }
    2817                 :            : 
    2818                 :            : 
    2819                 :          0 : static void nl80211_vendor_event(struct wpa_driver_nl80211_data *drv,
    2820                 :            :                                  struct nlattr **tb)
    2821                 :            : {
    2822                 :          0 :         u32 vendor_id, subcmd, wiphy = 0;
    2823                 :            :         int wiphy_idx;
    2824                 :          0 :         u8 *data = NULL;
    2825                 :          0 :         size_t len = 0;
    2826                 :            : 
    2827 [ #  # ][ #  # ]:          0 :         if (!tb[NL80211_ATTR_VENDOR_ID] ||
    2828                 :          0 :             !tb[NL80211_ATTR_VENDOR_SUBCMD])
    2829                 :          0 :                 return;
    2830                 :            : 
    2831                 :          0 :         vendor_id = nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]);
    2832                 :          0 :         subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
    2833                 :            : 
    2834         [ #  # ]:          0 :         if (tb[NL80211_ATTR_WIPHY])
    2835                 :          0 :                 wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
    2836                 :            : 
    2837                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Vendor event: wiphy=%u vendor_id=0x%x subcmd=%u",
    2838                 :            :                    wiphy, vendor_id, subcmd);
    2839                 :            : 
    2840         [ #  # ]:          0 :         if (tb[NL80211_ATTR_VENDOR_DATA]) {
    2841                 :          0 :                 data = nla_data(tb[NL80211_ATTR_VENDOR_DATA]);
    2842                 :          0 :                 len = nla_len(tb[NL80211_ATTR_VENDOR_DATA]);
    2843                 :          0 :                 wpa_hexdump(MSG_MSGDUMP, "nl80211: Vendor data", data, len);
    2844                 :            :         }
    2845                 :            : 
    2846                 :          0 :         wiphy_idx = nl80211_get_wiphy_index(drv->first_bss);
    2847 [ #  # ][ #  # ]:          0 :         if (wiphy_idx >= 0 && wiphy_idx != (int) wiphy) {
    2848                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore vendor event for foreign wiphy %u (own: %d)",
    2849                 :            :                            wiphy, wiphy_idx);
    2850                 :          0 :                 return;
    2851                 :            :         }
    2852                 :            : 
    2853         [ #  # ]:          0 :         switch (vendor_id) {
    2854                 :            :         case OUI_QCA:
    2855                 :          0 :                 nl80211_vendor_event_qca(drv, subcmd, data, len);
    2856                 :          0 :                 break;
    2857                 :            :         default:
    2858                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event");
    2859                 :          0 :                 break;
    2860                 :            :         }
    2861                 :            : }
    2862                 :            : 
    2863                 :            : 
    2864                 :      15725 : static void do_process_drv_event(struct i802_bss *bss, int cmd,
    2865                 :            :                                  struct nlattr **tb)
    2866                 :            : {
    2867                 :      15725 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    2868                 :            :         union wpa_event_data data;
    2869                 :            : 
    2870                 :      15725 :         wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
    2871                 :      15725 :                    cmd, nl80211_command_to_string(cmd), bss->ifname);
    2872                 :            : 
    2873 [ #  # ][ -  + ]:      15725 :         if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
    2874         [ #  # ]:          0 :             (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
    2875                 :            :              cmd == NL80211_CMD_SCAN_ABORTED)) {
    2876                 :          0 :                 wpa_driver_nl80211_set_mode(drv->first_bss,
    2877                 :            :                                             drv->ap_scan_as_station);
    2878                 :          0 :                 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
    2879                 :            :         }
    2880                 :            : 
    2881   [ +  -  -  +  :      15725 :         switch (cmd) {
          -  -  +  +  -  
          +  -  +  +  +  
          -  +  -  +  +  
          -  -  -  -  -  
             -  -  +  -  
                      - ]
    2882                 :            :         case NL80211_CMD_TRIGGER_SCAN:
    2883                 :       1381 :                 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
    2884                 :       1381 :                 drv->scan_state = SCAN_STARTED;
    2885                 :       1381 :                 wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
    2886                 :       1381 :                 break;
    2887                 :            :         case NL80211_CMD_START_SCHED_SCAN:
    2888                 :          0 :                 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
    2889                 :          0 :                 drv->scan_state = SCHED_SCAN_STARTED;
    2890                 :          0 :                 break;
    2891                 :            :         case NL80211_CMD_SCHED_SCAN_STOPPED:
    2892                 :          0 :                 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
    2893                 :          0 :                 drv->scan_state = SCHED_SCAN_STOPPED;
    2894                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
    2895                 :          0 :                 break;
    2896                 :            :         case NL80211_CMD_NEW_SCAN_RESULTS:
    2897                 :       1376 :                 wpa_dbg(drv->ctx, MSG_DEBUG,
    2898                 :            :                         "nl80211: New scan results available");
    2899                 :       1376 :                 drv->scan_state = SCAN_COMPLETED;
    2900                 :       1376 :                 drv->scan_complete_events = 1;
    2901                 :       1376 :                 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
    2902                 :            :                                      drv->ctx);
    2903                 :       1376 :                 send_scan_event(drv, 0, tb);
    2904                 :       1376 :                 break;
    2905                 :            :         case NL80211_CMD_SCHED_SCAN_RESULTS:
    2906                 :          0 :                 wpa_dbg(drv->ctx, MSG_DEBUG,
    2907                 :            :                         "nl80211: New sched scan results available");
    2908                 :          0 :                 drv->scan_state = SCHED_SCAN_RESULTS;
    2909                 :          0 :                 send_scan_event(drv, 0, tb);
    2910                 :          0 :                 break;
    2911                 :            :         case NL80211_CMD_SCAN_ABORTED:
    2912                 :          0 :                 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
    2913                 :          0 :                 drv->scan_state = SCAN_ABORTED;
    2914                 :            :                 /*
    2915                 :            :                  * Need to indicate that scan results are available in order
    2916                 :            :                  * not to make wpa_supplicant stop its scanning.
    2917                 :            :                  */
    2918                 :          0 :                 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
    2919                 :            :                                      drv->ctx);
    2920                 :          0 :                 send_scan_event(drv, 1, tb);
    2921                 :          0 :                 break;
    2922                 :            :         case NL80211_CMD_AUTHENTICATE:
    2923                 :            :         case NL80211_CMD_ASSOCIATE:
    2924                 :            :         case NL80211_CMD_DEAUTHENTICATE:
    2925                 :            :         case NL80211_CMD_DISASSOCIATE:
    2926                 :            :         case NL80211_CMD_FRAME_TX_STATUS:
    2927                 :            :         case NL80211_CMD_UNPROT_DEAUTHENTICATE:
    2928                 :            :         case NL80211_CMD_UNPROT_DISASSOCIATE:
    2929                 :       5143 :                 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
    2930                 :      10286 :                            tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
    2931                 :      10286 :                            tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
    2932                 :       5143 :                            tb[NL80211_ATTR_COOKIE],
    2933                 :       5143 :                            tb[NL80211_ATTR_RX_SIGNAL_DBM]);
    2934                 :       5143 :                 break;
    2935                 :            :         case NL80211_CMD_CONNECT:
    2936                 :            :         case NL80211_CMD_ROAM:
    2937                 :        674 :                 mlme_event_connect(drv, cmd,
    2938                 :        674 :                                    tb[NL80211_ATTR_STATUS_CODE],
    2939                 :        674 :                                    tb[NL80211_ATTR_MAC],
    2940                 :        674 :                                    tb[NL80211_ATTR_REQ_IE],
    2941                 :        674 :                                    tb[NL80211_ATTR_RESP_IE]);
    2942                 :        674 :                 break;
    2943                 :            :         case NL80211_CMD_CH_SWITCH_NOTIFY:
    2944                 :          0 :                 mlme_event_ch_switch(drv,
    2945                 :          0 :                                      tb[NL80211_ATTR_IFINDEX],
    2946                 :          0 :                                      tb[NL80211_ATTR_WIPHY_FREQ],
    2947                 :          0 :                                      tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
    2948                 :          0 :                                      tb[NL80211_ATTR_CHANNEL_WIDTH],
    2949                 :          0 :                                      tb[NL80211_ATTR_CENTER_FREQ1],
    2950                 :          0 :                                      tb[NL80211_ATTR_CENTER_FREQ2]);
    2951                 :          0 :                 break;
    2952                 :            :         case NL80211_CMD_DISCONNECT:
    2953                 :        661 :                 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
    2954                 :        661 :                                       tb[NL80211_ATTR_MAC],
    2955                 :        661 :                                       tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
    2956                 :        661 :                 break;
    2957                 :            :         case NL80211_CMD_MICHAEL_MIC_FAILURE:
    2958                 :          0 :                 mlme_event_michael_mic_failure(bss, tb);
    2959                 :          0 :                 break;
    2960                 :            :         case NL80211_CMD_JOIN_IBSS:
    2961                 :          5 :                 mlme_event_join_ibss(drv, tb);
    2962                 :          5 :                 break;
    2963                 :            :         case NL80211_CMD_REMAIN_ON_CHANNEL:
    2964                 :        775 :                 mlme_event_remain_on_channel(drv, 0, tb);
    2965                 :        775 :                 break;
    2966                 :            :         case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
    2967                 :        775 :                 mlme_event_remain_on_channel(drv, 1, tb);
    2968                 :        775 :                 break;
    2969                 :            :         case NL80211_CMD_NOTIFY_CQM:
    2970                 :          0 :                 nl80211_cqm_event(drv, tb);
    2971                 :          0 :                 break;
    2972                 :            :         case NL80211_CMD_REG_CHANGE:
    2973                 :       2053 :                 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
    2974         [ -  + ]:       2053 :                 if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
    2975                 :          0 :                         break;
    2976                 :       2053 :                 os_memset(&data, 0, sizeof(data));
    2977   [ +  -  -  -  :       2053 :                 switch (nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])) {
                      - ]
    2978                 :            :                 case NL80211_REGDOM_SET_BY_CORE:
    2979                 :       2053 :                         data.channel_list_changed.initiator =
    2980                 :            :                                 REGDOM_SET_BY_CORE;
    2981                 :       2053 :                         break;
    2982                 :            :                 case NL80211_REGDOM_SET_BY_USER:
    2983                 :          0 :                         data.channel_list_changed.initiator =
    2984                 :            :                                 REGDOM_SET_BY_USER;
    2985                 :          0 :                         break;
    2986                 :            :                 case NL80211_REGDOM_SET_BY_DRIVER:
    2987                 :          0 :                         data.channel_list_changed.initiator =
    2988                 :            :                                 REGDOM_SET_BY_DRIVER;
    2989                 :          0 :                         break;
    2990                 :            :                 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
    2991                 :          0 :                         data.channel_list_changed.initiator =
    2992                 :            :                                 REGDOM_SET_BY_COUNTRY_IE;
    2993                 :          0 :                         break;
    2994                 :            :                 default:
    2995                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Unknown reg change initiator %d received",
    2996                 :          0 :                                    nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]));
    2997                 :          0 :                         break;
    2998                 :            :                 }
    2999                 :       2053 :                 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
    3000                 :            :                                      &data);
    3001                 :       2053 :                 break;
    3002                 :            :         case NL80211_CMD_REG_BEACON_HINT:
    3003                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
    3004                 :          0 :                 os_memset(&data, 0, sizeof(data));
    3005                 :          0 :                 data.channel_list_changed.initiator = REGDOM_BEACON_HINT;
    3006                 :          0 :                 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
    3007                 :            :                                      &data);
    3008                 :          0 :                 break;
    3009                 :            :         case NL80211_CMD_NEW_STATION:
    3010                 :       1423 :                 nl80211_new_station_event(drv, tb);
    3011                 :       1423 :                 break;
    3012                 :            :         case NL80211_CMD_DEL_STATION:
    3013                 :       1388 :                 nl80211_del_station_event(drv, tb);
    3014                 :       1388 :                 break;
    3015                 :            :         case NL80211_CMD_SET_REKEY_OFFLOAD:
    3016                 :          0 :                 nl80211_rekey_offload_event(drv, tb);
    3017                 :          0 :                 break;
    3018                 :            :         case NL80211_CMD_PMKSA_CANDIDATE:
    3019                 :          0 :                 nl80211_pmksa_candidate_event(drv, tb);
    3020                 :          0 :                 break;
    3021                 :            :         case NL80211_CMD_PROBE_CLIENT:
    3022                 :          0 :                 nl80211_client_probe_event(drv, tb);
    3023                 :          0 :                 break;
    3024                 :            :         case NL80211_CMD_TDLS_OPER:
    3025                 :          0 :                 nl80211_tdls_oper_event(drv, tb);
    3026                 :          0 :                 break;
    3027                 :            :         case NL80211_CMD_CONN_FAILED:
    3028                 :          0 :                 nl80211_connect_failed_event(drv, tb);
    3029                 :          0 :                 break;
    3030                 :            :         case NL80211_CMD_FT_EVENT:
    3031                 :          0 :                 mlme_event_ft_event(drv, tb);
    3032                 :          0 :                 break;
    3033                 :            :         case NL80211_CMD_RADAR_DETECT:
    3034                 :          0 :                 nl80211_radar_event(drv, tb);
    3035                 :          0 :                 break;
    3036                 :            :         case NL80211_CMD_STOP_AP:
    3037                 :         71 :                 nl80211_stop_ap(drv, tb);
    3038                 :         71 :                 break;
    3039                 :            :         case NL80211_CMD_VENDOR:
    3040                 :          0 :                 nl80211_vendor_event(drv, tb);
    3041                 :          0 :                 break;
    3042                 :            :         default:
    3043                 :          0 :                 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
    3044                 :            :                         "(cmd=%d)", cmd);
    3045                 :          0 :                 break;
    3046                 :            :         }
    3047                 :      15725 : }
    3048                 :            : 
    3049                 :            : 
    3050                 :          0 : static int process_drv_event(struct nl_msg *msg, void *arg)
    3051                 :            : {
    3052                 :          0 :         struct wpa_driver_nl80211_data *drv = arg;
    3053                 :          0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    3054                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    3055                 :            :         struct i802_bss *bss;
    3056                 :          0 :         int ifidx = -1;
    3057                 :            : 
    3058                 :          0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    3059                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    3060                 :            : 
    3061         [ #  # ]:          0 :         if (tb[NL80211_ATTR_IFINDEX]) {
    3062                 :          0 :                 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
    3063                 :            : 
    3064         [ #  # ]:          0 :                 for (bss = drv->first_bss; bss; bss = bss->next)
    3065 [ #  # ][ #  # ]:          0 :                         if (ifidx == -1 || ifidx == bss->ifindex) {
    3066                 :          0 :                                 do_process_drv_event(bss, gnlh->cmd, tb);
    3067                 :          0 :                                 return NL_SKIP;
    3068                 :            :                         }
    3069                 :          0 :                 wpa_printf(MSG_DEBUG,
    3070                 :            :                            "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
    3071                 :          0 :                            gnlh->cmd, ifidx);
    3072         [ #  # ]:          0 :         } else if (tb[NL80211_ATTR_WDEV]) {
    3073                 :          0 :                 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
    3074                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
    3075         [ #  # ]:          0 :                 for (bss = drv->first_bss; bss; bss = bss->next) {
    3076 [ #  # ][ #  # ]:          0 :                         if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
    3077                 :          0 :                                 do_process_drv_event(bss, gnlh->cmd, tb);
    3078                 :          0 :                                 return NL_SKIP;
    3079                 :            :                         }
    3080                 :            :                 }
    3081                 :          0 :                 wpa_printf(MSG_DEBUG,
    3082                 :            :                            "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
    3083                 :          0 :                            gnlh->cmd, (long long unsigned int) wdev_id);
    3084                 :            :         }
    3085                 :            : 
    3086                 :          0 :         return NL_SKIP;
    3087                 :            : }
    3088                 :            : 
    3089                 :            : 
    3090                 :      73502 : static int process_global_event(struct nl_msg *msg, void *arg)
    3091                 :            : {
    3092                 :      73502 :         struct nl80211_global *global = arg;
    3093                 :      73502 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    3094                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    3095                 :            :         struct wpa_driver_nl80211_data *drv, *tmp;
    3096                 :      73502 :         int ifidx = -1;
    3097                 :            :         struct i802_bss *bss;
    3098                 :      73502 :         u64 wdev_id = 0;
    3099                 :      73502 :         int wdev_id_set = 0;
    3100                 :            : 
    3101                 :      73502 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    3102                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    3103                 :            : 
    3104         [ +  + ]:      73502 :         if (tb[NL80211_ATTR_IFINDEX])
    3105                 :      70809 :                 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
    3106         [ -  + ]:       2693 :         else if (tb[NL80211_ATTR_WDEV]) {
    3107                 :          0 :                 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
    3108                 :          0 :                 wdev_id_set = 1;
    3109                 :            :         }
    3110                 :            : 
    3111         [ +  + ]:     113172 :         dl_list_for_each_safe(drv, tmp, &global->interfaces,
    3112                 :            :                               struct wpa_driver_nl80211_data, list) {
    3113         [ +  + ]:      95893 :                 for (bss = drv->first_bss; bss; bss = bss->next) {
    3114 [ +  + ][ -  + ]:      56223 :                         if ((ifidx == -1 && !wdev_id_set) ||
                 [ +  + ]
    3115         [ -  + ]:      40498 :                             ifidx == bss->ifindex ||
    3116 [ #  # ][ #  # ]:          0 :                             (wdev_id_set && bss->wdev_id_set &&
    3117                 :          0 :                              wdev_id == bss->wdev_id)) {
    3118                 :      15725 :                                 do_process_drv_event(bss, gnlh->cmd, tb);
    3119                 :      15725 :                                 return NL_SKIP;
    3120                 :            :                         }
    3121                 :            :                 }
    3122                 :            :         }
    3123                 :            : 
    3124                 :      73502 :         return NL_SKIP;
    3125                 :            : }
    3126                 :            : 
    3127                 :            : 
    3128                 :       4406 : static int process_bss_event(struct nl_msg *msg, void *arg)
    3129                 :            : {
    3130                 :       4406 :         struct i802_bss *bss = arg;
    3131                 :       4406 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    3132                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    3133                 :            : 
    3134                 :       4406 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    3135                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    3136                 :            : 
    3137                 :       4406 :         wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
    3138                 :       8812 :                    gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
    3139                 :       4406 :                    bss->ifname);
    3140                 :            : 
    3141   [ +  -  -  - ]:       4406 :         switch (gnlh->cmd) {
    3142                 :            :         case NL80211_CMD_FRAME:
    3143                 :            :         case NL80211_CMD_FRAME_TX_STATUS:
    3144                 :       4406 :                 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
    3145                 :            :                            tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
    3146                 :            :                            tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
    3147                 :            :                            tb[NL80211_ATTR_COOKIE],
    3148                 :            :                            tb[NL80211_ATTR_RX_SIGNAL_DBM]);
    3149                 :       4406 :                 break;
    3150                 :            :         case NL80211_CMD_UNEXPECTED_FRAME:
    3151                 :          0 :                 nl80211_spurious_frame(bss, tb, 0);
    3152                 :          0 :                 break;
    3153                 :            :         case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
    3154                 :          0 :                 nl80211_spurious_frame(bss, tb, 1);
    3155                 :          0 :                 break;
    3156                 :            :         default:
    3157                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
    3158                 :          0 :                            "(cmd=%d)", gnlh->cmd);
    3159                 :          0 :                 break;
    3160                 :            :         }
    3161                 :            : 
    3162                 :       4406 :         return NL_SKIP;
    3163                 :            : }
    3164                 :            : 
    3165                 :            : 
    3166                 :      77866 : static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
    3167                 :            :                                              void *handle)
    3168                 :            : {
    3169                 :      77866 :         struct nl_cb *cb = eloop_ctx;
    3170                 :            :         int res;
    3171                 :            : 
    3172                 :      77866 :         wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
    3173                 :            : 
    3174                 :      77866 :         res = nl_recvmsgs(handle, cb);
    3175         [ -  + ]:      77866 :         if (res) {
    3176                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
    3177                 :            :                            __func__, res);
    3178                 :            :         }
    3179                 :      77866 : }
    3180                 :            : 
    3181                 :            : 
    3182                 :            : /**
    3183                 :            :  * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
    3184                 :            :  * @priv: driver_nl80211 private data
    3185                 :            :  * @alpha2_arg: country to which to switch to
    3186                 :            :  * Returns: 0 on success, -1 on failure
    3187                 :            :  *
    3188                 :            :  * This asks nl80211 to set the regulatory domain for given
    3189                 :            :  * country ISO / IEC alpha2.
    3190                 :            :  */
    3191                 :          0 : static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
    3192                 :            : {
    3193                 :          0 :         struct i802_bss *bss = priv;
    3194                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    3195                 :            :         char alpha2[3];
    3196                 :            :         struct nl_msg *msg;
    3197                 :            : 
    3198                 :          0 :         msg = nlmsg_alloc();
    3199         [ #  # ]:          0 :         if (!msg)
    3200                 :          0 :                 return -ENOMEM;
    3201                 :            : 
    3202                 :          0 :         alpha2[0] = alpha2_arg[0];
    3203                 :          0 :         alpha2[1] = alpha2_arg[1];
    3204                 :          0 :         alpha2[2] = '\0';
    3205                 :            : 
    3206                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
    3207                 :            : 
    3208         [ #  # ]:          0 :         NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
    3209         [ #  # ]:          0 :         if (send_and_recv_msgs(drv, msg, NULL, NULL))
    3210                 :          0 :                 return -EINVAL;
    3211                 :          0 :         return 0;
    3212                 :            : nla_put_failure:
    3213                 :          0 :         nlmsg_free(msg);
    3214                 :          0 :         return -EINVAL;
    3215                 :            : }
    3216                 :            : 
    3217                 :            : 
    3218                 :          0 : static int nl80211_get_country(struct nl_msg *msg, void *arg)
    3219                 :            : {
    3220                 :          0 :         char *alpha2 = arg;
    3221                 :            :         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
    3222                 :          0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    3223                 :            : 
    3224                 :          0 :         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    3225                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    3226         [ #  # ]:          0 :         if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
    3227                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
    3228                 :          0 :                 return NL_SKIP;
    3229                 :            :         }
    3230                 :          0 :         os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
    3231                 :          0 :         return NL_SKIP;
    3232                 :            : }
    3233                 :            : 
    3234                 :            : 
    3235                 :          0 : static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
    3236                 :            : {
    3237                 :          0 :         struct i802_bss *bss = priv;
    3238                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    3239                 :            :         struct nl_msg *msg;
    3240                 :            :         int ret;
    3241                 :            : 
    3242                 :          0 :         msg = nlmsg_alloc();
    3243         [ #  # ]:          0 :         if (!msg)
    3244                 :          0 :                 return -ENOMEM;
    3245                 :            : 
    3246                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
    3247                 :          0 :         alpha2[0] = '\0';
    3248                 :          0 :         ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
    3249         [ #  # ]:          0 :         if (!alpha2[0])
    3250                 :          0 :                 ret = -1;
    3251                 :            : 
    3252                 :          0 :         return ret;
    3253                 :            : }
    3254                 :            : 
    3255                 :            : 
    3256                 :       2508 : static int protocol_feature_handler(struct nl_msg *msg, void *arg)
    3257                 :            : {
    3258                 :       2508 :         u32 *feat = arg;
    3259                 :            :         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
    3260                 :       2508 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    3261                 :            : 
    3262                 :       2508 :         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    3263                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    3264                 :            : 
    3265         [ +  - ]:       2508 :         if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
    3266                 :       2508 :                 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
    3267                 :            : 
    3268                 :       2508 :         return NL_SKIP;
    3269                 :            : }
    3270                 :            : 
    3271                 :            : 
    3272                 :       2508 : static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
    3273                 :            : {
    3274                 :       2508 :         u32 feat = 0;
    3275                 :            :         struct nl_msg *msg;
    3276                 :            : 
    3277                 :       2508 :         msg = nlmsg_alloc();
    3278         [ -  + ]:       2508 :         if (!msg)
    3279                 :          0 :                 goto nla_put_failure;
    3280                 :            : 
    3281                 :       2508 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
    3282         [ +  - ]:       2508 :         if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
    3283                 :       2508 :                 return feat;
    3284                 :            : 
    3285                 :          0 :         msg = NULL;
    3286                 :            : nla_put_failure:
    3287                 :          0 :         nlmsg_free(msg);
    3288                 :       2508 :         return 0;
    3289                 :            : }
    3290                 :            : 
    3291                 :            : 
    3292                 :            : struct wiphy_info_data {
    3293                 :            :         struct wpa_driver_nl80211_data *drv;
    3294                 :            :         struct wpa_driver_capa *capa;
    3295                 :            : 
    3296                 :            :         unsigned int num_multichan_concurrent;
    3297                 :            : 
    3298                 :            :         unsigned int error:1;
    3299                 :            :         unsigned int device_ap_sme:1;
    3300                 :            :         unsigned int poll_command_supported:1;
    3301                 :            :         unsigned int data_tx_status:1;
    3302                 :            :         unsigned int monitor_supported:1;
    3303                 :            :         unsigned int auth_supported:1;
    3304                 :            :         unsigned int connect_supported:1;
    3305                 :            :         unsigned int p2p_go_supported:1;
    3306                 :            :         unsigned int p2p_client_supported:1;
    3307                 :            :         unsigned int p2p_concurrent:1;
    3308                 :            :         unsigned int channel_switch_supported:1;
    3309                 :            :         unsigned int set_qos_map_supported:1;
    3310                 :            : };
    3311                 :            : 
    3312                 :            : 
    3313                 :          0 : static unsigned int probe_resp_offload_support(int supp_protocols)
    3314                 :            : {
    3315                 :          0 :         unsigned int prot = 0;
    3316                 :            : 
    3317         [ #  # ]:          0 :         if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
    3318                 :          0 :                 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
    3319         [ #  # ]:          0 :         if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
    3320                 :          0 :                 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
    3321         [ #  # ]:          0 :         if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
    3322                 :          0 :                 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
    3323         [ #  # ]:          0 :         if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
    3324                 :          0 :                 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
    3325                 :            : 
    3326                 :          0 :         return prot;
    3327                 :            : }
    3328                 :            : 
    3329                 :            : 
    3330                 :      19663 : static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
    3331                 :            :                                          struct nlattr *tb)
    3332                 :            : {
    3333                 :            :         struct nlattr *nl_mode;
    3334                 :            :         int i;
    3335                 :            : 
    3336         [ +  + ]:      19663 :         if (tb == NULL)
    3337                 :      19663 :                 return;
    3338                 :            : 
    3339         [ +  + ]:       3339 :         nla_for_each_nested(nl_mode, tb, i) {
    3340   [ +  +  +  +  :       2968 :                 switch (nla_type(nl_mode)) {
                +  +  + ]
    3341                 :            :                 case NL80211_IFTYPE_AP:
    3342                 :        371 :                         info->capa->flags |= WPA_DRIVER_FLAGS_AP;
    3343                 :        371 :                         break;
    3344                 :            :                 case NL80211_IFTYPE_ADHOC:
    3345                 :        371 :                         info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
    3346                 :        371 :                         break;
    3347                 :            :                 case NL80211_IFTYPE_P2P_DEVICE:
    3348                 :        371 :                         info->capa->flags |=
    3349                 :            :                                 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
    3350                 :        371 :                         break;
    3351                 :            :                 case NL80211_IFTYPE_P2P_GO:
    3352                 :        371 :                         info->p2p_go_supported = 1;
    3353                 :        371 :                         break;
    3354                 :            :                 case NL80211_IFTYPE_P2P_CLIENT:
    3355                 :        371 :                         info->p2p_client_supported = 1;
    3356                 :        371 :                         break;
    3357                 :            :                 case NL80211_IFTYPE_MONITOR:
    3358                 :        371 :                         info->monitor_supported = 1;
    3359                 :        371 :                         break;
    3360                 :            :                 }
    3361                 :            :         }
    3362                 :            : }
    3363                 :            : 
    3364                 :            : 
    3365                 :        371 : static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
    3366                 :            :                                          struct nlattr *nl_combi)
    3367                 :            : {
    3368                 :            :         struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
    3369                 :            :         struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
    3370                 :            :         struct nlattr *nl_limit, *nl_mode;
    3371                 :            :         int err, rem_limit, rem_mode;
    3372                 :        371 :         int combination_has_p2p = 0, combination_has_mgd = 0;
    3373                 :            :         static struct nla_policy
    3374                 :            :         iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
    3375                 :            :                 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
    3376                 :            :                 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
    3377                 :            :                 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
    3378                 :            :                 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
    3379                 :            :                 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
    3380                 :            :         },
    3381                 :            :         iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
    3382                 :            :                 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
    3383                 :            :                 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
    3384                 :            :         };
    3385                 :            : 
    3386                 :        371 :         err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
    3387                 :            :                                nl_combi, iface_combination_policy);
    3388 [ +  - ][ +  - ]:        371 :         if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
                 [ +  - ]
    3389         [ -  + ]:        371 :             !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
    3390                 :        371 :             !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
    3391                 :          0 :                 return 0; /* broken combination */
    3392                 :            : 
    3393         [ +  - ]:        371 :         if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
    3394                 :        371 :                 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
    3395                 :            : 
    3396         [ +  - ]:        742 :         nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
    3397                 :            :                             rem_limit) {
    3398                 :        742 :                 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
    3399                 :            :                                        nl_limit, iface_limit_policy);
    3400 [ -  + ][ +  - ]:        742 :                 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
    3401                 :          0 :                         return 0; /* broken combination */
    3402                 :            : 
    3403         [ +  + ]:       2597 :                 nla_for_each_nested(nl_mode,
    3404                 :            :                                     tb_limit[NL80211_IFACE_LIMIT_TYPES],
    3405                 :            :                                     rem_mode) {
    3406                 :       1855 :                         int ift = nla_type(nl_mode);
    3407 [ +  + ][ +  + ]:       1855 :                         if (ift == NL80211_IFTYPE_P2P_GO ||
    3408                 :            :                             ift == NL80211_IFTYPE_P2P_CLIENT)
    3409                 :        742 :                                 combination_has_p2p = 1;
    3410         [ +  + ]:       1855 :                         if (ift == NL80211_IFTYPE_STATION)
    3411                 :        371 :                                 combination_has_mgd = 1;
    3412                 :            :                 }
    3413 [ +  + ][ +  - ]:        742 :                 if (combination_has_p2p && combination_has_mgd)
    3414                 :        371 :                         break;
    3415                 :            :         }
    3416                 :            : 
    3417 [ +  - ][ +  - ]:        371 :         if (combination_has_p2p && combination_has_mgd) {
    3418                 :        371 :                 info->p2p_concurrent = 1;
    3419                 :        371 :                 info->num_multichan_concurrent =
    3420                 :        371 :                         nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
    3421                 :        371 :                 return 1;
    3422                 :            :         }
    3423                 :            : 
    3424                 :        371 :         return 0;
    3425                 :            : }
    3426                 :            : 
    3427                 :            : 
    3428                 :      19663 : static void wiphy_info_iface_comb(struct wiphy_info_data *info,
    3429                 :            :                                   struct nlattr *tb)
    3430                 :            : {
    3431                 :            :         struct nlattr *nl_combi;
    3432                 :            :         int rem_combi;
    3433                 :            : 
    3434         [ +  + ]:      19663 :         if (tb == NULL)
    3435                 :      19663 :                 return;
    3436                 :            : 
    3437         [ +  - ]:        371 :         nla_for_each_nested(nl_combi, tb, rem_combi) {
    3438         [ +  - ]:        371 :                 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
    3439                 :        371 :                         break;
    3440                 :            :         }
    3441                 :            : }
    3442                 :            : 
    3443                 :            : 
    3444                 :      19663 : static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
    3445                 :            :                                  struct nlattr *tb)
    3446                 :            : {
    3447                 :            :         struct nlattr *nl_cmd;
    3448                 :            :         int i;
    3449                 :            : 
    3450         [ +  + ]:      19663 :         if (tb == NULL)
    3451                 :      19663 :                 return;
    3452                 :            : 
    3453         [ +  + ]:      10759 :         nla_for_each_nested(nl_cmd, tb, i) {
    3454   [ +  +  -  +  :      10388 :                 switch (nla_get_u32(nl_cmd)) {
                -  +  + ]
    3455                 :            :                 case NL80211_CMD_AUTHENTICATE:
    3456                 :        371 :                         info->auth_supported = 1;
    3457                 :        371 :                         break;
    3458                 :            :                 case NL80211_CMD_CONNECT:
    3459                 :        371 :                         info->connect_supported = 1;
    3460                 :        371 :                         break;
    3461                 :            :                 case NL80211_CMD_START_SCHED_SCAN:
    3462                 :          0 :                         info->capa->sched_scan_supported = 1;
    3463                 :          0 :                         break;
    3464                 :            :                 case NL80211_CMD_PROBE_CLIENT:
    3465                 :        371 :                         info->poll_command_supported = 1;
    3466                 :        371 :                         break;
    3467                 :            :                 case NL80211_CMD_CHANNEL_SWITCH:
    3468                 :          0 :                         info->channel_switch_supported = 1;
    3469                 :          0 :                         break;
    3470                 :            :                 case NL80211_CMD_SET_QOS_MAP:
    3471                 :        371 :                         info->set_qos_map_supported = 1;
    3472                 :        371 :                         break;
    3473                 :            :                 }
    3474                 :            :         }
    3475                 :            : }
    3476                 :            : 
    3477                 :            : 
    3478                 :      19663 : static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
    3479                 :            :                                      struct nlattr *tb)
    3480                 :            : {
    3481                 :            :         int i, num;
    3482                 :            :         u32 *ciphers;
    3483                 :            : 
    3484         [ +  + ]:      19663 :         if (tb == NULL)
    3485                 :      19663 :                 return;
    3486                 :            : 
    3487                 :        371 :         num = nla_len(tb) / sizeof(u32);
    3488                 :        371 :         ciphers = nla_data(tb);
    3489         [ +  + ]:       2226 :         for (i = 0; i < num; i++) {
    3490                 :       1855 :                 u32 c = ciphers[i];
    3491                 :            : 
    3492                 :       1855 :                 wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
    3493                 :       1855 :                            c >> 24, (c >> 16) & 0xff,
    3494                 :       1855 :                            (c >> 8) & 0xff, c & 0xff);
    3495   [ -  -  +  -  :       1855 :                 switch (c) {
          +  +  +  +  -  
             -  -  -  - ]
    3496                 :            :                 case WLAN_CIPHER_SUITE_CCMP_256:
    3497                 :          0 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
    3498                 :          0 :                         break;
    3499                 :            :                 case WLAN_CIPHER_SUITE_GCMP_256:
    3500                 :          0 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
    3501                 :          0 :                         break;
    3502                 :            :                 case WLAN_CIPHER_SUITE_CCMP:
    3503                 :        371 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
    3504                 :        371 :                         break;
    3505                 :            :                 case WLAN_CIPHER_SUITE_GCMP:
    3506                 :          0 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
    3507                 :          0 :                         break;
    3508                 :            :                 case WLAN_CIPHER_SUITE_TKIP:
    3509                 :        371 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
    3510                 :        371 :                         break;
    3511                 :            :                 case WLAN_CIPHER_SUITE_WEP104:
    3512                 :        371 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
    3513                 :        371 :                         break;
    3514                 :            :                 case WLAN_CIPHER_SUITE_WEP40:
    3515                 :        371 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
    3516                 :        371 :                         break;
    3517                 :            :                 case WLAN_CIPHER_SUITE_AES_CMAC:
    3518                 :        371 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
    3519                 :        371 :                         break;
    3520                 :            :                 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
    3521                 :          0 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
    3522                 :          0 :                         break;
    3523                 :            :                 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
    3524                 :          0 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
    3525                 :          0 :                         break;
    3526                 :            :                 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
    3527                 :          0 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
    3528                 :          0 :                         break;
    3529                 :            :                 case WLAN_CIPHER_SUITE_NO_GROUP_ADDR:
    3530                 :          0 :                         info->capa->enc |= WPA_DRIVER_CAPA_ENC_GTK_NOT_USED;
    3531                 :          0 :                         break;
    3532                 :            :                 }
    3533                 :            :         }
    3534                 :            : }
    3535                 :            : 
    3536                 :            : 
    3537                 :      19663 : static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
    3538                 :            :                                struct nlattr *tb)
    3539                 :            : {
    3540         [ +  + ]:      19663 :         if (tb)
    3541                 :        371 :                 capa->max_remain_on_chan = nla_get_u32(tb);
    3542                 :      19663 : }
    3543                 :            : 
    3544                 :            : 
    3545                 :      19663 : static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
    3546                 :            :                             struct nlattr *ext_setup)
    3547                 :            : {
    3548         [ +  + ]:      19663 :         if (tdls == NULL)
    3549                 :      19663 :                 return;
    3550                 :            : 
    3551                 :        371 :         wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
    3552                 :        371 :         capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
    3553                 :            : 
    3554         [ +  - ]:        371 :         if (ext_setup) {
    3555                 :        371 :                 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
    3556                 :        371 :                 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
    3557                 :            :         }
    3558                 :            : }
    3559                 :            : 
    3560                 :            : 
    3561                 :      19663 : static void wiphy_info_feature_flags(struct wiphy_info_data *info,
    3562                 :            :                                      struct nlattr *tb)
    3563                 :            : {
    3564                 :            :         u32 flags;
    3565                 :      19663 :         struct wpa_driver_capa *capa = info->capa;
    3566                 :            : 
    3567         [ +  + ]:      19663 :         if (tb == NULL)
    3568                 :      19663 :                 return;
    3569                 :            : 
    3570                 :        371 :         flags = nla_get_u32(tb);
    3571                 :            : 
    3572         [ +  - ]:        371 :         if (flags & NL80211_FEATURE_SK_TX_STATUS)
    3573                 :        371 :                 info->data_tx_status = 1;
    3574                 :            : 
    3575         [ -  + ]:        371 :         if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
    3576                 :          0 :                 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
    3577                 :            : 
    3578         [ +  - ]:        371 :         if (flags & NL80211_FEATURE_SAE)
    3579                 :        371 :                 capa->flags |= WPA_DRIVER_FLAGS_SAE;
    3580                 :            : 
    3581         [ -  + ]:        371 :         if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
    3582                 :          0 :                 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
    3583                 :            : }
    3584                 :            : 
    3585                 :            : 
    3586                 :      19663 : static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
    3587                 :            :                                           struct nlattr *tb)
    3588                 :            : {
    3589                 :            :         u32 protocols;
    3590                 :            : 
    3591         [ +  - ]:      19663 :         if (tb == NULL)
    3592                 :      19663 :                 return;
    3593                 :            : 
    3594                 :          0 :         protocols = nla_get_u32(tb);
    3595                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
    3596                 :            :                    "mode");
    3597                 :          0 :         capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
    3598                 :          0 :         capa->probe_resp_offloads = probe_resp_offload_support(protocols);
    3599                 :            : }
    3600                 :            : 
    3601                 :            : 
    3602                 :      19663 : static int wiphy_info_handler(struct nl_msg *msg, void *arg)
    3603                 :            : {
    3604                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    3605                 :      19663 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    3606                 :      19663 :         struct wiphy_info_data *info = arg;
    3607                 :      19663 :         struct wpa_driver_capa *capa = info->capa;
    3608                 :      19663 :         struct wpa_driver_nl80211_data *drv = info->drv;
    3609                 :            : 
    3610                 :      19663 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    3611                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    3612                 :            : 
    3613         [ +  - ]:      19663 :         if (tb[NL80211_ATTR_WIPHY_NAME])
    3614                 :      19663 :                 os_strlcpy(drv->phyname,
    3615                 :      19663 :                            nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
    3616                 :            :                            sizeof(drv->phyname));
    3617         [ +  + ]:      19663 :         if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
    3618                 :        371 :                 capa->max_scan_ssids =
    3619                 :        371 :                         nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
    3620                 :            : 
    3621         [ +  + ]:      19663 :         if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
    3622                 :        371 :                 capa->max_sched_scan_ssids =
    3623                 :        371 :                         nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
    3624                 :            : 
    3625         [ +  + ]:      19663 :         if (tb[NL80211_ATTR_MAX_MATCH_SETS])
    3626                 :        371 :                 capa->max_match_sets =
    3627                 :        371 :                         nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
    3628                 :            : 
    3629         [ -  + ]:      19663 :         if (tb[NL80211_ATTR_MAC_ACL_MAX])
    3630                 :          0 :                 capa->max_acl_mac_addrs =
    3631                 :          0 :                         nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
    3632                 :            : 
    3633                 :      19663 :         wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
    3634                 :      19663 :         wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
    3635                 :      19663 :         wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
    3636                 :      19663 :         wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
    3637                 :            : 
    3638         [ +  + ]:      19663 :         if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
    3639                 :        371 :                 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
    3640                 :            :                            "off-channel TX");
    3641                 :        371 :                 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
    3642                 :            :         }
    3643                 :            : 
    3644         [ -  + ]:      19663 :         if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
    3645                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
    3646                 :          0 :                 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
    3647                 :            :         }
    3648                 :            : 
    3649                 :      19663 :         wiphy_info_max_roc(capa,
    3650                 :            :                            tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
    3651                 :            : 
    3652         [ +  + ]:      19663 :         if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
    3653                 :        371 :                 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
    3654                 :            : 
    3655                 :      19663 :         wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
    3656                 :            :                         tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
    3657                 :            : 
    3658         [ -  + ]:      19663 :         if (tb[NL80211_ATTR_DEVICE_AP_SME])
    3659                 :          0 :                 info->device_ap_sme = 1;
    3660                 :            : 
    3661                 :      19663 :         wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
    3662                 :      19663 :         wiphy_info_probe_resp_offload(capa,
    3663                 :            :                                       tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
    3664                 :            : 
    3665 [ +  - ][ +  - ]:      19663 :         if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
                 [ +  + ]
    3666                 :        371 :             drv->extended_capa == NULL) {
    3667                 :        371 :                 drv->extended_capa =
    3668                 :        371 :                         os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
    3669         [ +  - ]:        371 :                 if (drv->extended_capa) {
    3670                 :        371 :                         os_memcpy(drv->extended_capa,
    3671                 :            :                                   nla_data(tb[NL80211_ATTR_EXT_CAPA]),
    3672                 :            :                                   nla_len(tb[NL80211_ATTR_EXT_CAPA]));
    3673                 :        371 :                         drv->extended_capa_len =
    3674                 :        371 :                                 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
    3675                 :            :                 }
    3676                 :        371 :                 drv->extended_capa_mask =
    3677                 :        371 :                         os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
    3678         [ +  - ]:        371 :                 if (drv->extended_capa_mask) {
    3679                 :        371 :                         os_memcpy(drv->extended_capa_mask,
    3680                 :            :                                   nla_data(tb[NL80211_ATTR_EXT_CAPA]),
    3681                 :            :                                   nla_len(tb[NL80211_ATTR_EXT_CAPA]));
    3682                 :            :                 } else {
    3683                 :          0 :                         os_free(drv->extended_capa);
    3684                 :          0 :                         drv->extended_capa = NULL;
    3685                 :          0 :                         drv->extended_capa_len = 0;
    3686                 :            :                 }
    3687                 :            :         }
    3688                 :            : 
    3689         [ -  + ]:      19663 :         if (tb[NL80211_ATTR_VENDOR_DATA]) {
    3690                 :            :                 struct nlattr *nl;
    3691                 :            :                 int rem;
    3692                 :            : 
    3693         [ #  # ]:          0 :                 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
    3694                 :            :                         struct nl80211_vendor_cmd_info *vinfo;
    3695         [ #  # ]:          0 :                         if (nla_len(nl) != sizeof(*vinfo)) {
    3696                 :          0 :                                 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
    3697                 :          0 :                                 continue;
    3698                 :            :                         }
    3699                 :          0 :                         vinfo = nla_data(nl);
    3700                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
    3701                 :            :                                    vinfo->vendor_id, vinfo->subcmd);
    3702                 :            :                 }
    3703                 :            :         }
    3704                 :            : 
    3705         [ -  + ]:      19663 :         if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
    3706                 :            :                 struct nlattr *nl;
    3707                 :            :                 int rem;
    3708                 :            : 
    3709         [ #  # ]:          0 :                 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
    3710                 :            :                         struct nl80211_vendor_cmd_info *vinfo;
    3711         [ #  # ]:          0 :                         if (nla_len(nl) != sizeof(*vinfo)) {
    3712                 :          0 :                                 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
    3713                 :          0 :                                 continue;
    3714                 :            :                         }
    3715                 :          0 :                         vinfo = nla_data(nl);
    3716                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
    3717                 :            :                                    vinfo->vendor_id, vinfo->subcmd);
    3718                 :            :                 }
    3719                 :            :         }
    3720                 :            : 
    3721                 :      19663 :         return NL_SKIP;
    3722                 :            : }
    3723                 :            : 
    3724                 :            : 
    3725                 :        371 : static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
    3726                 :            :                                        struct wiphy_info_data *info)
    3727                 :            : {
    3728                 :            :         u32 feat;
    3729                 :            :         struct nl_msg *msg;
    3730                 :            : 
    3731                 :        371 :         os_memset(info, 0, sizeof(*info));
    3732                 :        371 :         info->capa = &drv->capa;
    3733                 :        371 :         info->drv = drv;
    3734                 :            : 
    3735                 :        371 :         msg = nlmsg_alloc();
    3736         [ -  + ]:        371 :         if (!msg)
    3737                 :          0 :                 return -1;
    3738                 :            : 
    3739                 :        371 :         feat = get_nl80211_protocol_features(drv);
    3740         [ +  - ]:        371 :         if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
    3741                 :        371 :                 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
    3742                 :            :         else
    3743                 :          0 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
    3744                 :            : 
    3745         [ +  - ]:        371 :         NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
    3746         [ -  + ]:        371 :         if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
    3747                 :          0 :                 goto nla_put_failure;
    3748                 :            : 
    3749         [ -  + ]:        371 :         if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
    3750                 :          0 :                 return -1;
    3751                 :            : 
    3752         [ +  - ]:        371 :         if (info->auth_supported)
    3753                 :        371 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
    3754         [ #  # ]:          0 :         else if (!info->connect_supported) {
    3755                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
    3756                 :            :                            "authentication/association or connect commands");
    3757                 :          0 :                 info->error = 1;
    3758                 :            :         }
    3759                 :            : 
    3760 [ +  - ][ +  - ]:        371 :         if (info->p2p_go_supported && info->p2p_client_supported)
    3761                 :        371 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
    3762         [ +  - ]:        371 :         if (info->p2p_concurrent) {
    3763                 :        371 :                 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
    3764                 :            :                            "interface (driver advertised support)");
    3765                 :        371 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
    3766                 :        371 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
    3767                 :            :         }
    3768         [ -  + ]:        371 :         if (info->num_multichan_concurrent > 1) {
    3769                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
    3770                 :            :                            "concurrent (driver advertised support)");
    3771                 :          0 :                 drv->capa.num_multichan_concurrent =
    3772                 :          0 :                         info->num_multichan_concurrent;
    3773                 :            :         }
    3774                 :            : 
    3775                 :            :         /* default to 5000 since early versions of mac80211 don't set it */
    3776         [ -  + ]:        371 :         if (!drv->capa.max_remain_on_chan)
    3777                 :          0 :                 drv->capa.max_remain_on_chan = 5000;
    3778                 :            : 
    3779         [ -  + ]:        371 :         if (info->channel_switch_supported)
    3780                 :          0 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
    3781                 :            : 
    3782                 :        371 :         return 0;
    3783                 :            : nla_put_failure:
    3784                 :          0 :         nlmsg_free(msg);
    3785                 :        371 :         return -1;
    3786                 :            : }
    3787                 :            : 
    3788                 :            : 
    3789                 :        371 : static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
    3790                 :            : {
    3791                 :            :         struct wiphy_info_data info;
    3792         [ -  + ]:        371 :         if (wpa_driver_nl80211_get_info(drv, &info))
    3793                 :          0 :                 return -1;
    3794                 :            : 
    3795         [ -  + ]:        371 :         if (info.error)
    3796                 :          0 :                 return -1;
    3797                 :            : 
    3798                 :        371 :         drv->has_capability = 1;
    3799                 :        371 :         drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
    3800                 :            :                 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
    3801                 :            :                 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
    3802                 :            :                 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
    3803                 :        371 :         drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
    3804                 :            :                 WPA_DRIVER_AUTH_SHARED |
    3805                 :            :                 WPA_DRIVER_AUTH_LEAP;
    3806                 :            : 
    3807                 :        371 :         drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
    3808                 :        371 :         drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
    3809                 :        371 :         drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
    3810                 :            : 
    3811         [ +  - ]:        371 :         if (!info.device_ap_sme) {
    3812                 :        371 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
    3813                 :            : 
    3814                 :            :                 /*
    3815                 :            :                  * No AP SME is currently assumed to also indicate no AP MLME
    3816                 :            :                  * in the driver/firmware.
    3817                 :            :                  */
    3818                 :        371 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
    3819                 :            :         }
    3820                 :            : 
    3821                 :        371 :         drv->device_ap_sme = info.device_ap_sme;
    3822                 :        371 :         drv->poll_command_supported = info.poll_command_supported;
    3823                 :        371 :         drv->data_tx_status = info.data_tx_status;
    3824         [ +  - ]:        371 :         if (info.set_qos_map_supported)
    3825                 :        371 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
    3826                 :            : 
    3827                 :            :         /*
    3828                 :            :          * If poll command and tx status are supported, mac80211 is new enough
    3829                 :            :          * to have everything we need to not need monitor interfaces.
    3830                 :            :          */
    3831 [ +  - ][ -  + ]:        371 :         drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
    3832                 :            : 
    3833 [ -  + ][ #  # ]:        371 :         if (drv->device_ap_sme && drv->use_monitor) {
    3834                 :            :                 /*
    3835                 :            :                  * Non-mac80211 drivers may not support monitor interface.
    3836                 :            :                  * Make sure we do not get stuck with incorrect capability here
    3837                 :            :                  * by explicitly testing this.
    3838                 :            :                  */
    3839         [ #  # ]:          0 :                 if (!info.monitor_supported) {
    3840                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
    3841                 :            :                                    "with device_ap_sme since no monitor mode "
    3842                 :            :                                    "support detected");
    3843                 :          0 :                         drv->use_monitor = 0;
    3844                 :            :                 }
    3845                 :            :         }
    3846                 :            : 
    3847                 :            :         /*
    3848                 :            :          * If we aren't going to use monitor interfaces, but the
    3849                 :            :          * driver doesn't support data TX status, we won't get TX
    3850                 :            :          * status for EAPOL frames.
    3851                 :            :          */
    3852 [ +  - ][ -  + ]:        371 :         if (!drv->use_monitor && !info.data_tx_status)
    3853                 :          0 :                 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
    3854                 :            : 
    3855                 :        371 :         return 0;
    3856                 :            : }
    3857                 :            : 
    3858                 :            : 
    3859                 :            : #ifdef ANDROID
    3860                 :            : static int android_genl_ctrl_resolve(struct nl_handle *handle,
    3861                 :            :                                      const char *name)
    3862                 :            : {
    3863                 :            :         /*
    3864                 :            :          * Android ICS has very minimal genl_ctrl_resolve() implementation, so
    3865                 :            :          * need to work around that.
    3866                 :            :          */
    3867                 :            :         struct nl_cache *cache = NULL;
    3868                 :            :         struct genl_family *nl80211 = NULL;
    3869                 :            :         int id = -1;
    3870                 :            : 
    3871                 :            :         if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
    3872                 :            :                 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
    3873                 :            :                            "netlink cache");
    3874                 :            :                 goto fail;
    3875                 :            :         }
    3876                 :            : 
    3877                 :            :         nl80211 = genl_ctrl_search_by_name(cache, name);
    3878                 :            :         if (nl80211 == NULL)
    3879                 :            :                 goto fail;
    3880                 :            : 
    3881                 :            :         id = genl_family_get_id(nl80211);
    3882                 :            : 
    3883                 :            : fail:
    3884                 :            :         if (nl80211)
    3885                 :            :                 genl_family_put(nl80211);
    3886                 :            :         if (cache)
    3887                 :            :                 nl_cache_free(cache);
    3888                 :            : 
    3889                 :            :         return id;
    3890                 :            : }
    3891                 :            : #define genl_ctrl_resolve android_genl_ctrl_resolve
    3892                 :            : #endif /* ANDROID */
    3893                 :            : 
    3894                 :            : 
    3895                 :          5 : static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
    3896                 :            : {
    3897                 :            :         int ret;
    3898                 :            : 
    3899                 :          5 :         global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
    3900         [ -  + ]:          5 :         if (global->nl_cb == NULL) {
    3901                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
    3902                 :            :                            "callbacks");
    3903                 :          0 :                 return -1;
    3904                 :            :         }
    3905                 :            : 
    3906                 :          5 :         global->nl = nl_create_handle(global->nl_cb, "nl");
    3907         [ -  + ]:          5 :         if (global->nl == NULL)
    3908                 :          0 :                 goto err;
    3909                 :            : 
    3910                 :          5 :         global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
    3911         [ -  + ]:          5 :         if (global->nl80211_id < 0) {
    3912                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
    3913                 :            :                            "found");
    3914                 :          0 :                 goto err;
    3915                 :            :         }
    3916                 :            : 
    3917                 :          5 :         global->nl_event = nl_create_handle(global->nl_cb, "event");
    3918         [ -  + ]:          5 :         if (global->nl_event == NULL)
    3919                 :          0 :                 goto err;
    3920                 :            : 
    3921                 :          5 :         ret = nl_get_multicast_id(global, "nl80211", "scan");
    3922         [ +  - ]:          5 :         if (ret >= 0)
    3923                 :          5 :                 ret = nl_socket_add_membership(global->nl_event, ret);
    3924         [ -  + ]:          5 :         if (ret < 0) {
    3925                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
    3926                 :            :                            "membership for scan events: %d (%s)",
    3927                 :            :                            ret, strerror(-ret));
    3928                 :          0 :                 goto err;
    3929                 :            :         }
    3930                 :            : 
    3931                 :          5 :         ret = nl_get_multicast_id(global, "nl80211", "mlme");
    3932         [ +  - ]:          5 :         if (ret >= 0)
    3933                 :          5 :                 ret = nl_socket_add_membership(global->nl_event, ret);
    3934         [ -  + ]:          5 :         if (ret < 0) {
    3935                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
    3936                 :            :                            "membership for mlme events: %d (%s)",
    3937                 :            :                            ret, strerror(-ret));
    3938                 :          0 :                 goto err;
    3939                 :            :         }
    3940                 :            : 
    3941                 :          5 :         ret = nl_get_multicast_id(global, "nl80211", "regulatory");
    3942         [ +  - ]:          5 :         if (ret >= 0)
    3943                 :          5 :                 ret = nl_socket_add_membership(global->nl_event, ret);
    3944         [ -  + ]:          5 :         if (ret < 0) {
    3945                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
    3946                 :            :                            "membership for regulatory events: %d (%s)",
    3947                 :            :                            ret, strerror(-ret));
    3948                 :            :                 /* Continue without regulatory events */
    3949                 :            :         }
    3950                 :            : 
    3951                 :          5 :         ret = nl_get_multicast_id(global, "nl80211", "vendor");
    3952         [ +  - ]:          5 :         if (ret >= 0)
    3953                 :          5 :                 ret = nl_socket_add_membership(global->nl_event, ret);
    3954         [ -  + ]:          5 :         if (ret < 0) {
    3955                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
    3956                 :            :                            "membership for vendor events: %d (%s)",
    3957                 :            :                            ret, strerror(-ret));
    3958                 :            :                 /* Continue without vendor events */
    3959                 :            :         }
    3960                 :            : 
    3961                 :          5 :         nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
    3962                 :            :                   no_seq_check, NULL);
    3963                 :          5 :         nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
    3964                 :            :                   process_global_event, global);
    3965                 :            : 
    3966                 :          5 :         nl80211_register_eloop_read(&global->nl_event,
    3967                 :            :                                     wpa_driver_nl80211_event_receive,
    3968                 :          5 :                                     global->nl_cb);
    3969                 :            : 
    3970                 :          5 :         return 0;
    3971                 :            : 
    3972                 :            : err:
    3973                 :          0 :         nl_destroy_handles(&global->nl_event);
    3974                 :          0 :         nl_destroy_handles(&global->nl);
    3975                 :          0 :         nl_cb_put(global->nl_cb);
    3976                 :          0 :         global->nl_cb = NULL;
    3977                 :          5 :         return -1;
    3978                 :            : }
    3979                 :            : 
    3980                 :            : 
    3981                 :        371 : static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
    3982                 :            : {
    3983                 :        371 :         drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
    3984         [ -  + ]:        371 :         if (!drv->nl_cb) {
    3985                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
    3986                 :          0 :                 return -1;
    3987                 :            :         }
    3988                 :            : 
    3989                 :        371 :         nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
    3990                 :            :                   no_seq_check, NULL);
    3991                 :        371 :         nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
    3992                 :            :                   process_drv_event, drv);
    3993                 :            : 
    3994                 :        371 :         return 0;
    3995                 :            : }
    3996                 :            : 
    3997                 :            : 
    3998                 :          0 : static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
    3999                 :            : {
    4000                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
    4001                 :            :         /*
    4002                 :            :          * This may be for any interface; use ifdown event to disable
    4003                 :            :          * interface.
    4004                 :            :          */
    4005                 :          0 : }
    4006                 :            : 
    4007                 :            : 
    4008                 :          0 : static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
    4009                 :            : {
    4010                 :          0 :         struct wpa_driver_nl80211_data *drv = ctx;
    4011                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
    4012         [ #  # ]:          0 :         if (i802_set_iface_flags(drv->first_bss, 1)) {
    4013                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
    4014                 :            :                            "after rfkill unblock");
    4015                 :          0 :                 return;
    4016                 :            :         }
    4017                 :            :         /* rtnetlink ifup handler will report interface as enabled */
    4018                 :            : }
    4019                 :            : 
    4020                 :            : 
    4021                 :       3105 : static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
    4022                 :            :                                                       void *eloop_ctx,
    4023                 :            :                                                       void *handle)
    4024                 :            : {
    4025                 :       3105 :         struct wpa_driver_nl80211_data *drv = eloop_ctx;
    4026                 :            :         u8 data[2048];
    4027                 :            :         struct msghdr msg;
    4028                 :            :         struct iovec entry;
    4029                 :            :         u8 control[512];
    4030                 :            :         struct cmsghdr *cmsg;
    4031                 :       3105 :         int res, found_ee = 0, found_wifi = 0, acked = 0;
    4032                 :            :         union wpa_event_data event;
    4033                 :            : 
    4034                 :       3105 :         memset(&msg, 0, sizeof(msg));
    4035                 :       3105 :         msg.msg_iov = &entry;
    4036                 :       3105 :         msg.msg_iovlen = 1;
    4037                 :       3105 :         entry.iov_base = data;
    4038                 :       3105 :         entry.iov_len = sizeof(data);
    4039                 :       3105 :         msg.msg_control = &control;
    4040                 :       3105 :         msg.msg_controllen = sizeof(control);
    4041                 :            : 
    4042                 :       3105 :         res = recvmsg(sock, &msg, MSG_ERRQUEUE);
    4043                 :            :         /* if error or not fitting 802.3 header, return */
    4044         [ -  + ]:       3105 :         if (res < 14)
    4045                 :          0 :                 return;
    4046                 :            : 
    4047 [ +  - ][ +  + ]:       9315 :         for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
    4048                 :            :         {
    4049 [ +  + ][ +  - ]:       6210 :                 if (cmsg->cmsg_level == SOL_SOCKET &&
    4050                 :       3105 :                     cmsg->cmsg_type == SCM_WIFI_STATUS) {
    4051                 :            :                         int *ack;
    4052                 :            : 
    4053                 :       3105 :                         found_wifi = 1;
    4054                 :       3105 :                         ack = (void *)CMSG_DATA(cmsg);
    4055                 :       3105 :                         acked = *ack;
    4056                 :            :                 }
    4057                 :            : 
    4058 [ +  + ][ +  - ]:       6210 :                 if (cmsg->cmsg_level == SOL_PACKET &&
    4059                 :       3105 :                     cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
    4060                 :       3105 :                         struct sock_extended_err *err =
    4061                 :            :                                 (struct sock_extended_err *)CMSG_DATA(cmsg);
    4062                 :            : 
    4063         [ +  - ]:       3105 :                         if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
    4064                 :       3105 :                                 found_ee = 1;
    4065                 :            :                 }
    4066                 :            :         }
    4067                 :            : 
    4068 [ +  - ][ -  + ]:       3105 :         if (!found_ee || !found_wifi)
    4069                 :          0 :                 return;
    4070                 :            : 
    4071                 :       3105 :         memset(&event, 0, sizeof(event));
    4072                 :       3105 :         event.eapol_tx_status.dst = data;
    4073                 :       3105 :         event.eapol_tx_status.data = data + 14;
    4074                 :       3105 :         event.eapol_tx_status.data_len = res - 14;
    4075                 :       3105 :         event.eapol_tx_status.ack = acked;
    4076                 :       3105 :         wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
    4077                 :            : }
    4078                 :            : 
    4079                 :            : 
    4080                 :        388 : static int nl80211_init_bss(struct i802_bss *bss)
    4081                 :            : {
    4082                 :        388 :         bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
    4083         [ -  + ]:        388 :         if (!bss->nl_cb)
    4084                 :          0 :                 return -1;
    4085                 :            : 
    4086                 :        388 :         nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
    4087                 :            :                   no_seq_check, NULL);
    4088                 :        388 :         nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
    4089                 :            :                   process_bss_event, bss);
    4090                 :            : 
    4091                 :        388 :         return 0;
    4092                 :            : }
    4093                 :            : 
    4094                 :            : 
    4095                 :        388 : static void nl80211_destroy_bss(struct i802_bss *bss)
    4096                 :            : {
    4097                 :        388 :         nl_cb_put(bss->nl_cb);
    4098                 :        388 :         bss->nl_cb = NULL;
    4099                 :        388 : }
    4100                 :            : 
    4101                 :            : 
    4102                 :        371 : static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
    4103                 :            :                                           void *global_priv, int hostapd,
    4104                 :            :                                           const u8 *set_addr)
    4105                 :            : {
    4106                 :            :         struct wpa_driver_nl80211_data *drv;
    4107                 :            :         struct rfkill_config *rcfg;
    4108                 :            :         struct i802_bss *bss;
    4109                 :            : 
    4110         [ -  + ]:        371 :         if (global_priv == NULL)
    4111                 :          0 :                 return NULL;
    4112                 :        371 :         drv = os_zalloc(sizeof(*drv));
    4113         [ -  + ]:        371 :         if (drv == NULL)
    4114                 :          0 :                 return NULL;
    4115                 :        371 :         drv->global = global_priv;
    4116                 :        371 :         drv->ctx = ctx;
    4117                 :        371 :         drv->hostapd = !!hostapd;
    4118                 :        371 :         drv->eapol_sock = -1;
    4119                 :        371 :         drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
    4120                 :        371 :         drv->if_indices = drv->default_if_indices;
    4121                 :            : 
    4122                 :        371 :         drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
    4123         [ -  + ]:        371 :         if (!drv->first_bss) {
    4124                 :          0 :                 os_free(drv);
    4125                 :          0 :                 return NULL;
    4126                 :            :         }
    4127                 :        371 :         bss = drv->first_bss;
    4128                 :        371 :         bss->drv = drv;
    4129                 :        371 :         bss->ctx = ctx;
    4130                 :            : 
    4131                 :        371 :         os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
    4132                 :        371 :         drv->monitor_ifidx = -1;
    4133                 :        371 :         drv->monitor_sock = -1;
    4134                 :        371 :         drv->eapol_tx_sock = -1;
    4135                 :        371 :         drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
    4136                 :            : 
    4137         [ -  + ]:        371 :         if (wpa_driver_nl80211_init_nl(drv)) {
    4138                 :          0 :                 os_free(drv);
    4139                 :          0 :                 return NULL;
    4140                 :            :         }
    4141                 :            : 
    4142         [ -  + ]:        371 :         if (nl80211_init_bss(bss))
    4143                 :          0 :                 goto failed;
    4144                 :            : 
    4145                 :        371 :         rcfg = os_zalloc(sizeof(*rcfg));
    4146         [ -  + ]:        371 :         if (rcfg == NULL)
    4147                 :          0 :                 goto failed;
    4148                 :        371 :         rcfg->ctx = drv;
    4149                 :        371 :         os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
    4150                 :        371 :         rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
    4151                 :        371 :         rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
    4152                 :        371 :         drv->rfkill = rfkill_init(rcfg);
    4153         [ -  + ]:        371 :         if (drv->rfkill == NULL) {
    4154                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
    4155                 :          0 :                 os_free(rcfg);
    4156                 :            :         }
    4157                 :            : 
    4158         [ -  + ]:        371 :         if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
    4159                 :          0 :                 drv->start_iface_up = 1;
    4160                 :            : 
    4161         [ -  + ]:        371 :         if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1))
    4162                 :          0 :                 goto failed;
    4163                 :            : 
    4164                 :        371 :         drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
    4165         [ -  + ]:        371 :         if (drv->eapol_tx_sock < 0)
    4166                 :          0 :                 goto failed;
    4167                 :            : 
    4168         [ +  - ]:        371 :         if (drv->data_tx_status) {
    4169                 :        371 :                 int enabled = 1;
    4170                 :            : 
    4171         [ -  + ]:        371 :                 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
    4172                 :            :                                &enabled, sizeof(enabled)) < 0) {
    4173                 :          0 :                         wpa_printf(MSG_DEBUG,
    4174                 :            :                                 "nl80211: wifi status sockopt failed\n");
    4175                 :          0 :                         drv->data_tx_status = 0;
    4176         [ #  # ]:          0 :                         if (!drv->use_monitor)
    4177                 :          0 :                                 drv->capa.flags &=
    4178                 :            :                                         ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
    4179                 :            :                 } else {
    4180                 :        371 :                         eloop_register_read_sock(drv->eapol_tx_sock,
    4181                 :            :                                 wpa_driver_nl80211_handle_eapol_tx_status,
    4182                 :            :                                 drv, NULL);
    4183                 :            :                 }
    4184                 :            :         }
    4185                 :            : 
    4186         [ +  - ]:        371 :         if (drv->global) {
    4187                 :        371 :                 dl_list_add(&drv->global->interfaces, &drv->list);
    4188                 :        371 :                 drv->in_interface_list = 1;
    4189                 :            :         }
    4190                 :            : 
    4191                 :        371 :         return bss;
    4192                 :            : 
    4193                 :            : failed:
    4194                 :          0 :         wpa_driver_nl80211_deinit(bss);
    4195                 :        371 :         return NULL;
    4196                 :            : }
    4197                 :            : 
    4198                 :            : 
    4199                 :            : /**
    4200                 :            :  * wpa_driver_nl80211_init - Initialize nl80211 driver interface
    4201                 :            :  * @ctx: context to be used when calling wpa_supplicant functions,
    4202                 :            :  * e.g., wpa_supplicant_event()
    4203                 :            :  * @ifname: interface name, e.g., wlan0
    4204                 :            :  * @global_priv: private driver global data from global_init()
    4205                 :            :  * Returns: Pointer to private data, %NULL on failure
    4206                 :            :  */
    4207                 :         39 : static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
    4208                 :            :                                       void *global_priv)
    4209                 :            : {
    4210                 :         39 :         return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL);
    4211                 :            : }
    4212                 :            : 
    4213                 :            : 
    4214                 :       8452 : static int nl80211_register_frame(struct i802_bss *bss,
    4215                 :            :                                   struct nl_handle *nl_handle,
    4216                 :            :                                   u16 type, const u8 *match, size_t match_len)
    4217                 :            : {
    4218                 :       8452 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4219                 :            :         struct nl_msg *msg;
    4220                 :       8452 :         int ret = -1;
    4221                 :            :         char buf[30];
    4222                 :            : 
    4223                 :       8452 :         msg = nlmsg_alloc();
    4224         [ -  + ]:       8452 :         if (!msg)
    4225                 :          0 :                 return -1;
    4226                 :            : 
    4227                 :       8452 :         buf[0] = '\0';
    4228                 :       8452 :         wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
    4229                 :       8452 :         wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p match=%s",
    4230                 :            :                    type, nl_handle, buf);
    4231                 :            : 
    4232                 :       8452 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
    4233                 :            : 
    4234         [ -  + ]:       8452 :         if (nl80211_set_iface_id(msg, bss) < 0)
    4235                 :          0 :                 goto nla_put_failure;
    4236                 :            : 
    4237         [ +  - ]:       8452 :         NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
    4238         [ +  - ]:       8452 :         NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
    4239                 :            : 
    4240                 :       8452 :         ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
    4241                 :       8452 :         msg = NULL;
    4242         [ -  + ]:       8452 :         if (ret) {
    4243                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
    4244                 :            :                            "failed (type=%u): ret=%d (%s)",
    4245                 :            :                            type, ret, strerror(-ret));
    4246                 :          0 :                 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
    4247                 :            :                             match, match_len);
    4248                 :          0 :                 goto nla_put_failure;
    4249                 :            :         }
    4250                 :       8452 :         ret = 0;
    4251                 :            : nla_put_failure:
    4252                 :       8452 :         nlmsg_free(msg);
    4253                 :       8452 :         return ret;
    4254                 :            : }
    4255                 :            : 
    4256                 :            : 
    4257                 :        715 : static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
    4258                 :            : {
    4259                 :        715 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4260                 :            : 
    4261         [ -  + ]:        715 :         if (bss->nl_mgmt) {
    4262                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
    4263                 :            :                            "already on! (nl_mgmt=%p)", bss->nl_mgmt);
    4264                 :          0 :                 return -1;
    4265                 :            :         }
    4266                 :            : 
    4267                 :        715 :         bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
    4268         [ -  + ]:        715 :         if (bss->nl_mgmt == NULL)
    4269                 :          0 :                 return -1;
    4270                 :            : 
    4271                 :        715 :         return 0;
    4272                 :            : }
    4273                 :            : 
    4274                 :            : 
    4275                 :        715 : static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
    4276                 :            : {
    4277                 :        715 :         nl80211_register_eloop_read(&bss->nl_mgmt,
    4278                 :            :                                     wpa_driver_nl80211_event_receive,
    4279                 :        715 :                                     bss->nl_cb);
    4280                 :        715 : }
    4281                 :            : 
    4282                 :            : 
    4283                 :       4607 : static int nl80211_register_action_frame(struct i802_bss *bss,
    4284                 :            :                                          const u8 *match, size_t match_len)
    4285                 :            : {
    4286                 :       4607 :         u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
    4287                 :       4607 :         return nl80211_register_frame(bss, bss->nl_mgmt,
    4288                 :            :                                       type, match, match_len);
    4289                 :            : }
    4290                 :            : 
    4291                 :            : 
    4292                 :        271 : static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
    4293                 :            : {
    4294                 :        271 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4295                 :        271 :         int ret = 0;
    4296                 :            : 
    4297         [ -  + ]:        271 :         if (nl80211_alloc_mgmt_handle(bss))
    4298                 :          0 :                 return -1;
    4299                 :        271 :         wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
    4300                 :            :                    "handle %p", bss->nl_mgmt);
    4301                 :            : 
    4302         [ +  + ]:        271 :         if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
    4303                 :          5 :                 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
    4304                 :            : 
    4305                 :            :                 /* register for any AUTH message */
    4306                 :          5 :                 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
    4307                 :            :         }
    4308                 :            : 
    4309                 :            : #ifdef CONFIG_INTERWORKING
    4310                 :            :         /* QoS Map Configure */
    4311         [ -  + ]:        271 :         if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
    4312                 :          0 :                 ret = -1;
    4313                 :            : #endif /* CONFIG_INTERWORKING */
    4314                 :            : #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
    4315                 :            :         /* GAS Initial Request */
    4316         [ -  + ]:        271 :         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
    4317                 :          0 :                 ret = -1;
    4318                 :            :         /* GAS Initial Response */
    4319         [ -  + ]:        271 :         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
    4320                 :          0 :                 ret = -1;
    4321                 :            :         /* GAS Comeback Request */
    4322         [ -  + ]:        271 :         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
    4323                 :          0 :                 ret = -1;
    4324                 :            :         /* GAS Comeback Response */
    4325         [ -  + ]:        271 :         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
    4326                 :          0 :                 ret = -1;
    4327                 :            :         /* Protected GAS Initial Request */
    4328         [ -  + ]:        271 :         if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
    4329                 :          0 :                 ret = -1;
    4330                 :            :         /* Protected GAS Initial Response */
    4331         [ -  + ]:        271 :         if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
    4332                 :          0 :                 ret = -1;
    4333                 :            :         /* Protected GAS Comeback Request */
    4334         [ -  + ]:        271 :         if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
    4335                 :          0 :                 ret = -1;
    4336                 :            :         /* Protected GAS Comeback Response */
    4337         [ -  + ]:        271 :         if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
    4338                 :          0 :                 ret = -1;
    4339                 :            : #endif /* CONFIG_P2P || CONFIG_INTERWORKING */
    4340                 :            : #ifdef CONFIG_P2P
    4341                 :            :         /* P2P Public Action */
    4342         [ -  + ]:        271 :         if (nl80211_register_action_frame(bss,
    4343                 :            :                                           (u8 *) "\x04\x09\x50\x6f\x9a\x09",
    4344                 :            :                                           6) < 0)
    4345                 :          0 :                 ret = -1;
    4346                 :            :         /* P2P Action */
    4347         [ -  + ]:        271 :         if (nl80211_register_action_frame(bss,
    4348                 :            :                                           (u8 *) "\x7f\x50\x6f\x9a\x09",
    4349                 :            :                                           5) < 0)
    4350                 :          0 :                 ret = -1;
    4351                 :            : #endif /* CONFIG_P2P */
    4352                 :            : #ifdef CONFIG_IEEE80211W
    4353                 :            :         /* SA Query Response */
    4354         [ -  + ]:        271 :         if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
    4355                 :          0 :                 ret = -1;
    4356                 :            : #endif /* CONFIG_IEEE80211W */
    4357                 :            : #ifdef CONFIG_TDLS
    4358         [ +  - ]:        271 :         if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
    4359                 :            :                 /* TDLS Discovery Response */
    4360         [ -  + ]:        271 :                 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
    4361                 :            :                     0)
    4362                 :          0 :                         ret = -1;
    4363                 :            :         }
    4364                 :            : #endif /* CONFIG_TDLS */
    4365                 :            : 
    4366                 :            :         /* FT Action frames */
    4367         [ -  + ]:        271 :         if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
    4368                 :          0 :                 ret = -1;
    4369                 :            :         else
    4370                 :        271 :                 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
    4371                 :            :                         WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
    4372                 :            : 
    4373                 :            :         /* WNM - BSS Transition Management Request */
    4374         [ -  + ]:        271 :         if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
    4375                 :          0 :                 ret = -1;
    4376                 :            :         /* WNM-Sleep Mode Response */
    4377         [ -  + ]:        271 :         if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
    4378                 :          0 :                 ret = -1;
    4379                 :            : 
    4380                 :            : #ifdef CONFIG_HS20
    4381                 :            :         /* WNM-Notification */
    4382         [ -  + ]:        271 :         if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
    4383                 :          0 :                 return -1;
    4384                 :            : #endif /* CONFIG_HS20 */
    4385                 :            : 
    4386                 :        271 :         nl80211_mgmt_handle_register_eloop(bss);
    4387                 :            : 
    4388                 :        271 :         return ret;
    4389                 :            : }
    4390                 :            : 
    4391                 :            : 
    4392                 :        444 : static int nl80211_register_spurious_class3(struct i802_bss *bss)
    4393                 :            : {
    4394                 :        444 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4395                 :            :         struct nl_msg *msg;
    4396                 :        444 :         int ret = -1;
    4397                 :            : 
    4398                 :        444 :         msg = nlmsg_alloc();
    4399         [ -  + ]:        444 :         if (!msg)
    4400                 :          0 :                 return -1;
    4401                 :            : 
    4402                 :        444 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
    4403                 :            : 
    4404         [ +  - ]:        444 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
    4405                 :            : 
    4406                 :        444 :         ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
    4407                 :        444 :         msg = NULL;
    4408         [ -  + ]:        444 :         if (ret) {
    4409                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
    4410                 :            :                            "failed: ret=%d (%s)",
    4411                 :            :                            ret, strerror(-ret));
    4412                 :          0 :                 goto nla_put_failure;
    4413                 :            :         }
    4414                 :        444 :         ret = 0;
    4415                 :            : nla_put_failure:
    4416                 :        444 :         nlmsg_free(msg);
    4417                 :        444 :         return ret;
    4418                 :            : }
    4419                 :            : 
    4420                 :            : 
    4421                 :        444 : static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
    4422                 :            : {
    4423                 :            :         static const int stypes[] = {
    4424                 :            :                 WLAN_FC_STYPE_AUTH,
    4425                 :            :                 WLAN_FC_STYPE_ASSOC_REQ,
    4426                 :            :                 WLAN_FC_STYPE_REASSOC_REQ,
    4427                 :            :                 WLAN_FC_STYPE_DISASSOC,
    4428                 :            :                 WLAN_FC_STYPE_DEAUTH,
    4429                 :            :                 WLAN_FC_STYPE_ACTION,
    4430                 :            :                 WLAN_FC_STYPE_PROBE_REQ,
    4431                 :            : /* Beacon doesn't work as mac80211 doesn't currently allow
    4432                 :            :  * it, but it wouldn't really be the right thing anyway as
    4433                 :            :  * it isn't per interface ... maybe just dump the scan
    4434                 :            :  * results periodically for OLBC?
    4435                 :            :  */
    4436                 :            : //              WLAN_FC_STYPE_BEACON,
    4437                 :            :         };
    4438                 :            :         unsigned int i;
    4439                 :            : 
    4440         [ -  + ]:        444 :         if (nl80211_alloc_mgmt_handle(bss))
    4441                 :          0 :                 return -1;
    4442                 :        444 :         wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
    4443                 :            :                    "handle %p", bss->nl_mgmt);
    4444                 :            : 
    4445         [ +  + ]:       3552 :         for (i = 0; i < ARRAY_SIZE(stypes); i++) {
    4446         [ -  + ]:       3108 :                 if (nl80211_register_frame(bss, bss->nl_mgmt,
    4447                 :            :                                            (WLAN_FC_TYPE_MGMT << 2) |
    4448                 :       3108 :                                            (stypes[i] << 4),
    4449                 :            :                                            NULL, 0) < 0) {
    4450                 :          0 :                         goto out_err;
    4451                 :            :                 }
    4452                 :            :         }
    4453                 :            : 
    4454         [ -  + ]:        444 :         if (nl80211_register_spurious_class3(bss))
    4455                 :          0 :                 goto out_err;
    4456                 :            : 
    4457         [ -  + ]:        444 :         if (nl80211_get_wiphy_data_ap(bss) == NULL)
    4458                 :          0 :                 goto out_err;
    4459                 :            : 
    4460                 :        444 :         nl80211_mgmt_handle_register_eloop(bss);
    4461                 :        444 :         return 0;
    4462                 :            : 
    4463                 :            : out_err:
    4464                 :          0 :         nl_destroy_handles(&bss->nl_mgmt);
    4465                 :        444 :         return -1;
    4466                 :            : }
    4467                 :            : 
    4468                 :            : 
    4469                 :          0 : static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
    4470                 :            : {
    4471         [ #  # ]:          0 :         if (nl80211_alloc_mgmt_handle(bss))
    4472                 :          0 :                 return -1;
    4473                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
    4474                 :            :                    "handle %p (device SME)", bss->nl_mgmt);
    4475                 :            : 
    4476         [ #  # ]:          0 :         if (nl80211_register_frame(bss, bss->nl_mgmt,
    4477                 :            :                                    (WLAN_FC_TYPE_MGMT << 2) |
    4478                 :            :                                    (WLAN_FC_STYPE_ACTION << 4),
    4479                 :            :                                    NULL, 0) < 0)
    4480                 :          0 :                 goto out_err;
    4481                 :            : 
    4482                 :          0 :         nl80211_mgmt_handle_register_eloop(bss);
    4483                 :          0 :         return 0;
    4484                 :            : 
    4485                 :            : out_err:
    4486                 :          0 :         nl_destroy_handles(&bss->nl_mgmt);
    4487                 :          0 :         return -1;
    4488                 :            : }
    4489                 :            : 
    4490                 :            : 
    4491                 :       1457 : static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
    4492                 :            : {
    4493         [ +  + ]:       1457 :         if (bss->nl_mgmt == NULL)
    4494                 :       1457 :                 return;
    4495                 :        715 :         wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
    4496                 :            :                    "(%s)", bss->nl_mgmt, reason);
    4497                 :        715 :         nl80211_destroy_eloop_handle(&bss->nl_mgmt);
    4498                 :            : 
    4499                 :        715 :         nl80211_put_wiphy_data_ap(bss);
    4500                 :            : }
    4501                 :            : 
    4502                 :            : 
    4503                 :          0 : static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
    4504                 :            : {
    4505                 :          0 :         wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
    4506                 :          0 : }
    4507                 :            : 
    4508                 :            : 
    4509                 :          0 : static void nl80211_del_p2pdev(struct i802_bss *bss)
    4510                 :            : {
    4511                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4512                 :            :         struct nl_msg *msg;
    4513                 :            :         int ret;
    4514                 :            : 
    4515                 :          0 :         msg = nlmsg_alloc();
    4516         [ #  # ]:          0 :         if (!msg)
    4517                 :          0 :                 return;
    4518                 :            : 
    4519                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
    4520         [ #  # ]:          0 :         NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
    4521                 :            : 
    4522                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    4523                 :          0 :         msg = NULL;
    4524                 :            : 
    4525                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
    4526                 :          0 :                    bss->ifname, (long long unsigned int) bss->wdev_id,
    4527                 :            :                    strerror(-ret));
    4528                 :            : 
    4529                 :            : nla_put_failure:
    4530                 :          0 :         nlmsg_free(msg);
    4531                 :            : }
    4532                 :            : 
    4533                 :            : 
    4534                 :          0 : static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
    4535                 :            : {
    4536                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4537                 :            :         struct nl_msg *msg;
    4538                 :          0 :         int ret = -1;
    4539                 :            : 
    4540                 :          0 :         msg = nlmsg_alloc();
    4541         [ #  # ]:          0 :         if (!msg)
    4542                 :          0 :                 return -1;
    4543                 :            : 
    4544         [ #  # ]:          0 :         if (start)
    4545                 :          0 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
    4546                 :            :         else
    4547                 :          0 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
    4548                 :            : 
    4549         [ #  # ]:          0 :         NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
    4550                 :            : 
    4551                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    4552                 :          0 :         msg = NULL;
    4553                 :            : 
    4554         [ #  # ]:          0 :         wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
    4555                 :            :                    start ? "Start" : "Stop",
    4556                 :          0 :                    bss->ifname, (long long unsigned int) bss->wdev_id,
    4557                 :            :                    strerror(-ret));
    4558                 :            : 
    4559                 :            : nla_put_failure:
    4560                 :          0 :         nlmsg_free(msg);
    4561                 :          0 :         return ret;
    4562                 :            : }
    4563                 :            : 
    4564                 :            : 
    4565                 :        371 : static int i802_set_iface_flags(struct i802_bss *bss, int up)
    4566                 :            : {
    4567                 :            :         enum nl80211_iftype nlmode;
    4568                 :            : 
    4569                 :        371 :         nlmode = nl80211_get_ifmode(bss);
    4570         [ +  - ]:        371 :         if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
    4571                 :        371 :                 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
    4572                 :        371 :                                              bss->ifname, up);
    4573                 :            :         }
    4574                 :            : 
    4575                 :            :         /* P2P Device has start/stop which is equivalent */
    4576                 :        371 :         return nl80211_set_p2pdev(bss, up);
    4577                 :            : }
    4578                 :            : 
    4579                 :            : 
    4580                 :            : static int
    4581                 :        371 : wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
    4582                 :            :                                    const u8 *set_addr, int first)
    4583                 :            : {
    4584                 :        371 :         struct i802_bss *bss = drv->first_bss;
    4585                 :        371 :         int send_rfkill_event = 0;
    4586                 :            :         enum nl80211_iftype nlmode;
    4587                 :            : 
    4588                 :        371 :         drv->ifindex = if_nametoindex(bss->ifname);
    4589                 :        371 :         bss->ifindex = drv->ifindex;
    4590                 :        371 :         bss->wdev_id = drv->global->if_add_wdevid;
    4591                 :        371 :         bss->wdev_id_set = drv->global->if_add_wdevid_set;
    4592                 :            : 
    4593                 :        371 :         bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
    4594 [ -  + ][ +  + ]:        371 :         bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
    4595                 :        371 :         drv->global->if_add_wdevid_set = 0;
    4596                 :            : 
    4597         [ -  + ]:        371 :         if (wpa_driver_nl80211_capa(drv))
    4598                 :          0 :                 return -1;
    4599                 :            : 
    4600                 :        371 :         wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
    4601                 :        371 :                    bss->ifname, drv->phyname);
    4602                 :            : 
    4603   [ +  +  +  - ]:        377 :         if (set_addr &&
    4604         [ -  + ]:         12 :             (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
    4605                 :          6 :              linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
    4606                 :            :                                 set_addr)))
    4607                 :          0 :                 return -1;
    4608                 :            : 
    4609 [ +  - ][ -  + ]:        371 :         if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
    4610                 :          0 :                 drv->start_mode_ap = 1;
    4611                 :            : 
    4612         [ +  + ]:        371 :         if (drv->hostapd)
    4613                 :        332 :                 nlmode = NL80211_IFTYPE_AP;
    4614         [ +  + ]:         39 :         else if (bss->if_dynamic)
    4615                 :         24 :                 nlmode = nl80211_get_ifmode(bss);
    4616                 :            :         else
    4617                 :         15 :                 nlmode = NL80211_IFTYPE_STATION;
    4618                 :            : 
    4619         [ -  + ]:        371 :         if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
    4620                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
    4621                 :          0 :                 return -1;
    4622                 :            :         }
    4623                 :            : 
    4624         [ -  + ]:        371 :         if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
    4625                 :          0 :                 int ret = nl80211_set_p2pdev(bss, 1);
    4626         [ #  # ]:          0 :                 if (ret < 0)
    4627                 :          0 :                         wpa_printf(MSG_ERROR, "nl80211: Could not start P2P device");
    4628                 :          0 :                 nl80211_get_macaddr(bss);
    4629                 :          0 :                 return ret;
    4630                 :            :         }
    4631                 :            : 
    4632         [ -  + ]:        371 :         if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
    4633         [ #  # ]:          0 :                 if (rfkill_is_blocked(drv->rfkill)) {
    4634                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
    4635                 :            :                                    "interface '%s' due to rfkill",
    4636                 :          0 :                                    bss->ifname);
    4637                 :          0 :                         drv->if_disabled = 1;
    4638                 :          0 :                         send_rfkill_event = 1;
    4639                 :            :                 } else {
    4640                 :          0 :                         wpa_printf(MSG_ERROR, "nl80211: Could not set "
    4641                 :          0 :                                    "interface '%s' UP", bss->ifname);
    4642                 :          0 :                         return -1;
    4643                 :            :                 }
    4644                 :            :         }
    4645                 :            : 
    4646         [ +  + ]:        371 :         if (!drv->hostapd)
    4647                 :         39 :                 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
    4648                 :            :                                        1, IF_OPER_DORMANT);
    4649                 :            : 
    4650         [ -  + ]:        371 :         if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
    4651                 :        371 :                                bss->addr))
    4652                 :          0 :                 return -1;
    4653                 :            : 
    4654         [ -  + ]:        371 :         if (send_rfkill_event) {
    4655                 :          0 :                 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
    4656                 :            :                                        drv, drv->ctx);
    4657                 :            :         }
    4658                 :            : 
    4659                 :        371 :         return 0;
    4660                 :            : }
    4661                 :            : 
    4662                 :            : 
    4663                 :        431 : static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
    4664                 :            : {
    4665                 :            :         struct nl_msg *msg;
    4666                 :            : 
    4667                 :        431 :         msg = nlmsg_alloc();
    4668         [ -  + ]:        431 :         if (!msg)
    4669                 :          0 :                 return -ENOMEM;
    4670                 :            : 
    4671                 :        431 :         wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
    4672                 :            :                    drv->ifindex);
    4673                 :        431 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
    4674         [ +  - ]:        431 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    4675                 :            : 
    4676                 :        431 :         return send_and_recv_msgs(drv, msg, NULL, NULL);
    4677                 :            :  nla_put_failure:
    4678                 :          0 :         nlmsg_free(msg);
    4679                 :        431 :         return -ENOBUFS;
    4680                 :            : }
    4681                 :            : 
    4682                 :            : 
    4683                 :            : /**
    4684                 :            :  * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
    4685                 :            :  * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
    4686                 :            :  *
    4687                 :            :  * Shut down driver interface and processing of driver events. Free
    4688                 :            :  * private data buffer if one was allocated in wpa_driver_nl80211_init().
    4689                 :            :  */
    4690                 :        371 : static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
    4691                 :            : {
    4692                 :        371 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4693                 :            : 
    4694                 :        371 :         bss->in_deinit = 1;
    4695         [ +  - ]:        371 :         if (drv->data_tx_status)
    4696                 :        371 :                 eloop_unregister_read_sock(drv->eapol_tx_sock);
    4697         [ +  - ]:        371 :         if (drv->eapol_tx_sock >= 0)
    4698                 :        371 :                 close(drv->eapol_tx_sock);
    4699                 :            : 
    4700         [ -  + ]:        371 :         if (bss->nl_preq)
    4701                 :          0 :                 wpa_driver_nl80211_probe_req_report(bss, 0);
    4702         [ -  + ]:        371 :         if (bss->added_if_into_bridge) {
    4703         [ #  # ]:          0 :                 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
    4704                 :          0 :                                     bss->ifname) < 0)
    4705                 :          0 :                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
    4706                 :            :                                    "interface %s from bridge %s: %s",
    4707                 :          0 :                                    bss->ifname, bss->brname, strerror(errno));
    4708                 :            :         }
    4709         [ -  + ]:        371 :         if (bss->added_bridge) {
    4710         [ #  # ]:          0 :                 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
    4711                 :          0 :                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
    4712                 :            :                                    "bridge %s: %s",
    4713                 :          0 :                                    bss->brname, strerror(errno));
    4714                 :            :         }
    4715                 :            : 
    4716                 :        371 :         nl80211_remove_monitor_interface(drv);
    4717                 :            : 
    4718         [ +  + ]:        371 :         if (is_ap_interface(drv->nlmode))
    4719                 :        345 :                 wpa_driver_nl80211_del_beacon(drv);
    4720                 :            : 
    4721         [ +  + ]:        371 :         if (drv->eapol_sock >= 0) {
    4722                 :        332 :                 eloop_unregister_read_sock(drv->eapol_sock);
    4723                 :        332 :                 close(drv->eapol_sock);
    4724                 :            :         }
    4725                 :            : 
    4726         [ -  + ]:        371 :         if (drv->if_indices != drv->default_if_indices)
    4727                 :          0 :                 os_free(drv->if_indices);
    4728                 :            : 
    4729         [ +  + ]:        371 :         if (drv->disabled_11b_rates)
    4730                 :         13 :                 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
    4731                 :            : 
    4732                 :        371 :         netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
    4733                 :            :                                IF_OPER_UP);
    4734                 :        371 :         rfkill_deinit(drv->rfkill);
    4735                 :            : 
    4736                 :        371 :         eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
    4737                 :            : 
    4738         [ +  - ]:        371 :         if (!drv->start_iface_up)
    4739                 :        371 :                 (void) i802_set_iface_flags(bss, 0);
    4740         [ +  - ]:        371 :         if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
    4741 [ +  + ][ +  - ]:        371 :                 if (!drv->hostapd || !drv->start_mode_ap)
    4742                 :        371 :                         wpa_driver_nl80211_set_mode(bss,
    4743                 :            :                                                     NL80211_IFTYPE_STATION);
    4744                 :        371 :                 nl80211_mgmt_unsubscribe(bss, "deinit");
    4745                 :            :         } else {
    4746                 :          0 :                 nl80211_mgmt_unsubscribe(bss, "deinit");
    4747                 :          0 :                 nl80211_del_p2pdev(bss);
    4748                 :            :         }
    4749                 :        371 :         nl_cb_put(drv->nl_cb);
    4750                 :            : 
    4751                 :        371 :         nl80211_destroy_bss(drv->first_bss);
    4752                 :            : 
    4753                 :        371 :         os_free(drv->filter_ssids);
    4754                 :            : 
    4755                 :        371 :         os_free(drv->auth_ie);
    4756                 :            : 
    4757         [ +  - ]:        371 :         if (drv->in_interface_list)
    4758                 :        371 :                 dl_list_del(&drv->list);
    4759                 :            : 
    4760                 :        371 :         os_free(drv->extended_capa);
    4761                 :        371 :         os_free(drv->extended_capa_mask);
    4762                 :        371 :         os_free(drv->first_bss);
    4763                 :        371 :         os_free(drv);
    4764                 :        371 : }
    4765                 :            : 
    4766                 :            : 
    4767                 :            : /**
    4768                 :            :  * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
    4769                 :            :  * @eloop_ctx: Driver private data
    4770                 :            :  * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
    4771                 :            :  *
    4772                 :            :  * This function can be used as registered timeout when starting a scan to
    4773                 :            :  * generate a scan completed event if the driver does not report this.
    4774                 :            :  */
    4775                 :          0 : static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
    4776                 :            : {
    4777                 :          0 :         struct wpa_driver_nl80211_data *drv = eloop_ctx;
    4778         [ #  # ]:          0 :         if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
    4779                 :          0 :                 wpa_driver_nl80211_set_mode(drv->first_bss,
    4780                 :            :                                             drv->ap_scan_as_station);
    4781                 :          0 :                 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
    4782                 :            :         }
    4783                 :          0 :         wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
    4784                 :          0 :         wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
    4785                 :          0 : }
    4786                 :            : 
    4787                 :            : 
    4788                 :            : static struct nl_msg *
    4789                 :       1382 : nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
    4790                 :            :                     struct wpa_driver_scan_params *params, u64 *wdev_id)
    4791                 :            : {
    4792                 :            :         struct nl_msg *msg;
    4793                 :            :         size_t i;
    4794                 :            : 
    4795                 :       1382 :         msg = nlmsg_alloc();
    4796         [ -  + ]:       1382 :         if (!msg)
    4797                 :          0 :                 return NULL;
    4798                 :            : 
    4799                 :       1382 :         nl80211_cmd(drv, msg, 0, cmd);
    4800                 :            : 
    4801         [ +  - ]:       1382 :         if (!wdev_id)
    4802         [ +  - ]:       1382 :                 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    4803                 :            :         else
    4804         [ #  # ]:          0 :                 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
    4805                 :            : 
    4806         [ +  + ]:       1382 :         if (params->num_ssids) {
    4807                 :            :                 struct nlattr *ssids;
    4808                 :            : 
    4809                 :       1353 :                 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
    4810         [ -  + ]:       1353 :                 if (ssids == NULL)
    4811                 :          0 :                         goto fail;
    4812         [ +  + ]:       2709 :                 for (i = 0; i < params->num_ssids; i++) {
    4813                 :       1356 :                         wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
    4814                 :       1356 :                                           params->ssids[i].ssid,
    4815                 :            :                                           params->ssids[i].ssid_len);
    4816         [ -  + ]:       1356 :                         if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
    4817                 :       1356 :                                     params->ssids[i].ssid) < 0)
    4818                 :          0 :                                 goto fail;
    4819                 :            :                 }
    4820                 :       1353 :                 nla_nest_end(msg, ssids);
    4821                 :            :         }
    4822                 :            : 
    4823         [ +  + ]:       1382 :         if (params->extra_ies) {
    4824                 :       1353 :                 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
    4825                 :       1353 :                             params->extra_ies, params->extra_ies_len);
    4826         [ -  + ]:       1353 :                 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
    4827                 :       1353 :                             params->extra_ies) < 0)
    4828                 :          0 :                         goto fail;
    4829                 :            :         }
    4830                 :            : 
    4831         [ +  + ]:       1382 :         if (params->freqs) {
    4832                 :            :                 struct nlattr *freqs;
    4833                 :       1234 :                 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
    4834         [ -  + ]:       1234 :                 if (freqs == NULL)
    4835                 :          0 :                         goto fail;
    4836         [ +  + ]:       3954 :                 for (i = 0; params->freqs[i]; i++) {
    4837                 :       2720 :                         wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
    4838                 :       2720 :                                    "MHz", params->freqs[i]);
    4839         [ -  + ]:       2720 :                         if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
    4840                 :          0 :                                 goto fail;
    4841                 :            :                 }
    4842                 :       1234 :                 nla_nest_end(msg, freqs);
    4843                 :            :         }
    4844                 :            : 
    4845                 :       1382 :         os_free(drv->filter_ssids);
    4846                 :       1382 :         drv->filter_ssids = params->filter_ssids;
    4847                 :       1382 :         params->filter_ssids = NULL;
    4848                 :       1382 :         drv->num_filter_ssids = params->num_filter_ssids;
    4849                 :            : 
    4850         [ +  + ]:       1382 :         if (params->only_new_results) {
    4851                 :        471 :                 wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
    4852         [ +  - ]:        471 :                 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_FLAGS,
    4853                 :            :                             NL80211_SCAN_FLAG_FLUSH);
    4854                 :            :         }
    4855                 :            : 
    4856                 :       1382 :         return msg;
    4857                 :            : 
    4858                 :            : fail:
    4859                 :            : nla_put_failure:
    4860                 :          0 :         nlmsg_free(msg);
    4861                 :       1382 :         return NULL;
    4862                 :            : }
    4863                 :            : 
    4864                 :            : 
    4865                 :            : /**
    4866                 :            :  * wpa_driver_nl80211_scan - Request the driver to initiate scan
    4867                 :            :  * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
    4868                 :            :  * @params: Scan parameters
    4869                 :            :  * Returns: 0 on success, -1 on failure
    4870                 :            :  */
    4871                 :       1381 : static int wpa_driver_nl80211_scan(struct i802_bss *bss,
    4872                 :            :                                    struct wpa_driver_scan_params *params)
    4873                 :            : {
    4874                 :       1381 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4875                 :       1381 :         int ret = -1, timeout;
    4876                 :       1381 :         struct nl_msg *msg = NULL;
    4877                 :            : 
    4878                 :       1381 :         wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
    4879                 :       1381 :         drv->scan_for_auth = 0;
    4880                 :            : 
    4881         [ -  + ]:       1381 :         msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
    4882                 :       1381 :                                   bss->wdev_id_set ? &bss->wdev_id : NULL);
    4883         [ -  + ]:       1381 :         if (!msg)
    4884                 :          0 :                 return -1;
    4885                 :            : 
    4886         [ +  + ]:       1381 :         if (params->p2p_probe) {
    4887                 :            :                 struct nlattr *rates;
    4888                 :            : 
    4889                 :        767 :                 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
    4890                 :            : 
    4891                 :        767 :                 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
    4892         [ -  + ]:        767 :                 if (rates == NULL)
    4893                 :          0 :                         goto nla_put_failure;
    4894                 :            : 
    4895                 :            :                 /*
    4896                 :            :                  * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
    4897                 :            :                  * by masking out everything else apart from the OFDM rates 6,
    4898                 :            :                  * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
    4899                 :            :                  * rates are left enabled.
    4900                 :            :                  */
    4901         [ +  - ]:        767 :                 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
    4902                 :            :                         "\x0c\x12\x18\x24\x30\x48\x60\x6c");
    4903                 :        767 :                 nla_nest_end(msg, rates);
    4904                 :            : 
    4905         [ +  - ]:        767 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
    4906                 :            :         }
    4907                 :            : 
    4908                 :       1381 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    4909                 :       1381 :         msg = NULL;
    4910         [ +  + ]:       1381 :         if (ret) {
    4911                 :          1 :                 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
    4912                 :            :                            "(%s)", ret, strerror(-ret));
    4913 [ #  # ][ -  + ]:          1 :                 if (drv->hostapd && is_ap_interface(drv->nlmode)) {
    4914                 :          0 :                         enum nl80211_iftype old_mode = drv->nlmode;
    4915                 :            : 
    4916                 :            :                         /*
    4917                 :            :                          * mac80211 does not allow scan requests in AP mode, so
    4918                 :            :                          * try to do this in station mode.
    4919                 :            :                          */
    4920         [ #  # ]:          0 :                         if (wpa_driver_nl80211_set_mode(
    4921                 :            :                                     bss, NL80211_IFTYPE_STATION))
    4922                 :          0 :                                 goto nla_put_failure;
    4923                 :            : 
    4924         [ #  # ]:          0 :                         if (wpa_driver_nl80211_scan(bss, params)) {
    4925                 :          0 :                                 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
    4926                 :          0 :                                 goto nla_put_failure;
    4927                 :            :                         }
    4928                 :            : 
    4929                 :            :                         /* Restore AP mode when processing scan results */
    4930                 :          0 :                         drv->ap_scan_as_station = old_mode;
    4931                 :          0 :                         ret = 0;
    4932                 :            :                 } else
    4933                 :            :                         goto nla_put_failure;
    4934                 :            :         }
    4935                 :            : 
    4936                 :       1380 :         drv->scan_state = SCAN_REQUESTED;
    4937                 :            :         /* Not all drivers generate "scan completed" wireless event, so try to
    4938                 :            :          * read results after a timeout. */
    4939                 :       1380 :         timeout = 10;
    4940         [ +  + ]:       1380 :         if (drv->scan_complete_events) {
    4941                 :            :                 /*
    4942                 :            :                  * The driver seems to deliver events to notify when scan is
    4943                 :            :                  * complete, so use longer timeout to avoid race conditions
    4944                 :            :                  * with scanning and following association request.
    4945                 :            :                  */
    4946                 :       1340 :                 timeout = 30;
    4947                 :            :         }
    4948                 :       1380 :         wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
    4949                 :            :                    "seconds", ret, timeout);
    4950                 :       1380 :         eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
    4951                 :       1380 :         eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
    4952                 :            :                                drv, drv->ctx);
    4953                 :            : 
    4954                 :            : nla_put_failure:
    4955                 :       1381 :         nlmsg_free(msg);
    4956                 :       1381 :         return ret;
    4957                 :            : }
    4958                 :            : 
    4959                 :            : 
    4960                 :            : /**
    4961                 :            :  * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
    4962                 :            :  * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
    4963                 :            :  * @params: Scan parameters
    4964                 :            :  * @interval: Interval between scan cycles in milliseconds
    4965                 :            :  * Returns: 0 on success, -1 on failure or if not supported
    4966                 :            :  */
    4967                 :          1 : static int wpa_driver_nl80211_sched_scan(void *priv,
    4968                 :            :                                          struct wpa_driver_scan_params *params,
    4969                 :            :                                          u32 interval)
    4970                 :            : {
    4971                 :          1 :         struct i802_bss *bss = priv;
    4972                 :          1 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    4973                 :          1 :         int ret = -1;
    4974                 :            :         struct nl_msg *msg;
    4975                 :            :         size_t i;
    4976                 :            : 
    4977                 :          1 :         wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
    4978                 :            : 
    4979                 :            : #ifdef ANDROID
    4980                 :            :         if (!drv->capa.sched_scan_supported)
    4981                 :            :                 return android_pno_start(bss, params);
    4982                 :            : #endif /* ANDROID */
    4983                 :            : 
    4984         [ -  + ]:          1 :         msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
    4985                 :          1 :                                   bss->wdev_id_set ? &bss->wdev_id : NULL);
    4986         [ -  + ]:          1 :         if (!msg)
    4987                 :          0 :                 goto nla_put_failure;
    4988                 :            : 
    4989         [ +  - ]:          1 :         NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
    4990                 :            : 
    4991 [ +  - ][ +  - ]:          1 :         if ((drv->num_filter_ssids &&
    4992         [ -  + ]:          1 :             (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
    4993                 :          1 :             params->filter_rssi) {
    4994                 :            :                 struct nlattr *match_sets;
    4995                 :          0 :                 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
    4996         [ #  # ]:          0 :                 if (match_sets == NULL)
    4997                 :          0 :                         goto nla_put_failure;
    4998                 :            : 
    4999         [ #  # ]:          0 :                 for (i = 0; i < drv->num_filter_ssids; i++) {
    5000                 :            :                         struct nlattr *match_set_ssid;
    5001                 :          0 :                         wpa_hexdump_ascii(MSG_MSGDUMP,
    5002                 :            :                                           "nl80211: Sched scan filter SSID",
    5003                 :          0 :                                           drv->filter_ssids[i].ssid,
    5004                 :          0 :                                           drv->filter_ssids[i].ssid_len);
    5005                 :            : 
    5006                 :          0 :                         match_set_ssid = nla_nest_start(msg, i + 1);
    5007         [ #  # ]:          0 :                         if (match_set_ssid == NULL)
    5008                 :          0 :                                 goto nla_put_failure;
    5009         [ #  # ]:          0 :                         NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
    5010                 :            :                                 drv->filter_ssids[i].ssid_len,
    5011                 :            :                                 drv->filter_ssids[i].ssid);
    5012         [ #  # ]:          0 :                         if (params->filter_rssi)
    5013         [ #  # ]:          0 :                                 NLA_PUT_U32(msg,
    5014                 :            :                                             NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
    5015                 :            :                                             params->filter_rssi);
    5016                 :            : 
    5017                 :          0 :                         nla_nest_end(msg, match_set_ssid);
    5018                 :            :                 }
    5019                 :            : 
    5020                 :            :                 /*
    5021                 :            :                  * Due to backward compatibility code, newer kernels treat this
    5022                 :            :                  * matchset (with only an RSSI filter) as the default for all
    5023                 :            :                  * other matchsets, unless it's the only one, in which case the
    5024                 :            :                  * matchset will actually allow all SSIDs above the RSSI.
    5025                 :            :                  */
    5026         [ #  # ]:          0 :                 if (params->filter_rssi) {
    5027                 :            :                         struct nlattr *match_set_rssi;
    5028                 :          0 :                         match_set_rssi = nla_nest_start(msg, 0);
    5029         [ #  # ]:          0 :                         if (match_set_rssi == NULL)
    5030                 :          0 :                                 goto nla_put_failure;
    5031         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
    5032                 :            :                                     params->filter_rssi);
    5033                 :          0 :                         wpa_printf(MSG_MSGDUMP,
    5034                 :            :                                    "nl80211: Sched scan RSSI filter %d dBm",
    5035                 :            :                                    params->filter_rssi);
    5036                 :          0 :                         nla_nest_end(msg, match_set_rssi);
    5037                 :            :                 }
    5038                 :            : 
    5039                 :          0 :                 nla_nest_end(msg, match_sets);
    5040                 :            :         }
    5041                 :            : 
    5042                 :          1 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    5043                 :            : 
    5044                 :            :         /* TODO: if we get an error here, we should fall back to normal scan */
    5045                 :            : 
    5046                 :          1 :         msg = NULL;
    5047         [ +  - ]:          1 :         if (ret) {
    5048                 :          1 :                 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
    5049                 :            :                            "ret=%d (%s)", ret, strerror(-ret));
    5050                 :          1 :                 goto nla_put_failure;
    5051                 :            :         }
    5052                 :            : 
    5053                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
    5054                 :            :                    "scan interval %d msec", ret, interval);
    5055                 :            : 
    5056                 :            : nla_put_failure:
    5057                 :          1 :         nlmsg_free(msg);
    5058                 :          1 :         return ret;
    5059                 :            : }
    5060                 :            : 
    5061                 :            : 
    5062                 :            : /**
    5063                 :            :  * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
    5064                 :            :  * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
    5065                 :            :  * Returns: 0 on success, -1 on failure or if not supported
    5066                 :            :  */
    5067                 :          0 : static int wpa_driver_nl80211_stop_sched_scan(void *priv)
    5068                 :            : {
    5069                 :          0 :         struct i802_bss *bss = priv;
    5070                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    5071                 :          0 :         int ret = 0;
    5072                 :            :         struct nl_msg *msg;
    5073                 :            : 
    5074                 :            : #ifdef ANDROID
    5075                 :            :         if (!drv->capa.sched_scan_supported)
    5076                 :            :                 return android_pno_stop(bss);
    5077                 :            : #endif /* ANDROID */
    5078                 :            : 
    5079                 :          0 :         msg = nlmsg_alloc();
    5080         [ #  # ]:          0 :         if (!msg)
    5081                 :          0 :                 return -1;
    5082                 :            : 
    5083                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
    5084                 :            : 
    5085         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    5086                 :            : 
    5087                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    5088                 :          0 :         msg = NULL;
    5089         [ #  # ]:          0 :         if (ret) {
    5090                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
    5091                 :            :                            "ret=%d (%s)", ret, strerror(-ret));
    5092                 :          0 :                 goto nla_put_failure;
    5093                 :            :         }
    5094                 :            : 
    5095                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
    5096                 :            : 
    5097                 :            : nla_put_failure:
    5098                 :          0 :         nlmsg_free(msg);
    5099                 :          0 :         return ret;
    5100                 :            : }
    5101                 :            : 
    5102                 :            : 
    5103                 :        902 : static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
    5104                 :            : {
    5105                 :            :         const u8 *end, *pos;
    5106                 :            : 
    5107         [ -  + ]:        902 :         if (ies == NULL)
    5108                 :          0 :                 return NULL;
    5109                 :            : 
    5110                 :        902 :         pos = ies;
    5111                 :        902 :         end = ies + ies_len;
    5112                 :            : 
    5113         [ +  - ]:        902 :         while (pos + 1 < end) {
    5114         [ -  + ]:        902 :                 if (pos + 2 + pos[1] > end)
    5115                 :          0 :                         break;
    5116         [ +  - ]:        902 :                 if (pos[0] == ie)
    5117                 :        902 :                         return pos;
    5118                 :          0 :                 pos += 2 + pos[1];
    5119                 :            :         }
    5120                 :            : 
    5121                 :        902 :         return NULL;
    5122                 :            : }
    5123                 :            : 
    5124                 :            : 
    5125                 :       2293 : static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
    5126                 :            :                                  const u8 *ie, size_t ie_len)
    5127                 :            : {
    5128                 :            :         const u8 *ssid;
    5129                 :            :         size_t i;
    5130                 :            : 
    5131         [ +  - ]:       2293 :         if (drv->filter_ssids == NULL)
    5132                 :       2293 :                 return 0;
    5133                 :            : 
    5134                 :          0 :         ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
    5135         [ #  # ]:          0 :         if (ssid == NULL)
    5136                 :          0 :                 return 1;
    5137                 :            : 
    5138         [ #  # ]:          0 :         for (i = 0; i < drv->num_filter_ssids; i++) {
    5139 [ #  # ][ #  # ]:          0 :                 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
    5140                 :          0 :                     os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
    5141                 :            :                     0)
    5142                 :          0 :                         return 0;
    5143                 :            :         }
    5144                 :            : 
    5145                 :       2293 :         return 1;
    5146                 :            : }
    5147                 :            : 
    5148                 :            : 
    5149                 :       2303 : static int bss_info_handler(struct nl_msg *msg, void *arg)
    5150                 :            : {
    5151                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    5152                 :       2303 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    5153                 :            :         struct nlattr *bss[NL80211_BSS_MAX + 1];
    5154                 :            :         static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
    5155                 :            :                 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
    5156                 :            :                 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
    5157                 :            :                 [NL80211_BSS_TSF] = { .type = NLA_U64 },
    5158                 :            :                 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
    5159                 :            :                 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
    5160                 :            :                 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
    5161                 :            :                 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
    5162                 :            :                 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
    5163                 :            :                 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
    5164                 :            :                 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
    5165                 :            :                 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
    5166                 :            :         };
    5167                 :       2303 :         struct nl80211_bss_info_arg *_arg = arg;
    5168                 :       2303 :         struct wpa_scan_results *res = _arg->res;
    5169                 :            :         struct wpa_scan_res **tmp;
    5170                 :            :         struct wpa_scan_res *r;
    5171                 :            :         const u8 *ie, *beacon_ie;
    5172                 :            :         size_t ie_len, beacon_ie_len;
    5173                 :            :         u8 *pos;
    5174                 :            :         size_t i;
    5175                 :            : 
    5176                 :       2303 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    5177                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    5178         [ -  + ]:       2303 :         if (!tb[NL80211_ATTR_BSS])
    5179                 :          0 :                 return NL_SKIP;
    5180         [ -  + ]:       2303 :         if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
    5181                 :            :                              bss_policy))
    5182                 :          0 :                 return NL_SKIP;
    5183         [ +  + ]:       2303 :         if (bss[NL80211_BSS_STATUS]) {
    5184                 :            :                 enum nl80211_bss_status status;
    5185                 :         44 :                 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
    5186 [ +  - ][ +  - ]:         44 :                 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
    5187                 :         44 :                     bss[NL80211_BSS_FREQUENCY]) {
    5188                 :         44 :                         _arg->assoc_freq =
    5189                 :         44 :                                 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
    5190                 :         44 :                         wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
    5191                 :            :                                    _arg->assoc_freq);
    5192                 :            :                 }
    5193 [ +  - ][ +  - ]:         44 :                 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
    5194                 :         44 :                     bss[NL80211_BSS_BSSID]) {
    5195                 :         44 :                         os_memcpy(_arg->assoc_bssid,
    5196                 :            :                                   nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
    5197                 :         44 :                         wpa_printf(MSG_DEBUG, "nl80211: Associated with "
    5198                 :        264 :                                    MACSTR, MAC2STR(_arg->assoc_bssid));
    5199                 :            :                 }
    5200                 :            :         }
    5201         [ +  + ]:       2303 :         if (!res)
    5202                 :         10 :                 return NL_SKIP;
    5203         [ +  - ]:       2293 :         if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
    5204                 :       2293 :                 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
    5205                 :       2293 :                 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
    5206                 :            :         } else {
    5207                 :          0 :                 ie = NULL;
    5208                 :          0 :                 ie_len = 0;
    5209                 :            :         }
    5210         [ +  + ]:       2293 :         if (bss[NL80211_BSS_BEACON_IES]) {
    5211                 :       1433 :                 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
    5212                 :       1433 :                 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
    5213                 :            :         } else {
    5214                 :        860 :                 beacon_ie = NULL;
    5215                 :        860 :                 beacon_ie_len = 0;
    5216                 :            :         }
    5217                 :            : 
    5218 [ +  - ][ +  - ]:       2293 :         if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
                 [ -  + ]
    5219                 :            :                                   ie ? ie_len : beacon_ie_len))
    5220                 :          0 :                 return NL_SKIP;
    5221                 :            : 
    5222                 :       2293 :         r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
    5223         [ -  + ]:       2293 :         if (r == NULL)
    5224                 :          0 :                 return NL_SKIP;
    5225         [ +  - ]:       2293 :         if (bss[NL80211_BSS_BSSID])
    5226                 :       2293 :                 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
    5227                 :            :                           ETH_ALEN);
    5228         [ +  - ]:       2293 :         if (bss[NL80211_BSS_FREQUENCY])
    5229                 :       2293 :                 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
    5230         [ +  - ]:       2293 :         if (bss[NL80211_BSS_BEACON_INTERVAL])
    5231                 :       2293 :                 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
    5232         [ +  - ]:       2293 :         if (bss[NL80211_BSS_CAPABILITY])
    5233                 :       2293 :                 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
    5234                 :       2293 :         r->flags |= WPA_SCAN_NOISE_INVALID;
    5235         [ +  - ]:       2293 :         if (bss[NL80211_BSS_SIGNAL_MBM]) {
    5236                 :       2293 :                 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
    5237                 :       2293 :                 r->level /= 100; /* mBm to dBm */
    5238                 :       2293 :                 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
    5239         [ #  # ]:          0 :         } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
    5240                 :          0 :                 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
    5241                 :          0 :                 r->flags |= WPA_SCAN_QUAL_INVALID;
    5242                 :            :         } else
    5243                 :          0 :                 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
    5244         [ +  - ]:       2293 :         if (bss[NL80211_BSS_TSF])
    5245                 :       2293 :                 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
    5246         [ +  - ]:       2293 :         if (bss[NL80211_BSS_SEEN_MS_AGO])
    5247                 :       2293 :                 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
    5248                 :       2293 :         r->ie_len = ie_len;
    5249                 :       2293 :         pos = (u8 *) (r + 1);
    5250         [ +  - ]:       2293 :         if (ie) {
    5251                 :       2293 :                 os_memcpy(pos, ie, ie_len);
    5252                 :       2293 :                 pos += ie_len;
    5253                 :            :         }
    5254                 :       2293 :         r->beacon_ie_len = beacon_ie_len;
    5255         [ +  + ]:       2293 :         if (beacon_ie)
    5256                 :       1433 :                 os_memcpy(pos, beacon_ie, beacon_ie_len);
    5257                 :            : 
    5258         [ +  + ]:       2293 :         if (bss[NL80211_BSS_STATUS]) {
    5259                 :            :                 enum nl80211_bss_status status;
    5260                 :         40 :                 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
    5261      [ -  +  - ]:         40 :                 switch (status) {
    5262                 :            :                 case NL80211_BSS_STATUS_AUTHENTICATED:
    5263                 :          0 :                         r->flags |= WPA_SCAN_AUTHENTICATED;
    5264                 :          0 :                         break;
    5265                 :            :                 case NL80211_BSS_STATUS_ASSOCIATED:
    5266                 :         40 :                         r->flags |= WPA_SCAN_ASSOCIATED;
    5267                 :         40 :                         break;
    5268                 :            :                 default:
    5269                 :          0 :                         break;
    5270                 :            :                 }
    5271                 :            :         }
    5272                 :            : 
    5273                 :            :         /*
    5274                 :            :          * cfg80211 maintains separate BSS table entries for APs if the same
    5275                 :            :          * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
    5276                 :            :          * not use frequency as a separate key in the BSS table, so filter out
    5277                 :            :          * duplicated entries. Prefer associated BSS entry in such a case in
    5278                 :            :          * order to get the correct frequency into the BSS table. Similarly,
    5279                 :            :          * prefer newer entries over older.
    5280                 :            :          */
    5281         [ +  + ]:       3598 :         for (i = 0; i < res->num; i++) {
    5282                 :            :                 const u8 *s1, *s2;
    5283         [ +  + ]:       1305 :                 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
    5284                 :        854 :                         continue;
    5285                 :            : 
    5286                 :        451 :                 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
    5287                 :        451 :                                     res->res[i]->ie_len, WLAN_EID_SSID);
    5288                 :        451 :                 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
    5289 [ +  - ][ +  + ]:        451 :                 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
         [ +  - ][ +  - ]
    5290                 :         53 :                     os_memcmp(s1, s2, 2 + s1[1]) != 0)
    5291                 :        451 :                         continue;
    5292                 :            : 
    5293                 :            :                 /* Same BSSID,SSID was already included in scan results */
    5294                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
    5295                 :          0 :                            "for " MACSTR, MAC2STR(r->bssid));
    5296                 :            : 
    5297 [ #  # ][ #  # ]:          0 :                 if (((r->flags & WPA_SCAN_ASSOCIATED) &&
    5298         [ #  # ]:          0 :                      !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) ||
    5299                 :          0 :                     r->age < res->res[i]->age) {
    5300                 :          0 :                         os_free(res->res[i]);
    5301                 :          0 :                         res->res[i] = r;
    5302                 :            :                 } else
    5303                 :          0 :                         os_free(r);
    5304                 :          0 :                 return NL_SKIP;
    5305                 :            :         }
    5306                 :            : 
    5307                 :       2293 :         tmp = os_realloc_array(res->res, res->num + 1,
    5308                 :            :                                sizeof(struct wpa_scan_res *));
    5309         [ -  + ]:       2293 :         if (tmp == NULL) {
    5310                 :          0 :                 os_free(r);
    5311                 :          0 :                 return NL_SKIP;
    5312                 :            :         }
    5313                 :       2293 :         tmp[res->num++] = r;
    5314                 :       2293 :         res->res = tmp;
    5315                 :            : 
    5316                 :       2303 :         return NL_SKIP;
    5317                 :            : }
    5318                 :            : 
    5319                 :            : 
    5320                 :          0 : static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
    5321                 :            :                                  const u8 *addr)
    5322                 :            : {
    5323         [ #  # ]:          0 :         if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
    5324                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
    5325                 :          0 :                            "mismatch (" MACSTR ")", MAC2STR(addr));
    5326                 :          0 :                 wpa_driver_nl80211_mlme(drv, addr,
    5327                 :            :                                         NL80211_CMD_DEAUTHENTICATE,
    5328                 :            :                                         WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
    5329                 :            :         }
    5330                 :          0 : }
    5331                 :            : 
    5332                 :            : 
    5333                 :       1363 : static void wpa_driver_nl80211_check_bss_status(
    5334                 :            :         struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
    5335                 :            : {
    5336                 :            :         size_t i;
    5337                 :            : 
    5338         [ +  + ]:       3656 :         for (i = 0; i < res->num; i++) {
    5339                 :       2293 :                 struct wpa_scan_res *r = res->res[i];
    5340         [ -  + ]:       2293 :                 if (r->flags & WPA_SCAN_AUTHENTICATED) {
    5341                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Scan results "
    5342                 :            :                                    "indicates BSS status with " MACSTR
    5343                 :            :                                    " as authenticated",
    5344                 :          0 :                                    MAC2STR(r->bssid));
    5345 [ #  # ][ #  # ]:          0 :                         if (is_sta_interface(drv->nlmode) &&
    5346         [ #  # ]:          0 :                             os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
    5347                 :          0 :                             os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
    5348                 :            :                             0) {
    5349                 :          0 :                                 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
    5350                 :            :                                            " in local state (auth=" MACSTR
    5351                 :            :                                            " assoc=" MACSTR ")",
    5352                 :          0 :                                            MAC2STR(drv->auth_bssid),
    5353                 :          0 :                                            MAC2STR(drv->bssid));
    5354                 :          0 :                                 clear_state_mismatch(drv, r->bssid);
    5355                 :            :                         }
    5356                 :            :                 }
    5357                 :            : 
    5358         [ +  + ]:       2293 :                 if (r->flags & WPA_SCAN_ASSOCIATED) {
    5359                 :         40 :                         wpa_printf(MSG_DEBUG, "nl80211: Scan results "
    5360                 :            :                                    "indicate BSS status with " MACSTR
    5361                 :            :                                    " as associated",
    5362                 :        240 :                                    MAC2STR(r->bssid));
    5363 [ -  + ][ +  - ]:         40 :                         if (is_sta_interface(drv->nlmode) &&
    5364                 :         40 :                             !drv->associated) {
    5365                 :          0 :                                 wpa_printf(MSG_DEBUG, "nl80211: Local state "
    5366                 :            :                                            "(not associated) does not match "
    5367                 :            :                                            "with BSS state");
    5368                 :          0 :                                 clear_state_mismatch(drv, r->bssid);
    5369 [ +  - ][ -  + ]:         40 :                         } else if (is_sta_interface(drv->nlmode) &&
    5370                 :         40 :                                    os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
    5371                 :            :                                    0) {
    5372                 :          0 :                                 wpa_printf(MSG_DEBUG, "nl80211: Local state "
    5373                 :            :                                            "(associated with " MACSTR ") does "
    5374                 :            :                                            "not match with BSS state",
    5375                 :          0 :                                            MAC2STR(drv->bssid));
    5376                 :          0 :                                 clear_state_mismatch(drv, r->bssid);
    5377                 :          0 :                                 clear_state_mismatch(drv, drv->bssid);
    5378                 :            :                         }
    5379                 :            :                 }
    5380                 :            :         }
    5381                 :       1363 : }
    5382                 :            : 
    5383                 :            : 
    5384                 :            : static struct wpa_scan_results *
    5385                 :       1363 : nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
    5386                 :            : {
    5387                 :            :         struct nl_msg *msg;
    5388                 :            :         struct wpa_scan_results *res;
    5389                 :            :         int ret;
    5390                 :            :         struct nl80211_bss_info_arg arg;
    5391                 :            : 
    5392                 :       1363 :         res = os_zalloc(sizeof(*res));
    5393         [ -  + ]:       1363 :         if (res == NULL)
    5394                 :          0 :                 return NULL;
    5395                 :       1363 :         msg = nlmsg_alloc();
    5396         [ -  + ]:       1363 :         if (!msg)
    5397                 :          0 :                 goto nla_put_failure;
    5398                 :            : 
    5399                 :       1363 :         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
    5400         [ -  + ]:       1363 :         if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
    5401                 :          0 :                 goto nla_put_failure;
    5402                 :            : 
    5403                 :       1363 :         arg.drv = drv;
    5404                 :       1363 :         arg.res = res;
    5405                 :       1363 :         ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
    5406                 :       1363 :         msg = NULL;
    5407         [ +  - ]:       1363 :         if (ret == 0) {
    5408                 :       1363 :                 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
    5409                 :            :                            "BSSes)", (unsigned long) res->num);
    5410                 :       1363 :                 nl80211_get_noise_for_scan_results(drv, res);
    5411                 :       1363 :                 return res;
    5412                 :            :         }
    5413                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
    5414                 :            :                    "(%s)", ret, strerror(-ret));
    5415                 :            : nla_put_failure:
    5416                 :          0 :         nlmsg_free(msg);
    5417                 :          0 :         wpa_scan_results_free(res);
    5418                 :       1363 :         return NULL;
    5419                 :            : }
    5420                 :            : 
    5421                 :            : 
    5422                 :            : /**
    5423                 :            :  * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
    5424                 :            :  * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
    5425                 :            :  * Returns: Scan results on success, -1 on failure
    5426                 :            :  */
    5427                 :            : static struct wpa_scan_results *
    5428                 :       1363 : wpa_driver_nl80211_get_scan_results(void *priv)
    5429                 :            : {
    5430                 :       1363 :         struct i802_bss *bss = priv;
    5431                 :       1363 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    5432                 :            :         struct wpa_scan_results *res;
    5433                 :            : 
    5434                 :       1363 :         res = nl80211_get_scan_results(drv);
    5435         [ +  - ]:       1363 :         if (res)
    5436                 :       1363 :                 wpa_driver_nl80211_check_bss_status(drv, res);
    5437                 :       1363 :         return res;
    5438                 :            : }
    5439                 :            : 
    5440                 :            : 
    5441                 :          0 : static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
    5442                 :            : {
    5443                 :            :         struct wpa_scan_results *res;
    5444                 :            :         size_t i;
    5445                 :            : 
    5446                 :          0 :         res = nl80211_get_scan_results(drv);
    5447         [ #  # ]:          0 :         if (res == NULL) {
    5448                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
    5449                 :          0 :                 return;
    5450                 :            :         }
    5451                 :            : 
    5452                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
    5453         [ #  # ]:          0 :         for (i = 0; i < res->num; i++) {
    5454                 :          0 :                 struct wpa_scan_res *r = res->res[i];
    5455 [ #  # ][ #  # ]:          0 :                 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
    5456                 :          0 :                            (int) i, (int) res->num, MAC2STR(r->bssid),
    5457                 :          0 :                            r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
    5458                 :          0 :                            r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
    5459                 :            :         }
    5460                 :            : 
    5461                 :          0 :         wpa_scan_results_free(res);
    5462                 :            : }
    5463                 :            : 
    5464                 :            : 
    5465                 :       2241 : static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
    5466                 :            : {
    5467   [ +  +  +  -  :       2241 :         switch (alg) {
          -  -  +  -  -  
             -  -  -  -  
                      + ]
    5468                 :            :         case WPA_ALG_WEP:
    5469         [ +  + ]:         43 :                 if (key_len == 5)
    5470                 :         19 :                         return WLAN_CIPHER_SUITE_WEP40;
    5471                 :         24 :                 return WLAN_CIPHER_SUITE_WEP104;
    5472                 :            :         case WPA_ALG_TKIP:
    5473                 :         87 :                 return WLAN_CIPHER_SUITE_TKIP;
    5474                 :            :         case WPA_ALG_CCMP:
    5475                 :       1917 :                 return WLAN_CIPHER_SUITE_CCMP;
    5476                 :            :         case WPA_ALG_GCMP:
    5477                 :          0 :                 return WLAN_CIPHER_SUITE_GCMP;
    5478                 :            :         case WPA_ALG_CCMP_256:
    5479                 :          0 :                 return WLAN_CIPHER_SUITE_CCMP_256;
    5480                 :            :         case WPA_ALG_GCMP_256:
    5481                 :          0 :                 return WLAN_CIPHER_SUITE_GCMP_256;
    5482                 :            :         case WPA_ALG_IGTK:
    5483                 :        192 :                 return WLAN_CIPHER_SUITE_AES_CMAC;
    5484                 :            :         case WPA_ALG_BIP_GMAC_128:
    5485                 :          0 :                 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
    5486                 :            :         case WPA_ALG_BIP_GMAC_256:
    5487                 :          0 :                 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
    5488                 :            :         case WPA_ALG_BIP_CMAC_256:
    5489                 :          0 :                 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
    5490                 :            :         case WPA_ALG_SMS4:
    5491                 :          0 :                 return WLAN_CIPHER_SUITE_SMS4;
    5492                 :            :         case WPA_ALG_KRK:
    5493                 :          0 :                 return WLAN_CIPHER_SUITE_KRK;
    5494                 :            :         case WPA_ALG_NONE:
    5495                 :            :         case WPA_ALG_PMK:
    5496                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
    5497                 :            :                            alg);
    5498                 :          0 :                 return 0;
    5499                 :            :         }
    5500                 :            : 
    5501                 :          2 :         wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
    5502                 :            :                    alg);
    5503                 :       2241 :         return 0;
    5504                 :            : }
    5505                 :            : 
    5506                 :            : 
    5507                 :       2019 : static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
    5508                 :            : {
    5509   [ -  -  +  -  :       2019 :         switch (cipher) {
             +  +  +  -  
                      + ]
    5510                 :            :         case WPA_CIPHER_CCMP_256:
    5511                 :          0 :                 return WLAN_CIPHER_SUITE_CCMP_256;
    5512                 :            :         case WPA_CIPHER_GCMP_256:
    5513                 :          0 :                 return WLAN_CIPHER_SUITE_GCMP_256;
    5514                 :            :         case WPA_CIPHER_CCMP:
    5515                 :       1801 :                 return WLAN_CIPHER_SUITE_CCMP;
    5516                 :            :         case WPA_CIPHER_GCMP:
    5517                 :          0 :                 return WLAN_CIPHER_SUITE_GCMP;
    5518                 :            :         case WPA_CIPHER_TKIP:
    5519                 :         94 :                 return WLAN_CIPHER_SUITE_TKIP;
    5520                 :            :         case WPA_CIPHER_WEP104:
    5521                 :          9 :                 return WLAN_CIPHER_SUITE_WEP104;
    5522                 :            :         case WPA_CIPHER_WEP40:
    5523                 :         18 :                 return WLAN_CIPHER_SUITE_WEP40;
    5524                 :            :         case WPA_CIPHER_GTK_NOT_USED:
    5525                 :          0 :                 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
    5526                 :            :         }
    5527                 :            : 
    5528                 :       2019 :         return 0;
    5529                 :            : }
    5530                 :            : 
    5531                 :            : 
    5532                 :       1154 : static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
    5533                 :            :                                        int max_suites)
    5534                 :            : {
    5535                 :       1154 :         int num_suites = 0;
    5536                 :            : 
    5537 [ +  - ][ -  + ]:       1154 :         if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
    5538                 :          0 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
    5539 [ +  - ][ -  + ]:       1154 :         if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
    5540                 :          0 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
    5541 [ +  - ][ +  + ]:       1154 :         if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
    5542                 :       1030 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
    5543 [ +  - ][ -  + ]:       1154 :         if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
    5544                 :          0 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
    5545 [ +  - ][ +  + ]:       1154 :         if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
    5546                 :         45 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
    5547 [ +  - ][ +  + ]:       1154 :         if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
    5548                 :          5 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
    5549 [ +  - ][ +  + ]:       1154 :         if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
    5550                 :         10 :                 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
    5551                 :            : 
    5552                 :       1154 :         return num_suites;
    5553                 :            : }
    5554                 :            : 
    5555                 :            : 
    5556                 :      10035 : static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
    5557                 :            :                                       enum wpa_alg alg, const u8 *addr,
    5558                 :            :                                       int key_idx, int set_tx,
    5559                 :            :                                       const u8 *seq, size_t seq_len,
    5560                 :            :                                       const u8 *key, size_t key_len)
    5561                 :            : {
    5562                 :      10035 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    5563                 :            :         int ifindex;
    5564                 :            :         struct nl_msg *msg;
    5565                 :            :         int ret;
    5566                 :      10035 :         int tdls = 0;
    5567                 :            : 
    5568                 :            :         /* Ignore for P2P Device */
    5569         [ -  + ]:      10035 :         if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
    5570                 :          0 :                 return 0;
    5571                 :            : 
    5572                 :      10035 :         ifindex = if_nametoindex(ifname);
    5573                 :      10035 :         wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
    5574                 :            :                    "set_tx=%d seq_len=%lu key_len=%lu",
    5575                 :            :                    __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
    5576                 :            :                    (unsigned long) seq_len, (unsigned long) key_len);
    5577                 :            : #ifdef CONFIG_TDLS
    5578         [ +  + ]:       3934 :         if (key_idx == -1) {
    5579                 :         68 :                 key_idx = 0;
    5580                 :         68 :                 tdls = 1;
    5581                 :            :         }
    5582                 :            : #endif /* CONFIG_TDLS */
    5583                 :            : 
    5584                 :      10035 :         msg = nlmsg_alloc();
    5585         [ -  + ]:      10035 :         if (!msg)
    5586                 :          0 :                 return -ENOMEM;
    5587                 :            : 
    5588         [ +  + ]:      10035 :         if (alg == WPA_ALG_NONE) {
    5589                 :       7805 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
    5590                 :            :         } else {
    5591                 :       2230 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
    5592         [ +  - ]:       2230 :                 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
    5593         [ +  - ]:       2230 :                 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
    5594                 :            :                             wpa_alg_to_cipher_suite(alg, key_len));
    5595                 :            :         }
    5596                 :            : 
    5597 [ +  + ][ +  - ]:      10035 :         if (seq && seq_len)
    5598         [ +  - ]:        960 :                 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
    5599                 :            : 
    5600 [ +  + ][ +  + ]:      10035 :         if (addr && !is_broadcast_ether_addr(addr)) {
    5601                 :       3691 :                 wpa_printf(MSG_DEBUG, "   addr=" MACSTR, MAC2STR(addr));
    5602         [ +  - ]:       3691 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    5603                 :            : 
    5604 [ +  + ][ +  + ]:       3693 :                 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
                 [ +  - ]
    5605                 :          2 :                         wpa_printf(MSG_DEBUG, "   RSN IBSS RX GTK");
    5606         [ +  - ]:          2 :                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
    5607                 :            :                                     NL80211_KEYTYPE_GROUP);
    5608                 :            :                 }
    5609 [ +  + ][ +  - ]:       6344 :         } else if (addr && is_broadcast_ether_addr(addr)) {
    5610                 :            :                 struct nlattr *types;
    5611                 :            : 
    5612                 :       1251 :                 wpa_printf(MSG_DEBUG, "   broadcast key");
    5613                 :            : 
    5614                 :       1251 :                 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
    5615         [ -  + ]:       1251 :                 if (!types)
    5616                 :          0 :                         goto nla_put_failure;
    5617         [ +  - ]:       1251 :                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
    5618                 :       1251 :                 nla_nest_end(msg, types);
    5619                 :            :         }
    5620         [ +  - ]:      10035 :         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
    5621         [ +  - ]:      10035 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
    5622                 :            : 
    5623                 :      10035 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    5624 [ +  + ][ +  + ]:      10035 :         if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
                 [ +  + ]
    5625                 :       6947 :                 ret = 0;
    5626         [ +  + ]:      10035 :         if (ret)
    5627                 :         87 :                 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
    5628                 :            :                            ret, strerror(-ret));
    5629                 :            : 
    5630                 :            :         /*
    5631                 :            :          * If we failed or don't need to set the default TX key (below),
    5632                 :            :          * we're done here.
    5633                 :            :          */
    5634 [ +  + ][ +  + ]:      10035 :         if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
         [ +  + ][ +  + ]
    5635                 :       8359 :                 return ret;
    5636         [ +  + ]:       2912 :         if (is_ap_interface(drv->nlmode) && addr &&
           [ +  +  +  + ]
    5637                 :       1236 :             !is_broadcast_ether_addr(addr))
    5638                 :        428 :                 return ret;
    5639                 :            : 
    5640                 :       1248 :         msg = nlmsg_alloc();
    5641         [ -  + ]:       1248 :         if (!msg)
    5642                 :          0 :                 return -ENOMEM;
    5643                 :            : 
    5644                 :       1248 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
    5645         [ +  - ]:       1248 :         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
    5646         [ +  - ]:       1248 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
    5647         [ +  + ]:       1248 :         if (alg == WPA_ALG_IGTK)
    5648         [ +  - ]:        173 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
    5649                 :            :         else
    5650         [ +  - ]:       1075 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
    5651 [ +  + ][ +  + ]:       2060 :         if (addr && is_broadcast_ether_addr(addr)) {
    5652                 :            :                 struct nlattr *types;
    5653                 :            : 
    5654                 :        812 :                 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
    5655         [ -  + ]:        812 :                 if (!types)
    5656                 :          0 :                         goto nla_put_failure;
    5657         [ +  - ]:        812 :                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
    5658                 :        812 :                 nla_nest_end(msg, types);
    5659         [ +  + ]:        436 :         } else if (addr) {
    5660                 :            :                 struct nlattr *types;
    5661                 :            : 
    5662                 :        422 :                 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
    5663         [ -  + ]:        422 :                 if (!types)
    5664                 :          0 :                         goto nla_put_failure;
    5665         [ +  - ]:        422 :                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
    5666                 :        422 :                 nla_nest_end(msg, types);
    5667                 :            :         }
    5668                 :            : 
    5669                 :       1248 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    5670         [ -  + ]:       1248 :         if (ret == -ENOENT)
    5671                 :          0 :                 ret = 0;
    5672         [ -  + ]:       1248 :         if (ret)
    5673                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
    5674                 :            :                            "err=%d %s)", ret, strerror(-ret));
    5675                 :       1248 :         return ret;
    5676                 :            : 
    5677                 :            : nla_put_failure:
    5678                 :          0 :         nlmsg_free(msg);
    5679                 :      10035 :         return -ENOBUFS;
    5680                 :            : }
    5681                 :            : 
    5682                 :            : 
    5683                 :         11 : static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
    5684                 :            :                       int key_idx, int defkey,
    5685                 :            :                       const u8 *seq, size_t seq_len,
    5686                 :            :                       const u8 *key, size_t key_len)
    5687                 :            : {
    5688                 :         11 :         struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
    5689         [ -  + ]:         11 :         if (!key_attr)
    5690                 :          0 :                 return -1;
    5691                 :            : 
    5692 [ +  - ][ -  + ]:         11 :         if (defkey && alg == WPA_ALG_IGTK)
    5693         [ #  # ]:          0 :                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
    5694         [ +  - ]:         11 :         else if (defkey)
    5695         [ +  - ]:         11 :                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
    5696                 :            : 
    5697         [ +  - ]:         11 :         NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
    5698                 :            : 
    5699         [ +  - ]:         11 :         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
    5700                 :            :                     wpa_alg_to_cipher_suite(alg, key_len));
    5701                 :            : 
    5702 [ -  + ][ #  # ]:         11 :         if (seq && seq_len)
    5703         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
    5704                 :            : 
    5705         [ +  - ]:         11 :         NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
    5706                 :            : 
    5707                 :         11 :         nla_nest_end(msg, key_attr);
    5708                 :            : 
    5709                 :         11 :         return 0;
    5710                 :            :  nla_put_failure:
    5711                 :         11 :         return -1;
    5712                 :            : }
    5713                 :            : 
    5714                 :            : 
    5715                 :          9 : static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
    5716                 :            :                                  struct nl_msg *msg)
    5717                 :            : {
    5718                 :          9 :         int i, privacy = 0;
    5719                 :            :         struct nlattr *nl_keys, *nl_key;
    5720                 :            : 
    5721         [ +  + ]:         45 :         for (i = 0; i < 4; i++) {
    5722         [ +  - ]:         36 :                 if (!params->wep_key[i])
    5723                 :         36 :                         continue;
    5724                 :          0 :                 privacy = 1;
    5725                 :          0 :                 break;
    5726                 :            :         }
    5727         [ -  + ]:          9 :         if (params->wps == WPS_MODE_PRIVACY)
    5728                 :          0 :                 privacy = 1;
    5729 [ +  - ][ +  + ]:          9 :         if (params->pairwise_suite &&
    5730                 :          9 :             params->pairwise_suite != WPA_CIPHER_NONE)
    5731                 :          6 :                 privacy = 1;
    5732                 :            : 
    5733         [ +  + ]:          9 :         if (!privacy)
    5734                 :          3 :                 return 0;
    5735                 :            : 
    5736         [ +  - ]:          6 :         NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
    5737                 :            : 
    5738                 :          6 :         nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
    5739         [ -  + ]:          6 :         if (!nl_keys)
    5740                 :          0 :                 goto nla_put_failure;
    5741                 :            : 
    5742         [ +  + ]:         30 :         for (i = 0; i < 4; i++) {
    5743         [ +  - ]:         24 :                 if (!params->wep_key[i])
    5744                 :         24 :                         continue;
    5745                 :            : 
    5746                 :          0 :                 nl_key = nla_nest_start(msg, i);
    5747         [ #  # ]:          0 :                 if (!nl_key)
    5748                 :          0 :                         goto nla_put_failure;
    5749                 :            : 
    5750         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
    5751                 :            :                         params->wep_key[i]);
    5752         [ #  # ]:          0 :                 if (params->wep_key_len[i] == 5)
    5753         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
    5754                 :            :                                     WLAN_CIPHER_SUITE_WEP40);
    5755                 :            :                 else
    5756         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
    5757                 :            :                                     WLAN_CIPHER_SUITE_WEP104);
    5758                 :            : 
    5759         [ #  # ]:          0 :                 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
    5760                 :            : 
    5761         [ #  # ]:          0 :                 if (i == params->wep_tx_keyidx)
    5762         [ #  # ]:          0 :                         NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
    5763                 :            : 
    5764                 :          0 :                 nla_nest_end(msg, nl_key);
    5765                 :            :         }
    5766                 :          6 :         nla_nest_end(msg, nl_keys);
    5767                 :            : 
    5768                 :          6 :         return 0;
    5769                 :            : 
    5770                 :            : nla_put_failure:
    5771                 :          9 :         return -ENOBUFS;
    5772                 :            : }
    5773                 :            : 
    5774                 :            : 
    5775                 :        628 : static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
    5776                 :            :                                    const u8 *addr, int cmd, u16 reason_code,
    5777                 :            :                                    int local_state_change)
    5778                 :            : {
    5779                 :        628 :         int ret = -1;
    5780                 :            :         struct nl_msg *msg;
    5781                 :            : 
    5782                 :        628 :         msg = nlmsg_alloc();
    5783         [ -  + ]:        628 :         if (!msg)
    5784                 :          0 :                 return -1;
    5785                 :            : 
    5786                 :        628 :         nl80211_cmd(drv, msg, 0, cmd);
    5787                 :            : 
    5788         [ +  - ]:        628 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    5789         [ +  - ]:        628 :         NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
    5790         [ +  + ]:        628 :         if (addr)
    5791         [ +  - ]:        623 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    5792         [ -  + ]:        628 :         if (local_state_change)
    5793         [ #  # ]:          0 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
    5794                 :            : 
    5795                 :        628 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    5796                 :        628 :         msg = NULL;
    5797         [ +  + ]:        628 :         if (ret) {
    5798                 :         19 :                 wpa_dbg(drv->ctx, MSG_DEBUG,
    5799                 :            :                         "nl80211: MLME command failed: reason=%u ret=%d (%s)",
    5800                 :            :                         reason_code, ret, strerror(-ret));
    5801                 :         19 :                 goto nla_put_failure;
    5802                 :            :         }
    5803                 :        609 :         ret = 0;
    5804                 :            : 
    5805                 :            : nla_put_failure:
    5806                 :        628 :         nlmsg_free(msg);
    5807                 :        628 :         return ret;
    5808                 :            : }
    5809                 :            : 
    5810                 :            : 
    5811                 :          5 : static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
    5812                 :            :                                          int reason_code)
    5813                 :            : {
    5814                 :            :         int ret;
    5815                 :            : 
    5816                 :          5 :         wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
    5817                 :          5 :         nl80211_mark_disconnected(drv);
    5818                 :            :         /* Disconnect command doesn't need BSSID - it uses cached value */
    5819                 :          5 :         ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
    5820                 :            :                                       reason_code, 0);
    5821                 :            :         /*
    5822                 :            :          * For locally generated disconnect, supplicant already generates a
    5823                 :            :          * DEAUTH event, so ignore the event from NL80211.
    5824                 :            :          */
    5825                 :          5 :         drv->ignore_next_local_disconnect = ret == 0;
    5826                 :            : 
    5827                 :          5 :         return ret;
    5828                 :            : }
    5829                 :            : 
    5830                 :            : 
    5831                 :        633 : static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
    5832                 :            :                                              const u8 *addr, int reason_code)
    5833                 :            : {
    5834                 :        633 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    5835         [ +  + ]:        633 :         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
    5836                 :          5 :                 return wpa_driver_nl80211_disconnect(drv, reason_code);
    5837                 :        628 :         wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
    5838                 :       3768 :                    __func__, MAC2STR(addr), reason_code);
    5839                 :        628 :         nl80211_mark_disconnected(drv);
    5840         [ +  + ]:        628 :         if (drv->nlmode == NL80211_IFTYPE_ADHOC)
    5841                 :          5 :                 return nl80211_leave_ibss(drv);
    5842                 :        633 :         return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
    5843                 :            :                                        reason_code, 0);
    5844                 :            : }
    5845                 :            : 
    5846                 :            : 
    5847                 :          0 : static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
    5848                 :            :                                      struct wpa_driver_auth_params *params)
    5849                 :            : {
    5850                 :            :         int i;
    5851                 :            : 
    5852                 :          0 :         drv->auth_freq = params->freq;
    5853                 :          0 :         drv->auth_alg = params->auth_alg;
    5854                 :          0 :         drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
    5855                 :          0 :         drv->auth_local_state_change = params->local_state_change;
    5856                 :          0 :         drv->auth_p2p = params->p2p;
    5857                 :            : 
    5858         [ #  # ]:          0 :         if (params->bssid)
    5859                 :          0 :                 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
    5860                 :            :         else
    5861                 :          0 :                 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
    5862                 :            : 
    5863         [ #  # ]:          0 :         if (params->ssid) {
    5864                 :          0 :                 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
    5865                 :          0 :                 drv->auth_ssid_len = params->ssid_len;
    5866                 :            :         } else
    5867                 :          0 :                 drv->auth_ssid_len = 0;
    5868                 :            : 
    5869                 :            : 
    5870                 :          0 :         os_free(drv->auth_ie);
    5871                 :          0 :         drv->auth_ie = NULL;
    5872                 :          0 :         drv->auth_ie_len = 0;
    5873         [ #  # ]:          0 :         if (params->ie) {
    5874                 :          0 :                 drv->auth_ie = os_malloc(params->ie_len);
    5875         [ #  # ]:          0 :                 if (drv->auth_ie) {
    5876                 :          0 :                         os_memcpy(drv->auth_ie, params->ie, params->ie_len);
    5877                 :          0 :                         drv->auth_ie_len = params->ie_len;
    5878                 :            :                 }
    5879                 :            :         }
    5880                 :            : 
    5881         [ #  # ]:          0 :         for (i = 0; i < 4; i++) {
    5882 [ #  # ][ #  # ]:          0 :                 if (params->wep_key[i] && params->wep_key_len[i] &&
                 [ #  # ]
    5883                 :          0 :                     params->wep_key_len[i] <= 16) {
    5884                 :          0 :                         os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
    5885                 :            :                                   params->wep_key_len[i]);
    5886                 :          0 :                         drv->auth_wep_key_len[i] = params->wep_key_len[i];
    5887                 :            :                 } else
    5888                 :          0 :                         drv->auth_wep_key_len[i] = 0;
    5889                 :            :         }
    5890                 :          0 : }
    5891                 :            : 
    5892                 :            : 
    5893                 :        708 : static int wpa_driver_nl80211_authenticate(
    5894                 :            :         struct i802_bss *bss, struct wpa_driver_auth_params *params)
    5895                 :            : {
    5896                 :        708 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    5897                 :        708 :         int ret = -1, i;
    5898                 :            :         struct nl_msg *msg;
    5899                 :            :         enum nl80211_auth_type type;
    5900                 :            :         enum nl80211_iftype nlmode;
    5901                 :        708 :         int count = 0;
    5902                 :            :         int is_retry;
    5903                 :            : 
    5904                 :        708 :         is_retry = drv->retry_auth;
    5905                 :        708 :         drv->retry_auth = 0;
    5906                 :            : 
    5907                 :        708 :         nl80211_mark_disconnected(drv);
    5908                 :        708 :         os_memset(drv->auth_bssid, 0, ETH_ALEN);
    5909         [ +  - ]:        708 :         if (params->bssid)
    5910                 :        708 :                 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
    5911                 :            :         else
    5912                 :          0 :                 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
    5913                 :            :         /* FIX: IBSS mode */
    5914         [ +  + ]:        708 :         nlmode = params->p2p ?
    5915                 :            :                 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
    5916   [ +  +  -  + ]:        786 :         if (drv->nlmode != nlmode &&
    5917                 :         78 :             wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
    5918                 :          0 :                 return -1;
    5919                 :            : 
    5920                 :            : retry:
    5921                 :        708 :         msg = nlmsg_alloc();
    5922         [ -  + ]:        708 :         if (!msg)
    5923                 :          0 :                 return -1;
    5924                 :            : 
    5925                 :        708 :         wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
    5926                 :            :                    drv->ifindex);
    5927                 :            : 
    5928                 :        708 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
    5929                 :            : 
    5930         [ +  + ]:       3540 :         for (i = 0; i < 4; i++) {
    5931         [ +  + ]:       2832 :                 if (!params->wep_key[i])
    5932                 :       2821 :                         continue;
    5933                 :         11 :                 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
    5934                 :            :                                            NULL, i,
    5935                 :         11 :                                            i == params->wep_tx_keyidx, NULL, 0,
    5936                 :            :                                            params->wep_key[i],
    5937                 :            :                                            params->wep_key_len[i]);
    5938         [ -  + ]:         11 :                 if (params->wep_tx_keyidx != i)
    5939                 :          0 :                         continue;
    5940         [ -  + ]:         11 :                 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
    5941                 :            :                                params->wep_key[i], params->wep_key_len[i])) {
    5942                 :          0 :                         nlmsg_free(msg);
    5943                 :          0 :                         return -1;
    5944                 :            :                 }
    5945                 :            :         }
    5946                 :            : 
    5947         [ +  - ]:        708 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    5948         [ +  - ]:        708 :         if (params->bssid) {
    5949                 :        708 :                 wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
    5950                 :       4248 :                            MAC2STR(params->bssid));
    5951         [ +  - ]:        708 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
    5952                 :            :         }
    5953         [ +  - ]:        708 :         if (params->freq) {
    5954                 :        708 :                 wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
    5955         [ +  - ]:        708 :                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
    5956                 :            :         }
    5957         [ +  - ]:        708 :         if (params->ssid) {
    5958                 :        708 :                 wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
    5959                 :        708 :                                   params->ssid, params->ssid_len);
    5960         [ +  - ]:        708 :                 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
    5961                 :            :                         params->ssid);
    5962                 :            :         }
    5963                 :        708 :         wpa_hexdump(MSG_DEBUG, "  * IEs", params->ie, params->ie_len);
    5964         [ +  + ]:        708 :         if (params->ie)
    5965         [ +  - ]:         10 :                 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
    5966         [ +  + ]:        708 :         if (params->sae_data) {
    5967                 :         36 :                 wpa_hexdump(MSG_DEBUG, "  * SAE data", params->sae_data,
    5968                 :            :                             params->sae_data_len);
    5969         [ +  - ]:         36 :                 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
    5970                 :            :                         params->sae_data);
    5971                 :            :         }
    5972         [ +  + ]:        708 :         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
    5973                 :        640 :                 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
    5974         [ +  + ]:         68 :         else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
    5975                 :          3 :                 type = NL80211_AUTHTYPE_SHARED_KEY;
    5976         [ +  + ]:         65 :         else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
    5977                 :          3 :                 type = NL80211_AUTHTYPE_NETWORK_EAP;
    5978         [ +  + ]:         62 :         else if (params->auth_alg & WPA_AUTH_ALG_FT)
    5979                 :         26 :                 type = NL80211_AUTHTYPE_FT;
    5980         [ +  - ]:         36 :         else if (params->auth_alg & WPA_AUTH_ALG_SAE)
    5981                 :         36 :                 type = NL80211_AUTHTYPE_SAE;
    5982                 :            :         else
    5983                 :          0 :                 goto nla_put_failure;
    5984                 :        708 :         wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
    5985         [ +  - ]:        708 :         NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
    5986         [ +  + ]:        708 :         if (params->local_state_change) {
    5987                 :         16 :                 wpa_printf(MSG_DEBUG, "  * Local state change only");
    5988         [ +  - ]:         16 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
    5989                 :            :         }
    5990                 :            : 
    5991                 :        708 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    5992                 :        708 :         msg = NULL;
    5993         [ -  + ]:        708 :         if (ret) {
    5994                 :          0 :                 wpa_dbg(drv->ctx, MSG_DEBUG,
    5995                 :            :                         "nl80211: MLME command failed (auth): ret=%d (%s)",
    5996                 :            :                         ret, strerror(-ret));
    5997                 :          0 :                 count++;
    5998 [ #  # ][ #  # ]:          0 :                 if (ret == -EALREADY && count == 1 && params->bssid &&
         [ #  # ][ #  # ]
    5999                 :          0 :                     !params->local_state_change) {
    6000                 :            :                         /*
    6001                 :            :                          * mac80211 does not currently accept new
    6002                 :            :                          * authentication if we are already authenticated. As a
    6003                 :            :                          * workaround, force deauthentication and try again.
    6004                 :            :                          */
    6005                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
    6006                 :            :                                    "after forced deauthentication");
    6007                 :          0 :                         wpa_driver_nl80211_deauthenticate(
    6008                 :            :                                 bss, params->bssid,
    6009                 :            :                                 WLAN_REASON_PREV_AUTH_NOT_VALID);
    6010                 :          0 :                         nlmsg_free(msg);
    6011                 :          0 :                         goto retry;
    6012                 :            :                 }
    6013                 :            : 
    6014 [ #  # ][ #  # ]:          0 :                 if (ret == -ENOENT && params->freq && !is_retry) {
                 [ #  # ]
    6015                 :            :                         /*
    6016                 :            :                          * cfg80211 has likely expired the BSS entry even
    6017                 :            :                          * though it was previously available in our internal
    6018                 :            :                          * BSS table. To recover quickly, start a single
    6019                 :            :                          * channel scan on the specified channel.
    6020                 :            :                          */
    6021                 :            :                         struct wpa_driver_scan_params scan;
    6022                 :            :                         int freqs[2];
    6023                 :            : 
    6024                 :          0 :                         os_memset(&scan, 0, sizeof(scan));
    6025                 :          0 :                         scan.num_ssids = 1;
    6026         [ #  # ]:          0 :                         if (params->ssid) {
    6027                 :          0 :                                 scan.ssids[0].ssid = params->ssid;
    6028                 :          0 :                                 scan.ssids[0].ssid_len = params->ssid_len;
    6029                 :            :                         }
    6030                 :          0 :                         freqs[0] = params->freq;
    6031                 :          0 :                         freqs[1] = 0;
    6032                 :          0 :                         scan.freqs = freqs;
    6033                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
    6034                 :            :                                    "channel scan to refresh cfg80211 BSS "
    6035                 :            :                                    "entry");
    6036                 :          0 :                         ret = wpa_driver_nl80211_scan(bss, &scan);
    6037         [ #  # ]:          0 :                         if (ret == 0) {
    6038                 :          0 :                                 nl80211_copy_auth_params(drv, params);
    6039                 :          0 :                                 drv->scan_for_auth = 1;
    6040                 :            :                         }
    6041         [ #  # ]:          0 :                 } else if (is_retry) {
    6042                 :            :                         /*
    6043                 :            :                          * Need to indicate this with an event since the return
    6044                 :            :                          * value from the retry is not delivered to core code.
    6045                 :            :                          */
    6046                 :            :                         union wpa_event_data event;
    6047                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
    6048                 :            :                                    "failed");
    6049                 :          0 :                         os_memset(&event, 0, sizeof(event));
    6050                 :          0 :                         os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
    6051                 :            :                                   ETH_ALEN);
    6052                 :          0 :                         wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
    6053                 :            :                                              &event);
    6054                 :            :                 }
    6055                 :            : 
    6056                 :          0 :                 goto nla_put_failure;
    6057                 :            :         }
    6058                 :        708 :         ret = 0;
    6059                 :        708 :         wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
    6060                 :            :                    "successfully");
    6061                 :            : 
    6062                 :            : nla_put_failure:
    6063                 :        708 :         nlmsg_free(msg);
    6064                 :        708 :         return ret;
    6065                 :            : }
    6066                 :            : 
    6067                 :            : 
    6068                 :          0 : static int wpa_driver_nl80211_authenticate_retry(
    6069                 :            :         struct wpa_driver_nl80211_data *drv)
    6070                 :            : {
    6071                 :            :         struct wpa_driver_auth_params params;
    6072                 :          0 :         struct i802_bss *bss = drv->first_bss;
    6073                 :            :         int i;
    6074                 :            : 
    6075                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
    6076                 :            : 
    6077                 :          0 :         os_memset(&params, 0, sizeof(params));
    6078                 :          0 :         params.freq = drv->auth_freq;
    6079                 :          0 :         params.auth_alg = drv->auth_alg;
    6080                 :          0 :         params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
    6081                 :          0 :         params.local_state_change = drv->auth_local_state_change;
    6082                 :          0 :         params.p2p = drv->auth_p2p;
    6083                 :            : 
    6084         [ #  # ]:          0 :         if (!is_zero_ether_addr(drv->auth_bssid_))
    6085                 :          0 :                 params.bssid = drv->auth_bssid_;
    6086                 :            : 
    6087         [ #  # ]:          0 :         if (drv->auth_ssid_len) {
    6088                 :          0 :                 params.ssid = drv->auth_ssid;
    6089                 :          0 :                 params.ssid_len = drv->auth_ssid_len;
    6090                 :            :         }
    6091                 :            : 
    6092                 :          0 :         params.ie = drv->auth_ie;
    6093                 :          0 :         params.ie_len = drv->auth_ie_len;
    6094                 :            : 
    6095         [ #  # ]:          0 :         for (i = 0; i < 4; i++) {
    6096         [ #  # ]:          0 :                 if (drv->auth_wep_key_len[i]) {
    6097                 :          0 :                         params.wep_key[i] = drv->auth_wep_key[i];
    6098                 :          0 :                         params.wep_key_len[i] = drv->auth_wep_key_len[i];
    6099                 :            :                 }
    6100                 :            :         }
    6101                 :            : 
    6102                 :          0 :         drv->retry_auth = 1;
    6103                 :          0 :         return wpa_driver_nl80211_authenticate(bss, &params);
    6104                 :            : }
    6105                 :            : 
    6106                 :            : 
    6107                 :            : struct phy_info_arg {
    6108                 :            :         u16 *num_modes;
    6109                 :            :         struct hostapd_hw_modes *modes;
    6110                 :            :         int last_mode, last_chan_idx;
    6111                 :            : };
    6112                 :            : 
    6113                 :      89754 : static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
    6114                 :            :                              struct nlattr *ampdu_factor,
    6115                 :            :                              struct nlattr *ampdu_density,
    6116                 :            :                              struct nlattr *mcs_set)
    6117                 :            : {
    6118         [ +  + ]:      89754 :         if (capa)
    6119                 :       4274 :                 mode->ht_capab = nla_get_u16(capa);
    6120                 :            : 
    6121         [ +  + ]:      89754 :         if (ampdu_factor)
    6122                 :       4274 :                 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
    6123                 :            : 
    6124         [ +  + ]:      89754 :         if (ampdu_density)
    6125                 :       4274 :                 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
    6126                 :            : 
    6127 [ +  + ][ +  - ]:      89754 :         if (mcs_set && nla_len(mcs_set) >= 16) {
    6128                 :            :                 u8 *mcs;
    6129                 :       4274 :                 mcs = nla_data(mcs_set);
    6130                 :       4274 :                 os_memcpy(mode->mcs_set, mcs, 16);
    6131                 :            :         }
    6132                 :      89754 : }
    6133                 :            : 
    6134                 :            : 
    6135                 :      89754 : static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
    6136                 :            :                               struct nlattr *capa,
    6137                 :            :                               struct nlattr *mcs_set)
    6138                 :            : {
    6139         [ +  + ]:      89754 :         if (capa)
    6140                 :       4274 :                 mode->vht_capab = nla_get_u32(capa);
    6141                 :            : 
    6142 [ +  + ][ +  - ]:      89754 :         if (mcs_set && nla_len(mcs_set) >= 8) {
    6143                 :            :                 u8 *mcs;
    6144                 :       4274 :                 mcs = nla_data(mcs_set);
    6145                 :       4274 :                 os_memcpy(mode->vht_mcs_set, mcs, 8);
    6146                 :            :         }
    6147                 :      89754 : }
    6148                 :            : 
    6149                 :            : 
    6150                 :      81206 : static void phy_info_freq(struct hostapd_hw_modes *mode,
    6151                 :            :                           struct hostapd_channel_data *chan,
    6152                 :            :                           struct nlattr *tb_freq[])
    6153                 :            : {
    6154                 :            :         u8 channel;
    6155                 :      81206 :         chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
    6156                 :      81206 :         chan->flag = 0;
    6157         [ +  - ]:      81206 :         if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
    6158                 :      81206 :                 chan->chan = channel;
    6159                 :            : 
    6160         [ +  + ]:      81206 :         if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
    6161                 :      32055 :                 chan->flag |= HOSTAPD_CHAN_DISABLED;
    6162         [ +  + ]:      81206 :         if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
    6163                 :      57699 :                 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
    6164         [ +  + ]:      81206 :         if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
    6165                 :      32055 :                 chan->flag |= HOSTAPD_CHAN_RADAR;
    6166                 :            : 
    6167         [ +  + ]:      81206 :         if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
    6168                 :      32055 :                 enum nl80211_dfs_state state =
    6169                 :      32055 :                         nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
    6170                 :            : 
    6171   [ +  -  -  - ]:      32055 :                 switch (state) {
    6172                 :            :                 case NL80211_DFS_USABLE:
    6173                 :      32055 :                         chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
    6174                 :      32055 :                         break;
    6175                 :            :                 case NL80211_DFS_AVAILABLE:
    6176                 :          0 :                         chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
    6177                 :          0 :                         break;
    6178                 :            :                 case NL80211_DFS_UNAVAILABLE:
    6179                 :          0 :                         chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
    6180                 :          0 :                         break;
    6181                 :            :                 }
    6182                 :            :         }
    6183                 :      81206 : }
    6184                 :            : 
    6185                 :            : 
    6186                 :      89754 : static int phy_info_freqs(struct phy_info_arg *phy_info,
    6187                 :            :                           struct hostapd_hw_modes *mode, struct nlattr *tb)
    6188                 :            : {
    6189                 :            :         static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
    6190                 :            :                 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
    6191                 :            :                 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
    6192                 :            :                 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
    6193                 :            :                 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
    6194                 :            :                 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
    6195                 :            :                 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
    6196                 :            :         };
    6197                 :      89754 :         int new_channels = 0;
    6198                 :            :         struct hostapd_channel_data *channel;
    6199                 :            :         struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
    6200                 :            :         struct nlattr *nl_freq;
    6201                 :            :         int rem_freq, idx;
    6202                 :            : 
    6203         [ +  + ]:      89754 :         if (tb == NULL)
    6204                 :       4274 :                 return NL_OK;
    6205                 :            : 
    6206         [ +  + ]:     166686 :         nla_for_each_nested(nl_freq, tb, rem_freq) {
    6207                 :     162412 :                 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
    6208                 :      81206 :                           nla_data(nl_freq), nla_len(nl_freq), freq_policy);
    6209         [ -  + ]:      81206 :                 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
    6210                 :          0 :                         continue;
    6211                 :      81206 :                 new_channels++;
    6212                 :            :         }
    6213                 :            : 
    6214                 :      85480 :         channel = os_realloc_array(mode->channels,
    6215                 :      85480 :                                    mode->num_channels + new_channels,
    6216                 :            :                                    sizeof(struct hostapd_channel_data));
    6217         [ -  + ]:      85480 :         if (!channel)
    6218                 :          0 :                 return NL_SKIP;
    6219                 :            : 
    6220                 :      85480 :         mode->channels = channel;
    6221                 :      85480 :         mode->num_channels += new_channels;
    6222                 :            : 
    6223                 :      85480 :         idx = phy_info->last_chan_idx;
    6224                 :            : 
    6225         [ +  + ]:     166686 :         nla_for_each_nested(nl_freq, tb, rem_freq) {
    6226                 :     162412 :                 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
    6227                 :      81206 :                           nla_data(nl_freq), nla_len(nl_freq), freq_policy);
    6228         [ -  + ]:      81206 :                 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
    6229                 :          0 :                         continue;
    6230                 :      81206 :                 phy_info_freq(mode, &mode->channels[idx], tb_freq);
    6231                 :      81206 :                 idx++;
    6232                 :            :         }
    6233                 :      85480 :         phy_info->last_chan_idx = idx;
    6234                 :            : 
    6235                 :      89754 :         return NL_OK;
    6236                 :            : }
    6237                 :            : 
    6238                 :            : 
    6239                 :      89754 : static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
    6240                 :            : {
    6241                 :            :         static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
    6242                 :            :                 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
    6243                 :            :                 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
    6244                 :            :                 { .type = NLA_FLAG },
    6245                 :            :         };
    6246                 :            :         struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
    6247                 :            :         struct nlattr *nl_rate;
    6248                 :            :         int rem_rate, idx;
    6249                 :            : 
    6250         [ +  + ]:      89754 :         if (tb == NULL)
    6251                 :      85480 :                 return NL_OK;
    6252                 :            : 
    6253         [ +  + ]:      47014 :         nla_for_each_nested(nl_rate, tb, rem_rate) {
    6254                 :      85480 :                 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
    6255                 :      42740 :                           nla_data(nl_rate), nla_len(nl_rate),
    6256                 :            :                           rate_policy);
    6257         [ -  + ]:      42740 :                 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
    6258                 :          0 :                         continue;
    6259                 :      42740 :                 mode->num_rates++;
    6260                 :            :         }
    6261                 :            : 
    6262                 :       4274 :         mode->rates = os_calloc(mode->num_rates, sizeof(int));
    6263         [ -  + ]:       4274 :         if (!mode->rates)
    6264                 :          0 :                 return NL_SKIP;
    6265                 :            : 
    6266                 :       4274 :         idx = 0;
    6267                 :            : 
    6268         [ +  + ]:      47014 :         nla_for_each_nested(nl_rate, tb, rem_rate) {
    6269                 :      85480 :                 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
    6270                 :      42740 :                           nla_data(nl_rate), nla_len(nl_rate),
    6271                 :            :                           rate_policy);
    6272         [ -  + ]:      42740 :                 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
    6273                 :          0 :                         continue;
    6274                 :      42740 :                 mode->rates[idx] = nla_get_u32(
    6275                 :            :                         tb_rate[NL80211_BITRATE_ATTR_RATE]);
    6276                 :      42740 :                 idx++;
    6277                 :            :         }
    6278                 :            : 
    6279                 :      89754 :         return NL_OK;
    6280                 :            : }
    6281                 :            : 
    6282                 :            : 
    6283                 :      89754 : static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
    6284                 :            : {
    6285                 :            :         struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
    6286                 :            :         struct hostapd_hw_modes *mode;
    6287                 :            :         int ret;
    6288                 :            : 
    6289         [ +  + ]:      89754 :         if (phy_info->last_mode != nl_band->nla_type) {
    6290                 :       4274 :                 mode = os_realloc_array(phy_info->modes,
    6291                 :       4274 :                                         *phy_info->num_modes + 1,
    6292                 :            :                                         sizeof(*mode));
    6293         [ -  + ]:       4274 :                 if (!mode)
    6294                 :          0 :                         return NL_SKIP;
    6295                 :       4274 :                 phy_info->modes = mode;
    6296                 :            : 
    6297                 :       4274 :                 mode = &phy_info->modes[*(phy_info->num_modes)];
    6298                 :       4274 :                 os_memset(mode, 0, sizeof(*mode));
    6299                 :       4274 :                 mode->mode = NUM_HOSTAPD_MODES;
    6300                 :       4274 :                 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
    6301                 :            :                         HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
    6302                 :            : 
    6303                 :            :                 /*
    6304                 :            :                  * Unsupported VHT MCS stream is defined as value 3, so the VHT
    6305                 :            :                  * MCS RX/TX map must be initialized with 0xffff to mark all 8
    6306                 :            :                  * possible streams as unsupported. This will be overridden if
    6307                 :            :                  * driver advertises VHT support.
    6308                 :            :                  */
    6309                 :       4274 :                 mode->vht_mcs_set[0] = 0xff;
    6310                 :       4274 :                 mode->vht_mcs_set[1] = 0xff;
    6311                 :       4274 :                 mode->vht_mcs_set[4] = 0xff;
    6312                 :       4274 :                 mode->vht_mcs_set[5] = 0xff;
    6313                 :            : 
    6314                 :       4274 :                 *(phy_info->num_modes) += 1;
    6315                 :       4274 :                 phy_info->last_mode = nl_band->nla_type;
    6316                 :       4274 :                 phy_info->last_chan_idx = 0;
    6317                 :            :         } else
    6318                 :      85480 :                 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
    6319                 :            : 
    6320                 :      89754 :         nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
    6321                 :            :                   nla_len(nl_band), NULL);
    6322                 :            : 
    6323                 :      89754 :         phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
    6324                 :            :                          tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
    6325                 :            :                          tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
    6326                 :            :                          tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
    6327                 :      89754 :         phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
    6328                 :            :                           tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
    6329                 :      89754 :         ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
    6330         [ -  + ]:      89754 :         if (ret != NL_OK)
    6331                 :          0 :                 return ret;
    6332                 :      89754 :         ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
    6333         [ -  + ]:      89754 :         if (ret != NL_OK)
    6334                 :          0 :                 return ret;
    6335                 :            : 
    6336                 :      89754 :         return NL_OK;
    6337                 :            : }
    6338                 :            : 
    6339                 :            : 
    6340                 :     113261 : static int phy_info_handler(struct nl_msg *msg, void *arg)
    6341                 :            : {
    6342                 :            :         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
    6343                 :     113261 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    6344                 :     113261 :         struct phy_info_arg *phy_info = arg;
    6345                 :            :         struct nlattr *nl_band;
    6346                 :            :         int rem_band;
    6347                 :            : 
    6348                 :     113261 :         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    6349                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    6350                 :            : 
    6351         [ +  + ]:     113261 :         if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
    6352                 :      21370 :                 return NL_SKIP;
    6353                 :            : 
    6354         [ +  + ]:     181645 :         nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
    6355                 :            :         {
    6356                 :      89754 :                 int res = phy_info_band(phy_info, nl_band);
    6357         [ -  + ]:      89754 :                 if (res != NL_OK)
    6358                 :          0 :                         return res;
    6359                 :            :         }
    6360                 :            : 
    6361                 :     113261 :         return NL_SKIP;
    6362                 :            : }
    6363                 :            : 
    6364                 :            : 
    6365                 :            : static struct hostapd_hw_modes *
    6366                 :       2137 : wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
    6367                 :            :                                      u16 *num_modes)
    6368                 :            : {
    6369                 :            :         u16 m;
    6370                 :       2137 :         struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
    6371                 :       2137 :         int i, mode11g_idx = -1;
    6372                 :            : 
    6373                 :            :         /* heuristic to set up modes */
    6374         [ +  + ]:       6411 :         for (m = 0; m < *num_modes; m++) {
    6375         [ -  + ]:       4274 :                 if (!modes[m].num_channels)
    6376                 :          0 :                         continue;
    6377         [ +  + ]:       4274 :                 if (modes[m].channels[0].freq < 4000) {
    6378                 :       2137 :                         modes[m].mode = HOSTAPD_MODE_IEEE80211B;
    6379         [ +  - ]:      21370 :                         for (i = 0; i < modes[m].num_rates; i++) {
    6380         [ +  + ]:      19233 :                                 if (modes[m].rates[i] > 200) {
    6381                 :       2137 :                                         modes[m].mode = HOSTAPD_MODE_IEEE80211G;
    6382                 :       2137 :                                         break;
    6383                 :            :                                 }
    6384                 :            :                         }
    6385         [ -  + ]:       2137 :                 } else if (modes[m].channels[0].freq > 50000)
    6386                 :          0 :                         modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
    6387                 :            :                 else
    6388                 :       2137 :                         modes[m].mode = HOSTAPD_MODE_IEEE80211A;
    6389                 :            :         }
    6390                 :            : 
    6391                 :            :         /* If only 802.11g mode is included, use it to construct matching
    6392                 :            :          * 802.11b mode data. */
    6393                 :            : 
    6394         [ +  + ]:       6411 :         for (m = 0; m < *num_modes; m++) {
    6395         [ -  + ]:       4274 :                 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
    6396                 :          0 :                         return modes; /* 802.11b already included */
    6397         [ +  + ]:       4274 :                 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
    6398                 :       2137 :                         mode11g_idx = m;
    6399                 :            :         }
    6400                 :            : 
    6401         [ -  + ]:       2137 :         if (mode11g_idx < 0)
    6402                 :          0 :                 return modes; /* 2.4 GHz band not supported at all */
    6403                 :            : 
    6404                 :       2137 :         nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
    6405         [ -  + ]:       2137 :         if (nmodes == NULL)
    6406                 :          0 :                 return modes; /* Could not add 802.11b mode */
    6407                 :            : 
    6408                 :       2137 :         mode = &nmodes[*num_modes];
    6409                 :       2137 :         os_memset(mode, 0, sizeof(*mode));
    6410                 :       2137 :         (*num_modes)++;
    6411                 :       2137 :         modes = nmodes;
    6412                 :            : 
    6413                 :       2137 :         mode->mode = HOSTAPD_MODE_IEEE80211B;
    6414                 :            : 
    6415                 :       2137 :         mode11g = &modes[mode11g_idx];
    6416                 :       2137 :         mode->num_channels = mode11g->num_channels;
    6417                 :       2137 :         mode->channels = os_malloc(mode11g->num_channels *
    6418                 :            :                                    sizeof(struct hostapd_channel_data));
    6419         [ -  + ]:       2137 :         if (mode->channels == NULL) {
    6420                 :          0 :                 (*num_modes)--;
    6421                 :          0 :                 return modes; /* Could not add 802.11b mode */
    6422                 :            :         }
    6423                 :       2137 :         os_memcpy(mode->channels, mode11g->channels,
    6424                 :            :                   mode11g->num_channels * sizeof(struct hostapd_channel_data));
    6425                 :            : 
    6426                 :       2137 :         mode->num_rates = 0;
    6427                 :       2137 :         mode->rates = os_malloc(4 * sizeof(int));
    6428         [ -  + ]:       2137 :         if (mode->rates == NULL) {
    6429                 :          0 :                 os_free(mode->channels);
    6430                 :          0 :                 (*num_modes)--;
    6431                 :          0 :                 return modes; /* Could not add 802.11b mode */
    6432                 :            :         }
    6433                 :            : 
    6434         [ +  - ]:       8548 :         for (i = 0; i < mode11g->num_rates; i++) {
    6435 [ +  + ][ +  + ]:       8548 :                 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
                 [ +  + ]
    6436         [ -  + ]:       2137 :                     mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
    6437                 :          0 :                         continue;
    6438                 :       8548 :                 mode->rates[mode->num_rates] = mode11g->rates[i];
    6439                 :       8548 :                 mode->num_rates++;
    6440         [ +  + ]:       8548 :                 if (mode->num_rates == 4)
    6441                 :       2137 :                         break;
    6442                 :            :         }
    6443                 :            : 
    6444         [ -  + ]:       2137 :         if (mode->num_rates == 0) {
    6445                 :          0 :                 os_free(mode->channels);
    6446                 :          0 :                 os_free(mode->rates);
    6447                 :          0 :                 (*num_modes)--;
    6448                 :          0 :                 return modes; /* No 802.11b rates */
    6449                 :            :         }
    6450                 :            : 
    6451                 :       2137 :         wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
    6452                 :            :                    "information");
    6453                 :            : 
    6454                 :       2137 :         return modes;
    6455                 :            : }
    6456                 :            : 
    6457                 :            : 
    6458                 :      12822 : static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
    6459                 :            :                                   int end)
    6460                 :            : {
    6461                 :            :         int c;
    6462                 :            : 
    6463         [ +  + ]:     256440 :         for (c = 0; c < mode->num_channels; c++) {
    6464                 :     243618 :                 struct hostapd_channel_data *chan = &mode->channels[c];
    6465 [ +  + ][ +  + ]:     243618 :                 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
    6466                 :      42740 :                         chan->flag |= HOSTAPD_CHAN_HT40;
    6467                 :            :         }
    6468                 :      12822 : }
    6469                 :            : 
    6470                 :            : 
    6471                 :      21370 : static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
    6472                 :            :                                       int end)
    6473                 :            : {
    6474                 :            :         int c;
    6475                 :            : 
    6476         [ +  + ]:     427400 :         for (c = 0; c < mode->num_channels; c++) {
    6477                 :     406030 :                 struct hostapd_channel_data *chan = &mode->channels[c];
    6478         [ +  + ]:     406030 :                 if (!(chan->flag & HOSTAPD_CHAN_HT40))
    6479                 :     192330 :                         continue;
    6480 [ +  + ][ +  + ]:     213700 :                 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
    6481                 :      29918 :                         chan->flag |= HOSTAPD_CHAN_HT40MINUS;
    6482 [ +  + ][ +  + ]:     213700 :                 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
    6483                 :      34192 :                         chan->flag |= HOSTAPD_CHAN_HT40PLUS;
    6484                 :            :         }
    6485                 :      21370 : }
    6486                 :            : 
    6487                 :            : 
    6488                 :      10685 : static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
    6489                 :            :                                       struct phy_info_arg *results)
    6490                 :            : {
    6491                 :            :         u16 m;
    6492                 :            : 
    6493         [ +  + ]:      32055 :         for (m = 0; m < *results->num_modes; m++) {
    6494                 :            :                 int c;
    6495                 :      21370 :                 struct hostapd_hw_modes *mode = &results->modes[m];
    6496                 :            : 
    6497         [ +  + ]:     427400 :                 for (c = 0; c < mode->num_channels; c++) {
    6498                 :     406030 :                         struct hostapd_channel_data *chan = &mode->channels[c];
    6499 [ +  + ][ +  + ]:     406030 :                         if ((u32) chan->freq - 10 >= start &&
    6500                 :     254303 :                             (u32) chan->freq + 10 <= end)
    6501                 :      49151 :                                 chan->max_tx_power = max_eirp;
    6502                 :            :                 }
    6503                 :            :         }
    6504                 :      10685 : }
    6505                 :            : 
    6506                 :            : 
    6507                 :       6411 : static void nl80211_reg_rule_ht40(u32 start, u32 end,
    6508                 :            :                                   struct phy_info_arg *results)
    6509                 :            : {
    6510                 :            :         u16 m;
    6511                 :            : 
    6512         [ +  + ]:      19233 :         for (m = 0; m < *results->num_modes; m++) {
    6513         [ -  + ]:      12822 :                 if (!(results->modes[m].ht_capab &
    6514                 :            :                       HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
    6515                 :          0 :                         continue;
    6516                 :      12822 :                 nl80211_set_ht40_mode(&results->modes[m], start, end);
    6517                 :            :         }
    6518                 :       6411 : }
    6519                 :            : 
    6520                 :            : 
    6521                 :      10685 : static void nl80211_reg_rule_sec(struct nlattr *tb[],
    6522                 :            :                                  struct phy_info_arg *results)
    6523                 :            : {
    6524                 :            :         u32 start, end, max_bw;
    6525                 :            :         u16 m;
    6526                 :            : 
    6527 [ +  - ][ +  - ]:      10685 :         if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
    6528         [ -  + ]:      10685 :             tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
    6529                 :      10685 :             tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
    6530                 :          0 :                 return;
    6531                 :            : 
    6532                 :      10685 :         start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
    6533                 :      10685 :         end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
    6534                 :      10685 :         max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
    6535                 :            : 
    6536         [ -  + ]:      10685 :         if (max_bw < 20)
    6537                 :          0 :                 return;
    6538                 :            : 
    6539         [ +  + ]:      32055 :         for (m = 0; m < *results->num_modes; m++) {
    6540         [ -  + ]:      21370 :                 if (!(results->modes[m].ht_capab &
    6541                 :            :                       HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
    6542                 :          0 :                         continue;
    6543                 :      21370 :                 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
    6544                 :            :         }
    6545                 :            : }
    6546                 :            : 
    6547                 :            : 
    6548                 :          0 : static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
    6549                 :            :                                  int end)
    6550                 :            : {
    6551                 :            :         int c;
    6552                 :            : 
    6553         [ #  # ]:          0 :         for (c = 0; c < mode->num_channels; c++) {
    6554                 :          0 :                 struct hostapd_channel_data *chan = &mode->channels[c];
    6555 [ #  # ][ #  # ]:          0 :                 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
    6556                 :          0 :                         chan->flag |= HOSTAPD_CHAN_VHT_10_70;
    6557                 :            : 
    6558 [ #  # ][ #  # ]:          0 :                 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
    6559                 :          0 :                         chan->flag |= HOSTAPD_CHAN_VHT_30_50;
    6560                 :            : 
    6561 [ #  # ][ #  # ]:          0 :                 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
    6562                 :          0 :                         chan->flag |= HOSTAPD_CHAN_VHT_50_30;
    6563                 :            : 
    6564 [ #  # ][ #  # ]:          0 :                 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
    6565                 :          0 :                         chan->flag |= HOSTAPD_CHAN_VHT_70_10;
    6566                 :            :         }
    6567                 :          0 : }
    6568                 :            : 
    6569                 :            : 
    6570                 :      10685 : static void nl80211_reg_rule_vht(struct nlattr *tb[],
    6571                 :            :                                  struct phy_info_arg *results)
    6572                 :            : {
    6573                 :            :         u32 start, end, max_bw;
    6574                 :            :         u16 m;
    6575                 :            : 
    6576 [ +  - ][ +  - ]:      10685 :         if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
    6577         [ -  + ]:      10685 :             tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
    6578                 :      10685 :             tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
    6579                 :          0 :                 return;
    6580                 :            : 
    6581                 :      10685 :         start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
    6582                 :      10685 :         end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
    6583                 :      10685 :         max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
    6584                 :            : 
    6585         [ +  - ]:      10685 :         if (max_bw < 80)
    6586                 :      10685 :                 return;
    6587                 :            : 
    6588         [ #  # ]:      10685 :         for (m = 0; m < *results->num_modes; m++) {
    6589         [ #  # ]:          0 :                 if (!(results->modes[m].ht_capab &
    6590                 :            :                       HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
    6591                 :          0 :                         continue;
    6592                 :            :                 /* TODO: use a real VHT support indication */
    6593         [ #  # ]:          0 :                 if (!results->modes[m].vht_capab)
    6594                 :          0 :                         continue;
    6595                 :            : 
    6596                 :          0 :                 nl80211_set_vht_mode(&results->modes[m], start, end);
    6597                 :            :         }
    6598                 :            : }
    6599                 :            : 
    6600                 :            : 
    6601                 :          0 : static const char * dfs_domain_name(enum nl80211_dfs_regions region)
    6602                 :            : {
    6603   [ #  #  #  #  :          0 :         switch (region) {
                      # ]
    6604                 :            :         case NL80211_DFS_UNSET:
    6605                 :          0 :                 return "DFS-UNSET";
    6606                 :            :         case NL80211_DFS_FCC:
    6607                 :          0 :                 return "DFS-FCC";
    6608                 :            :         case NL80211_DFS_ETSI:
    6609                 :          0 :                 return "DFS-ETSI";
    6610                 :            :         case NL80211_DFS_JP:
    6611                 :          0 :                 return "DFS-JP";
    6612                 :            :         default:
    6613                 :          0 :                 return "DFS-invalid";
    6614                 :            :         }
    6615                 :            : }
    6616                 :            : 
    6617                 :            : 
    6618                 :       2137 : static int nl80211_get_reg(struct nl_msg *msg, void *arg)
    6619                 :            : {
    6620                 :       2137 :         struct phy_info_arg *results = arg;
    6621                 :            :         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
    6622                 :       2137 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    6623                 :            :         struct nlattr *nl_rule;
    6624                 :            :         struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
    6625                 :            :         int rem_rule;
    6626                 :            :         static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
    6627                 :            :                 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
    6628                 :            :                 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
    6629                 :            :                 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
    6630                 :            :                 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
    6631                 :            :                 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
    6632                 :            :                 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
    6633                 :            :         };
    6634                 :            : 
    6635                 :       2137 :         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    6636                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    6637 [ -  + ][ +  - ]:       2137 :         if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
    6638                 :       2137 :             !tb_msg[NL80211_ATTR_REG_RULES]) {
    6639                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
    6640                 :            :                            "available");
    6641                 :          0 :                 return NL_SKIP;
    6642                 :            :         }
    6643                 :            : 
    6644         [ -  + ]:       2137 :         if (tb_msg[NL80211_ATTR_DFS_REGION]) {
    6645                 :            :                 enum nl80211_dfs_regions dfs_domain;
    6646                 :          0 :                 dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
    6647                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
    6648                 :          0 :                            (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
    6649                 :            :                            dfs_domain_name(dfs_domain));
    6650                 :            :         } else {
    6651                 :       2137 :                 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
    6652                 :       2137 :                            (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
    6653                 :            :         }
    6654                 :            : 
    6655         [ +  + ]:      12822 :         nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
    6656                 :            :         {
    6657                 :      10685 :                 u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
    6658                 :      21370 :                 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
    6659                 :      10685 :                           nla_data(nl_rule), nla_len(nl_rule), reg_policy);
    6660 [ -  + ][ +  - ]:      10685 :                 if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
    6661                 :      10685 :                     tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
    6662                 :          0 :                         continue;
    6663                 :      10685 :                 start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
    6664                 :      10685 :                 end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
    6665         [ +  - ]:      10685 :                 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
    6666                 :      10685 :                         max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
    6667         [ +  - ]:      10685 :                 if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
    6668                 :      10685 :                         max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
    6669         [ +  - ]:      10685 :                 if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
    6670                 :      10685 :                         flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
    6671                 :            : 
    6672 [ +  + ][ -  + ]:      10685 :                 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
         [ -  + ][ -  + ]
         [ -  + ][ -  + ]
         [ -  + ][ +  + ]
    6673                 :            :                            start, end, max_bw, max_eirp,
    6674                 :      10685 :                            flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
    6675                 :      10685 :                            flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
    6676                 :      10685 :                            flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
    6677                 :      10685 :                            flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
    6678                 :            :                            "",
    6679                 :      10685 :                            flags & NL80211_RRF_DFS ? " (DFS)" : "",
    6680                 :      10685 :                            flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
    6681                 :      10685 :                            flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
    6682                 :      10685 :                            flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
    6683         [ +  + ]:      10685 :                 if (max_bw >= 40)
    6684                 :       6411 :                         nl80211_reg_rule_ht40(start, end, results);
    6685         [ +  - ]:      10685 :                 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
    6686                 :      10685 :                         nl80211_reg_rule_max_eirp(start, end, max_eirp,
    6687                 :            :                                                   results);
    6688                 :            :         }
    6689                 :            : 
    6690         [ +  + ]:      12822 :         nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
    6691                 :            :         {
    6692                 :      21370 :                 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
    6693                 :      10685 :                           nla_data(nl_rule), nla_len(nl_rule), reg_policy);
    6694                 :      10685 :                 nl80211_reg_rule_sec(tb_rule, results);
    6695                 :            :         }
    6696                 :            : 
    6697         [ +  + ]:      12822 :         nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
    6698                 :            :         {
    6699                 :      21370 :                 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
    6700                 :      10685 :                           nla_data(nl_rule), nla_len(nl_rule), reg_policy);
    6701                 :      10685 :                 nl80211_reg_rule_vht(tb_rule, results);
    6702                 :            :         }
    6703                 :            : 
    6704                 :       2137 :         return NL_SKIP;
    6705                 :            : }
    6706                 :            : 
    6707                 :            : 
    6708                 :       2137 : static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
    6709                 :            :                                         struct phy_info_arg *results)
    6710                 :            : {
    6711                 :            :         struct nl_msg *msg;
    6712                 :            : 
    6713                 :       2137 :         msg = nlmsg_alloc();
    6714         [ -  + ]:       2137 :         if (!msg)
    6715                 :          0 :                 return -ENOMEM;
    6716                 :            : 
    6717                 :       2137 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
    6718                 :       2137 :         return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
    6719                 :            : }
    6720                 :            : 
    6721                 :            : 
    6722                 :            : static struct hostapd_hw_modes *
    6723                 :       2137 : wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
    6724                 :            : {
    6725                 :            :         u32 feat;
    6726                 :       2137 :         struct i802_bss *bss = priv;
    6727                 :       2137 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    6728                 :            :         struct nl_msg *msg;
    6729                 :       2137 :         struct phy_info_arg result = {
    6730                 :            :                 .num_modes = num_modes,
    6731                 :            :                 .modes = NULL,
    6732                 :            :                 .last_mode = -1,
    6733                 :            :         };
    6734                 :            : 
    6735                 :       2137 :         *num_modes = 0;
    6736                 :       2137 :         *flags = 0;
    6737                 :            : 
    6738                 :       2137 :         msg = nlmsg_alloc();
    6739         [ -  + ]:       2137 :         if (!msg)
    6740                 :          0 :                 return NULL;
    6741                 :            : 
    6742                 :       2137 :         feat = get_nl80211_protocol_features(drv);
    6743         [ +  - ]:       2137 :         if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
    6744                 :       2137 :                 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
    6745                 :            :         else
    6746                 :          0 :                 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
    6747                 :            : 
    6748         [ +  - ]:       2137 :         NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
    6749         [ -  + ]:       2137 :         if (nl80211_set_iface_id(msg, bss) < 0)
    6750                 :          0 :                 goto nla_put_failure;
    6751                 :            : 
    6752         [ +  - ]:       2137 :         if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
    6753                 :       2137 :                 nl80211_set_regulatory_flags(drv, &result);
    6754                 :       2137 :                 return wpa_driver_nl80211_postprocess_modes(result.modes,
    6755                 :            :                                                             num_modes);
    6756                 :            :         }
    6757                 :          0 :         msg = NULL;
    6758                 :            :  nla_put_failure:
    6759                 :          0 :         nlmsg_free(msg);
    6760                 :       2137 :         return NULL;
    6761                 :            : }
    6762                 :            : 
    6763                 :            : 
    6764                 :         12 : static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
    6765                 :            :                                         const void *data, size_t len,
    6766                 :            :                                         int encrypt, int noack)
    6767                 :            : {
    6768                 :         12 :         __u8 rtap_hdr[] = {
    6769                 :            :                 0x00, 0x00, /* radiotap version */
    6770                 :            :                 0x0e, 0x00, /* radiotap length */
    6771                 :            :                 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
    6772                 :            :                 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
    6773                 :            :                 0x00,       /* padding */
    6774                 :            :                 0x00, 0x00, /* RX and TX flags to indicate that */
    6775                 :            :                 0x00, 0x00, /* this is the injected frame directly */
    6776                 :            :         };
    6777                 :         12 :         struct iovec iov[2] = {
    6778                 :            :                 {
    6779                 :            :                         .iov_base = &rtap_hdr,
    6780                 :            :                         .iov_len = sizeof(rtap_hdr),
    6781                 :            :                 },
    6782                 :            :                 {
    6783                 :            :                         .iov_base = (void *) data,
    6784                 :            :                         .iov_len = len,
    6785                 :            :                 }
    6786                 :            :         };
    6787                 :         12 :         struct msghdr msg = {
    6788                 :            :                 .msg_name = NULL,
    6789                 :            :                 .msg_namelen = 0,
    6790                 :            :                 .msg_iov = iov,
    6791                 :            :                 .msg_iovlen = 2,
    6792                 :            :                 .msg_control = NULL,
    6793                 :            :                 .msg_controllen = 0,
    6794                 :            :                 .msg_flags = 0,
    6795                 :            :         };
    6796                 :            :         int res;
    6797                 :         12 :         u16 txflags = 0;
    6798                 :            : 
    6799         [ +  + ]:         12 :         if (encrypt)
    6800                 :          8 :                 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
    6801                 :            : 
    6802         [ -  + ]:         12 :         if (drv->monitor_sock < 0) {
    6803                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
    6804                 :            :                            "for %s", __func__);
    6805                 :          0 :                 return -1;
    6806                 :            :         }
    6807                 :            : 
    6808         [ +  + ]:         12 :         if (noack)
    6809                 :          2 :                 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
    6810                 :         12 :         WPA_PUT_LE16(&rtap_hdr[12], txflags);
    6811                 :            : 
    6812                 :         12 :         res = sendmsg(drv->monitor_sock, &msg, 0);
    6813         [ -  + ]:         12 :         if (res < 0) {
    6814                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
    6815                 :          0 :                 return -1;
    6816                 :            :         }
    6817                 :         12 :         return 0;
    6818                 :            : }
    6819                 :            : 
    6820                 :            : 
    6821                 :       4004 : static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
    6822                 :            :                                          const void *data, size_t len,
    6823                 :            :                                          int encrypt, int noack,
    6824                 :            :                                          unsigned int freq, int no_cck,
    6825                 :            :                                          int offchanok, unsigned int wait_time)
    6826                 :            : {
    6827                 :       4004 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    6828                 :            :         u64 cookie;
    6829                 :            :         int res;
    6830                 :            : 
    6831         [ +  + ]:       4004 :         if (freq == 0) {
    6832                 :       3769 :                 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
    6833                 :            :                            bss->freq);
    6834                 :       3769 :                 freq = bss->freq;
    6835                 :            :         }
    6836                 :            : 
    6837         [ +  + ]:       4004 :         if (drv->use_monitor) {
    6838                 :         12 :                 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
    6839                 :            :                            freq, bss->freq);
    6840                 :         12 :                 return wpa_driver_nl80211_send_mntr(drv, data, len,
    6841                 :            :                                                     encrypt, noack);
    6842                 :            :         }
    6843                 :            : 
    6844                 :       3992 :         wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
    6845                 :       3992 :         res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
    6846                 :            :                                      &cookie, no_cck, noack, offchanok);
    6847 [ +  + ][ +  + ]:       3992 :         if (res == 0 && !noack) {
    6848                 :            :                 const struct ieee80211_mgmt *mgmt;
    6849                 :            :                 u16 fc;
    6850                 :            : 
    6851                 :       2605 :                 mgmt = (const struct ieee80211_mgmt *) data;
    6852                 :       2605 :                 fc = le_to_host16(mgmt->frame_control);
    6853 [ +  - ][ +  + ]:       2605 :                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
    6854                 :       2605 :                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
    6855                 :        475 :                         wpa_printf(MSG_MSGDUMP,
    6856                 :            :                                    "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
    6857                 :            :                                    (long long unsigned int)
    6858                 :        475 :                                    drv->send_action_cookie,
    6859                 :            :                                    (long long unsigned int) cookie);
    6860                 :        475 :                         drv->send_action_cookie = cookie;
    6861                 :            :                 }
    6862                 :            :         }
    6863                 :            : 
    6864                 :       4004 :         return res;
    6865                 :            : }
    6866                 :            : 
    6867                 :            : 
    6868                 :       4335 : static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
    6869                 :            :                                         size_t data_len, int noack,
    6870                 :            :                                         unsigned int freq, int no_cck,
    6871                 :            :                                         int offchanok,
    6872                 :            :                                         unsigned int wait_time)
    6873                 :            : {
    6874                 :       4335 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    6875                 :            :         struct ieee80211_mgmt *mgmt;
    6876                 :       4335 :         int encrypt = 1;
    6877                 :            :         u16 fc;
    6878                 :            : 
    6879                 :       4335 :         mgmt = (struct ieee80211_mgmt *) data;
    6880                 :       4335 :         fc = le_to_host16(mgmt->frame_control);
    6881                 :       4335 :         wpa_printf(MSG_DEBUG, "nl80211: send_mlme - noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x nlmode=%d",
    6882                 :       4335 :                    noack, freq, no_cck, offchanok, wait_time, fc, drv->nlmode);
    6883                 :            : 
    6884 [ -  + ][ +  + ]:       4335 :         if ((is_sta_interface(drv->nlmode) ||
    6885         [ +  - ]:        335 :              drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
    6886         [ +  - ]:        335 :             WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
    6887                 :        335 :             WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
    6888                 :            :                 /*
    6889                 :            :                  * The use of last_mgmt_freq is a bit of a hack,
    6890                 :            :                  * but it works due to the single-threaded nature
    6891                 :            :                  * of wpa_supplicant.
    6892                 :            :                  */
    6893         [ +  - ]:        335 :                 if (freq == 0) {
    6894                 :        335 :                         wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
    6895                 :            :                                    drv->last_mgmt_freq);
    6896                 :        335 :                         freq = drv->last_mgmt_freq;
    6897                 :            :                 }
    6898                 :        335 :                 return nl80211_send_frame_cmd(bss, freq, 0,
    6899                 :            :                                               data, data_len, NULL, 1, noack,
    6900                 :            :                                               1);
    6901                 :            :         }
    6902                 :            : 
    6903 [ -  + ][ #  # ]:       4000 :         if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
    6904         [ #  # ]:          0 :                 if (freq == 0) {
    6905                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
    6906                 :            :                                    bss->freq);
    6907                 :          0 :                         freq = bss->freq;
    6908                 :            :                 }
    6909         [ #  # ]:          0 :                 return nl80211_send_frame_cmd(bss, freq,
    6910                 :          0 :                                               (int) freq == bss->freq ? 0 :
    6911                 :            :                                               wait_time,
    6912                 :            :                                               data, data_len,
    6913                 :            :                                               &drv->send_action_cookie,
    6914                 :            :                                               no_cck, noack, offchanok);
    6915                 :            :         }
    6916                 :            : 
    6917 [ +  - ][ +  + ]:       4000 :         if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
    6918                 :       4000 :             WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
    6919                 :            :                 /*
    6920                 :            :                  * Only one of the authentication frame types is encrypted.
    6921                 :            :                  * In order for static WEP encryption to work properly (i.e.,
    6922                 :            :                  * to not encrypt the frame), we need to tell mac80211 about
    6923                 :            :                  * the frames that must not be encrypted.
    6924                 :            :                  */
    6925                 :        695 :                 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
    6926                 :        695 :                 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
    6927 [ +  + ][ +  - ]:        695 :                 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
    6928                 :        695 :                         encrypt = 0;
    6929                 :            :         }
    6930                 :            : 
    6931                 :       4000 :         wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
    6932                 :       4335 :         return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
    6933                 :            :                                              noack, freq, no_cck, offchanok,
    6934                 :            :                                              wait_time);
    6935                 :            : }
    6936                 :            : 
    6937                 :            : 
    6938                 :       1154 : static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
    6939                 :            :                            int slot, int ht_opmode, int ap_isolate,
    6940                 :            :                            int *basic_rates)
    6941                 :            : {
    6942                 :       1154 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    6943                 :            :         struct nl_msg *msg;
    6944                 :            : 
    6945                 :       1154 :         msg = nlmsg_alloc();
    6946         [ -  + ]:       1154 :         if (!msg)
    6947                 :          0 :                 return -ENOMEM;
    6948                 :            : 
    6949                 :       1154 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
    6950                 :            : 
    6951         [ +  - ]:       1154 :         if (cts >= 0)
    6952         [ +  - ]:       1154 :                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
    6953         [ +  - ]:       1154 :         if (preamble >= 0)
    6954         [ +  - ]:       1154 :                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
    6955         [ +  + ]:       1154 :         if (slot >= 0)
    6956         [ +  - ]:       1153 :                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
    6957         [ +  + ]:       1154 :         if (ht_opmode >= 0)
    6958         [ +  - ]:       1131 :                 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
    6959         [ +  - ]:       1154 :         if (ap_isolate >= 0)
    6960         [ +  - ]:       1154 :                 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
    6961                 :            : 
    6962         [ +  - ]:       1154 :         if (basic_rates) {
    6963                 :            :                 u8 rates[NL80211_MAX_SUPP_RATES];
    6964                 :       1154 :                 u8 rates_len = 0;
    6965                 :            :                 int i;
    6966                 :            : 
    6967 [ +  - ][ +  + ]:       5127 :                 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
    6968                 :       3973 :                      i++)
    6969                 :       3973 :                         rates[rates_len++] = basic_rates[i] / 5;
    6970                 :            : 
    6971         [ +  - ]:       1154 :                 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
    6972                 :            :         }
    6973                 :            : 
    6974         [ +  - ]:       1154 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
    6975                 :            : 
    6976                 :       1154 :         return send_and_recv_msgs(drv, msg, NULL, NULL);
    6977                 :            :  nla_put_failure:
    6978                 :          0 :         nlmsg_free(msg);
    6979                 :       1154 :         return -ENOBUFS;
    6980                 :            : }
    6981                 :            : 
    6982                 :            : 
    6983                 :          0 : static int wpa_driver_nl80211_set_acl(void *priv,
    6984                 :            :                                       struct hostapd_acl_params *params)
    6985                 :            : {
    6986                 :          0 :         struct i802_bss *bss = priv;
    6987                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    6988                 :            :         struct nl_msg *msg;
    6989                 :            :         struct nlattr *acl;
    6990                 :            :         unsigned int i;
    6991                 :          0 :         int ret = 0;
    6992                 :            : 
    6993         [ #  # ]:          0 :         if (!(drv->capa.max_acl_mac_addrs))
    6994                 :          0 :                 return -ENOTSUP;
    6995                 :            : 
    6996         [ #  # ]:          0 :         if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
    6997                 :          0 :                 return -ENOTSUP;
    6998                 :            : 
    6999                 :          0 :         msg = nlmsg_alloc();
    7000         [ #  # ]:          0 :         if (!msg)
    7001                 :          0 :                 return -ENOMEM;
    7002                 :            : 
    7003         [ #  # ]:          0 :         wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
    7004                 :          0 :                    params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
    7005                 :            : 
    7006                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
    7007                 :            : 
    7008         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    7009                 :            : 
    7010         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
    7011                 :            :                     NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
    7012                 :            :                     NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
    7013                 :            : 
    7014                 :          0 :         acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
    7015         [ #  # ]:          0 :         if (acl == NULL)
    7016                 :          0 :                 goto nla_put_failure;
    7017                 :            : 
    7018         [ #  # ]:          0 :         for (i = 0; i < params->num_mac_acl; i++)
    7019         [ #  # ]:          0 :                 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
    7020                 :            : 
    7021                 :          0 :         nla_nest_end(msg, acl);
    7022                 :            : 
    7023                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    7024                 :          0 :         msg = NULL;
    7025         [ #  # ]:          0 :         if (ret) {
    7026                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
    7027                 :            :                            ret, strerror(-ret));
    7028                 :            :         }
    7029                 :            : 
    7030                 :            : nla_put_failure:
    7031                 :          0 :         nlmsg_free(msg);
    7032                 :            : 
    7033                 :          0 :         return ret;
    7034                 :            : }
    7035                 :            : 
    7036                 :            : 
    7037                 :       1154 : static int wpa_driver_nl80211_set_ap(void *priv,
    7038                 :            :                                      struct wpa_driver_ap_params *params)
    7039                 :            : {
    7040                 :       1154 :         struct i802_bss *bss = priv;
    7041                 :       1154 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7042                 :            :         struct nl_msg *msg;
    7043                 :       1154 :         u8 cmd = NL80211_CMD_NEW_BEACON;
    7044                 :            :         int ret;
    7045                 :            :         int beacon_set;
    7046                 :       1154 :         int ifindex = if_nametoindex(bss->ifname);
    7047                 :            :         int num_suites;
    7048                 :            :         u32 suites[10], suite;
    7049                 :            :         u32 ver;
    7050                 :            : 
    7051                 :       1154 :         beacon_set = bss->beacon_set;
    7052                 :            : 
    7053                 :       1154 :         msg = nlmsg_alloc();
    7054         [ -  + ]:       1154 :         if (!msg)
    7055                 :          0 :                 return -ENOMEM;
    7056                 :            : 
    7057                 :       1154 :         wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
    7058                 :            :                    beacon_set);
    7059         [ +  + ]:       1154 :         if (beacon_set)
    7060                 :        726 :                 cmd = NL80211_CMD_SET_BEACON;
    7061                 :            : 
    7062                 :       1154 :         nl80211_cmd(drv, msg, 0, cmd);
    7063                 :       1154 :         wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
    7064                 :       1154 :                     params->head, params->head_len);
    7065         [ +  - ]:       1154 :         NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
    7066                 :       1154 :         wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
    7067                 :       1154 :                     params->tail, params->tail_len);
    7068         [ +  - ]:       1154 :         NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
    7069                 :       1154 :         wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
    7070         [ +  - ]:       1154 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
    7071                 :       1154 :         wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
    7072         [ +  - ]:       1154 :         NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
    7073                 :       1154 :         wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
    7074         [ +  - ]:       1154 :         NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
    7075                 :       1154 :         wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
    7076                 :       1154 :                           params->ssid, params->ssid_len);
    7077         [ +  - ]:       1154 :         NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
    7078                 :            :                 params->ssid);
    7079 [ -  + ][ #  # ]:       1154 :         if (params->proberesp && params->proberesp_len) {
    7080                 :          0 :                 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
    7081                 :          0 :                             params->proberesp, params->proberesp_len);
    7082         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
    7083                 :            :                         params->proberesp);
    7084                 :            :         }
    7085   [ +  +  +  - ]:       1154 :         switch (params->hide_ssid) {
    7086                 :            :         case NO_SSID_HIDING:
    7087                 :       1151 :                 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
    7088         [ +  - ]:       1151 :                 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
    7089                 :            :                             NL80211_HIDDEN_SSID_NOT_IN_USE);
    7090                 :       1151 :                 break;
    7091                 :            :         case HIDDEN_SSID_ZERO_LEN:
    7092                 :          2 :                 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
    7093         [ +  - ]:          2 :                 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
    7094                 :            :                             NL80211_HIDDEN_SSID_ZERO_LEN);
    7095                 :          2 :                 break;
    7096                 :            :         case HIDDEN_SSID_ZERO_CONTENTS:
    7097                 :          1 :                 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
    7098         [ +  - ]:          1 :                 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
    7099                 :            :                             NL80211_HIDDEN_SSID_ZERO_CONTENTS);
    7100                 :          1 :                 break;
    7101                 :            :         }
    7102                 :       1154 :         wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
    7103         [ +  + ]:       1154 :         if (params->privacy)
    7104         [ +  - ]:       1057 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
    7105                 :       1154 :         wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
    7106         [ +  + ]:       1154 :         if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
    7107                 :            :             (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
    7108                 :            :                 /* Leave out the attribute */
    7109         [ +  + ]:        693 :         } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
    7110         [ +  - ]:          3 :                 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
    7111                 :            :                             NL80211_AUTHTYPE_SHARED_KEY);
    7112                 :            :         else
    7113         [ +  - ]:        690 :                 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
    7114                 :            :                             NL80211_AUTHTYPE_OPEN_SYSTEM);
    7115                 :            : 
    7116                 :       1154 :         wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
    7117                 :       1154 :         ver = 0;
    7118         [ +  + ]:       1154 :         if (params->wpa_version & WPA_PROTO_WPA)
    7119                 :         59 :                 ver |= NL80211_WPA_VERSION_1;
    7120         [ +  + ]:       1154 :         if (params->wpa_version & WPA_PROTO_RSN)
    7121                 :       1032 :                 ver |= NL80211_WPA_VERSION_2;
    7122         [ +  + ]:       1154 :         if (ver)
    7123         [ +  - ]:       1041 :                 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
    7124                 :            : 
    7125                 :       1154 :         wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
    7126                 :            :                    params->key_mgmt_suites);
    7127                 :       1154 :         num_suites = 0;
    7128         [ +  + ]:       1154 :         if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
    7129                 :        164 :                 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
    7130         [ +  + ]:       1154 :         if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
    7131                 :        971 :                 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
    7132         [ +  + ]:       1154 :         if (num_suites) {
    7133         [ +  - ]:       1126 :                 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
    7134                 :            :                         num_suites * sizeof(u32), suites);
    7135                 :            :         }
    7136                 :            : 
    7137 [ +  + ][ -  + ]:       1154 :         if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
    7138                 :        164 :             params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
    7139         [ #  # ]:          0 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
    7140                 :            : 
    7141                 :       1154 :         wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
    7142                 :            :                    params->pairwise_ciphers);
    7143                 :       1154 :         num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
    7144                 :            :                                                  suites, ARRAY_SIZE(suites));
    7145         [ +  + ]:       1154 :         if (num_suites) {
    7146         [ +  - ]:       1051 :                 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
    7147                 :            :                         num_suites * sizeof(u32), suites);
    7148                 :            :         }
    7149                 :            : 
    7150                 :       1154 :         wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
    7151                 :            :                    params->group_cipher);
    7152                 :       1154 :         suite = wpa_cipher_to_cipher_suite(params->group_cipher);
    7153         [ +  + ]:       1154 :         if (suite)
    7154         [ +  - ]:       1057 :                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite);
    7155                 :            : 
    7156         [ +  - ]:       1154 :         if (params->beacon_ies) {
    7157                 :       1154 :                 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
    7158                 :            :                                 params->beacon_ies);
    7159         [ +  - ]:       1154 :                 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
    7160                 :            :                         wpabuf_head(params->beacon_ies));
    7161                 :            :         }
    7162         [ +  - ]:       1154 :         if (params->proberesp_ies) {
    7163                 :       1154 :                 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
    7164                 :            :                                 params->proberesp_ies);
    7165         [ +  - ]:       1154 :                 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
    7166                 :            :                         wpabuf_len(params->proberesp_ies),
    7167                 :            :                         wpabuf_head(params->proberesp_ies));
    7168                 :            :         }
    7169         [ +  - ]:       1154 :         if (params->assocresp_ies) {
    7170                 :       1154 :                 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
    7171                 :            :                                 params->assocresp_ies);
    7172         [ +  - ]:       1154 :                 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
    7173                 :            :                         wpabuf_len(params->assocresp_ies),
    7174                 :            :                         wpabuf_head(params->assocresp_ies));
    7175                 :            :         }
    7176                 :            : 
    7177         [ -  + ]:       1154 :         if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)  {
    7178                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
    7179                 :            :                            params->ap_max_inactivity);
    7180         [ #  # ]:          0 :                 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
    7181                 :            :                             params->ap_max_inactivity);
    7182                 :            :         }
    7183                 :            : 
    7184                 :       1154 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    7185         [ -  + ]:       1154 :         if (ret) {
    7186                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
    7187                 :            :                            ret, strerror(-ret));
    7188                 :            :         } else {
    7189                 :       1154 :                 bss->beacon_set = 1;
    7190                 :       1154 :                 nl80211_set_bss(bss, params->cts_protect, params->preamble,
    7191                 :            :                                 params->short_slot_time, params->ht_opmode,
    7192                 :            :                                 params->isolate, params->basic_rates);
    7193                 :            :         }
    7194                 :       1154 :         return ret;
    7195                 :            :  nla_put_failure:
    7196                 :          0 :         nlmsg_free(msg);
    7197                 :       1154 :         return -ENOBUFS;
    7198                 :            : }
    7199                 :            : 
    7200                 :            : 
    7201                 :        498 : static int nl80211_put_freq_params(struct nl_msg *msg,
    7202                 :            :                                    struct hostapd_freq_params *freq)
    7203                 :            : {
    7204         [ +  - ]:        498 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
    7205         [ +  + ]:        498 :         if (freq->vht_enabled) {
    7206   [ +  -  -  -  :          6 :                 switch (freq->bandwidth) {
                      - ]
    7207                 :            :                 case 20:
    7208         [ +  - ]:          6 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
    7209                 :            :                                     NL80211_CHAN_WIDTH_20);
    7210                 :          6 :                         break;
    7211                 :            :                 case 40:
    7212         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
    7213                 :            :                                     NL80211_CHAN_WIDTH_40);
    7214                 :          0 :                         break;
    7215                 :            :                 case 80:
    7216         [ #  # ]:          0 :                         if (freq->center_freq2)
    7217         [ #  # ]:          0 :                                 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
    7218                 :            :                                             NL80211_CHAN_WIDTH_80P80);
    7219                 :            :                         else
    7220         [ #  # ]:          0 :                                 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
    7221                 :            :                                             NL80211_CHAN_WIDTH_80);
    7222                 :          0 :                         break;
    7223                 :            :                 case 160:
    7224         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
    7225                 :            :                                     NL80211_CHAN_WIDTH_160);
    7226                 :          0 :                         break;
    7227                 :            :                 default:
    7228                 :          0 :                         return -EINVAL;
    7229                 :            :                 }
    7230         [ +  - ]:          6 :                 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
    7231         [ -  + ]:          6 :                 if (freq->center_freq2)
    7232         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
    7233                 :            :                                     freq->center_freq2);
    7234         [ +  + ]:        492 :         } else if (freq->ht_enabled) {
    7235      [ +  +  + ]:        405 :                 switch (freq->sec_channel_offset) {
    7236                 :            :                 case -1:
    7237         [ +  - ]:          3 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
    7238                 :            :                                     NL80211_CHAN_HT40MINUS);
    7239                 :          3 :                         break;
    7240                 :            :                 case 1:
    7241         [ +  - ]:          3 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
    7242                 :            :                                     NL80211_CHAN_HT40PLUS);
    7243                 :          3 :                         break;
    7244                 :            :                 default:
    7245         [ +  - ]:        399 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
    7246                 :            :                                     NL80211_CHAN_HT20);
    7247                 :        399 :                         break;
    7248                 :            :                 }
    7249                 :            :         }
    7250                 :        498 :         return 0;
    7251                 :            : 
    7252                 :            : nla_put_failure:
    7253                 :        498 :         return -ENOBUFS;
    7254                 :            : }
    7255                 :            : 
    7256                 :            : 
    7257                 :        498 : static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
    7258                 :            :                                        struct hostapd_freq_params *freq)
    7259                 :            : {
    7260                 :        498 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7261                 :            :         struct nl_msg *msg;
    7262                 :            :         int ret;
    7263                 :            : 
    7264                 :        498 :         wpa_printf(MSG_DEBUG,
    7265                 :            :                    "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
    7266                 :            :                    freq->freq, freq->ht_enabled, freq->vht_enabled,
    7267                 :            :                    freq->bandwidth, freq->center_freq1, freq->center_freq2);
    7268                 :        498 :         msg = nlmsg_alloc();
    7269         [ -  + ]:        498 :         if (!msg)
    7270                 :          0 :                 return -1;
    7271                 :            : 
    7272                 :        498 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
    7273                 :            : 
    7274         [ +  - ]:        498 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    7275         [ -  + ]:        498 :         if (nl80211_put_freq_params(msg, freq) < 0)
    7276                 :          0 :                 goto nla_put_failure;
    7277                 :            : 
    7278                 :        498 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    7279                 :        498 :         msg = NULL;
    7280         [ +  + ]:        498 :         if (ret == 0) {
    7281                 :        497 :                 bss->freq = freq->freq;
    7282                 :        497 :                 return 0;
    7283                 :            :         }
    7284                 :          1 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
    7285                 :            :                    "%d (%s)", freq->freq, ret, strerror(-ret));
    7286                 :            : nla_put_failure:
    7287                 :          1 :         nlmsg_free(msg);
    7288                 :        498 :         return -1;
    7289                 :            : }
    7290                 :            : 
    7291                 :            : 
    7292                 :       4697 : static u32 sta_flags_nl80211(int flags)
    7293                 :            : {
    7294                 :       4697 :         u32 f = 0;
    7295                 :            : 
    7296         [ +  + ]:       4697 :         if (flags & WPA_STA_AUTHORIZED)
    7297                 :       2161 :                 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
    7298         [ +  + ]:       4697 :         if (flags & WPA_STA_WMM)
    7299                 :       2022 :                 f |= BIT(NL80211_STA_FLAG_WME);
    7300         [ +  + ]:       4697 :         if (flags & WPA_STA_SHORT_PREAMBLE)
    7301                 :       2022 :                 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
    7302         [ +  + ]:       4697 :         if (flags & WPA_STA_MFP)
    7303                 :        712 :                 f |= BIT(NL80211_STA_FLAG_MFP);
    7304         [ +  + ]:       4697 :         if (flags & WPA_STA_TDLS_PEER)
    7305                 :         81 :                 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
    7306                 :            : 
    7307                 :       4697 :         return f;
    7308                 :            : }
    7309                 :            : 
    7310                 :            : 
    7311                 :        755 : static int wpa_driver_nl80211_sta_add(void *priv,
    7312                 :            :                                       struct hostapd_sta_add_params *params)
    7313                 :            : {
    7314                 :        755 :         struct i802_bss *bss = priv;
    7315                 :        755 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7316                 :            :         struct nl_msg *msg;
    7317                 :            :         struct nl80211_sta_flag_update upd;
    7318                 :        755 :         int ret = -ENOBUFS;
    7319                 :            : 
    7320 [ +  + ][ -  + ]:        755 :         if ((params->flags & WPA_STA_TDLS_PEER) &&
    7321                 :         81 :             !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
    7322                 :          0 :                 return -EOPNOTSUPP;
    7323                 :            : 
    7324                 :        755 :         msg = nlmsg_alloc();
    7325         [ -  + ]:        755 :         if (!msg)
    7326                 :          0 :                 return -ENOMEM;
    7327                 :            : 
    7328         [ +  + ]:        755 :         wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
    7329                 :       5285 :                    params->set ? "Set" : "Add", MAC2STR(params->addr));
    7330         [ +  + ]:        755 :         nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
    7331                 :            :                     NL80211_CMD_NEW_STATION);
    7332                 :            : 
    7333         [ +  - ]:        755 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
    7334         [ +  - ]:        755 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
    7335         [ +  - ]:        755 :         NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
    7336                 :            :                 params->supp_rates);
    7337                 :        755 :         wpa_hexdump(MSG_DEBUG, "  * supported rates", params->supp_rates,
    7338                 :            :                     params->supp_rates_len);
    7339         [ +  + ]:        755 :         if (!params->set) {
    7340         [ +  + ]:        717 :                 if (params->aid) {
    7341                 :        674 :                         wpa_printf(MSG_DEBUG, "  * aid=%u", params->aid);
    7342         [ +  - ]:        674 :                         NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
    7343                 :            :                 } else {
    7344                 :            :                         /*
    7345                 :            :                          * cfg80211 validates that AID is non-zero, so we have
    7346                 :            :                          * to make this a non-zero value for the TDLS case where
    7347                 :            :                          * a dummy STA entry is used for now.
    7348                 :            :                          */
    7349                 :         43 :                         wpa_printf(MSG_DEBUG, "  * aid=1 (TDLS workaround)");
    7350         [ +  - ]:         43 :                         NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
    7351                 :            :                 }
    7352                 :        717 :                 wpa_printf(MSG_DEBUG, "  * listen_interval=%u",
    7353                 :        717 :                            params->listen_interval);
    7354         [ +  - ]:        717 :                 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
    7355                 :            :                             params->listen_interval);
    7356 [ -  + ][ #  # ]:         38 :         } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
    7357                 :          0 :                 wpa_printf(MSG_DEBUG, "  * peer_aid=%u", params->aid);
    7358         [ #  # ]:          0 :                 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
    7359                 :            :         }
    7360         [ +  + ]:        755 :         if (params->ht_capabilities) {
    7361                 :        660 :                 wpa_hexdump(MSG_DEBUG, "  * ht_capabilities",
    7362                 :        660 :                             (u8 *) params->ht_capabilities,
    7363                 :            :                             sizeof(*params->ht_capabilities));
    7364         [ +  - ]:        660 :                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
    7365                 :            :                         sizeof(*params->ht_capabilities),
    7366                 :            :                         params->ht_capabilities);
    7367                 :            :         }
    7368                 :            : 
    7369         [ -  + ]:        755 :         if (params->vht_capabilities) {
    7370                 :          0 :                 wpa_hexdump(MSG_DEBUG, "  * vht_capabilities",
    7371                 :          0 :                             (u8 *) params->vht_capabilities,
    7372                 :            :                             sizeof(*params->vht_capabilities));
    7373         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
    7374                 :            :                         sizeof(*params->vht_capabilities),
    7375                 :            :                         params->vht_capabilities);
    7376                 :            :         }
    7377                 :            : 
    7378         [ -  + ]:        755 :         if (params->vht_opmode_enabled) {
    7379                 :          0 :                 wpa_printf(MSG_DEBUG, "  * opmode=%u", params->vht_opmode);
    7380         [ #  # ]:          0 :                 NLA_PUT_U8(msg, NL80211_ATTR_OPMODE_NOTIF,
    7381                 :            :                            params->vht_opmode);
    7382                 :            :         }
    7383                 :            : 
    7384                 :        755 :         wpa_printf(MSG_DEBUG, "  * capability=0x%x", params->capability);
    7385         [ +  - ]:        755 :         NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
    7386                 :            : 
    7387         [ +  + ]:        755 :         if (params->ext_capab) {
    7388                 :         38 :                 wpa_hexdump(MSG_DEBUG, "  * ext_capab",
    7389                 :         38 :                             params->ext_capab, params->ext_capab_len);
    7390         [ +  - ]:         38 :                 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
    7391                 :            :                         params->ext_capab_len, params->ext_capab);
    7392                 :            :         }
    7393                 :            : 
    7394         [ -  + ]:        755 :         if (params->supp_channels) {
    7395                 :          0 :                 wpa_hexdump(MSG_DEBUG, "  * supported channels",
    7396                 :          0 :                             params->supp_channels, params->supp_channels_len);
    7397         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
    7398                 :            :                         params->supp_channels_len, params->supp_channels);
    7399                 :            :         }
    7400                 :            : 
    7401         [ -  + ]:        755 :         if (params->supp_oper_classes) {
    7402                 :          0 :                 wpa_hexdump(MSG_DEBUG, "  * supported operating classes",
    7403                 :          0 :                             params->supp_oper_classes,
    7404                 :            :                             params->supp_oper_classes_len);
    7405         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
    7406                 :            :                         params->supp_oper_classes_len,
    7407                 :            :                         params->supp_oper_classes);
    7408                 :            :         }
    7409                 :            : 
    7410                 :        755 :         os_memset(&upd, 0, sizeof(upd));
    7411                 :        755 :         upd.mask = sta_flags_nl80211(params->flags);
    7412                 :        755 :         upd.set = upd.mask;
    7413                 :        755 :         wpa_printf(MSG_DEBUG, "  * flags set=0x%x mask=0x%x",
    7414                 :            :                    upd.set, upd.mask);
    7415         [ +  - ]:        755 :         NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
    7416                 :            : 
    7417         [ +  + ]:        755 :         if (params->flags & WPA_STA_WMM) {
    7418                 :        674 :                 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
    7419                 :            : 
    7420         [ -  + ]:        674 :                 if (!wme)
    7421                 :          0 :                         goto nla_put_failure;
    7422                 :            : 
    7423                 :        674 :                 wpa_printf(MSG_DEBUG, "  * qosinfo=0x%x", params->qosinfo);
    7424         [ +  - ]:        674 :                 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
    7425                 :            :                                 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
    7426         [ +  - ]:        674 :                 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
    7427                 :            :                                 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
    7428                 :            :                                 WMM_QOSINFO_STA_SP_MASK);
    7429                 :        674 :                 nla_nest_end(msg, wme);
    7430                 :            :         }
    7431                 :            : 
    7432                 :        755 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    7433                 :        755 :         msg = NULL;
    7434         [ -  + ]:        755 :         if (ret)
    7435         [ #  # ]:          0 :                 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
    7436                 :          0 :                            "result: %d (%s)", params->set ? "SET" : "NEW", ret,
    7437                 :            :                            strerror(-ret));
    7438         [ -  + ]:        755 :         if (ret == -EEXIST)
    7439                 :          0 :                 ret = 0;
    7440                 :            :  nla_put_failure:
    7441                 :        755 :         nlmsg_free(msg);
    7442                 :        755 :         return ret;
    7443                 :            : }
    7444                 :            : 
    7445                 :            : 
    7446                 :       1363 : static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
    7447                 :            : {
    7448                 :       1363 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    7449                 :            :         struct nl_msg *msg;
    7450                 :            :         int ret;
    7451                 :            : 
    7452                 :       1363 :         msg = nlmsg_alloc();
    7453         [ -  + ]:       1363 :         if (!msg)
    7454                 :          0 :                 return -ENOMEM;
    7455                 :            : 
    7456                 :       1363 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
    7457                 :            : 
    7458         [ +  - ]:       1363 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
    7459                 :            :                     if_nametoindex(bss->ifname));
    7460         [ +  - ]:       1363 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    7461                 :            : 
    7462                 :       1363 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    7463                 :       1363 :         wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
    7464                 :            :                    " --> %d (%s)",
    7465                 :       9541 :                    bss->ifname, MAC2STR(addr), ret, strerror(-ret));
    7466         [ +  + ]:       1363 :         if (ret == -ENOENT)
    7467                 :        690 :                 return 0;
    7468                 :        673 :         return ret;
    7469                 :            :  nla_put_failure:
    7470                 :          0 :         nlmsg_free(msg);
    7471                 :       1363 :         return -ENOBUFS;
    7472                 :            : }
    7473                 :            : 
    7474                 :            : 
    7475                 :         43 : static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
    7476                 :            :                                  int ifidx)
    7477                 :            : {
    7478                 :            :         struct nl_msg *msg;
    7479                 :            : 
    7480                 :         43 :         wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
    7481                 :            : 
    7482                 :            :         /* stop listening for EAPOL on this interface */
    7483                 :         43 :         del_ifidx(drv, ifidx);
    7484                 :            : 
    7485                 :         43 :         msg = nlmsg_alloc();
    7486         [ -  + ]:         43 :         if (!msg)
    7487                 :          0 :                 goto nla_put_failure;
    7488                 :            : 
    7489                 :         43 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
    7490         [ +  - ]:         43 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
    7491                 :            : 
    7492         [ +  - ]:         43 :         if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
    7493                 :         43 :                 return;
    7494                 :          0 :         msg = NULL;
    7495                 :            :  nla_put_failure:
    7496                 :          0 :         nlmsg_free(msg);
    7497                 :          0 :         wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
    7498                 :            : }
    7499                 :            : 
    7500                 :            : 
    7501                 :       1114 : static const char * nl80211_iftype_str(enum nl80211_iftype mode)
    7502                 :            : {
    7503   [ +  +  +  -  :       1114 :         switch (mode) {
          -  +  -  +  +  
                   -  - ]
    7504                 :            :         case NL80211_IFTYPE_ADHOC:
    7505                 :          5 :                 return "ADHOC";
    7506                 :            :         case NL80211_IFTYPE_STATION:
    7507                 :        546 :                 return "STATION";
    7508                 :            :         case NL80211_IFTYPE_AP:
    7509                 :        356 :                 return "AP";
    7510                 :            :         case NL80211_IFTYPE_AP_VLAN:
    7511                 :          0 :                 return "AP_VLAN";
    7512                 :            :         case NL80211_IFTYPE_WDS:
    7513                 :          0 :                 return "WDS";
    7514                 :            :         case NL80211_IFTYPE_MONITOR:
    7515                 :          2 :                 return "MONITOR";
    7516                 :            :         case NL80211_IFTYPE_MESH_POINT:
    7517                 :          0 :                 return "MESH_POINT";
    7518                 :            :         case NL80211_IFTYPE_P2P_CLIENT:
    7519                 :        105 :                 return "P2P_CLIENT";
    7520                 :            :         case NL80211_IFTYPE_P2P_GO:
    7521                 :        100 :                 return "P2P_GO";
    7522                 :            :         case NL80211_IFTYPE_P2P_DEVICE:
    7523                 :          0 :                 return "P2P_DEVICE";
    7524                 :            :         default:
    7525                 :       1114 :                 return "unknown";
    7526                 :            :         }
    7527                 :            : }
    7528                 :            : 
    7529                 :            : 
    7530                 :         43 : static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
    7531                 :            :                                      const char *ifname,
    7532                 :            :                                      enum nl80211_iftype iftype,
    7533                 :            :                                      const u8 *addr, int wds,
    7534                 :            :                                      int (*handler)(struct nl_msg *, void *),
    7535                 :            :                                      void *arg)
    7536                 :            : {
    7537                 :            :         struct nl_msg *msg;
    7538                 :            :         int ifidx;
    7539                 :         43 :         int ret = -ENOBUFS;
    7540                 :            : 
    7541                 :         43 :         wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
    7542                 :            :                    iftype, nl80211_iftype_str(iftype));
    7543                 :            : 
    7544                 :         43 :         msg = nlmsg_alloc();
    7545         [ -  + ]:         43 :         if (!msg)
    7546                 :          0 :                 return -1;
    7547                 :            : 
    7548                 :         43 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
    7549         [ -  + ]:         43 :         if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
    7550                 :          0 :                 goto nla_put_failure;
    7551         [ +  - ]:         43 :         NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
    7552         [ +  - ]:         43 :         NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
    7553                 :            : 
    7554         [ +  + ]:         43 :         if (iftype == NL80211_IFTYPE_MONITOR) {
    7555                 :            :                 struct nlattr *flags;
    7556                 :            : 
    7557                 :          2 :                 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
    7558         [ -  + ]:          2 :                 if (!flags)
    7559                 :          0 :                         goto nla_put_failure;
    7560                 :            : 
    7561         [ +  - ]:          2 :                 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
    7562                 :            : 
    7563                 :          2 :                 nla_nest_end(msg, flags);
    7564         [ -  + ]:         41 :         } else if (wds) {
    7565         [ #  # ]:          0 :                 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
    7566                 :            :         }
    7567                 :            : 
    7568                 :         43 :         ret = send_and_recv_msgs(drv, msg, handler, arg);
    7569                 :         43 :         msg = NULL;
    7570         [ -  + ]:         43 :         if (ret) {
    7571                 :            :  nla_put_failure:
    7572                 :          0 :                 nlmsg_free(msg);
    7573                 :          0 :                 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
    7574                 :            :                            ifname, ret, strerror(-ret));
    7575                 :          0 :                 return ret;
    7576                 :            :         }
    7577                 :            : 
    7578         [ -  + ]:         43 :         if (iftype == NL80211_IFTYPE_P2P_DEVICE)
    7579                 :          0 :                 return 0;
    7580                 :            : 
    7581                 :         43 :         ifidx = if_nametoindex(ifname);
    7582                 :         43 :         wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
    7583                 :            :                    ifname, ifidx);
    7584                 :            : 
    7585         [ -  + ]:         43 :         if (ifidx <= 0)
    7586                 :          0 :                 return -1;
    7587                 :            : 
    7588                 :            :         /* start listening for EAPOL on this interface */
    7589                 :         43 :         add_ifidx(drv, ifidx);
    7590                 :            : 
    7591   [ +  -  -  + ]:         60 :         if (addr && iftype != NL80211_IFTYPE_MONITOR &&
                 [ +  + ]
    7592                 :         17 :             linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
    7593                 :          0 :                 nl80211_remove_iface(drv, ifidx);
    7594                 :          0 :                 return -1;
    7595                 :            :         }
    7596                 :            : 
    7597                 :         43 :         return ifidx;
    7598                 :            : }
    7599                 :            : 
    7600                 :            : 
    7601                 :         43 : static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
    7602                 :            :                                 const char *ifname, enum nl80211_iftype iftype,
    7603                 :            :                                 const u8 *addr, int wds,
    7604                 :            :                                 int (*handler)(struct nl_msg *, void *),
    7605                 :            :                                 void *arg, int use_existing)
    7606                 :            : {
    7607                 :            :         int ret;
    7608                 :            : 
    7609                 :         43 :         ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
    7610                 :            :                                         arg);
    7611                 :            : 
    7612                 :            :         /* if error occurred and interface exists already */
    7613 [ #  # ][ -  + ]:         43 :         if (ret == -ENFILE && if_nametoindex(ifname)) {
    7614         [ #  # ]:          0 :                 if (use_existing) {
    7615                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
    7616                 :            :                                    ifname);
    7617                 :          0 :                         return -ENFILE;
    7618                 :            :                 }
    7619                 :          0 :                 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
    7620                 :            : 
    7621                 :            :                 /* Try to remove the interface that was already there. */
    7622                 :          0 :                 nl80211_remove_iface(drv, if_nametoindex(ifname));
    7623                 :            : 
    7624                 :            :                 /* Try to create the interface again */
    7625                 :          0 :                 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
    7626                 :            :                                                 wds, handler, arg);
    7627                 :            :         }
    7628                 :            : 
    7629 [ +  - ][ +  + ]:         43 :         if (ret >= 0 && is_p2p_net_interface(iftype))
    7630                 :         24 :                 nl80211_disable_11b_rates(drv, ret, 1);
    7631                 :            : 
    7632                 :         43 :         return ret;
    7633                 :            : }
    7634                 :            : 
    7635                 :            : 
    7636                 :          8 : static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
    7637                 :            : {
    7638                 :            :         struct ieee80211_hdr *hdr;
    7639                 :            :         u16 fc;
    7640                 :            :         union wpa_event_data event;
    7641                 :            : 
    7642                 :          8 :         hdr = (struct ieee80211_hdr *) buf;
    7643                 :          8 :         fc = le_to_host16(hdr->frame_control);
    7644                 :            : 
    7645                 :          8 :         os_memset(&event, 0, sizeof(event));
    7646                 :          8 :         event.tx_status.type = WLAN_FC_GET_TYPE(fc);
    7647                 :          8 :         event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
    7648                 :          8 :         event.tx_status.dst = hdr->addr1;
    7649                 :          8 :         event.tx_status.data = buf;
    7650                 :          8 :         event.tx_status.data_len = len;
    7651                 :          8 :         event.tx_status.ack = ok;
    7652                 :          8 :         wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
    7653                 :          8 : }
    7654                 :            : 
    7655                 :            : 
    7656                 :          0 : static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
    7657                 :            :                              u8 *buf, size_t len)
    7658                 :            : {
    7659                 :          0 :         struct ieee80211_hdr *hdr = (void *)buf;
    7660                 :            :         u16 fc;
    7661                 :            :         union wpa_event_data event;
    7662                 :            : 
    7663         [ #  # ]:          0 :         if (len < sizeof(*hdr))
    7664                 :          0 :                 return;
    7665                 :            : 
    7666                 :          0 :         fc = le_to_host16(hdr->frame_control);
    7667                 :            : 
    7668                 :          0 :         os_memset(&event, 0, sizeof(event));
    7669                 :          0 :         event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
    7670                 :          0 :         event.rx_from_unknown.addr = hdr->addr2;
    7671                 :          0 :         event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
    7672                 :            :                 (WLAN_FC_FROMDS | WLAN_FC_TODS);
    7673                 :          0 :         wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
    7674                 :            : }
    7675                 :            : 
    7676                 :            : 
    7677                 :          8 : static void handle_frame(struct wpa_driver_nl80211_data *drv,
    7678                 :            :                          u8 *buf, size_t len, int datarate, int ssi_signal)
    7679                 :            : {
    7680                 :            :         struct ieee80211_hdr *hdr;
    7681                 :            :         u16 fc;
    7682                 :            :         union wpa_event_data event;
    7683                 :            : 
    7684                 :          8 :         hdr = (struct ieee80211_hdr *) buf;
    7685                 :          8 :         fc = le_to_host16(hdr->frame_control);
    7686                 :            : 
    7687   [ +  -  -  - ]:          8 :         switch (WLAN_FC_GET_TYPE(fc)) {
    7688                 :            :         case WLAN_FC_TYPE_MGMT:
    7689                 :          8 :                 os_memset(&event, 0, sizeof(event));
    7690                 :          8 :                 event.rx_mgmt.frame = buf;
    7691                 :          8 :                 event.rx_mgmt.frame_len = len;
    7692                 :          8 :                 event.rx_mgmt.datarate = datarate;
    7693                 :          8 :                 event.rx_mgmt.ssi_signal = ssi_signal;
    7694                 :          8 :                 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
    7695                 :          8 :                 break;
    7696                 :            :         case WLAN_FC_TYPE_CTRL:
    7697                 :            :                 /* can only get here with PS-Poll frames */
    7698                 :          0 :                 wpa_printf(MSG_DEBUG, "CTRL");
    7699                 :          0 :                 from_unknown_sta(drv, buf, len);
    7700                 :          0 :                 break;
    7701                 :            :         case WLAN_FC_TYPE_DATA:
    7702                 :          0 :                 from_unknown_sta(drv, buf, len);
    7703                 :          0 :                 break;
    7704                 :            :         }
    7705                 :          8 : }
    7706                 :            : 
    7707                 :            : 
    7708                 :         16 : static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
    7709                 :            : {
    7710                 :         16 :         struct wpa_driver_nl80211_data *drv = eloop_ctx;
    7711                 :            :         int len;
    7712                 :            :         unsigned char buf[3000];
    7713                 :            :         struct ieee80211_radiotap_iterator iter;
    7714                 :            :         int ret;
    7715                 :         16 :         int datarate = 0, ssi_signal = 0;
    7716                 :         16 :         int injected = 0, failed = 0, rxflags = 0;
    7717                 :            : 
    7718                 :         16 :         len = recv(sock, buf, sizeof(buf), 0);
    7719         [ -  + ]:         16 :         if (len < 0) {
    7720                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
    7721                 :          0 :                            strerror(errno));
    7722                 :          0 :                 return;
    7723                 :            :         }
    7724                 :            : 
    7725         [ -  + ]:         16 :         if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
    7726                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
    7727                 :          0 :                 return;
    7728                 :            :         }
    7729                 :            : 
    7730                 :            :         while (1) {
    7731                 :         94 :                 ret = ieee80211_radiotap_iterator_next(&iter);
    7732         [ +  + ]:         94 :                 if (ret == -ENOENT)
    7733                 :         16 :                         break;
    7734         [ -  + ]:         78 :                 if (ret) {
    7735                 :          0 :                         wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
    7736                 :            :                                    ret);
    7737                 :          0 :                         return;
    7738                 :            :                 }
    7739   [ +  +  +  +  :         78 :                 switch (iter.this_arg_index) {
             +  +  +  + ]
    7740                 :            :                 case IEEE80211_RADIOTAP_FLAGS:
    7741         [ -  + ]:          8 :                         if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
    7742                 :          0 :                                 len -= 4;
    7743                 :          8 :                         break;
    7744                 :            :                 case IEEE80211_RADIOTAP_RX_FLAGS:
    7745                 :          8 :                         rxflags = 1;
    7746                 :          8 :                         break;
    7747                 :            :                 case IEEE80211_RADIOTAP_TX_FLAGS:
    7748                 :          8 :                         injected = 1;
    7749                 :          8 :                         failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
    7750                 :            :                                         IEEE80211_RADIOTAP_F_TX_FAIL;
    7751                 :          8 :                         break;
    7752                 :            :                 case IEEE80211_RADIOTAP_DATA_RETRIES:
    7753                 :          8 :                         break;
    7754                 :            :                 case IEEE80211_RADIOTAP_CHANNEL:
    7755                 :            :                         /* TODO: convert from freq/flags to channel number */
    7756                 :          8 :                         break;
    7757                 :            :                 case IEEE80211_RADIOTAP_RATE:
    7758                 :         14 :                         datarate = *iter.this_arg * 5;
    7759                 :         14 :                         break;
    7760                 :            :                 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
    7761                 :          8 :                         ssi_signal = (s8) *iter.this_arg;
    7762                 :          8 :                         break;
    7763                 :            :                 }
    7764                 :         78 :         }
    7765                 :            : 
    7766 [ +  + ][ -  + ]:         16 :         if (rxflags && injected)
    7767                 :          0 :                 return;
    7768                 :            : 
    7769         [ +  + ]:         16 :         if (!injected)
    7770                 :          8 :                 handle_frame(drv, buf + iter.max_length,
    7771                 :          8 :                              len - iter.max_length, datarate, ssi_signal);
    7772                 :            :         else
    7773                 :         16 :                 handle_tx_callback(drv->ctx, buf + iter.max_length,
    7774                 :          8 :                                    len - iter.max_length, !failed);
    7775                 :            : }
    7776                 :            : 
    7777                 :            : 
    7778                 :            : /*
    7779                 :            :  * we post-process the filter code later and rewrite
    7780                 :            :  * this to the offset to the last instruction
    7781                 :            :  */
    7782                 :            : #define PASS    0xFF
    7783                 :            : #define FAIL    0xFE
    7784                 :            : 
    7785                 :            : static struct sock_filter msock_filter_insns[] = {
    7786                 :            :         /*
    7787                 :            :          * do a little-endian load of the radiotap length field
    7788                 :            :          */
    7789                 :            :         /* load lower byte into A */
    7790                 :            :         BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 2),
    7791                 :            :         /* put it into X (== index register) */
    7792                 :            :         BPF_STMT(BPF_MISC| BPF_TAX, 0),
    7793                 :            :         /* load upper byte into A */
    7794                 :            :         BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 3),
    7795                 :            :         /* left-shift it by 8 */
    7796                 :            :         BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
    7797                 :            :         /* or with X */
    7798                 :            :         BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
    7799                 :            :         /* put result into X */
    7800                 :            :         BPF_STMT(BPF_MISC| BPF_TAX, 0),
    7801                 :            : 
    7802                 :            :         /*
    7803                 :            :          * Allow management frames through, this also gives us those
    7804                 :            :          * management frames that we sent ourselves with status
    7805                 :            :          */
    7806                 :            :         /* load the lower byte of the IEEE 802.11 frame control field */
    7807                 :            :         BPF_STMT(BPF_LD  | BPF_B | BPF_IND, 0),
    7808                 :            :         /* mask off frame type and version */
    7809                 :            :         BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
    7810                 :            :         /* accept frame if it's both 0, fall through otherwise */
    7811                 :            :         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
    7812                 :            : 
    7813                 :            :         /*
    7814                 :            :          * TODO: add a bit to radiotap RX flags that indicates
    7815                 :            :          * that the sending station is not associated, then
    7816                 :            :          * add a filter here that filters on our DA and that flag
    7817                 :            :          * to allow us to deauth frames to that bad station.
    7818                 :            :          *
    7819                 :            :          * For now allow all To DS data frames through.
    7820                 :            :          */
    7821                 :            :         /* load the IEEE 802.11 frame control field */
    7822                 :            :         BPF_STMT(BPF_LD  | BPF_H | BPF_IND, 0),
    7823                 :            :         /* mask off frame type, version and DS status */
    7824                 :            :         BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
    7825                 :            :         /* accept frame if version 0, type 2 and To DS, fall through otherwise
    7826                 :            :          */
    7827                 :            :         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
    7828                 :            : 
    7829                 :            : #if 0
    7830                 :            :         /*
    7831                 :            :          * drop non-data frames
    7832                 :            :          */
    7833                 :            :         /* load the lower byte of the frame control field */
    7834                 :            :         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
    7835                 :            :         /* mask off QoS bit */
    7836                 :            :         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x0c),
    7837                 :            :         /* drop non-data frames */
    7838                 :            :         BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 8, 0, FAIL),
    7839                 :            : #endif
    7840                 :            :         /* load the upper byte of the frame control field */
    7841                 :            :         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 1),
    7842                 :            :         /* mask off toDS/fromDS */
    7843                 :            :         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x03),
    7844                 :            :         /* accept WDS frames */
    7845                 :            :         BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 3, PASS, 0),
    7846                 :            : 
    7847                 :            :         /*
    7848                 :            :          * add header length to index
    7849                 :            :          */
    7850                 :            :         /* load the lower byte of the frame control field */
    7851                 :            :         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
    7852                 :            :         /* mask off QoS bit */
    7853                 :            :         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x80),
    7854                 :            :         /* right shift it by 6 to give 0 or 2 */
    7855                 :            :         BPF_STMT(BPF_ALU  | BPF_RSH | BPF_K, 6),
    7856                 :            :         /* add data frame header length */
    7857                 :            :         BPF_STMT(BPF_ALU  | BPF_ADD | BPF_K, 24),
    7858                 :            :         /* add index, was start of 802.11 header */
    7859                 :            :         BPF_STMT(BPF_ALU  | BPF_ADD | BPF_X, 0),
    7860                 :            :         /* move to index, now start of LL header */
    7861                 :            :         BPF_STMT(BPF_MISC | BPF_TAX, 0),
    7862                 :            : 
    7863                 :            :         /*
    7864                 :            :          * Accept empty data frames, we use those for
    7865                 :            :          * polling activity.
    7866                 :            :          */
    7867                 :            :         BPF_STMT(BPF_LD  | BPF_W | BPF_LEN, 0),
    7868                 :            :         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
    7869                 :            : 
    7870                 :            :         /*
    7871                 :            :          * Accept EAPOL frames
    7872                 :            :          */
    7873                 :            :         BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 0),
    7874                 :            :         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
    7875                 :            :         BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 4),
    7876                 :            :         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
    7877                 :            : 
    7878                 :            :         /* keep these last two statements or change the code below */
    7879                 :            :         /* return 0 == "DROP" */
    7880                 :            :         BPF_STMT(BPF_RET | BPF_K, 0),
    7881                 :            :         /* return ~0 == "keep all" */
    7882                 :            :         BPF_STMT(BPF_RET | BPF_K, ~0),
    7883                 :            : };
    7884                 :            : 
    7885                 :            : static struct sock_fprog msock_filter = {
    7886                 :            :         .len = ARRAY_SIZE(msock_filter_insns),
    7887                 :            :         .filter = msock_filter_insns,
    7888                 :            : };
    7889                 :            : 
    7890                 :            : 
    7891                 :          2 : static int add_monitor_filter(int s)
    7892                 :            : {
    7893                 :            :         int idx;
    7894                 :            : 
    7895                 :            :         /* rewrite all PASS/FAIL jump offsets */
    7896         [ +  + ]:         60 :         for (idx = 0; idx < msock_filter.len; idx++) {
    7897                 :         58 :                 struct sock_filter *insn = &msock_filter_insns[idx];
    7898                 :            : 
    7899         [ +  + ]:         58 :                 if (BPF_CLASS(insn->code) == BPF_JMP) {
    7900         [ -  + ]:         12 :                         if (insn->code == (BPF_JMP|BPF_JA)) {
    7901         [ #  # ]:          0 :                                 if (insn->k == PASS)
    7902                 :          0 :                                         insn->k = msock_filter.len - idx - 2;
    7903         [ #  # ]:          0 :                                 else if (insn->k == FAIL)
    7904                 :          0 :                                         insn->k = msock_filter.len - idx - 3;
    7905                 :            :                         }
    7906                 :            : 
    7907         [ +  + ]:         12 :                         if (insn->jt == PASS)
    7908                 :          5 :                                 insn->jt = msock_filter.len - idx - 2;
    7909         [ -  + ]:          7 :                         else if (insn->jt == FAIL)
    7910                 :          0 :                                 insn->jt = msock_filter.len - idx - 3;
    7911                 :            : 
    7912         [ -  + ]:         12 :                         if (insn->jf == PASS)
    7913                 :          0 :                                 insn->jf = msock_filter.len - idx - 2;
    7914         [ +  + ]:         12 :                         else if (insn->jf == FAIL)
    7915                 :          2 :                                 insn->jf = msock_filter.len - idx - 3;
    7916                 :            :                 }
    7917                 :            :         }
    7918                 :            : 
    7919         [ -  + ]:          2 :         if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
    7920                 :            :                        &msock_filter, sizeof(msock_filter))) {
    7921                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
    7922                 :          0 :                            strerror(errno));
    7923                 :          0 :                 return -1;
    7924                 :            :         }
    7925                 :            : 
    7926                 :          2 :         return 0;
    7927                 :            : }
    7928                 :            : 
    7929                 :            : 
    7930                 :        374 : static void nl80211_remove_monitor_interface(
    7931                 :            :         struct wpa_driver_nl80211_data *drv)
    7932                 :            : {
    7933         [ +  + ]:        374 :         if (drv->monitor_refcount > 0)
    7934                 :          2 :                 drv->monitor_refcount--;
    7935                 :        374 :         wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
    7936                 :            :                    drv->monitor_refcount);
    7937         [ -  + ]:        374 :         if (drv->monitor_refcount > 0)
    7938                 :        374 :                 return;
    7939                 :            : 
    7940         [ +  + ]:        374 :         if (drv->monitor_ifidx >= 0) {
    7941                 :          2 :                 nl80211_remove_iface(drv, drv->monitor_ifidx);
    7942                 :          2 :                 drv->monitor_ifidx = -1;
    7943                 :            :         }
    7944         [ +  + ]:        374 :         if (drv->monitor_sock >= 0) {
    7945                 :          2 :                 eloop_unregister_read_sock(drv->monitor_sock);
    7946                 :          2 :                 close(drv->monitor_sock);
    7947                 :          2 :                 drv->monitor_sock = -1;
    7948                 :            :         }
    7949                 :            : }
    7950                 :            : 
    7951                 :            : 
    7952                 :            : static int
    7953                 :          2 : nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
    7954                 :            : {
    7955                 :            :         char buf[IFNAMSIZ];
    7956                 :            :         struct sockaddr_ll ll;
    7957                 :            :         int optval;
    7958                 :            :         socklen_t optlen;
    7959                 :            : 
    7960         [ -  + ]:          2 :         if (drv->monitor_ifidx >= 0) {
    7961                 :          0 :                 drv->monitor_refcount++;
    7962                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
    7963                 :            :                            drv->monitor_refcount);
    7964                 :          0 :                 return 0;
    7965                 :            :         }
    7966                 :            : 
    7967         [ -  + ]:          2 :         if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
    7968                 :            :                 /*
    7969                 :            :                  * P2P interface name is of the format p2p-%s-%d. For monitor
    7970                 :            :                  * interface name corresponding to P2P GO, replace "p2p-" with
    7971                 :            :                  * "mon-" to retain the same interface name length and to
    7972                 :            :                  * indicate that it is a monitor interface.
    7973                 :            :                  */
    7974                 :          0 :                 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
    7975                 :            :         } else {
    7976                 :            :                 /* Non-P2P interface with AP functionality. */
    7977                 :          2 :                 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
    7978                 :            :         }
    7979                 :            : 
    7980                 :          2 :         buf[IFNAMSIZ - 1] = '\0';
    7981                 :            : 
    7982                 :          2 :         drv->monitor_ifidx =
    7983                 :          2 :                 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
    7984                 :            :                                      0, NULL, NULL, 0);
    7985                 :            : 
    7986         [ -  + ]:          2 :         if (drv->monitor_ifidx == -EOPNOTSUPP) {
    7987                 :            :                 /*
    7988                 :            :                  * This is backward compatibility for a few versions of
    7989                 :            :                  * the kernel only that didn't advertise the right
    7990                 :            :                  * attributes for the only driver that then supported
    7991                 :            :                  * AP mode w/o monitor -- ath6kl.
    7992                 :            :                  */
    7993                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
    7994                 :            :                            "monitor interface type - try to run without it");
    7995                 :          0 :                 drv->device_ap_sme = 1;
    7996                 :            :         }
    7997                 :            : 
    7998         [ -  + ]:          2 :         if (drv->monitor_ifidx < 0)
    7999                 :          0 :                 return -1;
    8000                 :            : 
    8001         [ -  + ]:          2 :         if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
    8002                 :          0 :                 goto error;
    8003                 :            : 
    8004                 :          2 :         memset(&ll, 0, sizeof(ll));
    8005                 :          2 :         ll.sll_family = AF_PACKET;
    8006                 :          2 :         ll.sll_ifindex = drv->monitor_ifidx;
    8007                 :          2 :         drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    8008         [ -  + ]:          2 :         if (drv->monitor_sock < 0) {
    8009                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
    8010                 :          0 :                            strerror(errno));
    8011                 :          0 :                 goto error;
    8012                 :            :         }
    8013                 :            : 
    8014         [ -  + ]:          2 :         if (add_monitor_filter(drv->monitor_sock)) {
    8015                 :          0 :                 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
    8016                 :            :                            "interface; do filtering in user space");
    8017                 :            :                 /* This works, but will cost in performance. */
    8018                 :            :         }
    8019                 :            : 
    8020         [ -  + ]:          2 :         if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
    8021                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
    8022                 :          0 :                            strerror(errno));
    8023                 :          0 :                 goto error;
    8024                 :            :         }
    8025                 :            : 
    8026                 :          2 :         optlen = sizeof(optval);
    8027                 :          2 :         optval = 20;
    8028         [ -  + ]:          2 :         if (setsockopt
    8029                 :          2 :             (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
    8030                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
    8031                 :          0 :                            strerror(errno));
    8032                 :          0 :                 goto error;
    8033                 :            :         }
    8034                 :            : 
    8035         [ -  + ]:          2 :         if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
    8036                 :            :                                      drv, NULL)) {
    8037                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
    8038                 :          0 :                 goto error;
    8039                 :            :         }
    8040                 :            : 
    8041                 :          2 :         drv->monitor_refcount++;
    8042                 :          2 :         return 0;
    8043                 :            :  error:
    8044                 :          0 :         nl80211_remove_monitor_interface(drv);
    8045                 :          2 :         return -1;
    8046                 :            : }
    8047                 :            : 
    8048                 :            : 
    8049                 :        446 : static int nl80211_setup_ap(struct i802_bss *bss)
    8050                 :            : {
    8051                 :        446 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8052                 :            : 
    8053                 :        446 :         wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
    8054                 :       1338 :                    bss->ifname, drv->device_ap_sme, drv->use_monitor);
    8055                 :            : 
    8056                 :            :         /*
    8057                 :            :          * Disable Probe Request reporting unless we need it in this way for
    8058                 :            :          * devices that include the AP SME, in the other case (unless using
    8059                 :            :          * monitor iface) we'll get it through the nl_mgmt socket instead.
    8060                 :            :          */
    8061         [ +  - ]:        446 :         if (!drv->device_ap_sme)
    8062                 :        446 :                 wpa_driver_nl80211_probe_req_report(bss, 0);
    8063                 :            : 
    8064 [ +  - ][ +  + ]:        446 :         if (!drv->device_ap_sme && !drv->use_monitor)
    8065         [ -  + ]:        444 :                 if (nl80211_mgmt_subscribe_ap(bss))
    8066                 :          0 :                         return -1;
    8067                 :            : 
    8068 [ -  + ][ #  # ]:        446 :         if (drv->device_ap_sme && !drv->use_monitor)
    8069         [ #  # ]:          0 :                 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
    8070                 :          0 :                         return -1;
    8071                 :            : 
    8072         [ +  - ]:        448 :         if (!drv->device_ap_sme && drv->use_monitor &&
           [ +  +  -  + ]
    8073         [ #  # ]:          2 :             nl80211_create_monitor_interface(drv) &&
    8074                 :          0 :             !drv->device_ap_sme)
    8075                 :          0 :                 return -1;
    8076                 :            : 
    8077   [ -  +  #  # ]:        446 :         if (drv->device_ap_sme &&
    8078                 :          0 :             wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
    8079                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
    8080                 :            :                            "Probe Request frame reporting in AP mode");
    8081                 :            :                 /* Try to survive without this */
    8082                 :            :         }
    8083                 :            : 
    8084                 :        446 :         return 0;
    8085                 :            : }
    8086                 :            : 
    8087                 :            : 
    8088                 :        436 : static void nl80211_teardown_ap(struct i802_bss *bss)
    8089                 :            : {
    8090                 :        436 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8091                 :            : 
    8092                 :        436 :         wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
    8093                 :       1308 :                    bss->ifname, drv->device_ap_sme, drv->use_monitor);
    8094         [ -  + ]:        436 :         if (drv->device_ap_sme) {
    8095                 :          0 :                 wpa_driver_nl80211_probe_req_report(bss, 0);
    8096         [ #  # ]:          0 :                 if (!drv->use_monitor)
    8097                 :          0 :                         nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
    8098         [ +  + ]:        436 :         } else if (drv->use_monitor)
    8099                 :          2 :                 nl80211_remove_monitor_interface(drv);
    8100                 :            :         else
    8101                 :        434 :                 nl80211_mgmt_unsubscribe(bss, "AP teardown");
    8102                 :            : 
    8103                 :        436 :         bss->beacon_set = 0;
    8104                 :        436 : }
    8105                 :            : 
    8106                 :            : 
    8107                 :       3105 : static int nl80211_send_eapol_data(struct i802_bss *bss,
    8108                 :            :                                    const u8 *addr, const u8 *data,
    8109                 :            :                                    size_t data_len)
    8110                 :            : {
    8111                 :            :         struct sockaddr_ll ll;
    8112                 :            :         int ret;
    8113                 :            : 
    8114         [ -  + ]:       3105 :         if (bss->drv->eapol_tx_sock < 0) {
    8115                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
    8116                 :          0 :                 return -1;
    8117                 :            :         }
    8118                 :            : 
    8119                 :       3105 :         os_memset(&ll, 0, sizeof(ll));
    8120                 :       3105 :         ll.sll_family = AF_PACKET;
    8121                 :       3105 :         ll.sll_ifindex = bss->ifindex;
    8122                 :       3105 :         ll.sll_protocol = htons(ETH_P_PAE);
    8123                 :       3105 :         ll.sll_halen = ETH_ALEN;
    8124                 :       3105 :         os_memcpy(ll.sll_addr, addr, ETH_ALEN);
    8125                 :       3105 :         ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
    8126                 :            :                      (struct sockaddr *) &ll, sizeof(ll));
    8127         [ -  + ]:       3105 :         if (ret < 0)
    8128                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
    8129                 :          0 :                            strerror(errno));
    8130                 :            : 
    8131                 :       3105 :         return ret;
    8132                 :            : }
    8133                 :            : 
    8134                 :            : 
    8135                 :            : static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
    8136                 :            : 
    8137                 :       3107 : static int wpa_driver_nl80211_hapd_send_eapol(
    8138                 :            :         void *priv, const u8 *addr, const u8 *data,
    8139                 :            :         size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
    8140                 :            : {
    8141                 :       3107 :         struct i802_bss *bss = priv;
    8142                 :       3107 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8143                 :            :         struct ieee80211_hdr *hdr;
    8144                 :            :         size_t len;
    8145                 :            :         u8 *pos;
    8146                 :            :         int res;
    8147                 :       3107 :         int qos = flags & WPA_STA_WMM;
    8148                 :            : 
    8149 [ +  - ][ +  + ]:       3107 :         if (drv->device_ap_sme || !drv->use_monitor)
    8150                 :       3105 :                 return nl80211_send_eapol_data(bss, addr, data, data_len);
    8151                 :            : 
    8152         [ +  - ]:          2 :         len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
    8153                 :            :                 data_len;
    8154                 :          2 :         hdr = os_zalloc(len);
    8155         [ -  + ]:          2 :         if (hdr == NULL) {
    8156                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
    8157                 :            :                            (unsigned long) len);
    8158                 :          0 :                 return -1;
    8159                 :            :         }
    8160                 :            : 
    8161                 :          2 :         hdr->frame_control =
    8162                 :            :                 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
    8163                 :          2 :         hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
    8164         [ -  + ]:          2 :         if (encrypt)
    8165                 :          0 :                 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
    8166         [ +  - ]:          2 :         if (qos) {
    8167                 :          2 :                 hdr->frame_control |=
    8168                 :            :                         host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
    8169                 :            :         }
    8170                 :            : 
    8171                 :          2 :         memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
    8172                 :          2 :         memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
    8173                 :          2 :         memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
    8174                 :          2 :         pos = (u8 *) (hdr + 1);
    8175                 :            : 
    8176         [ +  - ]:          2 :         if (qos) {
    8177                 :            :                 /* Set highest priority in QoS header */
    8178                 :          2 :                 pos[0] = 7;
    8179                 :          2 :                 pos[1] = 0;
    8180                 :          2 :                 pos += 2;
    8181                 :            :         }
    8182                 :            : 
    8183                 :          2 :         memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
    8184                 :          2 :         pos += sizeof(rfc1042_header);
    8185                 :          2 :         WPA_PUT_BE16(pos, ETH_P_PAE);
    8186                 :          2 :         pos += 2;
    8187                 :          2 :         memcpy(pos, data, data_len);
    8188                 :            : 
    8189                 :          2 :         res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
    8190                 :            :                                             0, 0, 0, 0);
    8191         [ -  + ]:          2 :         if (res < 0) {
    8192                 :          0 :                 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
    8193                 :            :                            "failed: %d (%s)",
    8194                 :          0 :                            (unsigned long) len, errno, strerror(errno));
    8195                 :            :         }
    8196                 :          2 :         os_free(hdr);
    8197                 :            : 
    8198                 :       3107 :         return res;
    8199                 :            : }
    8200                 :            : 
    8201                 :            : 
    8202                 :       1971 : static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
    8203                 :            :                                             int total_flags,
    8204                 :            :                                             int flags_or, int flags_and)
    8205                 :            : {
    8206                 :       1971 :         struct i802_bss *bss = priv;
    8207                 :       1971 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8208                 :            :         struct nl_msg *msg;
    8209                 :            :         struct nlattr *flags;
    8210                 :            :         struct nl80211_sta_flag_update upd;
    8211                 :            : 
    8212                 :       1971 :         msg = nlmsg_alloc();
    8213         [ -  + ]:       1971 :         if (!msg)
    8214                 :          0 :                 return -ENOMEM;
    8215                 :            : 
    8216                 :       1971 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
    8217                 :            : 
    8218         [ +  - ]:       1971 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
    8219                 :            :                     if_nametoindex(bss->ifname));
    8220         [ +  - ]:       1971 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    8221                 :            : 
    8222                 :            :         /*
    8223                 :            :          * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
    8224                 :            :          * can be removed eventually.
    8225                 :            :          */
    8226                 :       1971 :         flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
    8227         [ -  + ]:       1971 :         if (!flags)
    8228                 :          0 :                 goto nla_put_failure;
    8229         [ +  + ]:       1971 :         if (total_flags & WPA_STA_AUTHORIZED)
    8230         [ +  - ]:        533 :                 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
    8231                 :            : 
    8232         [ +  + ]:       1971 :         if (total_flags & WPA_STA_WMM)
    8233         [ +  - ]:       1967 :                 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
    8234                 :            : 
    8235         [ +  + ]:       1971 :         if (total_flags & WPA_STA_SHORT_PREAMBLE)
    8236         [ +  - ]:       1967 :                 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
    8237                 :            : 
    8238         [ +  + ]:       1971 :         if (total_flags & WPA_STA_MFP)
    8239         [ +  - ]:         60 :                 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
    8240                 :            : 
    8241         [ -  + ]:       1971 :         if (total_flags & WPA_STA_TDLS_PEER)
    8242         [ #  # ]:          0 :                 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
    8243                 :            : 
    8244                 :       1971 :         nla_nest_end(msg, flags);
    8245                 :            : 
    8246                 :       1971 :         os_memset(&upd, 0, sizeof(upd));
    8247                 :       1971 :         upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
    8248                 :       1971 :         upd.set = sta_flags_nl80211(flags_or);
    8249         [ +  - ]:       1971 :         NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
    8250                 :            : 
    8251                 :       1971 :         return send_and_recv_msgs(drv, msg, NULL, NULL);
    8252                 :            :  nla_put_failure:
    8253                 :          0 :         nlmsg_free(msg);
    8254                 :       1971 :         return -ENOBUFS;
    8255                 :            : }
    8256                 :            : 
    8257                 :            : 
    8258                 :         87 : static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
    8259                 :            :                                  struct wpa_driver_associate_params *params)
    8260                 :            : {
    8261                 :            :         enum nl80211_iftype nlmode, old_mode;
    8262                 :        174 :         struct hostapd_freq_params freq = {
    8263                 :         87 :                 .freq = params->freq,
    8264                 :            :         };
    8265                 :            : 
    8266         [ +  + ]:         87 :         if (params->p2p) {
    8267                 :         80 :                 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
    8268                 :            :                            "group (GO)");
    8269                 :         80 :                 nlmode = NL80211_IFTYPE_P2P_GO;
    8270                 :            :         } else
    8271                 :          7 :                 nlmode = NL80211_IFTYPE_AP;
    8272                 :            : 
    8273                 :         87 :         old_mode = drv->nlmode;
    8274         [ -  + ]:         87 :         if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
    8275                 :          0 :                 nl80211_remove_monitor_interface(drv);
    8276                 :          0 :                 return -1;
    8277                 :            :         }
    8278                 :            : 
    8279         [ +  + ]:         87 :         if (wpa_driver_nl80211_set_freq(drv->first_bss, &freq)) {
    8280         [ +  - ]:          1 :                 if (old_mode != nlmode)
    8281                 :          1 :                         wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
    8282                 :          1 :                 nl80211_remove_monitor_interface(drv);
    8283                 :          1 :                 return -1;
    8284                 :            :         }
    8285                 :            : 
    8286                 :         87 :         return 0;
    8287                 :            : }
    8288                 :            : 
    8289                 :            : 
    8290                 :          5 : static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
    8291                 :            : {
    8292                 :            :         struct nl_msg *msg;
    8293                 :          5 :         int ret = -1;
    8294                 :            : 
    8295                 :          5 :         msg = nlmsg_alloc();
    8296         [ -  + ]:          5 :         if (!msg)
    8297                 :          0 :                 return -1;
    8298                 :            : 
    8299                 :          5 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
    8300         [ +  - ]:          5 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    8301                 :          5 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    8302                 :          5 :         msg = NULL;
    8303         [ -  + ]:          5 :         if (ret) {
    8304                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
    8305                 :            :                            "(%s)", ret, strerror(-ret));
    8306                 :          0 :                 goto nla_put_failure;
    8307                 :            :         }
    8308                 :            : 
    8309                 :          5 :         ret = 0;
    8310                 :          5 :         wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
    8311                 :            : 
    8312                 :            : nla_put_failure:
    8313         [ -  + ]:          5 :         if (wpa_driver_nl80211_set_mode(drv->first_bss,
    8314                 :            :                                         NL80211_IFTYPE_STATION)) {
    8315                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
    8316                 :            :                            "station mode");
    8317                 :            :         }
    8318                 :            : 
    8319                 :          5 :         nlmsg_free(msg);
    8320                 :          5 :         return ret;
    8321                 :            : }
    8322                 :            : 
    8323                 :            : 
    8324                 :          5 : static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
    8325                 :            :                                    struct wpa_driver_associate_params *params)
    8326                 :            : {
    8327                 :            :         struct nl_msg *msg;
    8328                 :          5 :         int ret = -1;
    8329                 :          5 :         int count = 0;
    8330                 :            : 
    8331                 :          5 :         wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
    8332                 :            : 
    8333         [ -  + ]:          5 :         if (wpa_driver_nl80211_set_mode(drv->first_bss,
    8334                 :            :                                         NL80211_IFTYPE_ADHOC)) {
    8335                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
    8336                 :            :                            "IBSS mode");
    8337                 :          0 :                 return -1;
    8338                 :            :         }
    8339                 :            : 
    8340                 :            : retry:
    8341                 :          5 :         msg = nlmsg_alloc();
    8342         [ -  + ]:          5 :         if (!msg)
    8343                 :          0 :                 return -1;
    8344                 :            : 
    8345                 :          5 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
    8346         [ +  - ]:          5 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    8347                 :            : 
    8348 [ +  - ][ +  - ]:          5 :         if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
    8349                 :            :                 goto nla_put_failure;
    8350                 :            : 
    8351                 :          5 :         wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
    8352                 :          5 :                           params->ssid, params->ssid_len);
    8353         [ +  - ]:          5 :         NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
    8354                 :            :                 params->ssid);
    8355                 :          5 :         os_memcpy(drv->ssid, params->ssid, params->ssid_len);
    8356                 :          5 :         drv->ssid_len = params->ssid_len;
    8357                 :            : 
    8358                 :          5 :         wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
    8359         [ +  - ]:          5 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
    8360                 :            : 
    8361                 :          5 :         ret = nl80211_set_conn_keys(params, msg);
    8362         [ -  + ]:          5 :         if (ret)
    8363                 :          0 :                 goto nla_put_failure;
    8364                 :            : 
    8365 [ -  + ][ #  # ]:          5 :         if (params->bssid && params->fixed_bssid) {
    8366                 :          0 :                 wpa_printf(MSG_DEBUG, "  * BSSID=" MACSTR,
    8367                 :          0 :                            MAC2STR(params->bssid));
    8368         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
    8369                 :            :         }
    8370                 :            : 
    8371 [ +  - ][ +  + ]:          5 :         if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
    8372         [ +  - ]:          3 :             params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
    8373         [ -  + ]:          3 :             params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
    8374                 :          3 :             params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
    8375                 :          2 :                 wpa_printf(MSG_DEBUG, "  * control port");
    8376         [ +  - ]:          2 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
    8377                 :            :         }
    8378                 :            : 
    8379         [ +  - ]:          5 :         if (params->wpa_ie) {
    8380                 :          5 :                 wpa_hexdump(MSG_DEBUG,
    8381                 :            :                             "  * Extra IEs for Beacon/Probe Response frames",
    8382                 :          5 :                             params->wpa_ie, params->wpa_ie_len);
    8383         [ +  - ]:          5 :                 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
    8384                 :            :                         params->wpa_ie);
    8385                 :            :         }
    8386                 :            : 
    8387                 :          5 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    8388                 :          5 :         msg = NULL;
    8389         [ -  + ]:          5 :         if (ret) {
    8390                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
    8391                 :            :                            ret, strerror(-ret));
    8392                 :          0 :                 count++;
    8393 [ #  # ][ #  # ]:          0 :                 if (ret == -EALREADY && count == 1) {
    8394                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
    8395                 :            :                                    "forced leave");
    8396                 :          0 :                         nl80211_leave_ibss(drv);
    8397                 :          0 :                         nlmsg_free(msg);
    8398                 :          0 :                         goto retry;
    8399                 :            :                 }
    8400                 :            : 
    8401                 :          0 :                 goto nla_put_failure;
    8402                 :            :         }
    8403                 :          5 :         ret = 0;
    8404                 :          5 :         wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
    8405                 :            : 
    8406                 :            : nla_put_failure:
    8407                 :          5 :         nlmsg_free(msg);
    8408                 :          5 :         return ret;
    8409                 :            : }
    8410                 :            : 
    8411                 :            : 
    8412                 :        674 : static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
    8413                 :            :                                   struct wpa_driver_associate_params *params,
    8414                 :            :                                   struct nl_msg *msg)
    8415                 :            : {
    8416         [ +  - ]:        674 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    8417                 :            : 
    8418         [ +  - ]:        674 :         if (params->bssid) {
    8419                 :        674 :                 wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
    8420                 :       4044 :                            MAC2STR(params->bssid));
    8421         [ +  - ]:        674 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
    8422                 :            :         }
    8423                 :            : 
    8424         [ +  + ]:        674 :         if (params->bssid_hint) {
    8425                 :          4 :                 wpa_printf(MSG_DEBUG, "  * bssid_hint=" MACSTR,
    8426                 :         24 :                            MAC2STR(params->bssid_hint));
    8427         [ +  - ]:          4 :                 NLA_PUT(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
    8428                 :            :                         params->bssid_hint);
    8429                 :            :         }
    8430                 :            : 
    8431         [ +  - ]:        674 :         if (params->freq) {
    8432                 :        674 :                 wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
    8433         [ +  - ]:        674 :                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
    8434                 :        674 :                 drv->assoc_freq = params->freq;
    8435                 :            :         } else
    8436                 :          0 :                 drv->assoc_freq = 0;
    8437                 :            : 
    8438         [ +  + ]:        674 :         if (params->freq_hint) {
    8439                 :          4 :                 wpa_printf(MSG_DEBUG, "  * freq_hint=%d", params->freq_hint);
    8440         [ +  - ]:          4 :                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
    8441                 :            :                             params->freq_hint);
    8442                 :            :         }
    8443                 :            : 
    8444         [ -  + ]:        674 :         if (params->bg_scan_period >= 0) {
    8445                 :          0 :                 wpa_printf(MSG_DEBUG, "  * bg scan period=%d",
    8446                 :            :                            params->bg_scan_period);
    8447         [ #  # ]:          0 :                 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
    8448                 :            :                             params->bg_scan_period);
    8449                 :            :         }
    8450                 :            : 
    8451         [ +  - ]:        674 :         if (params->ssid) {
    8452                 :        674 :                 wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
    8453                 :        674 :                                   params->ssid, params->ssid_len);
    8454         [ +  - ]:        674 :                 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
    8455                 :            :                         params->ssid);
    8456         [ -  + ]:        674 :                 if (params->ssid_len > sizeof(drv->ssid))
    8457                 :          0 :                         goto nla_put_failure;
    8458                 :        674 :                 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
    8459                 :        674 :                 drv->ssid_len = params->ssid_len;
    8460                 :            :         }
    8461                 :            : 
    8462                 :        674 :         wpa_hexdump(MSG_DEBUG, "  * IEs", params->wpa_ie, params->wpa_ie_len);
    8463         [ +  - ]:        674 :         if (params->wpa_ie)
    8464         [ +  - ]:        674 :                 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
    8465                 :            :                         params->wpa_ie);
    8466                 :            : 
    8467         [ +  + ]:        674 :         if (params->wpa_proto) {
    8468                 :        427 :                 enum nl80211_wpa_versions ver = 0;
    8469                 :            : 
    8470         [ +  + ]:        427 :                 if (params->wpa_proto & WPA_PROTO_WPA)
    8471                 :          7 :                         ver |= NL80211_WPA_VERSION_1;
    8472         [ +  + ]:        427 :                 if (params->wpa_proto & WPA_PROTO_RSN)
    8473                 :        419 :                         ver |= NL80211_WPA_VERSION_2;
    8474                 :            : 
    8475                 :        427 :                 wpa_printf(MSG_DEBUG, "  * WPA Versions 0x%x", ver);
    8476         [ +  - ]:        427 :                 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
    8477                 :            :         }
    8478                 :            : 
    8479         [ +  + ]:        674 :         if (params->pairwise_suite != WPA_CIPHER_NONE) {
    8480                 :        433 :                 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
    8481                 :        433 :                 wpa_printf(MSG_DEBUG, "  * pairwise=0x%x", cipher);
    8482         [ +  - ]:        433 :                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
    8483                 :            :         }
    8484                 :            : 
    8485 [ +  + ][ +  - ]:        674 :         if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
    8486                 :          1 :             !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
    8487                 :            :                 /*
    8488                 :            :                  * This is likely to work even though many drivers do not
    8489                 :            :                  * advertise support for operations without GTK.
    8490                 :            :                  */
    8491                 :          1 :                 wpa_printf(MSG_DEBUG, "  * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
    8492         [ +  + ]:        673 :         } else if (params->group_suite != WPA_CIPHER_NONE) {
    8493                 :        432 :                 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
    8494                 :        432 :                 wpa_printf(MSG_DEBUG, "  * group=0x%x", cipher);
    8495         [ +  - ]:        432 :                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
    8496                 :            :         }
    8497                 :            : 
    8498 [ +  - ][ +  + ]:        674 :         if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
    8499         [ +  - ]:        673 :             params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
    8500         [ +  - ]:        673 :             params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
    8501         [ -  + ]:        673 :             params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
    8502                 :        673 :             params->key_mgmt_suite == WPA_KEY_MGMT_CCKM) {
    8503                 :          1 :                 int mgmt = WLAN_AKM_SUITE_PSK;
    8504                 :            : 
    8505   [ -  -  -  -  :          1 :                 switch (params->key_mgmt_suite) {
                      + ]
    8506                 :            :                 case WPA_KEY_MGMT_CCKM:
    8507                 :          0 :                         mgmt = WLAN_AKM_SUITE_CCKM;
    8508                 :          0 :                         break;
    8509                 :            :                 case WPA_KEY_MGMT_IEEE8021X:
    8510                 :          0 :                         mgmt = WLAN_AKM_SUITE_8021X;
    8511                 :          0 :                         break;
    8512                 :            :                 case WPA_KEY_MGMT_FT_IEEE8021X:
    8513                 :          0 :                         mgmt = WLAN_AKM_SUITE_FT_8021X;
    8514                 :          0 :                         break;
    8515                 :            :                 case WPA_KEY_MGMT_FT_PSK:
    8516                 :          0 :                         mgmt = WLAN_AKM_SUITE_FT_PSK;
    8517                 :          0 :                         break;
    8518                 :            :                 case WPA_KEY_MGMT_PSK:
    8519                 :            :                 default:
    8520                 :          1 :                         mgmt = WLAN_AKM_SUITE_PSK;
    8521                 :          1 :                         break;
    8522                 :            :                 }
    8523         [ +  - ]:          1 :                 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
    8524                 :            :         }
    8525                 :            : 
    8526         [ +  - ]:        674 :         NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
    8527                 :            : 
    8528         [ +  + ]:        674 :         if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
    8529         [ +  - ]:         19 :                 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
    8530                 :            : 
    8531         [ -  + ]:        674 :         if (params->disable_ht)
    8532         [ #  # ]:          0 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
    8533                 :            : 
    8534 [ -  + ][ #  # ]:        674 :         if (params->htcaps && params->htcaps_mask) {
    8535                 :          0 :                 int sz = sizeof(struct ieee80211_ht_capabilities);
    8536         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
    8537         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
    8538                 :            :                         params->htcaps_mask);
    8539                 :            :         }
    8540                 :            : 
    8541                 :            : #ifdef CONFIG_VHT_OVERRIDES
    8542                 :            :         if (params->disable_vht) {
    8543                 :            :                 wpa_printf(MSG_DEBUG, "  * VHT disabled");
    8544                 :            :                 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
    8545                 :            :         }
    8546                 :            : 
    8547                 :            :         if (params->vhtcaps && params->vhtcaps_mask) {
    8548                 :            :                 int sz = sizeof(struct ieee80211_vht_capabilities);
    8549                 :            :                 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
    8550                 :            :                 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
    8551                 :            :                         params->vhtcaps_mask);
    8552                 :            :         }
    8553                 :            : #endif /* CONFIG_VHT_OVERRIDES */
    8554                 :            : 
    8555         [ +  + ]:        674 :         if (params->p2p)
    8556                 :        167 :                 wpa_printf(MSG_DEBUG, "  * P2P group");
    8557                 :            : 
    8558                 :        674 :         return 0;
    8559                 :            : nla_put_failure:
    8560                 :        674 :         return -1;
    8561                 :            : }
    8562                 :            : 
    8563                 :            : 
    8564                 :          4 : static int wpa_driver_nl80211_try_connect(
    8565                 :            :         struct wpa_driver_nl80211_data *drv,
    8566                 :            :         struct wpa_driver_associate_params *params)
    8567                 :            : {
    8568                 :            :         struct nl_msg *msg;
    8569                 :            :         enum nl80211_auth_type type;
    8570                 :            :         int ret;
    8571                 :            :         int algs;
    8572                 :            : 
    8573                 :          4 :         msg = nlmsg_alloc();
    8574         [ -  + ]:          4 :         if (!msg)
    8575                 :          0 :                 return -1;
    8576                 :            : 
    8577                 :          4 :         wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
    8578                 :          4 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
    8579                 :            : 
    8580                 :          4 :         ret = nl80211_connect_common(drv, params, msg);
    8581         [ -  + ]:          4 :         if (ret)
    8582                 :          0 :                 goto nla_put_failure;
    8583                 :            : 
    8584                 :          4 :         algs = 0;
    8585         [ +  - ]:          4 :         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
    8586                 :          4 :                 algs++;
    8587         [ -  + ]:          4 :         if (params->auth_alg & WPA_AUTH_ALG_SHARED)
    8588                 :          0 :                 algs++;
    8589         [ -  + ]:          4 :         if (params->auth_alg & WPA_AUTH_ALG_LEAP)
    8590                 :          0 :                 algs++;
    8591         [ -  + ]:          4 :         if (algs > 1) {
    8592                 :          0 :                 wpa_printf(MSG_DEBUG, "  * Leave out Auth Type for automatic "
    8593                 :            :                            "selection");
    8594                 :          0 :                 goto skip_auth_type;
    8595                 :            :         }
    8596                 :            : 
    8597         [ +  - ]:          4 :         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
    8598                 :          4 :                 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
    8599         [ #  # ]:          0 :         else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
    8600                 :          0 :                 type = NL80211_AUTHTYPE_SHARED_KEY;
    8601         [ #  # ]:          0 :         else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
    8602                 :          0 :                 type = NL80211_AUTHTYPE_NETWORK_EAP;
    8603         [ #  # ]:          0 :         else if (params->auth_alg & WPA_AUTH_ALG_FT)
    8604                 :          0 :                 type = NL80211_AUTHTYPE_FT;
    8605                 :            :         else
    8606                 :          0 :                 goto nla_put_failure;
    8607                 :            : 
    8608                 :          4 :         wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
    8609         [ +  - ]:          4 :         NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
    8610                 :            : 
    8611                 :            : skip_auth_type:
    8612                 :          4 :         ret = nl80211_set_conn_keys(params, msg);
    8613         [ -  + ]:          4 :         if (ret)
    8614                 :          0 :                 goto nla_put_failure;
    8615                 :            : 
    8616                 :          4 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    8617                 :          4 :         msg = NULL;
    8618         [ -  + ]:          4 :         if (ret) {
    8619                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
    8620                 :            :                            "(%s)", ret, strerror(-ret));
    8621                 :          0 :                 goto nla_put_failure;
    8622                 :            :         }
    8623                 :          4 :         ret = 0;
    8624                 :          4 :         wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
    8625                 :            : 
    8626                 :            : nla_put_failure:
    8627                 :          4 :         nlmsg_free(msg);
    8628                 :          4 :         return ret;
    8629                 :            : 
    8630                 :            : }
    8631                 :            : 
    8632                 :            : 
    8633                 :          4 : static int wpa_driver_nl80211_connect(
    8634                 :            :         struct wpa_driver_nl80211_data *drv,
    8635                 :            :         struct wpa_driver_associate_params *params)
    8636                 :            : {
    8637                 :          4 :         int ret = wpa_driver_nl80211_try_connect(drv, params);
    8638         [ -  + ]:          4 :         if (ret == -EALREADY) {
    8639                 :            :                 /*
    8640                 :            :                  * cfg80211 does not currently accept new connections if
    8641                 :            :                  * we are already connected. As a workaround, force
    8642                 :            :                  * disconnection and try again.
    8643                 :            :                  */
    8644                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
    8645                 :            :                            "disconnecting before reassociation "
    8646                 :            :                            "attempt");
    8647         [ #  # ]:          0 :                 if (wpa_driver_nl80211_disconnect(
    8648                 :            :                             drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
    8649                 :          0 :                         return -1;
    8650                 :          0 :                 ret = wpa_driver_nl80211_try_connect(drv, params);
    8651                 :            :         }
    8652                 :          4 :         return ret;
    8653                 :            : }
    8654                 :            : 
    8655                 :            : 
    8656                 :        766 : static int wpa_driver_nl80211_associate(
    8657                 :            :         void *priv, struct wpa_driver_associate_params *params)
    8658                 :            : {
    8659                 :        766 :         struct i802_bss *bss = priv;
    8660                 :        766 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8661                 :            :         int ret;
    8662                 :            :         struct nl_msg *msg;
    8663                 :            : 
    8664         [ +  + ]:        766 :         if (params->mode == IEEE80211_MODE_AP)
    8665                 :         87 :                 return wpa_driver_nl80211_ap(drv, params);
    8666                 :            : 
    8667         [ +  + ]:        679 :         if (params->mode == IEEE80211_MODE_IBSS)
    8668                 :          5 :                 return wpa_driver_nl80211_ibss(drv, params);
    8669                 :            : 
    8670         [ +  + ]:        674 :         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
    8671         [ -  + ]:          4 :                 enum nl80211_iftype nlmode = params->p2p ?
    8672                 :            :                         NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
    8673                 :            : 
    8674         [ -  + ]:          4 :                 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
    8675                 :          0 :                         return -1;
    8676                 :          4 :                 return wpa_driver_nl80211_connect(drv, params);
    8677                 :            :         }
    8678                 :            : 
    8679                 :        670 :         nl80211_mark_disconnected(drv);
    8680                 :            : 
    8681                 :        670 :         msg = nlmsg_alloc();
    8682         [ -  + ]:        670 :         if (!msg)
    8683                 :          0 :                 return -1;
    8684                 :            : 
    8685                 :        670 :         wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
    8686                 :            :                    drv->ifindex);
    8687                 :        670 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
    8688                 :            : 
    8689                 :        670 :         ret = nl80211_connect_common(drv, params, msg);
    8690         [ -  + ]:        670 :         if (ret)
    8691                 :          0 :                 goto nla_put_failure;
    8692                 :            : 
    8693         [ +  + ]:        670 :         if (params->prev_bssid) {
    8694                 :         26 :                 wpa_printf(MSG_DEBUG, "  * prev_bssid=" MACSTR,
    8695                 :        156 :                            MAC2STR(params->prev_bssid));
    8696         [ +  - ]:         26 :                 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
    8697                 :            :                         params->prev_bssid);
    8698                 :            :         }
    8699                 :            : 
    8700                 :        670 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    8701                 :        670 :         msg = NULL;
    8702         [ -  + ]:        670 :         if (ret) {
    8703                 :          0 :                 wpa_dbg(drv->ctx, MSG_DEBUG,
    8704                 :            :                         "nl80211: MLME command failed (assoc): ret=%d (%s)",
    8705                 :            :                         ret, strerror(-ret));
    8706                 :          0 :                 nl80211_dump_scan(drv);
    8707                 :          0 :                 goto nla_put_failure;
    8708                 :            :         }
    8709                 :        670 :         ret = 0;
    8710                 :        670 :         wpa_printf(MSG_DEBUG, "nl80211: Association request send "
    8711                 :            :                    "successfully");
    8712                 :            : 
    8713                 :            : nla_put_failure:
    8714                 :        670 :         nlmsg_free(msg);
    8715                 :        766 :         return ret;
    8716                 :            : }
    8717                 :            : 
    8718                 :            : 
    8719                 :       1071 : static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
    8720                 :            :                             int ifindex, enum nl80211_iftype mode)
    8721                 :            : {
    8722                 :            :         struct nl_msg *msg;
    8723                 :       1071 :         int ret = -ENOBUFS;
    8724                 :            : 
    8725                 :       1071 :         wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
    8726                 :            :                    ifindex, mode, nl80211_iftype_str(mode));
    8727                 :            : 
    8728                 :       1071 :         msg = nlmsg_alloc();
    8729         [ -  + ]:       1071 :         if (!msg)
    8730                 :          0 :                 return -ENOMEM;
    8731                 :            : 
    8732                 :       1071 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
    8733         [ -  + ]:       1071 :         if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
    8734                 :          0 :                 goto nla_put_failure;
    8735         [ +  - ]:       1071 :         NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
    8736                 :            : 
    8737                 :       1071 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    8738                 :       1071 :         msg = NULL;
    8739         [ +  - ]:       1071 :         if (!ret)
    8740                 :       1071 :                 return 0;
    8741                 :            : nla_put_failure:
    8742                 :          0 :         nlmsg_free(msg);
    8743                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
    8744                 :            :                    " %d (%s)", ifindex, mode, ret, strerror(-ret));
    8745                 :       1071 :         return ret;
    8746                 :            : }
    8747                 :            : 
    8748                 :            : 
    8749                 :       1071 : static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
    8750                 :            :                                        enum nl80211_iftype nlmode)
    8751                 :            : {
    8752                 :       1071 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8753                 :       1071 :         int ret = -1;
    8754                 :            :         int i;
    8755                 :       1071 :         int was_ap = is_ap_interface(drv->nlmode);
    8756                 :            :         int res;
    8757                 :            : 
    8758                 :       1071 :         res = nl80211_set_mode(drv, drv->ifindex, nlmode);
    8759 [ #  # ][ -  + ]:       1071 :         if (res && nlmode == nl80211_get_ifmode(bss))
    8760                 :          0 :                 res = 0;
    8761                 :            : 
    8762         [ +  - ]:       1071 :         if (res == 0) {
    8763                 :       1071 :                 drv->nlmode = nlmode;
    8764                 :       1071 :                 ret = 0;
    8765                 :       1071 :                 goto done;
    8766                 :            :         }
    8767                 :            : 
    8768         [ #  # ]:          0 :         if (res == -ENODEV)
    8769                 :          0 :                 return -1;
    8770                 :            : 
    8771         [ #  # ]:          0 :         if (nlmode == drv->nlmode) {
    8772                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
    8773                 :            :                            "requested mode - ignore error");
    8774                 :          0 :                 ret = 0;
    8775                 :          0 :                 goto done; /* Already in the requested mode */
    8776                 :            :         }
    8777                 :            : 
    8778                 :            :         /* mac80211 doesn't allow mode changes while the device is up, so
    8779                 :            :          * take the device down, try to set the mode again, and bring the
    8780                 :            :          * device back up.
    8781                 :            :          */
    8782                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
    8783                 :            :                    "interface down");
    8784         [ #  # ]:          0 :         for (i = 0; i < 10; i++) {
    8785                 :          0 :                 res = i802_set_iface_flags(bss, 0);
    8786 [ #  # ][ #  # ]:          0 :                 if (res == -EACCES || res == -ENODEV)
    8787                 :            :                         break;
    8788         [ #  # ]:          0 :                 if (res == 0) {
    8789                 :            :                         /* Try to set the mode again while the interface is
    8790                 :            :                          * down */
    8791                 :          0 :                         ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
    8792         [ #  # ]:          0 :                         if (ret == -EACCES)
    8793                 :          0 :                                 break;
    8794                 :          0 :                         res = i802_set_iface_flags(bss, 1);
    8795 [ #  # ][ #  # ]:          0 :                         if (res && !ret)
    8796                 :          0 :                                 ret = -1;
    8797         [ #  # ]:          0 :                         else if (ret != -EBUSY)
    8798                 :          0 :                                 break;
    8799                 :            :                 } else
    8800                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
    8801                 :            :                                    "interface down");
    8802                 :          0 :                 os_sleep(0, 100000);
    8803                 :            :         }
    8804                 :            : 
    8805         [ #  # ]:          0 :         if (!ret) {
    8806                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
    8807                 :            :                            "interface is down");
    8808                 :          0 :                 drv->nlmode = nlmode;
    8809                 :          0 :                 drv->ignore_if_down_event = 1;
    8810                 :            :         }
    8811                 :            : 
    8812                 :            : done:
    8813         [ -  + ]:       1071 :         if (ret) {
    8814                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
    8815                 :          0 :                            "from %d failed", nlmode, drv->nlmode);
    8816                 :          0 :                 return ret;
    8817                 :            :         }
    8818                 :            : 
    8819         [ +  + ]:       1071 :         if (is_p2p_net_interface(nlmode))
    8820                 :        181 :                 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
    8821         [ +  + ]:        890 :         else if (drv->disabled_11b_rates)
    8822                 :        144 :                 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
    8823                 :            : 
    8824         [ +  + ]:       1071 :         if (is_ap_interface(nlmode)) {
    8825                 :        429 :                 nl80211_mgmt_unsubscribe(bss, "start AP");
    8826                 :            :                 /* Setup additional AP mode functionality if needed */
    8827         [ -  + ]:        429 :                 if (nl80211_setup_ap(bss))
    8828                 :          0 :                         return -1;
    8829         [ +  + ]:        642 :         } else if (was_ap) {
    8830                 :            :                 /* Remove additional AP mode functionality */
    8831                 :        419 :                 nl80211_teardown_ap(bss);
    8832                 :            :         } else {
    8833                 :        223 :                 nl80211_mgmt_unsubscribe(bss, "mode change");
    8834                 :            :         }
    8835                 :            : 
    8836         [ +  + ]:       1342 :         if (!bss->in_deinit && !is_ap_interface(nlmode) &&
           [ +  +  -  + ]
    8837                 :        271 :             nl80211_mgmt_subscribe_non_ap(bss) < 0)
    8838                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
    8839                 :            :                            "frame processing - ignore for now");
    8840                 :            : 
    8841                 :       1071 :         return 0;
    8842                 :            : }
    8843                 :            : 
    8844                 :            : 
    8845                 :        502 : static int wpa_driver_nl80211_get_capa(void *priv,
    8846                 :            :                                        struct wpa_driver_capa *capa)
    8847                 :            : {
    8848                 :        502 :         struct i802_bss *bss = priv;
    8849                 :        502 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8850         [ -  + ]:        502 :         if (!drv->has_capability)
    8851                 :          0 :                 return -1;
    8852                 :        502 :         os_memcpy(capa, &drv->capa, sizeof(*capa));
    8853 [ +  - ][ +  - ]:        502 :         if (drv->extended_capa && drv->extended_capa_mask) {
    8854                 :        502 :                 capa->extended_capa = drv->extended_capa;
    8855                 :        502 :                 capa->extended_capa_mask = drv->extended_capa_mask;
    8856                 :        502 :                 capa->extended_capa_len = drv->extended_capa_len;
    8857                 :            :         }
    8858                 :            : 
    8859 [ +  - ][ +  - ]:        502 :         if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
    8860                 :        502 :             !drv->allow_p2p_device) {
    8861                 :        502 :                 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
    8862                 :        502 :                 capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
    8863                 :            :         }
    8864                 :            : 
    8865                 :        502 :         return 0;
    8866                 :            : }
    8867                 :            : 
    8868                 :            : 
    8869                 :       4530 : static int wpa_driver_nl80211_set_operstate(void *priv, int state)
    8870                 :            : {
    8871                 :       4530 :         struct i802_bss *bss = priv;
    8872                 :       4530 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8873                 :            : 
    8874         [ +  + ]:       4530 :         wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
    8875                 :       4530 :                    bss->ifname, drv->operstate, state,
    8876                 :            :                    state ? "UP" : "DORMANT");
    8877                 :       4530 :         drv->operstate = state;
    8878         [ +  + ]:       4530 :         return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
    8879                 :            :                                       state ? IF_OPER_UP : IF_OPER_DORMANT);
    8880                 :            : }
    8881                 :            : 
    8882                 :            : 
    8883                 :       1402 : static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
    8884                 :            : {
    8885                 :       1402 :         struct i802_bss *bss = priv;
    8886                 :       1402 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8887                 :            :         struct nl_msg *msg;
    8888                 :            :         struct nl80211_sta_flag_update upd;
    8889                 :       1402 :         int ret = -ENOBUFS;
    8890                 :            : 
    8891 [ +  + ][ +  - ]:       1402 :         if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
                 [ +  - ]
    8892                 :        692 :                 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
    8893                 :        692 :                 return 0;
    8894                 :            :         }
    8895                 :            : 
    8896         [ +  + ]:        710 :         wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
    8897                 :       4260 :                    MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
    8898                 :            : 
    8899                 :        710 :         msg = nlmsg_alloc();
    8900         [ -  + ]:        710 :         if (!msg)
    8901                 :          0 :                 return -ENOMEM;
    8902                 :            : 
    8903                 :        710 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
    8904                 :            : 
    8905         [ +  - ]:        710 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
    8906                 :            :                     if_nametoindex(bss->ifname));
    8907         [ +  - ]:        710 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
    8908                 :            : 
    8909                 :        710 :         os_memset(&upd, 0, sizeof(upd));
    8910                 :        710 :         upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
    8911         [ +  + ]:        710 :         if (authorized)
    8912                 :        503 :                 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
    8913         [ +  - ]:        710 :         NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
    8914                 :            : 
    8915                 :        710 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    8916                 :        710 :         msg = NULL;
    8917         [ +  + ]:        710 :         if (!ret)
    8918                 :        705 :                 return 0;
    8919                 :            :  nla_put_failure:
    8920                 :          5 :         nlmsg_free(msg);
    8921                 :          5 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
    8922                 :            :                    ret, strerror(-ret));
    8923                 :       1402 :         return ret;
    8924                 :            : }
    8925                 :            : 
    8926                 :            : 
    8927                 :            : /* Set kernel driver on given frequency (MHz) */
    8928                 :        411 : static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
    8929                 :            : {
    8930                 :        411 :         struct i802_bss *bss = priv;
    8931                 :        411 :         return wpa_driver_nl80211_set_freq(bss, freq);
    8932                 :            : }
    8933                 :            : 
    8934                 :            : 
    8935                 :        449 : static inline int min_int(int a, int b)
    8936                 :            : {
    8937         [ -  + ]:        449 :         if (a < b)
    8938                 :          0 :                 return a;
    8939                 :        449 :         return b;
    8940                 :            : }
    8941                 :            : 
    8942                 :            : 
    8943                 :        449 : static int get_key_handler(struct nl_msg *msg, void *arg)
    8944                 :            : {
    8945                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    8946                 :        449 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    8947                 :            : 
    8948                 :        449 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    8949                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    8950                 :            : 
    8951                 :            :         /*
    8952                 :            :          * TODO: validate the key index and mac address!
    8953                 :            :          * Otherwise, there's a race condition as soon as
    8954                 :            :          * the kernel starts sending key notifications.
    8955                 :            :          */
    8956                 :            : 
    8957         [ +  - ]:        449 :         if (tb[NL80211_ATTR_KEY_SEQ])
    8958                 :        449 :                 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
    8959                 :        449 :                        min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
    8960                 :        449 :         return NL_SKIP;
    8961                 :            : }
    8962                 :            : 
    8963                 :            : 
    8964                 :        449 : static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
    8965                 :            :                            int idx, u8 *seq)
    8966                 :            : {
    8967                 :        449 :         struct i802_bss *bss = priv;
    8968                 :        449 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8969                 :            :         struct nl_msg *msg;
    8970                 :            : 
    8971                 :        449 :         msg = nlmsg_alloc();
    8972         [ -  + ]:        449 :         if (!msg)
    8973                 :          0 :                 return -ENOMEM;
    8974                 :            : 
    8975                 :        449 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
    8976                 :            : 
    8977         [ -  + ]:        449 :         if (addr)
    8978         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    8979         [ +  - ]:        449 :         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
    8980         [ +  - ]:        449 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
    8981                 :            : 
    8982                 :        449 :         memset(seq, 0, 6);
    8983                 :            : 
    8984                 :        449 :         return send_and_recv_msgs(drv, msg, get_key_handler, seq);
    8985                 :            :  nla_put_failure:
    8986                 :          0 :         nlmsg_free(msg);
    8987                 :        449 :         return -ENOBUFS;
    8988                 :            : }
    8989                 :            : 
    8990                 :            : 
    8991                 :          1 : static int i802_set_rts(void *priv, int rts)
    8992                 :            : {
    8993                 :          1 :         struct i802_bss *bss = priv;
    8994                 :          1 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    8995                 :            :         struct nl_msg *msg;
    8996                 :          1 :         int ret = -ENOBUFS;
    8997                 :            :         u32 val;
    8998                 :            : 
    8999                 :          1 :         msg = nlmsg_alloc();
    9000         [ -  + ]:          1 :         if (!msg)
    9001                 :          0 :                 return -ENOMEM;
    9002                 :            : 
    9003         [ -  + ]:          1 :         if (rts >= 2347)
    9004                 :          0 :                 val = (u32) -1;
    9005                 :            :         else
    9006                 :          1 :                 val = rts;
    9007                 :            : 
    9008                 :          1 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
    9009         [ +  - ]:          1 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    9010         [ +  - ]:          1 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
    9011                 :            : 
    9012                 :          1 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    9013                 :          1 :         msg = NULL;
    9014         [ +  - ]:          1 :         if (!ret)
    9015                 :          1 :                 return 0;
    9016                 :            : nla_put_failure:
    9017                 :          0 :         nlmsg_free(msg);
    9018                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
    9019                 :            :                    "%d (%s)", rts, ret, strerror(-ret));
    9020                 :          1 :         return ret;
    9021                 :            : }
    9022                 :            : 
    9023                 :            : 
    9024                 :          3 : static int i802_set_frag(void *priv, int frag)
    9025                 :            : {
    9026                 :          3 :         struct i802_bss *bss = priv;
    9027                 :          3 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9028                 :            :         struct nl_msg *msg;
    9029                 :          3 :         int ret = -ENOBUFS;
    9030                 :            :         u32 val;
    9031                 :            : 
    9032                 :          3 :         msg = nlmsg_alloc();
    9033         [ -  + ]:          3 :         if (!msg)
    9034                 :          0 :                 return -ENOMEM;
    9035                 :            : 
    9036         [ -  + ]:          3 :         if (frag >= 2346)
    9037                 :          0 :                 val = (u32) -1;
    9038                 :            :         else
    9039                 :          3 :                 val = frag;
    9040                 :            : 
    9041                 :          3 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
    9042         [ +  - ]:          3 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
    9043         [ +  - ]:          3 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
    9044                 :            : 
    9045                 :          3 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    9046                 :          3 :         msg = NULL;
    9047         [ +  - ]:          3 :         if (!ret)
    9048                 :          3 :                 return 0;
    9049                 :            : nla_put_failure:
    9050                 :          0 :         nlmsg_free(msg);
    9051                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
    9052                 :            :                    "%d: %d (%s)", frag, ret, strerror(-ret));
    9053                 :          3 :         return ret;
    9054                 :            : }
    9055                 :            : 
    9056                 :            : 
    9057                 :        878 : static int i802_flush(void *priv)
    9058                 :            : {
    9059                 :        878 :         struct i802_bss *bss = priv;
    9060                 :        878 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9061                 :            :         struct nl_msg *msg;
    9062                 :            :         int res;
    9063                 :            : 
    9064                 :        878 :         msg = nlmsg_alloc();
    9065         [ -  + ]:        878 :         if (!msg)
    9066                 :          0 :                 return -1;
    9067                 :            : 
    9068                 :        878 :         wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
    9069                 :        878 :                    bss->ifname);
    9070                 :        878 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
    9071                 :            : 
    9072                 :            :         /*
    9073                 :            :          * XXX: FIX! this needs to flush all VLANs too
    9074                 :            :          */
    9075         [ +  - ]:        878 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
    9076                 :            :                     if_nametoindex(bss->ifname));
    9077                 :            : 
    9078                 :        878 :         res = send_and_recv_msgs(drv, msg, NULL, NULL);
    9079         [ -  + ]:        878 :         if (res) {
    9080                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
    9081                 :            :                            "(%s)", res, strerror(-res));
    9082                 :            :         }
    9083                 :        878 :         return res;
    9084                 :            :  nla_put_failure:
    9085                 :          0 :         nlmsg_free(msg);
    9086                 :        878 :         return -ENOBUFS;
    9087                 :            : }
    9088                 :            : 
    9089                 :            : 
    9090                 :         22 : static int get_sta_handler(struct nl_msg *msg, void *arg)
    9091                 :            : {
    9092                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    9093                 :         22 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    9094                 :         22 :         struct hostap_sta_driver_data *data = arg;
    9095                 :            :         struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
    9096                 :            :         static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
    9097                 :            :                 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
    9098                 :            :                 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
    9099                 :            :                 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
    9100                 :            :                 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
    9101                 :            :                 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
    9102                 :            :                 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
    9103                 :            :         };
    9104                 :            : 
    9105                 :         22 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    9106                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    9107                 :            : 
    9108                 :            :         /*
    9109                 :            :          * TODO: validate the interface and mac address!
    9110                 :            :          * Otherwise, there's a race condition as soon as
    9111                 :            :          * the kernel starts sending station notifications.
    9112                 :            :          */
    9113                 :            : 
    9114         [ -  + ]:         22 :         if (!tb[NL80211_ATTR_STA_INFO]) {
    9115                 :          0 :                 wpa_printf(MSG_DEBUG, "sta stats missing!");
    9116                 :          0 :                 return NL_SKIP;
    9117                 :            :         }
    9118         [ -  + ]:         22 :         if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
    9119                 :            :                              tb[NL80211_ATTR_STA_INFO],
    9120                 :            :                              stats_policy)) {
    9121                 :          0 :                 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
    9122                 :          0 :                 return NL_SKIP;
    9123                 :            :         }
    9124                 :            : 
    9125         [ +  - ]:         22 :         if (stats[NL80211_STA_INFO_INACTIVE_TIME])
    9126                 :         22 :                 data->inactive_msec =
    9127                 :         22 :                         nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
    9128         [ +  - ]:         22 :         if (stats[NL80211_STA_INFO_RX_BYTES])
    9129                 :         22 :                 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
    9130         [ +  - ]:         22 :         if (stats[NL80211_STA_INFO_TX_BYTES])
    9131                 :         22 :                 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
    9132         [ +  - ]:         22 :         if (stats[NL80211_STA_INFO_RX_PACKETS])
    9133                 :         22 :                 data->rx_packets =
    9134                 :         22 :                         nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
    9135         [ +  - ]:         22 :         if (stats[NL80211_STA_INFO_TX_PACKETS])
    9136                 :         22 :                 data->tx_packets =
    9137                 :         22 :                         nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
    9138         [ +  - ]:         22 :         if (stats[NL80211_STA_INFO_TX_FAILED])
    9139                 :         22 :                 data->tx_retry_failed =
    9140                 :         22 :                         nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
    9141                 :            : 
    9142                 :         22 :         return NL_SKIP;
    9143                 :            : }
    9144                 :            : 
    9145                 :         22 : static int i802_read_sta_data(struct i802_bss *bss,
    9146                 :            :                               struct hostap_sta_driver_data *data,
    9147                 :            :                               const u8 *addr)
    9148                 :            : {
    9149                 :         22 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9150                 :            :         struct nl_msg *msg;
    9151                 :            : 
    9152                 :         22 :         os_memset(data, 0, sizeof(*data));
    9153                 :         22 :         msg = nlmsg_alloc();
    9154         [ -  + ]:         22 :         if (!msg)
    9155                 :          0 :                 return -ENOMEM;
    9156                 :            : 
    9157                 :         22 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
    9158                 :            : 
    9159         [ +  - ]:         22 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    9160         [ +  - ]:         22 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
    9161                 :            : 
    9162                 :         22 :         return send_and_recv_msgs(drv, msg, get_sta_handler, data);
    9163                 :            :  nla_put_failure:
    9164                 :          0 :         nlmsg_free(msg);
    9165                 :         22 :         return -ENOBUFS;
    9166                 :            : }
    9167                 :            : 
    9168                 :            : 
    9169                 :       1644 : static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
    9170                 :            :                                     int cw_min, int cw_max, int burst_time)
    9171                 :            : {
    9172                 :       1644 :         struct i802_bss *bss = priv;
    9173                 :       1644 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9174                 :            :         struct nl_msg *msg;
    9175                 :            :         struct nlattr *txq, *params;
    9176                 :            : 
    9177                 :       1644 :         msg = nlmsg_alloc();
    9178         [ -  + ]:       1644 :         if (!msg)
    9179                 :          0 :                 return -1;
    9180                 :            : 
    9181                 :       1644 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
    9182                 :            : 
    9183         [ +  - ]:       1644 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
    9184                 :            : 
    9185                 :       1644 :         txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
    9186         [ -  + ]:       1644 :         if (!txq)
    9187                 :          0 :                 goto nla_put_failure;
    9188                 :            : 
    9189                 :            :         /* We are only sending parameters for a single TXQ at a time */
    9190                 :       1644 :         params = nla_nest_start(msg, 1);
    9191         [ -  + ]:       1644 :         if (!params)
    9192                 :          0 :                 goto nla_put_failure;
    9193                 :            : 
    9194   [ +  +  +  +  :       1644 :         switch (queue) {
                      - ]
    9195                 :            :         case 0:
    9196         [ +  - ]:        411 :                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
    9197                 :        411 :                 break;
    9198                 :            :         case 1:
    9199         [ +  - ]:        411 :                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
    9200                 :        411 :                 break;
    9201                 :            :         case 2:
    9202         [ +  - ]:        411 :                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
    9203                 :        411 :                 break;
    9204                 :            :         case 3:
    9205         [ +  - ]:        411 :                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
    9206                 :        411 :                 break;
    9207                 :            :         }
    9208                 :            :         /* Burst time is configured in units of 0.1 msec and TXOP parameter in
    9209                 :            :          * 32 usec, so need to convert the value here. */
    9210         [ +  - ]:       1644 :         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
    9211         [ +  - ]:       1644 :         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
    9212         [ +  - ]:       1644 :         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
    9213         [ +  - ]:       1644 :         NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
    9214                 :            : 
    9215                 :       1644 :         nla_nest_end(msg, params);
    9216                 :            : 
    9217                 :       1644 :         nla_nest_end(msg, txq);
    9218                 :            : 
    9219         [ +  - ]:       1644 :         if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
    9220                 :       1644 :                 return 0;
    9221                 :          0 :         msg = NULL;
    9222                 :            :  nla_put_failure:
    9223                 :          0 :         nlmsg_free(msg);
    9224                 :       1644 :         return -1;
    9225                 :            : }
    9226                 :            : 
    9227                 :            : 
    9228                 :          0 : static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
    9229                 :            :                              const char *ifname, int vlan_id)
    9230                 :            : {
    9231                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9232                 :            :         struct nl_msg *msg;
    9233                 :          0 :         int ret = -ENOBUFS;
    9234                 :            : 
    9235                 :          0 :         msg = nlmsg_alloc();
    9236         [ #  # ]:          0 :         if (!msg)
    9237                 :          0 :                 return -ENOMEM;
    9238                 :            : 
    9239                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
    9240                 :            :                    ", ifname=%s[%d], vlan_id=%d)",
    9241                 :          0 :                    bss->ifname, if_nametoindex(bss->ifname),
    9242                 :          0 :                    MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
    9243                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
    9244                 :            : 
    9245         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
    9246                 :            :                     if_nametoindex(bss->ifname));
    9247         [ #  # ]:          0 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    9248         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
    9249                 :            :                     if_nametoindex(ifname));
    9250                 :            : 
    9251                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
    9252                 :          0 :         msg = NULL;
    9253         [ #  # ]:          0 :         if (ret < 0) {
    9254                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
    9255                 :            :                            MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
    9256                 :          0 :                            MAC2STR(addr), ifname, vlan_id, ret,
    9257                 :            :                            strerror(-ret));
    9258                 :            :         }
    9259                 :            :  nla_put_failure:
    9260                 :          0 :         nlmsg_free(msg);
    9261                 :          0 :         return ret;
    9262                 :            : }
    9263                 :            : 
    9264                 :            : 
    9265                 :          0 : static int i802_get_inact_sec(void *priv, const u8 *addr)
    9266                 :            : {
    9267                 :            :         struct hostap_sta_driver_data data;
    9268                 :            :         int ret;
    9269                 :            : 
    9270                 :          0 :         data.inactive_msec = (unsigned long) -1;
    9271                 :          0 :         ret = i802_read_sta_data(priv, &data, addr);
    9272 [ #  # ][ #  # ]:          0 :         if (ret || data.inactive_msec == (unsigned long) -1)
    9273                 :          0 :                 return -1;
    9274                 :          0 :         return data.inactive_msec / 1000;
    9275                 :            : }
    9276                 :            : 
    9277                 :            : 
    9278                 :        415 : static int i802_sta_clear_stats(void *priv, const u8 *addr)
    9279                 :            : {
    9280                 :            : #if 0
    9281                 :            :         /* TODO */
    9282                 :            : #endif
    9283                 :        415 :         return 0;
    9284                 :            : }
    9285                 :            : 
    9286                 :            : 
    9287                 :       1066 : static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
    9288                 :            :                            int reason)
    9289                 :            : {
    9290                 :       1066 :         struct i802_bss *bss = priv;
    9291                 :       1066 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9292                 :            :         struct ieee80211_mgmt mgmt;
    9293                 :            : 
    9294         [ -  + ]:       1066 :         if (drv->device_ap_sme)
    9295                 :          0 :                 return wpa_driver_nl80211_sta_remove(bss, addr);
    9296                 :            : 
    9297                 :       1066 :         memset(&mgmt, 0, sizeof(mgmt));
    9298                 :       1066 :         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
    9299                 :            :                                           WLAN_FC_STYPE_DEAUTH);
    9300                 :       1066 :         memcpy(mgmt.da, addr, ETH_ALEN);
    9301                 :       1066 :         memcpy(mgmt.sa, own_addr, ETH_ALEN);
    9302                 :       1066 :         memcpy(mgmt.bssid, own_addr, ETH_ALEN);
    9303                 :       1066 :         mgmt.u.deauth.reason_code = host_to_le16(reason);
    9304                 :       1066 :         return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
    9305                 :            :                                             IEEE80211_HDRLEN +
    9306                 :            :                                             sizeof(mgmt.u.deauth), 0, 0, 0, 0,
    9307                 :            :                                             0);
    9308                 :            : }
    9309                 :            : 
    9310                 :            : 
    9311                 :          1 : static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
    9312                 :            :                              int reason)
    9313                 :            : {
    9314                 :          1 :         struct i802_bss *bss = priv;
    9315                 :          1 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9316                 :            :         struct ieee80211_mgmt mgmt;
    9317                 :            : 
    9318         [ -  + ]:          1 :         if (drv->device_ap_sme)
    9319                 :          0 :                 return wpa_driver_nl80211_sta_remove(bss, addr);
    9320                 :            : 
    9321                 :          1 :         memset(&mgmt, 0, sizeof(mgmt));
    9322                 :          1 :         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
    9323                 :            :                                           WLAN_FC_STYPE_DISASSOC);
    9324                 :          1 :         memcpy(mgmt.da, addr, ETH_ALEN);
    9325                 :          1 :         memcpy(mgmt.sa, own_addr, ETH_ALEN);
    9326                 :          1 :         memcpy(mgmt.bssid, own_addr, ETH_ALEN);
    9327                 :          1 :         mgmt.u.disassoc.reason_code = host_to_le16(reason);
    9328                 :          1 :         return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
    9329                 :            :                                             IEEE80211_HDRLEN +
    9330                 :            :                                             sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
    9331                 :            :                                             0);
    9332                 :            : }
    9333                 :            : 
    9334                 :            : 
    9335                 :        375 : static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
    9336                 :            : {
    9337                 :            :         int i;
    9338                 :            :         int *old;
    9339                 :            : 
    9340                 :        375 :         wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
    9341                 :            :                    ifidx);
    9342         [ +  - ]:        400 :         for (i = 0; i < drv->num_if_indices; i++) {
    9343         [ +  + ]:        400 :                 if (drv->if_indices[i] == 0) {
    9344                 :        375 :                         drv->if_indices[i] = ifidx;
    9345                 :        375 :                         return;
    9346                 :            :                 }
    9347                 :            :         }
    9348                 :            : 
    9349         [ #  # ]:          0 :         if (drv->if_indices != drv->default_if_indices)
    9350                 :          0 :                 old = drv->if_indices;
    9351                 :            :         else
    9352                 :          0 :                 old = NULL;
    9353                 :            : 
    9354                 :          0 :         drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
    9355                 :            :                                            sizeof(int));
    9356         [ #  # ]:          0 :         if (!drv->if_indices) {
    9357         [ #  # ]:          0 :                 if (!old)
    9358                 :          0 :                         drv->if_indices = drv->default_if_indices;
    9359                 :            :                 else
    9360                 :          0 :                         drv->if_indices = old;
    9361                 :          0 :                 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
    9362                 :            :                            "interfaces");
    9363                 :          0 :                 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
    9364                 :          0 :                 return;
    9365         [ #  # ]:          0 :         } else if (!old)
    9366                 :          0 :                 os_memcpy(drv->if_indices, drv->default_if_indices,
    9367                 :            :                           sizeof(drv->default_if_indices));
    9368                 :          0 :         drv->if_indices[drv->num_if_indices] = ifidx;
    9369                 :        375 :         drv->num_if_indices++;
    9370                 :            : }
    9371                 :            : 
    9372                 :            : 
    9373                 :         43 : static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
    9374                 :            : {
    9375                 :            :         int i;
    9376                 :            : 
    9377         [ +  - ]:         68 :         for (i = 0; i < drv->num_if_indices; i++) {
    9378         [ +  + ]:         68 :                 if (drv->if_indices[i] == ifidx) {
    9379                 :         43 :                         drv->if_indices[i] = 0;
    9380                 :         43 :                         break;
    9381                 :            :                 }
    9382                 :            :         }
    9383                 :         43 : }
    9384                 :            : 
    9385                 :            : 
    9386                 :      15610 : static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
    9387                 :            : {
    9388                 :            :         int i;
    9389                 :            : 
    9390         [ +  + ]:     230552 :         for (i = 0; i < drv->num_if_indices; i++)
    9391         [ +  + ]:     217125 :                 if (drv->if_indices[i] == ifidx)
    9392                 :       2183 :                         return 1;
    9393                 :            : 
    9394                 :      15610 :         return 0;
    9395                 :            : }
    9396                 :            : 
    9397                 :            : 
    9398                 :          0 : static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
    9399                 :            :                             const char *bridge_ifname, char *ifname_wds)
    9400                 :            : {
    9401                 :          0 :         struct i802_bss *bss = priv;
    9402                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9403                 :            :         char name[IFNAMSIZ + 1];
    9404                 :            : 
    9405                 :          0 :         os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
    9406         [ #  # ]:          0 :         if (ifname_wds)
    9407                 :          0 :                 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
    9408                 :            : 
    9409                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
    9410                 :          0 :                    " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
    9411         [ #  # ]:          0 :         if (val) {
    9412         [ #  # ]:          0 :                 if (!if_nametoindex(name)) {
    9413         [ #  # ]:          0 :                         if (nl80211_create_iface(drv, name,
    9414                 :            :                                                  NL80211_IFTYPE_AP_VLAN,
    9415                 :          0 :                                                  bss->addr, 1, NULL, NULL, 0) <
    9416                 :            :                             0)
    9417                 :          0 :                                 return -1;
    9418   [ #  #  #  # ]:          0 :                         if (bridge_ifname &&
    9419                 :          0 :                             linux_br_add_if(drv->global->ioctl_sock,
    9420                 :            :                                             bridge_ifname, name) < 0)
    9421                 :          0 :                                 return -1;
    9422                 :            :                 }
    9423         [ #  # ]:          0 :                 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
    9424                 :          0 :                         wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
    9425                 :            :                                    "interface %s up", name);
    9426                 :            :                 }
    9427                 :          0 :                 return i802_set_sta_vlan(priv, addr, name, 0);
    9428                 :            :         } else {
    9429         [ #  # ]:          0 :                 if (bridge_ifname)
    9430                 :          0 :                         linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
    9431                 :            :                                         name);
    9432                 :            : 
    9433                 :          0 :                 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
    9434                 :          0 :                 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
    9435                 :            :                                                     name);
    9436                 :            :         }
    9437                 :            : }
    9438                 :            : 
    9439                 :            : 
    9440                 :       5217 : static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
    9441                 :            : {
    9442                 :       5217 :         struct wpa_driver_nl80211_data *drv = eloop_ctx;
    9443                 :            :         struct sockaddr_ll lladdr;
    9444                 :            :         unsigned char buf[3000];
    9445                 :            :         int len;
    9446                 :       5217 :         socklen_t fromlen = sizeof(lladdr);
    9447                 :            : 
    9448                 :       5217 :         len = recvfrom(sock, buf, sizeof(buf), 0,
    9449                 :            :                        (struct sockaddr *)&lladdr, &fromlen);
    9450         [ -  + ]:       5217 :         if (len < 0) {
    9451                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
    9452                 :          0 :                            strerror(errno));
    9453                 :       5217 :                 return;
    9454                 :            :         }
    9455                 :            : 
    9456         [ +  + ]:       5217 :         if (have_ifidx(drv, lladdr.sll_ifindex))
    9457                 :       2092 :                 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
    9458                 :            : }
    9459                 :            : 
    9460                 :            : 
    9461                 :          0 : static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
    9462                 :            :                              struct i802_bss *bss,
    9463                 :            :                              const char *brname, const char *ifname)
    9464                 :            : {
    9465                 :            :         int ifindex;
    9466                 :            :         char in_br[IFNAMSIZ];
    9467                 :            : 
    9468                 :          0 :         os_strlcpy(bss->brname, brname, IFNAMSIZ);
    9469                 :          0 :         ifindex = if_nametoindex(brname);
    9470         [ #  # ]:          0 :         if (ifindex == 0) {
    9471                 :            :                 /*
    9472                 :            :                  * Bridge was configured, but the bridge device does
    9473                 :            :                  * not exist. Try to add it now.
    9474                 :            :                  */
    9475         [ #  # ]:          0 :                 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
    9476                 :          0 :                         wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
    9477                 :            :                                    "bridge interface %s: %s",
    9478                 :          0 :                                    brname, strerror(errno));
    9479                 :          0 :                         return -1;
    9480                 :            :                 }
    9481                 :          0 :                 bss->added_bridge = 1;
    9482                 :          0 :                 add_ifidx(drv, if_nametoindex(brname));
    9483                 :            :         }
    9484                 :            : 
    9485         [ #  # ]:          0 :         if (linux_br_get(in_br, ifname) == 0) {
    9486         [ #  # ]:          0 :                 if (os_strcmp(in_br, brname) == 0)
    9487                 :          0 :                         return 0; /* already in the bridge */
    9488                 :            : 
    9489                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
    9490                 :            :                            "bridge %s", ifname, in_br);
    9491         [ #  # ]:          0 :                 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
    9492                 :            :                     0) {
    9493                 :          0 :                         wpa_printf(MSG_ERROR, "nl80211: Failed to "
    9494                 :            :                                    "remove interface %s from bridge "
    9495                 :            :                                    "%s: %s",
    9496                 :          0 :                                    ifname, brname, strerror(errno));
    9497                 :          0 :                         return -1;
    9498                 :            :                 }
    9499                 :            :         }
    9500                 :            : 
    9501                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
    9502                 :            :                    ifname, brname);
    9503         [ #  # ]:          0 :         if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
    9504                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
    9505                 :            :                            "into bridge %s: %s",
    9506                 :          0 :                            ifname, brname, strerror(errno));
    9507                 :          0 :                 return -1;
    9508                 :            :         }
    9509                 :          0 :         bss->added_if_into_bridge = 1;
    9510                 :            : 
    9511                 :          0 :         return 0;
    9512                 :            : }
    9513                 :            : 
    9514                 :            : 
    9515                 :        332 : static void *i802_init(struct hostapd_data *hapd,
    9516                 :            :                        struct wpa_init_params *params)
    9517                 :            : {
    9518                 :            :         struct wpa_driver_nl80211_data *drv;
    9519                 :            :         struct i802_bss *bss;
    9520                 :            :         size_t i;
    9521                 :            :         char brname[IFNAMSIZ];
    9522                 :            :         int ifindex, br_ifindex;
    9523                 :        332 :         int br_added = 0;
    9524                 :            : 
    9525                 :        332 :         bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
    9526                 :            :                                           params->global_priv, 1,
    9527                 :            :                                           params->bssid);
    9528         [ -  + ]:        332 :         if (bss == NULL)
    9529                 :          0 :                 return NULL;
    9530                 :            : 
    9531                 :        332 :         drv = bss->drv;
    9532                 :            : 
    9533         [ -  + ]:        332 :         if (linux_br_get(brname, params->ifname) == 0) {
    9534                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
    9535                 :            :                            params->ifname, brname);
    9536                 :          0 :                 br_ifindex = if_nametoindex(brname);
    9537                 :            :         } else {
    9538                 :        332 :                 brname[0] = '\0';
    9539                 :        332 :                 br_ifindex = 0;
    9540                 :            :         }
    9541                 :            : 
    9542         [ +  + ]:        670 :         for (i = 0; i < params->num_bridge; i++) {
    9543         [ -  + ]:        338 :                 if (params->bridge[i]) {
    9544                 :          0 :                         ifindex = if_nametoindex(params->bridge[i]);
    9545         [ #  # ]:          0 :                         if (ifindex)
    9546                 :          0 :                                 add_ifidx(drv, ifindex);
    9547         [ #  # ]:          0 :                         if (ifindex == br_ifindex)
    9548                 :          0 :                                 br_added = 1;
    9549                 :            :                 }
    9550                 :            :         }
    9551 [ +  - ][ -  + ]:        332 :         if (!br_added && br_ifindex &&
                 [ #  # ]
    9552         [ #  # ]:          0 :             (params->num_bridge == 0 || !params->bridge[0]))
    9553                 :          0 :                 add_ifidx(drv, br_ifindex);
    9554                 :            : 
    9555                 :            :         /* start listening for EAPOL on the default AP interface */
    9556                 :        332 :         add_ifidx(drv, drv->ifindex);
    9557                 :            : 
    9558   [ -  +  #  # ]:        332 :         if (params->num_bridge && params->bridge[0] &&
                 [ +  - ]
    9559                 :          0 :             i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
    9560                 :          0 :                 goto failed;
    9561                 :            : 
    9562                 :        332 :         drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
    9563         [ -  + ]:        332 :         if (drv->eapol_sock < 0) {
    9564                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
    9565                 :          0 :                            strerror(errno));
    9566                 :          0 :                 goto failed;
    9567                 :            :         }
    9568                 :            : 
    9569         [ -  + ]:        332 :         if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
    9570                 :            :         {
    9571                 :          0 :                 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
    9572                 :          0 :                 goto failed;
    9573                 :            :         }
    9574                 :            : 
    9575         [ -  + ]:        332 :         if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
    9576                 :            :                                params->own_addr))
    9577                 :          0 :                 goto failed;
    9578                 :            : 
    9579                 :        332 :         memcpy(bss->addr, params->own_addr, ETH_ALEN);
    9580                 :            : 
    9581                 :        332 :         return bss;
    9582                 :            : 
    9583                 :            : failed:
    9584                 :          0 :         wpa_driver_nl80211_deinit(bss);
    9585                 :        332 :         return NULL;
    9586                 :            : }
    9587                 :            : 
    9588                 :            : 
    9589                 :        332 : static void i802_deinit(void *priv)
    9590                 :            : {
    9591                 :        332 :         struct i802_bss *bss = priv;
    9592                 :        332 :         wpa_driver_nl80211_deinit(bss);
    9593                 :        332 : }
    9594                 :            : 
    9595                 :            : 
    9596                 :         41 : static enum nl80211_iftype wpa_driver_nl80211_if_type(
    9597                 :            :         enum wpa_driver_if_type type)
    9598                 :            : {
    9599   [ -  +  -  +  :         41 :         switch (type) {
                +  -  - ]
    9600                 :            :         case WPA_IF_STATION:
    9601                 :          0 :                 return NL80211_IFTYPE_STATION;
    9602                 :            :         case WPA_IF_P2P_CLIENT:
    9603                 :            :         case WPA_IF_P2P_GROUP:
    9604                 :         14 :                 return NL80211_IFTYPE_P2P_CLIENT;
    9605                 :            :         case WPA_IF_AP_VLAN:
    9606                 :          0 :                 return NL80211_IFTYPE_AP_VLAN;
    9607                 :            :         case WPA_IF_AP_BSS:
    9608                 :         17 :                 return NL80211_IFTYPE_AP;
    9609                 :            :         case WPA_IF_P2P_GO:
    9610                 :         10 :                 return NL80211_IFTYPE_P2P_GO;
    9611                 :            :         case WPA_IF_P2P_DEVICE:
    9612                 :          0 :                 return NL80211_IFTYPE_P2P_DEVICE;
    9613                 :            :         }
    9614                 :         41 :         return -1;
    9615                 :            : }
    9616                 :            : 
    9617                 :            : 
    9618                 :            : #ifdef CONFIG_P2P
    9619                 :            : 
    9620                 :         24 : static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
    9621                 :            : {
    9622                 :            :         struct wpa_driver_nl80211_data *drv;
    9623         [ +  + ]:         48 :         dl_list_for_each(drv, &global->interfaces,
    9624                 :            :                          struct wpa_driver_nl80211_data, list) {
    9625         [ -  + ]:         24 :                 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
    9626                 :          0 :                         return 1;
    9627                 :            :         }
    9628                 :         24 :         return 0;
    9629                 :            : }
    9630                 :            : 
    9631                 :            : 
    9632                 :          0 : static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
    9633                 :            :                                       u8 *new_addr)
    9634                 :            : {
    9635                 :            :         unsigned int idx;
    9636                 :            : 
    9637         [ #  # ]:          0 :         if (!drv->global)
    9638                 :          0 :                 return -1;
    9639                 :            : 
    9640                 :          0 :         os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
    9641         [ #  # ]:          0 :         for (idx = 0; idx < 64; idx++) {
    9642                 :          0 :                 new_addr[0] = drv->first_bss->addr[0] | 0x02;
    9643                 :          0 :                 new_addr[0] ^= idx << 2;
    9644         [ #  # ]:          0 :                 if (!nl80211_addr_in_use(drv->global, new_addr))
    9645                 :          0 :                         break;
    9646                 :            :         }
    9647         [ #  # ]:          0 :         if (idx == 64)
    9648                 :          0 :                 return -1;
    9649                 :            : 
    9650                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
    9651                 :          0 :                    MACSTR, MAC2STR(new_addr));
    9652                 :            : 
    9653                 :          0 :         return 0;
    9654                 :            : }
    9655                 :            : 
    9656                 :            : #endif /* CONFIG_P2P */
    9657                 :            : 
    9658                 :            : 
    9659                 :            : struct wdev_info {
    9660                 :            :         u64 wdev_id;
    9661                 :            :         int wdev_id_set;
    9662                 :            :         u8 macaddr[ETH_ALEN];
    9663                 :            : };
    9664                 :            : 
    9665                 :          0 : static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
    9666                 :            : {
    9667                 :          0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    9668                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    9669                 :          0 :         struct wdev_info *wi = arg;
    9670                 :            : 
    9671                 :          0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    9672                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    9673         [ #  # ]:          0 :         if (tb[NL80211_ATTR_WDEV]) {
    9674                 :          0 :                 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
    9675                 :          0 :                 wi->wdev_id_set = 1;
    9676                 :            :         }
    9677                 :            : 
    9678         [ #  # ]:          0 :         if (tb[NL80211_ATTR_MAC])
    9679                 :          0 :                 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
    9680                 :            :                           ETH_ALEN);
    9681                 :            : 
    9682                 :          0 :         return NL_SKIP;
    9683                 :            : }
    9684                 :            : 
    9685                 :            : 
    9686                 :         41 : static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
    9687                 :            :                                      const char *ifname, const u8 *addr,
    9688                 :            :                                      void *bss_ctx, void **drv_priv,
    9689                 :            :                                      char *force_ifname, u8 *if_addr,
    9690                 :            :                                      const char *bridge, int use_existing)
    9691                 :            : {
    9692                 :            :         enum nl80211_iftype nlmode;
    9693                 :         41 :         struct i802_bss *bss = priv;
    9694                 :         41 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9695                 :            :         int ifidx;
    9696                 :         41 :         int added = 1;
    9697                 :            : 
    9698         [ +  + ]:         41 :         if (addr)
    9699                 :         17 :                 os_memcpy(if_addr, addr, ETH_ALEN);
    9700                 :         41 :         nlmode = wpa_driver_nl80211_if_type(type);
    9701         [ -  + ]:         41 :         if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
    9702                 :            :                 struct wdev_info p2pdev_info;
    9703                 :            : 
    9704                 :          0 :                 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
    9705                 :          0 :                 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
    9706                 :            :                                              0, nl80211_wdev_handler,
    9707                 :            :                                              &p2pdev_info, use_existing);
    9708 [ #  # ][ #  # ]:          0 :                 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
    9709                 :          0 :                         wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
    9710                 :            :                                    ifname);
    9711                 :          0 :                         return -1;
    9712                 :            :                 }
    9713                 :            : 
    9714                 :          0 :                 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
    9715                 :          0 :                 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
    9716         [ #  # ]:          0 :                 if (!is_zero_ether_addr(p2pdev_info.macaddr))
    9717                 :          0 :                         os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
    9718                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
    9719                 :            :                            ifname,
    9720                 :          0 :                            (long long unsigned int) p2pdev_info.wdev_id);
    9721                 :            :         } else {
    9722                 :         41 :                 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
    9723                 :            :                                              0, NULL, NULL, use_existing);
    9724 [ -  + ][ +  + ]:         41 :                 if (use_existing && ifidx == -ENFILE) {
    9725                 :          0 :                         added = 0;
    9726                 :          0 :                         ifidx = if_nametoindex(ifname);
    9727         [ -  + ]:         41 :                 } else if (ifidx < 0) {
    9728                 :          0 :                         return -1;
    9729                 :            :                 }
    9730                 :            :         }
    9731                 :            : 
    9732         [ +  + ]:         41 :         if (!addr) {
    9733         [ -  + ]:         24 :                 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
    9734                 :          0 :                         os_memcpy(if_addr, bss->addr, ETH_ALEN);
    9735         [ -  + ]:         24 :                 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
    9736                 :         24 :                                             bss->ifname, if_addr) < 0) {
    9737         [ #  # ]:          0 :                         if (added)
    9738                 :          0 :                                 nl80211_remove_iface(drv, ifidx);
    9739                 :          0 :                         return -1;
    9740                 :            :                 }
    9741                 :            :         }
    9742                 :            : 
    9743                 :            : #ifdef CONFIG_P2P
    9744 [ +  - ][ +  + ]:         24 :         if (!addr &&
    9745 [ +  + ][ +  - ]:         21 :             (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
    9746                 :            :              type == WPA_IF_P2P_GO)) {
    9747                 :            :                 /* Enforce unique P2P Interface Address */
    9748                 :            :                 u8 new_addr[ETH_ALEN];
    9749                 :            : 
    9750         [ -  + ]:         24 :                 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
    9751                 :            :                                        new_addr) < 0) {
    9752                 :          0 :                         nl80211_remove_iface(drv, ifidx);
    9753                 :          0 :                         return -1;
    9754                 :            :                 }
    9755         [ -  + ]:         24 :                 if (nl80211_addr_in_use(drv->global, new_addr)) {
    9756                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
    9757                 :            :                                    "for P2P group interface");
    9758         [ #  # ]:          0 :                         if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
    9759                 :          0 :                                 nl80211_remove_iface(drv, ifidx);
    9760                 :          0 :                                 return -1;
    9761                 :            :                         }
    9762         [ #  # ]:          0 :                         if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
    9763                 :            :                                                new_addr) < 0) {
    9764                 :          0 :                                 nl80211_remove_iface(drv, ifidx);
    9765                 :          0 :                                 return -1;
    9766                 :            :                         }
    9767                 :            :                 }
    9768                 :         24 :                 os_memcpy(if_addr, new_addr, ETH_ALEN);
    9769                 :            :         }
    9770                 :            : #endif /* CONFIG_P2P */
    9771                 :            : 
    9772         [ +  + ]:         41 :         if (type == WPA_IF_AP_BSS) {
    9773                 :         17 :                 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
    9774         [ -  + ]:         17 :                 if (new_bss == NULL) {
    9775         [ #  # ]:          0 :                         if (added)
    9776                 :          0 :                                 nl80211_remove_iface(drv, ifidx);
    9777                 :          0 :                         return -1;
    9778                 :            :                 }
    9779                 :            : 
    9780   [ -  +  #  # ]:         17 :                 if (bridge &&
    9781                 :          0 :                     i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
    9782                 :          0 :                         wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
    9783                 :            :                                    "interface %s to a bridge %s",
    9784                 :            :                                    ifname, bridge);
    9785         [ #  # ]:          0 :                         if (added)
    9786                 :          0 :                                 nl80211_remove_iface(drv, ifidx);
    9787                 :          0 :                         os_free(new_bss);
    9788                 :          0 :                         return -1;
    9789                 :            :                 }
    9790                 :            : 
    9791         [ -  + ]:         17 :                 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
    9792                 :            :                 {
    9793                 :          0 :                         nl80211_remove_iface(drv, ifidx);
    9794                 :          0 :                         os_free(new_bss);
    9795                 :          0 :                         return -1;
    9796                 :            :                 }
    9797                 :         17 :                 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
    9798                 :         17 :                 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
    9799                 :         17 :                 new_bss->ifindex = ifidx;
    9800                 :         17 :                 new_bss->drv = drv;
    9801                 :         17 :                 new_bss->next = drv->first_bss->next;
    9802                 :         17 :                 new_bss->freq = drv->first_bss->freq;
    9803                 :         17 :                 new_bss->ctx = bss_ctx;
    9804                 :         17 :                 new_bss->added_if = added;
    9805                 :         17 :                 drv->first_bss->next = new_bss;
    9806         [ +  - ]:         17 :                 if (drv_priv)
    9807                 :         17 :                         *drv_priv = new_bss;
    9808                 :         17 :                 nl80211_init_bss(new_bss);
    9809                 :            : 
    9810                 :            :                 /* Subscribe management frames for this WPA_IF_AP_BSS */
    9811         [ -  + ]:         17 :                 if (nl80211_setup_ap(new_bss))
    9812                 :          0 :                         return -1;
    9813                 :            :         }
    9814                 :            : 
    9815         [ +  - ]:         41 :         if (drv->global)
    9816                 :         41 :                 drv->global->if_add_ifindex = ifidx;
    9817                 :            : 
    9818                 :         41 :         return 0;
    9819                 :            : }
    9820                 :            : 
    9821                 :            : 
    9822                 :         41 : static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
    9823                 :            :                                         enum wpa_driver_if_type type,
    9824                 :            :                                         const char *ifname)
    9825                 :            : {
    9826                 :         41 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9827                 :         41 :         int ifindex = if_nametoindex(ifname);
    9828                 :            : 
    9829                 :         41 :         wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
    9830                 :         41 :                    __func__, type, ifname, ifindex, bss->added_if);
    9831 [ +  + ][ +  - ]:         41 :         if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
                 [ +  - ]
    9832                 :         41 :                 nl80211_remove_iface(drv, ifindex);
    9833                 :            : 
    9834         [ +  + ]:         41 :         if (type != WPA_IF_AP_BSS)
    9835                 :         24 :                 return 0;
    9836                 :            : 
    9837         [ -  + ]:         17 :         if (bss->added_if_into_bridge) {
    9838         [ #  # ]:          0 :                 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
    9839                 :          0 :                                     bss->ifname) < 0)
    9840                 :          0 :                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
    9841                 :            :                                    "interface %s from bridge %s: %s",
    9842                 :          0 :                                    bss->ifname, bss->brname, strerror(errno));
    9843                 :            :         }
    9844         [ -  + ]:         17 :         if (bss->added_bridge) {
    9845         [ #  # ]:          0 :                 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
    9846                 :          0 :                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
    9847                 :            :                                    "bridge %s: %s",
    9848                 :          0 :                                    bss->brname, strerror(errno));
    9849                 :            :         }
    9850                 :            : 
    9851         [ +  - ]:         17 :         if (bss != drv->first_bss) {
    9852                 :            :                 struct i802_bss *tbss;
    9853                 :            : 
    9854                 :         17 :                 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
    9855         [ +  - ]:         20 :                 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
    9856         [ +  + ]:         20 :                         if (tbss->next == bss) {
    9857                 :         17 :                                 tbss->next = bss->next;
    9858                 :            :                                 /* Unsubscribe management frames */
    9859                 :         17 :                                 nl80211_teardown_ap(bss);
    9860                 :         17 :                                 nl80211_destroy_bss(bss);
    9861                 :         17 :                                 os_free(bss);
    9862                 :         17 :                                 bss = NULL;
    9863                 :         17 :                                 break;
    9864                 :            :                         }
    9865                 :            :                 }
    9866         [ -  + ]:         17 :                 if (bss)
    9867                 :          0 :                         wpa_printf(MSG_INFO, "nl80211: %s - could not find "
    9868                 :            :                                    "BSS %p in the list", __func__, bss);
    9869                 :            :         } else {
    9870                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
    9871                 :          0 :                 nl80211_teardown_ap(bss);
    9872 [ #  # ][ #  # ]:          0 :                 if (!bss->added_if && !drv->first_bss->next)
    9873                 :          0 :                         wpa_driver_nl80211_del_beacon(drv);
    9874                 :          0 :                 nl80211_destroy_bss(bss);
    9875         [ #  # ]:          0 :                 if (!bss->added_if)
    9876                 :          0 :                         i802_set_iface_flags(bss, 0);
    9877         [ #  # ]:          0 :                 if (drv->first_bss->next) {
    9878                 :          0 :                         drv->first_bss = drv->first_bss->next;
    9879                 :          0 :                         drv->ctx = drv->first_bss->ctx;
    9880                 :          0 :                         os_free(bss);
    9881                 :            :                 } else {
    9882                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
    9883                 :            :                 }
    9884                 :            :         }
    9885                 :            : 
    9886                 :         41 :         return 0;
    9887                 :            : }
    9888                 :            : 
    9889                 :            : 
    9890                 :       4236 : static int cookie_handler(struct nl_msg *msg, void *arg)
    9891                 :            : {
    9892                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
    9893                 :       4236 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
    9894                 :       4236 :         u64 *cookie = arg;
    9895                 :       4236 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
    9896                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
    9897         [ +  - ]:       4236 :         if (tb[NL80211_ATTR_COOKIE])
    9898                 :       4236 :                 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
    9899                 :       4236 :         return NL_SKIP;
    9900                 :            : }
    9901                 :            : 
    9902                 :            : 
    9903                 :       5183 : static int nl80211_send_frame_cmd(struct i802_bss *bss,
    9904                 :            :                                   unsigned int freq, unsigned int wait,
    9905                 :            :                                   const u8 *buf, size_t buf_len,
    9906                 :            :                                   u64 *cookie_out, int no_cck, int no_ack,
    9907                 :            :                                   int offchanok)
    9908                 :            : {
    9909                 :       5183 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9910                 :            :         struct nl_msg *msg;
    9911                 :            :         u64 cookie;
    9912                 :       5183 :         int ret = -1;
    9913                 :            : 
    9914                 :       5183 :         msg = nlmsg_alloc();
    9915         [ -  + ]:       5183 :         if (!msg)
    9916                 :          0 :                 return -1;
    9917                 :            : 
    9918                 :       5183 :         wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
    9919                 :            :                    "no_ack=%d offchanok=%d",
    9920                 :            :                    freq, wait, no_cck, no_ack, offchanok);
    9921                 :       5183 :         wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
    9922                 :       5183 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
    9923                 :            : 
    9924         [ -  + ]:       5183 :         if (nl80211_set_iface_id(msg, bss) < 0)
    9925                 :          0 :                 goto nla_put_failure;
    9926         [ +  + ]:       5183 :         if (freq)
    9927         [ +  - ]:       5171 :                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
    9928         [ +  + ]:       5183 :         if (wait)
    9929         [ +  - ]:        868 :                 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
    9930 [ +  + ][ +  + ]:       5183 :         if (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
                 [ +  - ]
    9931                 :            :                           drv->test_use_roc_tx))
    9932         [ +  - ]:       1426 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
    9933         [ +  + ]:       5183 :         if (no_cck)
    9934         [ +  - ]:        861 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
    9935         [ +  + ]:       5183 :         if (no_ack)
    9936         [ +  - ]:       1289 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
    9937                 :            : 
    9938         [ +  - ]:       5183 :         NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
    9939                 :            : 
    9940                 :       5183 :         cookie = 0;
    9941                 :       5183 :         ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
    9942                 :       5183 :         msg = NULL;
    9943         [ +  + ]:       5183 :         if (ret) {
    9944                 :        436 :                 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
    9945                 :            :                            "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
    9946                 :            :                            freq, wait);
    9947                 :        436 :                 goto nla_put_failure;
    9948                 :            :         }
    9949         [ +  + ]:       4747 :         wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
    9950                 :            :                    "cookie 0x%llx", no_ack ? " (no ACK)" : "",
    9951                 :            :                    (long long unsigned int) cookie);
    9952                 :            : 
    9953         [ +  + ]:       4747 :         if (cookie_out)
    9954         [ +  + ]:       4412 :                 *cookie_out = no_ack ? (u64) -1 : cookie;
    9955                 :            : 
    9956                 :            : nla_put_failure:
    9957                 :       5183 :         nlmsg_free(msg);
    9958                 :       5183 :         return ret;
    9959                 :            : }
    9960                 :            : 
    9961                 :            : 
    9962                 :       1091 : static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
    9963                 :            :                                           unsigned int freq,
    9964                 :            :                                           unsigned int wait_time,
    9965                 :            :                                           const u8 *dst, const u8 *src,
    9966                 :            :                                           const u8 *bssid,
    9967                 :            :                                           const u8 *data, size_t data_len,
    9968                 :            :                                           int no_cck)
    9969                 :            : {
    9970                 :       1091 :         struct wpa_driver_nl80211_data *drv = bss->drv;
    9971                 :       1091 :         int ret = -1;
    9972                 :            :         u8 *buf;
    9973                 :            :         struct ieee80211_hdr *hdr;
    9974                 :            : 
    9975                 :       1091 :         wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
    9976                 :            :                    "freq=%u MHz wait=%d ms no_cck=%d)",
    9977                 :            :                    drv->ifindex, freq, wait_time, no_cck);
    9978                 :            : 
    9979                 :       1091 :         buf = os_zalloc(24 + data_len);
    9980         [ -  + ]:       1091 :         if (buf == NULL)
    9981                 :          0 :                 return ret;
    9982                 :       1091 :         os_memcpy(buf + 24, data, data_len);
    9983                 :       1091 :         hdr = (struct ieee80211_hdr *) buf;
    9984                 :       1091 :         hdr->frame_control =
    9985                 :            :                 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
    9986                 :       1091 :         os_memcpy(hdr->addr1, dst, ETH_ALEN);
    9987                 :       1091 :         os_memcpy(hdr->addr2, src, ETH_ALEN);
    9988                 :       1091 :         os_memcpy(hdr->addr3, bssid, ETH_ALEN);
    9989                 :            : 
    9990 [ +  + ][ +  - ]:       1091 :         if (is_ap_interface(drv->nlmode) &&
    9991         [ +  + ]:        235 :             (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
    9992 [ +  - ][ +  - ]:          5 :              (int) freq == bss->freq || drv->device_ap_sme ||
    9993                 :          5 :              !drv->use_monitor))
    9994                 :        235 :                 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
    9995                 :            :                                                    0, freq, no_cck, 1,
    9996                 :            :                                                    wait_time);
    9997                 :            :         else
    9998                 :        856 :                 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
    9999                 :            :                                              24 + data_len,
   10000                 :            :                                              &drv->send_action_cookie,
   10001                 :            :                                              no_cck, 0, 1);
   10002                 :            : 
   10003                 :       1091 :         os_free(buf);
   10004                 :       1091 :         return ret;
   10005                 :            : }
   10006                 :            : 
   10007                 :            : 
   10008                 :        463 : static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
   10009                 :            : {
   10010                 :        463 :         struct i802_bss *bss = priv;
   10011                 :        463 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10012                 :            :         struct nl_msg *msg;
   10013                 :            :         int ret;
   10014                 :            : 
   10015                 :        463 :         msg = nlmsg_alloc();
   10016         [ -  + ]:        463 :         if (!msg)
   10017                 :        463 :                 return;
   10018                 :            : 
   10019                 :        463 :         wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
   10020                 :        463 :                    (long long unsigned int) drv->send_action_cookie);
   10021                 :        463 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
   10022                 :            : 
   10023         [ -  + ]:        463 :         if (nl80211_set_iface_id(msg, bss) < 0)
   10024                 :          0 :                 goto nla_put_failure;
   10025         [ +  - ]:        463 :         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
   10026                 :            : 
   10027                 :        463 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   10028                 :        463 :         msg = NULL;
   10029         [ +  + ]:        463 :         if (ret)
   10030                 :         67 :                 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
   10031                 :            :                            "(%s)", ret, strerror(-ret));
   10032                 :            : 
   10033                 :            :  nla_put_failure:
   10034                 :        463 :         nlmsg_free(msg);
   10035                 :            : }
   10036                 :            : 
   10037                 :            : 
   10038                 :        775 : static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
   10039                 :            :                                                 unsigned int duration)
   10040                 :            : {
   10041                 :        775 :         struct i802_bss *bss = priv;
   10042                 :        775 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10043                 :            :         struct nl_msg *msg;
   10044                 :            :         int ret;
   10045                 :            :         u64 cookie;
   10046                 :            : 
   10047                 :        775 :         msg = nlmsg_alloc();
   10048         [ -  + ]:        775 :         if (!msg)
   10049                 :          0 :                 return -1;
   10050                 :            : 
   10051                 :        775 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
   10052                 :            : 
   10053         [ -  + ]:        775 :         if (nl80211_set_iface_id(msg, bss) < 0)
   10054                 :          0 :                 goto nla_put_failure;
   10055                 :            : 
   10056         [ +  - ]:        775 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
   10057         [ +  - ]:        775 :         NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
   10058                 :            : 
   10059                 :        775 :         cookie = 0;
   10060                 :        775 :         ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
   10061                 :        775 :         msg = NULL;
   10062         [ +  - ]:        775 :         if (ret == 0) {
   10063                 :        775 :                 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
   10064                 :            :                            "0x%llx for freq=%u MHz duration=%u",
   10065                 :            :                            (long long unsigned int) cookie, freq, duration);
   10066                 :        775 :                 drv->remain_on_chan_cookie = cookie;
   10067                 :        775 :                 drv->pending_remain_on_chan = 1;
   10068                 :        775 :                 return 0;
   10069                 :            :         }
   10070                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
   10071                 :            :                    "(freq=%d duration=%u): %d (%s)",
   10072                 :            :                    freq, duration, ret, strerror(-ret));
   10073                 :            : nla_put_failure:
   10074                 :          0 :         nlmsg_free(msg);
   10075                 :        775 :         return -1;
   10076                 :            : }
   10077                 :            : 
   10078                 :            : 
   10079                 :        249 : static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
   10080                 :            : {
   10081                 :        249 :         struct i802_bss *bss = priv;
   10082                 :        249 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10083                 :            :         struct nl_msg *msg;
   10084                 :            :         int ret;
   10085                 :            : 
   10086         [ -  + ]:        249 :         if (!drv->pending_remain_on_chan) {
   10087                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
   10088                 :            :                            "to cancel");
   10089                 :          0 :                 return -1;
   10090                 :            :         }
   10091                 :            : 
   10092                 :        249 :         wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
   10093                 :            :                    "0x%llx",
   10094                 :        249 :                    (long long unsigned int) drv->remain_on_chan_cookie);
   10095                 :            : 
   10096                 :        249 :         msg = nlmsg_alloc();
   10097         [ -  + ]:        249 :         if (!msg)
   10098                 :          0 :                 return -1;
   10099                 :            : 
   10100                 :        249 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
   10101                 :            : 
   10102         [ -  + ]:        249 :         if (nl80211_set_iface_id(msg, bss) < 0)
   10103                 :          0 :                 goto nla_put_failure;
   10104                 :            : 
   10105         [ +  - ]:        249 :         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
   10106                 :            : 
   10107                 :        249 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   10108                 :        249 :         msg = NULL;
   10109         [ +  + ]:        249 :         if (ret == 0)
   10110                 :        246 :                 return 0;
   10111                 :          3 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
   10112                 :            :                    "%d (%s)", ret, strerror(-ret));
   10113                 :            : nla_put_failure:
   10114                 :          3 :         nlmsg_free(msg);
   10115                 :        249 :         return -1;
   10116                 :            : }
   10117                 :            : 
   10118                 :            : 
   10119                 :       4606 : static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
   10120                 :            : {
   10121                 :       4606 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10122                 :            : 
   10123         [ +  + ]:       4606 :         if (!report) {
   10124         [ +  + ]:       3833 :                 if (bss->nl_preq && drv->device_ap_sme &&
           [ -  +  #  # ]
   10125                 :          0 :                     is_ap_interface(drv->nlmode)) {
   10126                 :            :                         /*
   10127                 :            :                          * Do not disable Probe Request reporting that was
   10128                 :            :                          * enabled in nl80211_setup_ap().
   10129                 :            :                          */
   10130                 :          0 :                         wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
   10131                 :            :                                    "Probe Request reporting nl_preq=%p while "
   10132                 :            :                                    "in AP mode", bss->nl_preq);
   10133         [ +  + ]:       3833 :                 } else if (bss->nl_preq) {
   10134                 :        732 :                         wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
   10135                 :            :                                    "reporting nl_preq=%p", bss->nl_preq);
   10136                 :        732 :                         nl80211_destroy_eloop_handle(&bss->nl_preq);
   10137                 :            :                 }
   10138                 :       3833 :                 return 0;
   10139                 :            :         }
   10140                 :            : 
   10141         [ +  + ]:        773 :         if (bss->nl_preq) {
   10142                 :         41 :                 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
   10143                 :            :                            "already on! nl_preq=%p", bss->nl_preq);
   10144                 :         41 :                 return 0;
   10145                 :            :         }
   10146                 :            : 
   10147                 :        732 :         bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
   10148         [ -  + ]:        732 :         if (bss->nl_preq == NULL)
   10149                 :          0 :                 return -1;
   10150                 :        732 :         wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
   10151                 :            :                    "reporting nl_preq=%p", bss->nl_preq);
   10152                 :            : 
   10153         [ -  + ]:        732 :         if (nl80211_register_frame(bss, bss->nl_preq,
   10154                 :            :                                    (WLAN_FC_TYPE_MGMT << 2) |
   10155                 :            :                                    (WLAN_FC_STYPE_PROBE_REQ << 4),
   10156                 :            :                                    NULL, 0) < 0)
   10157                 :          0 :                 goto out_err;
   10158                 :            : 
   10159                 :        732 :         nl80211_register_eloop_read(&bss->nl_preq,
   10160                 :            :                                     wpa_driver_nl80211_event_receive,
   10161                 :        732 :                                     bss->nl_cb);
   10162                 :            : 
   10163                 :        732 :         return 0;
   10164                 :            : 
   10165                 :            :  out_err:
   10166                 :          0 :         nl_destroy_handles(&bss->nl_preq);
   10167                 :       4606 :         return -1;
   10168                 :            : }
   10169                 :            : 
   10170                 :            : 
   10171                 :        362 : static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
   10172                 :            :                                      int ifindex, int disabled)
   10173                 :            : {
   10174                 :            :         struct nl_msg *msg;
   10175                 :            :         struct nlattr *bands, *band;
   10176                 :            :         int ret;
   10177                 :            : 
   10178                 :        362 :         msg = nlmsg_alloc();
   10179         [ -  + ]:        362 :         if (!msg)
   10180                 :          0 :                 return -1;
   10181                 :            : 
   10182                 :        362 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
   10183         [ +  - ]:        362 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
   10184                 :            : 
   10185                 :        362 :         bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
   10186         [ -  + ]:        362 :         if (!bands)
   10187                 :          0 :                 goto nla_put_failure;
   10188                 :            : 
   10189                 :            :         /*
   10190                 :            :          * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
   10191                 :            :          * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
   10192                 :            :          * rates. All 5 GHz rates are left enabled.
   10193                 :            :          */
   10194                 :        362 :         band = nla_nest_start(msg, NL80211_BAND_2GHZ);
   10195         [ -  + ]:        362 :         if (!band)
   10196                 :          0 :                 goto nla_put_failure;
   10197         [ +  + ]:        362 :         if (disabled) {
   10198         [ +  - ]:        205 :                 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
   10199                 :            :                         "\x0c\x12\x18\x24\x30\x48\x60\x6c");
   10200                 :            :         }
   10201                 :        362 :         nla_nest_end(msg, band);
   10202                 :            : 
   10203                 :        362 :         nla_nest_end(msg, bands);
   10204                 :            : 
   10205                 :        362 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   10206                 :        362 :         msg = NULL;
   10207         [ +  + ]:        362 :         if (ret) {
   10208                 :         48 :                 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
   10209                 :            :                            "(%s)", ret, strerror(-ret));
   10210                 :            :         } else
   10211                 :        314 :                 drv->disabled_11b_rates = disabled;
   10212                 :            : 
   10213                 :        362 :         return ret;
   10214                 :            : 
   10215                 :            : nla_put_failure:
   10216                 :          0 :         nlmsg_free(msg);
   10217                 :        362 :         return -1;
   10218                 :            : }
   10219                 :            : 
   10220                 :            : 
   10221                 :         86 : static int wpa_driver_nl80211_deinit_ap(void *priv)
   10222                 :            : {
   10223                 :         86 :         struct i802_bss *bss = priv;
   10224                 :         86 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10225         [ -  + ]:         86 :         if (!is_ap_interface(drv->nlmode))
   10226                 :          0 :                 return -1;
   10227                 :         86 :         wpa_driver_nl80211_del_beacon(drv);
   10228                 :            : 
   10229                 :            :         /*
   10230                 :            :          * If the P2P GO interface was dynamically added, then it is
   10231                 :            :          * possible that the interface change to station is not possible.
   10232                 :            :          */
   10233 [ +  + ][ +  + ]:         86 :         if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
   10234                 :         13 :                 return 0;
   10235                 :            : 
   10236                 :         86 :         return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
   10237                 :            : }
   10238                 :            : 
   10239                 :            : 
   10240                 :          0 : static int wpa_driver_nl80211_stop_ap(void *priv)
   10241                 :            : {
   10242                 :          0 :         struct i802_bss *bss = priv;
   10243                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10244         [ #  # ]:          0 :         if (!is_ap_interface(drv->nlmode))
   10245                 :          0 :                 return -1;
   10246                 :          0 :         wpa_driver_nl80211_del_beacon(drv);
   10247                 :          0 :         bss->beacon_set = 0;
   10248                 :          0 :         return 0;
   10249                 :            : }
   10250                 :            : 
   10251                 :            : 
   10252                 :         76 : static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
   10253                 :            : {
   10254                 :         76 :         struct i802_bss *bss = priv;
   10255                 :         76 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10256         [ -  + ]:         76 :         if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
   10257                 :          0 :                 return -1;
   10258                 :            : 
   10259                 :            :         /*
   10260                 :            :          * If the P2P Client interface was dynamically added, then it is
   10261                 :            :          * possible that the interface change to station is not possible.
   10262                 :            :          */
   10263         [ -  + ]:         76 :         if (bss->if_dynamic)
   10264                 :          0 :                 return 0;
   10265                 :            : 
   10266                 :         76 :         return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
   10267                 :            : }
   10268                 :            : 
   10269                 :            : 
   10270                 :          0 : static void wpa_driver_nl80211_resume(void *priv)
   10271                 :            : {
   10272                 :          0 :         struct i802_bss *bss = priv;
   10273                 :            : 
   10274         [ #  # ]:          0 :         if (i802_set_iface_flags(bss, 1))
   10275                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
   10276                 :          0 : }
   10277                 :            : 
   10278                 :            : 
   10279                 :          6 : static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
   10280                 :            :                                   const u8 *ies, size_t ies_len)
   10281                 :            : {
   10282                 :          6 :         struct i802_bss *bss = priv;
   10283                 :          6 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10284                 :            :         int ret;
   10285                 :            :         u8 *data, *pos;
   10286                 :            :         size_t data_len;
   10287                 :          6 :         const u8 *own_addr = bss->addr;
   10288                 :            : 
   10289         [ -  + ]:          6 :         if (action != 1) {
   10290                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
   10291                 :            :                            "action %d", action);
   10292                 :          0 :                 return -1;
   10293                 :            :         }
   10294                 :            : 
   10295                 :            :         /*
   10296                 :            :          * Action frame payload:
   10297                 :            :          * Category[1] = 6 (Fast BSS Transition)
   10298                 :            :          * Action[1] = 1 (Fast BSS Transition Request)
   10299                 :            :          * STA Address
   10300                 :            :          * Target AP Address
   10301                 :            :          * FT IEs
   10302                 :            :          */
   10303                 :            : 
   10304                 :          6 :         data_len = 2 + 2 * ETH_ALEN + ies_len;
   10305                 :          6 :         data = os_malloc(data_len);
   10306         [ -  + ]:          6 :         if (data == NULL)
   10307                 :          0 :                 return -1;
   10308                 :          6 :         pos = data;
   10309                 :          6 :         *pos++ = 0x06; /* FT Action category */
   10310                 :          6 :         *pos++ = action;
   10311                 :          6 :         os_memcpy(pos, own_addr, ETH_ALEN);
   10312                 :          6 :         pos += ETH_ALEN;
   10313                 :          6 :         os_memcpy(pos, target_ap, ETH_ALEN);
   10314                 :          6 :         pos += ETH_ALEN;
   10315                 :          6 :         os_memcpy(pos, ies, ies_len);
   10316                 :            : 
   10317                 :          6 :         ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
   10318                 :          6 :                                              drv->bssid, own_addr, drv->bssid,
   10319                 :            :                                              data, data_len, 0);
   10320                 :          6 :         os_free(data);
   10321                 :            : 
   10322                 :          6 :         return ret;
   10323                 :            : }
   10324                 :            : 
   10325                 :            : 
   10326                 :          0 : static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
   10327                 :            : {
   10328                 :          0 :         struct i802_bss *bss = priv;
   10329                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10330                 :            :         struct nl_msg *msg;
   10331                 :            :         struct nlattr *cqm;
   10332                 :          0 :         int ret = -1;
   10333                 :            : 
   10334                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
   10335                 :            :                    "hysteresis=%d", threshold, hysteresis);
   10336                 :            : 
   10337                 :          0 :         msg = nlmsg_alloc();
   10338         [ #  # ]:          0 :         if (!msg)
   10339                 :          0 :                 return -1;
   10340                 :            : 
   10341                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
   10342                 :            : 
   10343         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
   10344                 :            : 
   10345                 :          0 :         cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
   10346         [ #  # ]:          0 :         if (cqm == NULL)
   10347                 :          0 :                 goto nla_put_failure;
   10348                 :            : 
   10349         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
   10350         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
   10351                 :          0 :         nla_nest_end(msg, cqm);
   10352                 :            : 
   10353                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   10354                 :          0 :         msg = NULL;
   10355                 :            : 
   10356                 :            : nla_put_failure:
   10357                 :          0 :         nlmsg_free(msg);
   10358                 :          0 :         return ret;
   10359                 :            : }
   10360                 :            : 
   10361                 :            : 
   10362                 :          0 : static int get_channel_width(struct nl_msg *msg, void *arg)
   10363                 :            : {
   10364                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
   10365                 :          0 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
   10366                 :          0 :         struct wpa_signal_info *sig_change = arg;
   10367                 :            : 
   10368                 :          0 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
   10369                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
   10370                 :            : 
   10371                 :          0 :         sig_change->center_frq1 = -1;
   10372                 :          0 :         sig_change->center_frq2 = -1;
   10373                 :          0 :         sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
   10374                 :            : 
   10375         [ #  # ]:          0 :         if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
   10376                 :          0 :                 sig_change->chanwidth = convert2width(
   10377                 :          0 :                         nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
   10378         [ #  # ]:          0 :                 if (tb[NL80211_ATTR_CENTER_FREQ1])
   10379                 :          0 :                         sig_change->center_frq1 =
   10380                 :          0 :                                 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
   10381         [ #  # ]:          0 :                 if (tb[NL80211_ATTR_CENTER_FREQ2])
   10382                 :          0 :                         sig_change->center_frq2 =
   10383                 :          0 :                                 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
   10384                 :            :         }
   10385                 :            : 
   10386                 :          0 :         return NL_SKIP;
   10387                 :            : }
   10388                 :            : 
   10389                 :            : 
   10390                 :          0 : static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
   10391                 :            :                                      struct wpa_signal_info *sig)
   10392                 :            : {
   10393                 :            :         struct nl_msg *msg;
   10394                 :            : 
   10395                 :          0 :         msg = nlmsg_alloc();
   10396         [ #  # ]:          0 :         if (!msg)
   10397                 :          0 :                 return -ENOMEM;
   10398                 :            : 
   10399                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
   10400         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   10401                 :            : 
   10402                 :          0 :         return send_and_recv_msgs(drv, msg, get_channel_width, sig);
   10403                 :            : 
   10404                 :            : nla_put_failure:
   10405                 :          0 :         nlmsg_free(msg);
   10406                 :          0 :         return -ENOBUFS;
   10407                 :            : }
   10408                 :            : 
   10409                 :            : 
   10410                 :          0 : static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
   10411                 :            : {
   10412                 :          0 :         struct i802_bss *bss = priv;
   10413                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10414                 :            :         int res;
   10415                 :            : 
   10416                 :          0 :         os_memset(si, 0, sizeof(*si));
   10417                 :          0 :         res = nl80211_get_link_signal(drv, si);
   10418         [ #  # ]:          0 :         if (res != 0)
   10419                 :          0 :                 return res;
   10420                 :            : 
   10421                 :          0 :         res = nl80211_get_channel_width(drv, si);
   10422         [ #  # ]:          0 :         if (res != 0)
   10423                 :          0 :                 return res;
   10424                 :            : 
   10425                 :          0 :         return nl80211_get_link_noise(drv, si);
   10426                 :            : }
   10427                 :            : 
   10428                 :            : 
   10429                 :          9 : static int wpa_driver_nl80211_shared_freq(void *priv)
   10430                 :            : {
   10431                 :          9 :         struct i802_bss *bss = priv;
   10432                 :          9 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10433                 :            :         struct wpa_driver_nl80211_data *driver;
   10434                 :          9 :         int freq = 0;
   10435                 :            : 
   10436                 :            :         /*
   10437                 :            :          * If the same PHY is in connected state with some other interface,
   10438                 :            :          * then retrieve the assoc freq.
   10439                 :            :          */
   10440                 :          9 :         wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
   10441                 :          9 :                    drv->phyname);
   10442                 :            : 
   10443         [ +  + ]:         18 :         dl_list_for_each(driver, &drv->global->interfaces,
   10444                 :            :                          struct wpa_driver_nl80211_data, list) {
   10445 [ -  + ][ #  # ]:          9 :                 if (drv == driver ||
   10446         [ #  # ]:          0 :                     os_strcmp(drv->phyname, driver->phyname) != 0 ||
   10447                 :          0 :                     !driver->associated)
   10448                 :          9 :                         continue;
   10449                 :            : 
   10450                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
   10451                 :            :                            MACSTR,
   10452                 :          0 :                            driver->phyname, driver->first_bss->ifname,
   10453                 :          0 :                            MAC2STR(driver->first_bss->addr));
   10454         [ #  # ]:          0 :                 if (is_ap_interface(driver->nlmode))
   10455                 :          0 :                         freq = driver->first_bss->freq;
   10456                 :            :                 else
   10457                 :          0 :                         freq = nl80211_get_assoc_freq(driver);
   10458                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
   10459                 :          0 :                            drv->phyname, freq);
   10460                 :            :         }
   10461                 :            : 
   10462         [ +  - ]:          9 :         if (!freq)
   10463                 :          9 :                 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
   10464                 :          9 :                            "PHY (%s) in associated state", drv->phyname);
   10465                 :            : 
   10466                 :          9 :         return freq;
   10467                 :            : }
   10468                 :            : 
   10469                 :            : 
   10470                 :          2 : static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
   10471                 :            :                               int encrypt)
   10472                 :            : {
   10473                 :          2 :         struct i802_bss *bss = priv;
   10474                 :          2 :         return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
   10475                 :            :                                              0, 0, 0, 0);
   10476                 :            : }
   10477                 :            : 
   10478                 :            : 
   10479                 :         39 : static int nl80211_set_param(void *priv, const char *param)
   10480                 :            : {
   10481                 :         39 :         wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
   10482         [ +  + ]:         39 :         if (param == NULL)
   10483                 :         27 :                 return 0;
   10484                 :            : 
   10485                 :            : #ifdef CONFIG_P2P
   10486         [ -  + ]:         12 :         if (os_strstr(param, "use_p2p_group_interface=1")) {
   10487                 :          0 :                 struct i802_bss *bss = priv;
   10488                 :          0 :                 struct wpa_driver_nl80211_data *drv = bss->drv;
   10489                 :            : 
   10490                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
   10491                 :            :                            "interface");
   10492                 :          0 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
   10493                 :          0 :                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
   10494                 :            :         }
   10495                 :            : 
   10496         [ -  + ]:         12 :         if (os_strstr(param, "p2p_device=1")) {
   10497                 :          0 :                 struct i802_bss *bss = priv;
   10498                 :          0 :                 struct wpa_driver_nl80211_data *drv = bss->drv;
   10499                 :          0 :                 drv->allow_p2p_device = 1;
   10500                 :            :         }
   10501                 :            : #endif /* CONFIG_P2P */
   10502                 :            : 
   10503         [ +  + ]:         12 :         if (os_strstr(param, "use_monitor=1")) {
   10504                 :          2 :                 struct i802_bss *bss = priv;
   10505                 :          2 :                 struct wpa_driver_nl80211_data *drv = bss->drv;
   10506                 :          2 :                 drv->use_monitor = 1;
   10507                 :            :         }
   10508                 :            : 
   10509         [ +  + ]:         12 :         if (os_strstr(param, "force_connect_cmd=1")) {
   10510                 :          5 :                 struct i802_bss *bss = priv;
   10511                 :          5 :                 struct wpa_driver_nl80211_data *drv = bss->drv;
   10512                 :          5 :                 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
   10513                 :            :         }
   10514                 :            : 
   10515         [ +  + ]:         12 :         if (os_strstr(param, "no_offchannel_tx=1")) {
   10516                 :          5 :                 struct i802_bss *bss = priv;
   10517                 :          5 :                 struct wpa_driver_nl80211_data *drv = bss->drv;
   10518                 :          5 :                 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
   10519                 :          5 :                 drv->test_use_roc_tx = 1;
   10520                 :            :         }
   10521                 :            : 
   10522                 :         39 :         return 0;
   10523                 :            : }
   10524                 :            : 
   10525                 :            : 
   10526                 :          5 : static void * nl80211_global_init(void)
   10527                 :            : {
   10528                 :            :         struct nl80211_global *global;
   10529                 :            :         struct netlink_config *cfg;
   10530                 :            : 
   10531                 :          5 :         global = os_zalloc(sizeof(*global));
   10532         [ -  + ]:          5 :         if (global == NULL)
   10533                 :          0 :                 return NULL;
   10534                 :          5 :         global->ioctl_sock = -1;
   10535                 :          5 :         dl_list_init(&global->interfaces);
   10536                 :          5 :         global->if_add_ifindex = -1;
   10537                 :            : 
   10538                 :          5 :         cfg = os_zalloc(sizeof(*cfg));
   10539         [ -  + ]:          5 :         if (cfg == NULL)
   10540                 :          0 :                 goto err;
   10541                 :            : 
   10542                 :          5 :         cfg->ctx = global;
   10543                 :          5 :         cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
   10544                 :          5 :         cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
   10545                 :          5 :         global->netlink = netlink_init(cfg);
   10546         [ -  + ]:          5 :         if (global->netlink == NULL) {
   10547                 :          0 :                 os_free(cfg);
   10548                 :          0 :                 goto err;
   10549                 :            :         }
   10550                 :            : 
   10551         [ -  + ]:          5 :         if (wpa_driver_nl80211_init_nl_global(global) < 0)
   10552                 :          0 :                 goto err;
   10553                 :            : 
   10554                 :          5 :         global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
   10555         [ -  + ]:          5 :         if (global->ioctl_sock < 0) {
   10556                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
   10557                 :          0 :                            strerror(errno));
   10558                 :          0 :                 goto err;
   10559                 :            :         }
   10560                 :            : 
   10561                 :          5 :         return global;
   10562                 :            : 
   10563                 :            : err:
   10564                 :          0 :         nl80211_global_deinit(global);
   10565                 :          5 :         return NULL;
   10566                 :            : }
   10567                 :            : 
   10568                 :            : 
   10569                 :          5 : static void nl80211_global_deinit(void *priv)
   10570                 :            : {
   10571                 :          5 :         struct nl80211_global *global = priv;
   10572         [ -  + ]:          5 :         if (global == NULL)
   10573                 :          5 :                 return;
   10574         [ -  + ]:          5 :         if (!dl_list_empty(&global->interfaces)) {
   10575                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
   10576                 :            :                            "nl80211_global_deinit",
   10577                 :            :                            dl_list_len(&global->interfaces));
   10578                 :            :         }
   10579                 :            : 
   10580         [ +  - ]:          5 :         if (global->netlink)
   10581                 :          5 :                 netlink_deinit(global->netlink);
   10582                 :            : 
   10583                 :          5 :         nl_destroy_handles(&global->nl);
   10584                 :            : 
   10585         [ +  - ]:          5 :         if (global->nl_event)
   10586                 :          5 :                 nl80211_destroy_eloop_handle(&global->nl_event);
   10587                 :            : 
   10588                 :          5 :         nl_cb_put(global->nl_cb);
   10589                 :            : 
   10590         [ +  - ]:          5 :         if (global->ioctl_sock >= 0)
   10591                 :          5 :                 close(global->ioctl_sock);
   10592                 :            : 
   10593                 :          5 :         os_free(global);
   10594                 :            : }
   10595                 :            : 
   10596                 :            : 
   10597                 :        450 : static const char * nl80211_get_radio_name(void *priv)
   10598                 :            : {
   10599                 :        450 :         struct i802_bss *bss = priv;
   10600                 :        450 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10601                 :        450 :         return drv->phyname;
   10602                 :            : }
   10603                 :            : 
   10604                 :            : 
   10605                 :        383 : static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
   10606                 :            :                          const u8 *pmkid)
   10607                 :            : {
   10608                 :            :         struct nl_msg *msg;
   10609                 :            : 
   10610                 :        383 :         msg = nlmsg_alloc();
   10611         [ -  + ]:        383 :         if (!msg)
   10612                 :          0 :                 return -ENOMEM;
   10613                 :            : 
   10614                 :        383 :         nl80211_cmd(bss->drv, msg, 0, cmd);
   10615                 :            : 
   10616         [ +  - ]:        383 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
   10617         [ +  + ]:        383 :         if (pmkid)
   10618         [ +  - ]:        344 :                 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
   10619         [ +  + ]:        383 :         if (bssid)
   10620         [ +  - ]:        344 :                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
   10621                 :            : 
   10622                 :        383 :         return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
   10623                 :            :  nla_put_failure:
   10624                 :          0 :         nlmsg_free(msg);
   10625                 :        383 :         return -ENOBUFS;
   10626                 :            : }
   10627                 :            : 
   10628                 :            : 
   10629                 :        172 : static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
   10630                 :            : {
   10631                 :        172 :         struct i802_bss *bss = priv;
   10632                 :        172 :         wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
   10633                 :        172 :         return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
   10634                 :            : }
   10635                 :            : 
   10636                 :            : 
   10637                 :        172 : static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
   10638                 :            : {
   10639                 :        172 :         struct i802_bss *bss = priv;
   10640                 :        172 :         wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
   10641                 :       1032 :                    MAC2STR(bssid));
   10642                 :        172 :         return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
   10643                 :            : }
   10644                 :            : 
   10645                 :            : 
   10646                 :         39 : static int nl80211_flush_pmkid(void *priv)
   10647                 :            : {
   10648                 :         39 :         struct i802_bss *bss = priv;
   10649                 :         39 :         wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
   10650                 :         39 :         return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
   10651                 :            : }
   10652                 :            : 
   10653                 :            : 
   10654                 :         15 : static void clean_survey_results(struct survey_results *survey_results)
   10655                 :            : {
   10656                 :            :         struct freq_survey *survey, *tmp;
   10657                 :            : 
   10658         [ +  - ]:         15 :         if (dl_list_empty(&survey_results->survey_list))
   10659                 :         15 :                 return;
   10660                 :            : 
   10661         [ #  # ]:          0 :         dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
   10662                 :            :                               struct freq_survey, list) {
   10663                 :          0 :                 dl_list_del(&survey->list);
   10664                 :          0 :                 os_free(survey);
   10665                 :            :         }
   10666                 :            : }
   10667                 :            : 
   10668                 :            : 
   10669                 :         15 : static void add_survey(struct nlattr **sinfo, u32 ifidx,
   10670                 :            :                        struct dl_list *survey_list)
   10671                 :            : {
   10672                 :            :         struct freq_survey *survey;
   10673                 :            : 
   10674                 :         15 :         survey = os_zalloc(sizeof(struct freq_survey));
   10675         [ -  + ]:         15 :         if  (!survey)
   10676                 :         15 :                 return;
   10677                 :            : 
   10678                 :         15 :         survey->ifidx = ifidx;
   10679                 :         15 :         survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
   10680                 :         15 :         survey->filled = 0;
   10681                 :            : 
   10682         [ +  - ]:         15 :         if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
   10683                 :         15 :                 survey->nf = (int8_t)
   10684                 :         15 :                         nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
   10685                 :         15 :                 survey->filled |= SURVEY_HAS_NF;
   10686                 :            :         }
   10687                 :            : 
   10688         [ -  + ]:         15 :         if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
   10689                 :          0 :                 survey->channel_time =
   10690                 :          0 :                         nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
   10691                 :          0 :                 survey->filled |= SURVEY_HAS_CHAN_TIME;
   10692                 :            :         }
   10693                 :            : 
   10694         [ -  + ]:         15 :         if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
   10695                 :          0 :                 survey->channel_time_busy =
   10696                 :          0 :                         nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
   10697                 :          0 :                 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
   10698                 :            :         }
   10699                 :            : 
   10700         [ -  + ]:         15 :         if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
   10701                 :          0 :                 survey->channel_time_rx =
   10702                 :          0 :                         nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
   10703                 :          0 :                 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
   10704                 :            :         }
   10705                 :            : 
   10706         [ -  + ]:         15 :         if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
   10707                 :          0 :                 survey->channel_time_tx =
   10708                 :          0 :                         nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
   10709                 :          0 :                 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
   10710                 :            :         }
   10711                 :            : 
   10712                 :         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)",
   10713                 :            :                    survey->freq,
   10714                 :         15 :                    survey->nf,
   10715                 :            :                    (unsigned long int) survey->channel_time,
   10716                 :            :                    (unsigned long int) survey->channel_time_busy,
   10717                 :            :                    (unsigned long int) survey->channel_time_tx,
   10718                 :            :                    (unsigned long int) survey->channel_time_rx,
   10719                 :            :                    survey->filled);
   10720                 :            : 
   10721                 :         15 :         dl_list_add_tail(survey_list, &survey->list);
   10722                 :            : }
   10723                 :            : 
   10724                 :            : 
   10725                 :         15 : static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
   10726                 :            :                            unsigned int freq_filter)
   10727                 :            : {
   10728         [ +  - ]:         15 :         if (!freq_filter)
   10729                 :         15 :                 return 1;
   10730                 :            : 
   10731                 :         15 :         return freq_filter == surveyed_freq;
   10732                 :            : }
   10733                 :            : 
   10734                 :            : 
   10735                 :         15 : static int survey_handler(struct nl_msg *msg, void *arg)
   10736                 :            : {
   10737                 :            :         struct nlattr *tb[NL80211_ATTR_MAX + 1];
   10738                 :         15 :         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
   10739                 :            :         struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
   10740                 :            :         struct survey_results *survey_results;
   10741                 :         15 :         u32 surveyed_freq = 0;
   10742                 :            :         u32 ifidx;
   10743                 :            : 
   10744                 :            :         static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
   10745                 :            :                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
   10746                 :            :                 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
   10747                 :            :         };
   10748                 :            : 
   10749                 :         15 :         survey_results = (struct survey_results *) arg;
   10750                 :            : 
   10751                 :         15 :         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
   10752                 :            :                   genlmsg_attrlen(gnlh, 0), NULL);
   10753                 :            : 
   10754         [ -  + ]:         15 :         if (!tb[NL80211_ATTR_IFINDEX])
   10755                 :          0 :                 return NL_SKIP;
   10756                 :            : 
   10757                 :         15 :         ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
   10758                 :            : 
   10759         [ -  + ]:         15 :         if (!tb[NL80211_ATTR_SURVEY_INFO])
   10760                 :          0 :                 return NL_SKIP;
   10761                 :            : 
   10762         [ -  + ]:         15 :         if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
   10763                 :            :                              tb[NL80211_ATTR_SURVEY_INFO],
   10764                 :            :                              survey_policy))
   10765                 :          0 :                 return NL_SKIP;
   10766                 :            : 
   10767         [ -  + ]:         15 :         if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
   10768                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
   10769                 :          0 :                 return NL_SKIP;
   10770                 :            :         }
   10771                 :            : 
   10772                 :         15 :         surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
   10773                 :            : 
   10774         [ -  + ]:         15 :         if (!check_survey_ok(sinfo, surveyed_freq,
   10775                 :            :                              survey_results->freq_filter))
   10776                 :          0 :                 return NL_SKIP;
   10777                 :            : 
   10778 [ -  + ][ #  # ]:         15 :         if (survey_results->freq_filter &&
   10779                 :          0 :             survey_results->freq_filter != surveyed_freq) {
   10780                 :          0 :                 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
   10781                 :            :                            surveyed_freq);
   10782                 :          0 :                 return NL_SKIP;
   10783                 :            :         }
   10784                 :            : 
   10785                 :         15 :         add_survey(sinfo, ifidx, &survey_results->survey_list);
   10786                 :            : 
   10787                 :         15 :         return NL_SKIP;
   10788                 :            : }
   10789                 :            : 
   10790                 :            : 
   10791                 :         15 : static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
   10792                 :            : {
   10793                 :         15 :         struct i802_bss *bss = priv;
   10794                 :         15 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10795                 :            :         struct nl_msg *msg;
   10796                 :         15 :         int err = -ENOBUFS;
   10797                 :            :         union wpa_event_data data;
   10798                 :            :         struct survey_results *survey_results;
   10799                 :            : 
   10800                 :         15 :         os_memset(&data, 0, sizeof(data));
   10801                 :         15 :         survey_results = &data.survey_results;
   10802                 :            : 
   10803                 :         15 :         dl_list_init(&survey_results->survey_list);
   10804                 :            : 
   10805                 :         15 :         msg = nlmsg_alloc();
   10806         [ -  + ]:         15 :         if (!msg)
   10807                 :          0 :                 goto nla_put_failure;
   10808                 :            : 
   10809                 :         15 :         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
   10810                 :            : 
   10811         [ +  - ]:         15 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   10812                 :            : 
   10813         [ -  + ]:         15 :         if (freq)
   10814                 :          0 :                 data.survey_results.freq_filter = freq;
   10815                 :            : 
   10816                 :            :         do {
   10817                 :         15 :                 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
   10818                 :         15 :                 err = send_and_recv_msgs(drv, msg, survey_handler,
   10819                 :            :                                          survey_results);
   10820         [ -  + ]:         15 :         } while (err > 0);
   10821                 :            : 
   10822         [ -  + ]:         15 :         if (err) {
   10823                 :          0 :                 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
   10824                 :          0 :                 goto out_clean;
   10825                 :            :         }
   10826                 :            : 
   10827                 :         15 :         wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
   10828                 :            : 
   10829                 :            : out_clean:
   10830                 :         15 :         clean_survey_results(survey_results);
   10831                 :            : nla_put_failure:
   10832                 :         15 :         return err;
   10833                 :            : }
   10834                 :            : 
   10835                 :            : 
   10836                 :        403 : static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
   10837                 :            :                                    const u8 *replay_ctr)
   10838                 :            : {
   10839                 :        403 :         struct i802_bss *bss = priv;
   10840                 :        403 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10841                 :            :         struct nlattr *replay_nested;
   10842                 :            :         struct nl_msg *msg;
   10843                 :            : 
   10844                 :        403 :         msg = nlmsg_alloc();
   10845         [ -  + ]:        403 :         if (!msg)
   10846                 :          0 :                 return;
   10847                 :            : 
   10848                 :        403 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
   10849                 :            : 
   10850         [ +  - ]:        403 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
   10851                 :            : 
   10852                 :        403 :         replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
   10853         [ -  + ]:        403 :         if (!replay_nested)
   10854                 :          0 :                 goto nla_put_failure;
   10855                 :            : 
   10856         [ +  - ]:        403 :         NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
   10857         [ +  - ]:        403 :         NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
   10858         [ +  - ]:        403 :         NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
   10859                 :            :                 replay_ctr);
   10860                 :            : 
   10861                 :        403 :         nla_nest_end(msg, replay_nested);
   10862                 :            : 
   10863                 :        403 :         send_and_recv_msgs(drv, msg, NULL, NULL);
   10864                 :        403 :         return;
   10865                 :            :  nla_put_failure:
   10866                 :        403 :         nlmsg_free(msg);
   10867                 :            : }
   10868                 :            : 
   10869                 :            : 
   10870                 :          0 : static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
   10871                 :            :                                     const u8 *addr, int qos)
   10872                 :            : {
   10873                 :            :         /* send data frame to poll STA and check whether
   10874                 :            :          * this frame is ACKed */
   10875                 :            :         struct {
   10876                 :            :                 struct ieee80211_hdr hdr;
   10877                 :            :                 u16 qos_ctl;
   10878                 :            :         } STRUCT_PACKED nulldata;
   10879                 :            :         size_t size;
   10880                 :            : 
   10881                 :            :         /* Send data frame to poll STA and check whether this frame is ACKed */
   10882                 :            : 
   10883                 :          0 :         os_memset(&nulldata, 0, sizeof(nulldata));
   10884                 :            : 
   10885         [ #  # ]:          0 :         if (qos) {
   10886                 :          0 :                 nulldata.hdr.frame_control =
   10887                 :            :                         IEEE80211_FC(WLAN_FC_TYPE_DATA,
   10888                 :            :                                      WLAN_FC_STYPE_QOS_NULL);
   10889                 :          0 :                 size = sizeof(nulldata);
   10890                 :            :         } else {
   10891                 :          0 :                 nulldata.hdr.frame_control =
   10892                 :            :                         IEEE80211_FC(WLAN_FC_TYPE_DATA,
   10893                 :            :                                      WLAN_FC_STYPE_NULLFUNC);
   10894                 :          0 :                 size = sizeof(struct ieee80211_hdr);
   10895                 :            :         }
   10896                 :            : 
   10897                 :          0 :         nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
   10898                 :          0 :         os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
   10899                 :          0 :         os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
   10900                 :          0 :         os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
   10901                 :            : 
   10902         [ #  # ]:          0 :         if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
   10903                 :            :                                          0, 0) < 0)
   10904                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
   10905                 :            :                            "send poll frame");
   10906                 :          0 : }
   10907                 :            : 
   10908                 :          0 : static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
   10909                 :            :                                 int qos)
   10910                 :            : {
   10911                 :          0 :         struct i802_bss *bss = priv;
   10912                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10913                 :            :         struct nl_msg *msg;
   10914                 :            : 
   10915         [ #  # ]:          0 :         if (!drv->poll_command_supported) {
   10916                 :          0 :                 nl80211_send_null_frame(bss, own_addr, addr, qos);
   10917                 :          0 :                 return;
   10918                 :            :         }
   10919                 :            : 
   10920                 :          0 :         msg = nlmsg_alloc();
   10921         [ #  # ]:          0 :         if (!msg)
   10922                 :          0 :                 return;
   10923                 :            : 
   10924                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
   10925                 :            : 
   10926         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
   10927         [ #  # ]:          0 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
   10928                 :            : 
   10929                 :          0 :         send_and_recv_msgs(drv, msg, NULL, NULL);
   10930                 :          0 :         return;
   10931                 :            :  nla_put_failure:
   10932                 :          0 :         nlmsg_free(msg);
   10933                 :            : }
   10934                 :            : 
   10935                 :            : 
   10936                 :          0 : static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
   10937                 :            : {
   10938                 :            :         struct nl_msg *msg;
   10939                 :            : 
   10940                 :          0 :         msg = nlmsg_alloc();
   10941         [ #  # ]:          0 :         if (!msg)
   10942                 :          0 :                 return -ENOMEM;
   10943                 :            : 
   10944                 :          0 :         nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
   10945         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
   10946         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
   10947                 :            :                     enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
   10948                 :          0 :         return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
   10949                 :            : nla_put_failure:
   10950                 :          0 :         nlmsg_free(msg);
   10951                 :          0 :         return -ENOBUFS;
   10952                 :            : }
   10953                 :            : 
   10954                 :            : 
   10955                 :          0 : static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
   10956                 :            :                                      int ctwindow)
   10957                 :            : {
   10958                 :          0 :         struct i802_bss *bss = priv;
   10959                 :            : 
   10960                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
   10961                 :            :                    "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
   10962                 :            : 
   10963 [ #  # ][ #  # ]:          0 :         if (opp_ps != -1 || ctwindow != -1) {
   10964                 :            : #ifdef ANDROID_P2P
   10965                 :            :                 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
   10966                 :            : #else /* ANDROID_P2P */
   10967                 :          0 :                 return -1; /* Not yet supported */
   10968                 :            : #endif /* ANDROID_P2P */
   10969                 :            :         }
   10970                 :            : 
   10971         [ #  # ]:          0 :         if (legacy_ps == -1)
   10972                 :          0 :                 return 0;
   10973 [ #  # ][ #  # ]:          0 :         if (legacy_ps != 0 && legacy_ps != 1)
   10974                 :          0 :                 return -1; /* Not yet supported */
   10975                 :            : 
   10976                 :          0 :         return nl80211_set_power_save(bss, legacy_ps);
   10977                 :            : }
   10978                 :            : 
   10979                 :            : 
   10980                 :          0 : static int nl80211_start_radar_detection(void *priv,
   10981                 :            :                                          struct hostapd_freq_params *freq)
   10982                 :            : {
   10983                 :          0 :         struct i802_bss *bss = priv;
   10984                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   10985                 :            :         struct nl_msg *msg;
   10986                 :            :         int ret;
   10987                 :            : 
   10988                 :          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)",
   10989                 :            :                    freq->freq, freq->ht_enabled, freq->vht_enabled,
   10990                 :            :                    freq->bandwidth, freq->center_freq1, freq->center_freq2);
   10991                 :            : 
   10992         [ #  # ]:          0 :         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
   10993                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
   10994                 :            :                            "detection");
   10995                 :          0 :                 return -1;
   10996                 :            :         }
   10997                 :            : 
   10998                 :          0 :         msg = nlmsg_alloc();
   10999         [ #  # ]:          0 :         if (!msg)
   11000                 :          0 :                 return -1;
   11001                 :            : 
   11002                 :          0 :         nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
   11003         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   11004         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
   11005                 :            : 
   11006         [ #  # ]:          0 :         if (freq->vht_enabled) {
   11007   [ #  #  #  #  :          0 :                 switch (freq->bandwidth) {
                      # ]
   11008                 :            :                 case 20:
   11009         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
   11010                 :            :                                     NL80211_CHAN_WIDTH_20);
   11011                 :          0 :                         break;
   11012                 :            :                 case 40:
   11013         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
   11014                 :            :                                     NL80211_CHAN_WIDTH_40);
   11015                 :          0 :                         break;
   11016                 :            :                 case 80:
   11017         [ #  # ]:          0 :                         if (freq->center_freq2)
   11018         [ #  # ]:          0 :                                 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
   11019                 :            :                                             NL80211_CHAN_WIDTH_80P80);
   11020                 :            :                         else
   11021         [ #  # ]:          0 :                                 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
   11022                 :            :                                             NL80211_CHAN_WIDTH_80);
   11023                 :          0 :                         break;
   11024                 :            :                 case 160:
   11025         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
   11026                 :            :                                     NL80211_CHAN_WIDTH_160);
   11027                 :          0 :                         break;
   11028                 :            :                 default:
   11029                 :          0 :                         return -1;
   11030                 :            :                 }
   11031         [ #  # ]:          0 :                 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
   11032         [ #  # ]:          0 :                 if (freq->center_freq2)
   11033         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
   11034                 :            :                                     freq->center_freq2);
   11035         [ #  # ]:          0 :         } else if (freq->ht_enabled) {
   11036      [ #  #  # ]:          0 :                 switch (freq->sec_channel_offset) {
   11037                 :            :                 case -1:
   11038         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
   11039                 :            :                                     NL80211_CHAN_HT40MINUS);
   11040                 :          0 :                         break;
   11041                 :            :                 case 1:
   11042         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
   11043                 :            :                                     NL80211_CHAN_HT40PLUS);
   11044                 :          0 :                         break;
   11045                 :            :                 default:
   11046         [ #  # ]:          0 :                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
   11047                 :            :                                     NL80211_CHAN_HT20);
   11048                 :          0 :                         break;
   11049                 :            :                 }
   11050                 :            :         }
   11051                 :            : 
   11052                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   11053         [ #  # ]:          0 :         if (ret == 0)
   11054                 :          0 :                 return 0;
   11055                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
   11056                 :            :                    "%d (%s)", ret, strerror(-ret));
   11057                 :            : nla_put_failure:
   11058                 :          0 :         return -1;
   11059                 :            : }
   11060                 :            : 
   11061                 :            : #ifdef CONFIG_TDLS
   11062                 :            : 
   11063                 :        106 : static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
   11064                 :            :                                   u8 dialog_token, u16 status_code,
   11065                 :            :                                   const u8 *buf, size_t len)
   11066                 :            : {
   11067                 :        106 :         struct i802_bss *bss = priv;
   11068                 :        106 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11069                 :            :         struct nl_msg *msg;
   11070                 :            : 
   11071         [ -  + ]:        106 :         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
   11072                 :          0 :                 return -EOPNOTSUPP;
   11073                 :            : 
   11074         [ -  + ]:        106 :         if (!dst)
   11075                 :          0 :                 return -EINVAL;
   11076                 :            : 
   11077                 :        106 :         msg = nlmsg_alloc();
   11078         [ -  + ]:        106 :         if (!msg)
   11079                 :          0 :                 return -ENOMEM;
   11080                 :            : 
   11081                 :        106 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
   11082         [ +  - ]:        106 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   11083         [ +  - ]:        106 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
   11084         [ +  - ]:        106 :         NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
   11085         [ +  - ]:        106 :         NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
   11086         [ +  - ]:        106 :         NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
   11087         [ +  - ]:        106 :         NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
   11088                 :            : 
   11089                 :        106 :         return send_and_recv_msgs(drv, msg, NULL, NULL);
   11090                 :            : 
   11091                 :            : nla_put_failure:
   11092                 :          0 :         nlmsg_free(msg);
   11093                 :        106 :         return -ENOBUFS;
   11094                 :            : }
   11095                 :            : 
   11096                 :            : 
   11097                 :       1281 : static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
   11098                 :            : {
   11099                 :       1281 :         struct i802_bss *bss = priv;
   11100                 :       1281 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11101                 :            :         struct nl_msg *msg;
   11102                 :            :         enum nl80211_tdls_operation nl80211_oper;
   11103                 :            : 
   11104         [ -  + ]:       1281 :         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
   11105                 :          0 :                 return -EOPNOTSUPP;
   11106                 :            : 
   11107   [ -  -  -  +  :       1281 :         switch (oper) {
             +  +  -  - ]
   11108                 :            :         case TDLS_DISCOVERY_REQ:
   11109                 :          0 :                 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
   11110                 :          0 :                 break;
   11111                 :            :         case TDLS_SETUP:
   11112                 :          0 :                 nl80211_oper = NL80211_TDLS_SETUP;
   11113                 :          0 :                 break;
   11114                 :            :         case TDLS_TEARDOWN:
   11115                 :          0 :                 nl80211_oper = NL80211_TDLS_TEARDOWN;
   11116                 :          0 :                 break;
   11117                 :            :         case TDLS_ENABLE_LINK:
   11118                 :         38 :                 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
   11119                 :         38 :                 break;
   11120                 :            :         case TDLS_DISABLE_LINK:
   11121                 :         60 :                 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
   11122                 :         60 :                 break;
   11123                 :            :         case TDLS_ENABLE:
   11124                 :       1183 :                 return 0;
   11125                 :            :         case TDLS_DISABLE:
   11126                 :          0 :                 return 0;
   11127                 :            :         default:
   11128                 :          0 :                 return -EINVAL;
   11129                 :            :         }
   11130                 :            : 
   11131                 :         98 :         msg = nlmsg_alloc();
   11132         [ -  + ]:         98 :         if (!msg)
   11133                 :          0 :                 return -ENOMEM;
   11134                 :            : 
   11135                 :         98 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
   11136         [ +  - ]:         98 :         NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
   11137         [ +  - ]:         98 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   11138         [ +  - ]:         98 :         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
   11139                 :            : 
   11140                 :         98 :         return send_and_recv_msgs(drv, msg, NULL, NULL);
   11141                 :            : 
   11142                 :            : nla_put_failure:
   11143                 :          0 :         nlmsg_free(msg);
   11144                 :       1281 :         return -ENOBUFS;
   11145                 :            : }
   11146                 :            : 
   11147                 :            : #endif /* CONFIG TDLS */
   11148                 :            : 
   11149                 :            : 
   11150                 :            : #ifdef ANDROID
   11151                 :            : 
   11152                 :            : typedef struct android_wifi_priv_cmd {
   11153                 :            :         char *buf;
   11154                 :            :         int used_len;
   11155                 :            :         int total_len;
   11156                 :            : } android_wifi_priv_cmd;
   11157                 :            : 
   11158                 :            : static int drv_errors = 0;
   11159                 :            : 
   11160                 :            : static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
   11161                 :            : {
   11162                 :            :         drv_errors++;
   11163                 :            :         if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
   11164                 :            :                 drv_errors = 0;
   11165                 :            :                 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
   11166                 :            :         }
   11167                 :            : }
   11168                 :            : 
   11169                 :            : 
   11170                 :            : static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
   11171                 :            : {
   11172                 :            :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11173                 :            :         struct ifreq ifr;
   11174                 :            :         android_wifi_priv_cmd priv_cmd;
   11175                 :            :         char buf[MAX_DRV_CMD_SIZE];
   11176                 :            :         int ret;
   11177                 :            : 
   11178                 :            :         os_memset(&ifr, 0, sizeof(ifr));
   11179                 :            :         os_memset(&priv_cmd, 0, sizeof(priv_cmd));
   11180                 :            :         os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
   11181                 :            : 
   11182                 :            :         os_memset(buf, 0, sizeof(buf));
   11183                 :            :         os_strlcpy(buf, cmd, sizeof(buf));
   11184                 :            : 
   11185                 :            :         priv_cmd.buf = buf;
   11186                 :            :         priv_cmd.used_len = sizeof(buf);
   11187                 :            :         priv_cmd.total_len = sizeof(buf);
   11188                 :            :         ifr.ifr_data = &priv_cmd;
   11189                 :            : 
   11190                 :            :         ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
   11191                 :            :         if (ret < 0) {
   11192                 :            :                 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
   11193                 :            :                            __func__);
   11194                 :            :                 wpa_driver_send_hang_msg(drv);
   11195                 :            :                 return ret;
   11196                 :            :         }
   11197                 :            : 
   11198                 :            :         drv_errors = 0;
   11199                 :            :         return 0;
   11200                 :            : }
   11201                 :            : 
   11202                 :            : 
   11203                 :            : static int android_pno_start(struct i802_bss *bss,
   11204                 :            :                              struct wpa_driver_scan_params *params)
   11205                 :            : {
   11206                 :            :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11207                 :            :         struct ifreq ifr;
   11208                 :            :         android_wifi_priv_cmd priv_cmd;
   11209                 :            :         int ret = 0, i = 0, bp;
   11210                 :            :         char buf[WEXT_PNO_MAX_COMMAND_SIZE];
   11211                 :            : 
   11212                 :            :         bp = WEXT_PNOSETUP_HEADER_SIZE;
   11213                 :            :         os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
   11214                 :            :         buf[bp++] = WEXT_PNO_TLV_PREFIX;
   11215                 :            :         buf[bp++] = WEXT_PNO_TLV_VERSION;
   11216                 :            :         buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
   11217                 :            :         buf[bp++] = WEXT_PNO_TLV_RESERVED;
   11218                 :            : 
   11219                 :            :         while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
   11220                 :            :                 /* Check that there is enough space needed for 1 more SSID, the
   11221                 :            :                  * other sections and null termination */
   11222                 :            :                 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
   11223                 :            :                      WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
   11224                 :            :                         break;
   11225                 :            :                 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
   11226                 :            :                                   params->ssids[i].ssid,
   11227                 :            :                                   params->ssids[i].ssid_len);
   11228                 :            :                 buf[bp++] = WEXT_PNO_SSID_SECTION;
   11229                 :            :                 buf[bp++] = params->ssids[i].ssid_len;
   11230                 :            :                 os_memcpy(&buf[bp], params->ssids[i].ssid,
   11231                 :            :                           params->ssids[i].ssid_len);
   11232                 :            :                 bp += params->ssids[i].ssid_len;
   11233                 :            :                 i++;
   11234                 :            :         }
   11235                 :            : 
   11236                 :            :         buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
   11237                 :            :         os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
   11238                 :            :                     WEXT_PNO_SCAN_INTERVAL);
   11239                 :            :         bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
   11240                 :            : 
   11241                 :            :         buf[bp++] = WEXT_PNO_REPEAT_SECTION;
   11242                 :            :         os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
   11243                 :            :                     WEXT_PNO_REPEAT);
   11244                 :            :         bp += WEXT_PNO_REPEAT_LENGTH;
   11245                 :            : 
   11246                 :            :         buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
   11247                 :            :         os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
   11248                 :            :                     WEXT_PNO_MAX_REPEAT);
   11249                 :            :         bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
   11250                 :            : 
   11251                 :            :         memset(&ifr, 0, sizeof(ifr));
   11252                 :            :         memset(&priv_cmd, 0, sizeof(priv_cmd));
   11253                 :            :         os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
   11254                 :            : 
   11255                 :            :         priv_cmd.buf = buf;
   11256                 :            :         priv_cmd.used_len = bp;
   11257                 :            :         priv_cmd.total_len = bp;
   11258                 :            :         ifr.ifr_data = &priv_cmd;
   11259                 :            : 
   11260                 :            :         ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
   11261                 :            : 
   11262                 :            :         if (ret < 0) {
   11263                 :            :                 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
   11264                 :            :                            ret);
   11265                 :            :                 wpa_driver_send_hang_msg(drv);
   11266                 :            :                 return ret;
   11267                 :            :         }
   11268                 :            : 
   11269                 :            :         drv_errors = 0;
   11270                 :            : 
   11271                 :            :         return android_priv_cmd(bss, "PNOFORCE 1");
   11272                 :            : }
   11273                 :            : 
   11274                 :            : 
   11275                 :            : static int android_pno_stop(struct i802_bss *bss)
   11276                 :            : {
   11277                 :            :         return android_priv_cmd(bss, "PNOFORCE 0");
   11278                 :            : }
   11279                 :            : 
   11280                 :            : #endif /* ANDROID */
   11281                 :            : 
   11282                 :            : 
   11283                 :      10024 : static int driver_nl80211_set_key(const char *ifname, void *priv,
   11284                 :            :                                   enum wpa_alg alg, const u8 *addr,
   11285                 :            :                                   int key_idx, int set_tx,
   11286                 :            :                                   const u8 *seq, size_t seq_len,
   11287                 :            :                                   const u8 *key, size_t key_len)
   11288                 :            : {
   11289                 :      10024 :         struct i802_bss *bss = priv;
   11290                 :      10024 :         return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
   11291                 :            :                                           set_tx, seq, seq_len, key, key_len);
   11292                 :            : }
   11293                 :            : 
   11294                 :            : 
   11295                 :       1381 : static int driver_nl80211_scan2(void *priv,
   11296                 :            :                                 struct wpa_driver_scan_params *params)
   11297                 :            : {
   11298                 :       1381 :         struct i802_bss *bss = priv;
   11299                 :       1381 :         return wpa_driver_nl80211_scan(bss, params);
   11300                 :            : }
   11301                 :            : 
   11302                 :            : 
   11303                 :        633 : static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
   11304                 :            :                                          int reason_code)
   11305                 :            : {
   11306                 :        633 :         struct i802_bss *bss = priv;
   11307                 :        633 :         return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
   11308                 :            : }
   11309                 :            : 
   11310                 :            : 
   11311                 :        708 : static int driver_nl80211_authenticate(void *priv,
   11312                 :            :                                        struct wpa_driver_auth_params *params)
   11313                 :            : {
   11314                 :        708 :         struct i802_bss *bss = priv;
   11315                 :        708 :         return wpa_driver_nl80211_authenticate(bss, params);
   11316                 :            : }
   11317                 :            : 
   11318                 :            : 
   11319                 :         39 : static void driver_nl80211_deinit(void *priv)
   11320                 :            : {
   11321                 :         39 :         struct i802_bss *bss = priv;
   11322                 :         39 :         wpa_driver_nl80211_deinit(bss);
   11323                 :         39 : }
   11324                 :            : 
   11325                 :            : 
   11326                 :         41 : static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
   11327                 :            :                                     const char *ifname)
   11328                 :            : {
   11329                 :         41 :         struct i802_bss *bss = priv;
   11330                 :         41 :         return wpa_driver_nl80211_if_remove(bss, type, ifname);
   11331                 :            : }
   11332                 :            : 
   11333                 :            : 
   11334                 :       3033 : static int driver_nl80211_send_mlme(void *priv, const u8 *data,
   11335                 :            :                                     size_t data_len, int noack)
   11336                 :            : {
   11337                 :       3033 :         struct i802_bss *bss = priv;
   11338                 :       3033 :         return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
   11339                 :            :                                             0, 0, 0, 0);
   11340                 :            : }
   11341                 :            : 
   11342                 :            : 
   11343                 :       1363 : static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
   11344                 :            : {
   11345                 :       1363 :         struct i802_bss *bss = priv;
   11346                 :       1363 :         return wpa_driver_nl80211_sta_remove(bss, addr);
   11347                 :            : }
   11348                 :            : 
   11349                 :            : 
   11350                 :          0 : static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
   11351                 :            :                                        const char *ifname, int vlan_id)
   11352                 :            : {
   11353                 :          0 :         struct i802_bss *bss = priv;
   11354                 :          0 :         return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
   11355                 :            : }
   11356                 :            : 
   11357                 :            : 
   11358                 :         22 : static int driver_nl80211_read_sta_data(void *priv,
   11359                 :            :                                         struct hostap_sta_driver_data *data,
   11360                 :            :                                         const u8 *addr)
   11361                 :            : {
   11362                 :         22 :         struct i802_bss *bss = priv;
   11363                 :         22 :         return i802_read_sta_data(bss, data, addr);
   11364                 :            : }
   11365                 :            : 
   11366                 :            : 
   11367                 :       1085 : static int driver_nl80211_send_action(void *priv, unsigned int freq,
   11368                 :            :                                       unsigned int wait_time,
   11369                 :            :                                       const u8 *dst, const u8 *src,
   11370                 :            :                                       const u8 *bssid,
   11371                 :            :                                       const u8 *data, size_t data_len,
   11372                 :            :                                       int no_cck)
   11373                 :            : {
   11374                 :       1085 :         struct i802_bss *bss = priv;
   11375                 :       1085 :         return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
   11376                 :            :                                               bssid, data, data_len, no_cck);
   11377                 :            : }
   11378                 :            : 
   11379                 :            : 
   11380                 :       4160 : static int driver_nl80211_probe_req_report(void *priv, int report)
   11381                 :            : {
   11382                 :       4160 :         struct i802_bss *bss = priv;
   11383                 :       4160 :         return wpa_driver_nl80211_probe_req_report(bss, report);
   11384                 :            : }
   11385                 :            : 
   11386                 :            : 
   11387                 :          0 : static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
   11388                 :            :                                             const u8 *ies, size_t ies_len)
   11389                 :            : {
   11390                 :            :         int ret;
   11391                 :            :         struct nl_msg *msg;
   11392                 :          0 :         struct i802_bss *bss = priv;
   11393                 :          0 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11394                 :          0 :         u16 mdid = WPA_GET_LE16(md);
   11395                 :            : 
   11396                 :          0 :         msg = nlmsg_alloc();
   11397         [ #  # ]:          0 :         if (!msg)
   11398                 :          0 :                 return -ENOMEM;
   11399                 :            : 
   11400                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
   11401                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
   11402         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   11403         [ #  # ]:          0 :         NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
   11404         [ #  # ]:          0 :         NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
   11405                 :            : 
   11406                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   11407         [ #  # ]:          0 :         if (ret) {
   11408                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
   11409                 :            :                            "err=%d (%s)", ret, strerror(-ret));
   11410                 :            :         }
   11411                 :            : 
   11412                 :          0 :         return ret;
   11413                 :            : 
   11414                 :            : nla_put_failure:
   11415                 :          0 :         nlmsg_free(msg);
   11416                 :          0 :         return -ENOBUFS;
   11417                 :            : }
   11418                 :            : 
   11419                 :            : 
   11420                 :         39 : const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
   11421                 :            : {
   11422                 :         39 :         struct i802_bss *bss = priv;
   11423                 :         39 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11424                 :            : 
   11425         [ +  - ]:         39 :         if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
   11426                 :         39 :                 return NULL;
   11427                 :            : 
   11428                 :         39 :         return bss->addr;
   11429                 :            : }
   11430                 :            : 
   11431                 :            : 
   11432                 :       1186 : static const char * scan_state_str(enum scan_states scan_state)
   11433                 :            : {
   11434   [ +  -  +  +  :       1186 :         switch (scan_state) {
             -  -  -  -  
                      - ]
   11435                 :            :         case NO_SCAN:
   11436                 :         31 :                 return "NO_SCAN";
   11437                 :            :         case SCAN_REQUESTED:
   11438                 :          0 :                 return "SCAN_REQUESTED";
   11439                 :            :         case SCAN_STARTED:
   11440                 :         15 :                 return "SCAN_STARTED";
   11441                 :            :         case SCAN_COMPLETED:
   11442                 :       1140 :                 return "SCAN_COMPLETED";
   11443                 :            :         case SCAN_ABORTED:
   11444                 :          0 :                 return "SCAN_ABORTED";
   11445                 :            :         case SCHED_SCAN_STARTED:
   11446                 :          0 :                 return "SCHED_SCAN_STARTED";
   11447                 :            :         case SCHED_SCAN_STOPPED:
   11448                 :          0 :                 return "SCHED_SCAN_STOPPED";
   11449                 :            :         case SCHED_SCAN_RESULTS:
   11450                 :          0 :                 return "SCHED_SCAN_RESULTS";
   11451                 :            :         }
   11452                 :            : 
   11453                 :       1186 :         return "??";
   11454                 :            : }
   11455                 :            : 
   11456                 :            : 
   11457                 :       1186 : static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
   11458                 :            : {
   11459                 :       1186 :         struct i802_bss *bss = priv;
   11460                 :       1186 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11461                 :            :         int res;
   11462                 :            :         char *pos, *end;
   11463                 :            : 
   11464                 :       1186 :         pos = buf;
   11465                 :       1186 :         end = buf + buflen;
   11466                 :            : 
   11467 [ -  + ][ -  + ]:       1186 :         res = os_snprintf(pos, end - pos,
         [ -  + ][ -  + ]
                 [ -  + ]
   11468                 :            :                           "ifindex=%d\n"
   11469                 :            :                           "ifname=%s\n"
   11470                 :            :                           "brname=%s\n"
   11471                 :            :                           "addr=" MACSTR "\n"
   11472                 :            :                           "freq=%d\n"
   11473                 :            :                           "%s%s%s%s%s",
   11474                 :            :                           bss->ifindex,
   11475                 :       1186 :                           bss->ifname,
   11476                 :       1186 :                           bss->brname,
   11477                 :       7116 :                           MAC2STR(bss->addr),
   11478                 :            :                           bss->freq,
   11479                 :       1186 :                           bss->beacon_set ? "beacon_set=1\n" : "",
   11480                 :       1186 :                           bss->added_if_into_bridge ?
   11481                 :            :                           "added_if_into_bridge=1\n" : "",
   11482                 :       1186 :                           bss->added_bridge ? "added_bridge=1\n" : "",
   11483                 :       1186 :                           bss->in_deinit ? "in_deinit=1\n" : "",
   11484                 :       1186 :                           bss->if_dynamic ? "if_dynamic=1\n" : "");
   11485 [ +  - ][ -  + ]:       1186 :         if (res < 0 || res >= end - pos)
   11486                 :          0 :                 return pos - buf;
   11487                 :       1186 :         pos += res;
   11488                 :            : 
   11489         [ -  + ]:       1186 :         if (bss->wdev_id_set) {
   11490                 :          0 :                 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
   11491                 :          0 :                                   (unsigned long long) bss->wdev_id);
   11492 [ #  # ][ #  # ]:          0 :                 if (res < 0 || res >= end - pos)
   11493                 :          0 :                         return pos - buf;
   11494                 :          0 :                 pos += res;
   11495                 :            :         }
   11496                 :            : 
   11497 [ -  + ][ -  + ]:       1186 :         res = os_snprintf(pos, end - pos,
         [ -  + ][ -  + ]
         [ -  + ][ +  - ]
         [ +  - ][ -  + ]
         [ +  - ][ -  + ]
         [ +  + ][ +  + ]
                 [ -  + ]
   11498                 :            :                           "phyname=%s\n"
   11499                 :            :                           "drv_ifindex=%d\n"
   11500                 :            :                           "operstate=%d\n"
   11501                 :            :                           "scan_state=%s\n"
   11502                 :            :                           "auth_bssid=" MACSTR "\n"
   11503                 :            :                           "auth_attempt_bssid=" MACSTR "\n"
   11504                 :            :                           "bssid=" MACSTR "\n"
   11505                 :            :                           "prev_bssid=" MACSTR "\n"
   11506                 :            :                           "associated=%d\n"
   11507                 :            :                           "assoc_freq=%u\n"
   11508                 :            :                           "monitor_sock=%d\n"
   11509                 :            :                           "monitor_ifidx=%d\n"
   11510                 :            :                           "monitor_refcount=%d\n"
   11511                 :            :                           "last_mgmt_freq=%u\n"
   11512                 :            :                           "eapol_tx_sock=%d\n"
   11513                 :            :                           "%s%s%s%s%s%s%s%s%s%s%s%s%s",
   11514                 :       1186 :                           drv->phyname,
   11515                 :            :                           drv->ifindex,
   11516                 :            :                           drv->operstate,
   11517                 :            :                           scan_state_str(drv->scan_state),
   11518                 :       7116 :                           MAC2STR(drv->auth_bssid),
   11519                 :       7116 :                           MAC2STR(drv->auth_attempt_bssid),
   11520                 :       7116 :                           MAC2STR(drv->bssid),
   11521                 :       7116 :                           MAC2STR(drv->prev_bssid),
   11522                 :            :                           drv->associated,
   11523                 :            :                           drv->assoc_freq,
   11524                 :            :                           drv->monitor_sock,
   11525                 :            :                           drv->monitor_ifidx,
   11526                 :            :                           drv->monitor_refcount,
   11527                 :            :                           drv->last_mgmt_freq,
   11528                 :            :                           drv->eapol_tx_sock,
   11529                 :       1186 :                           drv->ignore_if_down_event ?
   11530                 :            :                           "ignore_if_down_event=1\n" : "",
   11531                 :       1186 :                           drv->scan_complete_events ?
   11532                 :            :                           "scan_complete_events=1\n" : "",
   11533                 :       1186 :                           drv->disabled_11b_rates ?
   11534                 :            :                           "disabled_11b_rates=1\n" : "",
   11535                 :       1186 :                           drv->pending_remain_on_chan ?
   11536                 :            :                           "pending_remain_on_chan=1\n" : "",
   11537                 :       1186 :                           drv->in_interface_list ? "in_interface_list=1\n" : "",
   11538                 :       1186 :                           drv->device_ap_sme ? "device_ap_sme=1\n" : "",
   11539                 :       1186 :                           drv->poll_command_supported ?
   11540                 :            :                           "poll_command_supported=1\n" : "",
   11541                 :       1186 :                           drv->data_tx_status ? "data_tx_status=1\n" : "",
   11542                 :       1186 :                           drv->scan_for_auth ? "scan_for_auth=1\n" : "",
   11543                 :       1186 :                           drv->retry_auth ? "retry_auth=1\n" : "",
   11544                 :       1186 :                           drv->use_monitor ? "use_monitor=1\n" : "",
   11545                 :       1186 :                           drv->ignore_next_local_disconnect ?
   11546                 :            :                           "ignore_next_local_disconnect=1\n" : "",
   11547                 :       1186 :                           drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
   11548 [ -  + ][ +  - ]:       1186 :         if (res < 0 || res >= end - pos)
   11549                 :          0 :                 return pos - buf;
   11550                 :       1186 :         pos += res;
   11551                 :            : 
   11552         [ +  - ]:       1186 :         if (drv->has_capability) {
   11553                 :       1186 :                 res = os_snprintf(pos, end - pos,
   11554                 :            :                                   "capa.key_mgmt=0x%x\n"
   11555                 :            :                                   "capa.enc=0x%x\n"
   11556                 :            :                                   "capa.auth=0x%x\n"
   11557                 :            :                                   "capa.flags=0x%x\n"
   11558                 :            :                                   "capa.max_scan_ssids=%d\n"
   11559                 :            :                                   "capa.max_sched_scan_ssids=%d\n"
   11560                 :            :                                   "capa.sched_scan_supported=%d\n"
   11561                 :            :                                   "capa.max_match_sets=%d\n"
   11562                 :            :                                   "capa.max_remain_on_chan=%u\n"
   11563                 :            :                                   "capa.max_stations=%u\n"
   11564                 :            :                                   "capa.probe_resp_offloads=0x%x\n"
   11565                 :            :                                   "capa.max_acl_mac_addrs=%u\n"
   11566                 :            :                                   "capa.num_multichan_concurrent=%u\n",
   11567                 :            :                                   drv->capa.key_mgmt,
   11568                 :            :                                   drv->capa.enc,
   11569                 :            :                                   drv->capa.auth,
   11570                 :            :                                   drv->capa.flags,
   11571                 :            :                                   drv->capa.max_scan_ssids,
   11572                 :            :                                   drv->capa.max_sched_scan_ssids,
   11573                 :            :                                   drv->capa.sched_scan_supported,
   11574                 :            :                                   drv->capa.max_match_sets,
   11575                 :            :                                   drv->capa.max_remain_on_chan,
   11576                 :            :                                   drv->capa.max_stations,
   11577                 :            :                                   drv->capa.probe_resp_offloads,
   11578                 :            :                                   drv->capa.max_acl_mac_addrs,
   11579                 :            :                                   drv->capa.num_multichan_concurrent);
   11580 [ +  - ][ -  + ]:       1186 :                 if (res < 0 || res >= end - pos)
   11581                 :          0 :                         return pos - buf;
   11582                 :       1186 :                 pos += res;
   11583                 :            :         }
   11584                 :            : 
   11585                 :       1186 :         return pos - buf;
   11586                 :            : }
   11587                 :            : 
   11588                 :            : 
   11589                 :          0 : static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
   11590                 :            : {
   11591         [ #  # ]:          0 :         if (settings->head)
   11592         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
   11593                 :            :                         settings->head_len, settings->head);
   11594                 :            : 
   11595         [ #  # ]:          0 :         if (settings->tail)
   11596         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
   11597                 :            :                         settings->tail_len, settings->tail);
   11598                 :            : 
   11599         [ #  # ]:          0 :         if (settings->beacon_ies)
   11600         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_IE,
   11601                 :            :                         settings->beacon_ies_len, settings->beacon_ies);
   11602                 :            : 
   11603         [ #  # ]:          0 :         if (settings->proberesp_ies)
   11604         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
   11605                 :            :                         settings->proberesp_ies_len, settings->proberesp_ies);
   11606                 :            : 
   11607         [ #  # ]:          0 :         if (settings->assocresp_ies)
   11608         [ #  # ]:          0 :                 NLA_PUT(msg,
   11609                 :            :                         NL80211_ATTR_IE_ASSOC_RESP,
   11610                 :            :                         settings->assocresp_ies_len, settings->assocresp_ies);
   11611                 :            : 
   11612         [ #  # ]:          0 :         if (settings->probe_resp)
   11613         [ #  # ]:          0 :                 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
   11614                 :            :                         settings->probe_resp_len, settings->probe_resp);
   11615                 :            : 
   11616                 :          0 :         return 0;
   11617                 :            : 
   11618                 :            : nla_put_failure:
   11619                 :          0 :         return -ENOBUFS;
   11620                 :            : }
   11621                 :            : 
   11622                 :            : 
   11623                 :          1 : static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
   11624                 :            : {
   11625                 :            :         struct nl_msg *msg;
   11626                 :          1 :         struct i802_bss *bss = priv;
   11627                 :          1 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11628                 :            :         struct nlattr *beacon_csa;
   11629                 :          1 :         int ret = -ENOBUFS;
   11630                 :            : 
   11631                 :          1 :         wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
   11632                 :          2 :                    settings->cs_count, settings->block_tx,
   11633                 :            :                    settings->freq_params.freq, settings->freq_params.bandwidth,
   11634                 :            :                    settings->freq_params.center_freq1,
   11635                 :            :                    settings->freq_params.center_freq2);
   11636                 :            : 
   11637         [ +  - ]:          1 :         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
   11638                 :          1 :                 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
   11639                 :          1 :                 return -EOPNOTSUPP;
   11640                 :            :         }
   11641                 :            : 
   11642 [ #  # ][ #  # ]:          0 :         if ((drv->nlmode != NL80211_IFTYPE_AP) &&
   11643                 :          0 :             (drv->nlmode != NL80211_IFTYPE_P2P_GO))
   11644                 :          0 :                 return -EOPNOTSUPP;
   11645                 :            : 
   11646                 :            :         /* check settings validity */
   11647 [ #  # ][ #  # ]:          0 :         if (!settings->beacon_csa.tail ||
   11648                 :          0 :             ((settings->beacon_csa.tail_len <=
   11649         [ #  # ]:          0 :               settings->counter_offset_beacon) ||
   11650                 :          0 :              (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
   11651                 :          0 :               settings->cs_count)))
   11652                 :          0 :                 return -EINVAL;
   11653                 :            : 
   11654 [ #  # ][ #  # ]:          0 :         if (settings->beacon_csa.probe_resp &&
   11655                 :          0 :             ((settings->beacon_csa.probe_resp_len <=
   11656         [ #  # ]:          0 :               settings->counter_offset_presp) ||
   11657                 :          0 :              (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
   11658                 :          0 :               settings->cs_count)))
   11659                 :          0 :                 return -EINVAL;
   11660                 :            : 
   11661                 :          0 :         msg = nlmsg_alloc();
   11662         [ #  # ]:          0 :         if (!msg)
   11663                 :          0 :                 return -ENOMEM;
   11664                 :            : 
   11665                 :          0 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
   11666         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   11667         [ #  # ]:          0 :         NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
   11668                 :          0 :         ret = nl80211_put_freq_params(msg, &settings->freq_params);
   11669         [ #  # ]:          0 :         if (ret)
   11670                 :          0 :                 goto error;
   11671                 :            : 
   11672         [ #  # ]:          0 :         if (settings->block_tx)
   11673         [ #  # ]:          0 :                 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
   11674                 :            : 
   11675                 :            :         /* beacon_after params */
   11676                 :          0 :         ret = set_beacon_data(msg, &settings->beacon_after);
   11677         [ #  # ]:          0 :         if (ret)
   11678                 :          0 :                 goto error;
   11679                 :            : 
   11680                 :            :         /* beacon_csa params */
   11681                 :          0 :         beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
   11682         [ #  # ]:          0 :         if (!beacon_csa)
   11683                 :          0 :                 goto nla_put_failure;
   11684                 :            : 
   11685                 :          0 :         ret = set_beacon_data(msg, &settings->beacon_csa);
   11686         [ #  # ]:          0 :         if (ret)
   11687                 :          0 :                 goto error;
   11688                 :            : 
   11689         [ #  # ]:          0 :         NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
   11690                 :            :                     settings->counter_offset_beacon);
   11691                 :            : 
   11692         [ #  # ]:          0 :         if (settings->beacon_csa.probe_resp)
   11693         [ #  # ]:          0 :                 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
   11694                 :            :                             settings->counter_offset_presp);
   11695                 :            : 
   11696                 :          0 :         nla_nest_end(msg, beacon_csa);
   11697                 :          0 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   11698         [ #  # ]:          0 :         if (ret) {
   11699                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
   11700                 :            :                            ret, strerror(-ret));
   11701                 :            :         }
   11702                 :          0 :         return ret;
   11703                 :            : 
   11704                 :            : nla_put_failure:
   11705                 :          0 :         ret = -ENOBUFS;
   11706                 :            : error:
   11707                 :          0 :         nlmsg_free(msg);
   11708                 :          0 :         wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
   11709                 :          1 :         return ret;
   11710                 :            : }
   11711                 :            : 
   11712                 :            : 
   11713                 :          4 : static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
   11714                 :            :                                u8 qos_map_set_len)
   11715                 :            : {
   11716                 :          4 :         struct i802_bss *bss = priv;
   11717                 :          4 :         struct wpa_driver_nl80211_data *drv = bss->drv;
   11718                 :            :         struct nl_msg *msg;
   11719                 :            :         int ret;
   11720                 :            : 
   11721                 :          4 :         msg = nlmsg_alloc();
   11722         [ -  + ]:          4 :         if (!msg)
   11723                 :          0 :                 return -ENOMEM;
   11724                 :            : 
   11725                 :          4 :         wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
   11726                 :            :                     qos_map_set, qos_map_set_len);
   11727                 :            : 
   11728                 :          4 :         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
   11729         [ +  - ]:          4 :         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
   11730         [ +  - ]:          4 :         NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
   11731                 :            : 
   11732                 :          4 :         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
   11733         [ -  + ]:          4 :         if (ret)
   11734                 :          0 :                 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
   11735                 :            : 
   11736                 :          4 :         return ret;
   11737                 :            : 
   11738                 :            : nla_put_failure:
   11739                 :          0 :         nlmsg_free(msg);
   11740                 :          4 :         return -ENOBUFS;
   11741                 :            : }
   11742                 :            : 
   11743                 :            : 
   11744                 :            : const struct wpa_driver_ops wpa_driver_nl80211_ops = {
   11745                 :            :         .name = "nl80211",
   11746                 :            :         .desc = "Linux nl80211/cfg80211",
   11747                 :            :         .get_bssid = wpa_driver_nl80211_get_bssid,
   11748                 :            :         .get_ssid = wpa_driver_nl80211_get_ssid,
   11749                 :            :         .set_key = driver_nl80211_set_key,
   11750                 :            :         .scan2 = driver_nl80211_scan2,
   11751                 :            :         .sched_scan = wpa_driver_nl80211_sched_scan,
   11752                 :            :         .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
   11753                 :            :         .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
   11754                 :            :         .deauthenticate = driver_nl80211_deauthenticate,
   11755                 :            :         .authenticate = driver_nl80211_authenticate,
   11756                 :            :         .associate = wpa_driver_nl80211_associate,
   11757                 :            :         .global_init = nl80211_global_init,
   11758                 :            :         .global_deinit = nl80211_global_deinit,
   11759                 :            :         .init2 = wpa_driver_nl80211_init,
   11760                 :            :         .deinit = driver_nl80211_deinit,
   11761                 :            :         .get_capa = wpa_driver_nl80211_get_capa,
   11762                 :            :         .set_operstate = wpa_driver_nl80211_set_operstate,
   11763                 :            :         .set_supp_port = wpa_driver_nl80211_set_supp_port,
   11764                 :            :         .set_country = wpa_driver_nl80211_set_country,
   11765                 :            :         .get_country = wpa_driver_nl80211_get_country,
   11766                 :            :         .set_ap = wpa_driver_nl80211_set_ap,
   11767                 :            :         .set_acl = wpa_driver_nl80211_set_acl,
   11768                 :            :         .if_add = wpa_driver_nl80211_if_add,
   11769                 :            :         .if_remove = driver_nl80211_if_remove,
   11770                 :            :         .send_mlme = driver_nl80211_send_mlme,
   11771                 :            :         .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
   11772                 :            :         .sta_add = wpa_driver_nl80211_sta_add,
   11773                 :            :         .sta_remove = driver_nl80211_sta_remove,
   11774                 :            :         .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
   11775                 :            :         .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
   11776                 :            :         .hapd_init = i802_init,
   11777                 :            :         .hapd_deinit = i802_deinit,
   11778                 :            :         .set_wds_sta = i802_set_wds_sta,
   11779                 :            :         .get_seqnum = i802_get_seqnum,
   11780                 :            :         .flush = i802_flush,
   11781                 :            :         .get_inact_sec = i802_get_inact_sec,
   11782                 :            :         .sta_clear_stats = i802_sta_clear_stats,
   11783                 :            :         .set_rts = i802_set_rts,
   11784                 :            :         .set_frag = i802_set_frag,
   11785                 :            :         .set_tx_queue_params = i802_set_tx_queue_params,
   11786                 :            :         .set_sta_vlan = driver_nl80211_set_sta_vlan,
   11787                 :            :         .sta_deauth = i802_sta_deauth,
   11788                 :            :         .sta_disassoc = i802_sta_disassoc,
   11789                 :            :         .read_sta_data = driver_nl80211_read_sta_data,
   11790                 :            :         .set_freq = i802_set_freq,
   11791                 :            :         .send_action = driver_nl80211_send_action,
   11792                 :            :         .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
   11793                 :            :         .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
   11794                 :            :         .cancel_remain_on_channel =
   11795                 :            :         wpa_driver_nl80211_cancel_remain_on_channel,
   11796                 :            :         .probe_req_report = driver_nl80211_probe_req_report,
   11797                 :            :         .deinit_ap = wpa_driver_nl80211_deinit_ap,
   11798                 :            :         .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
   11799                 :            :         .resume = wpa_driver_nl80211_resume,
   11800                 :            :         .send_ft_action = nl80211_send_ft_action,
   11801                 :            :         .signal_monitor = nl80211_signal_monitor,
   11802                 :            :         .signal_poll = nl80211_signal_poll,
   11803                 :            :         .send_frame = nl80211_send_frame,
   11804                 :            :         .shared_freq = wpa_driver_nl80211_shared_freq,
   11805                 :            :         .set_param = nl80211_set_param,
   11806                 :            :         .get_radio_name = nl80211_get_radio_name,
   11807                 :            :         .add_pmkid = nl80211_add_pmkid,
   11808                 :            :         .remove_pmkid = nl80211_remove_pmkid,
   11809                 :            :         .flush_pmkid = nl80211_flush_pmkid,
   11810                 :            :         .set_rekey_info = nl80211_set_rekey_info,
   11811                 :            :         .poll_client = nl80211_poll_client,
   11812                 :            :         .set_p2p_powersave = nl80211_set_p2p_powersave,
   11813                 :            :         .start_dfs_cac = nl80211_start_radar_detection,
   11814                 :            :         .stop_ap = wpa_driver_nl80211_stop_ap,
   11815                 :            : #ifdef CONFIG_TDLS
   11816                 :            :         .send_tdls_mgmt = nl80211_send_tdls_mgmt,
   11817                 :            :         .tdls_oper = nl80211_tdls_oper,
   11818                 :            : #endif /* CONFIG_TDLS */
   11819                 :            :         .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
   11820                 :            :         .get_mac_addr = wpa_driver_nl80211_get_macaddr,
   11821                 :            :         .get_survey = wpa_driver_nl80211_get_survey,
   11822                 :            :         .status = wpa_driver_nl80211_status,
   11823                 :            :         .switch_channel = nl80211_switch_channel,
   11824                 :            : #ifdef ANDROID_P2P
   11825                 :            :         .set_noa = wpa_driver_set_p2p_noa,
   11826                 :            :         .get_noa = wpa_driver_get_p2p_noa,
   11827                 :            :         .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
   11828                 :            : #endif /* ANDROID_P2P */
   11829                 :            : #ifdef ANDROID
   11830                 :            :         .driver_cmd = wpa_driver_nl80211_driver_cmd,
   11831                 :            : #endif /* ANDROID */
   11832                 :            :         .set_qos_map = nl80211_set_qos_map,
   11833                 :            : };

Generated by: LCOV version 1.9