Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "apmanager/config.h" |
| 6 | |
| 7 | #include <base/strings/stringprintf.h> |
| 8 | #include <chromeos/dbus/service_constants.h> |
| 9 | |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame^] | 10 | using chromeos::dbus_utils::AsyncEventSequencer; |
| 11 | using chromeos::dbus_utils::ExportedObjectManager; |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 12 | using chromeos::ErrorPtr; |
| 13 | using std::string; |
| 14 | |
| 15 | namespace apmanager { |
| 16 | |
| 17 | // static |
| 18 | const char Config::kHostapdConfigKeyBridgeInterface[] = "bridge"; |
| 19 | const char Config::kHostapdConfigKeyChannel[] = "channel"; |
| 20 | const char Config::kHostapdConfigKeyControlInterface[] = "ctrl_interface"; |
| 21 | const char Config::kHostapdConfigKeyDriver[] = "driver"; |
| 22 | const char Config::kHostapdConfigKeyFragmThreshold[] = "fragm_threshold"; |
| 23 | const char Config::kHostapdConfigKeyHwMode[] = "hw_mode"; |
| 24 | const char Config::kHostapdConfigKeyIeee80211ac[] = "ieee80211ac"; |
| 25 | const char Config::kHostapdConfigKeyIeee80211n[] = "ieee80211n"; |
| 26 | const char Config::kHostapdConfigKeyIgnoreBroadcastSsid[] = |
| 27 | "ignore_broadcast_ssid"; |
| 28 | const char Config::kHostapdConfigKeyInterface[] = "interface"; |
| 29 | const char Config::kHostapdConfigKeyRsnPairwise[] = "rsn_pairwise"; |
| 30 | const char Config::kHostapdConfigKeyRtsThreshold[] = "rts_threshold"; |
| 31 | const char Config::kHostapdConfigKeySsid[] = "ssid"; |
| 32 | const char Config::kHostapdConfigKeyWepDefaultKey[] = "wep_default_key"; |
| 33 | const char Config::kHostapdConfigKeyWepKey0[] = "wep_key0"; |
| 34 | const char Config::kHostapdConfigKeyWpa[] = "wpa"; |
| 35 | const char Config::kHostapdConfigKeyWpaKeyMgmt[] = "wpa_key_mgmt"; |
| 36 | const char Config::kHostapdConfigKeyWpaPassphrase[] = "wpa_passphrase"; |
| 37 | |
| 38 | const char Config::kHostapdHwMode80211a[] = "a"; |
| 39 | const char Config::kHostapdHwMode80211b[] = "b"; |
| 40 | const char Config::kHostapdHwMode80211g[] = "g"; |
| 41 | |
| 42 | // static |
| 43 | const uint16_t Config::kPropertyDefaultChannel = 6; |
| 44 | const uint16_t Config::kPropertyDefaultServerAddressIndex = 0; |
| 45 | const bool Config::kPropertyDefaultHiddenNetwork = false; |
| 46 | |
| 47 | // static |
| 48 | const char Config::kHostapdDefaultDriver[] = "nl80211"; |
| 49 | const char Config::kHostapdDefaultRsnPairwise[] = "CCMP"; |
| 50 | const char Config::kHostapdDefaultWpaKeyMgmt[] = "WPA-PSK"; |
| 51 | // Fragmentation threshold: disabled. |
| 52 | const int Config::kHostapdDefaultFragmThreshold = 2346; |
| 53 | // RTS threshold: disabled. |
| 54 | const int Config::kHostapdDefaultRtsThreshold = 2347; |
| 55 | |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame^] | 56 | Config::Config(const string& service_path) |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 57 | : org::chromium::apmanager::ConfigAdaptor(this), |
| 58 | dbus_path_(dbus::ObjectPath( |
| 59 | base::StringPrintf("%s/config", service_path.c_str()))) { |
| 60 | // Initialize default configuration values. |
| 61 | SetSecurityMode(kSecurityModeNone); |
| 62 | SetHwMode(kHwMode80211g); |
| 63 | SetOperationMode(kOperationModeServer); |
| 64 | SetServerAddressIndex(kPropertyDefaultServerAddressIndex); |
| 65 | SetChannel(kPropertyDefaultChannel); |
| 66 | SetHiddenNetwork(kPropertyDefaultHiddenNetwork); |
| 67 | } |
| 68 | |
| 69 | Config::~Config() {} |
| 70 | |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame^] | 71 | void Config::RegisterAsync(ExportedObjectManager* object_manager, |
| 72 | AsyncEventSequencer* sequencer) { |
| 73 | CHECK(!dbus_object_) << "Already registered"; |
| 74 | dbus_object_.reset( |
| 75 | new chromeos::dbus_utils::DBusObject( |
| 76 | object_manager, |
| 77 | object_manager ? object_manager->GetBus() : nullptr, |
| 78 | dbus_path_)); |
| 79 | RegisterWithDBusObject(dbus_object_.get()); |
| 80 | dbus_object_->RegisterAsync( |
| 81 | sequencer->GetHandler("Config.RegisterAsync() failed.", true)); |
| 82 | } |
| 83 | |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 84 | bool Config::GenerateConfigFile(ErrorPtr* error, string* config_str) { |
| 85 | // SSID. |
| 86 | string ssid = GetSsid(); |
| 87 | if (ssid.empty()) { |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame^] | 88 | chromeos::Error::AddTo( |
| 89 | error, FROM_HERE, chromeos::errors::dbus::kDomain, kConfigError, |
| 90 | "SSID not specified"); |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | base::StringAppendF( |
| 94 | config_str, "%s=%s\n", kHostapdConfigKeySsid, ssid.c_str()); |
| 95 | |
| 96 | // Channel. |
| 97 | base::StringAppendF( |
| 98 | config_str, "%s=%d\n", kHostapdConfigKeyChannel, GetChannel()); |
| 99 | |
| 100 | // Hardware mode. |
| 101 | if (!AppendHwMode(error, config_str)) { |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | // Interface. |
| 106 | if (!AppendInterface(error, config_str)) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | // Security mode configurations. |
| 111 | if (!AppendSecurityMode(error, config_str)) { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | // Hostapd default configurations. |
| 116 | if (!AppendHostapdDefaults(error, config_str)) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | return true; |
| 121 | } |
| 122 | |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame^] | 123 | bool Config::AppendHwMode(ErrorPtr* error, std::string* config_str) { |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 124 | string hw_mode = GetHwMode(); |
| 125 | string hostapd_hw_mode; |
| 126 | if (hw_mode == kHwMode80211a) { |
| 127 | hostapd_hw_mode = kHostapdHwMode80211a; |
| 128 | } else if (hw_mode == kHwMode80211b) { |
| 129 | hostapd_hw_mode = kHostapdHwMode80211b; |
| 130 | } else if (hw_mode == kHwMode80211g) { |
| 131 | hostapd_hw_mode = kHostapdHwMode80211g; |
| 132 | } else if (hw_mode == kHwMode80211n) { |
| 133 | // Use 802.11a for 5GHz channel and 802.11g for 2.4GHz channel |
| 134 | if (GetChannel() >= 34) { |
| 135 | hostapd_hw_mode = kHostapdHwMode80211a; |
| 136 | } else { |
| 137 | hostapd_hw_mode = kHostapdHwMode80211g; |
| 138 | } |
| 139 | base::StringAppendF(config_str, "%s=1\n", kHostapdConfigKeyIeee80211n); |
| 140 | |
| 141 | // TODO(zqiu): Determine HT Capabilities based on the interface PHY's |
| 142 | // capababilites. |
| 143 | } else if (hw_mode == kHwMode80211ac) { |
| 144 | if (GetChannel() >= 34) { |
| 145 | hostapd_hw_mode = kHostapdHwMode80211a; |
| 146 | } else { |
| 147 | hostapd_hw_mode = kHostapdHwMode80211g; |
| 148 | } |
| 149 | base::StringAppendF(config_str, "%s=1\n", kHostapdConfigKeyIeee80211ac); |
| 150 | |
| 151 | // TODO(zqiu): Determine VHT Capabilities based on the interface PHY's |
| 152 | // capababilites. |
| 153 | } else { |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame^] | 154 | chromeos::Error::AddToPrintf( |
| 155 | error, FROM_HERE, chromeos::errors::dbus::kDomain, kConfigError, |
| 156 | "Invalid hardware mode: %s", hw_mode.c_str()); |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | |
| 160 | base::StringAppendF( |
| 161 | config_str, "%s=%s\n", kHostapdConfigKeyHwMode, hostapd_hw_mode.c_str()); |
| 162 | return true; |
| 163 | } |
| 164 | |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame^] | 165 | bool Config::AppendHostapdDefaults(ErrorPtr* error, |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 166 | std::string* config_str) { |
| 167 | // Driver: NL80211. |
| 168 | base::StringAppendF( |
| 169 | config_str, "%s=%s\n", kHostapdConfigKeyDriver, kHostapdDefaultDriver); |
| 170 | |
| 171 | // Fragmentation threshold: disabled. |
| 172 | base::StringAppendF(config_str, |
| 173 | "%s=%d\n", |
| 174 | kHostapdConfigKeyFragmThreshold, |
| 175 | kHostapdDefaultFragmThreshold); |
| 176 | |
| 177 | // RTS threshold: disabled. |
| 178 | base::StringAppendF(config_str, |
| 179 | "%s=%d\n", |
| 180 | kHostapdConfigKeyRtsThreshold, |
| 181 | kHostapdDefaultRtsThreshold); |
| 182 | |
| 183 | return true; |
| 184 | } |
| 185 | |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame^] | 186 | bool Config::AppendInterface(ErrorPtr* error, |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 187 | std::string* config_str) { |
| 188 | string interface = GetInterfaceName(); |
| 189 | if (interface.empty()) { |
| 190 | // TODO(zqiu): Ask manager for available ap mode interface. |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | base::StringAppendF( |
| 195 | config_str, "%s=%s\n", kHostapdConfigKeyInterface, interface.c_str()); |
| 196 | return true; |
| 197 | } |
| 198 | |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame^] | 199 | bool Config::AppendSecurityMode(ErrorPtr* error, |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 200 | std::string* config_str) { |
| 201 | string security_mode = GetSecurityMode(); |
| 202 | if (security_mode == kSecurityModeNone) { |
| 203 | // Nothing need to be done for open network. |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | if (security_mode == kSecurityModeRSN) { |
| 208 | string passphrase = GetPassphrase(); |
| 209 | if (passphrase.empty()) { |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame^] | 210 | chromeos::Error::AddToPrintf( |
| 211 | error, FROM_HERE, chromeos::errors::dbus::kDomain, kConfigError, |
| 212 | "Passphrase not set for security mode: %s", security_mode.c_str()); |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 213 | return false; |
| 214 | } |
| 215 | |
| 216 | base::StringAppendF(config_str, "%s=2\n", kHostapdConfigKeyWpa); |
| 217 | base::StringAppendF(config_str, |
| 218 | "%s=%s\n", |
| 219 | kHostapdConfigKeyRsnPairwise, |
| 220 | kHostapdDefaultRsnPairwise); |
| 221 | base::StringAppendF(config_str, |
| 222 | "%s=%s\n", |
| 223 | kHostapdConfigKeyWpaKeyMgmt, |
| 224 | kHostapdDefaultWpaKeyMgmt); |
| 225 | base::StringAppendF(config_str, |
| 226 | "%s=%s\n", |
| 227 | kHostapdConfigKeyWpaPassphrase, |
| 228 | passphrase.c_str()); |
| 229 | return true; |
| 230 | } |
| 231 | |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 232 | chromeos::Error::AddToPrintf( |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame^] | 233 | error, FROM_HERE, chromeos::errors::dbus::kDomain, kConfigError, |
| 234 | "Invalid security mode: %s", security_mode.c_str()); |
| 235 | return false; |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | } // namespace apmanager |