Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #include "bgp/inet/inet_route.h" 7 : 8 : #include <algorithm> 9 : 10 : #include "bgp/inet/inet_table.h" 11 : 12 : using std::copy; 13 : using std::string; 14 : using std::vector; 15 : 16 0 : int Ip4Prefix::FromProtoPrefix(const BgpProtoPrefix &proto_prefix, 17 : Ip4Prefix *prefix) { 18 0 : if (proto_prefix.prefix.size() > Address::kMaxV4Bytes) 19 0 : return -1; 20 0 : prefix->prefixlen_ = proto_prefix.prefixlen; 21 0 : Ip4Address::bytes_type bt = { { 0 } }; 22 0 : copy(proto_prefix.prefix.begin(), proto_prefix.prefix.end(), bt.begin()); 23 0 : prefix->ip4_addr_ = Ip4Address(bt); 24 : 25 0 : return 0; 26 : } 27 : 28 0 : int Ip4Prefix::FromProtoPrefix(BgpServer *server, 29 : const BgpProtoPrefix &proto_prefix, 30 : const BgpAttr *attr, 31 : const Address::Family family, 32 : Ip4Prefix *prefix, 33 : BgpAttrPtr *new_attr, uint32_t *label, 34 : uint32_t *l3_label) { 35 0 : if (family == Address::INET) { 36 0 : return FromProtoPrefix(proto_prefix, prefix); 37 : } 38 0 : size_t nlri_size = proto_prefix.prefix.size(); 39 0 : size_t expected_min_nlri_size = 40 : BgpProtoPrefix::kLabelSize; 41 : 42 0 : if (nlri_size < expected_min_nlri_size) 43 0 : return -1; 44 0 : if (nlri_size > expected_min_nlri_size + Address::kMaxV4Bytes) 45 0 : return -1; 46 : 47 0 : size_t label_offset = 0; 48 0 : *label = proto_prefix.ReadLabel(label_offset); 49 0 : size_t prefix_offset = label_offset + BgpProtoPrefix::kLabelSize; 50 0 : prefix->prefixlen_ = proto_prefix.prefixlen - prefix_offset * 8; 51 0 : Ip4Address::bytes_type bt = { { 0 } }; 52 0 : copy(proto_prefix.prefix.begin() + prefix_offset, 53 : proto_prefix.prefix.end(), bt.begin()); 54 0 : prefix->ip4_addr_ = Ip4Address(bt); 55 : 56 0 : return 0; 57 : } 58 : 59 : 60 : 61 51865 : string Ip4Prefix::ToString() const { 62 51865 : string repr(ip4_addr().to_string()); 63 : char strplen[4]; 64 51865 : snprintf(strplen, sizeof(strplen), "/%d", prefixlen()); 65 51861 : repr.append(strplen); 66 103798 : return repr; 67 0 : } 68 : 69 352211 : int Ip4Prefix::CompareTo(const Ip4Prefix &rhs) const { 70 352211 : if (ip4_addr_ < rhs.ip4_addr_) { 71 178895 : return -1; 72 : } 73 173014 : if (ip4_addr_ > rhs.ip4_addr_) { 74 114373 : return 1; 75 : } 76 59160 : if (prefixlen_ < rhs.prefixlen_) { 77 0 : return -1; 78 : } 79 59160 : if (prefixlen_ > rhs.prefixlen_) { 80 0 : return 1; 81 : } 82 59160 : return 0; 83 : } 84 : 85 388 : Ip4Prefix Ip4Prefix::FromString(const string &str, 86 : boost::system::error_code *errorp) { 87 388 : Ip4Prefix prefix; 88 388 : boost::system::error_code pfxerr = Ip4SubnetParse(str, &prefix.ip4_addr_, 89 : &prefix.prefixlen_); 90 388 : if (errorp != NULL) { 91 320 : *errorp = pfxerr; 92 : } 93 776 : return prefix; 94 : } 95 : 96 : // Check whether 'this' is more specific than rhs. 97 0 : bool Ip4Prefix::IsMoreSpecific(const Ip4Prefix &rhs) const { 98 : // My prefixlen must be longer in order to be more specific. 99 0 : if (prefixlen_ < rhs.prefixlen()) return false; 100 : 101 0 : uint32_t mask = 0; 102 0 : if (rhs.prefixlen()) 103 0 : mask = ((uint32_t) ~0) << (Address::kMaxV4PrefixLen - rhs.prefixlen()); 104 0 : return (ip4_addr_.to_ulong() & mask) == 105 0 : (rhs.ip4_addr().to_ulong() & mask); 106 : } 107 : 108 51307 : InetRoute::InetRoute(const Ip4Prefix &prefix) 109 51294 : : prefix_(prefix), 110 51307 : prefix_str_(prefix.ToString()) { 111 51323 : } 112 : 113 352069 : int InetRoute::CompareTo(const Route &rhs) const { 114 352069 : const InetRoute &rt_other = static_cast<const InetRoute &>(rhs); 115 352069 : return prefix_.CompareTo(rt_other.prefix_); 116 : } 117 : 118 : // Check whether 'this' is more specific than rhs. 119 0 : bool InetRoute::IsMoreSpecific(const string &match) const { 120 0 : boost::system::error_code ec; 121 : 122 0 : Ip4Prefix prefix = Ip4Prefix::FromString(match, &ec); 123 0 : if (!ec) { 124 0 : return GetPrefix().IsMoreSpecific(prefix); 125 : } 126 : 127 0 : return false; 128 : } 129 : 130 : // Check whether 'this' is less specific than rhs. 131 0 : bool InetRoute::IsLessSpecific(const string &match) const { 132 0 : boost::system::error_code ec; 133 : 134 0 : Ip4Prefix prefix = Ip4Prefix::FromString(match, &ec); 135 0 : if (!ec) { 136 0 : return prefix.IsMoreSpecific(GetPrefix()); 137 : } 138 : 139 0 : return false; 140 : } 141 : 142 16320 : DBEntryBase::KeyPtr InetRoute::GetDBRequestKey() const { 143 16320 : InetTable::RequestKey *key = new InetTable::RequestKey(prefix_, NULL); 144 16273 : return KeyPtr(key); 145 : } 146 : 147 0 : void InetRoute::SetKey(const DBRequestKey *reqkey) { 148 0 : const InetTable::RequestKey *key = 149 : static_cast<const InetTable::RequestKey *>(reqkey); 150 0 : prefix_ = key->prefix; 151 0 : } 152 : 153 0 : void InetRoute::BuildProtoPrefix(BgpProtoPrefix *prefix, 154 : const BgpAttr *attr, 155 : const uint32_t label, 156 : uint32_t l3_label) const { 157 : size_t prefix_size; 158 0 : prefix->prefix.clear(); 159 0 : if (label) { // Labeled Unicast 160 0 : prefix_size = (prefix_.prefixlen() + 7) / 8; 161 0 : size_t nlri_size = BgpProtoPrefix::kLabelSize + prefix_size; 162 0 : prefix->prefix.resize(nlri_size, 0); 163 0 : size_t label_offset = 0; 164 0 : prefix->WriteLabel(label_offset, label); 165 0 : size_t prefix_offset = label_offset + BgpProtoPrefix::kLabelSize; 166 0 : prefix->prefixlen = prefix_offset * 8 + prefix_.prefixlen(); 167 0 : const Ip4Address::bytes_type &addr_bytes = prefix_.ip4_addr().to_bytes(); 168 0 : copy(addr_bytes.begin(), addr_bytes.begin() + prefix_size, 169 0 : prefix->prefix.begin() + prefix_offset); 170 : } else { 171 0 : prefix->prefixlen = prefix_.prefixlen(); 172 0 : const Ip4Address::bytes_type &addr_bytes = prefix_.ip4_addr().to_bytes(); 173 0 : prefix_size = (prefix->prefixlen + 7) / 8; 174 0 : copy(addr_bytes.begin(), addr_bytes.begin() + prefix_size, 175 0 : back_inserter(prefix->prefix)); 176 : } 177 0 : } 178 : 179 0 : void InetRoute::BuildBgpProtoNextHop(vector<uint8_t> &nh, 180 : IpAddress nexthop) const { 181 0 : nh.resize(Address::kMaxV4Bytes); 182 0 : const Ip4Address::bytes_type &addr_bytes = nexthop.to_v4().to_bytes(); 183 0 : copy(addr_bytes.begin(), addr_bytes.end(), nh.begin()); 184 0 : }