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