Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 1 | // Copyright 2018 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 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 5 | #include "patchpanel/subnet.h" |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 6 | |
| 7 | #include <arpa/inet.h> |
| 8 | |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 9 | #include <string> |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 10 | #include <utility> |
| 11 | |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 12 | #include <base/bind.h> |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 13 | #include <base/logging.h> |
| 14 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 15 | #include "patchpanel/net_util.h" |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 16 | |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 17 | namespace { |
| 18 | // Returns the offset from the base address given in network-byte order for |
| 19 | // the address given in network-byte order, or 0 if the second address is |
| 20 | // lower than the base address. Returns the offset in host-byte order. |
| 21 | uint32_t OffsetFromBaseAddress(uint32_t base_no, uint32_t addr_no) { |
| 22 | if (ntohl(addr_no) < ntohl(base_no)) |
| 23 | return 0; |
| 24 | return ntohl(addr_no) - ntohl(base_no); |
| 25 | } |
| 26 | // Adds a positive offset given in host order to the address given in |
| 27 | // network byte order. Returns the address in network-byte order. |
| 28 | uint32_t AddOffset(uint32_t addr_no, uint32_t offset_ho) { |
| 29 | return htonl(ntohl(addr_no) + offset_ho); |
| 30 | } |
| 31 | } // namespace |
| 32 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 33 | namespace patchpanel { |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 34 | |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 35 | SubnetAddress::SubnetAddress(uint32_t addr, |
| 36 | uint32_t prefix_length, |
| 37 | base::Closure release_cb) |
| 38 | : addr_(addr), |
| 39 | prefix_length_(prefix_length), |
| 40 | release_cb_(std::move(release_cb)) {} |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 41 | |
| 42 | SubnetAddress::~SubnetAddress() { |
| 43 | release_cb_.Run(); |
| 44 | } |
| 45 | |
| 46 | uint32_t SubnetAddress::Address() const { |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 47 | return addr_; |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 50 | std::string SubnetAddress::ToCidrString() const { |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 51 | return IPv4AddressToCidrString(addr_, prefix_length_); |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | std::string SubnetAddress::ToIPv4String() const { |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 55 | return IPv4AddressToString(addr_); |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 56 | } |
| 57 | |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 58 | uint32_t SubnetAddress::Netmask() const { |
Garrick Evans | 6f4fa3a | 2020-02-10 16:15:09 +0900 | [diff] [blame] | 59 | return Ipv4Netmask(prefix_length_); |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 60 | } |
| 61 | |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 62 | Subnet::Subnet(uint32_t base_addr, |
| 63 | uint32_t prefix_length, |
| 64 | base::Closure release_cb) |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 65 | : base_addr_(base_addr), |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 66 | prefix_length_(prefix_length), |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 67 | release_cb_(std::move(release_cb)), |
| 68 | weak_factory_(this) { |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 69 | CHECK_LT(prefix_length, 32); |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 70 | |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 71 | addrs_.resize(1ull << (32 - prefix_length), false); |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 72 | |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 73 | // Mark the base address and broadcast address as allocated. |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 74 | addrs_.front() = true; |
| 75 | addrs_.back() = true; |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | Subnet::~Subnet() { |
| 79 | release_cb_.Run(); |
| 80 | } |
| 81 | |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 82 | std::unique_ptr<SubnetAddress> Subnet::Allocate(uint32_t addr) { |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 83 | return AllocateAtOffset(OffsetFromBaseAddress(base_addr_, addr) - 1); |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 86 | std::unique_ptr<SubnetAddress> Subnet::AllocateAtOffset(uint32_t offset) { |
| 87 | uint32_t addr = AddressAtOffset(offset); |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 88 | if (addr == INADDR_ANY) { |
| 89 | return nullptr; |
| 90 | } |
| 91 | |
| 92 | if (addrs_[offset + 1]) { |
| 93 | // Address is already allocated. |
| 94 | return nullptr; |
| 95 | } |
| 96 | |
| 97 | addrs_[offset + 1] = true; |
| 98 | return std::make_unique<SubnetAddress>( |
| 99 | addr, prefix_length_, |
| 100 | base::Bind(&Subnet::Free, weak_factory_.GetWeakPtr(), offset + 1)); |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 101 | } |
| 102 | |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 103 | uint32_t Subnet::AddressAtOffset(uint32_t offset) const { |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 104 | if (offset < 0 || offset >= AvailableCount()) |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 105 | return INADDR_ANY; |
| 106 | |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 107 | // The first usable IP is after the base address. |
| 108 | return AddOffset(base_addr_, 1 + offset); |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 111 | uint32_t Subnet::AvailableCount() const { |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 112 | // The available IP count is all IPs in a subnet, minus the network ID |
| 113 | // and the broadcast address. |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 114 | return addrs_.size() - 2; |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 117 | uint32_t Subnet::BaseAddress() const { |
| 118 | return base_addr_; |
| 119 | } |
| 120 | |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 121 | uint32_t Subnet::Netmask() const { |
Garrick Evans | 6f4fa3a | 2020-02-10 16:15:09 +0900 | [diff] [blame] | 122 | return Ipv4Netmask(prefix_length_); |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 125 | uint32_t Subnet::Prefix() const { |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 126 | return base_addr_ & Netmask(); |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | uint32_t Subnet::PrefixLength() const { |
| 130 | return prefix_length_; |
| 131 | } |
| 132 | |
| 133 | std::string Subnet::ToCidrString() const { |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 134 | return IPv4AddressToCidrString(base_addr_, prefix_length_); |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 137 | void Subnet::Free(uint32_t offset) { |
| 138 | DCHECK_NE(offset, 0); |
| 139 | DCHECK_LT(offset, addrs_.size() - 1); |
| 140 | |
| 141 | addrs_[offset] = false; |
| 142 | } |
| 143 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 144 | } // namespace patchpanel |