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> |
| 28 | |
| 29 | ipip_main_t ipip_main; |
| 30 | |
| 31 | /* Packet trace structure */ |
| 32 | typedef struct |
| 33 | { |
| 34 | u32 tunnel_id; |
| 35 | u32 length; |
| 36 | ip46_address_t src; |
| 37 | ip46_address_t dst; |
| 38 | } ipip_tx_trace_t; |
| 39 | |
| 40 | u8 * |
| 41 | format_ipip_tx_trace (u8 * s, va_list * args) |
| 42 | { |
| 43 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 44 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 45 | ipip_tx_trace_t *t = va_arg (*args, ipip_tx_trace_t *); |
| 46 | |
| 47 | s = |
| 48 | format (s, "IPIP: tunnel %d len %d src %U dst %U", t->tunnel_id, |
| 49 | t->length, format_ip46_address, &t->src, IP46_TYPE_ANY, |
| 50 | format_ip46_address, &t->dst, IP46_TYPE_ANY); |
| 51 | return s; |
| 52 | } |
| 53 | |
| 54 | static u8 * |
| 55 | ipip_build_rewrite (vnet_main_t * vnm, u32 sw_if_index, |
| 56 | vnet_link_t link_type, const void *dst_address) |
| 57 | { |
| 58 | ip4_header_t *ip4; |
| 59 | ip6_header_t *ip6; |
| 60 | u8 *rewrite = NULL; |
| 61 | ipip_tunnel_t *t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
| 62 | |
| 63 | if (!t) |
| 64 | /* not one of ours */ |
| 65 | return (0); |
| 66 | |
| 67 | switch (t->transport) |
| 68 | { |
| 69 | case IPIP_TRANSPORT_IP4: |
| 70 | vec_validate (rewrite, sizeof (*ip4) - 1); |
| 71 | ip4 = (ip4_header_t *) rewrite; |
| 72 | ip4->ip_version_and_header_length = 0x45; |
| 73 | ip4->ttl = 64; |
| 74 | /* fixup ip4 header length, protocol and checksum after-the-fact */ |
| 75 | ip4->src_address.as_u32 = t->tunnel_src.ip4.as_u32; |
| 76 | ip4->dst_address.as_u32 = t->tunnel_dst.ip4.as_u32; |
| 77 | ip4->checksum = ip4_header_checksum (ip4); |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame^] | 78 | if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)) |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 79 | ip4_header_set_dscp (ip4, t->dscp); |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame^] | 80 | if (t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_SET_DF) |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 81 | ip4_header_set_df (ip4); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 82 | break; |
| 83 | |
| 84 | case IPIP_TRANSPORT_IP6: |
| 85 | vec_validate (rewrite, sizeof (*ip6) - 1); |
| 86 | ip6 = (ip6_header_t *) rewrite; |
| 87 | ip6->ip_version_traffic_class_and_flow_label = |
| 88 | clib_host_to_net_u32 (6 << 28); |
| 89 | ip6->hop_limit = 64; |
| 90 | /* fixup ip6 header length and protocol after-the-fact */ |
| 91 | ip6->src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0]; |
| 92 | ip6->src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1]; |
| 93 | ip6->dst_address.as_u64[0] = t->tunnel_dst.ip6.as_u64[0]; |
| 94 | ip6->dst_address.as_u64[1] = t->tunnel_dst.ip6.as_u64[1]; |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame^] | 95 | if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)) |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 96 | ip6_set_dscp_network_order (ip6, t->dscp); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 97 | break; |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 98 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 99 | default: |
| 100 | /* pass through */ |
| 101 | ; |
| 102 | } |
| 103 | return (rewrite); |
| 104 | } |
| 105 | |
| 106 | static void |
Neale Ranns | 960eeea | 2019-12-02 23:28:50 +0000 | [diff] [blame] | 107 | ipip4_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b, |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 108 | const void *data) |
| 109 | { |
| 110 | ip4_header_t *ip4; |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 111 | const ipip_tunnel_t *t = data; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 112 | |
| 113 | ip4 = vlib_buffer_get_current (b); |
| 114 | ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b)); |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 115 | switch (adj->ia_link) |
| 116 | { |
| 117 | case VNET_LINK_IP6: |
| 118 | ip4->protocol = IP_PROTOCOL_IPV6; |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame^] | 119 | if (t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP) |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 120 | ip4_header_set_dscp (ip4, |
| 121 | ip6_dscp_network_order ((ip6_header_t *) (ip4 + |
| 122 | 1))); |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame^] | 123 | if (t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN) |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 124 | ip4_header_set_ecn (ip4, |
| 125 | ip6_ecn_network_order ((ip6_header_t *) (ip4 + |
| 126 | 1))); |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 127 | break; |
| 128 | |
| 129 | case VNET_LINK_IP4: |
| 130 | ip4->protocol = IP_PROTOCOL_IP_IN_IP; |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame^] | 131 | if (t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP) |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 132 | ip4_header_set_dscp (ip4, ip4_header_get_dscp (ip4 + 1)); |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame^] | 133 | if (t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN) |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 134 | ip4_header_set_ecn (ip4, ip4_header_get_ecn (ip4 + 1)); |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame^] | 135 | if ((t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DF) && |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 136 | ip4_header_get_df (ip4 + 1)) |
| 137 | ip4_header_set_df (ip4); |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 138 | break; |
| 139 | |
| 140 | default: |
| 141 | break; |
| 142 | } |
| 143 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 144 | ip4->checksum = ip4_header_checksum (ip4); |
| 145 | } |
| 146 | |
| 147 | static void |
Neale Ranns | 960eeea | 2019-12-02 23:28:50 +0000 | [diff] [blame] | 148 | ipip6_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b, |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 149 | const void *data) |
| 150 | { |
| 151 | ip6_header_t *ip6; |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 152 | const ipip_tunnel_t *t = data; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 153 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 154 | /* Must set locally originated otherwise we're not allowed to |
| 155 | fragment the packet later */ |
| 156 | b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
| 157 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 158 | ip6 = vlib_buffer_get_current (b); |
| 159 | ip6->payload_length = |
| 160 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) - |
| 161 | sizeof (*ip6)); |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 162 | switch (adj->ia_link) |
| 163 | { |
| 164 | case VNET_LINK_IP6: |
| 165 | ip6->protocol = IP_PROTOCOL_IPV6; |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame^] | 166 | if (t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP) |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 167 | ip6_set_dscp_network_order (ip6, ip6_dscp_network_order (ip6 + 1)); |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame^] | 168 | if (t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN) |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 169 | ip6_set_ecn_network_order (ip6, ip6_ecn_network_order (ip6 + 1)); |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 170 | break; |
| 171 | |
| 172 | case VNET_LINK_IP4: |
| 173 | ip6->protocol = IP_PROTOCOL_IP_IN_IP; |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame^] | 174 | if (t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP) |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 175 | ip6_set_dscp_network_order |
| 176 | (ip6, ip4_header_get_dscp ((ip4_header_t *) (ip6 + 1))); |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame^] | 177 | if (t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN) |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 178 | ip6_set_ecn_network_order |
| 179 | (ip6, ip4_header_get_ecn ((ip4_header_t *) (ip6 + 1))); |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 180 | break; |
| 181 | |
| 182 | default: |
| 183 | break; |
| 184 | } |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static void |
| 188 | ipip_tunnel_stack (adj_index_t ai) |
| 189 | { |
| 190 | ip_adjacency_t *adj; |
| 191 | ipip_tunnel_t *t; |
| 192 | u32 sw_if_index; |
| 193 | |
| 194 | adj = adj_get (ai); |
| 195 | sw_if_index = adj->rewrite_header.sw_if_index; |
| 196 | |
| 197 | t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
| 198 | if (!t) |
| 199 | return; |
| 200 | |
| 201 | if ((vnet_hw_interface_get_flags (vnet_get_main (), t->hw_if_index) & |
| 202 | VNET_HW_INTERFACE_FLAG_LINK_UP) == 0) |
| 203 | { |
Neale Ranns | 4c3ba81 | 2019-03-26 07:02:58 +0000 | [diff] [blame] | 204 | adj_midchain_delegate_unstack (ai); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 205 | } |
Neale Ranns | 521a8d7 | 2018-12-06 13:46:49 +0000 | [diff] [blame] | 206 | else |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 207 | { |
Neale Ranns | 4c3ba81 | 2019-03-26 07:02:58 +0000 | [diff] [blame] | 208 | /* *INDENT-OFF* */ |
| 209 | fib_prefix_t dst = { |
| 210 | .fp_len = t->transport == IPIP_TRANSPORT_IP6 ? 128 : 32, |
| 211 | .fp_proto = (t->transport == IPIP_TRANSPORT_IP6 ? |
| 212 | FIB_PROTOCOL_IP6 : |
| 213 | FIB_PROTOCOL_IP4), |
| 214 | .fp_addr = t->tunnel_dst |
| 215 | }; |
| 216 | /* *INDENT-ON* */ |
| 217 | |
| 218 | adj_midchain_delegate_stack (ai, t->fib_index, &dst); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 219 | } |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static adj_walk_rc_t |
| 223 | ipip_adj_walk_cb (adj_index_t ai, void *ctx) |
| 224 | { |
| 225 | ipip_tunnel_stack (ai); |
| 226 | |
| 227 | return (ADJ_WALK_RC_CONTINUE); |
| 228 | } |
| 229 | |
| 230 | static void |
| 231 | ipip_tunnel_restack (ipip_tunnel_t * gt) |
| 232 | { |
| 233 | fib_protocol_t proto; |
| 234 | |
| 235 | /* |
| 236 | * walk all the adjacencies on th IPIP interface and restack them |
| 237 | */ |
| 238 | FOR_EACH_FIB_IP_PROTOCOL (proto) |
| 239 | { |
| 240 | adj_nbr_walk (gt->sw_if_index, proto, ipip_adj_walk_cb, NULL); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | void |
| 245 | ipip_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai) |
| 246 | { |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 247 | adj_midchain_fixup_t f; |
Neale Ranns | 521a8d7 | 2018-12-06 13:46:49 +0000 | [diff] [blame] | 248 | ipip_tunnel_t *t; |
| 249 | adj_flags_t af; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 250 | |
| 251 | t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
| 252 | if (!t) |
| 253 | return; |
| 254 | |
| 255 | f = t->transport == IPIP_TRANSPORT_IP6 ? ipip6_fixup : ipip4_fixup; |
Neale Ranns | 521a8d7 | 2018-12-06 13:46:49 +0000 | [diff] [blame] | 256 | af = ADJ_FLAG_MIDCHAIN_IP_STACK; |
| 257 | if (VNET_LINK_ETHERNET == adj_get_link_type (ai)) |
| 258 | af |= ADJ_FLAG_MIDCHAIN_NO_COUNT; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 259 | |
Neale Ranns | 521a8d7 | 2018-12-06 13:46:49 +0000 | [diff] [blame] | 260 | adj_nbr_midchain_update_rewrite (ai, f, t, af, |
| 261 | ipip_build_rewrite (vnm, |
| 262 | sw_if_index, |
| 263 | adj_get_link_type |
| 264 | (ai), NULL)); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 265 | ipip_tunnel_stack (ai); |
| 266 | } |
| 267 | |
| 268 | static u8 * |
| 269 | format_ipip_tunnel_name (u8 * s, va_list * args) |
| 270 | { |
| 271 | u32 dev_instance = va_arg (*args, u32); |
| 272 | ipip_main_t *gm = &ipip_main; |
| 273 | ipip_tunnel_t *t; |
| 274 | |
| 275 | if (dev_instance >= vec_len (gm->tunnels)) |
| 276 | return format (s, "<improperly-referenced>"); |
| 277 | |
| 278 | t = pool_elt_at_index (gm->tunnels, dev_instance); |
| 279 | return format (s, "ipip%d", t->user_instance); |
| 280 | } |
| 281 | |
| 282 | static u8 * |
| 283 | format_ipip_device (u8 * s, va_list * args) |
| 284 | { |
| 285 | u32 dev_instance = va_arg (*args, u32); |
| 286 | CLIB_UNUSED (int verbose) = va_arg (*args, int); |
| 287 | |
| 288 | s = format (s, "IPIP tunnel: id %d\n", dev_instance); |
| 289 | return s; |
| 290 | } |
| 291 | |
| 292 | static clib_error_t * |
| 293 | ipip_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 294 | { |
| 295 | vnet_hw_interface_t *hi; |
| 296 | ipip_tunnel_t *t; |
| 297 | |
| 298 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 299 | |
| 300 | t = ipip_tunnel_db_find_by_sw_if_index (hi->sw_if_index); |
| 301 | if (!t) |
| 302 | return 0; |
| 303 | |
| 304 | if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) |
| 305 | vnet_hw_interface_set_flags (vnm, hw_if_index, |
| 306 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 307 | else |
| 308 | vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ ); |
| 309 | |
| 310 | ipip_tunnel_restack (t); |
| 311 | |
| 312 | return /* no error */ 0; |
| 313 | } |
| 314 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 315 | static int |
| 316 | ipip_tunnel_desc (u32 sw_if_index, |
| 317 | ip46_address_t * src, ip46_address_t * dst, u8 * is_l2) |
| 318 | { |
| 319 | ipip_tunnel_t *t; |
| 320 | |
| 321 | t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
| 322 | if (!t) |
| 323 | return -1; |
| 324 | |
| 325 | *src = t->tunnel_src; |
| 326 | *dst = t->tunnel_dst; |
| 327 | *is_l2 = 0; |
| 328 | |
| 329 | return (0); |
| 330 | } |
| 331 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 332 | /* *INDENT-OFF* */ |
| 333 | VNET_DEVICE_CLASS(ipip_device_class) = { |
| 334 | .name = "IPIP tunnel device", |
| 335 | .format_device_name = format_ipip_tunnel_name, |
| 336 | .format_device = format_ipip_device, |
| 337 | .format_tx_trace = format_ipip_tx_trace, |
| 338 | .admin_up_down_function = ipip_interface_admin_up_down, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 339 | .ip_tun_desc = ipip_tunnel_desc, |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 340 | #ifdef SOON |
| 341 | .clear counter = 0; |
| 342 | #endif |
| 343 | }; |
| 344 | |
| 345 | VNET_HW_INTERFACE_CLASS(ipip_hw_interface_class) = { |
| 346 | .name = "IPIP", |
| 347 | //.format_header = format_ipip_header_with_length, |
| 348 | //.unformat_header = unformat_ipip_header, |
| 349 | .build_rewrite = ipip_build_rewrite, |
| 350 | .update_adjacency = ipip_update_adj, |
| 351 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
| 352 | }; |
| 353 | /* *INDENT-ON* */ |
| 354 | |
| 355 | ipip_tunnel_t * |
| 356 | ipip_tunnel_db_find (ipip_tunnel_key_t * key) |
| 357 | { |
| 358 | ipip_main_t *gm = &ipip_main; |
| 359 | uword *p; |
| 360 | |
| 361 | p = hash_get_mem (gm->tunnel_by_key, key); |
| 362 | if (!p) |
| 363 | return (NULL); |
| 364 | return (pool_elt_at_index (gm->tunnels, p[0])); |
| 365 | } |
| 366 | |
| 367 | ipip_tunnel_t * |
| 368 | ipip_tunnel_db_find_by_sw_if_index (u32 sw_if_index) |
| 369 | { |
| 370 | ipip_main_t *gm = &ipip_main; |
Eyal Bari | cd30774 | 2018-07-22 12:45:15 +0300 | [diff] [blame] | 371 | 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] | 372 | return NULL; |
| 373 | u32 ti = gm->tunnel_index_by_sw_if_index[sw_if_index]; |
| 374 | if (ti == ~0) |
| 375 | return NULL; |
| 376 | return pool_elt_at_index (gm->tunnels, ti); |
| 377 | } |
| 378 | |
| 379 | void |
| 380 | ipip_tunnel_db_add (ipip_tunnel_t * t, ipip_tunnel_key_t * key) |
| 381 | { |
| 382 | ipip_main_t *gm = &ipip_main; |
| 383 | |
| 384 | t->key = clib_mem_alloc (sizeof (*t->key)); |
| 385 | clib_memcpy (t->key, key, sizeof (*key)); |
| 386 | hash_set_mem (gm->tunnel_by_key, t->key, t->dev_instance); |
| 387 | } |
| 388 | |
| 389 | void |
| 390 | ipip_tunnel_db_remove (ipip_tunnel_t * t) |
| 391 | { |
| 392 | ipip_main_t *gm = &ipip_main; |
| 393 | |
| 394 | hash_unset_mem (gm->tunnel_by_key, t->key); |
| 395 | clib_mem_free (t->key); |
| 396 | t->key = NULL; |
| 397 | } |
| 398 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 399 | int |
| 400 | ipip_add_tunnel (ipip_transport_t transport, |
| 401 | u32 instance, ip46_address_t * src, ip46_address_t * dst, |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame^] | 402 | u32 fib_index, tunnel_encap_decap_flags_t flags, |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 403 | ip_dscp_t dscp, u32 * sw_if_indexp) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 404 | { |
| 405 | ipip_main_t *gm = &ipip_main; |
| 406 | vnet_main_t *vnm = gm->vnet_main; |
| 407 | ip4_main_t *im4 = &ip4_main; |
| 408 | ip6_main_t *im6 = &ip6_main; |
| 409 | ipip_tunnel_t *t; |
| 410 | vnet_hw_interface_t *hi; |
| 411 | u32 hw_if_index, sw_if_index; |
| 412 | ipip_tunnel_key_t key = {.transport = transport, |
| 413 | .fib_index = fib_index, |
| 414 | .src = *src, |
| 415 | .dst = *dst |
| 416 | }; |
| 417 | t = ipip_tunnel_db_find (&key); |
| 418 | if (t) |
Filip Tehlar | 4c6b1b6 | 2019-11-30 20:49:40 +0000 | [diff] [blame] | 419 | { |
| 420 | if (sw_if_indexp) |
| 421 | sw_if_indexp[0] = t->sw_if_index; |
| 422 | return VNET_API_ERROR_IF_ALREADY_EXISTS; |
| 423 | } |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 424 | |
| 425 | pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 426 | clib_memset (t, 0, sizeof (*t)); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 427 | |
| 428 | /* Reconcile the real dev_instance and a possible requested instance */ |
| 429 | u32 t_idx = t - gm->tunnels; /* tunnel index (or instance) */ |
| 430 | u32 u_idx = instance; /* user specified instance */ |
| 431 | if (u_idx == ~0) |
| 432 | u_idx = t_idx; |
| 433 | if (hash_get (gm->instance_used, u_idx)) |
| 434 | { |
| 435 | pool_put (gm->tunnels, t); |
| 436 | return VNET_API_ERROR_INSTANCE_IN_USE; |
| 437 | } |
| 438 | hash_set (gm->instance_used, u_idx, 1); |
| 439 | |
| 440 | t->dev_instance = t_idx; /* actual */ |
| 441 | t->user_instance = u_idx; /* name */ |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 442 | |
| 443 | hw_if_index = vnet_register_interface (vnm, ipip_device_class.index, t_idx, |
| 444 | ipip_hw_interface_class.index, |
| 445 | t_idx); |
| 446 | |
| 447 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 448 | sw_if_index = hi->sw_if_index; |
| 449 | |
| 450 | t->hw_if_index = hw_if_index; |
| 451 | t->fib_index = fib_index; |
| 452 | t->sw_if_index = sw_if_index; |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 453 | t->dscp = dscp; |
| 454 | t->flags = flags; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 455 | t->transport = transport; |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 456 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 457 | vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0); |
| 458 | gm->tunnel_index_by_sw_if_index[sw_if_index] = t_idx; |
| 459 | |
| 460 | if (t->transport == IPIP_TRANSPORT_IP4) |
| 461 | { |
| 462 | vec_validate (im4->fib_index_by_sw_if_index, sw_if_index); |
| 463 | hi->min_packet_bytes = 64 + sizeof (ip4_header_t); |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | vec_validate (im6->fib_index_by_sw_if_index, sw_if_index); |
| 468 | hi->min_packet_bytes = 64 + sizeof (ip6_header_t); |
| 469 | } |
| 470 | |
Damjan Marion | fe7d4a2 | 2018-04-13 19:43:39 +0200 | [diff] [blame] | 471 | /* Standard default ipip MTU. */ |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 472 | vnet_sw_interface_set_mtu (vnm, sw_if_index, 9000); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 473 | |
| 474 | t->tunnel_src = *src; |
| 475 | t->tunnel_dst = *dst; |
| 476 | |
| 477 | ipip_tunnel_db_add (t, &key); |
| 478 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 479 | if (sw_if_indexp) |
| 480 | *sw_if_indexp = sw_if_index; |
| 481 | |
| 482 | if (t->transport == IPIP_TRANSPORT_IP6 && !gm->ip6_protocol_registered) |
| 483 | { |
| 484 | ip6_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip6_input_node.index); |
| 485 | ip6_register_protocol (IP_PROTOCOL_IPV6, ipip6_input_node.index); |
| 486 | gm->ip6_protocol_registered = true; |
| 487 | } |
| 488 | else if (t->transport == IPIP_TRANSPORT_IP4 && !gm->ip4_protocol_registered) |
| 489 | { |
| 490 | ip4_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip4_input_node.index); |
| 491 | ip4_register_protocol (IP_PROTOCOL_IPV6, ipip4_input_node.index); |
| 492 | gm->ip4_protocol_registered = true; |
| 493 | } |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | int |
| 498 | ipip_del_tunnel (u32 sw_if_index) |
| 499 | { |
| 500 | ipip_main_t *gm = &ipip_main; |
| 501 | vnet_main_t *vnm = gm->vnet_main; |
| 502 | ipip_tunnel_t *t; |
| 503 | |
| 504 | |
| 505 | t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
| 506 | if (t == NULL) |
| 507 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 508 | |
| 509 | vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ ); |
| 510 | gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0; |
| 511 | vnet_delete_hw_interface (vnm, t->hw_if_index); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 512 | hash_unset (gm->instance_used, t->user_instance); |
| 513 | ipip_tunnel_db_remove (t); |
| 514 | pool_put (gm->tunnels, t); |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | static clib_error_t * |
| 520 | ipip_init (vlib_main_t * vm) |
| 521 | { |
| 522 | ipip_main_t *gm = &ipip_main; |
| 523 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 524 | clib_memset (gm, 0, sizeof (gm[0])); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 525 | gm->vlib_main = vm; |
| 526 | gm->vnet_main = vnet_get_main (); |
| 527 | gm->tunnel_by_key = |
| 528 | hash_create_mem (0, sizeof (ipip_tunnel_key_t), sizeof (uword)); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | VLIB_INIT_FUNCTION (ipip_init); |
| 534 | |
| 535 | /* |
| 536 | * fd.io coding-style-patch-verification: ON |
| 537 | * |
| 538 | * Local Variables: |
| 539 | * eval: (c-set-style "gnu") |
| 540 | * End: |
| 541 | */ |