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 1920 : int Ip4Prefix::FromProtoPrefix(const BgpProtoPrefix &proto_prefix, 17 : Ip4Prefix *prefix) { 18 1920 : if (proto_prefix.prefix.size() > Address::kMaxV4Bytes) 19 0 : return -1; 20 1920 : prefix->prefixlen_ = proto_prefix.prefixlen; 21 1920 : Ip4Address::bytes_type bt = { { 0 } }; 22 1920 : copy(proto_prefix.prefix.begin(), proto_prefix.prefix.end(), bt.begin()); 23 1920 : prefix->ip4_addr_ = Ip4Address(bt); 24 : 25 1920 : return 0; 26 : } 27 : 28 1920 : 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 1920 : if (family == Address::INET) { 36 1920 : 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 10773 : string Ip4Prefix::ToString() const { 62 10773 : string repr(ip4_addr().to_string()); 63 : char strplen[4]; 64 10775 : snprintf(strplen, sizeof(strplen), "/%d", prefixlen()); 65 10775 : repr.append(strplen); 66 21552 : return repr; 67 0 : } 68 : 69 16279 : int Ip4Prefix::CompareTo(const Ip4Prefix &rhs) const { 70 16279 : if (ip4_addr_ < rhs.ip4_addr_) { 71 5438 : return -1; 72 : } 73 10840 : if (ip4_addr_ > rhs.ip4_addr_) { 74 2520 : return 1; 75 : } 76 8322 : if (prefixlen_ < rhs.prefixlen_) { 77 0 : return -1; 78 : } 79 8322 : if (prefixlen_ > rhs.prefixlen_) { 80 0 : return 1; 81 : } 82 8322 : return 0; 83 : } 84 : 85 2368 : Ip4Prefix Ip4Prefix::FromString(const string &str, 86 : boost::system::error_code *errorp) { 87 2368 : Ip4Prefix prefix; 88 2368 : boost::system::error_code pfxerr = Ip4SubnetParse(str, &prefix.ip4_addr_, 89 : &prefix.prefixlen_); 90 2368 : if (errorp != NULL) { 91 640 : *errorp = pfxerr; 92 : } 93 4736 : 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 6802 : InetRoute::InetRoute(const Ip4Prefix &prefix) 109 6801 : : prefix_(prefix), 110 6802 : prefix_str_(prefix.ToString()) { 111 6802 : } 112 : 113 16279 : int InetRoute::CompareTo(const Route &rhs) const { 114 16279 : const InetRoute &rt_other = static_cast<const InetRoute &>(rhs); 115 16279 : 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 1473 : DBEntryBase::KeyPtr InetRoute::GetDBRequestKey() const { 143 1473 : InetTable::RequestKey *key = new InetTable::RequestKey(prefix_, NULL); 144 1473 : 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 1599 : 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 1599 : prefix->prefix.clear(); 159 1599 : 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 1599 : prefix->prefixlen = prefix_.prefixlen(); 172 1599 : const Ip4Address::bytes_type &addr_bytes = prefix_.ip4_addr().to_bytes(); 173 1599 : prefix_size = (prefix->prefixlen + 7) / 8; 174 1599 : copy(addr_bytes.begin(), addr_bytes.begin() + prefix_size, 175 1599 : back_inserter(prefix->prefix)); 176 : } 177 1599 : } 178 : 179 992 : void InetRoute::BuildBgpProtoNextHop(vector<uint8_t> &nh, 180 : IpAddress nexthop) const { 181 992 : nh.resize(Address::kMaxV4Bytes); 182 992 : const Ip4Address::bytes_type &addr_bytes = nexthop.to_v4().to_bytes(); 183 992 : copy(addr_bytes.begin(), addr_bytes.end(), nh.begin()); 184 992 : }