Home | History | Annotate | Download | only in net
      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 <android-base/macros.h>
     24 
     25 #include "wificond/net/kernel-header-latest/nl80211.h"
     26 #include "wificond/net/netlink_manager.h"
     27 
     28 namespace android {
     29 namespace wificond {
     30 
     31 struct InterfaceInfo {
     32   InterfaceInfo() = default;
     33   InterfaceInfo(uint32_t index_,
     34                 const std::string name_,
     35                 const std::vector<uint8_t> mac_address_)
     36       : index(index_),
     37         name(name_),
     38         mac_address(mac_address_) {}
     39   // Index of this interface.
     40   uint32_t index;
     41   // Name of this interface.
     42   std::string name;
     43   // MAC address of this interface.
     44   std::vector<uint8_t> mac_address;
     45 };
     46 
     47 struct BandInfo {
     48   BandInfo() = default;
     49   BandInfo(std::vector<uint32_t>& band_2g_,
     50            std::vector<uint32_t>& band_5g_,
     51            std::vector<uint32_t>& band_dfs_)
     52       : band_2g(band_2g_),
     53         band_5g(band_5g_),
     54         band_dfs(band_dfs_) {}
     55   // Frequencies for 2.4 GHz band.
     56   std::vector<uint32_t> band_2g;
     57   // Frequencies for 5 GHz band without DFS.
     58   std::vector<uint32_t> band_5g;
     59   // Frequencies for DFS.
     60   std::vector<uint32_t> band_dfs;
     61 };
     62 
     63 struct ScanCapabilities {
     64   ScanCapabilities() = default;
     65   ScanCapabilities(uint8_t max_num_scan_ssids_,
     66                    uint8_t max_num_sched_scan_ssids_,
     67                    uint8_t max_match_sets_,
     68                    uint32_t max_num_scan_plans_,
     69                    uint32_t max_scan_plan_interval_,
     70                    uint32_t max_scan_plan_iterations_)
     71       : max_num_scan_ssids(max_num_scan_ssids_),
     72         max_num_sched_scan_ssids(max_num_sched_scan_ssids_),
     73         max_match_sets(max_match_sets_),
     74         max_num_scan_plans(max_num_scan_plans_),
     75         max_scan_plan_interval(max_scan_plan_interval_),
     76         max_scan_plan_iterations(max_scan_plan_iterations_) {}
     77   // Number of SSIDs you can scan with a single scan request.
     78   uint8_t max_num_scan_ssids;
     79   // Number of SSIDs you can scan with a single scheduled scan request.
     80   uint8_t max_num_sched_scan_ssids;
     81   // Maximum number of sets that can be used with NL80211_ATTR_SCHED_SCAN_MATCH.
     82   uint8_t max_match_sets;
     83   // Maximum number of scan plans that can be specified.
     84   uint32_t max_num_scan_plans;
     85   // Maximum interval in seconds for a particular scan plan that can be
     86   // specified.
     87   uint32_t max_scan_plan_interval;
     88   // Maximum number of iterations for a particular scan plan that can be
     89   // specified.
     90   uint32_t max_scan_plan_iterations;
     91 };
     92 
     93 struct WiphyFeatures {
     94   WiphyFeatures()
     95       : supports_random_mac_oneshot_scan(false),
     96         supports_random_mac_sched_scan(false),
     97         supports_low_span_oneshot_scan(false),
     98         supports_low_power_oneshot_scan(false),
     99         supports_high_accuracy_oneshot_scan(false) {}
    100   WiphyFeatures(uint32_t feature_flags,
    101                 const std::vector<uint8_t>& ext_feature_flags_bytes);
    102   // This device/driver supports using a random MAC address during scan
    103   // (while not associated).
    104   bool supports_random_mac_oneshot_scan;
    105   // This device/driver supports using a random MAC address for every
    106   // scan iteration during scheduled scan (while not associated).
    107   bool supports_random_mac_sched_scan;
    108   // This device/driver supports performing low-span/low-latency one-shot scans.
    109   bool supports_low_span_oneshot_scan;
    110   // This device/driver supports performing low-power one-shot scans.
    111   bool supports_low_power_oneshot_scan;
    112   // This device/driver supports performing high-accuracy one-shot scans.
    113   bool supports_high_accuracy_oneshot_scan;
    114   // There are other flags included in NL80211_ATTR_FEATURE_FLAGS.
    115   // We will add them once we find them useful.
    116 };
    117 
    118 struct StationInfo {
    119   StationInfo() = default;
    120   StationInfo(uint32_t station_tx_packets_,
    121               uint32_t station_tx_failed_,
    122               uint32_t station_tx_bitrate_,
    123               int8_t current_rssi_)
    124       : station_tx_packets(station_tx_packets_),
    125         station_tx_failed(station_tx_failed_),
    126         station_tx_bitrate(station_tx_bitrate_),
    127         current_rssi(current_rssi_) {}
    128   // Number of successfully transmitted packets.
    129   int32_t station_tx_packets;
    130   // Number of tramsmission failures.
    131   int32_t station_tx_failed;
    132   // Transimission bit rate in 100kbit/s.
    133   uint32_t station_tx_bitrate;
    134   // Current signal strength.
    135   int8_t current_rssi;
    136   // There are many other counters/parameters included in station info.
    137   // We will add them once we find them useful.
    138 };
    139 
    140 class MlmeEventHandler;
    141 class NetlinkManager;
    142 class NL80211Packet;
    143 
    144 // Provides NL80211 helper functions.
    145 class NetlinkUtils {
    146  public:
    147   // Currently we only support setting the interface to STATION mode.
    148   // This is used for cleaning up interface after KILLING hostapd.
    149   enum InterfaceMode{
    150       STATION_MODE
    151   };
    152 
    153   explicit NetlinkUtils(NetlinkManager* netlink_manager);
    154   virtual ~NetlinkUtils();
    155 
    156   // Get the wiphy index from kernel.
    157   // |*out_wiphy_index| returns the wiphy index from kernel.
    158   // Returns true on success.
    159   virtual bool GetWiphyIndex(uint32_t* out_wiphy_index);
    160 
    161   // Get wifi interfaces info from kernel.
    162   // |wiphy_index| is the wiphy index we get using GetWiphyIndex().
    163   // |interface_info| returns a vector of InterfaceInfo structs with
    164   // information about all existing interfaces.
    165   // Returns true on success.
    166   virtual bool GetInterfaces(uint32_t wiphy_index,
    167                              std::vector<InterfaceInfo>* interface_info);
    168 
    169   // Set the mode of interface.
    170   // |interface_index| is the interface index.
    171   // |mode| is one of the values in |enum InterfaceMode|.
    172   // Returns true on success.
    173   virtual bool SetInterfaceMode(uint32_t interface_index,
    174                                 InterfaceMode mode);
    175 
    176   // Get wiphy capability information from kernel.
    177   // Returns true on success.
    178   virtual bool GetWiphyInfo(uint32_t wiphy_index,
    179                             BandInfo* out_band_info,
    180                             ScanCapabilities* out_scan_capabilities,
    181                             WiphyFeatures* out_wiphy_features);
    182 
    183   // Get station info from kernel.
    184   // |*out_station_info]| is the struct of available station information.
    185   // Returns true on success.
    186   virtual bool GetStationInfo(uint32_t interface_index,
    187                               const std::vector<uint8_t>& mac_address,
    188                               StationInfo* out_station_info);
    189 
    190   // Get a bitmap for nl80211 protocol features,
    191   // i.e. features for the nl80211 protocol rather than device features.
    192   // See enum nl80211_protocol_features in nl80211.h for decoding the bitmap.
    193   // Returns true on success.
    194   virtual bool GetProtocolFeatures(uint32_t* features);
    195 
    196   // Get current alpha2 country code from kernel.
    197   // Returns true on success.
    198   virtual bool GetCountryCode(std::string* out_country_code);
    199 
    200   // Sign up to be notified when there is MLME event.
    201   // Only one handler can be registered per interface index.
    202   // New handler will replace the registered handler if they are for the
    203   // same interface index.
    204   // NetlinkUtils is not going to take ownership of this pointer, and that it
    205   // is the caller's responsibility to make sure that the object exists for the
    206   // duration of the subscription.
    207   virtual void SubscribeMlmeEvent(uint32_t interface_index,
    208                                   MlmeEventHandler* handler);
    209 
    210   // Cancel the sign-up of receiving MLME event notification
    211   // from interface with index |interface_index|.
    212   virtual void UnsubscribeMlmeEvent(uint32_t interface_index);
    213 
    214   // Sign up to be notified when there is an regulatory domain change.
    215   // Only one handler can be registered per wiphy index.
    216   // New handler will replace the registered handler if they are for the
    217   // same wiphy index.
    218   virtual void SubscribeRegDomainChange(uint32_t wiphy_index,
    219                                         OnRegDomainChangedHandler handler);
    220 
    221   // Cancel the sign-up of receiving regulatory domain change notification
    222   // from wiphy with index |wiphy_index|.
    223   virtual void UnsubscribeRegDomainChange(uint32_t wiphy_index);
    224 
    225   // Sign up to be notified when there is a station event.
    226   // Only one handler can be registered per interface index.
    227   // New handler will replace the registered handler if they are for the
    228   // same interface index.
    229   virtual void SubscribeStationEvent(uint32_t interface_index,
    230                                      OnStationEventHandler handler);
    231 
    232   // Cancel the sign-up of receiving station events.
    233   virtual void UnsubscribeStationEvent(uint32_t interface_index);
    234 
    235   // Sign up to be notified when there is a channel switch event.
    236   // Only one handler can be registered per interface index.
    237   // New handler will replace the registered handler if they are for the
    238   // same interface index.
    239   virtual void SubscribeChannelSwitchEvent(uint32_t interface_index,
    240                                            OnChannelSwitchEventHandler handler);
    241 
    242   // Cancel the sign-up of receiving channel switch events.
    243   virtual void UnsubscribeChannelSwitchEvent(uint32_t interface_index);
    244 
    245   // Visible for testing.
    246   bool supports_split_wiphy_dump_;
    247 
    248  private:
    249   bool ParseWiphyInfoFromPacket(
    250       const NL80211Packet& packet,
    251       BandInfo* out_band_info,
    252       ScanCapabilities* out_scan_capabilities,
    253       WiphyFeatures* out_wiphy_features);
    254   bool ParseBandInfo(const NL80211Packet* const packet,
    255                      BandInfo* out_band_info);
    256   bool ParseScanCapabilities(const NL80211Packet* const packet,
    257                              ScanCapabilities* out_scan_capabilities);
    258 
    259   bool MergePacketsForSplitWiphyDump(
    260       const std::vector<std::unique_ptr<const NL80211Packet>>& split_dump_info,
    261       std::vector<NL80211Packet>* packet_per_wiphy);
    262 
    263   NetlinkManager* netlink_manager_;
    264 
    265   DISALLOW_COPY_AND_ASSIGN(NetlinkUtils);
    266 };
    267 
    268 }  // namespace wificond
    269 }  // namespace android
    270 
    271 #endif  // WIFICOND_NET_NETLINK_UTILS_H_
    272