blob: f836f2d38b95bca1d6ed8b4f3487e1ab8e72fe02 [file] [log] [blame]
mukesh agrawalddc378f2012-02-17 18:26:20 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewartdd60e452011-08-08 11:38:36 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/connection.h"
6
Paul Stewart9a908082011-08-31 12:18:48 -07007#include <arpa/inet.h>
8#include <linux/rtnetlink.h>
9
10#include "shill/device_info.h"
Paul Stewartdd60e452011-08-08 11:38:36 -070011#include "shill/resolver.h"
12#include "shill/routing_table.h"
13#include "shill/rtnl_handler.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070014#include "shill/scope_logger.h"
Paul Stewartdd60e452011-08-08 11:38:36 -070015
16using std::string;
17
18namespace shill {
19
20// static
21const uint32 Connection::kDefaultMetric = 1;
22// static
Paul Stewart7cfca042011-12-08 14:18:17 -080023const uint32 Connection::kNonDefaultMetricBase = 10;
Paul Stewartdd60e452011-08-08 11:38:36 -070024
Paul Stewart9a908082011-08-31 12:18:48 -070025Connection::Connection(int interface_index,
26 const std::string& interface_name,
Paul Stewarte00600e2012-03-16 07:08:00 -070027 Technology::Identifier technology,
Paul Stewart9a908082011-08-31 12:18:48 -070028 const DeviceInfo *device_info)
Paul Stewartdd60e452011-08-08 11:38:36 -070029 : is_default_(false),
Paul Stewartc8f4bef2011-12-13 09:45:51 -080030 routing_request_count_(0),
Paul Stewartdd60e452011-08-08 11:38:36 -070031 interface_index_(interface_index),
32 interface_name_(interface_name),
Paul Stewarte00600e2012-03-16 07:08:00 -070033 technology_(technology),
Paul Stewart9a908082011-08-31 12:18:48 -070034 device_info_(device_info),
Paul Stewartdd60e452011-08-08 11:38:36 -070035 resolver_(Resolver::GetInstance()),
36 routing_table_(RoutingTable::GetInstance()),
37 rtnl_handler_(RTNLHandler::GetInstance()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070038 SLOG(Connection, 2) << __func__ << "(" << interface_index << ", "
39 << interface_name << ", "
40 << Technology::NameFromIdentifier(technology) << ")";
Paul Stewartdd60e452011-08-08 11:38:36 -070041}
42
43Connection::~Connection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070044 SLOG(Connection, 2) << __func__ << " " << interface_name_;
Paul Stewart9a908082011-08-31 12:18:48 -070045
Paul Stewartc8f4bef2011-12-13 09:45:51 -080046 DCHECK(!routing_request_count_);
Thieu Lefb46caf2012-03-08 11:57:15 -080047 routing_table_->FlushRoutes(interface_index_);
Paul Stewarte93b0382012-04-24 13:11:28 -070048 routing_table_->FlushRoutesWithTag(interface_index_);
Paul Stewart9a908082011-08-31 12:18:48 -070049 device_info_->FlushAddresses(interface_index_);
Paul Stewartdd60e452011-08-08 11:38:36 -070050}
51
52void Connection::UpdateFromIPConfig(const IPConfigRefPtr &config) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070053 SLOG(Connection, 2) << __func__ << " " << interface_name_;
Paul Stewarte6132022011-08-16 09:11:02 -070054
Paul Stewart9a908082011-08-31 12:18:48 -070055 const IPConfig::Properties &properties = config->properties();
Paul Stewarte93b0382012-04-24 13:11:28 -070056 if (!properties.trusted_ip.empty() && !PinHostRoute(properties)) {
57 LOG(ERROR) << "Unable to pin host route to " << properties.trusted_ip;
58 return;
59 }
60
Paul Stewart9a908082011-08-31 12:18:48 -070061 IPAddress local(properties.address_family);
62 if (!local.SetAddressFromString(properties.address)) {
63 LOG(ERROR) << "Local address " << properties.address << " is invalid";
64 return;
65 }
Paul Stewart48100b02012-03-19 07:53:52 -070066 local.set_prefix(properties.subnet_prefix);
Paul Stewart9a908082011-08-31 12:18:48 -070067
68 IPAddress broadcast(properties.address_family);
Paul Stewarte00600e2012-03-16 07:08:00 -070069 if (!broadcast.SetAddressFromString(properties.broadcast_address) &&
Darin Petkov273028a2012-03-19 10:20:46 +010070 technology_ != Technology::kVPN) {
Paul Stewart9a908082011-08-31 12:18:48 -070071 LOG(ERROR) << "Broadcast address " << properties.broadcast_address
72 << " is invalid";
73 return;
74 }
75
Paul Stewart48100b02012-03-19 07:53:52 -070076 IPAddress peer(properties.address_family);
77 if (!properties.peer_address.empty() &&
78 !peer.SetAddressFromString(properties.peer_address)) {
79 LOG(ERROR) << "Peer address " << properties.peer_address
80 << " is invalid";
81 return;
82 }
83
Paul Stewart5b7ba8c2012-04-18 09:08:00 -070084 IPAddress gateway_address(properties.address_family);
85 if (!properties.gateway.empty() &&
86 !gateway_address.SetAddressFromString(properties.gateway)) {
87 LOG(ERROR) << "Gateway address " << properties.peer_address
88 << " is invalid";
89 return;
90 }
91
92 FixGatewayReachability(&local, gateway_address);
93
Paul Stewart48100b02012-03-19 07:53:52 -070094 rtnl_handler_->AddInterfaceAddress(interface_index_, local, broadcast, peer);
Paul Stewartdd60e452011-08-08 11:38:36 -070095
Paul Stewart5b7ba8c2012-04-18 09:08:00 -070096 if (gateway_address.IsValid()) {
97 routing_table_->SetDefaultRoute(interface_index_, gateway_address,
98 GetMetric(is_default_));
99 } else if (!peer.IsValid()) {
100 LOG(WARNING) << "No gateway or peer address was provided for this "
101 << "connection. Expect limited network connectivity.";
102 }
Paul Stewartdd60e452011-08-08 11:38:36 -0700103
Paul Stewart3f68bb12012-03-15 13:33:10 -0700104 // Install any explicitly configured routes at the default metric.
105 routing_table_->ConfigureRoutes(interface_index_, config, kDefaultMetric);
106
Paul Stewartdd60e452011-08-08 11:38:36 -0700107 // Save a copy of the last non-null DNS config
108 if (!config->properties().dns_servers.empty()) {
109 dns_servers_ = config->properties().dns_servers;
110 dns_domain_search_ = config->properties().domain_search;
111 }
112
Paul Stewart10241e32012-04-23 18:15:06 -0700113 ipconfig_rpc_identifier_ = config->GetRpcIdentifier();
114
Paul Stewartdd60e452011-08-08 11:38:36 -0700115 if (is_default_) {
116 resolver_->SetDNSFromIPConfig(config);
117 }
118}
119
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800120void Connection::SetIsDefault(bool is_default) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700121 SLOG(Connection, 2) << __func__ << " " << interface_name_
122 << " (index " << interface_index_ << ") "
123 << is_default_ << " -> " << is_default;
Paul Stewartdd60e452011-08-08 11:38:36 -0700124 if (is_default == is_default_) {
125 return;
126 }
127
Paul Stewart7cfca042011-12-08 14:18:17 -0800128 routing_table_->SetDefaultMetric(interface_index_, GetMetric(is_default));
Paul Stewartdd60e452011-08-08 11:38:36 -0700129
Paul Stewartc681fa02012-03-02 19:40:04 -0800130 is_default_ = is_default;
131
Paul Stewartdd60e452011-08-08 11:38:36 -0700132 if (is_default) {
133 resolver_->SetDNSFromLists(dns_servers_, dns_domain_search_);
Paul Stewartc681fa02012-03-02 19:40:04 -0800134 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
135 if (device) {
136 device->RequestPortalDetection();
137 }
Paul Stewartdd60e452011-08-08 11:38:36 -0700138 }
Paul Stewartdd60e452011-08-08 11:38:36 -0700139}
140
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800141void Connection::RequestRouting() {
142 if (routing_request_count_++ == 0) {
143 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
144 DCHECK(device.get());
145 if (!device.get()) {
146 LOG(ERROR) << "Device is NULL!";
147 return;
148 }
149 device->DisableReversePathFilter();
150 }
151}
152
153void Connection::ReleaseRouting() {
154 DCHECK(routing_request_count_ > 0);
155 if (--routing_request_count_ == 0) {
156 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
157 DCHECK(device.get());
158 if (!device.get()) {
159 LOG(ERROR) << "Device is NULL!";
160 return;
161 }
162 device->EnableReversePathFilter();
163
164 // Clear any cached routes that might have accumulated while reverse-path
165 // filtering was disabled.
166 routing_table_->FlushCache();
167 }
168}
169
Paul Stewartf748a362012-03-07 12:01:20 -0800170bool Connection::RequestHostRoute(const IPAddress &address) {
171 // Set the prefix to be the entire address size.
172 IPAddress address_prefix(address);
173 address_prefix.set_prefix(address_prefix.GetLength() * 8);
174
Paul Stewart536820d2012-03-19 16:05:59 -0700175 // Do not set interface_index_ since this may not be the
176 // default route through which this destination can be found.
Paul Stewarte93b0382012-04-24 13:11:28 -0700177 // However, we should tag the created route with our interface
178 // index so we can clean this route up when this connection closes.
179 if (!routing_table_->RequestRouteToHost(address_prefix, -1,
180 interface_index_)) {
Paul Stewartf748a362012-03-07 12:01:20 -0800181 LOG(ERROR) << "Could not request route to " << address.ToString();
182 return false;
183 }
184
185 return true;
186}
187
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700188// static
189void Connection::FixGatewayReachability(IPAddress *local,
190 const IPAddress &gateway) {
191 if (!gateway.IsValid() || local->CanReachAddress(gateway)) {
192 return;
193 }
194
195 LOG(WARNING) << "Gateway "
196 << gateway.ToString()
197 << " is unreachable from local address/prefix "
198 << local->ToString() << "/" << local->prefix();
199
200 size_t original_prefix = local->prefix();
201 size_t prefix = original_prefix - 1;
202 for (; prefix >= local->GetMinPrefixLength(); --prefix) {
203 local->set_prefix(prefix);
204 if (local->CanReachAddress(gateway)) {
205 break;
206 }
207 }
208
209 if (prefix < local->GetMinPrefixLength()) {
210 // Restore the original prefix since we cannot find a better one.
211 local->set_prefix(original_prefix);
212 LOG(WARNING) << "Expect limited network connectivity.";
213 } else {
214 LOG(WARNING) << "Mitigating this by setting local prefix to " << prefix;
215 }
216}
217
Paul Stewart7cfca042011-12-08 14:18:17 -0800218uint32 Connection::GetMetric(bool is_default) {
219 // If this is not the default route, assign a metric based on the interface
220 // index. This way all non-default routes (even to the same gateway IP) end
221 // up with unique metrics so they do not collide.
222 return is_default ? kDefaultMetric : kNonDefaultMetricBase + interface_index_;
223}
224
Paul Stewarte93b0382012-04-24 13:11:28 -0700225bool Connection::PinHostRoute(const IPConfig::Properties &properties) {
226 SLOG(Connection, 2) << __func__;
227 if (properties.gateway.empty() || properties.trusted_ip.empty()) {
228 return false;
229 }
230
231 IPAddress trusted_ip(properties.address_family);
232 if (!trusted_ip.SetAddressFromString(properties.trusted_ip)) {
233 LOG(ERROR) << "Failed to parse trusted_ip "
234 << properties.trusted_ip << "; ignored.";
235 return false;
236 }
237
238 return RequestHostRoute(trusted_ip);
239}
240
Paul Stewartdd60e452011-08-08 11:38:36 -0700241} // namespace shill