blob: 71cec11d0a88dec82187ac1485dfdc7d6e3206f6 [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
113 if (is_default_) {
114 resolver_->SetDNSFromIPConfig(config);
115 }
116}
117
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800118void Connection::SetIsDefault(bool is_default) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700119 SLOG(Connection, 2) << __func__ << " " << interface_name_
120 << " (index " << interface_index_ << ") "
121 << is_default_ << " -> " << is_default;
Paul Stewartdd60e452011-08-08 11:38:36 -0700122 if (is_default == is_default_) {
123 return;
124 }
125
Paul Stewart7cfca042011-12-08 14:18:17 -0800126 routing_table_->SetDefaultMetric(interface_index_, GetMetric(is_default));
Paul Stewartdd60e452011-08-08 11:38:36 -0700127
Paul Stewartc681fa02012-03-02 19:40:04 -0800128 is_default_ = is_default;
129
Paul Stewartdd60e452011-08-08 11:38:36 -0700130 if (is_default) {
131 resolver_->SetDNSFromLists(dns_servers_, dns_domain_search_);
Paul Stewartc681fa02012-03-02 19:40:04 -0800132 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
133 if (device) {
134 device->RequestPortalDetection();
135 }
Paul Stewartdd60e452011-08-08 11:38:36 -0700136 }
Paul Stewartdd60e452011-08-08 11:38:36 -0700137}
138
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800139void Connection::RequestRouting() {
140 if (routing_request_count_++ == 0) {
141 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
142 DCHECK(device.get());
143 if (!device.get()) {
144 LOG(ERROR) << "Device is NULL!";
145 return;
146 }
147 device->DisableReversePathFilter();
148 }
149}
150
151void Connection::ReleaseRouting() {
152 DCHECK(routing_request_count_ > 0);
153 if (--routing_request_count_ == 0) {
154 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
155 DCHECK(device.get());
156 if (!device.get()) {
157 LOG(ERROR) << "Device is NULL!";
158 return;
159 }
160 device->EnableReversePathFilter();
161
162 // Clear any cached routes that might have accumulated while reverse-path
163 // filtering was disabled.
164 routing_table_->FlushCache();
165 }
166}
167
Paul Stewartf748a362012-03-07 12:01:20 -0800168bool Connection::RequestHostRoute(const IPAddress &address) {
169 // Set the prefix to be the entire address size.
170 IPAddress address_prefix(address);
171 address_prefix.set_prefix(address_prefix.GetLength() * 8);
172
Paul Stewart536820d2012-03-19 16:05:59 -0700173 // Do not set interface_index_ since this may not be the
174 // default route through which this destination can be found.
Paul Stewarte93b0382012-04-24 13:11:28 -0700175 // However, we should tag the created route with our interface
176 // index so we can clean this route up when this connection closes.
177 if (!routing_table_->RequestRouteToHost(address_prefix, -1,
178 interface_index_)) {
Paul Stewartf748a362012-03-07 12:01:20 -0800179 LOG(ERROR) << "Could not request route to " << address.ToString();
180 return false;
181 }
182
183 return true;
184}
185
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700186// static
187void Connection::FixGatewayReachability(IPAddress *local,
188 const IPAddress &gateway) {
189 if (!gateway.IsValid() || local->CanReachAddress(gateway)) {
190 return;
191 }
192
193 LOG(WARNING) << "Gateway "
194 << gateway.ToString()
195 << " is unreachable from local address/prefix "
196 << local->ToString() << "/" << local->prefix();
197
198 size_t original_prefix = local->prefix();
199 size_t prefix = original_prefix - 1;
200 for (; prefix >= local->GetMinPrefixLength(); --prefix) {
201 local->set_prefix(prefix);
202 if (local->CanReachAddress(gateway)) {
203 break;
204 }
205 }
206
207 if (prefix < local->GetMinPrefixLength()) {
208 // Restore the original prefix since we cannot find a better one.
209 local->set_prefix(original_prefix);
210 LOG(WARNING) << "Expect limited network connectivity.";
211 } else {
212 LOG(WARNING) << "Mitigating this by setting local prefix to " << prefix;
213 }
214}
215
Paul Stewart7cfca042011-12-08 14:18:17 -0800216uint32 Connection::GetMetric(bool is_default) {
217 // If this is not the default route, assign a metric based on the interface
218 // index. This way all non-default routes (even to the same gateway IP) end
219 // up with unique metrics so they do not collide.
220 return is_default ? kDefaultMetric : kNonDefaultMetricBase + interface_index_;
221}
222
Paul Stewarte93b0382012-04-24 13:11:28 -0700223bool Connection::PinHostRoute(const IPConfig::Properties &properties) {
224 SLOG(Connection, 2) << __func__;
225 if (properties.gateway.empty() || properties.trusted_ip.empty()) {
226 return false;
227 }
228
229 IPAddress trusted_ip(properties.address_family);
230 if (!trusted_ip.SetAddressFromString(properties.trusted_ip)) {
231 LOG(ERROR) << "Failed to parse trusted_ip "
232 << properties.trusted_ip << "; ignored.";
233 return false;
234 }
235
236 return RequestHostRoute(trusted_ip);
237}
238
Paul Stewartdd60e452011-08-08 11:38:36 -0700239} // namespace shill