Line data Source code
1 : /* 2 : * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #include "base/misc_utils.h" 7 : #include "base/string_util.h" 8 : #include "bgp/inet6/inet6_route.h" 9 : #include "bgp/inet6/inet6_table.h" 10 : 11 : using boost::system::error_code; 12 : using std::copy; 13 : using std::string; 14 : using std::vector; 15 : 16 0 : int Inet6Prefix::FromProtoPrefix(const BgpProtoPrefix &proto_prefix, 17 : Inet6Prefix *prefix) { 18 0 : if (proto_prefix.prefix.size() > Address::kMaxV6Bytes) 19 0 : return -1; 20 0 : prefix->prefixlen_ = proto_prefix.prefixlen; 21 0 : Ip6Address::bytes_type bt = { { 0 } }; 22 0 : copy(proto_prefix.prefix.begin(), proto_prefix.prefix.end(), bt.begin()); 23 0 : prefix->ip6_addr_ = Ip6Address(bt); 24 : 25 0 : return 0; 26 : } 27 : 28 0 : int Inet6Prefix::FromProtoPrefix(BgpServer *server, 29 : const BgpProtoPrefix &proto_prefix, 30 : const BgpAttr *attr, 31 : const Address::Family family, 32 : Inet6Prefix *prefix, BgpAttrPtr *new_attr, 33 : uint32_t *label, uint32_t *l3_label) { 34 0 : return FromProtoPrefix(proto_prefix, prefix); 35 : } 36 : 37 49953 : string Inet6Prefix::ToString() const { 38 49953 : string repr(ip6_addr().to_string()); 39 49960 : repr.append("/" + integerToString(prefixlen())); 40 49983 : return repr; 41 0 : } 42 : 43 382491 : int Inet6Prefix::CompareTo(const Inet6Prefix &rhs) const { 44 382491 : if (ip6_addr_ < rhs.ip6_addr_) { 45 195494 : return -1; 46 : } 47 186920 : if (ip6_addr_ > rhs.ip6_addr_) { 48 130804 : return 1; 49 : } 50 56526 : if (prefixlen_ < rhs.prefixlen_) { 51 0 : return -1; 52 : } 53 56526 : if (prefixlen_ > rhs.prefixlen_) { 54 0 : return 1; 55 : } 56 56526 : return 0; 57 : } 58 : 59 576 : Inet6Prefix Inet6Prefix::FromString(const string &str, error_code *error) { 60 576 : Inet6Prefix prefix; 61 576 : error_code pfxerr = Inet6SubnetParse(str, &prefix.ip6_addr_, 62 : &prefix.prefixlen_); 63 576 : if (error != NULL) { 64 320 : *error = pfxerr; 65 : } 66 1152 : return prefix; 67 : } 68 : 69 : // Check whether 'this' is more specific than rhs. 70 0 : bool Inet6Prefix::IsMoreSpecific(const Inet6Prefix &rhs) const { 71 : // My prefixlen must be longer in order to be more specific. 72 0 : if (prefixlen_ < rhs.prefixlen()) { 73 0 : return false; 74 : } 75 0 : Inet6Prefix mask = Inet6Masks::PrefixlenToMask(rhs.prefixlen()); 76 0 : Inet6Prefix left = operator&(mask); 77 0 : Inet6Prefix right = rhs.operator&(mask); 78 : 79 0 : return (left.ToBytes() == right.ToBytes()); 80 : } 81 : 82 0 : Inet6Prefix Inet6Prefix::operator&(const Inet6Prefix& right) const { 83 : Ip6Address::bytes_type addr_bytes; 84 0 : ARRAYBYTES_FILL(addr_bytes,0); 85 : 86 0 : Ip6Address::bytes_type lhs = ToBytes(); 87 0 : Ip6Address::bytes_type rhs = right.ToBytes(); 88 : 89 0 : int plen = (prefixlen_ <= right.prefixlen_ ? prefixlen() : right.prefixlen()); 90 : 91 0 : for (size_t i = 0; i < sizeof(Ip6Address::bytes_type); ++i) { 92 0 : addr_bytes[i] = lhs[i] & rhs[i]; 93 : } 94 : 95 0 : return Inet6Prefix(Ip6Address(addr_bytes), plen); 96 : } 97 : 98 : // Routines for class Inet6Route 99 : 100 49638 : Inet6Route::Inet6Route(const Inet6Prefix &prefix) 101 49639 : : prefix_(prefix), 102 49638 : prefix_str_(prefix.ToString()) { 103 49655 : } 104 : 105 382476 : int Inet6Route::CompareTo(const Route &rhs) const { 106 382476 : const Inet6Route &rt_other = static_cast<const Inet6Route &>(rhs); 107 382476 : return prefix_.CompareTo(rt_other.prefix_); 108 : } 109 : 110 : // Check whether 'this' is more specific than rhs. 111 0 : bool Inet6Route::IsMoreSpecific(const string &match) const { 112 0 : error_code ec; 113 : 114 0 : Inet6Prefix prefix = Inet6Prefix::FromString(match, &ec); 115 0 : if (!ec) { 116 0 : return GetPrefix().IsMoreSpecific(prefix); 117 : } 118 : 119 0 : return false; 120 : } 121 : 122 : // Check whether 'this' is less specific than rhs. 123 0 : bool Inet6Route::IsLessSpecific(const string &match) const { 124 0 : error_code ec; 125 : 126 0 : Inet6Prefix prefix = Inet6Prefix::FromString(match, &ec); 127 0 : if (!ec) { 128 0 : return prefix.IsMoreSpecific(GetPrefix()); 129 : } 130 : 131 0 : return false; 132 : } 133 : 134 16405 : DBEntryBase::KeyPtr Inet6Route::GetDBRequestKey() const { 135 16405 : Inet6Table::RequestKey *key = new Inet6Table::RequestKey(prefix_, NULL); 136 16396 : return KeyPtr(key); 137 : } 138 : 139 0 : void Inet6Route::SetKey(const DBRequestKey *reqkey) { 140 0 : const Inet6Table::RequestKey *key = 141 : static_cast<const Inet6Table::RequestKey *>(reqkey); 142 0 : prefix_ = key->prefix; 143 0 : } 144 : 145 0 : void Inet6Route::BuildProtoPrefix(BgpProtoPrefix *prefix, 146 : const BgpAttr *attr, 147 : uint32_t label, 148 : uint32_t l3_label) const { 149 0 : prefix->prefixlen = prefix_.prefixlen(); 150 0 : prefix->prefix.clear(); 151 0 : const Ip6Address::bytes_type &addr_bytes = prefix_.ip6_addr().to_bytes(); 152 0 : int num_bytes = (prefix->prefixlen + 7) / 8; 153 0 : copy(addr_bytes.begin(), addr_bytes.begin() + num_bytes, 154 0 : back_inserter(prefix->prefix)); 155 0 : } 156 : 157 : // 158 : // Fill in the vector based on the supplied nexthop. 159 : // 160 0 : void Inet6Route::BuildBgpProtoNextHop(vector<uint8_t> &nh, 161 : IpAddress nexthop) const { 162 0 : nh.resize(Address::kMaxV6Bytes); 163 0 : Ip6Address address; 164 0 : if (nexthop.is_v4()) { 165 0 : address = Ip6Address::v4_mapped(nexthop.to_v4()); 166 : } else { 167 0 : address = nexthop.to_v6(); 168 : } 169 : 170 0 : const Ip6Address::bytes_type &addr_bytes = address.to_bytes(); 171 0 : copy(addr_bytes.begin(), addr_bytes.end(), nh.begin()); 172 0 : } 173 : 174 : // Routines for class Inet6Masks 175 : 176 : // Definitions of the static members 177 : bool Inet6Masks::initialized_ = false; 178 : vector<Inet6Prefix> Inet6Masks::masks_; 179 : 180 0 : const Inet6Prefix& Inet6Masks::PrefixlenToMask(uint8_t prefix_len) { 181 0 : assert(prefix_len <= Inet6Prefix::kMaxV6PrefixLen); 182 0 : return masks_.at(prefix_len); 183 : } 184 : 185 1 : void Inet6Masks::Init() { 186 1 : assert(initialized_ == false); 187 130 : for (int i = 0; i <= Inet6Prefix::kMaxV6PrefixLen; ++i) { 188 129 : masks_.push_back(CalculateMaskFromPrefixlen(i)); 189 : } 190 1 : initialized_ = true; 191 1 : } 192 : 193 0 : void Inet6Masks::Clear() { 194 0 : masks_.clear(); 195 0 : initialized_ = false; 196 0 : } 197 : 198 129 : Inet6Prefix Inet6Masks::CalculateMaskFromPrefixlen(int prefixlen) { 199 129 : int num_bytes = prefixlen / 8; 200 129 : int num_bits = prefixlen % 8; 201 : 202 : Ip6Address::bytes_type addr_bytes; 203 129 : ARRAYBYTES_FILL(addr_bytes,0); 204 : 205 1105 : for (int i = 0; i < num_bytes; ++i) { 206 976 : addr_bytes[i] = 0xff; 207 : } 208 129 : if (num_bits) { 209 112 : uint8_t hex_val = 0xff << (8 - num_bits); 210 112 : addr_bytes[num_bytes] = hex_val; 211 : } 212 129 : return Inet6Prefix(Ip6Address(addr_bytes), prefixlen); 213 : } 214 : 215 1 : static void Inet6InitRoutines() { 216 1 : Inet6Masks::Init(); 217 1 : } 218 : MODULE_INITIALIZER(Inet6InitRoutines);