Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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 "shill/connection.h" |
| 6 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 7 | #include <arpa/inet.h> |
| 8 | #include <linux/rtnetlink.h> |
| 9 | |
| 10 | #include "shill/device_info.h" |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 11 | #include "shill/resolver.h" |
| 12 | #include "shill/routing_table.h" |
| 13 | #include "shill/rtnl_handler.h" |
| 14 | |
| 15 | using std::string; |
| 16 | |
| 17 | namespace shill { |
| 18 | |
| 19 | // static |
| 20 | const uint32 Connection::kDefaultMetric = 1; |
| 21 | // static |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 22 | const uint32 Connection::kNonDefaultMetricBase = 10; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 23 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 24 | Connection::Connection(int interface_index, |
| 25 | const std::string& interface_name, |
| 26 | const DeviceInfo *device_info) |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 27 | : is_default_(false), |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 28 | routing_request_count_(0), |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 29 | interface_index_(interface_index), |
| 30 | interface_name_(interface_name), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 31 | device_info_(device_info), |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 32 | resolver_(Resolver::GetInstance()), |
| 33 | routing_table_(RoutingTable::GetInstance()), |
| 34 | rtnl_handler_(RTNLHandler::GetInstance()) { |
| 35 | VLOG(2) << __func__; |
| 36 | } |
| 37 | |
| 38 | Connection::~Connection() { |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 39 | VLOG(2) << __func__; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 40 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 41 | DCHECK(!routing_request_count_); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 42 | routing_table_->FlushRoutes(interface_index_); |
| 43 | device_info_->FlushAddresses(interface_index_); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void Connection::UpdateFromIPConfig(const IPConfigRefPtr &config) { |
| 47 | VLOG(2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 48 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 49 | const IPConfig::Properties &properties = config->properties(); |
| 50 | IPAddress local(properties.address_family); |
| 51 | if (!local.SetAddressFromString(properties.address)) { |
| 52 | LOG(ERROR) << "Local address " << properties.address << " is invalid"; |
| 53 | return; |
| 54 | } |
| 55 | local.set_prefix(properties.subnet_cidr); |
| 56 | |
| 57 | IPAddress broadcast(properties.address_family); |
| 58 | if (!broadcast.SetAddressFromString(properties.broadcast_address)) { |
| 59 | LOG(ERROR) << "Broadcast address " << properties.broadcast_address |
| 60 | << " is invalid"; |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | rtnl_handler_->AddInterfaceAddress(interface_index_, local, broadcast); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 65 | |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 66 | routing_table_->SetDefaultRoute(interface_index_, config, |
| 67 | GetMetric(is_default_)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 68 | |
| 69 | // Save a copy of the last non-null DNS config |
| 70 | if (!config->properties().dns_servers.empty()) { |
| 71 | dns_servers_ = config->properties().dns_servers; |
| 72 | dns_domain_search_ = config->properties().domain_search; |
| 73 | } |
| 74 | |
| 75 | if (is_default_) { |
| 76 | resolver_->SetDNSFromIPConfig(config); |
| 77 | } |
| 78 | } |
| 79 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 80 | void Connection::SetIsDefault(bool is_default) { |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 81 | VLOG(2) << __func__; |
| 82 | if (is_default == is_default_) { |
| 83 | return; |
| 84 | } |
| 85 | |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 86 | routing_table_->SetDefaultMetric(interface_index_, GetMetric(is_default)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 87 | |
| 88 | if (is_default) { |
| 89 | resolver_->SetDNSFromLists(dns_servers_, dns_domain_search_); |
| 90 | } |
| 91 | |
| 92 | is_default_ = is_default; |
| 93 | } |
| 94 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 95 | void Connection::RequestRouting() { |
| 96 | if (routing_request_count_++ == 0) { |
| 97 | DeviceRefPtr device = device_info_->GetDevice(interface_index_); |
| 98 | DCHECK(device.get()); |
| 99 | if (!device.get()) { |
| 100 | LOG(ERROR) << "Device is NULL!"; |
| 101 | return; |
| 102 | } |
| 103 | device->DisableReversePathFilter(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void Connection::ReleaseRouting() { |
| 108 | DCHECK(routing_request_count_ > 0); |
| 109 | if (--routing_request_count_ == 0) { |
| 110 | DeviceRefPtr device = device_info_->GetDevice(interface_index_); |
| 111 | DCHECK(device.get()); |
| 112 | if (!device.get()) { |
| 113 | LOG(ERROR) << "Device is NULL!"; |
| 114 | return; |
| 115 | } |
| 116 | device->EnableReversePathFilter(); |
| 117 | |
| 118 | // Clear any cached routes that might have accumulated while reverse-path |
| 119 | // filtering was disabled. |
| 120 | routing_table_->FlushCache(); |
| 121 | } |
| 122 | } |
| 123 | |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 124 | uint32 Connection::GetMetric(bool is_default) { |
| 125 | // If this is not the default route, assign a metric based on the interface |
| 126 | // index. This way all non-default routes (even to the same gateway IP) end |
| 127 | // up with unique metrics so they do not collide. |
| 128 | return is_default ? kDefaultMetric : kNonDefaultMetricBase + interface_index_; |
| 129 | } |
| 130 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 131 | } // namespace shill |