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