blob: 047a2df811ed119b49c3d5506aea945d2492d21a [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 Ranns14053c92019-12-29 23:55:18 +000087 ip4->checksum = ip4_header_checksum (ip4);
Neale Rannse5b94dd2019-12-31 05:13:14 +000088
89 switch (link_type)
90 {
91 case VNET_LINK_IP6:
92 ip4->protocol = IP_PROTOCOL_IPV6;
93 break;
94 case VNET_LINK_IP4:
95 ip4->protocol = IP_PROTOCOL_IP_IN_IP;
96 break;
97 default:
98 break;
99 }
Ole Troan298c6952018-03-08 12:30:43 +0100100 break;
101
102 case IPIP_TRANSPORT_IP6:
103 vec_validate (rewrite, sizeof (*ip6) - 1);
104 ip6 = (ip6_header_t *) rewrite;
105 ip6->ip_version_traffic_class_and_flow_label =
106 clib_host_to_net_u32 (6 << 28);
107 ip6->hop_limit = 64;
108 /* fixup ip6 header length and protocol after-the-fact */
109 ip6->src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0];
110 ip6->src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1];
Neale Ranns14053c92019-12-29 23:55:18 +0000111 ip6->dst_address.as_u64[0] = dst->ip6.as_u64[0];
112 ip6->dst_address.as_u64[1] = dst->ip6.as_u64[1];
Neale Ranns59ff9182019-12-29 23:55:18 +0000113 if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP))
Neale Ranns95346962019-11-25 13:04:44 +0000114 ip6_set_dscp_network_order (ip6, t->dscp);
Neale Rannse5b94dd2019-12-31 05:13:14 +0000115
116 switch (link_type)
117 {
118 case VNET_LINK_IP6:
119 ip6->protocol = IP_PROTOCOL_IPV6;
120 break;
121 case VNET_LINK_IP4:
122 ip6->protocol = IP_PROTOCOL_IP_IN_IP;
123 break;
124 default:
125 break;
126 }
Ole Troan298c6952018-03-08 12:30:43 +0100127 break;
Ole Troan298c6952018-03-08 12:30:43 +0100128 }
129 return (rewrite);
130}
131
132static void
Neale Rannse5b94dd2019-12-31 05:13:14 +0000133ipip64_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b,
134 const void *data)
Ole Troan298c6952018-03-08 12:30:43 +0100135{
Neale Ranns14053c92019-12-29 23:55:18 +0000136 tunnel_encap_decap_flags_t flags;
Ole Troan298c6952018-03-08 12:30:43 +0100137 ip4_header_t *ip4;
Neale Ranns14053c92019-12-29 23:55:18 +0000138
139 flags = pointer_to_uword (data);
Ole Troan298c6952018-03-08 12:30:43 +0100140
141 ip4 = vlib_buffer_get_current (b);
142 ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b));
Neale Rannse5b94dd2019-12-31 05:13:14 +0000143 tunnel_encap_fixup_6o4 (flags, ((ip6_header_t *) (ip4 + 1)), ip4);
Ole Troand57f6362018-05-24 13:21:43 +0200144
Ole Troan298c6952018-03-08 12:30:43 +0100145 ip4->checksum = ip4_header_checksum (ip4);
146}
147
148static void
Neale Rannse5b94dd2019-12-31 05:13:14 +0000149ipip44_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b,
150 const void *data)
151{
152 tunnel_encap_decap_flags_t flags;
153 ip4_header_t *ip4;
154
155 flags = pointer_to_uword (data);
156
157 ip4 = vlib_buffer_get_current (b);
158 ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b));
159 tunnel_encap_fixup_4o4 (flags, ip4 + 1, ip4);
160
161 ip4->checksum = ip4_header_checksum (ip4);
162}
163
164static void
165ipip46_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b,
166 const void *data)
Ole Troan298c6952018-03-08 12:30:43 +0100167{
Neale Ranns14053c92019-12-29 23:55:18 +0000168 tunnel_encap_decap_flags_t flags;
Ole Troan298c6952018-03-08 12:30:43 +0100169 ip6_header_t *ip6;
Neale Ranns14053c92019-12-29 23:55:18 +0000170
171 flags = pointer_to_uword (data);
Ole Troan298c6952018-03-08 12:30:43 +0100172
Ole Troan282093f2018-09-19 12:38:51 +0200173 /* Must set locally originated otherwise we're not allowed to
174 fragment the packet later */
175 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
176
Ole Troan298c6952018-03-08 12:30:43 +0100177 ip6 = vlib_buffer_get_current (b);
178 ip6->payload_length =
179 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) -
180 sizeof (*ip6));
Neale Rannse5b94dd2019-12-31 05:13:14 +0000181 tunnel_encap_fixup_4o6 (flags, ((ip4_header_t *) (ip6 + 1)), ip6);
182}
Ole Troand57f6362018-05-24 13:21:43 +0200183
Neale Rannse5b94dd2019-12-31 05:13:14 +0000184static void
185ipip66_fixup (vlib_main_t * vm,
186 const ip_adjacency_t * adj, vlib_buffer_t * b, const void *data)
187{
188 tunnel_encap_decap_flags_t flags;
189 ip6_header_t *ip6;
Ole Troand57f6362018-05-24 13:21:43 +0200190
Neale Rannse5b94dd2019-12-31 05:13:14 +0000191 flags = pointer_to_uword (data);
192
193 /* Must set locally originated otherwise we're not allowed to
194 fragment the packet later */
195 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
196
197 ip6 = vlib_buffer_get_current (b);
198 ip6->payload_length =
199 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) -
200 sizeof (*ip6));
201 tunnel_encap_fixup_6o6 (flags, ip6 + 1, ip6);
Ole Troan298c6952018-03-08 12:30:43 +0100202}
203
204static void
205ipip_tunnel_stack (adj_index_t ai)
206{
207 ip_adjacency_t *adj;
208 ipip_tunnel_t *t;
209 u32 sw_if_index;
210
211 adj = adj_get (ai);
212 sw_if_index = adj->rewrite_header.sw_if_index;
213
214 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
215 if (!t)
216 return;
217
218 if ((vnet_hw_interface_get_flags (vnet_get_main (), t->hw_if_index) &
219 VNET_HW_INTERFACE_FLAG_LINK_UP) == 0)
220 {
Neale Ranns4c3ba812019-03-26 07:02:58 +0000221 adj_midchain_delegate_unstack (ai);
Ole Troan298c6952018-03-08 12:30:43 +0100222 }
Neale Ranns521a8d72018-12-06 13:46:49 +0000223 else
Ole Troan298c6952018-03-08 12:30:43 +0100224 {
Neale Ranns4c3ba812019-03-26 07:02:58 +0000225 /* *INDENT-OFF* */
226 fib_prefix_t dst = {
227 .fp_len = t->transport == IPIP_TRANSPORT_IP6 ? 128 : 32,
228 .fp_proto = (t->transport == IPIP_TRANSPORT_IP6 ?
229 FIB_PROTOCOL_IP6 :
230 FIB_PROTOCOL_IP4),
231 .fp_addr = t->tunnel_dst
232 };
233 /* *INDENT-ON* */
234
235 adj_midchain_delegate_stack (ai, t->fib_index, &dst);
Ole Troan298c6952018-03-08 12:30:43 +0100236 }
Ole Troan298c6952018-03-08 12:30:43 +0100237}
238
239static adj_walk_rc_t
240ipip_adj_walk_cb (adj_index_t ai, void *ctx)
241{
242 ipip_tunnel_stack (ai);
243
244 return (ADJ_WALK_RC_CONTINUE);
245}
246
247static void
248ipip_tunnel_restack (ipip_tunnel_t * gt)
249{
250 fib_protocol_t proto;
251
252 /*
253 * walk all the adjacencies on th IPIP interface and restack them
254 */
255 FOR_EACH_FIB_IP_PROTOCOL (proto)
256 {
257 adj_nbr_walk (gt->sw_if_index, proto, ipip_adj_walk_cb, NULL);
258 }
259}
260
Neale Rannse5b94dd2019-12-31 05:13:14 +0000261static adj_midchain_fixup_t
262ipip_get_fixup (const ipip_tunnel_t * t, vnet_link_t lt)
263{
264 if (t->transport == IPIP_TRANSPORT_IP6 && lt == VNET_LINK_IP6)
265 return (ipip66_fixup);
266 if (t->transport == IPIP_TRANSPORT_IP6 && lt == VNET_LINK_IP4)
267 return (ipip46_fixup);
268 if (t->transport == IPIP_TRANSPORT_IP4 && lt == VNET_LINK_IP6)
269 return (ipip64_fixup);
270 if (t->transport == IPIP_TRANSPORT_IP4 && lt == VNET_LINK_IP4)
271 return (ipip44_fixup);
272
273 ASSERT (0);
274 return (ipip44_fixup);
275}
276
Ole Troan298c6952018-03-08 12:30:43 +0100277void
278ipip_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
279{
Neale Ranns521a8d72018-12-06 13:46:49 +0000280 ipip_tunnel_t *t;
281 adj_flags_t af;
Ole Troan298c6952018-03-08 12:30:43 +0100282
283 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
284 if (!t)
285 return;
286
Neale Ranns521a8d72018-12-06 13:46:49 +0000287 af = ADJ_FLAG_MIDCHAIN_IP_STACK;
288 if (VNET_LINK_ETHERNET == adj_get_link_type (ai))
289 af |= ADJ_FLAG_MIDCHAIN_NO_COUNT;
Ole Troan298c6952018-03-08 12:30:43 +0100290
Neale Ranns14053c92019-12-29 23:55:18 +0000291 adj_nbr_midchain_update_rewrite
Neale Rannse5b94dd2019-12-31 05:13:14 +0000292 (ai, ipip_get_fixup (t, adj_get_link_type (ai)),
293 uword_to_pointer (t->flags, void *), af,
Neale Ranns14053c92019-12-29 23:55:18 +0000294 ipip_build_rewrite (vnm, sw_if_index,
295 adj_get_link_type (ai), &t->tunnel_dst));
Ole Troan298c6952018-03-08 12:30:43 +0100296 ipip_tunnel_stack (ai);
297}
298
Neale Ranns14053c92019-12-29 23:55:18 +0000299typedef struct mipip_walk_ctx_t_
300{
301 const ipip_tunnel_t *t;
Neale Ranns03ce4622020-02-03 10:55:09 +0000302 const teib_entry_t *ne;
Neale Ranns14053c92019-12-29 23:55:18 +0000303} mipip_walk_ctx_t;
304
305static adj_walk_rc_t
306mipip_mk_complete_walk (adj_index_t ai, void *data)
307{
308 mipip_walk_ctx_t *ctx = data;
Neale Ranns14053c92019-12-29 23:55:18 +0000309
310 adj_nbr_midchain_update_rewrite
Neale Rannse5b94dd2019-12-31 05:13:14 +0000311 (ai, ipip_get_fixup (ctx->t, adj_get_link_type (ai)),
312 uword_to_pointer (ctx->t->flags, void *),
Neale Ranns14053c92019-12-29 23:55:18 +0000313 ADJ_FLAG_MIDCHAIN_IP_STACK, ipip_build_rewrite (vnet_get_main (),
314 ctx->t->sw_if_index,
315 adj_get_link_type (ai),
Neale Ranns03ce4622020-02-03 10:55:09 +0000316 &teib_entry_get_nh
Neale Ranns14053c92019-12-29 23:55:18 +0000317 (ctx->ne)->fp_addr));
318
Neale Ranns03ce4622020-02-03 10:55:09 +0000319 teib_entry_adj_stack (ctx->ne, ai);
Neale Ranns14053c92019-12-29 23:55:18 +0000320
321 return (ADJ_WALK_RC_CONTINUE);
322}
323
324static adj_walk_rc_t
325mipip_mk_incomplete_walk (adj_index_t ai, void *data)
326{
327 ipip_tunnel_t *t = data;
Neale Ranns14053c92019-12-29 23:55:18 +0000328
Neale Rannse5b94dd2019-12-31 05:13:14 +0000329 adj_nbr_midchain_update_rewrite
330 (ai, ipip_get_fixup (t, adj_get_link_type (ai)),
331 NULL, ADJ_FLAG_NONE, NULL);
Neale Ranns14053c92019-12-29 23:55:18 +0000332
333 adj_midchain_delegate_unstack (ai);
334
335 return (ADJ_WALK_RC_CONTINUE);
336}
337
338void
339mipip_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
340{
341 ipip_main_t *gm = &ipip_main;
Neale Ranns14053c92019-12-29 23:55:18 +0000342 ip_adjacency_t *adj;
Neale Ranns03ce4622020-02-03 10:55:09 +0000343 teib_entry_t *ne;
Neale Ranns14053c92019-12-29 23:55:18 +0000344 ipip_tunnel_t *t;
345 u32 ti;
346
347 adj = adj_get (ai);
348 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
349 t = pool_elt_at_index (gm->tunnels, ti);
Neale Ranns14053c92019-12-29 23:55:18 +0000350
Neale Ranns03ce4622020-02-03 10:55:09 +0000351 ne = teib_entry_find (sw_if_index, &adj->sub_type.nbr.next_hop);
Neale Ranns14053c92019-12-29 23:55:18 +0000352
353 if (NULL == ne)
354 {
355 // no NHRP entry to provide the next-hop
356 adj_nbr_midchain_update_rewrite
Neale Rannse5b94dd2019-12-31 05:13:14 +0000357 (ai, ipip_get_fixup (t, adj_get_link_type (ai)),
358 uword_to_pointer (t->flags, void *), ADJ_FLAG_NONE, NULL);
Neale Ranns14053c92019-12-29 23:55:18 +0000359 return;
360 }
361
362 mipip_walk_ctx_t ctx = {
363 .t = t,
364 .ne = ne
365 };
366 adj_nbr_walk_nh (sw_if_index,
367 adj->ia_nh_proto,
368 &adj->sub_type.nbr.next_hop, mipip_mk_complete_walk, &ctx);
369}
370
Ole Troan298c6952018-03-08 12:30:43 +0100371static u8 *
372format_ipip_tunnel_name (u8 * s, va_list * args)
373{
374 u32 dev_instance = va_arg (*args, u32);
375 ipip_main_t *gm = &ipip_main;
376 ipip_tunnel_t *t;
377
378 if (dev_instance >= vec_len (gm->tunnels))
379 return format (s, "<improperly-referenced>");
380
381 t = pool_elt_at_index (gm->tunnels, dev_instance);
382 return format (s, "ipip%d", t->user_instance);
383}
384
385static u8 *
386format_ipip_device (u8 * s, va_list * args)
387{
388 u32 dev_instance = va_arg (*args, u32);
389 CLIB_UNUSED (int verbose) = va_arg (*args, int);
390
391 s = format (s, "IPIP tunnel: id %d\n", dev_instance);
392 return s;
393}
394
395static clib_error_t *
396ipip_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
397{
398 vnet_hw_interface_t *hi;
399 ipip_tunnel_t *t;
400
401 hi = vnet_get_hw_interface (vnm, hw_if_index);
402
403 t = ipip_tunnel_db_find_by_sw_if_index (hi->sw_if_index);
404 if (!t)
405 return 0;
406
407 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
408 vnet_hw_interface_set_flags (vnm, hw_if_index,
409 VNET_HW_INTERFACE_FLAG_LINK_UP);
410 else
411 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
412
413 ipip_tunnel_restack (t);
414
415 return /* no error */ 0;
416}
417
Neale Rannsc87b66c2019-02-07 07:26:12 -0800418static int
419ipip_tunnel_desc (u32 sw_if_index,
420 ip46_address_t * src, ip46_address_t * dst, u8 * is_l2)
421{
422 ipip_tunnel_t *t;
423
424 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
425 if (!t)
426 return -1;
427
428 *src = t->tunnel_src;
429 *dst = t->tunnel_dst;
430 *is_l2 = 0;
431
432 return (0);
433}
434
Ole Troan298c6952018-03-08 12:30:43 +0100435/* *INDENT-OFF* */
436VNET_DEVICE_CLASS(ipip_device_class) = {
437 .name = "IPIP tunnel device",
438 .format_device_name = format_ipip_tunnel_name,
439 .format_device = format_ipip_device,
440 .format_tx_trace = format_ipip_tx_trace,
441 .admin_up_down_function = ipip_interface_admin_up_down,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800442 .ip_tun_desc = ipip_tunnel_desc,
Ole Troan298c6952018-03-08 12:30:43 +0100443#ifdef SOON
444 .clear counter = 0;
445#endif
446};
447
448VNET_HW_INTERFACE_CLASS(ipip_hw_interface_class) = {
449 .name = "IPIP",
450 //.format_header = format_ipip_header_with_length,
451 //.unformat_header = unformat_ipip_header,
452 .build_rewrite = ipip_build_rewrite,
453 .update_adjacency = ipip_update_adj,
454 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
455};
Neale Ranns14053c92019-12-29 23:55:18 +0000456
457VNET_HW_INTERFACE_CLASS(mipip_hw_interface_class) = {
458 .name = "mIPIP",
459 //.format_header = format_ipip_header_with_length,
460 //.unformat_header = unformat_ipip_header,
461 .build_rewrite = ipip_build_rewrite,
462 .update_adjacency = mipip_update_adj,
463 .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA,
464};
Ole Troan298c6952018-03-08 12:30:43 +0100465/* *INDENT-ON* */
466
467ipip_tunnel_t *
Neale Ranns14053c92019-12-29 23:55:18 +0000468ipip_tunnel_db_find (const ipip_tunnel_key_t * key)
Ole Troan298c6952018-03-08 12:30:43 +0100469{
470 ipip_main_t *gm = &ipip_main;
471 uword *p;
472
473 p = hash_get_mem (gm->tunnel_by_key, key);
474 if (!p)
475 return (NULL);
476 return (pool_elt_at_index (gm->tunnels, p[0]));
477}
478
479ipip_tunnel_t *
480ipip_tunnel_db_find_by_sw_if_index (u32 sw_if_index)
481{
482 ipip_main_t *gm = &ipip_main;
Eyal Baricd307742018-07-22 12:45:15 +0300483 if (vec_len (gm->tunnel_index_by_sw_if_index) <= sw_if_index)
Ole Troan298c6952018-03-08 12:30:43 +0100484 return NULL;
485 u32 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
486 if (ti == ~0)
487 return NULL;
488 return pool_elt_at_index (gm->tunnels, ti);
489}
490
491void
Neale Ranns14053c92019-12-29 23:55:18 +0000492ipip_tunnel_db_add (ipip_tunnel_t * t, const ipip_tunnel_key_t * key)
Ole Troan298c6952018-03-08 12:30:43 +0100493{
494 ipip_main_t *gm = &ipip_main;
495
Neale Ranns14053c92019-12-29 23:55:18 +0000496 hash_set_mem_alloc (&gm->tunnel_by_key, key, t->dev_instance);
Ole Troan298c6952018-03-08 12:30:43 +0100497}
498
499void
Neale Ranns14053c92019-12-29 23:55:18 +0000500ipip_tunnel_db_remove (ipip_tunnel_t * t, const ipip_tunnel_key_t * key)
Ole Troan298c6952018-03-08 12:30:43 +0100501{
502 ipip_main_t *gm = &ipip_main;
503
Neale Ranns14053c92019-12-29 23:55:18 +0000504 hash_unset_mem_free (&gm->tunnel_by_key, key);
505}
506
507void
508ipip_mk_key_i (ipip_transport_t transport,
509 ipip_mode_t mode,
510 const ip46_address_t * src,
511 const ip46_address_t * dst,
512 u32 fib_index, ipip_tunnel_key_t * key)
513{
514 key->transport = transport;
515 key->mode = mode;
516 key->src = *src;
517 key->dst = *dst;
518 key->fib_index = fib_index;
519 key->__pad = 0;;
520}
521
522void
523ipip_mk_key (const ipip_tunnel_t * t, ipip_tunnel_key_t * key)
524{
525 ipip_mk_key_i (t->transport, t->mode,
526 &t->tunnel_src, &t->tunnel_dst, t->fib_index, key);
527}
528
529static void
Neale Ranns03ce4622020-02-03 10:55:09 +0000530ipip_teib_mk_key (const ipip_tunnel_t * t,
531 const teib_entry_t * ne, ipip_tunnel_key_t * key)
Neale Ranns14053c92019-12-29 23:55:18 +0000532{
533 const fib_prefix_t *nh;
534
Neale Ranns03ce4622020-02-03 10:55:09 +0000535 nh = teib_entry_get_nh (ne);
Neale Ranns14053c92019-12-29 23:55:18 +0000536
537 /* construct the key using mode P2P so it can be found in the DP */
538 ipip_mk_key_i (t->transport, IPIP_MODE_P2P,
539 &t->tunnel_src, &nh->fp_addr,
Neale Ranns03ce4622020-02-03 10:55:09 +0000540 teib_entry_get_fib_index (ne), key);
Neale Ranns14053c92019-12-29 23:55:18 +0000541}
542
543static void
Neale Ranns03ce4622020-02-03 10:55:09 +0000544ipip_teib_entry_added (const teib_entry_t * ne)
Neale Ranns14053c92019-12-29 23:55:18 +0000545{
546 ipip_main_t *gm = &ipip_main;
547 const ip46_address_t *nh;
548 ipip_tunnel_key_t key;
549 ipip_tunnel_t *t;
550 u32 sw_if_index;
551 u32 t_idx;
552
Neale Ranns03ce4622020-02-03 10:55:09 +0000553 sw_if_index = teib_entry_get_sw_if_index (ne);
Neale Ranns14053c92019-12-29 23:55:18 +0000554 if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index)
555 return;
556
557 t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index];
558
559 if (INDEX_INVALID == t_idx)
560 return;
561
562 t = pool_elt_at_index (gm->tunnels, t_idx);
563
Neale Ranns03ce4622020-02-03 10:55:09 +0000564 ipip_teib_mk_key (t, ne, &key);
Neale Ranns14053c92019-12-29 23:55:18 +0000565 ipip_tunnel_db_add (t, &key);
566
567 // update the rewrites for each of the adjacencies for this next-hop
568 mipip_walk_ctx_t ctx = {
569 .t = t,
570 .ne = ne
571 };
Neale Ranns03ce4622020-02-03 10:55:09 +0000572 nh = teib_entry_get_peer (ne);
573 adj_nbr_walk_nh (teib_entry_get_sw_if_index (ne),
Neale Ranns14053c92019-12-29 23:55:18 +0000574 (ip46_address_is_ip4 (nh) ?
575 FIB_PROTOCOL_IP4 :
576 FIB_PROTOCOL_IP6), nh, mipip_mk_complete_walk, &ctx);
577}
578
579static void
Neale Ranns03ce4622020-02-03 10:55:09 +0000580ipip_teib_entry_deleted (const teib_entry_t * ne)
Neale Ranns14053c92019-12-29 23:55:18 +0000581{
582 ipip_main_t *gm = &ipip_main;
583 const ip46_address_t *nh;
584 ipip_tunnel_key_t key;
585 ipip_tunnel_t *t;
586 u32 sw_if_index;
587 u32 t_idx;
588
Neale Ranns03ce4622020-02-03 10:55:09 +0000589 sw_if_index = teib_entry_get_sw_if_index (ne);
Neale Ranns14053c92019-12-29 23:55:18 +0000590 if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index)
591 return;
592
593 t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index];
594
595 if (INDEX_INVALID == t_idx)
596 return;
597
598 t = pool_elt_at_index (gm->tunnels, t_idx);
599
Neale Ranns03ce4622020-02-03 10:55:09 +0000600 ipip_teib_mk_key (t, ne, &key);
Neale Ranns14053c92019-12-29 23:55:18 +0000601 ipip_tunnel_db_remove (t, &key);
602
Neale Ranns03ce4622020-02-03 10:55:09 +0000603 nh = teib_entry_get_peer (ne);
Neale Ranns14053c92019-12-29 23:55:18 +0000604
605 /* make all the adjacencies incomplete */
Neale Ranns03ce4622020-02-03 10:55:09 +0000606 adj_nbr_walk_nh (teib_entry_get_sw_if_index (ne),
Neale Ranns14053c92019-12-29 23:55:18 +0000607 (ip46_address_is_ip4 (nh) ?
608 FIB_PROTOCOL_IP4 :
609 FIB_PROTOCOL_IP6), nh, mipip_mk_incomplete_walk, t);
610}
611
612static walk_rc_t
Neale Ranns03ce4622020-02-03 10:55:09 +0000613ipip_tunnel_delete_teib_walk (index_t nei, void *ctx)
Neale Ranns14053c92019-12-29 23:55:18 +0000614{
615 ipip_tunnel_t *t = ctx;
616 ipip_tunnel_key_t key;
617
Neale Ranns03ce4622020-02-03 10:55:09 +0000618 ipip_teib_mk_key (t, teib_entry_get (nei), &key);
Neale Ranns14053c92019-12-29 23:55:18 +0000619 ipip_tunnel_db_remove (t, &key);
620
621 return (WALK_CONTINUE);
622}
623
624static walk_rc_t
Neale Ranns03ce4622020-02-03 10:55:09 +0000625ipip_tunnel_add_teib_walk (index_t nei, void *ctx)
Neale Ranns14053c92019-12-29 23:55:18 +0000626{
627 ipip_tunnel_t *t = ctx;
628 ipip_tunnel_key_t key;
629
Neale Ranns03ce4622020-02-03 10:55:09 +0000630 ipip_teib_mk_key (t, teib_entry_get (nei), &key);
Neale Ranns14053c92019-12-29 23:55:18 +0000631 ipip_tunnel_db_add (t, &key);
632
633 return (WALK_CONTINUE);
Ole Troan298c6952018-03-08 12:30:43 +0100634}
635
Ole Troan298c6952018-03-08 12:30:43 +0100636int
637ipip_add_tunnel (ipip_transport_t transport,
638 u32 instance, ip46_address_t * src, ip46_address_t * dst,
Neale Ranns59ff9182019-12-29 23:55:18 +0000639 u32 fib_index, tunnel_encap_decap_flags_t flags,
Neale Ranns14053c92019-12-29 23:55:18 +0000640 ip_dscp_t dscp, tunnel_mode_t tmode, u32 * sw_if_indexp)
Ole Troan298c6952018-03-08 12:30:43 +0100641{
642 ipip_main_t *gm = &ipip_main;
643 vnet_main_t *vnm = gm->vnet_main;
644 ip4_main_t *im4 = &ip4_main;
645 ip6_main_t *im6 = &ip6_main;
646 ipip_tunnel_t *t;
647 vnet_hw_interface_t *hi;
648 u32 hw_if_index, sw_if_index;
Neale Ranns14053c92019-12-29 23:55:18 +0000649 ipip_tunnel_key_t key;
650 ipip_mode_t mode;
651
652 if (tmode == TUNNEL_MODE_MP && !ip46_address_is_zero (dst))
653 return (VNET_API_ERROR_INVALID_DST_ADDRESS);
654
655 mode = (tmode == TUNNEL_MODE_P2P ? IPIP_MODE_P2P : IPIP_MODE_P2MP);
656 ipip_mk_key_i (transport, mode, src, dst, fib_index, &key);
657
Ole Troan298c6952018-03-08 12:30:43 +0100658 t = ipip_tunnel_db_find (&key);
659 if (t)
Filip Tehlar4c6b1b62019-11-30 20:49:40 +0000660 {
661 if (sw_if_indexp)
662 sw_if_indexp[0] = t->sw_if_index;
663 return VNET_API_ERROR_IF_ALREADY_EXISTS;
664 }
Ole Troan298c6952018-03-08 12:30:43 +0100665
666 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400667 clib_memset (t, 0, sizeof (*t));
Ole Troan298c6952018-03-08 12:30:43 +0100668
669 /* Reconcile the real dev_instance and a possible requested instance */
670 u32 t_idx = t - gm->tunnels; /* tunnel index (or instance) */
671 u32 u_idx = instance; /* user specified instance */
672 if (u_idx == ~0)
673 u_idx = t_idx;
674 if (hash_get (gm->instance_used, u_idx))
675 {
676 pool_put (gm->tunnels, t);
677 return VNET_API_ERROR_INSTANCE_IN_USE;
678 }
679 hash_set (gm->instance_used, u_idx, 1);
680
681 t->dev_instance = t_idx; /* actual */
682 t->user_instance = u_idx; /* name */
Ole Troan298c6952018-03-08 12:30:43 +0100683
684 hw_if_index = vnet_register_interface (vnm, ipip_device_class.index, t_idx,
Neale Ranns14053c92019-12-29 23:55:18 +0000685 (mode == IPIP_MODE_P2P ?
686 ipip_hw_interface_class.index :
687 mipip_hw_interface_class.index),
Ole Troan298c6952018-03-08 12:30:43 +0100688 t_idx);
689
690 hi = vnet_get_hw_interface (vnm, hw_if_index);
691 sw_if_index = hi->sw_if_index;
692
Neale Ranns14053c92019-12-29 23:55:18 +0000693 t->mode = mode;
Ole Troan298c6952018-03-08 12:30:43 +0100694 t->hw_if_index = hw_if_index;
695 t->fib_index = fib_index;
696 t->sw_if_index = sw_if_index;
Neale Ranns95346962019-11-25 13:04:44 +0000697 t->dscp = dscp;
698 t->flags = flags;
Ole Troan298c6952018-03-08 12:30:43 +0100699 t->transport = transport;
Neale Ranns95346962019-11-25 13:04:44 +0000700
Ole Troan298c6952018-03-08 12:30:43 +0100701 vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
702 gm->tunnel_index_by_sw_if_index[sw_if_index] = t_idx;
703
704 if (t->transport == IPIP_TRANSPORT_IP4)
705 {
706 vec_validate (im4->fib_index_by_sw_if_index, sw_if_index);
707 hi->min_packet_bytes = 64 + sizeof (ip4_header_t);
708 }
709 else
710 {
711 vec_validate (im6->fib_index_by_sw_if_index, sw_if_index);
712 hi->min_packet_bytes = 64 + sizeof (ip6_header_t);
713 }
714
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200715 /* Standard default ipip MTU. */
Ole Troand7231612018-06-07 10:17:57 +0200716 vnet_sw_interface_set_mtu (vnm, sw_if_index, 9000);
Ole Troan298c6952018-03-08 12:30:43 +0100717
718 t->tunnel_src = *src;
719 t->tunnel_dst = *dst;
720
721 ipip_tunnel_db_add (t, &key);
722
Neale Ranns14053c92019-12-29 23:55:18 +0000723 if (t->mode == IPIP_MODE_P2MP)
Neale Ranns03ce4622020-02-03 10:55:09 +0000724 teib_walk_itf (t->sw_if_index, ipip_tunnel_add_teib_walk, t);
Neale Ranns14053c92019-12-29 23:55:18 +0000725
Ole Troan298c6952018-03-08 12:30:43 +0100726 if (sw_if_indexp)
727 *sw_if_indexp = sw_if_index;
728
729 if (t->transport == IPIP_TRANSPORT_IP6 && !gm->ip6_protocol_registered)
730 {
731 ip6_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip6_input_node.index);
732 ip6_register_protocol (IP_PROTOCOL_IPV6, ipip6_input_node.index);
733 gm->ip6_protocol_registered = true;
734 }
735 else if (t->transport == IPIP_TRANSPORT_IP4 && !gm->ip4_protocol_registered)
736 {
737 ip4_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip4_input_node.index);
738 ip4_register_protocol (IP_PROTOCOL_IPV6, ipip4_input_node.index);
739 gm->ip4_protocol_registered = true;
740 }
741 return 0;
742}
743
744int
745ipip_del_tunnel (u32 sw_if_index)
746{
747 ipip_main_t *gm = &ipip_main;
748 vnet_main_t *vnm = gm->vnet_main;
749 ipip_tunnel_t *t;
Neale Ranns14053c92019-12-29 23:55:18 +0000750 ipip_tunnel_key_t key;
Ole Troan298c6952018-03-08 12:30:43 +0100751
752 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
753 if (t == NULL)
754 return VNET_API_ERROR_NO_SUCH_ENTRY;
755
Neale Ranns14053c92019-12-29 23:55:18 +0000756 if (t->mode == IPIP_MODE_P2MP)
Neale Ranns03ce4622020-02-03 10:55:09 +0000757 teib_walk_itf (t->sw_if_index, ipip_tunnel_delete_teib_walk, t);
Neale Ranns14053c92019-12-29 23:55:18 +0000758
Ole Troan298c6952018-03-08 12:30:43 +0100759 vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ );
760 gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
761 vnet_delete_hw_interface (vnm, t->hw_if_index);
Ole Troan298c6952018-03-08 12:30:43 +0100762 hash_unset (gm->instance_used, t->user_instance);
Neale Ranns14053c92019-12-29 23:55:18 +0000763
764 ipip_mk_key (t, &key);
765 ipip_tunnel_db_remove (t, &key);
Ole Troan298c6952018-03-08 12:30:43 +0100766 pool_put (gm->tunnels, t);
767
768 return 0;
769}
770
Neale Ranns03ce4622020-02-03 10:55:09 +0000771const static teib_vft_t ipip_teib_vft = {
772 .nv_added = ipip_teib_entry_added,
773 .nv_deleted = ipip_teib_entry_deleted,
Neale Ranns14053c92019-12-29 23:55:18 +0000774};
775
Ole Troan298c6952018-03-08 12:30:43 +0100776static clib_error_t *
777ipip_init (vlib_main_t * vm)
778{
779 ipip_main_t *gm = &ipip_main;
780
Dave Barachb7b92992018-10-17 10:38:51 -0400781 clib_memset (gm, 0, sizeof (gm[0]));
Ole Troan298c6952018-03-08 12:30:43 +0100782 gm->vlib_main = vm;
783 gm->vnet_main = vnet_get_main ();
784 gm->tunnel_by_key =
785 hash_create_mem (0, sizeof (ipip_tunnel_key_t), sizeof (uword));
Ole Troan298c6952018-03-08 12:30:43 +0100786
Neale Ranns03ce4622020-02-03 10:55:09 +0000787 teib_register (&ipip_teib_vft);
Neale Ranns14053c92019-12-29 23:55:18 +0000788
Ole Troan298c6952018-03-08 12:30:43 +0100789 return 0;
790}
791
792VLIB_INIT_FUNCTION (ipip_init);
793
794/*
795 * fd.io coding-style-patch-verification: ON
796 *
797 * Local Variables:
798 * eval: (c-set-style "gnu")
799 * End:
800 */