Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * ipip.c: ipip |
| 3 | * |
| 4 | * Copyright (c) 2018 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or aipiped to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <stddef.h> |
| 19 | #include <vnet/adj/adj_midchain.h> |
| 20 | #include <vnet/ipip/ipip.h> |
| 21 | #include <vnet/vnet.h> |
| 22 | #include <vnet/adj/adj_nbr.h> |
Neale Ranns | 4c3ba81 | 2019-03-26 07:02:58 +0000 | [diff] [blame] | 23 | #include <vnet/adj/adj_midchain.h> |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 24 | #include <vnet/fib/ip4_fib.h> |
| 25 | #include <vnet/fib/ip6_fib.h> |
| 26 | #include <vnet/ip/format.h> |
| 27 | #include <vnet/ipip/ipip.h> |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 28 | #include <vnet/teib/teib.h> |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 29 | #include <vnet/tunnel/tunnel_dp.h> |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 30 | |
| 31 | ipip_main_t ipip_main; |
| 32 | |
| 33 | /* Packet trace structure */ |
| 34 | typedef struct |
| 35 | { |
| 36 | u32 tunnel_id; |
| 37 | u32 length; |
| 38 | ip46_address_t src; |
| 39 | ip46_address_t dst; |
| 40 | } ipip_tx_trace_t; |
| 41 | |
| 42 | u8 * |
| 43 | format_ipip_tx_trace (u8 * s, va_list * args) |
| 44 | { |
| 45 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 46 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 47 | ipip_tx_trace_t *t = va_arg (*args, ipip_tx_trace_t *); |
| 48 | |
| 49 | s = |
| 50 | format (s, "IPIP: tunnel %d len %d src %U dst %U", t->tunnel_id, |
| 51 | t->length, format_ip46_address, &t->src, IP46_TYPE_ANY, |
| 52 | format_ip46_address, &t->dst, IP46_TYPE_ANY); |
| 53 | return s; |
| 54 | } |
| 55 | |
| 56 | static u8 * |
| 57 | ipip_build_rewrite (vnet_main_t * vnm, u32 sw_if_index, |
| 58 | vnet_link_t link_type, const void *dst_address) |
| 59 | { |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 60 | const ip46_address_t *dst; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 61 | ip4_header_t *ip4; |
| 62 | ip6_header_t *ip6; |
| 63 | u8 *rewrite = NULL; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 64 | ipip_tunnel_t *t; |
| 65 | |
| 66 | dst = dst_address; |
| 67 | t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 68 | |
| 69 | if (!t) |
| 70 | /* not one of ours */ |
| 71 | return (0); |
| 72 | |
| 73 | switch (t->transport) |
| 74 | { |
| 75 | case IPIP_TRANSPORT_IP4: |
| 76 | vec_validate (rewrite, sizeof (*ip4) - 1); |
| 77 | ip4 = (ip4_header_t *) rewrite; |
| 78 | ip4->ip_version_and_header_length = 0x45; |
| 79 | ip4->ttl = 64; |
| 80 | /* fixup ip4 header length, protocol and checksum after-the-fact */ |
| 81 | ip4->src_address.as_u32 = t->tunnel_src.ip4.as_u32; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 82 | ip4->dst_address.as_u32 = dst->ip4.as_u32; |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 83 | if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)) |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 84 | ip4_header_set_dscp (ip4, t->dscp); |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 85 | if (t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_SET_DF) |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 86 | ip4_header_set_df (ip4); |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 87 | |
| 88 | switch (link_type) |
| 89 | { |
| 90 | case VNET_LINK_IP6: |
| 91 | ip4->protocol = IP_PROTOCOL_IPV6; |
| 92 | break; |
| 93 | case VNET_LINK_IP4: |
| 94 | ip4->protocol = IP_PROTOCOL_IP_IN_IP; |
| 95 | break; |
Neale Ranns | e294de6 | 2020-12-21 09:52:24 +0000 | [diff] [blame] | 96 | case VNET_LINK_MPLS: |
| 97 | ip4->protocol = IP_PROTOCOL_MPLS_IN_IP; |
| 98 | break; |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 99 | default: |
| 100 | break; |
| 101 | } |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 102 | ip4->checksum = ip4_header_checksum (ip4); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 103 | break; |
| 104 | |
| 105 | case IPIP_TRANSPORT_IP6: |
| 106 | vec_validate (rewrite, sizeof (*ip6) - 1); |
| 107 | ip6 = (ip6_header_t *) rewrite; |
| 108 | ip6->ip_version_traffic_class_and_flow_label = |
| 109 | clib_host_to_net_u32 (6 << 28); |
| 110 | ip6->hop_limit = 64; |
| 111 | /* fixup ip6 header length and protocol after-the-fact */ |
| 112 | ip6->src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0]; |
| 113 | ip6->src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1]; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 114 | ip6->dst_address.as_u64[0] = dst->ip6.as_u64[0]; |
| 115 | ip6->dst_address.as_u64[1] = dst->ip6.as_u64[1]; |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 116 | if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)) |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 117 | ip6_set_dscp_network_order (ip6, t->dscp); |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 118 | |
| 119 | switch (link_type) |
| 120 | { |
| 121 | case VNET_LINK_IP6: |
| 122 | ip6->protocol = IP_PROTOCOL_IPV6; |
| 123 | break; |
| 124 | case VNET_LINK_IP4: |
| 125 | ip6->protocol = IP_PROTOCOL_IP_IN_IP; |
| 126 | break; |
Neale Ranns | e294de6 | 2020-12-21 09:52:24 +0000 | [diff] [blame] | 127 | case VNET_LINK_MPLS: |
| 128 | ip6->protocol = IP_PROTOCOL_MPLS_IN_IP; |
| 129 | break; |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 130 | default: |
| 131 | break; |
| 132 | } |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 133 | break; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 134 | } |
| 135 | return (rewrite); |
| 136 | } |
| 137 | |
| 138 | static void |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 139 | ipip64_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b, |
| 140 | const void *data) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 141 | { |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 142 | tunnel_encap_decap_flags_t flags; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 143 | ip4_header_t *ip4; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 144 | |
| 145 | flags = pointer_to_uword (data); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 146 | |
| 147 | ip4 = vlib_buffer_get_current (b); |
| 148 | ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b)); |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 149 | tunnel_encap_fixup_6o4 (flags, ((ip6_header_t *) (ip4 + 1)), ip4); |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 150 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 151 | ip4->checksum = ip4_header_checksum (ip4); |
| 152 | } |
| 153 | |
| 154 | static void |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 155 | ipip44_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b, |
| 156 | const void *data) |
| 157 | { |
| 158 | tunnel_encap_decap_flags_t flags; |
| 159 | ip4_header_t *ip4; |
| 160 | |
| 161 | flags = pointer_to_uword (data); |
| 162 | |
| 163 | ip4 = vlib_buffer_get_current (b); |
| 164 | ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b)); |
| 165 | tunnel_encap_fixup_4o4 (flags, ip4 + 1, ip4); |
| 166 | |
| 167 | ip4->checksum = ip4_header_checksum (ip4); |
| 168 | } |
| 169 | |
| 170 | static void |
| 171 | ipip46_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b, |
| 172 | const void *data) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 173 | { |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 174 | tunnel_encap_decap_flags_t flags; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 175 | ip6_header_t *ip6; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 176 | |
| 177 | flags = pointer_to_uword (data); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 178 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 179 | /* Must set locally originated otherwise we're not allowed to |
| 180 | fragment the packet later */ |
| 181 | b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
| 182 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 183 | ip6 = vlib_buffer_get_current (b); |
| 184 | ip6->payload_length = |
| 185 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) - |
| 186 | sizeof (*ip6)); |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 187 | tunnel_encap_fixup_4o6 (flags, b, ((ip4_header_t *) (ip6 + 1)), ip6); |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 188 | } |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 189 | |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 190 | static void |
| 191 | ipip66_fixup (vlib_main_t * vm, |
| 192 | const ip_adjacency_t * adj, vlib_buffer_t * b, const void *data) |
| 193 | { |
| 194 | tunnel_encap_decap_flags_t flags; |
| 195 | ip6_header_t *ip6; |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 196 | |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 197 | flags = pointer_to_uword (data); |
| 198 | |
| 199 | /* Must set locally originated otherwise we're not allowed to |
| 200 | fragment the packet later */ |
| 201 | b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
| 202 | |
| 203 | ip6 = vlib_buffer_get_current (b); |
| 204 | ip6->payload_length = |
| 205 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) - |
| 206 | sizeof (*ip6)); |
| 207 | tunnel_encap_fixup_6o6 (flags, ip6 + 1, ip6); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static void |
Neale Ranns | e294de6 | 2020-12-21 09:52:24 +0000 | [diff] [blame] | 211 | ipipm6_fixup (vlib_main_t *vm, const ip_adjacency_t *adj, vlib_buffer_t *b, |
| 212 | const void *data) |
| 213 | { |
| 214 | tunnel_encap_decap_flags_t flags; |
| 215 | ip6_header_t *ip6; |
| 216 | |
| 217 | flags = pointer_to_uword (data); |
| 218 | |
| 219 | /* Must set locally originated otherwise we're not allowed to |
| 220 | fragment the packet later and we'll get an unwanted hop-limt |
| 221 | decrement */ |
| 222 | b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
| 223 | |
| 224 | ip6 = vlib_buffer_get_current (b); |
| 225 | ip6->payload_length = |
| 226 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) - sizeof (*ip6)); |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 227 | tunnel_encap_fixup_mplso6 (flags, b, (mpls_unicast_header_t *) (ip6 + 1), |
| 228 | ip6); |
Neale Ranns | e294de6 | 2020-12-21 09:52:24 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static void |
| 232 | ipipm4_fixup (vlib_main_t *vm, const ip_adjacency_t *adj, vlib_buffer_t *b, |
| 233 | const void *data) |
| 234 | { |
| 235 | tunnel_encap_decap_flags_t flags; |
| 236 | ip4_header_t *ip4; |
| 237 | |
| 238 | flags = pointer_to_uword (data); |
| 239 | |
| 240 | /* Must set locally originated otherwise we'll do a TTL decrement |
| 241 | * during ip4-rewrite */ |
| 242 | b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
| 243 | |
| 244 | ip4 = vlib_buffer_get_current (b); |
| 245 | ip4->length = |
| 246 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) - sizeof (*ip4)); |
| 247 | tunnel_encap_fixup_mplso4 (flags, (mpls_unicast_header_t *) (ip4 + 1), ip4); |
| 248 | ip4->checksum = ip4_header_checksum (ip4); |
| 249 | } |
| 250 | |
| 251 | static void |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 252 | ipip_tunnel_stack (adj_index_t ai) |
| 253 | { |
| 254 | ip_adjacency_t *adj; |
| 255 | ipip_tunnel_t *t; |
| 256 | u32 sw_if_index; |
| 257 | |
| 258 | adj = adj_get (ai); |
| 259 | sw_if_index = adj->rewrite_header.sw_if_index; |
| 260 | |
| 261 | t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
| 262 | if (!t) |
| 263 | return; |
| 264 | |
| 265 | if ((vnet_hw_interface_get_flags (vnet_get_main (), t->hw_if_index) & |
| 266 | VNET_HW_INTERFACE_FLAG_LINK_UP) == 0) |
| 267 | { |
Neale Ranns | 4c3ba81 | 2019-03-26 07:02:58 +0000 | [diff] [blame] | 268 | adj_midchain_delegate_unstack (ai); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 269 | } |
Neale Ranns | 521a8d7 | 2018-12-06 13:46:49 +0000 | [diff] [blame] | 270 | else |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 271 | { |
Neale Ranns | 4c3ba81 | 2019-03-26 07:02:58 +0000 | [diff] [blame] | 272 | /* *INDENT-OFF* */ |
| 273 | fib_prefix_t dst = { |
| 274 | .fp_len = t->transport == IPIP_TRANSPORT_IP6 ? 128 : 32, |
| 275 | .fp_proto = (t->transport == IPIP_TRANSPORT_IP6 ? |
| 276 | FIB_PROTOCOL_IP6 : |
| 277 | FIB_PROTOCOL_IP4), |
| 278 | .fp_addr = t->tunnel_dst |
| 279 | }; |
| 280 | /* *INDENT-ON* */ |
| 281 | |
| 282 | adj_midchain_delegate_stack (ai, t->fib_index, &dst); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 283 | } |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static adj_walk_rc_t |
| 287 | ipip_adj_walk_cb (adj_index_t ai, void *ctx) |
| 288 | { |
| 289 | ipip_tunnel_stack (ai); |
| 290 | |
| 291 | return (ADJ_WALK_RC_CONTINUE); |
| 292 | } |
| 293 | |
| 294 | static void |
| 295 | ipip_tunnel_restack (ipip_tunnel_t * gt) |
| 296 | { |
| 297 | fib_protocol_t proto; |
| 298 | |
| 299 | /* |
| 300 | * walk all the adjacencies on th IPIP interface and restack them |
| 301 | */ |
| 302 | FOR_EACH_FIB_IP_PROTOCOL (proto) |
| 303 | { |
| 304 | adj_nbr_walk (gt->sw_if_index, proto, ipip_adj_walk_cb, NULL); |
| 305 | } |
| 306 | } |
| 307 | |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 308 | static adj_midchain_fixup_t |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 309 | ipip_get_fixup (const ipip_tunnel_t * t, vnet_link_t lt, adj_flags_t * aflags) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 310 | { |
| 311 | if (t->transport == IPIP_TRANSPORT_IP6 && lt == VNET_LINK_IP6) |
| 312 | return (ipip66_fixup); |
| 313 | if (t->transport == IPIP_TRANSPORT_IP6 && lt == VNET_LINK_IP4) |
| 314 | return (ipip46_fixup); |
Neale Ranns | e294de6 | 2020-12-21 09:52:24 +0000 | [diff] [blame] | 315 | if (t->transport == IPIP_TRANSPORT_IP6 && lt == VNET_LINK_MPLS) |
| 316 | return (ipipm6_fixup); |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 317 | if (t->transport == IPIP_TRANSPORT_IP4 && lt == VNET_LINK_IP6) |
| 318 | return (ipip64_fixup); |
Neale Ranns | e294de6 | 2020-12-21 09:52:24 +0000 | [diff] [blame] | 319 | if (t->transport == IPIP_TRANSPORT_IP4 && lt == VNET_LINK_MPLS) |
| 320 | return (ipipm4_fixup); |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 321 | if (t->transport == IPIP_TRANSPORT_IP4 && lt == VNET_LINK_IP4) |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 322 | { |
| 323 | *aflags = *aflags | ADJ_FLAG_MIDCHAIN_FIXUP_IP4O4_HDR; |
| 324 | return (ipip44_fixup); |
| 325 | } |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 326 | |
| 327 | ASSERT (0); |
| 328 | return (ipip44_fixup); |
| 329 | } |
| 330 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 331 | void |
| 332 | ipip_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai) |
| 333 | { |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 334 | adj_midchain_fixup_t fixup; |
Neale Ranns | 521a8d7 | 2018-12-06 13:46:49 +0000 | [diff] [blame] | 335 | ipip_tunnel_t *t; |
| 336 | adj_flags_t af; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 337 | |
Neale Ranns | 65d789e | 2021-02-08 15:24:56 +0000 | [diff] [blame] | 338 | af = ADJ_FLAG_NONE; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 339 | t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
| 340 | if (!t) |
| 341 | return; |
| 342 | |
Neale Ranns | 65d789e | 2021-02-08 15:24:56 +0000 | [diff] [blame] | 343 | /* |
| 344 | * the user has not requested that the load-balancing be based on |
| 345 | * a flow hash of the inner packet. so use the stacking to choose |
| 346 | * a path. |
| 347 | */ |
| 348 | if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_INNER_HASH)) |
| 349 | af |= ADJ_FLAG_MIDCHAIN_IP_STACK; |
Mohammed Hawari | 33c45f5 | 2020-11-30 13:50:24 +0100 | [diff] [blame] | 350 | |
Neale Ranns | 521a8d7 | 2018-12-06 13:46:49 +0000 | [diff] [blame] | 351 | if (VNET_LINK_ETHERNET == adj_get_link_type (ai)) |
| 352 | af |= ADJ_FLAG_MIDCHAIN_NO_COUNT; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 353 | |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 354 | fixup = ipip_get_fixup (t, adj_get_link_type (ai), &af); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 355 | adj_nbr_midchain_update_rewrite |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 356 | (ai, fixup, |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 357 | uword_to_pointer (t->flags, void *), af, |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 358 | ipip_build_rewrite (vnm, sw_if_index, |
| 359 | adj_get_link_type (ai), &t->tunnel_dst)); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 360 | ipip_tunnel_stack (ai); |
| 361 | } |
| 362 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 363 | typedef struct mipip_walk_ctx_t_ |
| 364 | { |
| 365 | const ipip_tunnel_t *t; |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 366 | const teib_entry_t *ne; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 367 | } mipip_walk_ctx_t; |
| 368 | |
| 369 | static adj_walk_rc_t |
| 370 | mipip_mk_complete_walk (adj_index_t ai, void *data) |
| 371 | { |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 372 | adj_midchain_fixup_t fixup; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 373 | mipip_walk_ctx_t *ctx = data; |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 374 | adj_flags_t af; |
| 375 | |
| 376 | af = ADJ_FLAG_NONE; |
| 377 | fixup = ipip_get_fixup (ctx->t, adj_get_link_type (ai), &af); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 378 | |
Neale Ranns | 65d789e | 2021-02-08 15:24:56 +0000 | [diff] [blame] | 379 | /* |
| 380 | * the user has not requested that the load-balancing be based on |
| 381 | * a flow hash of the inner packet. so use the stacking to choose |
| 382 | * a path. |
| 383 | */ |
| 384 | if (!(ctx->t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_INNER_HASH)) |
| 385 | af |= ADJ_FLAG_MIDCHAIN_IP_STACK; |
Mohammed Hawari | 33c45f5 | 2020-11-30 13:50:24 +0100 | [diff] [blame] | 386 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 387 | adj_nbr_midchain_update_rewrite |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 388 | (ai, fixup, |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 389 | uword_to_pointer (ctx->t->flags, void *), |
Mohammed Hawari | 33c45f5 | 2020-11-30 13:50:24 +0100 | [diff] [blame] | 390 | af, ipip_build_rewrite (vnet_get_main (), |
| 391 | ctx->t->sw_if_index, |
| 392 | adj_get_link_type (ai), |
| 393 | &teib_entry_get_nh (ctx->ne)->fp_addr)); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 394 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 395 | teib_entry_adj_stack (ctx->ne, ai); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 396 | |
| 397 | return (ADJ_WALK_RC_CONTINUE); |
| 398 | } |
| 399 | |
| 400 | static adj_walk_rc_t |
| 401 | mipip_mk_incomplete_walk (adj_index_t ai, void *data) |
| 402 | { |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 403 | adj_midchain_fixup_t fixup; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 404 | ipip_tunnel_t *t = data; |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 405 | adj_flags_t af; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 406 | |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 407 | af = ADJ_FLAG_NONE; |
| 408 | fixup = ipip_get_fixup (t, adj_get_link_type (ai), &af); |
| 409 | |
| 410 | adj_nbr_midchain_update_rewrite (ai, fixup, NULL, ADJ_FLAG_NONE, NULL); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 411 | |
| 412 | adj_midchain_delegate_unstack (ai); |
| 413 | |
| 414 | return (ADJ_WALK_RC_CONTINUE); |
| 415 | } |
| 416 | |
| 417 | void |
| 418 | mipip_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai) |
| 419 | { |
| 420 | ipip_main_t *gm = &ipip_main; |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 421 | adj_midchain_fixup_t fixup; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 422 | ip_adjacency_t *adj; |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 423 | teib_entry_t *ne; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 424 | ipip_tunnel_t *t; |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 425 | adj_flags_t af; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 426 | u32 ti; |
| 427 | |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 428 | af = ADJ_FLAG_NONE; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 429 | adj = adj_get (ai); |
| 430 | ti = gm->tunnel_index_by_sw_if_index[sw_if_index]; |
| 431 | t = pool_elt_at_index (gm->tunnels, ti); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 432 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 433 | ne = teib_entry_find_46 (sw_if_index, |
| 434 | adj->ia_nh_proto, &adj->sub_type.nbr.next_hop); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 435 | |
| 436 | if (NULL == ne) |
| 437 | { |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 438 | // no TEIB entry to provide the next-hop |
| 439 | fixup = ipip_get_fixup (t, adj_get_link_type (ai), &af); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 440 | adj_nbr_midchain_update_rewrite |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 441 | (ai, fixup, uword_to_pointer (t->flags, void *), ADJ_FLAG_NONE, NULL); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 442 | return; |
| 443 | } |
| 444 | |
| 445 | mipip_walk_ctx_t ctx = { |
| 446 | .t = t, |
| 447 | .ne = ne |
| 448 | }; |
| 449 | adj_nbr_walk_nh (sw_if_index, |
| 450 | adj->ia_nh_proto, |
| 451 | &adj->sub_type.nbr.next_hop, mipip_mk_complete_walk, &ctx); |
| 452 | } |
| 453 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 454 | static u8 * |
| 455 | format_ipip_tunnel_name (u8 * s, va_list * args) |
| 456 | { |
| 457 | u32 dev_instance = va_arg (*args, u32); |
| 458 | ipip_main_t *gm = &ipip_main; |
| 459 | ipip_tunnel_t *t; |
| 460 | |
| 461 | if (dev_instance >= vec_len (gm->tunnels)) |
| 462 | return format (s, "<improperly-referenced>"); |
| 463 | |
| 464 | t = pool_elt_at_index (gm->tunnels, dev_instance); |
| 465 | return format (s, "ipip%d", t->user_instance); |
| 466 | } |
| 467 | |
| 468 | static u8 * |
| 469 | format_ipip_device (u8 * s, va_list * args) |
| 470 | { |
| 471 | u32 dev_instance = va_arg (*args, u32); |
| 472 | CLIB_UNUSED (int verbose) = va_arg (*args, int); |
| 473 | |
| 474 | s = format (s, "IPIP tunnel: id %d\n", dev_instance); |
| 475 | return s; |
| 476 | } |
| 477 | |
| 478 | static clib_error_t * |
| 479 | ipip_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 480 | { |
| 481 | vnet_hw_interface_t *hi; |
| 482 | ipip_tunnel_t *t; |
| 483 | |
| 484 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 485 | |
| 486 | t = ipip_tunnel_db_find_by_sw_if_index (hi->sw_if_index); |
| 487 | if (!t) |
| 488 | return 0; |
| 489 | |
| 490 | if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) |
| 491 | vnet_hw_interface_set_flags (vnm, hw_if_index, |
| 492 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 493 | else |
| 494 | vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ ); |
| 495 | |
| 496 | ipip_tunnel_restack (t); |
| 497 | |
| 498 | return /* no error */ 0; |
| 499 | } |
| 500 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 501 | static int |
| 502 | ipip_tunnel_desc (u32 sw_if_index, |
| 503 | ip46_address_t * src, ip46_address_t * dst, u8 * is_l2) |
| 504 | { |
| 505 | ipip_tunnel_t *t; |
| 506 | |
| 507 | t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
| 508 | if (!t) |
| 509 | return -1; |
| 510 | |
| 511 | *src = t->tunnel_src; |
| 512 | *dst = t->tunnel_dst; |
| 513 | *is_l2 = 0; |
| 514 | |
| 515 | return (0); |
| 516 | } |
| 517 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 518 | /* *INDENT-OFF* */ |
| 519 | VNET_DEVICE_CLASS(ipip_device_class) = { |
| 520 | .name = "IPIP tunnel device", |
| 521 | .format_device_name = format_ipip_tunnel_name, |
| 522 | .format_device = format_ipip_device, |
| 523 | .format_tx_trace = format_ipip_tx_trace, |
| 524 | .admin_up_down_function = ipip_interface_admin_up_down, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 525 | .ip_tun_desc = ipip_tunnel_desc, |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 526 | #ifdef SOON |
| 527 | .clear counter = 0; |
| 528 | #endif |
| 529 | }; |
| 530 | |
| 531 | VNET_HW_INTERFACE_CLASS(ipip_hw_interface_class) = { |
| 532 | .name = "IPIP", |
| 533 | //.format_header = format_ipip_header_with_length, |
| 534 | //.unformat_header = unformat_ipip_header, |
| 535 | .build_rewrite = ipip_build_rewrite, |
| 536 | .update_adjacency = ipip_update_adj, |
| 537 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
| 538 | }; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 539 | |
| 540 | VNET_HW_INTERFACE_CLASS(mipip_hw_interface_class) = { |
| 541 | .name = "mIPIP", |
| 542 | //.format_header = format_ipip_header_with_length, |
| 543 | //.unformat_header = unformat_ipip_header, |
| 544 | .build_rewrite = ipip_build_rewrite, |
| 545 | .update_adjacency = mipip_update_adj, |
| 546 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA, |
| 547 | }; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 548 | /* *INDENT-ON* */ |
| 549 | |
| 550 | ipip_tunnel_t * |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 551 | ipip_tunnel_db_find (const ipip_tunnel_key_t * key) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 552 | { |
| 553 | ipip_main_t *gm = &ipip_main; |
| 554 | uword *p; |
| 555 | |
| 556 | p = hash_get_mem (gm->tunnel_by_key, key); |
| 557 | if (!p) |
| 558 | return (NULL); |
| 559 | return (pool_elt_at_index (gm->tunnels, p[0])); |
| 560 | } |
| 561 | |
| 562 | ipip_tunnel_t * |
| 563 | ipip_tunnel_db_find_by_sw_if_index (u32 sw_if_index) |
| 564 | { |
| 565 | ipip_main_t *gm = &ipip_main; |
Eyal Bari | cd30774 | 2018-07-22 12:45:15 +0300 | [diff] [blame] | 566 | if (vec_len (gm->tunnel_index_by_sw_if_index) <= sw_if_index) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 567 | return NULL; |
| 568 | u32 ti = gm->tunnel_index_by_sw_if_index[sw_if_index]; |
| 569 | if (ti == ~0) |
| 570 | return NULL; |
| 571 | return pool_elt_at_index (gm->tunnels, ti); |
| 572 | } |
| 573 | |
| 574 | void |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 575 | ipip_tunnel_db_add (ipip_tunnel_t * t, const ipip_tunnel_key_t * key) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 576 | { |
| 577 | ipip_main_t *gm = &ipip_main; |
| 578 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 579 | hash_set_mem_alloc (&gm->tunnel_by_key, key, t->dev_instance); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | void |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 583 | ipip_tunnel_db_remove (ipip_tunnel_t * t, const ipip_tunnel_key_t * key) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 584 | { |
| 585 | ipip_main_t *gm = &ipip_main; |
| 586 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 587 | hash_unset_mem_free (&gm->tunnel_by_key, key); |
| 588 | } |
| 589 | |
| 590 | void |
| 591 | ipip_mk_key_i (ipip_transport_t transport, |
| 592 | ipip_mode_t mode, |
| 593 | const ip46_address_t * src, |
| 594 | const ip46_address_t * dst, |
| 595 | u32 fib_index, ipip_tunnel_key_t * key) |
| 596 | { |
| 597 | key->transport = transport; |
| 598 | key->mode = mode; |
| 599 | key->src = *src; |
| 600 | key->dst = *dst; |
| 601 | key->fib_index = fib_index; |
| 602 | key->__pad = 0;; |
| 603 | } |
| 604 | |
| 605 | void |
| 606 | ipip_mk_key (const ipip_tunnel_t * t, ipip_tunnel_key_t * key) |
| 607 | { |
| 608 | ipip_mk_key_i (t->transport, t->mode, |
| 609 | &t->tunnel_src, &t->tunnel_dst, t->fib_index, key); |
| 610 | } |
| 611 | |
| 612 | static void |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 613 | ipip_teib_mk_key (const ipip_tunnel_t * t, |
| 614 | const teib_entry_t * ne, ipip_tunnel_key_t * key) |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 615 | { |
| 616 | const fib_prefix_t *nh; |
| 617 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 618 | nh = teib_entry_get_nh (ne); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 619 | |
| 620 | /* construct the key using mode P2P so it can be found in the DP */ |
| 621 | ipip_mk_key_i (t->transport, IPIP_MODE_P2P, |
| 622 | &t->tunnel_src, &nh->fp_addr, |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 623 | teib_entry_get_fib_index (ne), key); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | static void |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 627 | ipip_teib_entry_added (const teib_entry_t * ne) |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 628 | { |
| 629 | ipip_main_t *gm = &ipip_main; |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 630 | const ip_address_t *nh; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 631 | ipip_tunnel_key_t key; |
| 632 | ipip_tunnel_t *t; |
| 633 | u32 sw_if_index; |
| 634 | u32 t_idx; |
| 635 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 636 | sw_if_index = teib_entry_get_sw_if_index (ne); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 637 | if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index) |
| 638 | return; |
| 639 | |
| 640 | t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index]; |
| 641 | |
| 642 | if (INDEX_INVALID == t_idx) |
| 643 | return; |
| 644 | |
| 645 | t = pool_elt_at_index (gm->tunnels, t_idx); |
| 646 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 647 | ipip_teib_mk_key (t, ne, &key); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 648 | ipip_tunnel_db_add (t, &key); |
| 649 | |
| 650 | // update the rewrites for each of the adjacencies for this next-hop |
| 651 | mipip_walk_ctx_t ctx = { |
| 652 | .t = t, |
| 653 | .ne = ne |
| 654 | }; |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 655 | nh = teib_entry_get_peer (ne); |
| 656 | adj_nbr_walk_nh (teib_entry_get_sw_if_index (ne), |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 657 | (AF_IP4 == ip_addr_version (nh) ? |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 658 | FIB_PROTOCOL_IP4 : |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 659 | FIB_PROTOCOL_IP6), |
| 660 | &ip_addr_46 (nh), mipip_mk_complete_walk, &ctx); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | static void |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 664 | ipip_teib_entry_deleted (const teib_entry_t * ne) |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 665 | { |
| 666 | ipip_main_t *gm = &ipip_main; |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 667 | const ip_address_t *nh; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 668 | ipip_tunnel_key_t key; |
| 669 | ipip_tunnel_t *t; |
| 670 | u32 sw_if_index; |
| 671 | u32 t_idx; |
| 672 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 673 | sw_if_index = teib_entry_get_sw_if_index (ne); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 674 | if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index) |
| 675 | return; |
| 676 | |
| 677 | t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index]; |
| 678 | |
| 679 | if (INDEX_INVALID == t_idx) |
| 680 | return; |
| 681 | |
| 682 | t = pool_elt_at_index (gm->tunnels, t_idx); |
| 683 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 684 | ipip_teib_mk_key (t, ne, &key); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 685 | ipip_tunnel_db_remove (t, &key); |
| 686 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 687 | nh = teib_entry_get_peer (ne); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 688 | |
| 689 | /* make all the adjacencies incomplete */ |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 690 | adj_nbr_walk_nh (teib_entry_get_sw_if_index (ne), |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 691 | (AF_IP4 == ip_addr_version (nh) ? |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 692 | FIB_PROTOCOL_IP4 : |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 693 | FIB_PROTOCOL_IP6), |
| 694 | &ip_addr_46 (nh), mipip_mk_incomplete_walk, t); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | static walk_rc_t |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 698 | ipip_tunnel_delete_teib_walk (index_t nei, void *ctx) |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 699 | { |
| 700 | ipip_tunnel_t *t = ctx; |
| 701 | ipip_tunnel_key_t key; |
| 702 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 703 | ipip_teib_mk_key (t, teib_entry_get (nei), &key); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 704 | ipip_tunnel_db_remove (t, &key); |
| 705 | |
| 706 | return (WALK_CONTINUE); |
| 707 | } |
| 708 | |
| 709 | static walk_rc_t |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 710 | ipip_tunnel_add_teib_walk (index_t nei, void *ctx) |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 711 | { |
| 712 | ipip_tunnel_t *t = ctx; |
| 713 | ipip_tunnel_key_t key; |
| 714 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 715 | ipip_teib_mk_key (t, teib_entry_get (nei), &key); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 716 | ipip_tunnel_db_add (t, &key); |
| 717 | |
| 718 | return (WALK_CONTINUE); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 719 | } |
| 720 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 721 | int |
| 722 | ipip_add_tunnel (ipip_transport_t transport, |
| 723 | u32 instance, ip46_address_t * src, ip46_address_t * dst, |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 724 | u32 fib_index, tunnel_encap_decap_flags_t flags, |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 725 | ip_dscp_t dscp, tunnel_mode_t tmode, u32 * sw_if_indexp) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 726 | { |
| 727 | ipip_main_t *gm = &ipip_main; |
| 728 | vnet_main_t *vnm = gm->vnet_main; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 729 | ipip_tunnel_t *t; |
| 730 | vnet_hw_interface_t *hi; |
| 731 | u32 hw_if_index, sw_if_index; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 732 | ipip_tunnel_key_t key; |
| 733 | ipip_mode_t mode; |
| 734 | |
| 735 | if (tmode == TUNNEL_MODE_MP && !ip46_address_is_zero (dst)) |
| 736 | return (VNET_API_ERROR_INVALID_DST_ADDRESS); |
| 737 | |
| 738 | mode = (tmode == TUNNEL_MODE_P2P ? IPIP_MODE_P2P : IPIP_MODE_P2MP); |
| 739 | ipip_mk_key_i (transport, mode, src, dst, fib_index, &key); |
| 740 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 741 | t = ipip_tunnel_db_find (&key); |
| 742 | if (t) |
Filip Tehlar | 4c6b1b6 | 2019-11-30 20:49:40 +0000 | [diff] [blame] | 743 | { |
| 744 | if (sw_if_indexp) |
| 745 | sw_if_indexp[0] = t->sw_if_index; |
| 746 | return VNET_API_ERROR_IF_ALREADY_EXISTS; |
| 747 | } |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 748 | |
| 749 | pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 750 | clib_memset (t, 0, sizeof (*t)); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 751 | |
| 752 | /* Reconcile the real dev_instance and a possible requested instance */ |
| 753 | u32 t_idx = t - gm->tunnels; /* tunnel index (or instance) */ |
| 754 | u32 u_idx = instance; /* user specified instance */ |
| 755 | if (u_idx == ~0) |
| 756 | u_idx = t_idx; |
| 757 | if (hash_get (gm->instance_used, u_idx)) |
| 758 | { |
| 759 | pool_put (gm->tunnels, t); |
| 760 | return VNET_API_ERROR_INSTANCE_IN_USE; |
| 761 | } |
| 762 | hash_set (gm->instance_used, u_idx, 1); |
| 763 | |
| 764 | t->dev_instance = t_idx; /* actual */ |
| 765 | t->user_instance = u_idx; /* name */ |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 766 | |
| 767 | hw_if_index = vnet_register_interface (vnm, ipip_device_class.index, t_idx, |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 768 | (mode == IPIP_MODE_P2P ? |
| 769 | ipip_hw_interface_class.index : |
| 770 | mipip_hw_interface_class.index), |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 771 | t_idx); |
| 772 | |
| 773 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 774 | sw_if_index = hi->sw_if_index; |
| 775 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 776 | t->mode = mode; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 777 | t->hw_if_index = hw_if_index; |
| 778 | t->fib_index = fib_index; |
| 779 | t->sw_if_index = sw_if_index; |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 780 | t->dscp = dscp; |
| 781 | t->flags = flags; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 782 | t->transport = transport; |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 783 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 784 | vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0); |
| 785 | gm->tunnel_index_by_sw_if_index[sw_if_index] = t_idx; |
| 786 | |
| 787 | if (t->transport == IPIP_TRANSPORT_IP4) |
| 788 | { |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 789 | hi->min_packet_bytes = 64 + sizeof (ip4_header_t); |
| 790 | } |
| 791 | else |
| 792 | { |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 793 | hi->min_packet_bytes = 64 + sizeof (ip6_header_t); |
| 794 | } |
| 795 | |
Damjan Marion | fe7d4a2 | 2018-04-13 19:43:39 +0200 | [diff] [blame] | 796 | /* Standard default ipip MTU. */ |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 797 | vnet_sw_interface_set_mtu (vnm, sw_if_index, 9000); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 798 | |
| 799 | t->tunnel_src = *src; |
| 800 | t->tunnel_dst = *dst; |
| 801 | |
| 802 | ipip_tunnel_db_add (t, &key); |
| 803 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 804 | if (t->mode == IPIP_MODE_P2MP) |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 805 | teib_walk_itf (t->sw_if_index, ipip_tunnel_add_teib_walk, t); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 806 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 807 | if (sw_if_indexp) |
| 808 | *sw_if_indexp = sw_if_index; |
| 809 | |
| 810 | if (t->transport == IPIP_TRANSPORT_IP6 && !gm->ip6_protocol_registered) |
| 811 | { |
| 812 | ip6_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip6_input_node.index); |
Neale Ranns | e294de6 | 2020-12-21 09:52:24 +0000 | [diff] [blame] | 813 | ip6_register_protocol (IP_PROTOCOL_MPLS_IN_IP, ipip6_input_node.index); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 814 | ip6_register_protocol (IP_PROTOCOL_IPV6, ipip6_input_node.index); |
| 815 | gm->ip6_protocol_registered = true; |
| 816 | } |
| 817 | else if (t->transport == IPIP_TRANSPORT_IP4 && !gm->ip4_protocol_registered) |
| 818 | { |
| 819 | ip4_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip4_input_node.index); |
Neale Ranns | e294de6 | 2020-12-21 09:52:24 +0000 | [diff] [blame] | 820 | ip4_register_protocol (IP_PROTOCOL_MPLS_IN_IP, ipip4_input_node.index); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 821 | ip4_register_protocol (IP_PROTOCOL_IPV6, ipip4_input_node.index); |
| 822 | gm->ip4_protocol_registered = true; |
| 823 | } |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | int |
| 828 | ipip_del_tunnel (u32 sw_if_index) |
| 829 | { |
| 830 | ipip_main_t *gm = &ipip_main; |
| 831 | vnet_main_t *vnm = gm->vnet_main; |
| 832 | ipip_tunnel_t *t; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 833 | ipip_tunnel_key_t key; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 834 | |
| 835 | t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
| 836 | if (t == NULL) |
| 837 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 838 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 839 | if (t->mode == IPIP_MODE_P2MP) |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 840 | teib_walk_itf (t->sw_if_index, ipip_tunnel_delete_teib_walk, t); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 841 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 842 | vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ ); |
| 843 | gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0; |
| 844 | vnet_delete_hw_interface (vnm, t->hw_if_index); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 845 | hash_unset (gm->instance_used, t->user_instance); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 846 | |
| 847 | ipip_mk_key (t, &key); |
| 848 | ipip_tunnel_db_remove (t, &key); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 849 | pool_put (gm->tunnels, t); |
| 850 | |
| 851 | return 0; |
| 852 | } |
| 853 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 854 | const static teib_vft_t ipip_teib_vft = { |
| 855 | .nv_added = ipip_teib_entry_added, |
| 856 | .nv_deleted = ipip_teib_entry_deleted, |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 857 | }; |
| 858 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 859 | static clib_error_t * |
| 860 | ipip_init (vlib_main_t * vm) |
| 861 | { |
| 862 | ipip_main_t *gm = &ipip_main; |
| 863 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 864 | clib_memset (gm, 0, sizeof (gm[0])); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 865 | gm->vlib_main = vm; |
| 866 | gm->vnet_main = vnet_get_main (); |
| 867 | gm->tunnel_by_key = |
| 868 | hash_create_mem (0, sizeof (ipip_tunnel_key_t), sizeof (uword)); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 869 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 870 | teib_register (&ipip_teib_vft); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 871 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | VLIB_INIT_FUNCTION (ipip_init); |
| 876 | |
| 877 | /* |
| 878 | * fd.io coding-style-patch-verification: ON |
| 879 | * |
| 880 | * Local Variables: |
| 881 | * eval: (c-set-style "gnu") |
| 882 | * End: |
| 883 | */ |