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 <string> 21 #include <vector> 22 23 #include <linux/nl80211.h> 24 25 #include <android-base/macros.h> 26 27 #include "wificond/net/netlink_manager.h" 28 29 namespace android { 30 namespace wificond { 31 32 struct InterfaceInfo { 33 InterfaceInfo() = default; InterfaceInfoInterfaceInfo34 InterfaceInfo(uint32_t index_, 35 const std::string name_, 36 const std::vector<uint8_t> mac_address_) 37 : index(index_), 38 name(name_), 39 mac_address(mac_address_) {} 40 // Index of this interface. 41 uint32_t index; 42 // Name of this interface. 43 std::string name; 44 // MAC address of this interface. 45 std::vector<uint8_t> mac_address; 46 }; 47 48 struct BandInfo { 49 BandInfo() = default; BandInfoBandInfo50 BandInfo(std::vector<uint32_t>& band_2g_, 51 std::vector<uint32_t>& band_5g_, 52 std::vector<uint32_t>& band_dfs_) 53 : band_2g(band_2g_), 54 band_5g(band_5g_), 55 band_dfs(band_dfs_) {} 56 // Frequencies for 2.4 GHz band. 57 std::vector<uint32_t> band_2g; 58 // Frequencies for 5 GHz band without DFS. 59 std::vector<uint32_t> band_5g; 60 // Frequencies for DFS. 61 std::vector<uint32_t> band_dfs; 62 }; 63 64 struct ScanCapabilities { 65 ScanCapabilities() = default; ScanCapabilitiesScanCapabilities66 ScanCapabilities(uint8_t max_num_scan_ssids_, 67 uint8_t max_num_sched_scan_ssids_, 68 uint8_t max_match_sets_) 69 : max_num_scan_ssids(max_num_scan_ssids_), 70 max_num_sched_scan_ssids(max_num_sched_scan_ssids_), 71 max_match_sets(max_match_sets_) {} 72 // Number of SSIDs you can scan with a single scan request. 73 uint8_t max_num_scan_ssids; 74 // Number of SSIDs you can scan with a single scheduled scan request. 75 uint8_t max_num_sched_scan_ssids; 76 // Maximum number of sets that can be used with NL80211_ATTR_SCHED_SCAN_MATCH. 77 uint8_t max_match_sets; 78 }; 79 80 struct WiphyFeatures { WiphyFeaturesWiphyFeatures81 WiphyFeatures() 82 : supports_random_mac_oneshot_scan(false), 83 supports_random_mac_sched_scan(false) {} WiphyFeaturesWiphyFeatures84 WiphyFeatures(uint32_t feature_flags) 85 : supports_random_mac_oneshot_scan( 86 feature_flags & NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR), 87 supports_random_mac_sched_scan( 88 feature_flags & NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR) {} 89 // This device/driver supports using a random MAC address during scan 90 // (while not associated). 91 bool supports_random_mac_oneshot_scan; 92 // This device/driver supports using a random MAC address for every 93 // scan iteration during scheduled scan (while not associated). 94 bool supports_random_mac_sched_scan; 95 // There are other flags included in NL80211_ATTR_FEATURE_FLAGS. 96 // We will add them once we find them useful. 97 }; 98 99 struct StationInfo { 100 StationInfo() = default; StationInfoStationInfo101 StationInfo(uint32_t station_tx_packets_, 102 uint32_t station_tx_failed_, 103 uint32_t station_tx_bitrate_, 104 int8_t current_rssi_) 105 : station_tx_packets(station_tx_packets_), 106 station_tx_failed(station_tx_failed_), 107 station_tx_bitrate(station_tx_bitrate_), 108 current_rssi(current_rssi_) {} 109 // Number of successfully transmitted packets. 110 int32_t station_tx_packets; 111 // Number of tramsmission failures. 112 int32_t station_tx_failed; 113 // Transimission bit rate in 100kbit/s. 114 uint32_t station_tx_bitrate; 115 // Current signal strength. 116 int8_t current_rssi; 117 // There are many other counters/parameters included in station info. 118 // We will add them once we find them useful. 119 }; 120 121 class MlmeEventHandler; 122 class NetlinkManager; 123 class NL80211Packet; 124 125 // Provides NL80211 helper functions. 126 class NetlinkUtils { 127 public: 128 // Currently we only support setting the interface to STATION mode. 129 // This is used for cleaning up interface after KILLING hostapd. 130 enum InterfaceMode{ 131 STATION_MODE 132 }; 133 134 explicit NetlinkUtils(NetlinkManager* netlink_manager); 135 virtual ~NetlinkUtils(); 136 137 // Get the wiphy index from kernel. 138 // |*out_wiphy_index| returns the wiphy index from kernel. 139 // Returns true on success. 140 virtual bool GetWiphyIndex(uint32_t* out_wiphy_index); 141 142 // Get wifi interfaces info from kernel. 143 // |wiphy_index| is the wiphy index we get using GetWiphyIndex(). 144 // |interface_info| returns a vector of InterfaceInfo structs with 145 // information about all existing interfaces. 146 // Returns true on success. 147 virtual bool GetInterfaces(uint32_t wiphy_index, 148 std::vector<InterfaceInfo>* interface_info); 149 150 // Set the mode of interface. 151 // |interface_index| is the interface index. 152 // |mode| is one of the values in |enum InterfaceMode|. 153 // Returns true on success. 154 virtual bool SetInterfaceMode(uint32_t interface_index, 155 InterfaceMode mode); 156 157 // Get wiphy capability information from kernel. 158 // Returns true on success. 159 virtual bool GetWiphyInfo(uint32_t wiphy_index, 160 BandInfo* out_band_info, 161 ScanCapabilities* out_scan_capabilities, 162 WiphyFeatures* out_wiphy_features); 163 164 // Get station info from kernel. 165 // |*out_station_info]| is the struct of available station information. 166 // Returns true on success. 167 virtual bool GetStationInfo(uint32_t interface_index, 168 const std::vector<uint8_t>& mac_address, 169 StationInfo* out_station_info); 170 171 // Sign up to be notified when there is MLME event. 172 // Only one handler can be registered per interface index. 173 // New handler will replace the registered handler if they are for the 174 // same interface index. 175 // NetlinkUtils is not going to take ownership of this pointer, and that it 176 // is the caller's responsibility to make sure that the object exists for the 177 // duration of the subscription. 178 virtual void SubscribeMlmeEvent(uint32_t interface_index, 179 MlmeEventHandler* handler); 180 181 // Cancel the sign-up of receiving MLME event notification 182 // from interface with index |interface_index|. 183 virtual void UnsubscribeMlmeEvent(uint32_t interface_index); 184 185 // Sign up to be notified when there is an regulatory domain change. 186 // Only one handler can be registered per wiphy index. 187 // New handler will replace the registered handler if they are for the 188 // same wiphy index. 189 virtual void SubscribeRegDomainChange(uint32_t wiphy_index, 190 OnRegDomainChangedHandler handler); 191 192 // Cancel the sign-up of receiving regulatory domain change notification 193 // from wiphy with index |wiphy_index|. 194 virtual void UnsubscribeRegDomainChange(uint32_t wiphy_index); 195 196 // Sign up to be notified when there is an station event. 197 // Only one handler can be registered per interface index. 198 // New handler will replace the registered handler if they are for the 199 // same interface index. 200 virtual void SubscribeStationEvent(uint32_t interface_index, 201 OnStationEventHandler handler); 202 203 // Cancel the sign-up of receiving station events. 204 virtual void UnsubscribeStationEvent(uint32_t interface_index); 205 206 private: 207 bool ParseBandInfo(const NL80211Packet* const packet, 208 BandInfo* out_band_info); 209 bool ParseScanCapabilities(const NL80211Packet* const packet, 210 ScanCapabilities* out_scan_capabilities); 211 NetlinkManager* netlink_manager_; 212 213 DISALLOW_COPY_AND_ASSIGN(NetlinkUtils); 214 }; 215 216 } // namespace wificond 217 } // namespace android 218 219 #endif // WIFICOND_NET_NETLINK_UTILS_H_ 220