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 <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;
     34   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;
     50   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;
     66   ScanCapabilities(uint8_t max_num_scan_ssids_,
     67                    uint8_t max_num_sched_scan_ssids_,
     68                    uint8_t max_match_sets_,
     69                    uint32_t max_num_scan_plans_,
     70                    uint32_t max_scan_plan_interval_,
     71                    uint32_t max_scan_plan_iterations_)
     72       : max_num_scan_ssids(max_num_scan_ssids_),
     73         max_num_sched_scan_ssids(max_num_sched_scan_ssids_),
     74         max_match_sets(max_match_sets_),
     75         max_num_scan_plans(max_num_scan_plans_),
     76         max_scan_plan_interval(max_scan_plan_interval_),
     77         max_scan_plan_iterations(max_scan_plan_iterations_) {}
     78   // Number of SSIDs you can scan with a single scan request.
     79   uint8_t max_num_scan_ssids;
     80   // Number of SSIDs you can scan with a single scheduled scan request.
     81   uint8_t max_num_sched_scan_ssids;
     82   // Maximum number of sets that can be used with NL80211_ATTR_SCHED_SCAN_MATCH.
     83   uint8_t max_match_sets;
     84   // Maximum number of scan plans that can be specified.
     85   uint32_t max_num_scan_plans;
     86   // Maximum interval in seconds for a particular scan plan that can be
     87   // specified.
     88   uint32_t max_scan_plan_interval;
     89   // Maximum number of iterations for a particular scan plan that can be
     90   // specified.
     91   uint32_t max_scan_plan_iterations;
     92 };
     93 
     94 struct WiphyFeatures {
     95   WiphyFeatures()
     96       : supports_random_mac_oneshot_scan(false),
     97         supports_random_mac_sched_scan(false) {}
     98   WiphyFeatures(uint32_t feature_flags)
     99       : supports_random_mac_oneshot_scan(
    100             feature_flags & NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR),
    101         supports_random_mac_sched_scan(
    102             feature_flags & NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR) {}
    103   // This device/driver supports using a random MAC address during scan
    104   // (while not associated).
    105   bool supports_random_mac_oneshot_scan;
    106   // This device/driver supports using a random MAC address for every
    107   // scan iteration during scheduled scan (while not associated).
    108   bool supports_random_mac_sched_scan;
    109   // There are other flags included in NL80211_ATTR_FEATURE_FLAGS.
    110   // We will add them once we find them useful.
    111 };
    112 
    113 struct StationInfo {
    114   StationInfo() = default;
    115   StationInfo(uint32_t station_tx_packets_,
    116               uint32_t station_tx_failed_,
    117               uint32_t station_tx_bitrate_,
    118               int8_t current_rssi_)
    119       : station_tx_packets(station_tx_packets_),
    120         station_tx_failed(station_tx_failed_),
    121         station_tx_bitrate(station_tx_bitrate_),
    122         current_rssi(current_rssi_) {}
    123   // Number of successfully transmitted packets.
    124   int32_t station_tx_packets;
    125   // Number of tramsmission failures.
    126   int32_t station_tx_failed;
    127   // Transimission bit rate in 100kbit/s.
    128   uint32_t station_tx_bitrate;
    129   // Current signal strength.
    130   int8_t current_rssi;
    131   // There are many other counters/parameters included in station info.
    132   // We will add them once we find them useful.
    133 };
    134 
    135 class MlmeEventHandler;
    136 class NetlinkManager;
    137 class NL80211Packet;
    138 
    139 // Provides NL80211 helper functions.
    140 class NetlinkUtils {
    141  public:
    142   // Currently we only support setting the interface to STATION mode.
    143   // This is used for cleaning up interface after KILLING hostapd.
    144   enum InterfaceMode{
    145       STATION_MODE
    146   };
    147 
    148   explicit NetlinkUtils(NetlinkManager* netlink_manager);
    149   virtual ~NetlinkUtils();
    150 
    151   // Get the wiphy index from kernel.
    152   // |*out_wiphy_index| returns the wiphy index from kernel.
    153   // Returns true on success.
    154   virtual bool GetWiphyIndex(uint32_t* out_wiphy_index);
    155 
    156   // Get wifi interfaces info from kernel.
    157   // |wiphy_index| is the wiphy index we get using GetWiphyIndex().
    158   // |interface_info| returns a vector of InterfaceInfo structs with
    159   // information about all existing interfaces.
    160   // Returns true on success.
    161   virtual bool GetInterfaces(uint32_t wiphy_index,
    162                              std::vector<InterfaceInfo>* interface_info);
    163 
    164   // Set the mode of interface.
    165   // |interface_index| is the interface index.
    166   // |mode| is one of the values in |enum InterfaceMode|.
    167   // Returns true on success.
    168   virtual bool SetInterfaceMode(uint32_t interface_index,
    169                                 InterfaceMode mode);
    170 
    171   // Get wiphy capability information from kernel.
    172   // Returns true on success.
    173   virtual bool GetWiphyInfo(uint32_t wiphy_index,
    174                             BandInfo* out_band_info,
    175                             ScanCapabilities* out_scan_capabilities,
    176                             WiphyFeatures* out_wiphy_features);
    177 
    178   // Get station info from kernel.
    179   // |*out_station_info]| is the struct of available station information.
    180   // Returns true on success.
    181   virtual bool GetStationInfo(uint32_t interface_index,
    182                               const std::vector<uint8_t>& mac_address,
    183                               StationInfo* out_station_info);
    184 
    185   // Sign up to be notified when there is MLME event.
    186   // Only one handler can be registered per interface index.
    187   // New handler will replace the registered handler if they are for the
    188   // same interface index.
    189   // NetlinkUtils is not going to take ownership of this pointer, and that it
    190   // is the caller's responsibility to make sure that the object exists for the
    191   // duration of the subscription.
    192   virtual void SubscribeMlmeEvent(uint32_t interface_index,
    193                                   MlmeEventHandler* handler);
    194 
    195   // Cancel the sign-up of receiving MLME event notification
    196   // from interface with index |interface_index|.
    197   virtual void UnsubscribeMlmeEvent(uint32_t interface_index);
    198 
    199   // Sign up to be notified when there is an regulatory domain change.
    200   // Only one handler can be registered per wiphy index.
    201   // New handler will replace the registered handler if they are for the
    202   // same wiphy index.
    203   virtual void SubscribeRegDomainChange(uint32_t wiphy_index,
    204                                         OnRegDomainChangedHandler handler);
    205 
    206   // Cancel the sign-up of receiving regulatory domain change notification
    207   // from wiphy with index |wiphy_index|.
    208   virtual void UnsubscribeRegDomainChange(uint32_t wiphy_index);
    209 
    210   // Sign up to be notified when there is an station event.
    211   // Only one handler can be registered per interface index.
    212   // New handler will replace the registered handler if they are for the
    213   // same interface index.
    214   virtual void SubscribeStationEvent(uint32_t interface_index,
    215                                      OnStationEventHandler handler);
    216 
    217   // Cancel the sign-up of receiving station events.
    218   virtual void UnsubscribeStationEvent(uint32_t interface_index);
    219 
    220  private:
    221   bool ParseBandInfo(const NL80211Packet* const packet,
    222                      BandInfo* out_band_info);
    223   bool ParseScanCapabilities(const NL80211Packet* const packet,
    224                              ScanCapabilities* out_scan_capabilities);
    225   NetlinkManager* netlink_manager_;
    226 
    227   DISALLOW_COPY_AND_ASSIGN(NetlinkUtils);
    228 };
    229 
    230 }  // namespace wificond
    231 }  // namespace android
    232 
    233 #endif  // WIFICOND_NET_NETLINK_UTILS_H_
    234