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