Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 <vnet/ip/ip.h> |
| 17 | #include <vnet/fib/fib_table.h> |
| 18 | |
Neale Ranns | 3d5f08a | 2021-01-22 16:12:38 +0000 | [diff] [blame] | 19 | u32 ip_flow_hash_router_id; |
| 20 | |
Neale Ranns | b28652e | 2021-10-25 09:47:09 +0000 | [diff] [blame] | 21 | ethernet_type_t |
| 22 | ip_address_family_to_ether_type (ip_address_family_t af) |
| 23 | { |
| 24 | switch (af) |
| 25 | { |
| 26 | case AF_IP4: |
| 27 | return (ETHERNET_TYPE_IP4); |
| 28 | case AF_IP6: |
| 29 | return (ETHERNET_TYPE_IP6); |
| 30 | } |
| 31 | ASSERT (0); |
| 32 | return (ETHERNET_TYPE_IP4); |
| 33 | } |
| 34 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 35 | u8 |
| 36 | ip_is_zero (ip46_address_t * ip46_address, u8 is_ip4) |
| 37 | { |
| 38 | if (is_ip4) |
| 39 | return (ip46_address->ip4.as_u32 == 0); |
| 40 | else |
| 41 | return (ip46_address->as_u64[0] == 0 && ip46_address->as_u64[1] == 0); |
| 42 | } |
| 43 | |
| 44 | u8 |
| 45 | ip_is_local_host (ip46_address_t * ip46_address, u8 is_ip4) |
| 46 | { |
| 47 | if (is_ip4) |
| 48 | return (ip46_address->ip4.as_u8[0] == 127); |
| 49 | else |
Dave Wallace | de91006 | 2018-03-20 09:22:13 -0400 | [diff] [blame] | 50 | return (ip46_address->as_u64[0] == 0 && |
| 51 | clib_net_to_host_u64 (ip46_address->as_u64[1]) == 1); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 54 | u8 |
| 55 | ip4_is_local_host (ip4_address_t * ip4_address) |
| 56 | { |
| 57 | return (ip4_address->as_u8[0] == 127); |
| 58 | } |
| 59 | |
| 60 | u8 |
| 61 | ip6_is_local_host (ip6_address_t * ip6_address) |
| 62 | { |
Dave Wallace | de91006 | 2018-03-20 09:22:13 -0400 | [diff] [blame] | 63 | return (ip6_address->as_u64[0] == 0 && |
| 64 | clib_net_to_host_u64 (ip6_address->as_u64[1]) == 1); |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Checks that an ip is local to the requested fib |
| 69 | */ |
| 70 | u8 |
| 71 | ip_is_local (u32 fib_index, ip46_address_t * ip46_address, u8 is_ip4) |
| 72 | { |
| 73 | fib_node_index_t fei; |
| 74 | fib_entry_flag_t flags; |
| 75 | fib_prefix_t prefix; |
| 76 | |
| 77 | /* Check if requester is local */ |
| 78 | if (is_ip4) |
| 79 | { |
| 80 | prefix.fp_len = 32; |
| 81 | prefix.fp_proto = FIB_PROTOCOL_IP4; |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | prefix.fp_len = 128; |
| 86 | prefix.fp_proto = FIB_PROTOCOL_IP6; |
| 87 | } |
| 88 | |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 89 | clib_memcpy_fast (&prefix.fp_addr, ip46_address, sizeof (ip46_address_t)); |
Florin Coras | a46b4c9 | 2018-04-03 02:10:05 -0700 | [diff] [blame] | 90 | fei = fib_table_lookup (fib_index, &prefix); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 91 | flags = fib_entry_get_flags (fei); |
| 92 | |
| 93 | return (flags & FIB_ENTRY_FLAG_LOCAL); |
| 94 | } |
| 95 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 96 | void |
| 97 | ip_copy (ip46_address_t * dst, ip46_address_t * src, u8 is_ip4) |
| 98 | { |
| 99 | if (is_ip4) |
jiangxiaoming | 9268b58 | 2020-06-22 09:06:16 +0800 | [diff] [blame] | 100 | { |
| 101 | ip46_address_mask_ip4 (dst); |
| 102 | dst->ip4.as_u32 = src->ip4.as_u32; |
| 103 | } |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 104 | else |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 105 | clib_memcpy_fast (&dst->ip6, &src->ip6, sizeof (ip6_address_t)); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void |
| 109 | ip_set (ip46_address_t * dst, void *src, u8 is_ip4) |
| 110 | { |
| 111 | if (is_ip4) |
jiangxiaoming | 9268b58 | 2020-06-22 09:06:16 +0800 | [diff] [blame] | 112 | { |
| 113 | ip46_address_mask_ip4 (dst); |
| 114 | dst->ip4.as_u32 = ((ip4_address_t *) src)->as_u32; |
| 115 | } |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 116 | else |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 117 | clib_memcpy_fast (&dst->ip6, (ip6_address_t *) src, |
| 118 | sizeof (ip6_address_t)); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Neale Ranns | 6dc0c8d | 2020-11-26 14:15:33 +0000 | [diff] [blame] | 121 | static const char *ip_arc_names[N_IP_FEATURE_LOCATIONS][N_AF][N_SAFI] = { |
| 122 | [IP_FEATURE_INPUT] = { |
| 123 | [AF_IP4] = { |
| 124 | [SAFI_UNICAST] = "ip4-unicast", |
| 125 | [SAFI_MULTICAST] = "ip4-multicast", |
| 126 | }, |
| 127 | [AF_IP6] = { |
| 128 | [SAFI_UNICAST] = "ip6-unicast", |
| 129 | [SAFI_MULTICAST] = "ip6-multicast", |
| 130 | }, |
| 131 | }, |
| 132 | [IP_FEATURE_OUTPUT] = { |
| 133 | [AF_IP4] = { |
| 134 | [SAFI_UNICAST] = "ip4-output", |
| 135 | [SAFI_MULTICAST] = "ip4-output", |
| 136 | }, |
| 137 | [AF_IP6] = { |
| 138 | [SAFI_UNICAST] = "ip6-output", |
| 139 | [SAFI_MULTICAST] = "ip6-output", |
| 140 | }, |
| 141 | }, |
| 142 | [IP_FEATURE_LOCAL] = { |
| 143 | [AF_IP4] = { |
| 144 | [SAFI_UNICAST] = "ip4-local", |
| 145 | [SAFI_MULTICAST] = "ip4-local", |
| 146 | }, |
| 147 | [AF_IP6] = { |
| 148 | [SAFI_UNICAST] = "ip6-local", |
| 149 | [SAFI_MULTICAST] = "ip6-local", |
| 150 | }, |
| 151 | }, |
| 152 | [IP_FEATURE_PUNT] = { |
| 153 | [AF_IP4] = { |
| 154 | [SAFI_UNICAST] = "ip4-punt", |
| 155 | [SAFI_MULTICAST] = "ip4-punt", |
| 156 | }, |
| 157 | [AF_IP6] = { |
| 158 | [SAFI_UNICAST] = "ip6-punt", |
| 159 | [SAFI_MULTICAST] = "ip6-punt", |
| 160 | }, |
| 161 | }, |
| 162 | [IP_FEATURE_DROP] = { |
| 163 | [AF_IP4] = { |
| 164 | [SAFI_UNICAST] = "ip4-drop", |
| 165 | [SAFI_MULTICAST] = "ip4-drop", |
| 166 | }, |
| 167 | [AF_IP6] = { |
| 168 | [SAFI_UNICAST] = "ip6-drop", |
| 169 | [SAFI_MULTICAST] = "ip6-drop", |
| 170 | }, |
| 171 | }, |
| 172 | }; |
Neale Ranns | 6dc0c8d | 2020-11-26 14:15:33 +0000 | [diff] [blame] | 173 | |
| 174 | void |
| 175 | ip_feature_enable_disable (ip_address_family_t af, |
| 176 | ip_sub_address_family_t safi, |
| 177 | ip_feature_location_t loc, |
| 178 | const char *feature_name, |
| 179 | u32 sw_if_index, int enable, |
| 180 | void *feature_config, u32 n_feature_config_bytes) |
| 181 | { |
| 182 | if (IP_FEATURE_INPUT == loc) |
| 183 | { |
| 184 | if (N_SAFI == safi) |
| 185 | FOR_EACH_IP_ADDRESS_SUB_FAMILY (safi) |
| 186 | vnet_feature_enable_disable (ip_arc_names[loc][af][safi], |
| 187 | feature_name, sw_if_index, |
| 188 | enable, feature_config, |
| 189 | n_feature_config_bytes); |
| 190 | else |
| 191 | vnet_feature_enable_disable (ip_arc_names[loc][af][safi], |
| 192 | feature_name, sw_if_index, |
| 193 | enable, feature_config, |
| 194 | n_feature_config_bytes); |
| 195 | } |
| 196 | else |
| 197 | vnet_feature_enable_disable (ip_arc_names[loc][af][SAFI_UNICAST], |
| 198 | feature_name, sw_if_index, |
| 199 | enable, feature_config, |
| 200 | n_feature_config_bytes); |
| 201 | } |
| 202 | |
Ahmed Abdelsalam | f2984bb | 2020-11-20 18:56:09 +0000 | [diff] [blame] | 203 | int |
Damjan Marion | 804255c | 2024-04-24 11:21:05 +0000 | [diff] [blame^] | 204 | ip_flow_hash_set (ip_address_family_t af, u32 table_id, |
| 205 | flow_hash_config_t flow_hash_config) |
Ahmed Abdelsalam | f2984bb | 2020-11-20 18:56:09 +0000 | [diff] [blame] | 206 | { |
| 207 | fib_protocol_t fproto; |
| 208 | u32 fib_index; |
Neale Ranns | 6dc0c8d | 2020-11-26 14:15:33 +0000 | [diff] [blame] | 209 | |
Ahmed Abdelsalam | f2984bb | 2020-11-20 18:56:09 +0000 | [diff] [blame] | 210 | fproto = ip_address_family_to_fib_proto (af); |
| 211 | fib_index = fib_table_find (fproto, table_id); |
| 212 | |
| 213 | if (~0 == fib_index) |
| 214 | return VNET_API_ERROR_NO_SUCH_FIB; |
| 215 | |
| 216 | fib_table_set_flow_hash_config (fib_index, fproto, flow_hash_config); |
| 217 | |
| 218 | return 0; |
| 219 | } |
Neale Ranns | 6dc0c8d | 2020-11-26 14:15:33 +0000 | [diff] [blame] | 220 | |
Neale Ranns | 3d5f08a | 2021-01-22 16:12:38 +0000 | [diff] [blame] | 221 | void |
| 222 | ip_flow_hash_router_id_set (u32 router_id) |
| 223 | { |
| 224 | ip_flow_hash_router_id = router_id; |
| 225 | } |
| 226 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 227 | u8 * |
| 228 | format_ip_address_family (u8 * s, va_list * args) |
| 229 | { |
Neale Ranns | 3ec09e9 | 2020-02-24 13:32:30 +0000 | [diff] [blame] | 230 | ip_address_family_t af = va_arg (*args, int); // int promo ip_address_family_t); |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 231 | |
| 232 | switch (af) |
| 233 | { |
| 234 | case AF_IP4: |
| 235 | return (format (s, "ip4")); |
| 236 | case AF_IP6: |
| 237 | return (format (s, "ip6")); |
| 238 | } |
| 239 | |
| 240 | return (format (s, "unknown")); |
| 241 | } |
| 242 | |
Neale Ranns | 3ec09e9 | 2020-02-24 13:32:30 +0000 | [diff] [blame] | 243 | uword |
| 244 | unformat_ip_address_family (unformat_input_t * input, va_list * args) |
| 245 | { |
| 246 | ip_address_family_t *af = va_arg (*args, ip_address_family_t *); |
| 247 | |
| 248 | if (unformat (input, "ip4") || unformat (input, "ipv4") || |
| 249 | unformat (input, "IP4") || unformat (input, "IPv4")) |
| 250 | { |
| 251 | *af = AF_IP4; |
| 252 | return (1); |
| 253 | } |
| 254 | else if (unformat (input, "ip6") || unformat (input, "ipv6") || |
| 255 | unformat (input, "IP6") || unformat (input, "IPv6")) |
| 256 | { |
| 257 | *af = AF_IP6; |
| 258 | return (1); |
| 259 | } |
| 260 | return (0); |
| 261 | } |
| 262 | |
Neale Ranns | 038e1df | 2019-07-19 14:01:02 +0000 | [diff] [blame] | 263 | u8 * |
Neale Ranns | 6dc0c8d | 2020-11-26 14:15:33 +0000 | [diff] [blame] | 264 | format_ip_sub_address_family (u8 * s, va_list * args) |
| 265 | { |
| 266 | ip_sub_address_family_t safi = va_arg (*args, int); // int promo ip_sub_address_family_t); |
| 267 | |
| 268 | switch (safi) |
| 269 | { |
| 270 | case SAFI_UNICAST: |
| 271 | return (format (s, "unicast")); |
| 272 | case SAFI_MULTICAST: |
| 273 | return (format (s, "multicast")); |
| 274 | } |
| 275 | |
| 276 | return (format (s, "unknown")); |
| 277 | } |
| 278 | |
| 279 | uword |
| 280 | unformat_ip_sub_address_family (unformat_input_t * input, va_list * args) |
| 281 | { |
| 282 | ip_sub_address_family_t *safi = va_arg (*args, ip_sub_address_family_t *); |
| 283 | |
| 284 | if (unformat (input, "unicast") || unformat (input, "uni")) |
| 285 | { |
| 286 | *safi = SAFI_UNICAST; |
| 287 | return (1); |
| 288 | } |
| 289 | else if (unformat (input, "multicast") || unformat (input, "multi")) |
| 290 | { |
| 291 | *safi = SAFI_MULTICAST; |
| 292 | return (1); |
| 293 | } |
| 294 | return (0); |
| 295 | } |
| 296 | |
| 297 | u8 * |
Neale Ranns | 038e1df | 2019-07-19 14:01:02 +0000 | [diff] [blame] | 298 | format_ip_dscp (u8 * s, va_list * va) |
| 299 | { |
| 300 | ip_dscp_t dscp = va_arg (*va, u32); // int promotion of u8 |
| 301 | |
| 302 | switch (dscp) |
| 303 | { |
| 304 | #define _(n,v) \ |
| 305 | case IP_DSCP_##v: \ |
| 306 | return (format (s, "%s", #v)); |
| 307 | foreach_ip_dscp |
| 308 | #undef _ |
| 309 | } |
| 310 | |
Paul Vinciguerra | 5b755e2 | 2019-10-26 19:34:40 -0400 | [diff] [blame] | 311 | return (format (s, "unknown")); |
Neale Ranns | 038e1df | 2019-07-19 14:01:02 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 314 | uword |
| 315 | unformat_ip_dscp (unformat_input_t * input, va_list * args) |
| 316 | { |
| 317 | ip_dscp_t *dscp = va_arg (*args, ip_dscp_t *); |
| 318 | |
| 319 | if (0) |
| 320 | ; |
| 321 | #define _(n,v) \ |
| 322 | else if (unformat (input, #v)) \ |
| 323 | *dscp = IP_DSCP_##v; |
| 324 | foreach_ip_dscp |
| 325 | #undef _ |
| 326 | else |
| 327 | return 0; |
| 328 | |
| 329 | return 1; |
| 330 | } |
| 331 | |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 332 | u8 * |
| 333 | format_ip_ecn (u8 * s, va_list * va) |
| 334 | { |
| 335 | ip_ecn_t ecn = va_arg (*va, u32); // int promotion of u8 |
| 336 | |
| 337 | switch (ecn) |
| 338 | { |
| 339 | #define _(n,v) \ |
| 340 | case IP_ECN_##v: \ |
| 341 | return (format (s, "%s", #v)); |
| 342 | foreach_ip_ecn |
| 343 | #undef _ |
| 344 | } |
| 345 | |
| 346 | return (format (s, "unknown")); |
| 347 | } |
| 348 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 349 | /* |
| 350 | * fd.io coding-style-patch-verification: ON |
| 351 | * |
| 352 | * Local Variables: |
| 353 | * eval: (c-set-style "gnu") |
| 354 | * End: |
| 355 | */ |