Ole Troan | 8034a36 | 2021-08-11 13:54:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include "ip_sas.h" |
| 17 | #include <vppinfra/types.h> |
| 18 | #include <vnet/ip/ip_interface.h> |
| 19 | #include <vnet/fib/fib_table.h> |
| 20 | #include <vnet/ip/ip6_link.h> |
| 21 | #include <vppinfra/byte_order.h> |
| 22 | |
| 23 | /* |
| 24 | * This file implement source address selection for VPP applications |
| 25 | * (e.g. ping, DNS, ICMP) |
| 26 | * It does not yet implement full fledged RFC6724 SAS. |
| 27 | * SAS assumes every IP enabled interface has an address. The algorithm will |
| 28 | * not go and hunt for a suitable IP address on other interfaces than the |
| 29 | * output interface or the specified preferred sw_if_index. |
| 30 | * That means that an interface with just an IPv6 link-local address must also |
| 31 | * be configured with an unnumbered configuration pointing to a numbered |
| 32 | * interface. |
| 33 | */ |
| 34 | |
| 35 | static int |
| 36 | ip6_sas_commonlen (const ip6_address_t *a1, const ip6_address_t *a2) |
| 37 | { |
| 38 | u64 fa = clib_net_to_host_u64 (a1->as_u64[0]) ^ |
| 39 | clib_net_to_host_u64 (a2->as_u64[0]); |
| 40 | if (fa == 0) |
| 41 | { |
| 42 | u64 la = clib_net_to_host_u64 (a1->as_u64[1]) ^ |
| 43 | clib_net_to_host_u64 (a2->as_u64[1]); |
| 44 | if (la == 0) |
| 45 | return 128; |
| 46 | return 64 + __builtin_clzll (la); |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | return __builtin_clzll (fa); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static int |
| 55 | ip4_sas_commonlen (const ip4_address_t *a1, const ip4_address_t *a2) |
| 56 | { |
Steven Luong | 3ac2ac5 | 2024-07-22 09:27:33 -0700 | [diff] [blame] | 57 | if (!a1 || !a2) |
| 58 | return 0; |
Ole Troan | 8034a36 | 2021-08-11 13:54:14 +0200 | [diff] [blame] | 59 | u64 a = |
| 60 | clib_net_to_host_u32 (a1->as_u32) ^ clib_net_to_host_u32 (a2->as_u32); |
| 61 | if (a == 0) |
| 62 | return 32; |
| 63 | return __builtin_clz (a); |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * walk all addresses on an interface: |
| 68 | * - prefer a source matching the scope of the destination address. |
| 69 | * - last resort pick the source address with the longest |
| 70 | * common prefix with destination |
| 71 | * NOTE: This should at some point implement RFC6724. |
| 72 | */ |
| 73 | bool |
| 74 | ip6_sas_by_sw_if_index (u32 sw_if_index, const ip6_address_t *dst, |
| 75 | ip6_address_t *src) |
| 76 | { |
| 77 | ip_interface_address_t *ia = 0; |
| 78 | ip_lookup_main_t *lm6 = &ip6_main.lookup_main; |
| 79 | ip6_address_t *tmp, *bestsrc = 0; |
| 80 | int bestlen = 0, l; |
| 81 | |
| 82 | if (ip6_address_is_link_local_unicast (dst) || |
| 83 | dst->as_u32[0] == clib_host_to_net_u32 (0xff020000)) |
| 84 | { |
Artem Glazychev | 5b5b89a | 2021-08-23 13:35:31 +0700 | [diff] [blame] | 85 | const ip6_address_t *ll = ip6_get_link_local_address (sw_if_index); |
| 86 | if (NULL == ll) |
| 87 | { |
| 88 | return false; |
| 89 | } |
| 90 | ip6_address_copy (src, ll); |
Ole Troan | 8034a36 | 2021-08-11 13:54:14 +0200 | [diff] [blame] | 91 | return true; |
| 92 | } |
| 93 | |
| 94 | foreach_ip_interface_address ( |
| 95 | lm6, ia, sw_if_index, 1, ({ |
| 96 | if (ia->flags & IP_INTERFACE_ADDRESS_FLAG_STALE) |
| 97 | continue; |
| 98 | tmp = ip_interface_address_get_address (lm6, ia); |
| 99 | l = ip6_sas_commonlen (tmp, dst); |
| 100 | if (l > bestlen || bestsrc == 0) |
| 101 | { |
| 102 | bestsrc = tmp; |
| 103 | bestlen = l; |
| 104 | } |
| 105 | })); |
| 106 | if (bestsrc) |
| 107 | { |
| 108 | ip6_address_copy (src, bestsrc); |
| 109 | return true; |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * walk all addresses on an interface and pick the source address with the |
| 116 | * longest common prefix with destination. |
| 117 | */ |
| 118 | bool |
| 119 | ip4_sas_by_sw_if_index (u32 sw_if_index, const ip4_address_t *dst, |
| 120 | ip4_address_t *src) |
| 121 | { |
| 122 | ip_interface_address_t *ia = 0; |
| 123 | ip_lookup_main_t *lm4 = &ip4_main.lookup_main; |
| 124 | ip4_address_t *tmp, *bestsrc = 0; |
| 125 | int bestlen = 0, l; |
| 126 | |
| 127 | foreach_ip_interface_address ( |
| 128 | lm4, ia, sw_if_index, 1, ({ |
| 129 | if (ia->flags & IP_INTERFACE_ADDRESS_FLAG_STALE) |
| 130 | continue; |
| 131 | tmp = ip_interface_address_get_address (lm4, ia); |
| 132 | l = ip4_sas_commonlen (tmp, dst); |
| 133 | if (l > bestlen || bestsrc == 0) |
| 134 | { |
| 135 | bestsrc = tmp; |
| 136 | bestlen = l; |
| 137 | } |
| 138 | })); |
| 139 | if (bestsrc) |
| 140 | { |
| 141 | src->as_u32 = bestsrc->as_u32; |
| 142 | return true; |
| 143 | } |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * table_id must be set. Default = 0. |
| 149 | * sw_if_index is the interface to pick SA from otherwise ~0 will pick from |
| 150 | * outbound interface. |
| 151 | * |
| 152 | * NOTE: What to do if multiple output interfaces? |
| 153 | * |
| 154 | */ |
| 155 | bool |
| 156 | ip6_sas (u32 table_id, u32 sw_if_index, const ip6_address_t *dst, |
| 157 | ip6_address_t *src) |
| 158 | { |
| 159 | fib_prefix_t prefix; |
| 160 | u32 if_index = sw_if_index; |
| 161 | |
| 162 | /* If sw_if_index is not specified use the output interface. */ |
| 163 | if (sw_if_index == ~0) |
| 164 | { |
| 165 | clib_memcpy (&prefix.fp_addr.ip6, dst, sizeof (*dst)); |
| 166 | prefix.fp_proto = FIB_PROTOCOL_IP6; |
| 167 | prefix.fp_len = 128; |
| 168 | |
| 169 | u32 fib_index = fib_table_find (prefix.fp_proto, table_id); |
| 170 | if (fib_index == (u32) ~0) |
| 171 | return false; |
| 172 | |
| 173 | fib_node_index_t fei = fib_table_lookup (fib_index, &prefix); |
| 174 | if (fei == FIB_NODE_INDEX_INVALID) |
| 175 | return false; |
| 176 | |
| 177 | u32 output_sw_if_index = fib_entry_get_resolving_interface (fei); |
| 178 | if (output_sw_if_index == ~0) |
| 179 | return false; |
| 180 | if_index = output_sw_if_index; |
| 181 | } |
| 182 | return ip6_sas_by_sw_if_index (if_index, dst, src); |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * table_id must be set. Default = 0. |
| 187 | * sw_if_index is the interface to pick SA from otherwise ~0 will pick from |
| 188 | * outbound interface. |
| 189 | * |
| 190 | * NOTE: What to do if multiple output interfaces? |
| 191 | * |
| 192 | */ |
| 193 | bool |
| 194 | ip4_sas (u32 table_id, u32 sw_if_index, const ip4_address_t *dst, |
| 195 | ip4_address_t *src) |
| 196 | { |
| 197 | fib_prefix_t prefix; |
| 198 | u32 if_index = sw_if_index; |
| 199 | |
| 200 | /* If sw_if_index is not specified use the output interface. */ |
| 201 | if (sw_if_index == ~0) |
| 202 | { |
| 203 | clib_memcpy (&prefix.fp_addr.ip4, dst, sizeof (*dst)); |
| 204 | prefix.fp_proto = FIB_PROTOCOL_IP4; |
| 205 | prefix.fp_len = 32; |
| 206 | |
| 207 | u32 fib_index = fib_table_find (prefix.fp_proto, table_id); |
| 208 | if (fib_index == (u32) ~0) |
| 209 | return false; |
| 210 | |
| 211 | fib_node_index_t fei = fib_table_lookup (fib_index, &prefix); |
| 212 | if (fei == FIB_NODE_INDEX_INVALID) |
| 213 | return false; |
| 214 | |
| 215 | u32 output_sw_if_index = fib_entry_get_resolving_interface (fei); |
| 216 | if (output_sw_if_index == ~0) |
| 217 | return false; |
| 218 | if_index = output_sw_if_index; |
| 219 | } |
| 220 | return ip4_sas_by_sw_if_index (if_index, dst, src); |
| 221 | } |