blob: 50ac978c2962ec54c52e0905656b20ff3ff3ec09 [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"
14
15using std::string;
16
17namespace shill {
18
19// static
20const uint32 Connection::kDefaultMetric = 1;
21// static
Paul Stewart7cfca042011-12-08 14:18:17 -080022const uint32 Connection::kNonDefaultMetricBase = 10;
Paul Stewartdd60e452011-08-08 11:38:36 -070023
Paul Stewart9a908082011-08-31 12:18:48 -070024Connection::Connection(int interface_index,
25 const std::string& interface_name,
Paul Stewarte00600e2012-03-16 07:08:00 -070026 Technology::Identifier technology,
Paul Stewart9a908082011-08-31 12:18:48 -070027 const DeviceInfo *device_info)
Paul Stewartdd60e452011-08-08 11:38:36 -070028 : is_default_(false),
Paul Stewartc8f4bef2011-12-13 09:45:51 -080029 routing_request_count_(0),
Paul Stewartdd60e452011-08-08 11:38:36 -070030 interface_index_(interface_index),
31 interface_name_(interface_name),
Paul Stewarte00600e2012-03-16 07:08:00 -070032 technology_(technology),
Paul Stewart9a908082011-08-31 12:18:48 -070033 device_info_(device_info),
Paul Stewartdd60e452011-08-08 11:38:36 -070034 resolver_(Resolver::GetInstance()),
35 routing_table_(RoutingTable::GetInstance()),
36 rtnl_handler_(RTNLHandler::GetInstance()) {
Darin Petkov273028a2012-03-19 10:20:46 +010037 VLOG(2) << __func__ << "(" << interface_index
38 << ", " << interface_name
39 << ", " << Technology::NameFromIdentifier(technology) << ")";
Paul Stewartdd60e452011-08-08 11:38:36 -070040}
41
42Connection::~Connection() {
mukesh agrawal2c15d2c2012-02-21 16:09:21 -080043 VLOG(2) << __func__ << " " << interface_name_;
Paul Stewart9a908082011-08-31 12:18:48 -070044
Paul Stewartc8f4bef2011-12-13 09:45:51 -080045 DCHECK(!routing_request_count_);
Thieu Lefb46caf2012-03-08 11:57:15 -080046 routing_table_->FlushRoutes(interface_index_);
Paul Stewart9a908082011-08-31 12:18:48 -070047 device_info_->FlushAddresses(interface_index_);
Paul Stewartdd60e452011-08-08 11:38:36 -070048}
49
50void Connection::UpdateFromIPConfig(const IPConfigRefPtr &config) {
mukesh agrawal2c15d2c2012-02-21 16:09:21 -080051 VLOG(2) << __func__ << " " << interface_name_;
Paul Stewarte6132022011-08-16 09:11:02 -070052
Paul Stewart9a908082011-08-31 12:18:48 -070053 const IPConfig::Properties &properties = config->properties();
54 IPAddress local(properties.address_family);
55 if (!local.SetAddressFromString(properties.address)) {
56 LOG(ERROR) << "Local address " << properties.address << " is invalid";
57 return;
58 }
Paul Stewart48100b02012-03-19 07:53:52 -070059 local.set_prefix(properties.subnet_prefix);
Paul Stewart9a908082011-08-31 12:18:48 -070060
61 IPAddress broadcast(properties.address_family);
Paul Stewarte00600e2012-03-16 07:08:00 -070062 if (!broadcast.SetAddressFromString(properties.broadcast_address) &&
Darin Petkov273028a2012-03-19 10:20:46 +010063 technology_ != Technology::kVPN) {
Paul Stewart9a908082011-08-31 12:18:48 -070064 LOG(ERROR) << "Broadcast address " << properties.broadcast_address
65 << " is invalid";
66 return;
67 }
68
Paul Stewart48100b02012-03-19 07:53:52 -070069 IPAddress peer(properties.address_family);
70 if (!properties.peer_address.empty() &&
71 !peer.SetAddressFromString(properties.peer_address)) {
72 LOG(ERROR) << "Peer address " << properties.peer_address
73 << " is invalid";
74 return;
75 }
76
77 rtnl_handler_->AddInterfaceAddress(interface_index_, local, broadcast, peer);
Paul Stewartdd60e452011-08-08 11:38:36 -070078
Paul Stewart7cfca042011-12-08 14:18:17 -080079 routing_table_->SetDefaultRoute(interface_index_, config,
80 GetMetric(is_default_));
Paul Stewartdd60e452011-08-08 11:38:36 -070081
Paul Stewart3f68bb12012-03-15 13:33:10 -070082 // Install any explicitly configured routes at the default metric.
83 routing_table_->ConfigureRoutes(interface_index_, config, kDefaultMetric);
84
Paul Stewartdd60e452011-08-08 11:38:36 -070085 // Save a copy of the last non-null DNS config
86 if (!config->properties().dns_servers.empty()) {
87 dns_servers_ = config->properties().dns_servers;
88 dns_domain_search_ = config->properties().domain_search;
89 }
90
91 if (is_default_) {
92 resolver_->SetDNSFromIPConfig(config);
93 }
94}
95
Paul Stewartc1dec4d2011-12-08 15:25:28 -080096void Connection::SetIsDefault(bool is_default) {
mukesh agrawal2c15d2c2012-02-21 16:09:21 -080097 VLOG(2) << __func__ << " "
98 << interface_name_ << " (index " << interface_index_ << ") "
99 << is_default_ << " -> " << is_default;
Paul Stewartdd60e452011-08-08 11:38:36 -0700100 if (is_default == is_default_) {
101 return;
102 }
103
Paul Stewart7cfca042011-12-08 14:18:17 -0800104 routing_table_->SetDefaultMetric(interface_index_, GetMetric(is_default));
Paul Stewartdd60e452011-08-08 11:38:36 -0700105
Paul Stewartc681fa02012-03-02 19:40:04 -0800106 is_default_ = is_default;
107
Paul Stewartdd60e452011-08-08 11:38:36 -0700108 if (is_default) {
109 resolver_->SetDNSFromLists(dns_servers_, dns_domain_search_);
Paul Stewartc681fa02012-03-02 19:40:04 -0800110 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
111 if (device) {
112 device->RequestPortalDetection();
113 }
Paul Stewartdd60e452011-08-08 11:38:36 -0700114 }
Paul Stewartdd60e452011-08-08 11:38:36 -0700115}
116
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800117void Connection::RequestRouting() {
118 if (routing_request_count_++ == 0) {
119 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
120 DCHECK(device.get());
121 if (!device.get()) {
122 LOG(ERROR) << "Device is NULL!";
123 return;
124 }
125 device->DisableReversePathFilter();
126 }
127}
128
129void Connection::ReleaseRouting() {
130 DCHECK(routing_request_count_ > 0);
131 if (--routing_request_count_ == 0) {
132 DeviceRefPtr device = device_info_->GetDevice(interface_index_);
133 DCHECK(device.get());
134 if (!device.get()) {
135 LOG(ERROR) << "Device is NULL!";
136 return;
137 }
138 device->EnableReversePathFilter();
139
140 // Clear any cached routes that might have accumulated while reverse-path
141 // filtering was disabled.
142 routing_table_->FlushCache();
143 }
144}
145
Paul Stewartf748a362012-03-07 12:01:20 -0800146bool Connection::RequestHostRoute(const IPAddress &address) {
147 // Set the prefix to be the entire address size.
148 IPAddress address_prefix(address);
149 address_prefix.set_prefix(address_prefix.GetLength() * 8);
150
Paul Stewart536820d2012-03-19 16:05:59 -0700151 // Do not set interface_index_ since this may not be the
152 // default route through which this destination can be found.
153 if (!routing_table_->RequestRouteToHost(address_prefix, -1)) {
Paul Stewartf748a362012-03-07 12:01:20 -0800154 LOG(ERROR) << "Could not request route to " << address.ToString();
155 return false;
156 }
157
158 return true;
159}
160
Paul Stewart7cfca042011-12-08 14:18:17 -0800161uint32 Connection::GetMetric(bool is_default) {
162 // If this is not the default route, assign a metric based on the interface
163 // index. This way all non-default routes (even to the same gateway IP) end
164 // up with unique metrics so they do not collide.
165 return is_default ? kDefaultMetric : kNonDefaultMetricBase + interface_index_;
166}
167
Paul Stewartdd60e452011-08-08 11:38:36 -0700168} // namespace shill