1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef WIFICOND_NET_NETLINK_UTILS_H_ 18 #define WIFICOND_NET_NETLINK_UTILS_H_ 19 20 #include <array> 21 #include <functional> 22 #include <string> 23 #include <vector> 24 25 #include <linux/if_ether.h> 26 27 #include <android-base/macros.h> 28 29 #include "wificond/net/kernel-header-latest/nl80211.h" 30 #include "wificond/net/netlink_manager.h" 31 #include "wificond/net/nl80211_packet.h" 32 33 namespace android { 34 namespace wificond { 35 36 struct InterfaceInfo { 37 InterfaceInfo() = default; InterfaceInfoInterfaceInfo38 InterfaceInfo(uint32_t if_index, 39 uint32_t wiphy_index, 40 const std::string& name, 41 const std::array<uint8_t, ETH_ALEN>& mac_address) 42 : if_index(if_index), 43 wiphy_index(wiphy_index), 44 name(name), 45 mac_address(mac_address) {} 46 // Index of this interface. 47 uint32_t if_index; 48 // Wiphy Index of this interface. 49 uint32_t wiphy_index; 50 // Name of this interface. 51 std::string name; 52 // MAC address of this interface. 53 std::array<uint8_t, ETH_ALEN> mac_address; 54 }; 55 56 struct BandInfo { BandInfoBandInfo57 BandInfo(): 58 is_80211n_supported(false), 59 is_80211ac_supported(false), 60 is_80211ax_supported(false), 61 is_80211be_supported(false), 62 is_160_mhz_supported(false), 63 is_80p80_mhz_supported(false), 64 is_320_mhz_supported(false), 65 max_tx_streams(1), 66 max_rx_streams(1) {}; 67 // Frequencies for 2.4 GHz band. 68 std::vector<uint32_t> band_2g; 69 // Frequencies for 5 GHz band without DFS. 70 std::vector<uint32_t> band_5g; 71 // Frequencies for DFS. 72 std::vector<uint32_t> band_dfs; 73 // Frequencies for 6 GHz band. 74 std::vector<uint32_t> band_6g; 75 // Frequencies for 60 GHz band. 76 std::vector<uint32_t> band_60g; 77 // support for 802.11n 78 bool is_80211n_supported; 79 // support for 802.11ac 80 bool is_80211ac_supported; 81 // support for 802.11ax 82 bool is_80211ax_supported; 83 // support for 802.11be 84 bool is_80211be_supported; 85 // support for 160Mhz channel width 86 bool is_160_mhz_supported; 87 // support for 80+80Mhz channel width 88 bool is_80p80_mhz_supported; 89 // support for 320Mhz channel width 90 bool is_320_mhz_supported; 91 // Max number of transmit spatial streams 92 uint32_t max_tx_streams; 93 // Max number of receive spatial streams 94 uint32_t max_rx_streams; 95 }; 96 97 struct ScanCapabilities { 98 ScanCapabilities() = default; ScanCapabilitiesScanCapabilities99 ScanCapabilities(uint8_t max_num_scan_ssids_, 100 uint8_t max_num_sched_scan_ssids_, 101 uint8_t max_match_sets_, 102 uint32_t max_num_scan_plans_, 103 uint32_t max_scan_plan_interval_, 104 uint32_t max_scan_plan_iterations_) 105 : max_num_scan_ssids(max_num_scan_ssids_), 106 max_num_sched_scan_ssids(max_num_sched_scan_ssids_), 107 max_match_sets(max_match_sets_), 108 max_num_scan_plans(max_num_scan_plans_), 109 max_scan_plan_interval(max_scan_plan_interval_), 110 max_scan_plan_iterations(max_scan_plan_iterations_) {} 111 // Number of SSIDs you can scan with a single scan request. 112 uint8_t max_num_scan_ssids; 113 // Number of SSIDs you can scan with a single scheduled scan request. 114 uint8_t max_num_sched_scan_ssids; 115 // Maximum number of sets that can be used with NL80211_ATTR_SCHED_SCAN_MATCH. 116 uint8_t max_match_sets; 117 // Maximum number of scan plans that can be specified. 118 uint32_t max_num_scan_plans; 119 // Maximum interval in seconds for a particular scan plan that can be 120 // specified. 121 uint32_t max_scan_plan_interval; 122 // Maximum number of iterations for a particular scan plan that can be 123 // specified. 124 uint32_t max_scan_plan_iterations; 125 }; 126 127 struct WiphyFeatures { WiphyFeaturesWiphyFeatures128 WiphyFeatures() 129 : supports_random_mac_oneshot_scan(false), 130 supports_random_mac_sched_scan(false), 131 supports_low_span_oneshot_scan(false), 132 supports_low_power_oneshot_scan(false), 133 supports_high_accuracy_oneshot_scan(false), 134 supports_tx_mgmt_frame_mcs(false) {} 135 WiphyFeatures(uint32_t feature_flags, 136 const std::vector<uint8_t>& ext_feature_flags_bytes); 137 // This device/driver supports using a random MAC address during scan 138 // (while not associated). 139 bool supports_random_mac_oneshot_scan; 140 // This device/driver supports using a random MAC address for every 141 // scan iteration during scheduled scan (while not associated). 142 bool supports_random_mac_sched_scan; 143 // This device/driver supports performing low-span/low-latency one-shot scans. 144 bool supports_low_span_oneshot_scan; 145 // This device/driver supports performing low-power one-shot scans. 146 bool supports_low_power_oneshot_scan; 147 // This device/driver supports performing high-accuracy one-shot scans. 148 bool supports_high_accuracy_oneshot_scan; 149 // This device/driver supports sending a management frame at a specified MCS. 150 bool supports_tx_mgmt_frame_mcs; 151 // This device/driver supports sched_scan for reporting BSSs 152 // with better RSSI than the current connected BSS 153 bool supports_ext_sched_scan_relative_rssi; 154 // There are other flags included in NL80211_ATTR_FEATURE_FLAGS. 155 // We will add them once we find them useful. 156 }; 157 158 struct StationInfo { 159 StationInfo() = default; StationInfoStationInfo160 StationInfo(uint32_t station_tx_packets_, 161 uint32_t station_tx_failed_, 162 uint32_t station_tx_bitrate_, 163 int8_t current_rssi_, 164 uint32_t station_rx_bitrate_) 165 : station_tx_packets(station_tx_packets_), 166 station_tx_failed(station_tx_failed_), 167 station_tx_bitrate(station_tx_bitrate_), 168 current_rssi(current_rssi_), 169 station_rx_bitrate(station_rx_bitrate_) {} 170 // Number of successfully transmitted packets. 171 int32_t station_tx_packets; 172 // Number of tramsmission failures. 173 int32_t station_tx_failed; 174 // Transimission bit rate in 100kbit/s. 175 uint32_t station_tx_bitrate; 176 // Current signal strength. 177 int8_t current_rssi; 178 // Last Received unicast packet bit rate in 100kbit/s. 179 uint32_t station_rx_bitrate; 180 // There are many other counters/parameters included in station info. 181 // We will add them once we find them useful. 182 }; 183 184 class MlmeEventHandler; 185 class NetlinkManager; 186 class NL80211Packet; 187 188 // Provides NL80211 helper functions. 189 class NetlinkUtils { 190 public: 191 // Currently we only support setting the interface to STATION mode. 192 // This is used for cleaning up interface after KILLING hostapd. 193 enum InterfaceMode{ 194 STATION_MODE 195 }; 196 197 explicit NetlinkUtils(NetlinkManager* netlink_manager); 198 virtual ~NetlinkUtils(); 199 200 // Get the wiphy index from kernel. 201 // |*out_wiphy_index| returns the wiphy index from kernel. 202 // Returns true on success. 203 virtual bool GetWiphyIndex(uint32_t* out_wiphy_index); 204 virtual bool GetWiphyIndex(uint32_t* out_wiphy_index, 205 const std::string& iface_name); 206 207 // Get wifi interfaces info from kernel. 208 // |wiphy_index| is the wiphy index we get using GetWiphyIndex(). 209 // |interface_info| returns a vector of InterfaceInfo structs with 210 // information about all existing interfaces. 211 // Returns true on success. 212 virtual bool GetInterfaces(uint32_t wiphy_index, 213 std::vector<InterfaceInfo>* interface_info); 214 215 // Set the mode of interface. 216 // |interface_index| is the interface index. 217 // |mode| is one of the values in |enum InterfaceMode|. 218 // Returns true on success. 219 virtual bool SetInterfaceMode(uint32_t interface_index, 220 InterfaceMode mode); 221 222 // Get wiphy capability information from kernel. 223 // Returns true on success. 224 virtual bool GetWiphyInfo(uint32_t wiphy_index, 225 BandInfo* out_band_info, 226 ScanCapabilities* out_scan_capabilities, 227 WiphyFeatures* out_wiphy_features); 228 229 // Get station info from kernel. 230 // |*out_station_info]| is the struct of available station information. 231 // Returns true on success. 232 virtual bool GetStationInfo(uint32_t interface_index, 233 const std::array<uint8_t, ETH_ALEN>& mac_address, 234 StationInfo* out_station_info); 235 236 // Get a bitmap for nl80211 protocol features, 237 // i.e. features for the nl80211 protocol rather than device features. 238 // See enum nl80211_protocol_features in nl80211.h for decoding the bitmap. 239 // Returns true on success. 240 virtual bool GetProtocolFeatures(uint32_t* features); 241 242 // Get current alpha2 country code from kernel. 243 // Returns true on success. 244 virtual bool GetCountryCode(std::string* out_country_code); 245 246 // Sign up to be notified when there is MLME event. 247 // Only one handler can be registered per interface index. 248 // New handler will replace the registered handler if they are for the 249 // same interface index. 250 // NetlinkUtils is not going to take ownership of this pointer, and that it 251 // is the caller's responsibility to make sure that the object exists for the 252 // duration of the subscription. 253 virtual void SubscribeMlmeEvent(uint32_t interface_index, 254 MlmeEventHandler* handler); 255 256 // Cancel the sign-up of receiving MLME event notification 257 // from interface with index |interface_index|. 258 virtual void UnsubscribeMlmeEvent(uint32_t interface_index); 259 260 // Sign up to be notified when there is an regulatory domain change. 261 // Only one handler can be registered per wiphy index. 262 // New handler will replace the registered handler if they are for the 263 // same wiphy index. 264 virtual void SubscribeRegDomainChange(uint32_t wiphy_index, 265 OnRegDomainChangedHandler handler); 266 267 // Cancel the sign-up of receiving regulatory domain change notification 268 // from wiphy with index |wiphy_index|. 269 virtual void UnsubscribeRegDomainChange(uint32_t wiphy_index); 270 271 // Sign up to be notified when there is a station event. 272 // Only one handler can be registered per interface index. 273 // New handler will replace the registered handler if they are for the 274 // same interface index. 275 virtual void SubscribeStationEvent(uint32_t interface_index, 276 OnStationEventHandler handler); 277 278 // Cancel the sign-up of receiving station events. 279 virtual void UnsubscribeStationEvent(uint32_t interface_index); 280 281 // Sign up to be notified when there is a channel switch event. 282 // Only one handler can be registered per interface index. 283 // New handler will replace the registered handler if they are for the 284 // same interface index. 285 virtual void SubscribeChannelSwitchEvent(uint32_t interface_index, 286 OnChannelSwitchEventHandler handler); 287 288 // Cancel the sign-up of receiving channel switch events. 289 virtual void UnsubscribeChannelSwitchEvent(uint32_t interface_index); 290 291 // Sign up to be notified of frame tx status events. 292 virtual void SubscribeFrameTxStatusEvent( 293 uint32_t interface_index, OnFrameTxStatusEventHandler handler); 294 295 // Cancel the sign-up of receiving frame tx status events. 296 virtual void UnsubscribeFrameTxStatusEvent(uint32_t interface_index); 297 298 virtual bool SendMgmtFrame(uint32_t interface_index, 299 const std::vector<uint8_t>& frame, int32_t mcs, uint64_t* out_cookie); 300 301 // Visible for testing. 302 bool supports_split_wiphy_dump_; 303 304 private: 305 bool ParseWiphyInfoFromPacket( 306 const NL80211Packet& packet, 307 BandInfo* out_band_info, 308 ScanCapabilities* out_scan_capabilities, 309 WiphyFeatures* out_wiphy_features); 310 bool ParseBandInfo(const NL80211Packet* const packet, 311 BandInfo* out_band_info); 312 void ParseIfTypeDataAttributes(const NL80211NestedAttr& iftype_data_attr, 313 BandInfo* out_band_info); 314 void ParseHtVhtPhyCapabilities(const NL80211NestedAttr& band, 315 BandInfo* out_band_info); 316 void ParseHtMcsSetAttribute(const NL80211NestedAttr& band, 317 BandInfo* out_band_info); 318 void ParseVhtMcsSetAttribute(const NL80211NestedAttr& band, 319 BandInfo* out_band_info); 320 void ParseHeMcsSetAttribute(const NL80211NestedAttr& attribute, 321 BandInfo* out_band_info); 322 std::pair<uint32_t, uint32_t> ParseHtMcsSet( 323 const std::vector<uint8_t>& ht_mcs_set); 324 uint32_t ParseMcsMap(uint16_t mcs_map); 325 void ParseVhtCapAttribute(const NL80211NestedAttr& band, 326 BandInfo* out_band_info); 327 void ParseHeCapPhyAttribute(const NL80211NestedAttr& attribute, 328 BandInfo* out_band_info); 329 void ParseEhtCapPhyAttribute(const NL80211NestedAttr& attribute, 330 BandInfo* out_band_info); 331 332 bool ParseScanCapabilities(const NL80211Packet* const packet, 333 ScanCapabilities* out_scan_capabilities); 334 335 bool MergePacketsForSplitWiphyDump( 336 const std::vector<std::unique_ptr<const NL80211Packet>>& split_dump_info, 337 std::vector<NL80211Packet>* packet_per_wiphy); 338 void handleBandFreqAttributes(const NL80211NestedAttr& freqs_attr, 339 BandInfo* out_band_info); 340 341 NetlinkManager* netlink_manager_; 342 343 DISALLOW_COPY_AND_ASSIGN(NetlinkUtils); 344 }; 345 346 } // namespace wificond 347 } // namespace android 348 349 #endif // WIFICOND_NET_NETLINK_UTILS_H_ 350