blob: 600f54211256150a0fedce7606cbba6d1e6ca8c7 [file] [log] [blame]
Ole Troan298c6952018-03-08 12:30:43 +01001/*
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 Ranns4c3ba812019-03-26 07:02:58 +000023#include <vnet/adj/adj_midchain.h>
Ole Troan298c6952018-03-08 12:30:43 +010024#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 Ranns03ce4622020-02-03 10:55:09 +000028#include <vnet/teib/teib.h>
Neale Rannse5b94dd2019-12-31 05:13:14 +000029#include <vnet/tunnel/tunnel_dp.h>
Ole Troan298c6952018-03-08 12:30:43 +010030
31ipip_main_t ipip_main;
32
33/* Packet trace structure */
34typedef 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
42u8 *
43format_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
56static u8 *
57ipip_build_rewrite (vnet_main_t * vnm, u32 sw_if_index,
58 vnet_link_t link_type, const void *dst_address)
59{
Neale Ranns14053c92019-12-29 23:55:18 +000060 const ip46_address_t *dst;
Ole Troan298c6952018-03-08 12:30:43 +010061 ip4_header_t *ip4;
62 ip6_header_t *ip6;
63 u8 *rewrite = NULL;
Neale Ranns14053c92019-12-29 23:55:18 +000064 ipip_tunnel_t *t;
65
66 dst = dst_address;
67 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
Ole Troan298c6952018-03-08 12:30:43 +010068
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 Ranns14053c92019-12-29 23:55:18 +000082 ip4->dst_address.as_u32 = dst->ip4.as_u32;
Neale Ranns59ff9182019-12-29 23:55:18 +000083 if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP))
Neale Ranns95346962019-11-25 13:04:44 +000084 ip4_header_set_dscp (ip4, t->dscp);
Neale Ranns59ff9182019-12-29 23:55:18 +000085 if (t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_SET_DF)
Neale Ranns95346962019-11-25 13:04:44 +000086 ip4_header_set_df (ip4);
Neale Rannse5b94dd2019-12-31 05:13:14 +000087
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 Rannse294de62020-12-21 09:52:24 +000096 case VNET_LINK_MPLS:
97 ip4->protocol = IP_PROTOCOL_MPLS_IN_IP;
98 break;
Neale Rannse5b94dd2019-12-31 05:13:14 +000099 default:
100 break;
101 }
Neale Ranns4ec36c52020-03-31 09:21:29 -0400102 ip4->checksum = ip4_header_checksum (ip4);
Ole Troan298c6952018-03-08 12:30:43 +0100103 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 Ranns14053c92019-12-29 23:55:18 +0000114 ip6->dst_address.as_u64[0] = dst->ip6.as_u64[0];
115 ip6->dst_address.as_u64[1] = dst->ip6.as_u64[1];
Neale Ranns59ff9182019-12-29 23:55:18 +0000116 if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP))
Neale Ranns95346962019-11-25 13:04:44 +0000117 ip6_set_dscp_network_order (ip6, t->dscp);
Neale Rannse5b94dd2019-12-31 05:13:14 +0000118
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 Rannse294de62020-12-21 09:52:24 +0000127 case VNET_LINK_MPLS:
128 ip6->protocol = IP_PROTOCOL_MPLS_IN_IP;
129 break;
Neale Rannse5b94dd2019-12-31 05:13:14 +0000130 default:
131 break;
132 }
Ole Troan298c6952018-03-08 12:30:43 +0100133 break;
Ole Troan298c6952018-03-08 12:30:43 +0100134 }
135 return (rewrite);
136}
137
138static void
Neale Rannse5b94dd2019-12-31 05:13:14 +0000139ipip64_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b,
140 const void *data)
Ole Troan298c6952018-03-08 12:30:43 +0100141{
Neale Ranns14053c92019-12-29 23:55:18 +0000142 tunnel_encap_decap_flags_t flags;
Ole Troan298c6952018-03-08 12:30:43 +0100143 ip4_header_t *ip4;
Neale Ranns14053c92019-12-29 23:55:18 +0000144
145 flags = pointer_to_uword (data);
Ole Troan298c6952018-03-08 12:30:43 +0100146
147 ip4 = vlib_buffer_get_current (b);
148 ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b));
Neale Rannse5b94dd2019-12-31 05:13:14 +0000149 tunnel_encap_fixup_6o4 (flags, ((ip6_header_t *) (ip4 + 1)), ip4);
Ole Troand57f6362018-05-24 13:21:43 +0200150
Ole Troan298c6952018-03-08 12:30:43 +0100151 ip4->checksum = ip4_header_checksum (ip4);
152}
153
154static void
Neale Rannse5b94dd2019-12-31 05:13:14 +0000155ipip44_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
170static void
171ipip46_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b,
172 const void *data)
Ole Troan298c6952018-03-08 12:30:43 +0100173{
Neale Ranns14053c92019-12-29 23:55:18 +0000174 tunnel_encap_decap_flags_t flags;
Ole Troan298c6952018-03-08 12:30:43 +0100175 ip6_header_t *ip6;
Neale Ranns14053c92019-12-29 23:55:18 +0000176
177 flags = pointer_to_uword (data);
Ole Troan298c6952018-03-08 12:30:43 +0100178
Ole Troan282093f2018-09-19 12:38:51 +0200179 /* 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 Troan298c6952018-03-08 12:30:43 +0100183 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 Rannsa91cb452021-02-04 11:02:52 +0000187 tunnel_encap_fixup_4o6 (flags, b, ((ip4_header_t *) (ip6 + 1)), ip6);
Neale Rannse5b94dd2019-12-31 05:13:14 +0000188}
Ole Troand57f6362018-05-24 13:21:43 +0200189
Neale Rannse5b94dd2019-12-31 05:13:14 +0000190static void
191ipip66_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 Troand57f6362018-05-24 13:21:43 +0200196
Neale Rannse5b94dd2019-12-31 05:13:14 +0000197 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 Troan298c6952018-03-08 12:30:43 +0100208}
209
210static void
Neale Rannse294de62020-12-21 09:52:24 +0000211ipipm6_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 Rannsa91cb452021-02-04 11:02:52 +0000227 tunnel_encap_fixup_mplso6 (flags, b, (mpls_unicast_header_t *) (ip6 + 1),
228 ip6);
Neale Rannse294de62020-12-21 09:52:24 +0000229}
230
231static void
232ipipm4_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
251static void
Ole Troan298c6952018-03-08 12:30:43 +0100252ipip_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 Ranns4c3ba812019-03-26 07:02:58 +0000268 adj_midchain_delegate_unstack (ai);
Ole Troan298c6952018-03-08 12:30:43 +0100269 }
Neale Ranns521a8d72018-12-06 13:46:49 +0000270 else
Ole Troan298c6952018-03-08 12:30:43 +0100271 {
Neale Ranns4c3ba812019-03-26 07:02:58 +0000272 /* *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 Troan298c6952018-03-08 12:30:43 +0100283 }
Ole Troan298c6952018-03-08 12:30:43 +0100284}
285
286static adj_walk_rc_t
287ipip_adj_walk_cb (adj_index_t ai, void *ctx)
288{
289 ipip_tunnel_stack (ai);
290
291 return (ADJ_WALK_RC_CONTINUE);
292}
293
294static void
295ipip_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 Rannse5b94dd2019-12-31 05:13:14 +0000308static adj_midchain_fixup_t
Neale Ranns4ec36c52020-03-31 09:21:29 -0400309ipip_get_fixup (const ipip_tunnel_t * t, vnet_link_t lt, adj_flags_t * aflags)
Neale Rannse5b94dd2019-12-31 05:13:14 +0000310{
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 Rannse294de62020-12-21 09:52:24 +0000315 if (t->transport == IPIP_TRANSPORT_IP6 && lt == VNET_LINK_MPLS)
316 return (ipipm6_fixup);
Neale Rannse5b94dd2019-12-31 05:13:14 +0000317 if (t->transport == IPIP_TRANSPORT_IP4 && lt == VNET_LINK_IP6)
318 return (ipip64_fixup);
Neale Rannse294de62020-12-21 09:52:24 +0000319 if (t->transport == IPIP_TRANSPORT_IP4 && lt == VNET_LINK_MPLS)
320 return (ipipm4_fixup);
Neale Rannse5b94dd2019-12-31 05:13:14 +0000321 if (t->transport == IPIP_TRANSPORT_IP4 && lt == VNET_LINK_IP4)
Neale Ranns4ec36c52020-03-31 09:21:29 -0400322 {
323 *aflags = *aflags | ADJ_FLAG_MIDCHAIN_FIXUP_IP4O4_HDR;
324 return (ipip44_fixup);
325 }
Neale Rannse5b94dd2019-12-31 05:13:14 +0000326
327 ASSERT (0);
328 return (ipip44_fixup);
329}
330
Ole Troan298c6952018-03-08 12:30:43 +0100331void
332ipip_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
333{
Neale Ranns4ec36c52020-03-31 09:21:29 -0400334 adj_midchain_fixup_t fixup;
Neale Ranns521a8d72018-12-06 13:46:49 +0000335 ipip_tunnel_t *t;
336 adj_flags_t af;
Ole Troan298c6952018-03-08 12:30:43 +0100337
Neale Ranns65d789e2021-02-08 15:24:56 +0000338 af = ADJ_FLAG_NONE;
Ole Troan298c6952018-03-08 12:30:43 +0100339 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
340 if (!t)
341 return;
342
Neale Ranns65d789e2021-02-08 15:24:56 +0000343 /*
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 Hawari33c45f52020-11-30 13:50:24 +0100350
Neale Ranns4ec36c52020-03-31 09:21:29 -0400351 fixup = ipip_get_fixup (t, adj_get_link_type (ai), &af);
Neale Ranns14053c92019-12-29 23:55:18 +0000352 adj_nbr_midchain_update_rewrite
Neale Ranns4ec36c52020-03-31 09:21:29 -0400353 (ai, fixup,
Neale Rannse5b94dd2019-12-31 05:13:14 +0000354 uword_to_pointer (t->flags, void *), af,
Neale Ranns14053c92019-12-29 23:55:18 +0000355 ipip_build_rewrite (vnm, sw_if_index,
356 adj_get_link_type (ai), &t->tunnel_dst));
Ole Troan298c6952018-03-08 12:30:43 +0100357 ipip_tunnel_stack (ai);
358}
359
Neale Ranns14053c92019-12-29 23:55:18 +0000360typedef struct mipip_walk_ctx_t_
361{
362 const ipip_tunnel_t *t;
Neale Ranns03ce4622020-02-03 10:55:09 +0000363 const teib_entry_t *ne;
Neale Ranns14053c92019-12-29 23:55:18 +0000364} mipip_walk_ctx_t;
365
366static adj_walk_rc_t
367mipip_mk_complete_walk (adj_index_t ai, void *data)
368{
Neale Ranns4ec36c52020-03-31 09:21:29 -0400369 adj_midchain_fixup_t fixup;
Neale Ranns14053c92019-12-29 23:55:18 +0000370 mipip_walk_ctx_t *ctx = data;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400371 adj_flags_t af;
372
373 af = ADJ_FLAG_NONE;
374 fixup = ipip_get_fixup (ctx->t, adj_get_link_type (ai), &af);
Neale Ranns14053c92019-12-29 23:55:18 +0000375
Neale Ranns65d789e2021-02-08 15:24:56 +0000376 /*
377 * the user has not requested that the load-balancing be based on
378 * a flow hash of the inner packet. so use the stacking to choose
379 * a path.
380 */
381 if (!(ctx->t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_INNER_HASH))
382 af |= ADJ_FLAG_MIDCHAIN_IP_STACK;
Mohammed Hawari33c45f52020-11-30 13:50:24 +0100383
Neale Ranns14053c92019-12-29 23:55:18 +0000384 adj_nbr_midchain_update_rewrite
Neale Ranns4ec36c52020-03-31 09:21:29 -0400385 (ai, fixup,
Neale Rannse5b94dd2019-12-31 05:13:14 +0000386 uword_to_pointer (ctx->t->flags, void *),
Mohammed Hawari33c45f52020-11-30 13:50:24 +0100387 af, ipip_build_rewrite (vnet_get_main (),
388 ctx->t->sw_if_index,
389 adj_get_link_type (ai),
390 &teib_entry_get_nh (ctx->ne)->fp_addr));
Neale Ranns14053c92019-12-29 23:55:18 +0000391
Neale Ranns03ce4622020-02-03 10:55:09 +0000392 teib_entry_adj_stack (ctx->ne, ai);
Neale Ranns14053c92019-12-29 23:55:18 +0000393
394 return (ADJ_WALK_RC_CONTINUE);
395}
396
397static adj_walk_rc_t
398mipip_mk_incomplete_walk (adj_index_t ai, void *data)
399{
Neale Ranns4ec36c52020-03-31 09:21:29 -0400400 adj_midchain_fixup_t fixup;
Neale Ranns14053c92019-12-29 23:55:18 +0000401 ipip_tunnel_t *t = data;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400402 adj_flags_t af;
Neale Ranns14053c92019-12-29 23:55:18 +0000403
Neale Ranns4ec36c52020-03-31 09:21:29 -0400404 af = ADJ_FLAG_NONE;
405 fixup = ipip_get_fixup (t, adj_get_link_type (ai), &af);
406
407 adj_nbr_midchain_update_rewrite (ai, fixup, NULL, ADJ_FLAG_NONE, NULL);
Neale Ranns14053c92019-12-29 23:55:18 +0000408
409 adj_midchain_delegate_unstack (ai);
410
411 return (ADJ_WALK_RC_CONTINUE);
412}
413
414void
415mipip_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
416{
417 ipip_main_t *gm = &ipip_main;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400418 adj_midchain_fixup_t fixup;
Neale Ranns14053c92019-12-29 23:55:18 +0000419 ip_adjacency_t *adj;
Neale Ranns03ce4622020-02-03 10:55:09 +0000420 teib_entry_t *ne;
Neale Ranns14053c92019-12-29 23:55:18 +0000421 ipip_tunnel_t *t;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400422 adj_flags_t af;
Neale Ranns14053c92019-12-29 23:55:18 +0000423 u32 ti;
424
Neale Ranns4ec36c52020-03-31 09:21:29 -0400425 af = ADJ_FLAG_NONE;
Neale Ranns14053c92019-12-29 23:55:18 +0000426 adj = adj_get (ai);
427 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
428 t = pool_elt_at_index (gm->tunnels, ti);
Neale Ranns14053c92019-12-29 23:55:18 +0000429
Neale Rannse6b83052020-09-17 12:56:47 +0000430 ne = teib_entry_find_46 (sw_if_index,
431 adj->ia_nh_proto, &adj->sub_type.nbr.next_hop);
Neale Ranns14053c92019-12-29 23:55:18 +0000432
433 if (NULL == ne)
434 {
Neale Ranns4ec36c52020-03-31 09:21:29 -0400435 // no TEIB entry to provide the next-hop
436 fixup = ipip_get_fixup (t, adj_get_link_type (ai), &af);
Neale Ranns14053c92019-12-29 23:55:18 +0000437 adj_nbr_midchain_update_rewrite
Neale Ranns4ec36c52020-03-31 09:21:29 -0400438 (ai, fixup, uword_to_pointer (t->flags, void *), ADJ_FLAG_NONE, NULL);
Neale Ranns14053c92019-12-29 23:55:18 +0000439 return;
440 }
441
442 mipip_walk_ctx_t ctx = {
443 .t = t,
444 .ne = ne
445 };
446 adj_nbr_walk_nh (sw_if_index,
447 adj->ia_nh_proto,
448 &adj->sub_type.nbr.next_hop, mipip_mk_complete_walk, &ctx);
449}
450
Ole Troan298c6952018-03-08 12:30:43 +0100451static u8 *
452format_ipip_tunnel_name (u8 * s, va_list * args)
453{
454 u32 dev_instance = va_arg (*args, u32);
455 ipip_main_t *gm = &ipip_main;
456 ipip_tunnel_t *t;
457
458 if (dev_instance >= vec_len (gm->tunnels))
459 return format (s, "<improperly-referenced>");
460
461 t = pool_elt_at_index (gm->tunnels, dev_instance);
462 return format (s, "ipip%d", t->user_instance);
463}
464
465static u8 *
466format_ipip_device (u8 * s, va_list * args)
467{
468 u32 dev_instance = va_arg (*args, u32);
469 CLIB_UNUSED (int verbose) = va_arg (*args, int);
470
471 s = format (s, "IPIP tunnel: id %d\n", dev_instance);
472 return s;
473}
474
475static clib_error_t *
476ipip_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
477{
478 vnet_hw_interface_t *hi;
479 ipip_tunnel_t *t;
480
481 hi = vnet_get_hw_interface (vnm, hw_if_index);
482
483 t = ipip_tunnel_db_find_by_sw_if_index (hi->sw_if_index);
484 if (!t)
485 return 0;
486
487 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
488 vnet_hw_interface_set_flags (vnm, hw_if_index,
489 VNET_HW_INTERFACE_FLAG_LINK_UP);
490 else
491 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
492
493 ipip_tunnel_restack (t);
494
495 return /* no error */ 0;
496}
497
Neale Rannsc87b66c2019-02-07 07:26:12 -0800498static int
499ipip_tunnel_desc (u32 sw_if_index,
500 ip46_address_t * src, ip46_address_t * dst, u8 * is_l2)
501{
502 ipip_tunnel_t *t;
503
504 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
505 if (!t)
506 return -1;
507
508 *src = t->tunnel_src;
509 *dst = t->tunnel_dst;
510 *is_l2 = 0;
511
512 return (0);
513}
514
Ole Troan298c6952018-03-08 12:30:43 +0100515/* *INDENT-OFF* */
516VNET_DEVICE_CLASS(ipip_device_class) = {
517 .name = "IPIP tunnel device",
518 .format_device_name = format_ipip_tunnel_name,
519 .format_device = format_ipip_device,
520 .format_tx_trace = format_ipip_tx_trace,
521 .admin_up_down_function = ipip_interface_admin_up_down,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800522 .ip_tun_desc = ipip_tunnel_desc,
Ole Troan298c6952018-03-08 12:30:43 +0100523#ifdef SOON
524 .clear counter = 0;
525#endif
526};
527
528VNET_HW_INTERFACE_CLASS(ipip_hw_interface_class) = {
529 .name = "IPIP",
530 //.format_header = format_ipip_header_with_length,
531 //.unformat_header = unformat_ipip_header,
532 .build_rewrite = ipip_build_rewrite,
533 .update_adjacency = ipip_update_adj,
534 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
535};
Neale Ranns14053c92019-12-29 23:55:18 +0000536
537VNET_HW_INTERFACE_CLASS(mipip_hw_interface_class) = {
538 .name = "mIPIP",
539 //.format_header = format_ipip_header_with_length,
540 //.unformat_header = unformat_ipip_header,
541 .build_rewrite = ipip_build_rewrite,
542 .update_adjacency = mipip_update_adj,
543 .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA,
544};
Ole Troan298c6952018-03-08 12:30:43 +0100545/* *INDENT-ON* */
546
547ipip_tunnel_t *
Neale Ranns14053c92019-12-29 23:55:18 +0000548ipip_tunnel_db_find (const ipip_tunnel_key_t * key)
Ole Troan298c6952018-03-08 12:30:43 +0100549{
550 ipip_main_t *gm = &ipip_main;
551 uword *p;
552
553 p = hash_get_mem (gm->tunnel_by_key, key);
554 if (!p)
555 return (NULL);
556 return (pool_elt_at_index (gm->tunnels, p[0]));
557}
558
559ipip_tunnel_t *
560ipip_tunnel_db_find_by_sw_if_index (u32 sw_if_index)
561{
562 ipip_main_t *gm = &ipip_main;
Eyal Baricd307742018-07-22 12:45:15 +0300563 if (vec_len (gm->tunnel_index_by_sw_if_index) <= sw_if_index)
Ole Troan298c6952018-03-08 12:30:43 +0100564 return NULL;
565 u32 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
566 if (ti == ~0)
567 return NULL;
568 return pool_elt_at_index (gm->tunnels, ti);
569}
570
571void
Neale Ranns14053c92019-12-29 23:55:18 +0000572ipip_tunnel_db_add (ipip_tunnel_t * t, const ipip_tunnel_key_t * key)
Ole Troan298c6952018-03-08 12:30:43 +0100573{
574 ipip_main_t *gm = &ipip_main;
575
Neale Ranns14053c92019-12-29 23:55:18 +0000576 hash_set_mem_alloc (&gm->tunnel_by_key, key, t->dev_instance);
Ole Troan298c6952018-03-08 12:30:43 +0100577}
578
579void
Neale Ranns14053c92019-12-29 23:55:18 +0000580ipip_tunnel_db_remove (ipip_tunnel_t * t, const ipip_tunnel_key_t * key)
Ole Troan298c6952018-03-08 12:30:43 +0100581{
582 ipip_main_t *gm = &ipip_main;
583
Neale Ranns14053c92019-12-29 23:55:18 +0000584 hash_unset_mem_free (&gm->tunnel_by_key, key);
585}
586
587void
588ipip_mk_key_i (ipip_transport_t transport,
589 ipip_mode_t mode,
590 const ip46_address_t * src,
591 const ip46_address_t * dst,
592 u32 fib_index, ipip_tunnel_key_t * key)
593{
594 key->transport = transport;
595 key->mode = mode;
596 key->src = *src;
597 key->dst = *dst;
598 key->fib_index = fib_index;
599 key->__pad = 0;;
600}
601
602void
603ipip_mk_key (const ipip_tunnel_t * t, ipip_tunnel_key_t * key)
604{
605 ipip_mk_key_i (t->transport, t->mode,
606 &t->tunnel_src, &t->tunnel_dst, t->fib_index, key);
607}
608
609static void
Neale Ranns03ce4622020-02-03 10:55:09 +0000610ipip_teib_mk_key (const ipip_tunnel_t * t,
611 const teib_entry_t * ne, ipip_tunnel_key_t * key)
Neale Ranns14053c92019-12-29 23:55:18 +0000612{
613 const fib_prefix_t *nh;
614
Neale Ranns03ce4622020-02-03 10:55:09 +0000615 nh = teib_entry_get_nh (ne);
Neale Ranns14053c92019-12-29 23:55:18 +0000616
617 /* construct the key using mode P2P so it can be found in the DP */
618 ipip_mk_key_i (t->transport, IPIP_MODE_P2P,
619 &t->tunnel_src, &nh->fp_addr,
Neale Ranns03ce4622020-02-03 10:55:09 +0000620 teib_entry_get_fib_index (ne), key);
Neale Ranns14053c92019-12-29 23:55:18 +0000621}
622
623static void
Neale Ranns03ce4622020-02-03 10:55:09 +0000624ipip_teib_entry_added (const teib_entry_t * ne)
Neale Ranns14053c92019-12-29 23:55:18 +0000625{
626 ipip_main_t *gm = &ipip_main;
Neale Rannse6b83052020-09-17 12:56:47 +0000627 const ip_address_t *nh;
Neale Ranns14053c92019-12-29 23:55:18 +0000628 ipip_tunnel_key_t key;
629 ipip_tunnel_t *t;
630 u32 sw_if_index;
631 u32 t_idx;
632
Neale Ranns03ce4622020-02-03 10:55:09 +0000633 sw_if_index = teib_entry_get_sw_if_index (ne);
Neale Ranns14053c92019-12-29 23:55:18 +0000634 if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index)
635 return;
636
637 t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index];
638
639 if (INDEX_INVALID == t_idx)
640 return;
641
642 t = pool_elt_at_index (gm->tunnels, t_idx);
643
Neale Ranns03ce4622020-02-03 10:55:09 +0000644 ipip_teib_mk_key (t, ne, &key);
Neale Ranns14053c92019-12-29 23:55:18 +0000645 ipip_tunnel_db_add (t, &key);
646
647 // update the rewrites for each of the adjacencies for this next-hop
648 mipip_walk_ctx_t ctx = {
649 .t = t,
650 .ne = ne
651 };
Neale Ranns03ce4622020-02-03 10:55:09 +0000652 nh = teib_entry_get_peer (ne);
653 adj_nbr_walk_nh (teib_entry_get_sw_if_index (ne),
Neale Rannse6b83052020-09-17 12:56:47 +0000654 (AF_IP4 == ip_addr_version (nh) ?
Neale Ranns14053c92019-12-29 23:55:18 +0000655 FIB_PROTOCOL_IP4 :
Neale Rannse6b83052020-09-17 12:56:47 +0000656 FIB_PROTOCOL_IP6),
657 &ip_addr_46 (nh), mipip_mk_complete_walk, &ctx);
Neale Ranns14053c92019-12-29 23:55:18 +0000658}
659
660static void
Neale Ranns03ce4622020-02-03 10:55:09 +0000661ipip_teib_entry_deleted (const teib_entry_t * ne)
Neale Ranns14053c92019-12-29 23:55:18 +0000662{
663 ipip_main_t *gm = &ipip_main;
Neale Rannse6b83052020-09-17 12:56:47 +0000664 const ip_address_t *nh;
Neale Ranns14053c92019-12-29 23:55:18 +0000665 ipip_tunnel_key_t key;
666 ipip_tunnel_t *t;
667 u32 sw_if_index;
668 u32 t_idx;
669
Neale Ranns03ce4622020-02-03 10:55:09 +0000670 sw_if_index = teib_entry_get_sw_if_index (ne);
Neale Ranns14053c92019-12-29 23:55:18 +0000671 if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index)
672 return;
673
674 t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index];
675
676 if (INDEX_INVALID == t_idx)
677 return;
678
679 t = pool_elt_at_index (gm->tunnels, t_idx);
680
Neale Ranns03ce4622020-02-03 10:55:09 +0000681 ipip_teib_mk_key (t, ne, &key);
Neale Ranns14053c92019-12-29 23:55:18 +0000682 ipip_tunnel_db_remove (t, &key);
683
Neale Ranns03ce4622020-02-03 10:55:09 +0000684 nh = teib_entry_get_peer (ne);
Neale Ranns14053c92019-12-29 23:55:18 +0000685
686 /* make all the adjacencies incomplete */
Neale Ranns03ce4622020-02-03 10:55:09 +0000687 adj_nbr_walk_nh (teib_entry_get_sw_if_index (ne),
Neale Rannse6b83052020-09-17 12:56:47 +0000688 (AF_IP4 == ip_addr_version (nh) ?
Neale Ranns14053c92019-12-29 23:55:18 +0000689 FIB_PROTOCOL_IP4 :
Neale Rannse6b83052020-09-17 12:56:47 +0000690 FIB_PROTOCOL_IP6),
691 &ip_addr_46 (nh), mipip_mk_incomplete_walk, t);
Neale Ranns14053c92019-12-29 23:55:18 +0000692}
693
694static walk_rc_t
Neale Ranns03ce4622020-02-03 10:55:09 +0000695ipip_tunnel_delete_teib_walk (index_t nei, void *ctx)
Neale Ranns14053c92019-12-29 23:55:18 +0000696{
697 ipip_tunnel_t *t = ctx;
698 ipip_tunnel_key_t key;
699
Neale Ranns03ce4622020-02-03 10:55:09 +0000700 ipip_teib_mk_key (t, teib_entry_get (nei), &key);
Neale Ranns14053c92019-12-29 23:55:18 +0000701 ipip_tunnel_db_remove (t, &key);
702
703 return (WALK_CONTINUE);
704}
705
706static walk_rc_t
Neale Ranns03ce4622020-02-03 10:55:09 +0000707ipip_tunnel_add_teib_walk (index_t nei, void *ctx)
Neale Ranns14053c92019-12-29 23:55:18 +0000708{
709 ipip_tunnel_t *t = ctx;
710 ipip_tunnel_key_t key;
711
Neale Ranns03ce4622020-02-03 10:55:09 +0000712 ipip_teib_mk_key (t, teib_entry_get (nei), &key);
Neale Ranns14053c92019-12-29 23:55:18 +0000713 ipip_tunnel_db_add (t, &key);
714
715 return (WALK_CONTINUE);
Ole Troan298c6952018-03-08 12:30:43 +0100716}
717
Ole Troan298c6952018-03-08 12:30:43 +0100718int
719ipip_add_tunnel (ipip_transport_t transport,
720 u32 instance, ip46_address_t * src, ip46_address_t * dst,
Neale Ranns59ff9182019-12-29 23:55:18 +0000721 u32 fib_index, tunnel_encap_decap_flags_t flags,
Neale Ranns14053c92019-12-29 23:55:18 +0000722 ip_dscp_t dscp, tunnel_mode_t tmode, u32 * sw_if_indexp)
Ole Troan298c6952018-03-08 12:30:43 +0100723{
724 ipip_main_t *gm = &ipip_main;
725 vnet_main_t *vnm = gm->vnet_main;
Ole Troan298c6952018-03-08 12:30:43 +0100726 ipip_tunnel_t *t;
727 vnet_hw_interface_t *hi;
728 u32 hw_if_index, sw_if_index;
Neale Ranns14053c92019-12-29 23:55:18 +0000729 ipip_tunnel_key_t key;
730 ipip_mode_t mode;
731
732 if (tmode == TUNNEL_MODE_MP && !ip46_address_is_zero (dst))
733 return (VNET_API_ERROR_INVALID_DST_ADDRESS);
734
735 mode = (tmode == TUNNEL_MODE_P2P ? IPIP_MODE_P2P : IPIP_MODE_P2MP);
736 ipip_mk_key_i (transport, mode, src, dst, fib_index, &key);
737
Ole Troan298c6952018-03-08 12:30:43 +0100738 t = ipip_tunnel_db_find (&key);
739 if (t)
Filip Tehlar4c6b1b62019-11-30 20:49:40 +0000740 {
741 if (sw_if_indexp)
742 sw_if_indexp[0] = t->sw_if_index;
743 return VNET_API_ERROR_IF_ALREADY_EXISTS;
744 }
Ole Troan298c6952018-03-08 12:30:43 +0100745
746 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400747 clib_memset (t, 0, sizeof (*t));
Ole Troan298c6952018-03-08 12:30:43 +0100748
749 /* Reconcile the real dev_instance and a possible requested instance */
750 u32 t_idx = t - gm->tunnels; /* tunnel index (or instance) */
751 u32 u_idx = instance; /* user specified instance */
752 if (u_idx == ~0)
753 u_idx = t_idx;
754 if (hash_get (gm->instance_used, u_idx))
755 {
756 pool_put (gm->tunnels, t);
757 return VNET_API_ERROR_INSTANCE_IN_USE;
758 }
759 hash_set (gm->instance_used, u_idx, 1);
760
761 t->dev_instance = t_idx; /* actual */
762 t->user_instance = u_idx; /* name */
Ole Troan298c6952018-03-08 12:30:43 +0100763
764 hw_if_index = vnet_register_interface (vnm, ipip_device_class.index, t_idx,
Neale Ranns14053c92019-12-29 23:55:18 +0000765 (mode == IPIP_MODE_P2P ?
766 ipip_hw_interface_class.index :
767 mipip_hw_interface_class.index),
Ole Troan298c6952018-03-08 12:30:43 +0100768 t_idx);
769
770 hi = vnet_get_hw_interface (vnm, hw_if_index);
771 sw_if_index = hi->sw_if_index;
772
Neale Ranns14053c92019-12-29 23:55:18 +0000773 t->mode = mode;
Ole Troan298c6952018-03-08 12:30:43 +0100774 t->hw_if_index = hw_if_index;
775 t->fib_index = fib_index;
776 t->sw_if_index = sw_if_index;
Neale Ranns95346962019-11-25 13:04:44 +0000777 t->dscp = dscp;
778 t->flags = flags;
Ole Troan298c6952018-03-08 12:30:43 +0100779 t->transport = transport;
Neale Ranns95346962019-11-25 13:04:44 +0000780
Ole Troan298c6952018-03-08 12:30:43 +0100781 vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
782 gm->tunnel_index_by_sw_if_index[sw_if_index] = t_idx;
783
784 if (t->transport == IPIP_TRANSPORT_IP4)
Damjan Marion1cd0e5d2022-01-17 14:49:17 +0100785 hi->frame_overhead = sizeof (ip4_header_t);
Ole Troan298c6952018-03-08 12:30:43 +0100786 else
Damjan Marion1cd0e5d2022-01-17 14:49:17 +0100787 hi->frame_overhead = sizeof (ip6_header_t);
788
789 hi->min_frame_size = hi->frame_overhead + 64;
Ole Troan298c6952018-03-08 12:30:43 +0100790
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200791 /* Standard default ipip MTU. */
Ole Troand7231612018-06-07 10:17:57 +0200792 vnet_sw_interface_set_mtu (vnm, sw_if_index, 9000);
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000793 vnet_set_interface_l3_output_node (gm->vlib_main, sw_if_index,
794 (u8 *) "tunnel-output");
Ole Troan298c6952018-03-08 12:30:43 +0100795
796 t->tunnel_src = *src;
797 t->tunnel_dst = *dst;
798
799 ipip_tunnel_db_add (t, &key);
800
Neale Ranns14053c92019-12-29 23:55:18 +0000801 if (t->mode == IPIP_MODE_P2MP)
Neale Ranns03ce4622020-02-03 10:55:09 +0000802 teib_walk_itf (t->sw_if_index, ipip_tunnel_add_teib_walk, t);
Neale Ranns14053c92019-12-29 23:55:18 +0000803
Ole Troan298c6952018-03-08 12:30:43 +0100804 if (sw_if_indexp)
805 *sw_if_indexp = sw_if_index;
806
807 if (t->transport == IPIP_TRANSPORT_IP6 && !gm->ip6_protocol_registered)
808 {
809 ip6_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip6_input_node.index);
Neale Rannse294de62020-12-21 09:52:24 +0000810 ip6_register_protocol (IP_PROTOCOL_MPLS_IN_IP, ipip6_input_node.index);
Ole Troan298c6952018-03-08 12:30:43 +0100811 ip6_register_protocol (IP_PROTOCOL_IPV6, ipip6_input_node.index);
812 gm->ip6_protocol_registered = true;
813 }
814 else if (t->transport == IPIP_TRANSPORT_IP4 && !gm->ip4_protocol_registered)
815 {
816 ip4_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip4_input_node.index);
Neale Rannse294de62020-12-21 09:52:24 +0000817 ip4_register_protocol (IP_PROTOCOL_MPLS_IN_IP, ipip4_input_node.index);
Ole Troan298c6952018-03-08 12:30:43 +0100818 ip4_register_protocol (IP_PROTOCOL_IPV6, ipip4_input_node.index);
819 gm->ip4_protocol_registered = true;
820 }
821 return 0;
822}
823
824int
825ipip_del_tunnel (u32 sw_if_index)
826{
827 ipip_main_t *gm = &ipip_main;
828 vnet_main_t *vnm = gm->vnet_main;
829 ipip_tunnel_t *t;
Neale Ranns14053c92019-12-29 23:55:18 +0000830 ipip_tunnel_key_t key;
Ole Troan298c6952018-03-08 12:30:43 +0100831
832 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
833 if (t == NULL)
834 return VNET_API_ERROR_NO_SUCH_ENTRY;
835
Neale Ranns14053c92019-12-29 23:55:18 +0000836 if (t->mode == IPIP_MODE_P2MP)
Neale Ranns03ce4622020-02-03 10:55:09 +0000837 teib_walk_itf (t->sw_if_index, ipip_tunnel_delete_teib_walk, t);
Neale Ranns14053c92019-12-29 23:55:18 +0000838
Ole Troan298c6952018-03-08 12:30:43 +0100839 vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ );
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000840 vnet_reset_interface_l3_output_node (gm->vlib_main, t->sw_if_index);
Ole Troan298c6952018-03-08 12:30:43 +0100841 gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
842 vnet_delete_hw_interface (vnm, t->hw_if_index);
Ole Troan298c6952018-03-08 12:30:43 +0100843 hash_unset (gm->instance_used, t->user_instance);
Neale Ranns14053c92019-12-29 23:55:18 +0000844
845 ipip_mk_key (t, &key);
846 ipip_tunnel_db_remove (t, &key);
Ole Troan298c6952018-03-08 12:30:43 +0100847 pool_put (gm->tunnels, t);
848
849 return 0;
850}
851
Neale Ranns03ce4622020-02-03 10:55:09 +0000852const static teib_vft_t ipip_teib_vft = {
853 .nv_added = ipip_teib_entry_added,
854 .nv_deleted = ipip_teib_entry_deleted,
Neale Ranns14053c92019-12-29 23:55:18 +0000855};
856
Ole Troan298c6952018-03-08 12:30:43 +0100857static clib_error_t *
858ipip_init (vlib_main_t * vm)
859{
860 ipip_main_t *gm = &ipip_main;
861
Dave Barachb7b92992018-10-17 10:38:51 -0400862 clib_memset (gm, 0, sizeof (gm[0]));
Ole Troan298c6952018-03-08 12:30:43 +0100863 gm->vlib_main = vm;
864 gm->vnet_main = vnet_get_main ();
865 gm->tunnel_by_key =
866 hash_create_mem (0, sizeof (ipip_tunnel_key_t), sizeof (uword));
Ole Troan298c6952018-03-08 12:30:43 +0100867
Neale Ranns03ce4622020-02-03 10:55:09 +0000868 teib_register (&ipip_teib_vft);
Neale Ranns14053c92019-12-29 23:55:18 +0000869
Ole Troan298c6952018-03-08 12:30:43 +0100870 return 0;
871}
872
873VLIB_INIT_FUNCTION (ipip_init);
874
875/*
876 * fd.io coding-style-patch-verification: ON
877 *
878 * Local Variables:
879 * eval: (c-set-style "gnu")
880 * End:
881 */