blob: aa8d8daca4bca2fb5aa3ba00d7e1f3976f41c807 [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 Ranns14053c92019-12-29 23:55:18 +000028#include <vnet/nhrp/nhrp.h>
Ole Troan298c6952018-03-08 12:30:43 +010029
30ipip_main_t ipip_main;
31
32/* Packet trace structure */
33typedef struct
34{
35 u32 tunnel_id;
36 u32 length;
37 ip46_address_t src;
38 ip46_address_t dst;
39} ipip_tx_trace_t;
40
41u8 *
42format_ipip_tx_trace (u8 * s, va_list * args)
43{
44 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
45 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
46 ipip_tx_trace_t *t = va_arg (*args, ipip_tx_trace_t *);
47
48 s =
49 format (s, "IPIP: tunnel %d len %d src %U dst %U", t->tunnel_id,
50 t->length, format_ip46_address, &t->src, IP46_TYPE_ANY,
51 format_ip46_address, &t->dst, IP46_TYPE_ANY);
52 return s;
53}
54
55static u8 *
56ipip_build_rewrite (vnet_main_t * vnm, u32 sw_if_index,
57 vnet_link_t link_type, const void *dst_address)
58{
Neale Ranns14053c92019-12-29 23:55:18 +000059 const ip46_address_t *dst;
Ole Troan298c6952018-03-08 12:30:43 +010060 ip4_header_t *ip4;
61 ip6_header_t *ip6;
62 u8 *rewrite = NULL;
Neale Ranns14053c92019-12-29 23:55:18 +000063 ipip_tunnel_t *t;
64
65 dst = dst_address;
66 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
Ole Troan298c6952018-03-08 12:30:43 +010067
68 if (!t)
69 /* not one of ours */
70 return (0);
71
72 switch (t->transport)
73 {
74 case IPIP_TRANSPORT_IP4:
75 vec_validate (rewrite, sizeof (*ip4) - 1);
76 ip4 = (ip4_header_t *) rewrite;
77 ip4->ip_version_and_header_length = 0x45;
78 ip4->ttl = 64;
79 /* fixup ip4 header length, protocol and checksum after-the-fact */
80 ip4->src_address.as_u32 = t->tunnel_src.ip4.as_u32;
Neale Ranns14053c92019-12-29 23:55:18 +000081 ip4->dst_address.as_u32 = dst->ip4.as_u32;
Neale Ranns59ff9182019-12-29 23:55:18 +000082 if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP))
Neale Ranns95346962019-11-25 13:04:44 +000083 ip4_header_set_dscp (ip4, t->dscp);
Neale Ranns59ff9182019-12-29 23:55:18 +000084 if (t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_SET_DF)
Neale Ranns95346962019-11-25 13:04:44 +000085 ip4_header_set_df (ip4);
Neale Ranns14053c92019-12-29 23:55:18 +000086 ip4->checksum = ip4_header_checksum (ip4);
Ole Troan298c6952018-03-08 12:30:43 +010087 break;
88
89 case IPIP_TRANSPORT_IP6:
90 vec_validate (rewrite, sizeof (*ip6) - 1);
91 ip6 = (ip6_header_t *) rewrite;
92 ip6->ip_version_traffic_class_and_flow_label =
93 clib_host_to_net_u32 (6 << 28);
94 ip6->hop_limit = 64;
95 /* fixup ip6 header length and protocol after-the-fact */
96 ip6->src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0];
97 ip6->src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1];
Neale Ranns14053c92019-12-29 23:55:18 +000098 ip6->dst_address.as_u64[0] = dst->ip6.as_u64[0];
99 ip6->dst_address.as_u64[1] = dst->ip6.as_u64[1];
Neale Ranns59ff9182019-12-29 23:55:18 +0000100 if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP))
Neale Ranns95346962019-11-25 13:04:44 +0000101 ip6_set_dscp_network_order (ip6, t->dscp);
Ole Troan298c6952018-03-08 12:30:43 +0100102 break;
Ole Troan298c6952018-03-08 12:30:43 +0100103 }
104 return (rewrite);
105}
106
107static void
Neale Ranns960eeea2019-12-02 23:28:50 +0000108ipip4_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b,
Ole Troan298c6952018-03-08 12:30:43 +0100109 const void *data)
110{
Neale Ranns14053c92019-12-29 23:55:18 +0000111 tunnel_encap_decap_flags_t flags;
Ole Troan298c6952018-03-08 12:30:43 +0100112 ip4_header_t *ip4;
Neale Ranns14053c92019-12-29 23:55:18 +0000113
114 flags = pointer_to_uword (data);
Ole Troan298c6952018-03-08 12:30:43 +0100115
116 ip4 = vlib_buffer_get_current (b);
117 ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b));
Ole Troand57f6362018-05-24 13:21:43 +0200118 switch (adj->ia_link)
119 {
120 case VNET_LINK_IP6:
121 ip4->protocol = IP_PROTOCOL_IPV6;
Neale Ranns14053c92019-12-29 23:55:18 +0000122 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
Neale Ranns95346962019-11-25 13:04:44 +0000123 ip4_header_set_dscp (ip4,
124 ip6_dscp_network_order ((ip6_header_t *) (ip4 +
125 1)));
Neale Ranns14053c92019-12-29 23:55:18 +0000126 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)
Neale Ranns95346962019-11-25 13:04:44 +0000127 ip4_header_set_ecn (ip4,
128 ip6_ecn_network_order ((ip6_header_t *) (ip4 +
129 1)));
Ole Troand57f6362018-05-24 13:21:43 +0200130 break;
131
132 case VNET_LINK_IP4:
133 ip4->protocol = IP_PROTOCOL_IP_IN_IP;
Neale Ranns14053c92019-12-29 23:55:18 +0000134 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
Neale Ranns95346962019-11-25 13:04:44 +0000135 ip4_header_set_dscp (ip4, ip4_header_get_dscp (ip4 + 1));
Neale Ranns14053c92019-12-29 23:55:18 +0000136 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)
Neale Ranns95346962019-11-25 13:04:44 +0000137 ip4_header_set_ecn (ip4, ip4_header_get_ecn (ip4 + 1));
Neale Ranns14053c92019-12-29 23:55:18 +0000138 if ((flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DF) &&
Neale Ranns95346962019-11-25 13:04:44 +0000139 ip4_header_get_df (ip4 + 1))
140 ip4_header_set_df (ip4);
Ole Troand57f6362018-05-24 13:21:43 +0200141 break;
142
143 default:
144 break;
145 }
146
Ole Troan298c6952018-03-08 12:30:43 +0100147 ip4->checksum = ip4_header_checksum (ip4);
148}
149
150static void
Neale Ranns960eeea2019-12-02 23:28:50 +0000151ipip6_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b,
Ole Troan298c6952018-03-08 12:30:43 +0100152 const void *data)
153{
Neale Ranns14053c92019-12-29 23:55:18 +0000154 tunnel_encap_decap_flags_t flags;
Ole Troan298c6952018-03-08 12:30:43 +0100155 ip6_header_t *ip6;
Neale Ranns14053c92019-12-29 23:55:18 +0000156
157 flags = pointer_to_uword (data);
Ole Troan298c6952018-03-08 12:30:43 +0100158
Ole Troan282093f2018-09-19 12:38:51 +0200159 /* Must set locally originated otherwise we're not allowed to
160 fragment the packet later */
161 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
162
Ole Troan298c6952018-03-08 12:30:43 +0100163 ip6 = vlib_buffer_get_current (b);
164 ip6->payload_length =
165 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) -
166 sizeof (*ip6));
Ole Troand57f6362018-05-24 13:21:43 +0200167 switch (adj->ia_link)
168 {
169 case VNET_LINK_IP6:
170 ip6->protocol = IP_PROTOCOL_IPV6;
Neale Ranns14053c92019-12-29 23:55:18 +0000171 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
Neale Ranns95346962019-11-25 13:04:44 +0000172 ip6_set_dscp_network_order (ip6, ip6_dscp_network_order (ip6 + 1));
Neale Ranns14053c92019-12-29 23:55:18 +0000173 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)
Neale Ranns95346962019-11-25 13:04:44 +0000174 ip6_set_ecn_network_order (ip6, ip6_ecn_network_order (ip6 + 1));
Ole Troand57f6362018-05-24 13:21:43 +0200175 break;
176
177 case VNET_LINK_IP4:
178 ip6->protocol = IP_PROTOCOL_IP_IN_IP;
Neale Ranns14053c92019-12-29 23:55:18 +0000179 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
Neale Ranns95346962019-11-25 13:04:44 +0000180 ip6_set_dscp_network_order
181 (ip6, ip4_header_get_dscp ((ip4_header_t *) (ip6 + 1)));
Neale Ranns14053c92019-12-29 23:55:18 +0000182 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)
Neale Ranns95346962019-11-25 13:04:44 +0000183 ip6_set_ecn_network_order
184 (ip6, ip4_header_get_ecn ((ip4_header_t *) (ip6 + 1)));
Ole Troand57f6362018-05-24 13:21:43 +0200185 break;
186
187 default:
188 break;
189 }
Ole Troan298c6952018-03-08 12:30:43 +0100190}
191
192static void
193ipip_tunnel_stack (adj_index_t ai)
194{
195 ip_adjacency_t *adj;
196 ipip_tunnel_t *t;
197 u32 sw_if_index;
198
199 adj = adj_get (ai);
200 sw_if_index = adj->rewrite_header.sw_if_index;
201
202 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
203 if (!t)
204 return;
205
206 if ((vnet_hw_interface_get_flags (vnet_get_main (), t->hw_if_index) &
207 VNET_HW_INTERFACE_FLAG_LINK_UP) == 0)
208 {
Neale Ranns4c3ba812019-03-26 07:02:58 +0000209 adj_midchain_delegate_unstack (ai);
Ole Troan298c6952018-03-08 12:30:43 +0100210 }
Neale Ranns521a8d72018-12-06 13:46:49 +0000211 else
Ole Troan298c6952018-03-08 12:30:43 +0100212 {
Neale Ranns4c3ba812019-03-26 07:02:58 +0000213 /* *INDENT-OFF* */
214 fib_prefix_t dst = {
215 .fp_len = t->transport == IPIP_TRANSPORT_IP6 ? 128 : 32,
216 .fp_proto = (t->transport == IPIP_TRANSPORT_IP6 ?
217 FIB_PROTOCOL_IP6 :
218 FIB_PROTOCOL_IP4),
219 .fp_addr = t->tunnel_dst
220 };
221 /* *INDENT-ON* */
222
223 adj_midchain_delegate_stack (ai, t->fib_index, &dst);
Ole Troan298c6952018-03-08 12:30:43 +0100224 }
Ole Troan298c6952018-03-08 12:30:43 +0100225}
226
227static adj_walk_rc_t
228ipip_adj_walk_cb (adj_index_t ai, void *ctx)
229{
230 ipip_tunnel_stack (ai);
231
232 return (ADJ_WALK_RC_CONTINUE);
233}
234
235static void
236ipip_tunnel_restack (ipip_tunnel_t * gt)
237{
238 fib_protocol_t proto;
239
240 /*
241 * walk all the adjacencies on th IPIP interface and restack them
242 */
243 FOR_EACH_FIB_IP_PROTOCOL (proto)
244 {
245 adj_nbr_walk (gt->sw_if_index, proto, ipip_adj_walk_cb, NULL);
246 }
247}
248
249void
250ipip_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
251{
Ole Troan298c6952018-03-08 12:30:43 +0100252 adj_midchain_fixup_t f;
Neale Ranns521a8d72018-12-06 13:46:49 +0000253 ipip_tunnel_t *t;
254 adj_flags_t af;
Ole Troan298c6952018-03-08 12:30:43 +0100255
256 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
257 if (!t)
258 return;
259
260 f = t->transport == IPIP_TRANSPORT_IP6 ? ipip6_fixup : ipip4_fixup;
Neale Ranns521a8d72018-12-06 13:46:49 +0000261 af = ADJ_FLAG_MIDCHAIN_IP_STACK;
262 if (VNET_LINK_ETHERNET == adj_get_link_type (ai))
263 af |= ADJ_FLAG_MIDCHAIN_NO_COUNT;
Ole Troan298c6952018-03-08 12:30:43 +0100264
Neale Ranns14053c92019-12-29 23:55:18 +0000265 adj_nbr_midchain_update_rewrite
266 (ai, f, uword_to_pointer (t->flags, void *), af,
267 ipip_build_rewrite (vnm, sw_if_index,
268 adj_get_link_type (ai), &t->tunnel_dst));
Ole Troan298c6952018-03-08 12:30:43 +0100269 ipip_tunnel_stack (ai);
270}
271
Neale Ranns14053c92019-12-29 23:55:18 +0000272typedef struct mipip_walk_ctx_t_
273{
274 const ipip_tunnel_t *t;
275 const nhrp_entry_t *ne;
276} mipip_walk_ctx_t;
277
278static adj_walk_rc_t
279mipip_mk_complete_walk (adj_index_t ai, void *data)
280{
281 mipip_walk_ctx_t *ctx = data;
282 adj_midchain_fixup_t f;
283
284 f = ctx->t->transport == IPIP_TRANSPORT_IP6 ? ipip6_fixup : ipip4_fixup;
285
286 adj_nbr_midchain_update_rewrite
287 (ai, f, uword_to_pointer (ctx->t->flags, void *),
288 ADJ_FLAG_MIDCHAIN_IP_STACK, ipip_build_rewrite (vnet_get_main (),
289 ctx->t->sw_if_index,
290 adj_get_link_type (ai),
291 &nhrp_entry_get_nh
292 (ctx->ne)->fp_addr));
293
294 nhrp_entry_adj_stack (ctx->ne, ai);
295
296 return (ADJ_WALK_RC_CONTINUE);
297}
298
299static adj_walk_rc_t
300mipip_mk_incomplete_walk (adj_index_t ai, void *data)
301{
302 ipip_tunnel_t *t = data;
303 adj_midchain_fixup_t f;
304
305 f = t->transport == IPIP_TRANSPORT_IP6 ? ipip6_fixup : ipip4_fixup;
306
307 adj_nbr_midchain_update_rewrite (ai, f, NULL, ADJ_FLAG_NONE, NULL);
308
309 adj_midchain_delegate_unstack (ai);
310
311 return (ADJ_WALK_RC_CONTINUE);
312}
313
314void
315mipip_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
316{
317 ipip_main_t *gm = &ipip_main;
318 adj_midchain_fixup_t f;
319 ip_adjacency_t *adj;
320 nhrp_entry_t *ne;
321 ipip_tunnel_t *t;
322 u32 ti;
323
324 adj = adj_get (ai);
325 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
326 t = pool_elt_at_index (gm->tunnels, ti);
327 f = t->transport == IPIP_TRANSPORT_IP6 ? ipip6_fixup : ipip4_fixup;
328
329 ne = nhrp_entry_find (sw_if_index, &adj->sub_type.nbr.next_hop);
330
331 if (NULL == ne)
332 {
333 // no NHRP entry to provide the next-hop
334 adj_nbr_midchain_update_rewrite
335 (ai, f, uword_to_pointer (t->flags, void *), ADJ_FLAG_NONE, NULL);
336 return;
337 }
338
339 mipip_walk_ctx_t ctx = {
340 .t = t,
341 .ne = ne
342 };
343 adj_nbr_walk_nh (sw_if_index,
344 adj->ia_nh_proto,
345 &adj->sub_type.nbr.next_hop, mipip_mk_complete_walk, &ctx);
346}
347
Ole Troan298c6952018-03-08 12:30:43 +0100348static u8 *
349format_ipip_tunnel_name (u8 * s, va_list * args)
350{
351 u32 dev_instance = va_arg (*args, u32);
352 ipip_main_t *gm = &ipip_main;
353 ipip_tunnel_t *t;
354
355 if (dev_instance >= vec_len (gm->tunnels))
356 return format (s, "<improperly-referenced>");
357
358 t = pool_elt_at_index (gm->tunnels, dev_instance);
359 return format (s, "ipip%d", t->user_instance);
360}
361
362static u8 *
363format_ipip_device (u8 * s, va_list * args)
364{
365 u32 dev_instance = va_arg (*args, u32);
366 CLIB_UNUSED (int verbose) = va_arg (*args, int);
367
368 s = format (s, "IPIP tunnel: id %d\n", dev_instance);
369 return s;
370}
371
372static clib_error_t *
373ipip_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
374{
375 vnet_hw_interface_t *hi;
376 ipip_tunnel_t *t;
377
378 hi = vnet_get_hw_interface (vnm, hw_if_index);
379
380 t = ipip_tunnel_db_find_by_sw_if_index (hi->sw_if_index);
381 if (!t)
382 return 0;
383
384 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
385 vnet_hw_interface_set_flags (vnm, hw_if_index,
386 VNET_HW_INTERFACE_FLAG_LINK_UP);
387 else
388 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
389
390 ipip_tunnel_restack (t);
391
392 return /* no error */ 0;
393}
394
Neale Rannsc87b66c2019-02-07 07:26:12 -0800395static int
396ipip_tunnel_desc (u32 sw_if_index,
397 ip46_address_t * src, ip46_address_t * dst, u8 * is_l2)
398{
399 ipip_tunnel_t *t;
400
401 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
402 if (!t)
403 return -1;
404
405 *src = t->tunnel_src;
406 *dst = t->tunnel_dst;
407 *is_l2 = 0;
408
409 return (0);
410}
411
Ole Troan298c6952018-03-08 12:30:43 +0100412/* *INDENT-OFF* */
413VNET_DEVICE_CLASS(ipip_device_class) = {
414 .name = "IPIP tunnel device",
415 .format_device_name = format_ipip_tunnel_name,
416 .format_device = format_ipip_device,
417 .format_tx_trace = format_ipip_tx_trace,
418 .admin_up_down_function = ipip_interface_admin_up_down,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800419 .ip_tun_desc = ipip_tunnel_desc,
Ole Troan298c6952018-03-08 12:30:43 +0100420#ifdef SOON
421 .clear counter = 0;
422#endif
423};
424
425VNET_HW_INTERFACE_CLASS(ipip_hw_interface_class) = {
426 .name = "IPIP",
427 //.format_header = format_ipip_header_with_length,
428 //.unformat_header = unformat_ipip_header,
429 .build_rewrite = ipip_build_rewrite,
430 .update_adjacency = ipip_update_adj,
431 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
432};
Neale Ranns14053c92019-12-29 23:55:18 +0000433
434VNET_HW_INTERFACE_CLASS(mipip_hw_interface_class) = {
435 .name = "mIPIP",
436 //.format_header = format_ipip_header_with_length,
437 //.unformat_header = unformat_ipip_header,
438 .build_rewrite = ipip_build_rewrite,
439 .update_adjacency = mipip_update_adj,
440 .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA,
441};
Ole Troan298c6952018-03-08 12:30:43 +0100442/* *INDENT-ON* */
443
444ipip_tunnel_t *
Neale Ranns14053c92019-12-29 23:55:18 +0000445ipip_tunnel_db_find (const ipip_tunnel_key_t * key)
Ole Troan298c6952018-03-08 12:30:43 +0100446{
447 ipip_main_t *gm = &ipip_main;
448 uword *p;
449
450 p = hash_get_mem (gm->tunnel_by_key, key);
451 if (!p)
452 return (NULL);
453 return (pool_elt_at_index (gm->tunnels, p[0]));
454}
455
456ipip_tunnel_t *
457ipip_tunnel_db_find_by_sw_if_index (u32 sw_if_index)
458{
459 ipip_main_t *gm = &ipip_main;
Eyal Baricd307742018-07-22 12:45:15 +0300460 if (vec_len (gm->tunnel_index_by_sw_if_index) <= sw_if_index)
Ole Troan298c6952018-03-08 12:30:43 +0100461 return NULL;
462 u32 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
463 if (ti == ~0)
464 return NULL;
465 return pool_elt_at_index (gm->tunnels, ti);
466}
467
468void
Neale Ranns14053c92019-12-29 23:55:18 +0000469ipip_tunnel_db_add (ipip_tunnel_t * t, const ipip_tunnel_key_t * key)
Ole Troan298c6952018-03-08 12:30:43 +0100470{
471 ipip_main_t *gm = &ipip_main;
472
Neale Ranns14053c92019-12-29 23:55:18 +0000473 hash_set_mem_alloc (&gm->tunnel_by_key, key, t->dev_instance);
Ole Troan298c6952018-03-08 12:30:43 +0100474}
475
476void
Neale Ranns14053c92019-12-29 23:55:18 +0000477ipip_tunnel_db_remove (ipip_tunnel_t * t, const ipip_tunnel_key_t * key)
Ole Troan298c6952018-03-08 12:30:43 +0100478{
479 ipip_main_t *gm = &ipip_main;
480
Neale Ranns14053c92019-12-29 23:55:18 +0000481 hash_unset_mem_free (&gm->tunnel_by_key, key);
482}
483
484void
485ipip_mk_key_i (ipip_transport_t transport,
486 ipip_mode_t mode,
487 const ip46_address_t * src,
488 const ip46_address_t * dst,
489 u32 fib_index, ipip_tunnel_key_t * key)
490{
491 key->transport = transport;
492 key->mode = mode;
493 key->src = *src;
494 key->dst = *dst;
495 key->fib_index = fib_index;
496 key->__pad = 0;;
497}
498
499void
500ipip_mk_key (const ipip_tunnel_t * t, ipip_tunnel_key_t * key)
501{
502 ipip_mk_key_i (t->transport, t->mode,
503 &t->tunnel_src, &t->tunnel_dst, t->fib_index, key);
504}
505
506static void
507ipip_nhrp_mk_key (const ipip_tunnel_t * t,
508 const nhrp_entry_t * ne, ipip_tunnel_key_t * key)
509{
510 const fib_prefix_t *nh;
511
512 nh = nhrp_entry_get_nh (ne);
513
514 /* construct the key using mode P2P so it can be found in the DP */
515 ipip_mk_key_i (t->transport, IPIP_MODE_P2P,
516 &t->tunnel_src, &nh->fp_addr,
517 nhrp_entry_get_fib_index (ne), key);
518}
519
520static void
521ipip_nhrp_entry_added (const nhrp_entry_t * ne)
522{
523 ipip_main_t *gm = &ipip_main;
524 const ip46_address_t *nh;
525 ipip_tunnel_key_t key;
526 ipip_tunnel_t *t;
527 u32 sw_if_index;
528 u32 t_idx;
529
530 sw_if_index = nhrp_entry_get_sw_if_index (ne);
531 if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index)
532 return;
533
534 t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index];
535
536 if (INDEX_INVALID == t_idx)
537 return;
538
539 t = pool_elt_at_index (gm->tunnels, t_idx);
540
541 ipip_nhrp_mk_key (t, ne, &key);
542 ipip_tunnel_db_add (t, &key);
543
544 // update the rewrites for each of the adjacencies for this next-hop
545 mipip_walk_ctx_t ctx = {
546 .t = t,
547 .ne = ne
548 };
549 nh = nhrp_entry_get_peer (ne);
550 adj_nbr_walk_nh (nhrp_entry_get_sw_if_index (ne),
551 (ip46_address_is_ip4 (nh) ?
552 FIB_PROTOCOL_IP4 :
553 FIB_PROTOCOL_IP6), nh, mipip_mk_complete_walk, &ctx);
554}
555
556static void
557ipip_nhrp_entry_deleted (const nhrp_entry_t * ne)
558{
559 ipip_main_t *gm = &ipip_main;
560 const ip46_address_t *nh;
561 ipip_tunnel_key_t key;
562 ipip_tunnel_t *t;
563 u32 sw_if_index;
564 u32 t_idx;
565
566 sw_if_index = nhrp_entry_get_sw_if_index (ne);
567 if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index)
568 return;
569
570 t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index];
571
572 if (INDEX_INVALID == t_idx)
573 return;
574
575 t = pool_elt_at_index (gm->tunnels, t_idx);
576
577 ipip_nhrp_mk_key (t, ne, &key);
578 ipip_tunnel_db_remove (t, &key);
579
580 nh = nhrp_entry_get_peer (ne);
581
582 /* make all the adjacencies incomplete */
583 adj_nbr_walk_nh (nhrp_entry_get_sw_if_index (ne),
584 (ip46_address_is_ip4 (nh) ?
585 FIB_PROTOCOL_IP4 :
586 FIB_PROTOCOL_IP6), nh, mipip_mk_incomplete_walk, t);
587}
588
589static walk_rc_t
590ipip_tunnel_delete_nhrp_walk (index_t nei, void *ctx)
591{
592 ipip_tunnel_t *t = ctx;
593 ipip_tunnel_key_t key;
594
595 ipip_nhrp_mk_key (t, nhrp_entry_get (nei), &key);
596 ipip_tunnel_db_remove (t, &key);
597
598 return (WALK_CONTINUE);
599}
600
601static walk_rc_t
602ipip_tunnel_add_nhrp_walk (index_t nei, void *ctx)
603{
604 ipip_tunnel_t *t = ctx;
605 ipip_tunnel_key_t key;
606
607 ipip_nhrp_mk_key (t, nhrp_entry_get (nei), &key);
608 ipip_tunnel_db_add (t, &key);
609
610 return (WALK_CONTINUE);
Ole Troan298c6952018-03-08 12:30:43 +0100611}
612
Ole Troan298c6952018-03-08 12:30:43 +0100613int
614ipip_add_tunnel (ipip_transport_t transport,
615 u32 instance, ip46_address_t * src, ip46_address_t * dst,
Neale Ranns59ff9182019-12-29 23:55:18 +0000616 u32 fib_index, tunnel_encap_decap_flags_t flags,
Neale Ranns14053c92019-12-29 23:55:18 +0000617 ip_dscp_t dscp, tunnel_mode_t tmode, u32 * sw_if_indexp)
Ole Troan298c6952018-03-08 12:30:43 +0100618{
619 ipip_main_t *gm = &ipip_main;
620 vnet_main_t *vnm = gm->vnet_main;
621 ip4_main_t *im4 = &ip4_main;
622 ip6_main_t *im6 = &ip6_main;
623 ipip_tunnel_t *t;
624 vnet_hw_interface_t *hi;
625 u32 hw_if_index, sw_if_index;
Neale Ranns14053c92019-12-29 23:55:18 +0000626 ipip_tunnel_key_t key;
627 ipip_mode_t mode;
628
629 if (tmode == TUNNEL_MODE_MP && !ip46_address_is_zero (dst))
630 return (VNET_API_ERROR_INVALID_DST_ADDRESS);
631
632 mode = (tmode == TUNNEL_MODE_P2P ? IPIP_MODE_P2P : IPIP_MODE_P2MP);
633 ipip_mk_key_i (transport, mode, src, dst, fib_index, &key);
634
Ole Troan298c6952018-03-08 12:30:43 +0100635 t = ipip_tunnel_db_find (&key);
636 if (t)
Filip Tehlar4c6b1b62019-11-30 20:49:40 +0000637 {
638 if (sw_if_indexp)
639 sw_if_indexp[0] = t->sw_if_index;
640 return VNET_API_ERROR_IF_ALREADY_EXISTS;
641 }
Ole Troan298c6952018-03-08 12:30:43 +0100642
643 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400644 clib_memset (t, 0, sizeof (*t));
Ole Troan298c6952018-03-08 12:30:43 +0100645
646 /* Reconcile the real dev_instance and a possible requested instance */
647 u32 t_idx = t - gm->tunnels; /* tunnel index (or instance) */
648 u32 u_idx = instance; /* user specified instance */
649 if (u_idx == ~0)
650 u_idx = t_idx;
651 if (hash_get (gm->instance_used, u_idx))
652 {
653 pool_put (gm->tunnels, t);
654 return VNET_API_ERROR_INSTANCE_IN_USE;
655 }
656 hash_set (gm->instance_used, u_idx, 1);
657
658 t->dev_instance = t_idx; /* actual */
659 t->user_instance = u_idx; /* name */
Ole Troan298c6952018-03-08 12:30:43 +0100660
661 hw_if_index = vnet_register_interface (vnm, ipip_device_class.index, t_idx,
Neale Ranns14053c92019-12-29 23:55:18 +0000662 (mode == IPIP_MODE_P2P ?
663 ipip_hw_interface_class.index :
664 mipip_hw_interface_class.index),
Ole Troan298c6952018-03-08 12:30:43 +0100665 t_idx);
666
667 hi = vnet_get_hw_interface (vnm, hw_if_index);
668 sw_if_index = hi->sw_if_index;
669
Neale Ranns14053c92019-12-29 23:55:18 +0000670 t->mode = mode;
Ole Troan298c6952018-03-08 12:30:43 +0100671 t->hw_if_index = hw_if_index;
672 t->fib_index = fib_index;
673 t->sw_if_index = sw_if_index;
Neale Ranns95346962019-11-25 13:04:44 +0000674 t->dscp = dscp;
675 t->flags = flags;
Ole Troan298c6952018-03-08 12:30:43 +0100676 t->transport = transport;
Neale Ranns95346962019-11-25 13:04:44 +0000677
Ole Troan298c6952018-03-08 12:30:43 +0100678 vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
679 gm->tunnel_index_by_sw_if_index[sw_if_index] = t_idx;
680
681 if (t->transport == IPIP_TRANSPORT_IP4)
682 {
683 vec_validate (im4->fib_index_by_sw_if_index, sw_if_index);
684 hi->min_packet_bytes = 64 + sizeof (ip4_header_t);
685 }
686 else
687 {
688 vec_validate (im6->fib_index_by_sw_if_index, sw_if_index);
689 hi->min_packet_bytes = 64 + sizeof (ip6_header_t);
690 }
691
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200692 /* Standard default ipip MTU. */
Ole Troand7231612018-06-07 10:17:57 +0200693 vnet_sw_interface_set_mtu (vnm, sw_if_index, 9000);
Ole Troan298c6952018-03-08 12:30:43 +0100694
695 t->tunnel_src = *src;
696 t->tunnel_dst = *dst;
697
698 ipip_tunnel_db_add (t, &key);
699
Neale Ranns14053c92019-12-29 23:55:18 +0000700 if (t->mode == IPIP_MODE_P2MP)
701 nhrp_walk_itf (t->sw_if_index, ipip_tunnel_add_nhrp_walk, t);
702
Ole Troan298c6952018-03-08 12:30:43 +0100703 if (sw_if_indexp)
704 *sw_if_indexp = sw_if_index;
705
706 if (t->transport == IPIP_TRANSPORT_IP6 && !gm->ip6_protocol_registered)
707 {
708 ip6_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip6_input_node.index);
709 ip6_register_protocol (IP_PROTOCOL_IPV6, ipip6_input_node.index);
710 gm->ip6_protocol_registered = true;
711 }
712 else if (t->transport == IPIP_TRANSPORT_IP4 && !gm->ip4_protocol_registered)
713 {
714 ip4_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip4_input_node.index);
715 ip4_register_protocol (IP_PROTOCOL_IPV6, ipip4_input_node.index);
716 gm->ip4_protocol_registered = true;
717 }
718 return 0;
719}
720
721int
722ipip_del_tunnel (u32 sw_if_index)
723{
724 ipip_main_t *gm = &ipip_main;
725 vnet_main_t *vnm = gm->vnet_main;
726 ipip_tunnel_t *t;
Neale Ranns14053c92019-12-29 23:55:18 +0000727 ipip_tunnel_key_t key;
Ole Troan298c6952018-03-08 12:30:43 +0100728
729 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
730 if (t == NULL)
731 return VNET_API_ERROR_NO_SUCH_ENTRY;
732
Neale Ranns14053c92019-12-29 23:55:18 +0000733 if (t->mode == IPIP_MODE_P2MP)
734 nhrp_walk_itf (t->sw_if_index, ipip_tunnel_delete_nhrp_walk, t);
735
Ole Troan298c6952018-03-08 12:30:43 +0100736 vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ );
737 gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
738 vnet_delete_hw_interface (vnm, t->hw_if_index);
Ole Troan298c6952018-03-08 12:30:43 +0100739 hash_unset (gm->instance_used, t->user_instance);
Neale Ranns14053c92019-12-29 23:55:18 +0000740
741 ipip_mk_key (t, &key);
742 ipip_tunnel_db_remove (t, &key);
Ole Troan298c6952018-03-08 12:30:43 +0100743 pool_put (gm->tunnels, t);
744
745 return 0;
746}
747
Neale Ranns14053c92019-12-29 23:55:18 +0000748const static nhrp_vft_t ipip_nhrp_vft = {
749 .nv_added = ipip_nhrp_entry_added,
750 .nv_deleted = ipip_nhrp_entry_deleted,
751};
752
Ole Troan298c6952018-03-08 12:30:43 +0100753static clib_error_t *
754ipip_init (vlib_main_t * vm)
755{
756 ipip_main_t *gm = &ipip_main;
757
Dave Barachb7b92992018-10-17 10:38:51 -0400758 clib_memset (gm, 0, sizeof (gm[0]));
Ole Troan298c6952018-03-08 12:30:43 +0100759 gm->vlib_main = vm;
760 gm->vnet_main = vnet_get_main ();
761 gm->tunnel_by_key =
762 hash_create_mem (0, sizeof (ipip_tunnel_key_t), sizeof (uword));
Ole Troan298c6952018-03-08 12:30:43 +0100763
Neale Ranns14053c92019-12-29 23:55:18 +0000764 nhrp_register (&ipip_nhrp_vft);
765
Ole Troan298c6952018-03-08 12:30:43 +0100766 return 0;
767}
768
769VLIB_INIT_FUNCTION (ipip_init);
770
771/*
772 * fd.io coding-style-patch-verification: ON
773 *
774 * Local Variables:
775 * eval: (c-set-style "gnu")
776 * End:
777 */