blob: e182fe7715d377c61e8b0ff5387d2741937ccd19 [file] [log] [blame]
Thieu Lee41a72d2012-02-06 20:46:51 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
mukesh agrawalb54601c2011-06-07 17:39:22 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SHILL_WIFI_ENDPOINT_
6#define SHILL_WIFI_ENDPOINT_
7
8#include <map>
mukesh agrawal6e277772011-09-29 15:04:23 -07009#include <set>
mukesh agrawalb54601c2011-06-07 17:39:22 -070010#include <string>
11#include <vector>
12
13#include <base/memory/ref_counted.h>
14#include <dbus-c++/dbus.h>
mukesh agrawal6e277772011-09-29 15:04:23 -070015#include <gtest/gtest_prod.h> // for FRIEND_TEST
mukesh agrawalb54601c2011-06-07 17:39:22 -070016
17#include "shill/endpoint.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070018#include "shill/event_dispatcher.h"
Thieu Le1df7f4e2012-02-10 15:21:45 -080019#include "shill/metrics.h"
mukesh agrawalb20776f2012-02-10 16:00:36 -080020#include "shill/refptr_types.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070021
22namespace shill {
23
mukesh agrawalb20776f2012-02-10 16:00:36 -080024class ProxyFactory;
25class SupplicantBSSProxyInterface;
26
mukesh agrawalb54601c2011-06-07 17:39:22 -070027class WiFiEndpoint : public Endpoint {
28 public:
Paul Stewartaffc0552013-03-25 07:50:15 -070029 struct SecurityFlags {
30 SecurityFlags()
31 : rsn_8021x(false),
32 rsn_psk(false),
33 wpa_8021x(false),
34 wpa_psk(false),
35 privacy(false) {}
36 bool rsn_8021x;
37 bool rsn_psk;
38 bool wpa_8021x;
39 bool wpa_psk;
40 bool privacy;
41 };
Paul Stewart72b2fdc2012-06-02 08:58:51 -070042 struct VendorInformation {
43 std::string wps_manufacturer;
44 std::string wps_model_name;
45 std::string wps_model_number;
46 std::string wps_device_name;
47 std::set<uint32_t> oui_list;
48 };
mukesh agrawalb20776f2012-02-10 16:00:36 -080049 WiFiEndpoint(ProxyFactory *proxy_factory,
50 const WiFiRefPtr &device,
51 const std::string &rpc_id,
52 const std::map<std::string, ::DBus::Variant> &properties);
mukesh agrawalb54601c2011-06-07 17:39:22 -070053 virtual ~WiFiEndpoint();
Chris Masone092df3e2011-08-22 09:41:39 -070054
mukesh agrawalb20776f2012-02-10 16:00:36 -080055 // Set up RPC channel. Broken out from the ctor, so that WiFi can
56 // look over the Endpoint details before commiting to setting up
57 // RPC.
58 virtual void Start();
59
60 // Called by SupplicantBSSProxy, in response to events from
61 // wpa_supplicant.
62 void PropertiesChanged(
63 const std::map<std::string, ::DBus::Variant> &properties);
64
Chris Masone092df3e2011-08-22 09:41:39 -070065 // Maps mode strings from flimflam's nomenclature, as defined
66 // in chromeos/dbus/service_constants.h, to uints used by supplicant
67 static uint32_t ModeStringToUint(const std::string &mode_string);
68
Paul Stewart72b2fdc2012-06-02 08:58:51 -070069 // Returns a stringmap containing information gleaned about the
70 // vendor of this AP.
71 std::map<std::string, std::string> GetVendorInformation() const;
72
mukesh agrawalb54601c2011-06-07 17:39:22 -070073 const std::vector<uint8_t> &ssid() const;
74 const std::string &ssid_string() const;
75 const std::string &ssid_hex() const;
76 const std::string &bssid_string() const;
77 const std::string &bssid_hex() const;
Paul Stewartbdbd3c32013-04-17 09:47:21 -070078 const std::string &country_code() const;
Paul Stewart3c504012013-01-17 17:49:58 -080079 const WiFiRefPtr &device() const;
mukesh agrawalb54601c2011-06-07 17:39:22 -070080 int16_t signal_strength() const;
Thieu Lee41a72d2012-02-06 20:46:51 +000081 uint16 frequency() const;
Thieu Le1df7f4e2012-02-10 15:21:45 -080082 uint16 physical_mode() const;
Chris Masone092df3e2011-08-22 09:41:39 -070083 const std::string &network_mode() const;
mukesh agrawal6e277772011-09-29 15:04:23 -070084 const std::string &security_mode() const;
Paul Stewarta5e7d5f2013-01-09 18:06:15 -080085 bool ieee80211w_required() const;
mukesh agrawal43970a22013-02-15 16:00:07 -080086 bool has_rsn_property() const;
87 bool has_wpa_property() const;
mukesh agrawalb54601c2011-06-07 17:39:22 -070088
89 private:
mukesh agrawal6e277772011-09-29 15:04:23 -070090 friend class WiFiEndpointTest;
Paul Stewarte369ece2012-05-22 09:11:03 -070091 friend class WiFiObjectTest; // for MakeOpenEndpoint
Paul Stewart3c504012013-01-17 17:49:58 -080092 friend class WiFiProviderTest; // for MakeOpenEndpoint
mukesh agrawal8a3188d2011-12-01 20:56:44 +000093 friend class WiFiServiceTest; // for MakeOpenEndpoint
mukesh agrawal6e277772011-09-29 15:04:23 -070094 // these test cases need access to the KeyManagement enum
Paul Stewart72b2fdc2012-06-02 08:58:51 -070095 FRIEND_TEST(WiFiEndpointTest, DeterminePhyModeFromFrequency);
mukesh agrawal6e277772011-09-29 15:04:23 -070096 FRIEND_TEST(WiFiEndpointTest, ParseKeyManagementMethodsEAP);
97 FRIEND_TEST(WiFiEndpointTest, ParseKeyManagementMethodsPSK);
98 FRIEND_TEST(WiFiEndpointTest, ParseKeyManagementMethodsEAPAndPSK);
Paul Stewart08a54eb2013-03-11 12:07:36 -070099 FRIEND_TEST(WiFiProviderTest, OnEndpointAddedWithSecurity);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800100 FRIEND_TEST(WiFiServiceTest, ConnectTaskWPA80211w);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800101 FRIEND_TEST(WiFiServiceUpdateFromEndpointsTest, EndpointModified);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800102 FRIEND_TEST(WiFiServiceUpdateFromEndpointsTest, Ieee80211w);
mukesh agrawalf6b32092013-04-10 15:49:55 -0700103 // for physical_mode_
104 FRIEND_TEST(WiFiServiceUpdateFromEndpointsTest, PhysicalMode);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700105
mukesh agrawal6e277772011-09-29 15:04:23 -0700106 enum KeyManagement {
107 kKeyManagement802_1x,
108 kKeyManagementPSK
109 };
mukesh agrawalb54601c2011-06-07 17:39:22 -0700110
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000111 // Build a simple WiFiEndpoint, for testing purposes.
mukesh agrawal43970a22013-02-15 16:00:07 -0800112 static WiFiEndpoint *MakeEndpoint(ProxyFactory *proxy_factory,
113 const WiFiRefPtr &wifi,
114 const std::string &ssid,
115 const std::string &bssid,
116 const std::string &network_mode,
117 uint16 frequency,
118 int16 signal_dbm,
119 bool has_wpa_property,
120 bool has_rsn_property);
121 // As above, but with the last two parameters false.
mukesh agrawalb20776f2012-02-10 16:00:36 -0800122 static WiFiEndpoint *MakeOpenEndpoint(ProxyFactory *proxy_factory,
123 const WiFiRefPtr &wifi,
124 const std::string &ssid,
mukesh agrawale1d90e92012-02-15 17:36:08 -0800125 const std::string &bssid,
Paul Stewart3c504012013-01-17 17:49:58 -0800126 const std::string &network_mode,
mukesh agrawale1d90e92012-02-15 17:36:08 -0800127 uint16 frequency,
128 int16 signal_dbm);
Chris Masone092df3e2011-08-22 09:41:39 -0700129 // Maps mode strings from supplicant into flimflam's nomenclature, as defined
Paul Stewartaffc0552013-03-25 07:50:15 -0700130 // in chromeos/dbus/service_constants.h.
Chris Masone092df3e2011-08-22 09:41:39 -0700131 static const char *ParseMode(const std::string &mode_string);
Paul Stewartaffc0552013-03-25 07:50:15 -0700132 // Parses an Endpoint's properties to identify an approprirate flimflam
133 // security property value, as defined in chromeos/dbus/service_constants.h.
134 // The stored data in the |flags| parameter is merged with the provided
135 // properties, and the security value returned is the result of the
136 // merger.
mukesh agrawal6e277772011-09-29 15:04:23 -0700137 static const char *ParseSecurity(
Paul Stewartaffc0552013-03-25 07:50:15 -0700138 const std::map<std::string, ::DBus::Variant> &properties,
139 SecurityFlags *flags);
mukesh agrawalb20776f2012-02-10 16:00:36 -0800140 // Parses an Endpoint's properties' "RSN" or "WPA" sub-dictionary, to
mukesh agrawal6e277772011-09-29 15:04:23 -0700141 // identify supported key management methods (802.1x or PSK).
142 static void ParseKeyManagementMethods(
143 const std::map<std::string, ::DBus::Variant> &security_method_properties,
144 std::set<KeyManagement> *key_management_methods);
Thieu Le1df7f4e2012-02-10 15:21:45 -0800145 // Determine the negotiated operating mode for the channel by looking at
146 // the information elements, frequency and data rates. The information
147 // elements and data rates live in |properties|.
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700148 static Metrics::WiFiNetworkPhyMode DeterminePhyModeFromFrequency(
Thieu Le1df7f4e2012-02-10 15:21:45 -0800149 const std::map<std::string, ::DBus::Variant> &properties,
150 uint16 frequency);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800151 // Parse information elements to determine the physical mode, vendor
152 // information and IEEE 802.11w requirement information associated
153 // with the AP. Returns true if a physical mode was determined from
154 // the IE elements, false otherwise.
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700155 static bool ParseIEs(const std::map<std::string, ::DBus::Variant> &properties,
156 Metrics::WiFiNetworkPhyMode *phy_mode,
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800157 VendorInformation *vendor_information,
Paul Stewartbdbd3c32013-04-17 09:47:21 -0700158 bool *ieee80211w_required, std::string *country_code);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800159 // Parse a WPA information element and set *|ieee80211w_required| to true
160 // if IEEE 802.11w is required by this AP.
161 static void ParseWPACapabilities(std::vector<uint8_t>::const_iterator ie,
162 std::vector<uint8_t>::const_iterator end,
163 bool *ieee80211w_required);
164 // Parse a single vendor information element. If this is a WPA vendor
165 // element, call ParseWPACapabilites with |ieee80211w_required|.
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700166 static void ParseVendorIE(std::vector<uint8_t>::const_iterator ie,
167 std::vector<uint8_t>::const_iterator end,
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800168 VendorInformation *vendor_information,
169 bool *ieee80211w_required);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700170
Paul Stewart08a54eb2013-03-11 12:07:36 -0700171 // Private setter used in unit tests.
172 void set_security_mode(const std::string &mode) { security_mode_ = mode; }
173
mukesh agrawal6e277772011-09-29 15:04:23 -0700174 // TODO(quiche): make const?
mukesh agrawalb54601c2011-06-07 17:39:22 -0700175 std::vector<uint8_t> ssid_;
176 std::vector<uint8_t> bssid_;
177 std::string ssid_string_;
178 std::string ssid_hex_;
179 std::string bssid_string_;
180 std::string bssid_hex_;
Paul Stewartbdbd3c32013-04-17 09:47:21 -0700181 std::string country_code_;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800182 int16 signal_strength_;
Thieu Lee41a72d2012-02-06 20:46:51 +0000183 uint16 frequency_;
Thieu Le1df7f4e2012-02-10 15:21:45 -0800184 uint16 physical_mode_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700185 // network_mode_ and security_mode_ are represented as flimflam names
186 // (not necessarily the same as wpa_supplicant names)
Chris Masone092df3e2011-08-22 09:41:39 -0700187 std::string network_mode_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700188 std::string security_mode_;
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700189 VendorInformation vendor_information_;
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800190 bool ieee80211w_required_;
mukesh agrawal43970a22013-02-15 16:00:07 -0800191 bool has_rsn_property_;
192 bool has_wpa_property_;
Paul Stewartaffc0552013-03-25 07:50:15 -0700193 SecurityFlags security_flags_;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700194
mukesh agrawalb20776f2012-02-10 16:00:36 -0800195 ProxyFactory *proxy_factory_;
196 WiFiRefPtr device_;
197 std::string rpc_id_;
198 scoped_ptr<SupplicantBSSProxyInterface> supplicant_bss_proxy_;
199
mukesh agrawalb54601c2011-06-07 17:39:22 -0700200 DISALLOW_COPY_AND_ASSIGN(WiFiEndpoint);
201};
202
203} // namespace shill
204
205#endif // SHILL_WIFI_ENDPOINT_