blob: 6a707df10c72f7ca115b59b9308bdee352d9a330 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * map.c : MAP support
3 *
4 * Copyright (c) 2015 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 agreed 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
Neale Ranns0bfe5d82016-08-25 15:29:12 +010018#include <vnet/fib/fib_table.h>
19#include <vnet/fib/ip6_fib.h>
20#include <vnet/adj/adj.h>
21#include <vnet/map/map_dpo.h>
22
Ed Warnickecb9cada2015-12-08 15:45:58 -070023#include "map.h"
24
Damjan Marionc967a822016-10-28 22:35:17 +020025#ifdef __SSE4_2__
26static inline u32
27crc_u32 (u32 data, u32 value)
28{
29 __asm__ volatile ("crc32l %[data], %[value];":[value] "+r" (value):[data]
30 "rm" (data));
31 return value;
32}
33#else
Dave Barachbfdedbd2016-01-20 09:11:55 -050034#include <vppinfra/xxhash.h>
35
36static inline u32
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070037crc_u32 (u32 data, u32 value)
Dave Barachbfdedbd2016-01-20 09:11:55 -050038{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070039 u64 tmp = ((u64) data << 32) | (u64) value;
40 return (u32) clib_xxhash (tmp);
Dave Barachbfdedbd2016-01-20 09:11:55 -050041}
42#endif
43
Neale Ranns80823802017-02-20 18:23:41 -080044
Ed Warnickecb9cada2015-12-08 15:45:58 -070045/*
46 * This code supports the following MAP modes:
Damjan Marion607de1a2016-08-16 22:53:54 +020047 *
Ed Warnickecb9cada2015-12-08 15:45:58 -070048 * Algorithmic Shared IPv4 address (ea_bits_len > 0):
49 * ea_bits_len + ip4_prefix > 32
50 * psid_length > 0, ip6_prefix < 64, ip4_prefix <= 32
51 * Algorithmic Full IPv4 address (ea_bits_len > 0):
52 * ea_bits_len + ip4_prefix = 32
53 * psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
54 * Algorithmic IPv4 prefix (ea_bits_len > 0):
55 * ea_bits_len + ip4_prefix < 32
56 * psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
57 *
58 * Independent Shared IPv4 address (ea_bits_len = 0):
59 * ip4_prefix = 32
60 * psid_length > 0
61 * Rule IPv6 address = 128, Rule PSID Set
62 * Independent Full IPv4 address (ea_bits_len = 0):
63 * ip4_prefix = 32
64 * psid_length = 0, ip6_prefix = 128
65 * Independent IPv4 prefix (ea_bits_len = 0):
66 * ip4_prefix < 32
67 * psid_length = 0, ip6_prefix = 128
68 *
69 */
70
71/*
72 * This code supports MAP-T:
73 *
74 * With DMR prefix length equal to 96.
75 *
76 */
77
78
79i32
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070080ip4_get_port (ip4_header_t * ip, map_dir_e dir, u16 buffer_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -070081{
82 //TODO: use buffer length
83 if (ip->ip_version_and_header_length != 0x45 ||
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070084 ip4_get_fragment_offset (ip))
85 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070087 if (PREDICT_TRUE ((ip->protocol == IP_PROTOCOL_TCP) ||
88 (ip->protocol == IP_PROTOCOL_UDP)))
89 {
90 udp_header_t *udp = (void *) (ip + 1);
91 return (dir == MAP_SENDER) ? udp->src_port : udp->dst_port;
Ed Warnickecb9cada2015-12-08 15:45:58 -070092 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070093 else if (ip->protocol == IP_PROTOCOL_ICMP)
94 {
95 icmp46_header_t *icmp = (void *) (ip + 1);
96 if (icmp->type == ICMP4_echo_request || icmp->type == ICMP4_echo_reply)
97 {
98 return *((u16 *) (icmp + 1));
99 }
100 else if (clib_net_to_host_u16 (ip->length) >= 64)
101 {
102 ip = (ip4_header_t *) (icmp + 2);
103 if (PREDICT_TRUE ((ip->protocol == IP_PROTOCOL_TCP) ||
104 (ip->protocol == IP_PROTOCOL_UDP)))
105 {
106 udp_header_t *udp = (void *) (ip + 1);
107 return (dir == MAP_SENDER) ? udp->dst_port : udp->src_port;
108 }
109 else if (ip->protocol == IP_PROTOCOL_ICMP)
110 {
111 icmp46_header_t *icmp = (void *) (ip + 1);
112 if (icmp->type == ICMP4_echo_request ||
113 icmp->type == ICMP4_echo_reply)
114 {
115 return *((u16 *) (icmp + 1));
116 }
117 }
118 }
119 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120 return -1;
121}
122
123i32
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700124ip6_get_port (ip6_header_t * ip6, map_dir_e dir, u16 buffer_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125{
126 u8 l4_protocol;
127 u16 l4_offset;
128 u16 frag_offset;
129 u8 *l4;
130
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700131 if (ip6_parse (ip6, buffer_len, &l4_protocol, &l4_offset, &frag_offset))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132 return -1;
133
134 //TODO: Use buffer length
135
136 if (frag_offset &&
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700137 ip6_frag_hdr_offset (((ip6_frag_hdr_t *)
138 u8_ptr_add (ip6, frag_offset))))
139 return -1; //Can't deal with non-first fragment for now
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700141 l4 = u8_ptr_add (ip6, l4_offset);
142 if (l4_protocol == IP_PROTOCOL_TCP || l4_protocol == IP_PROTOCOL_UDP)
143 {
144 return (dir ==
Ed Warnicke853e7202016-08-12 11:42:26 -0700145 MAP_SENDER) ? ((udp_header_t *) (l4))->src_port : ((udp_header_t
146 *)
147 (l4))->dst_port;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700149 else if (l4_protocol == IP_PROTOCOL_ICMP6)
150 {
151 icmp46_header_t *icmp = (icmp46_header_t *) (l4);
152 if (icmp->type == ICMP6_echo_request)
153 {
154 return (dir == MAP_SENDER) ? ((u16 *) (icmp))[2] : -1;
155 }
156 else if (icmp->type == ICMP6_echo_reply)
157 {
158 return (dir == MAP_SENDER) ? -1 : ((u16 *) (icmp))[2];
159 }
160 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 return -1;
162}
163
164
165int
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700166map_create_domain (ip4_address_t * ip4_prefix,
167 u8 ip4_prefix_len,
168 ip6_address_t * ip6_prefix,
169 u8 ip6_prefix_len,
170 ip6_address_t * ip6_src,
171 u8 ip6_src_len,
172 u8 ea_bits_len,
173 u8 psid_offset,
174 u8 psid_length, u32 * map_domain_index, u16 mtu, u8 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175{
Ole Troan07e7eab2016-08-25 00:24:08 +0200176 u8 suffix_len, suffix_shift;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100177 map_main_t *mm = &map_main;
Neale Ranns948e00f2016-10-20 13:39:34 +0100178 dpo_id_t dpo_v4 = DPO_INVALID;
179 dpo_id_t dpo_v6 = DPO_INVALID;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100180 map_domain_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182 /* Sanity check on the src prefix length */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700183 if (flags & MAP_DOMAIN_TRANSLATION)
184 {
185 if (ip6_src_len != 96)
186 {
187 clib_warning ("MAP-T only supports ip6_src_len = 96 for now.");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188 return -1;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700189 }
190 }
191 else
192 {
193 if (ip6_src_len != 128)
194 {
195 clib_warning
Ole Troan07e7eab2016-08-25 00:24:08 +0200196 ("MAP-E requires a BR address, not a prefix (ip6_src_len should "
197 "be 128).");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198 return -1;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700199 }
200 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201
Ole Troan07e7eab2016-08-25 00:24:08 +0200202 /* How many, and which bits to grab from the IPv4 DA */
203 if (ip4_prefix_len + ea_bits_len < 32)
204 {
205 flags |= MAP_DOMAIN_PREFIX;
Ole Troand575e692016-08-25 12:26:47 +0200206 suffix_shift = 32 - ip4_prefix_len - ea_bits_len;
207 suffix_len = ea_bits_len;
Ole Troan07e7eab2016-08-25 00:24:08 +0200208 }
209 else
210 {
211 suffix_shift = 0;
212 suffix_len = 32 - ip4_prefix_len;
213 }
214
215 /* EA bits must be within the first 64 bits */
216 if (ea_bits_len > 0 && ((ip6_prefix_len + ea_bits_len) > 64 ||
217 ip6_prefix_len + suffix_len + psid_length > 64))
218 {
219 clib_warning
220 ("Embedded Address bits must be within the first 64 bits of "
221 "the IPv6 prefix");
222 return -1;
223 }
224
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225 /* Get domain index */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700226 pool_get_aligned (mm->domains, d, CLIB_CACHE_LINE_BYTES);
227 memset (d, 0, sizeof (*d));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228 *map_domain_index = d - mm->domains;
229
230 /* Init domain struct */
231 d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
232 d->ip4_prefix_len = ip4_prefix_len;
233 d->ip6_prefix = *ip6_prefix;
234 d->ip6_prefix_len = ip6_prefix_len;
235 d->ip6_src = *ip6_src;
236 d->ip6_src_len = ip6_src_len;
237 d->ea_bits_len = ea_bits_len;
238 d->psid_offset = psid_offset;
239 d->psid_length = psid_length;
240 d->mtu = mtu;
241 d->flags = flags;
Ole Troan07e7eab2016-08-25 00:24:08 +0200242 d->suffix_shift = suffix_shift;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700243 d->suffix_mask = (1 << suffix_len) - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244
245 d->psid_shift = 16 - psid_length - psid_offset;
246 d->psid_mask = (1 << d->psid_length) - 1;
247 d->ea_shift = 64 - ip6_prefix_len - suffix_len - d->psid_length;
248
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100249 /* MAP data-plane object */
250 if (d->flags & MAP_DOMAIN_TRANSLATION)
251 map_t_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700252 else
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100253 map_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
254
255 /* Create ip4 route */
256 fib_prefix_t pfx = {
257 .fp_proto = FIB_PROTOCOL_IP4,
258 .fp_len = d->ip4_prefix_len,
259 .fp_addr = {
260 .ip4 = d->ip4_prefix,
261 }
262 ,
263 };
264 fib_table_entry_special_dpo_add (0, &pfx,
265 FIB_SOURCE_MAP,
266 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
267 dpo_reset (&dpo_v4);
268
269 /*
Neale Ranns9705c382017-02-20 20:29:41 -0800270 * construct a DPO to use the v6 domain
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100271 */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100272 if (d->flags & MAP_DOMAIN_TRANSLATION)
273 map_t_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
274 else
275 map_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
276
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100277 /*
Neale Ranns9705c382017-02-20 20:29:41 -0800278 * Multiple MAP domains may share same source IPv6 TEP. Which is just dandy.
279 * We are not tracking the sharing. So a v4 lookup to find the correct
280 * domain post decap/trnaslate is always done
281 *
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100282 * Create ip6 route. This is a reference counted add. If the prefix
283 * already exists and is MAP sourced, it is now MAP source n+1 times
284 * and will need to be removed n+1 times.
285 */
Neale Ranns9705c382017-02-20 20:29:41 -0800286 fib_prefix_t pfx6 = {
287 .fp_proto = FIB_PROTOCOL_IP6,
288 .fp_len = d->ip6_src_len,
289 .fp_addr.ip6 = d->ip6_src,
290 };
291
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100292 fib_table_entry_special_dpo_add (0, &pfx6,
293 FIB_SOURCE_MAP,
294 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v6);
295 dpo_reset (&dpo_v6);
296
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297 /* Validate packet/byte counters */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700298 map_domain_counter_lock (mm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700300 for (i = 0; i < vec_len (mm->simple_domain_counters); i++)
301 {
302 vlib_validate_simple_counter (&mm->simple_domain_counters[i],
303 *map_domain_index);
304 vlib_zero_simple_counter (&mm->simple_domain_counters[i],
305 *map_domain_index);
306 }
307 for (i = 0; i < vec_len (mm->domain_counters); i++)
308 {
309 vlib_validate_combined_counter (&mm->domain_counters[i],
310 *map_domain_index);
311 vlib_zero_combined_counter (&mm->domain_counters[i], *map_domain_index);
312 }
313 map_domain_counter_unlock (mm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314
315 return 0;
316}
317
318/*
319 * map_delete_domain
320 */
321int
322map_delete_domain (u32 map_domain_index)
323{
324 map_main_t *mm = &map_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325 map_domain_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700327 if (pool_is_free_index (mm->domains, map_domain_index))
328 {
329 clib_warning ("MAP domain delete: domain does not exist: %d",
330 map_domain_index);
331 return -1;
332 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700334 d = pool_elt_at_index (mm->domains, map_domain_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100336 fib_prefix_t pfx = {
337 .fp_proto = FIB_PROTOCOL_IP4,
338 .fp_len = d->ip4_prefix_len,
339 .fp_addr = {
340 .ip4 = d->ip4_prefix,
341 }
342 ,
343 };
344 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_MAP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100346 fib_prefix_t pfx6 = {
347 .fp_proto = FIB_PROTOCOL_IP6,
348 .fp_len = d->ip6_src_len,
349 .fp_addr = {
350 .ip6 = d->ip6_src,
351 }
352 ,
353 };
354 fib_table_entry_special_remove (0, &pfx6, FIB_SOURCE_MAP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356 /* Deleting rules */
357 if (d->rules)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700358 clib_mem_free (d->rules);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700360 pool_put (mm->domains, d);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361
362 return 0;
363}
364
365int
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700366map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367 u8 is_add)
368{
369 map_domain_t *d;
370 map_main_t *mm = &map_main;
371
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700372 if (pool_is_free_index (mm->domains, map_domain_index))
373 {
374 clib_warning ("MAP rule: domain does not exist: %d", map_domain_index);
375 return -1;
376 }
377 d = pool_elt_at_index (mm->domains, map_domain_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378
379 /* Rules are only used in 1:1 independent case */
380 if (d->ea_bits_len > 0)
381 return (-1);
382
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700383 if (!d->rules)
384 {
385 u32 l = (0x1 << d->psid_length) * sizeof (ip6_address_t);
386 d->rules = clib_mem_alloc_aligned (l, CLIB_CACHE_LINE_BYTES);
387 if (!d->rules)
388 return -1;
389 memset (d->rules, 0, l);
390 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700392 if (psid >= (0x1 << d->psid_length))
393 {
394 clib_warning ("MAP rule: PSID outside bounds: %d [%d]", psid,
395 0x1 << d->psid_length);
396 return -1;
397 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700399 if (is_add)
400 {
401 d->rules[psid] = *tep;
402 }
403 else
404 {
405 memset (&d->rules[psid], 0, sizeof (ip6_address_t));
406 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407 return 0;
408}
409
410#ifdef MAP_SKIP_IP6_LOOKUP
Neale Ranns80823802017-02-20 18:23:41 -0800411/**
412 * Pre-resolvd per-protocol global next-hops
413 */
414map_main_pre_resolved_t pre_resolved[FIB_PROTOCOL_MAX];
415
416static void
417map_pre_resolve_init (map_main_pre_resolved_t * pr)
418{
419 pr->fei = FIB_NODE_INDEX_INVALID;
420 fib_node_init (&pr->node, FIB_NODE_TYPE_MAP_E);
421}
422
423static u8 *
424format_map_pre_resolve (u8 * s, va_list ap)
425{
426 map_main_pre_resolved_t *pr = va_arg (ap, map_main_pre_resolved_t *);
427
428 if (FIB_NODE_INDEX_INVALID != pr->fei)
429 {
430 fib_prefix_t pfx;
431
432 fib_entry_get_prefix (pr->fei, &pfx);
433
434 return (format (s, "%U (%u)",
435 format_ip46_address, &pfx.fp_addr, IP46_TYPE_ANY,
436 pr->dpo.dpoi_index));
437 }
438 else
439 {
440 return (format (s, "un-set"));
441 }
442}
443
444
445/**
446 * Function definition to inform the FIB node that its last lock has gone.
447 */
448static void
449map_last_lock_gone (fib_node_t * node)
450{
451 /*
452 * The MAP is a root of the graph. As such
453 * it never has children and thus is never locked.
454 */
455 ASSERT (0);
456}
457
458static map_main_pre_resolved_t *
459map_from_fib_node (fib_node_t * node)
460{
461#if (CLIB_DEBUG > 0)
462 ASSERT (FIB_NODE_TYPE_MAP_E == node->fn_type);
463#endif
464 return ((map_main_pre_resolved_t *)
465 (((char *) node) -
466 STRUCT_OFFSET_OF (map_main_pre_resolved_t, node)));
467}
468
469static void
470map_stack (map_main_pre_resolved_t * pr)
471{
472 const dpo_id_t *dpo;
473
474 dpo = fib_entry_contribute_ip_forwarding (pr->fei);
475
476 dpo_copy (&pr->dpo, dpo);
477}
478
479/**
480 * Function definition to backwalk a FIB node
481 */
482static fib_node_back_walk_rc_t
483map_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
484{
485 map_stack (map_from_fib_node (node));
486
487 return (FIB_NODE_BACK_WALK_CONTINUE);
488}
489
490/**
491 * Function definition to get a FIB node from its index
492 */
493static fib_node_t *
494map_fib_node_get (fib_node_index_t index)
495{
496 return (&pre_resolved[index].node);
497}
498
499/*
500 * Virtual function table registered by MPLS GRE tunnels
501 * for participation in the FIB object graph.
502 */
503const static fib_node_vft_t map_vft = {
504 .fnv_get = map_fib_node_get,
505 .fnv_last_lock = map_last_lock_gone,
506 .fnv_back_walk = map_back_walk,
507};
508
509static void
510map_fib_resolve (map_main_pre_resolved_t * pr,
511 fib_protocol_t proto, u8 len, const ip46_address_t * addr)
512{
513 fib_prefix_t pfx = {
514 .fp_proto = proto,
515 .fp_len = len,
516 .fp_addr = *addr,
517 };
518
519 pr->fei = fib_table_entry_special_add (0, // default fib
520 &pfx,
Neale Rannsa0558302017-04-13 00:44:52 -0700521 FIB_SOURCE_RR, FIB_ENTRY_FLAG_NONE);
Neale Ranns80823802017-02-20 18:23:41 -0800522 pr->sibling = fib_entry_child_add (pr->fei, FIB_NODE_TYPE_MAP_E, proto);
523 map_stack (pr);
524}
525
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526static void
Neale Ranns69b7aa42017-03-10 03:04:12 -0800527map_fib_unresolve (map_main_pre_resolved_t * pr,
528 fib_protocol_t proto, u8 len, const ip46_address_t * addr)
529{
530 fib_prefix_t pfx = {
531 .fp_proto = proto,
532 .fp_len = len,
533 .fp_addr = *addr,
534 };
535
536 fib_entry_child_remove (pr->fei, pr->sibling);
537
538 fib_table_entry_special_remove (0, // default fib
539 &pfx, FIB_SOURCE_RR);
540 dpo_reset (&pr->dpo);
541
542 pr->fei = FIB_NODE_INDEX_INVALID;
543 pr->sibling = FIB_NODE_INDEX_INVALID;
544}
545
546static void
547map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6, int is_del)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548{
Neale Ranns80823802017-02-20 18:23:41 -0800549 if (ip6 && (ip6->as_u64[0] != 0 || ip6->as_u64[1] != 0))
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700550 {
Neale Ranns80823802017-02-20 18:23:41 -0800551 ip46_address_t addr = {
552 .ip6 = *ip6,
553 };
Neale Ranns69b7aa42017-03-10 03:04:12 -0800554 if (is_del)
555 map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP6],
556 FIB_PROTOCOL_IP6, 128, &addr);
557 else
558 map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP6],
559 FIB_PROTOCOL_IP6, 128, &addr);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700560 }
Neale Ranns80823802017-02-20 18:23:41 -0800561 if (ip4 && (ip4->as_u32 != 0))
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700562 {
Neale Ranns80823802017-02-20 18:23:41 -0800563 ip46_address_t addr = {
564 .ip4 = *ip4,
565 };
Neale Ranns69b7aa42017-03-10 03:04:12 -0800566 if (is_del)
567 map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP4],
568 FIB_PROTOCOL_IP4, 32, &addr);
569 else
570 map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP4],
571 FIB_PROTOCOL_IP4, 32, &addr);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700572 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573}
574#endif
575
576static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700577map_security_check_command_fn (vlib_main_t * vm,
578 unformat_input_t * input,
579 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580{
581 unformat_input_t _line_input, *line_input = &_line_input;
582 map_main_t *mm = &map_main;
Billy McFalla9a20e72017-02-15 11:39:12 -0500583 clib_error_t *error = NULL;
584
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700586 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700588
589 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
590 {
591 if (unformat (line_input, "off"))
592 mm->sec_check = false;
593 else if (unformat (line_input, "on"))
594 mm->sec_check = true;
595 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500596 {
597 error = clib_error_return (0, "unknown input `%U'",
598 format_unformat_error, line_input);
599 goto done;
600 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700601 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500602
603done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700604 unformat_free (line_input);
Billy McFalla9a20e72017-02-15 11:39:12 -0500605
606 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607}
608
609static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700610map_security_check_frag_command_fn (vlib_main_t * vm,
611 unformat_input_t * input,
612 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613{
614 unformat_input_t _line_input, *line_input = &_line_input;
615 map_main_t *mm = &map_main;
Billy McFalla9a20e72017-02-15 11:39:12 -0500616 clib_error_t *error = NULL;
617
Ed Warnickecb9cada2015-12-08 15:45:58 -0700618 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700619 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700620 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700621
622 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
623 {
624 if (unformat (line_input, "off"))
625 mm->sec_check_frag = false;
626 else if (unformat (line_input, "on"))
627 mm->sec_check_frag = true;
628 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500629 {
630 error = clib_error_return (0, "unknown input `%U'",
631 format_unformat_error, line_input);
632 goto done;
633 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700634 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500635
636done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700637 unformat_free (line_input);
Billy McFalla9a20e72017-02-15 11:39:12 -0500638
639 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700640}
641
642static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700643map_add_domain_command_fn (vlib_main_t * vm,
644 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645{
646 unformat_input_t _line_input, *line_input = &_line_input;
647 ip4_address_t ip4_prefix;
648 ip6_address_t ip6_prefix;
649 ip6_address_t ip6_src;
Dave Barach042ffb42016-08-12 09:26:47 -0400650 u32 ip6_prefix_len = 0, ip4_prefix_len = 0, map_domain_index, ip6_src_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651 u32 num_m_args = 0;
652 /* Optional arguments */
Dave Barach042ffb42016-08-12 09:26:47 -0400653 u32 ea_bits_len = 0, psid_offset = 0, psid_length = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654 u32 mtu = 0;
655 u8 flags = 0;
656 ip6_src_len = 128;
Billy McFalla9a20e72017-02-15 11:39:12 -0500657 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658
659 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700660 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700662
663 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
664 {
665 if (unformat
666 (line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix,
667 &ip4_prefix_len))
668 num_m_args++;
669 else
670 if (unformat
671 (line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix,
672 &ip6_prefix_len))
673 num_m_args++;
674 else
675 if (unformat
676 (line_input, "ip6-src %U/%d", unformat_ip6_address, &ip6_src,
677 &ip6_src_len))
678 num_m_args++;
679 else
680 if (unformat
681 (line_input, "ip6-src %U", unformat_ip6_address, &ip6_src))
682 num_m_args++;
683 else if (unformat (line_input, "ea-bits-len %d", &ea_bits_len))
684 num_m_args++;
685 else if (unformat (line_input, "psid-offset %d", &psid_offset))
686 num_m_args++;
687 else if (unformat (line_input, "psid-len %d", &psid_length))
688 num_m_args++;
689 else if (unformat (line_input, "mtu %d", &mtu))
690 num_m_args++;
691 else if (unformat (line_input, "map-t"))
692 flags |= MAP_DOMAIN_TRANSLATION;
693 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500694 {
695 error = clib_error_return (0, "unknown input `%U'",
696 format_unformat_error, line_input);
697 goto done;
698 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700699 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700
701 if (num_m_args < 3)
Billy McFalla9a20e72017-02-15 11:39:12 -0500702 {
703 error = clib_error_return (0, "mandatory argument(s) missing");
704 goto done;
705 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700706
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700707 map_create_domain (&ip4_prefix, ip4_prefix_len,
708 &ip6_prefix, ip6_prefix_len, &ip6_src, ip6_src_len,
709 ea_bits_len, psid_offset, psid_length, &map_domain_index,
710 mtu, flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711
Billy McFalla9a20e72017-02-15 11:39:12 -0500712done:
713 unformat_free (line_input);
714
715 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716}
717
718static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700719map_del_domain_command_fn (vlib_main_t * vm,
720 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721{
722 unformat_input_t _line_input, *line_input = &_line_input;
723 u32 num_m_args = 0;
724 u32 map_domain_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500725 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726
727 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700728 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700730
731 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
732 {
733 if (unformat (line_input, "index %d", &map_domain_index))
734 num_m_args++;
735 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500736 {
737 error = clib_error_return (0, "unknown input `%U'",
738 format_unformat_error, line_input);
739 goto done;
740 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700741 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742
743 if (num_m_args != 1)
Billy McFalla9a20e72017-02-15 11:39:12 -0500744 {
745 error = clib_error_return (0, "mandatory argument(s) missing");
746 goto done;
747 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700749 map_delete_domain (map_domain_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750
Billy McFalla9a20e72017-02-15 11:39:12 -0500751done:
752 unformat_free (line_input);
753
754 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755}
756
757static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700758map_add_rule_command_fn (vlib_main_t * vm,
759 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760{
761 unformat_input_t _line_input, *line_input = &_line_input;
762 ip6_address_t tep;
763 u32 num_m_args = 0;
Dave Barach042ffb42016-08-12 09:26:47 -0400764 u32 psid = 0, map_domain_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500765 clib_error_t *error = NULL;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700766
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700768 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769 return 0;
770
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700771 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
772 {
773 if (unformat (line_input, "index %d", &map_domain_index))
774 num_m_args++;
775 else if (unformat (line_input, "psid %d", &psid))
776 num_m_args++;
777 else
778 if (unformat (line_input, "ip6-dst %U", unformat_ip6_address, &tep))
779 num_m_args++;
780 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500781 {
782 error = clib_error_return (0, "unknown input `%U'",
783 format_unformat_error, line_input);
784 goto done;
785 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700786 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787
788 if (num_m_args != 3)
Billy McFalla9a20e72017-02-15 11:39:12 -0500789 {
790 error = clib_error_return (0, "mandatory argument(s) missing");
791 goto done;
792 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700794 if (map_add_del_psid (map_domain_index, psid, &tep, 1) != 0)
795 {
Billy McFalla9a20e72017-02-15 11:39:12 -0500796 error = clib_error_return (0, "Failing to add Mapping Rule");
797 goto done;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700798 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500799
800done:
801 unformat_free (line_input);
802
803 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700804}
805
806#if MAP_SKIP_IP6_LOOKUP
807static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700808map_pre_resolve_command_fn (vlib_main_t * vm,
809 unformat_input_t * input,
810 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700811{
812 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns80823802017-02-20 18:23:41 -0800813 ip4_address_t ip4nh, *p_v4 = NULL;
814 ip6_address_t ip6nh, *p_v6 = NULL;
Billy McFalla9a20e72017-02-15 11:39:12 -0500815 clib_error_t *error = NULL;
Neale Ranns69b7aa42017-03-10 03:04:12 -0800816 int is_del = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700818 memset (&ip4nh, 0, sizeof (ip4nh));
819 memset (&ip6nh, 0, sizeof (ip6nh));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700820
821 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700822 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700823 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700824
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700825 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
826 {
827 if (unformat (line_input, "ip4-nh %U", unformat_ip4_address, &ip4nh))
Neale Ranns80823802017-02-20 18:23:41 -0800828 p_v4 = &ip4nh;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700829 else
830 if (unformat (line_input, "ip6-nh %U", unformat_ip6_address, &ip6nh))
Neale Ranns80823802017-02-20 18:23:41 -0800831 p_v6 = &ip6nh;
Neale Ranns69b7aa42017-03-10 03:04:12 -0800832 else if (unformat (line_input, "del"))
833 is_del = 1;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700834 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500835 {
836 error = clib_error_return (0, "unknown input `%U'",
837 format_unformat_error, line_input);
838 goto done;
839 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700840 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700841
Neale Ranns69b7aa42017-03-10 03:04:12 -0800842 map_pre_resolve (p_v4, p_v6, is_del);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700843
Billy McFalla9a20e72017-02-15 11:39:12 -0500844done:
845 unformat_free (line_input);
846
847 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700848}
849#endif
850
851static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700852map_icmp_relay_source_address_command_fn (vlib_main_t * vm,
853 unformat_input_t * input,
854 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700855{
856 unformat_input_t _line_input, *line_input = &_line_input;
857 ip4_address_t icmp_src_address;
858 map_main_t *mm = &map_main;
Billy McFalla9a20e72017-02-15 11:39:12 -0500859 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700860
Ole Troancda94822016-01-07 14:37:25 +0100861 mm->icmp4_src_address.as_u32 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700862
863 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700864 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700866
867 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
868 {
869 if (unformat
870 (line_input, "%U", unformat_ip4_address, &icmp_src_address))
871 mm->icmp4_src_address = icmp_src_address;
872 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500873 {
874 error = clib_error_return (0, "unknown input `%U'",
875 format_unformat_error, line_input);
876 goto done;
877 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700878 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500879
880done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700881 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882
Billy McFalla9a20e72017-02-15 11:39:12 -0500883 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700884}
885
886static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700887map_icmp_unreachables_command_fn (vlib_main_t * vm,
888 unformat_input_t * input,
889 vlib_cli_command_t * cmd)
Ole Troancda94822016-01-07 14:37:25 +0100890{
891 unformat_input_t _line_input, *line_input = &_line_input;
892 map_main_t *mm = &map_main;
893 int num_m_args = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500894 clib_error_t *error = NULL;
Ole Troancda94822016-01-07 14:37:25 +0100895
896 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700897 if (!unformat_user (input, unformat_line_input, line_input))
Ole Troancda94822016-01-07 14:37:25 +0100898 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700899
900 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
901 {
902 num_m_args++;
903 if (unformat (line_input, "on"))
904 mm->icmp6_enabled = true;
905 else if (unformat (line_input, "off"))
906 mm->icmp6_enabled = false;
907 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500908 {
909 error = clib_error_return (0, "unknown input `%U'",
910 format_unformat_error, line_input);
911 goto done;
912 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700913 }
Ole Troancda94822016-01-07 14:37:25 +0100914
915
916 if (num_m_args != 1)
Billy McFalla9a20e72017-02-15 11:39:12 -0500917 error = clib_error_return (0, "mandatory argument(s) missing");
Ole Troancda94822016-01-07 14:37:25 +0100918
Billy McFalla9a20e72017-02-15 11:39:12 -0500919done:
920 unformat_free (line_input);
921
922 return error;
Ole Troancda94822016-01-07 14:37:25 +0100923}
924
925static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700926map_fragment_command_fn (vlib_main_t * vm,
927 unformat_input_t * input, vlib_cli_command_t * cmd)
Ole Troan9fb87552016-01-13 22:30:43 +0100928{
929 unformat_input_t _line_input, *line_input = &_line_input;
930 map_main_t *mm = &map_main;
Billy McFalla9a20e72017-02-15 11:39:12 -0500931 clib_error_t *error = NULL;
Ole Troan9fb87552016-01-13 22:30:43 +0100932
933 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700934 if (!unformat_user (input, unformat_line_input, line_input))
Ole Troan9fb87552016-01-13 22:30:43 +0100935 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700936
937 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
938 {
939 if (unformat (line_input, "inner"))
940 mm->frag_inner = true;
941 else if (unformat (line_input, "outer"))
942 mm->frag_inner = false;
943 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500944 {
945 error = clib_error_return (0, "unknown input `%U'",
946 format_unformat_error, line_input);
947 goto done;
948 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700949 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500950
951done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700952 unformat_free (line_input);
Ole Troan9fb87552016-01-13 22:30:43 +0100953
Billy McFalla9a20e72017-02-15 11:39:12 -0500954 return error;
Ole Troan9fb87552016-01-13 22:30:43 +0100955}
956
957static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700958map_fragment_df_command_fn (vlib_main_t * vm,
959 unformat_input_t * input,
960 vlib_cli_command_t * cmd)
Ole Troan9fb87552016-01-13 22:30:43 +0100961{
962 unformat_input_t _line_input, *line_input = &_line_input;
963 map_main_t *mm = &map_main;
Billy McFalla9a20e72017-02-15 11:39:12 -0500964 clib_error_t *error = NULL;
Ole Troan9fb87552016-01-13 22:30:43 +0100965
966 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700967 if (!unformat_user (input, unformat_line_input, line_input))
Ole Troan9fb87552016-01-13 22:30:43 +0100968 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700969
970 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
971 {
972 if (unformat (line_input, "on"))
973 mm->frag_ignore_df = true;
974 else if (unformat (line_input, "off"))
975 mm->frag_ignore_df = false;
976 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500977 {
978 error = clib_error_return (0, "unknown input `%U'",
979 format_unformat_error, line_input);
980 goto done;
981 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700982 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500983
984done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700985 unformat_free (line_input);
Ole Troan9fb87552016-01-13 22:30:43 +0100986
Billy McFalla9a20e72017-02-15 11:39:12 -0500987 return error;
Ole Troan9fb87552016-01-13 22:30:43 +0100988}
989
990static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700991map_traffic_class_command_fn (vlib_main_t * vm,
992 unformat_input_t * input,
993 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700994{
995 unformat_input_t _line_input, *line_input = &_line_input;
996 map_main_t *mm = &map_main;
997 u32 tc = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500998 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700999
1000 mm->tc_copy = false;
1001
1002 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001003 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001004 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001005
1006 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1007 {
1008 if (unformat (line_input, "copy"))
1009 mm->tc_copy = true;
1010 else if (unformat (line_input, "%x", &tc))
1011 mm->tc = tc & 0xff;
1012 else
Billy McFalla9a20e72017-02-15 11:39:12 -05001013 {
1014 error = clib_error_return (0, "unknown input `%U'",
1015 format_unformat_error, line_input);
1016 goto done;
1017 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001018 }
Billy McFalla9a20e72017-02-15 11:39:12 -05001019
1020done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001021 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001022
Billy McFalla9a20e72017-02-15 11:39:12 -05001023 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001024}
1025
1026static u8 *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001027format_map_domain (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001028{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001029 map_domain_t *d = va_arg (*args, map_domain_t *);
1030 bool counters = va_arg (*args, int);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001031 map_main_t *mm = &map_main;
1032 ip6_address_t ip6_prefix;
1033
1034 if (d->rules)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001035 memset (&ip6_prefix, 0, sizeof (ip6_prefix));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001036 else
1037 ip6_prefix = d->ip6_prefix;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001038
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001039 s = format (s,
1040 "[%d] ip4-pfx %U/%d ip6-pfx %U/%d ip6-src %U/%d ea_bits_len %d psid-offset %d psid-len %d mtu %d %s",
1041 d - mm->domains,
1042 format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
1043 format_ip6_address, &ip6_prefix, d->ip6_prefix_len,
1044 format_ip6_address, &d->ip6_src, d->ip6_src_len,
1045 d->ea_bits_len, d->psid_offset, d->psid_length, d->mtu,
1046 (d->flags & MAP_DOMAIN_TRANSLATION) ? "map-t" : "");
Ole Troan366ac6e2016-01-06 12:40:28 +01001047
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001048 if (counters)
1049 {
1050 map_domain_counter_lock (mm);
1051 vlib_counter_t v;
1052 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_TX],
1053 d - mm->domains, &v);
1054 s = format (s, " TX: %lld/%lld", v.packets, v.bytes);
1055 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_RX],
1056 d - mm->domains, &v);
1057 s = format (s, " RX: %lld/%lld", v.packets, v.bytes);
1058 map_domain_counter_unlock (mm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001059 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001060 s = format (s, "\n");
1061
1062 if (d->rules)
1063 {
1064 int i;
1065 ip6_address_t dst;
1066 for (i = 0; i < (0x1 << d->psid_length); i++)
1067 {
1068 dst = d->rules[i];
1069 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
1070 continue;
1071 s = format (s,
1072 " rule psid: %d ip6-dst %U\n", i, format_ip6_address,
1073 &dst);
1074 }
1075 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001076 return s;
1077}
1078
1079static u8 *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001080format_map_ip4_reass (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001081{
1082 map_main_t *mm = &map_main;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001083 map_ip4_reass_t *r = va_arg (*args, map_ip4_reass_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084 map_ip4_reass_key_t *k = &r->key;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001085 f64 now = vlib_time_now (mm->vlib_main);
1086 f64 lifetime = (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001087 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001088 s = format (s,
1089 "ip4-reass src=%U dst=%U protocol=%d identifier=%d port=%d lifetime=%.3lf\n",
1090 format_ip4_address, &k->src.as_u8, format_ip4_address,
1091 &k->dst.as_u8, k->protocol,
1092 clib_net_to_host_u16 (k->fragment_id),
1093 (r->port >= 0) ? clib_net_to_host_u16 (r->port) : -1, dt);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001094 return s;
1095}
1096
1097static u8 *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001098format_map_ip6_reass (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001099{
1100 map_main_t *mm = &map_main;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001101 map_ip6_reass_t *r = va_arg (*args, map_ip6_reass_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001102 map_ip6_reass_key_t *k = &r->key;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001103 f64 now = vlib_time_now (mm->vlib_main);
1104 f64 lifetime = (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001105 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001106 s = format (s,
1107 "ip6-reass src=%U dst=%U protocol=%d identifier=%d lifetime=%.3lf\n",
1108 format_ip6_address, &k->src.as_u8, format_ip6_address,
1109 &k->dst.as_u8, k->protocol,
1110 clib_net_to_host_u32 (k->fragment_id), dt);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001111 return s;
1112}
1113
1114static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001115show_map_domain_command_fn (vlib_main_t * vm, unformat_input_t * input,
1116 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001117{
1118 unformat_input_t _line_input, *line_input = &_line_input;
1119 map_main_t *mm = &map_main;
1120 map_domain_t *d;
1121 bool counters = false;
1122 u32 map_domain_index = ~0;
Billy McFalla9a20e72017-02-15 11:39:12 -05001123 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001124
1125 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001126 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001127 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001128
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001129 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1130 {
1131 if (unformat (line_input, "counters"))
1132 counters = true;
1133 else if (unformat (line_input, "index %d", &map_domain_index))
1134 ;
1135 else
Billy McFalla9a20e72017-02-15 11:39:12 -05001136 {
1137 error = clib_error_return (0, "unknown input `%U'",
1138 format_unformat_error, line_input);
1139 goto done;
1140 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001141 }
1142
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001143 if (pool_elts (mm->domains) == 0)
1144 vlib_cli_output (vm, "No MAP domains are configured...");
1145
1146 if (map_domain_index == ~0)
1147 {
1148 /* *INDENT-OFF* */
1149 pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
1150 /* *INDENT-ON* */
1151 }
1152 else
1153 {
1154 if (pool_is_free_index (mm->domains, map_domain_index))
1155 {
Billy McFalla9a20e72017-02-15 11:39:12 -05001156 error = clib_error_return (0, "MAP domain does not exists %d",
1157 map_domain_index);
1158 goto done;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001159 }
1160
1161 d = pool_elt_at_index (mm->domains, map_domain_index);
1162 vlib_cli_output (vm, "%U", format_map_domain, d, counters);
1163 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001164
Billy McFalla9a20e72017-02-15 11:39:12 -05001165done:
1166 unformat_free (line_input);
1167
1168 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001169}
1170
1171static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001172show_map_fragments_command_fn (vlib_main_t * vm, unformat_input_t * input,
1173 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001174{
1175 map_main_t *mm = &map_main;
1176 map_ip4_reass_t *f4;
1177 map_ip6_reass_t *f6;
1178
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001179 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001180 pool_foreach(f4, mm->ip4_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip4_reass, f4);}));
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001181 /* *INDENT-ON* */
1182 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001183 pool_foreach(f6, mm->ip6_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip6_reass, f6);}));
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001184 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001185 return (0);
1186}
1187
1188u64
1189map_error_counter_get (u32 node_index, map_error_t map_error)
1190{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001191 vlib_main_t *vm = vlib_get_main ();
1192 vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001193 vlib_error_main_t *em = &vm->error_main;
1194 vlib_error_t e = error_node->errors[map_error];
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001195 vlib_node_t *n = vlib_get_node (vm, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001196 u32 ci;
1197
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001198 ci = vlib_error_get_code (e);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001199 ASSERT (ci < n->n_errors);
1200 ci += n->error_heap_index;
1201
1202 return (em->counters[ci]);
1203}
1204
1205static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001206show_map_stats_command_fn (vlib_main_t * vm, unformat_input_t * input,
1207 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001208{
1209 map_main_t *mm = &map_main;
1210 map_domain_t *d;
1211 int domains = 0, rules = 0, domaincount = 0, rulecount = 0;
1212 if (pool_elts (mm->domains) == 0)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001213 vlib_cli_output (vm, "No MAP domains are configured...");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001214
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001215 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001216 pool_foreach(d, mm->domains, ({
1217 if (d->rules) {
1218 rulecount+= 0x1 << d->psid_length;
1219 rules += sizeof(ip6_address_t) * 0x1 << d->psid_length;
1220 }
1221 domains += sizeof(*d);
1222 domaincount++;
1223 }));
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001224 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001225
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001226 vlib_cli_output (vm, "MAP domains structure: %d\n", sizeof (map_domain_t));
1227 vlib_cli_output (vm, "MAP domains: %d (%d bytes)\n", domaincount, domains);
1228 vlib_cli_output (vm, "MAP rules: %d (%d bytes)\n", rulecount, rules);
1229 vlib_cli_output (vm, "Total: %d bytes)\n", rules + domains);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001230
1231#if MAP_SKIP_IP6_LOOKUP
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001232 vlib_cli_output (vm,
Neale Ranns80823802017-02-20 18:23:41 -08001233 "MAP pre-resolve: IP6 next-hop: %U, IP4 next-hop: %U\n",
1234 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP6],
1235 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP4]);
1236
Ed Warnickecb9cada2015-12-08 15:45:58 -07001237#endif
1238
1239 if (mm->tc_copy)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001240 vlib_cli_output (vm, "MAP traffic-class: copy");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001241 else
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001242 vlib_cli_output (vm, "MAP traffic-class: %x", mm->tc);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001243
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001244 vlib_cli_output (vm,
1245 "MAP IPv6 inbound security check: %s, fragmented packet security check: %s",
1246 mm->sec_check ? "enabled" : "disabled",
1247 mm->sec_check_frag ? "enabled" : "disabled");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001249 vlib_cli_output (vm, "ICMP-relay IPv4 source address: %U\n",
1250 format_ip4_address, &mm->icmp4_src_address);
1251 vlib_cli_output (vm, "ICMP6 unreachables sent for unmatched packets: %s\n",
1252 mm->icmp6_enabled ? "enabled" : "disabled");
1253 vlib_cli_output (vm, "Inner fragmentation: %s\n",
1254 mm->frag_inner ? "enabled" : "disabled");
1255 vlib_cli_output (vm, "Fragment packets regardless of DF flag: %s\n",
1256 mm->frag_ignore_df ? "enabled" : "disabled");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001257
1258 /*
1259 * Counters
1260 */
1261 vlib_combined_counter_main_t *cm = mm->domain_counters;
1262 u64 total_pkts[MAP_N_DOMAIN_COUNTER];
1263 u64 total_bytes[MAP_N_DOMAIN_COUNTER];
1264 int which, i;
1265 vlib_counter_t v;
1266
1267 memset (total_pkts, 0, sizeof (total_pkts));
1268 memset (total_bytes, 0, sizeof (total_bytes));
1269
1270 map_domain_counter_lock (mm);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001271 vec_foreach (cm, mm->domain_counters)
1272 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001273 which = cm - mm->domain_counters;
1274
Neale Ranns1bd01092017-03-15 15:41:17 -04001275 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001276 {
1277 vlib_get_combined_counter (cm, i, &v);
1278 total_pkts[which] += v.packets;
1279 total_bytes[which] += v.bytes;
1280 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001281 }
1282 map_domain_counter_unlock (mm);
1283
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001284 vlib_cli_output (vm, "Encapsulated packets: %lld bytes: %lld\n",
1285 total_pkts[MAP_DOMAIN_COUNTER_TX],
1286 total_bytes[MAP_DOMAIN_COUNTER_TX]);
1287 vlib_cli_output (vm, "Decapsulated packets: %lld bytes: %lld\n",
1288 total_pkts[MAP_DOMAIN_COUNTER_RX],
1289 total_bytes[MAP_DOMAIN_COUNTER_RX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001290
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001291 vlib_cli_output (vm, "ICMP relayed packets: %d\n",
1292 vlib_get_simple_counter (&mm->icmp_relayed, 0));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001293
1294 return 0;
1295}
1296
1297static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001298map_params_reass_command_fn (vlib_main_t * vm, unformat_input_t * input,
1299 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001300{
1301 unformat_input_t _line_input, *line_input = &_line_input;
1302 u32 lifetime = ~0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001303 f64 ht_ratio = (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001304 u32 pool_size = ~0;
1305 u64 buffers = ~(0ull);
1306 u8 ip4 = 0, ip6 = 0;
1307
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001308 if (!unformat_user (input, unformat_line_input, line_input))
1309 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001310
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001311 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1312 {
Ole Troan58a90a42016-08-04 10:19:17 +02001313 if (unformat (line_input, "lifetime %u", &lifetime))
1314 ;
1315 else if (unformat (line_input, "ht-ratio %lf", &ht_ratio))
1316 ;
1317 else if (unformat (line_input, "pool-size %u", &pool_size))
1318 ;
1319 else if (unformat (line_input, "buffers %llu", &buffers))
1320 ;
1321 else if (unformat (line_input, "ip4"))
1322 ip4 = 1;
1323 else if (unformat (line_input, "ip6"))
1324 ip6 = 1;
1325 else
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001326 {
1327 unformat_free (line_input);
1328 return clib_error_return (0, "invalid input");
1329 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001330 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001331 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001332
1333 if (!ip4 && !ip6)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001334 return clib_error_return (0, "must specify ip4 and/or ip6");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001335
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001336 if (ip4)
1337 {
1338 if (pool_size != ~0 && pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1339 return clib_error_return (0, "invalid ip4-reass pool-size ( > %d)",
1340 MAP_IP4_REASS_CONF_POOL_SIZE_MAX);
1341 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1342 && ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1343 return clib_error_return (0, "invalid ip4-reass ht-ratio ( > %d)",
1344 MAP_IP4_REASS_CONF_HT_RATIO_MAX);
1345 if (lifetime != ~0 && lifetime > MAP_IP4_REASS_CONF_LIFETIME_MAX)
1346 return clib_error_return (0, "invalid ip4-reass lifetime ( > %d)",
1347 MAP_IP4_REASS_CONF_LIFETIME_MAX);
1348 if (buffers != ~(0ull) && buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
1349 return clib_error_return (0, "invalid ip4-reass buffers ( > %ld)",
1350 MAP_IP4_REASS_CONF_BUFFERS_MAX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001351 }
1352
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001353 if (ip6)
1354 {
1355 if (pool_size != ~0 && pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1356 return clib_error_return (0, "invalid ip6-reass pool-size ( > %d)",
1357 MAP_IP6_REASS_CONF_POOL_SIZE_MAX);
1358 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1359 && ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1360 return clib_error_return (0, "invalid ip6-reass ht-log2len ( > %d)",
1361 MAP_IP6_REASS_CONF_HT_RATIO_MAX);
1362 if (lifetime != ~0 && lifetime > MAP_IP6_REASS_CONF_LIFETIME_MAX)
1363 return clib_error_return (0, "invalid ip6-reass lifetime ( > %d)",
1364 MAP_IP6_REASS_CONF_LIFETIME_MAX);
1365 if (buffers != ~(0ull) && buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
1366 return clib_error_return (0, "invalid ip6-reass buffers ( > %ld)",
1367 MAP_IP6_REASS_CONF_BUFFERS_MAX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001368 }
1369
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001370 if (ip4)
1371 {
1372 u32 reass = 0, packets = 0;
1373 if (pool_size != ~0)
1374 {
1375 if (map_ip4_reass_conf_pool_size (pool_size, &reass, &packets))
1376 {
1377 vlib_cli_output (vm, "Could not set ip4-reass pool-size");
1378 }
1379 else
1380 {
1381 vlib_cli_output (vm,
1382 "Setting ip4-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)",
1383 reass, packets);
1384 }
1385 }
1386 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
1387 {
1388 if (map_ip4_reass_conf_ht_ratio (ht_ratio, &reass, &packets))
1389 {
1390 vlib_cli_output (vm, "Could not set ip4-reass ht-log2len");
1391 }
1392 else
1393 {
1394 vlib_cli_output (vm,
1395 "Setting ip4-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)",
1396 reass, packets);
1397 }
1398 }
1399 if (lifetime != ~0)
1400 {
1401 if (map_ip4_reass_conf_lifetime (lifetime))
1402 vlib_cli_output (vm, "Could not set ip4-reass lifetime");
1403 else
1404 vlib_cli_output (vm, "Setting ip4-reass lifetime");
1405 }
1406 if (buffers != ~(0ull))
1407 {
1408 if (map_ip4_reass_conf_buffers (buffers))
1409 vlib_cli_output (vm, "Could not set ip4-reass buffers");
1410 else
1411 vlib_cli_output (vm, "Setting ip4-reass buffers");
1412 }
1413
1414 if (map_main.ip4_reass_conf_buffers >
1415 map_main.ip4_reass_conf_pool_size *
1416 MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
1417 {
1418 vlib_cli_output (vm,
1419 "Note: 'ip4-reass buffers' > pool-size * max-fragments-per-reassembly.");
1420 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001421 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001422
1423 if (ip6)
1424 {
1425 u32 reass = 0, packets = 0;
1426 if (pool_size != ~0)
1427 {
1428 if (map_ip6_reass_conf_pool_size (pool_size, &reass, &packets))
1429 {
1430 vlib_cli_output (vm, "Could not set ip6-reass pool-size");
1431 }
1432 else
1433 {
1434 vlib_cli_output (vm,
1435 "Setting ip6-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)",
1436 reass, packets);
1437 }
1438 }
1439 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
1440 {
1441 if (map_ip6_reass_conf_ht_ratio (ht_ratio, &reass, &packets))
1442 {
1443 vlib_cli_output (vm, "Could not set ip6-reass ht-log2len");
1444 }
1445 else
1446 {
1447 vlib_cli_output (vm,
1448 "Setting ip6-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)",
1449 reass, packets);
1450 }
1451 }
1452 if (lifetime != ~0)
1453 {
1454 if (map_ip6_reass_conf_lifetime (lifetime))
1455 vlib_cli_output (vm, "Could not set ip6-reass lifetime");
1456 else
1457 vlib_cli_output (vm, "Setting ip6-reass lifetime");
1458 }
1459 if (buffers != ~(0ull))
1460 {
1461 if (map_ip6_reass_conf_buffers (buffers))
1462 vlib_cli_output (vm, "Could not set ip6-reass buffers");
1463 else
1464 vlib_cli_output (vm, "Setting ip6-reass buffers");
1465 }
1466
1467 if (map_main.ip6_reass_conf_buffers >
1468 map_main.ip6_reass_conf_pool_size *
1469 MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
1470 {
1471 vlib_cli_output (vm,
1472 "Note: 'ip6-reass buffers' > pool-size * max-fragments-per-reassembly.");
1473 }
1474 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001475
1476 return 0;
1477}
1478
1479
1480/*
1481 * packet trace format function
1482 */
1483u8 *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001484format_map_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001485{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001486 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1487 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001488 map_trace_t *t = va_arg (*args, map_trace_t *);
1489 u32 map_domain_index = t->map_domain_index;
1490 u16 port = t->port;
1491
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001492 s =
1493 format (s, "MAP domain index: %d L4 port: %u", map_domain_index,
1494 clib_net_to_host_u16 (port));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001495
1496 return s;
1497}
1498
1499static_always_inline map_ip4_reass_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001500map_ip4_reass_lookup (map_ip4_reass_key_t * k, u32 bucket, f64 now)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001501{
1502 map_main_t *mm = &map_main;
1503 u32 ri = mm->ip4_reass_hash_table[bucket];
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001504 while (ri != MAP_REASS_INDEX_NONE)
1505 {
1506 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1507 if (r->key.as_u64[0] == k->as_u64[0] &&
1508 r->key.as_u64[1] == k->as_u64[1] &&
1509 now < r->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000))
1510 {
1511 return r;
1512 }
1513 ri = r->bucket_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001514 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001515 return NULL;
1516}
1517
1518#define map_ip4_reass_pool_index(r) (r - map_main.ip4_reass_pool)
1519
1520void
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001521map_ip4_reass_free (map_ip4_reass_t * r, u32 ** pi_to_drop)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001522{
1523 map_main_t *mm = &map_main;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001524 map_ip4_reass_get_fragments (r, pi_to_drop);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001525
1526 // Unlink in hash bucket
1527 map_ip4_reass_t *r2 = NULL;
1528 u32 r2i = mm->ip4_reass_hash_table[r->bucket];
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001529 while (r2i != map_ip4_reass_pool_index (r))
1530 {
1531 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1532 r2 = pool_elt_at_index (mm->ip4_reass_pool, r2i);
1533 r2i = r2->bucket_next;
1534 }
1535 if (r2)
1536 {
1537 r2->bucket_next = r->bucket_next;
1538 }
1539 else
1540 {
1541 mm->ip4_reass_hash_table[r->bucket] = r->bucket_next;
1542 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001543
1544 // Unlink in list
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001545 if (r->fifo_next == map_ip4_reass_pool_index (r))
1546 {
1547 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1548 }
1549 else
1550 {
1551 if (mm->ip4_reass_fifo_last == map_ip4_reass_pool_index (r))
1552 mm->ip4_reass_fifo_last = r->fifo_prev;
1553 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next =
1554 r->fifo_next;
1555 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev =
1556 r->fifo_prev;
1557 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001558
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001559 pool_put (mm->ip4_reass_pool, r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001560 mm->ip4_reass_allocated--;
1561}
1562
1563map_ip4_reass_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001564map_ip4_reass_get (u32 src, u32 dst, u16 fragment_id,
1565 u8 protocol, u32 ** pi_to_drop)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001566{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001567 map_ip4_reass_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001568 map_main_t *mm = &map_main;
1569 map_ip4_reass_key_t k = {.src.data_u32 = src,
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001570 .dst.data_u32 = dst,
1571 .fragment_id = fragment_id,
1572 .protocol = protocol
1573 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07001574
1575 u32 h = 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001576 h = crc_u32 (k.as_u32[0], h);
1577 h = crc_u32 (k.as_u32[1], h);
1578 h = crc_u32 (k.as_u32[2], h);
1579 h = crc_u32 (k.as_u32[3], h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001580 h = h >> (32 - mm->ip4_reass_ht_log2len);
1581
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001582 f64 now = vlib_time_now (mm->vlib_main);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001583
1584 //Cache garbage collection
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001585 while (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1586 {
1587 map_ip4_reass_t *last =
1588 pool_elt_at_index (mm->ip4_reass_pool, mm->ip4_reass_fifo_last);
1589 if (last->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000) < now)
1590 map_ip4_reass_free (last, pi_to_drop);
1591 else
1592 break;
1593 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001594
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001595 if ((r = map_ip4_reass_lookup (&k, h, now)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001596 return r;
1597
1598 if (mm->ip4_reass_allocated >= mm->ip4_reass_conf_pool_size)
1599 return NULL;
1600
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001601 pool_get (mm->ip4_reass_pool, r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001602 mm->ip4_reass_allocated++;
1603 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001604 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001605 r->fragments[i] = ~0;
1606
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001607 u32 ri = map_ip4_reass_pool_index (r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001608
1609 //Link in new bucket
1610 r->bucket = h;
1611 r->bucket_next = mm->ip4_reass_hash_table[h];
1612 mm->ip4_reass_hash_table[h] = ri;
1613
1614 //Link in fifo
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001615 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1616 {
1617 r->fifo_next =
1618 pool_elt_at_index (mm->ip4_reass_pool,
1619 mm->ip4_reass_fifo_last)->fifo_next;
1620 r->fifo_prev = mm->ip4_reass_fifo_last;
1621 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next = ri;
1622 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev = ri;
1623 }
1624 else
1625 {
1626 r->fifo_next = r->fifo_prev = ri;
1627 mm->ip4_reass_fifo_last = ri;
1628 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001629
1630 //Set other fields
1631 r->ts = now;
1632 r->key = k;
1633 r->port = -1;
1634#ifdef MAP_IP4_REASS_COUNT_BYTES
1635 r->expected_total = 0xffff;
1636 r->forwarded = 0;
1637#endif
1638
1639 return r;
1640}
1641
1642int
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001643map_ip4_reass_add_fragment (map_ip4_reass_t * r, u32 pi)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001644{
1645 if (map_main.ip4_reass_buffered_counter >= map_main.ip4_reass_conf_buffers)
1646 return -1;
1647
1648 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001649 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1650 if (r->fragments[i] == ~0)
1651 {
1652 r->fragments[i] = pi;
1653 map_main.ip4_reass_buffered_counter++;
1654 return 0;
1655 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001656 return -1;
1657}
1658
1659static_always_inline map_ip6_reass_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001660map_ip6_reass_lookup (map_ip6_reass_key_t * k, u32 bucket, f64 now)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001661{
1662 map_main_t *mm = &map_main;
1663 u32 ri = mm->ip6_reass_hash_table[bucket];
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001664 while (ri != MAP_REASS_INDEX_NONE)
1665 {
1666 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1667 if (now < r->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) &&
1668 r->key.as_u64[0] == k->as_u64[0] &&
1669 r->key.as_u64[1] == k->as_u64[1] &&
1670 r->key.as_u64[2] == k->as_u64[2] &&
1671 r->key.as_u64[3] == k->as_u64[3] &&
1672 r->key.as_u64[4] == k->as_u64[4])
1673 return r;
1674 ri = r->bucket_next;
1675 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001676 return NULL;
1677}
1678
1679#define map_ip6_reass_pool_index(r) (r - map_main.ip6_reass_pool)
1680
1681void
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001682map_ip6_reass_free (map_ip6_reass_t * r, u32 ** pi_to_drop)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001683{
1684 map_main_t *mm = &map_main;
1685 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001686 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1687 if (r->fragments[i].pi != ~0)
1688 {
1689 vec_add1 (*pi_to_drop, r->fragments[i].pi);
1690 r->fragments[i].pi = ~0;
1691 map_main.ip6_reass_buffered_counter--;
1692 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001693
1694 // Unlink in hash bucket
1695 map_ip6_reass_t *r2 = NULL;
1696 u32 r2i = mm->ip6_reass_hash_table[r->bucket];
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001697 while (r2i != map_ip6_reass_pool_index (r))
1698 {
1699 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1700 r2 = pool_elt_at_index (mm->ip6_reass_pool, r2i);
1701 r2i = r2->bucket_next;
1702 }
1703 if (r2)
1704 {
1705 r2->bucket_next = r->bucket_next;
1706 }
1707 else
1708 {
1709 mm->ip6_reass_hash_table[r->bucket] = r->bucket_next;
1710 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001711
1712 // Unlink in list
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001713 if (r->fifo_next == map_ip6_reass_pool_index (r))
1714 {
1715 //Single element in the list, list is now empty
1716 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1717 }
1718 else
1719 {
1720 if (mm->ip6_reass_fifo_last == map_ip6_reass_pool_index (r)) //First element
1721 mm->ip6_reass_fifo_last = r->fifo_prev;
1722 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next =
1723 r->fifo_next;
1724 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev =
1725 r->fifo_prev;
1726 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001727
1728 // Free from pool if necessary
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001729 pool_put (mm->ip6_reass_pool, r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001730 mm->ip6_reass_allocated--;
1731}
1732
1733map_ip6_reass_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001734map_ip6_reass_get (ip6_address_t * src, ip6_address_t * dst, u32 fragment_id,
1735 u8 protocol, u32 ** pi_to_drop)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001736{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001737 map_ip6_reass_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001738 map_main_t *mm = &map_main;
1739 map_ip6_reass_key_t k = {
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001740 .src = *src,
1741 .dst = *dst,
1742 .fragment_id = fragment_id,
1743 .protocol = protocol
1744 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07001745
1746 u32 h = 0;
1747 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001748 for (i = 0; i < 10; i++)
1749 h = crc_u32 (k.as_u32[i], h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001750 h = h >> (32 - mm->ip6_reass_ht_log2len);
1751
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001752 f64 now = vlib_time_now (mm->vlib_main);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001753
1754 //Cache garbage collection
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001755 while (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1756 {
1757 map_ip6_reass_t *last =
1758 pool_elt_at_index (mm->ip6_reass_pool, mm->ip6_reass_fifo_last);
1759 if (last->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) < now)
1760 map_ip6_reass_free (last, pi_to_drop);
1761 else
1762 break;
1763 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001764
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001765 if ((r = map_ip6_reass_lookup (&k, h, now)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001766 return r;
1767
1768 if (mm->ip6_reass_allocated >= mm->ip6_reass_conf_pool_size)
1769 return NULL;
1770
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001771 pool_get (mm->ip6_reass_pool, r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001772 mm->ip6_reass_allocated++;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001773 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1774 {
1775 r->fragments[i].pi = ~0;
1776 r->fragments[i].next_data_len = 0;
1777 r->fragments[i].next_data_offset = 0;
1778 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001779
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001780 u32 ri = map_ip6_reass_pool_index (r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001781
1782 //Link in new bucket
1783 r->bucket = h;
1784 r->bucket_next = mm->ip6_reass_hash_table[h];
1785 mm->ip6_reass_hash_table[h] = ri;
1786
1787 //Link in fifo
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001788 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1789 {
1790 r->fifo_next =
1791 pool_elt_at_index (mm->ip6_reass_pool,
1792 mm->ip6_reass_fifo_last)->fifo_next;
1793 r->fifo_prev = mm->ip6_reass_fifo_last;
1794 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next = ri;
1795 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev = ri;
1796 }
1797 else
1798 {
1799 r->fifo_next = r->fifo_prev = ri;
1800 mm->ip6_reass_fifo_last = ri;
1801 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001802
1803 //Set other fields
1804 r->ts = now;
1805 r->key = k;
1806 r->ip4_header.ip_version_and_header_length = 0;
1807#ifdef MAP_IP6_REASS_COUNT_BYTES
1808 r->expected_total = 0xffff;
1809 r->forwarded = 0;
1810#endif
1811 return r;
1812}
1813
1814int
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001815map_ip6_reass_add_fragment (map_ip6_reass_t * r, u32 pi,
1816 u16 data_offset, u16 next_data_offset,
1817 u8 * data_start, u16 data_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001818{
1819 map_ip6_fragment_t *f = NULL, *prev_f = NULL;
1820 u16 copied_len = (data_len > 20) ? 20 : data_len;
1821
1822 if (map_main.ip6_reass_buffered_counter >= map_main.ip6_reass_conf_buffers)
1823 return -1;
1824
1825 //Lookup for fragments for the current buffer
1826 //and the one before that
1827 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001828 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1829 {
1830 if (data_offset && r->fragments[i].next_data_offset == data_offset)
1831 {
1832 prev_f = &r->fragments[i]; // This is buffer for previous packet
1833 }
1834 else if (r->fragments[i].next_data_offset == next_data_offset)
1835 {
1836 f = &r->fragments[i]; // This is a buffer for the current packet
1837 }
1838 else if (r->fragments[i].next_data_offset == 0)
1839 { //Available
1840 if (f == NULL)
1841 f = &r->fragments[i];
1842 else if (prev_f == NULL)
1843 prev_f = &r->fragments[i];
1844 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001845 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001846
1847 if (!f || f->pi != ~0)
1848 return -1;
1849
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001850 if (data_offset)
1851 {
1852 if (!prev_f)
1853 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001854
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001855 clib_memcpy (prev_f->next_data, data_start, copied_len);
1856 prev_f->next_data_len = copied_len;
1857 prev_f->next_data_offset = data_offset;
1858 }
1859 else
1860 {
1861 if (((ip4_header_t *) data_start)->ip_version_and_header_length != 0x45)
1862 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001863
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001864 if (r->ip4_header.ip_version_and_header_length == 0)
1865 clib_memcpy (&r->ip4_header, data_start, sizeof (ip4_header_t));
1866 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001867
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001868 if (data_len > 20)
1869 {
1870 f->next_data_offset = next_data_offset;
1871 f->pi = pi;
1872 map_main.ip6_reass_buffered_counter++;
1873 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001874 return 0;
1875}
1876
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001877void
1878map_ip4_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001879{
1880 map_main_t *mm = &map_main;
1881 int i;
1882
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001883 if (dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001884 *dropped_packets = mm->ip4_reass_buffered_counter;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001885 if (trashed_reass)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001886 *trashed_reass = mm->ip4_reass_allocated;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001887 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1888 {
1889 u16 ri = mm->ip4_reass_fifo_last;
1890 do
1891 {
1892 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1893 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1894 if (r->fragments[i] != ~0)
1895 map_ip4_drop_pi (r->fragments[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001896
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001897 ri = r->fifo_next;
1898 pool_put (mm->ip4_reass_pool, r);
1899 }
1900 while (ri != mm->ip4_reass_fifo_last);
1901 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001902
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001903 vec_free (mm->ip4_reass_hash_table);
1904 vec_resize (mm->ip4_reass_hash_table, 1 << mm->ip4_reass_ht_log2len);
1905 for (i = 0; i < (1 << mm->ip4_reass_ht_log2len); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001906 mm->ip4_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001907 pool_free (mm->ip4_reass_pool);
1908 pool_alloc (mm->ip4_reass_pool, mm->ip4_reass_conf_pool_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001909
1910 mm->ip4_reass_allocated = 0;
1911 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1912 mm->ip4_reass_buffered_counter = 0;
1913}
1914
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001915u8
1916map_get_ht_log2len (f32 ht_ratio, u16 pool_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001917{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001918 u32 desired_size = (u32) (pool_size * ht_ratio);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001919 u8 i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001920 for (i = 1; i < 31; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001921 if ((1 << i) >= desired_size)
1922 return i;
1923 return 4;
1924}
1925
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001926int
1927map_ip4_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1928 u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001929{
1930 map_main_t *mm = &map_main;
1931 if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1932 return -1;
1933
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001934 map_ip4_reass_lock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001935 mm->ip4_reass_conf_ht_ratio = ht_ratio;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001936 mm->ip4_reass_ht_log2len =
1937 map_get_ht_log2len (ht_ratio, mm->ip4_reass_conf_pool_size);
1938 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1939 map_ip4_reass_unlock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001940 return 0;
1941}
1942
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001943int
1944map_ip4_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1945 u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001946{
1947 map_main_t *mm = &map_main;
1948 if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1949 return -1;
1950
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001951 map_ip4_reass_lock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001952 mm->ip4_reass_conf_pool_size = pool_size;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001953 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1954 map_ip4_reass_unlock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001955 return 0;
1956}
1957
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001958int
1959map_ip4_reass_conf_lifetime (u16 lifetime_ms)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001960{
1961 map_main.ip4_reass_conf_lifetime_ms = lifetime_ms;
1962 return 0;
1963}
1964
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001965int
1966map_ip4_reass_conf_buffers (u32 buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001967{
1968 map_main.ip4_reass_conf_buffers = buffers;
1969 return 0;
1970}
1971
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001972void
1973map_ip6_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001974{
1975 map_main_t *mm = &map_main;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001976 if (dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001977 *dropped_packets = mm->ip6_reass_buffered_counter;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001978 if (trashed_reass)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001979 *trashed_reass = mm->ip6_reass_allocated;
1980 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001981 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1982 {
1983 u16 ri = mm->ip6_reass_fifo_last;
1984 do
1985 {
1986 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1987 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1988 if (r->fragments[i].pi != ~0)
1989 map_ip6_drop_pi (r->fragments[i].pi);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001990
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001991 ri = r->fifo_next;
1992 pool_put (mm->ip6_reass_pool, r);
1993 }
1994 while (ri != mm->ip6_reass_fifo_last);
1995 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1996 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001997
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001998 vec_free (mm->ip6_reass_hash_table);
1999 vec_resize (mm->ip6_reass_hash_table, 1 << mm->ip6_reass_ht_log2len);
2000 for (i = 0; i < (1 << mm->ip6_reass_ht_log2len); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002001 mm->ip6_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002002 pool_free (mm->ip6_reass_pool);
2003 pool_alloc (mm->ip6_reass_pool, mm->ip4_reass_conf_pool_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002004
2005 mm->ip6_reass_allocated = 0;
2006 mm->ip6_reass_buffered_counter = 0;
2007}
2008
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002009int
2010map_ip6_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
2011 u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002012{
2013 map_main_t *mm = &map_main;
2014 if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
2015 return -1;
2016
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002017 map_ip6_reass_lock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07002018 mm->ip6_reass_conf_ht_ratio = ht_ratio;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002019 mm->ip6_reass_ht_log2len =
2020 map_get_ht_log2len (ht_ratio, mm->ip6_reass_conf_pool_size);
2021 map_ip6_reass_reinit (trashed_reass, dropped_packets);
2022 map_ip6_reass_unlock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07002023 return 0;
2024}
2025
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002026int
2027map_ip6_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
2028 u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002029{
2030 map_main_t *mm = &map_main;
2031 if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
2032 return -1;
2033
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002034 map_ip6_reass_lock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07002035 mm->ip6_reass_conf_pool_size = pool_size;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002036 map_ip6_reass_reinit (trashed_reass, dropped_packets);
2037 map_ip6_reass_unlock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07002038 return 0;
2039}
2040
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002041int
2042map_ip6_reass_conf_lifetime (u16 lifetime_ms)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002043{
2044 map_main.ip6_reass_conf_lifetime_ms = lifetime_ms;
2045 return 0;
2046}
2047
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002048int
2049map_ip6_reass_conf_buffers (u32 buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002050{
2051 map_main.ip6_reass_conf_buffers = buffers;
2052 return 0;
2053}
2054
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002055/* *INDENT-OFF* */
Ole Troan96eefd42016-09-12 12:25:30 +02002056
2057/*?
2058 * Configure MAP reassembly behaviour
2059 *
2060 * @cliexpar
2061 * @cliexstart{map params reassembly}
2062 * @cliexend
2063 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002064VLIB_CLI_COMMAND(map_ip4_reass_lifetime_command, static) = {
2065 .path = "map params reassembly",
Ole Troan96eefd42016-09-12 12:25:30 +02002066 .short_help = "map params reassembly [ip4 | ip6] [lifetime <lifetime-ms>] "
2067 "[pool-size <pool-size>] [buffers <buffers>] "
2068 "[ht-ratio <ht-ratio>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002069 .function = map_params_reass_command_fn,
2070};
2071
Ole Troan96eefd42016-09-12 12:25:30 +02002072/*?
2073 * Set or copy the IP TOS/Traffic Class field
2074 *
2075 * @cliexpar
2076 * @cliexstart{map params traffic-class}
2077 *
2078 * This command is used to set the traffic-class field in translated
2079 * or encapsulated packets. If copy is specifed (the default) then the
2080 * traffic-class/TOS field is copied from the original packet to the
2081 * translated / encapsulating header.
2082 * @cliexend
2083 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002084VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
2085 .path = "map params traffic-class",
Ole Troan96eefd42016-09-12 12:25:30 +02002086 .short_help = "map params traffic-class {0x0-0xff | copy}",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002087 .function = map_traffic_class_command_fn,
2088};
2089
Ole Troan96eefd42016-09-12 12:25:30 +02002090/*?
2091 * Bypass IP4/IP6 lookup
2092 *
2093 * @cliexpar
2094 * @cliexstart{map params pre-resolve}
2095 *
2096 * Bypass a second FIB lookup of the translated or encapsulated
2097 * packet, and forward the packet directly to the specified
2098 * next-hop. This optimization trades forwarding flexibility for
2099 * performance.
2100 * @cliexend
2101 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002102VLIB_CLI_COMMAND(map_pre_resolve_command, static) = {
2103 .path = "map params pre-resolve",
Ole Troan96eefd42016-09-12 12:25:30 +02002104 .short_help = " map params pre-resolve {ip4-nh <address>} "
2105 "| {ip6-nh <address>}",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002106 .function = map_pre_resolve_command_fn,
2107};
2108
Ole Troan96eefd42016-09-12 12:25:30 +02002109/*?
2110 * Enable or disable the MAP-E inbound security check
2111 *
2112 * @cliexpar
2113 * @cliexstart{map params security-check}
2114 *
2115 * By default, a decapsulated packet's IPv4 source address will be
2116 * verified against the outer header's IPv6 source address. Disabling
2117 * this feature will allow IPv4 source address spoofing.
2118 * @cliexend
2119 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002120VLIB_CLI_COMMAND(map_security_check_command, static) = {
2121 .path = "map params security-check",
Ole Troan96eefd42016-09-12 12:25:30 +02002122 .short_help = "map params security-check on|off",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002123 .function = map_security_check_command_fn,
2124};
2125
Ole Troan96eefd42016-09-12 12:25:30 +02002126/*?
2127 * Specifiy the IPv4 source address used for relayed ICMP error messages
2128 *
2129 * @cliexpar
2130 * @cliexstart{map params icmp source-address}
2131 *
2132 * This command specifies which IPv4 source address (must be local to
2133 * the system), that is used for relayed received IPv6 ICMP error
2134 * messages.
2135 * @cliexend
2136 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002137VLIB_CLI_COMMAND(map_icmp_relay_source_address_command, static) = {
Ole Troancda94822016-01-07 14:37:25 +01002138 .path = "map params icmp source-address",
Ole Troan96eefd42016-09-12 12:25:30 +02002139 .short_help = "map params icmp source-address <ip4-address>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002140 .function = map_icmp_relay_source_address_command_fn,
2141};
2142
Ole Troan96eefd42016-09-12 12:25:30 +02002143/*?
2144 * Send IPv6 ICMP unreachables
2145 *
2146 * @cliexpar
2147 * @cliexstart{map params icmp6 unreachables}
2148 *
2149 * Send IPv6 ICMP unreachable messages back if security check fails or
2150 * no MAP domain exists.
2151 * @cliexend
2152 ?*/
Ole Troancda94822016-01-07 14:37:25 +01002153VLIB_CLI_COMMAND(map_icmp_unreachables_command, static) = {
Ole Troan9fb87552016-01-13 22:30:43 +01002154 .path = "map params icmp6 unreachables",
Ole Troan96eefd42016-09-12 12:25:30 +02002155 .short_help = "map params icmp6 unreachables {on|off}",
Ole Troancda94822016-01-07 14:37:25 +01002156 .function = map_icmp_unreachables_command_fn,
2157};
2158
Ole Troan96eefd42016-09-12 12:25:30 +02002159/*?
2160 * Configure MAP fragmentation behaviour
2161 *
2162 * @cliexpar
2163 * @cliexstart{map params fragment}
2164 * @cliexend
2165 ?*/
Ole Troan9fb87552016-01-13 22:30:43 +01002166VLIB_CLI_COMMAND(map_fragment_command, static) = {
2167 .path = "map params fragment",
Ole Troan96eefd42016-09-12 12:25:30 +02002168 .short_help = "map params fragment inner|outer",
Ole Troan9fb87552016-01-13 22:30:43 +01002169 .function = map_fragment_command_fn,
2170};
2171
Ole Troan96eefd42016-09-12 12:25:30 +02002172/*?
2173 * Ignore the IPv4 Don't fragment bit
2174 *
2175 * @cliexpar
2176 * @cliexstart{map params fragment ignore-df}
2177 *
2178 * Allows fragmentation of the IPv4 packet even if the DF bit is
2179 * set. The choice between inner or outer fragmentation of tunnel
2180 * packets is complicated. The benefit of inner fragmentation is that
2181 * the ultimate endpoint must reassemble, instead of the tunnel
2182 * endpoint.
2183 * @cliexend
2184 ?*/
Ole Troan9fb87552016-01-13 22:30:43 +01002185VLIB_CLI_COMMAND(map_fragment_df_command, static) = {
2186 .path = "map params fragment ignore-df",
Ole Troan96eefd42016-09-12 12:25:30 +02002187 .short_help = "map params fragment ignore-df on|off",
Ole Troan9fb87552016-01-13 22:30:43 +01002188 .function = map_fragment_df_command_fn,
2189};
2190
Ole Troan96eefd42016-09-12 12:25:30 +02002191/*?
2192 * Specifiy if the inbound security check should be done on fragments
2193 *
2194 * @cliexpar
2195 * @cliexstart{map params security-check fragments}
2196 *
2197 * Typically the inbound on-decapsulation security check is only done
2198 * on the first packet. The packet that contains the L4
2199 * information. While a security check on every fragment is possible,
2200 * it has a cost. State must be created on the first fragment.
2201 * @cliexend
2202 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002203VLIB_CLI_COMMAND(map_security_check_frag_command, static) = {
2204 .path = "map params security-check fragments",
Ole Troan96eefd42016-09-12 12:25:30 +02002205 .short_help = "map params security-check fragments on|off",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002206 .function = map_security_check_frag_command_fn,
2207};
2208
Ole Troan96eefd42016-09-12 12:25:30 +02002209/*?
2210 * Add MAP domain
2211 *
2212 * @cliexpar
2213 * @cliexstart{map add domain}
2214 * @cliexend
2215 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002216VLIB_CLI_COMMAND(map_add_domain_command, static) = {
2217 .path = "map add domain",
Ole Troan96eefd42016-09-12 12:25:30 +02002218 .short_help = "map add domain ip4-pfx <ip4-pfx> ip6-pfx <ip6-pfx> "
2219 "ip6-src <ip6-pfx> ea-bits-len <n> psid-offset <n> psid-len <n> "
2220 "[map-t] [mtu <mtu>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002221 .function = map_add_domain_command_fn,
2222};
2223
Ole Troan96eefd42016-09-12 12:25:30 +02002224/*?
2225 * Add MAP rule to a domain
2226 *
2227 * @cliexpar
2228 * @cliexstart{map add rule}
2229 * @cliexend
2230 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002231VLIB_CLI_COMMAND(map_add_rule_command, static) = {
2232 .path = "map add rule",
Ole Troan96eefd42016-09-12 12:25:30 +02002233 .short_help = "map add rule index <domain> psid <psid> ip6-dst <ip6-addr>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002234 .function = map_add_rule_command_fn,
2235};
2236
Ole Troan96eefd42016-09-12 12:25:30 +02002237/*?
2238 * Delete MAP domain
2239 *
2240 * @cliexpar
2241 * @cliexstart{map del domain}
2242 * @cliexend
2243 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002244VLIB_CLI_COMMAND(map_del_command, static) = {
2245 .path = "map del domain",
Ole Troan96eefd42016-09-12 12:25:30 +02002246 .short_help = "map del domain index <domain>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002247 .function = map_del_domain_command_fn,
2248};
2249
Ole Troan96eefd42016-09-12 12:25:30 +02002250/*?
2251 * Show MAP domains
2252 *
2253 * @cliexpar
2254 * @cliexstart{show map domain}
2255 * @cliexend
2256 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002257VLIB_CLI_COMMAND(show_map_domain_command, static) = {
2258 .path = "show map domain",
Ole Troan96eefd42016-09-12 12:25:30 +02002259 .short_help = "show map domain index <n> [counters]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002260 .function = show_map_domain_command_fn,
2261};
2262
Ole Troan96eefd42016-09-12 12:25:30 +02002263/*?
2264 * Show MAP statistics
2265 *
2266 * @cliexpar
2267 * @cliexstart{show map stats}
2268 * @cliexend
2269 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002270VLIB_CLI_COMMAND(show_map_stats_command, static) = {
2271 .path = "show map stats",
Ole Troan96eefd42016-09-12 12:25:30 +02002272 .short_help = "show map stats",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002273 .function = show_map_stats_command_fn,
2274};
2275
Ole Troan96eefd42016-09-12 12:25:30 +02002276/*?
2277 * Show MAP fragmentation information
2278 *
2279 * @cliexpar
2280 * @cliexstart{show map fragments}
2281 * @cliexend
2282 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002283VLIB_CLI_COMMAND(show_map_fragments_command, static) = {
2284 .path = "show map fragments",
Ole Troan96eefd42016-09-12 12:25:30 +02002285 .short_help = "show map fragments",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002286 .function = show_map_fragments_command_fn,
2287};
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002288/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002289
2290/*
2291 * map_init
2292 */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002293clib_error_t *
2294map_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002295{
2296 map_main_t *mm = &map_main;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002297 mm->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07002298 mm->vlib_main = vm;
2299
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002300#ifdef MAP_SKIP_IP6_LOOKUP
Neale Ranns80823802017-02-20 18:23:41 -08002301 fib_protocol_t proto;
2302
2303 FOR_EACH_FIB_PROTOCOL (proto)
2304 {
2305 map_pre_resolve_init (&pre_resolved[proto]);
2306 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002307#endif
2308
2309 /* traffic class */
2310 mm->tc = 0;
2311 mm->tc_copy = true;
2312
2313 /* Inbound security check */
2314 mm->sec_check = true;
2315 mm->sec_check_frag = false;
2316
Ole Troancda94822016-01-07 14:37:25 +01002317 /* ICMP6 Type 1, Code 5 for security check failure */
2318 mm->icmp6_enabled = false;
2319
Ole Troan9fb87552016-01-13 22:30:43 +01002320 /* Inner or outer fragmentation */
2321 mm->frag_inner = false;
2322 mm->frag_ignore_df = false;
2323
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002324 vec_validate (mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002325 mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "rx";
2326 mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "tx";
2327
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002328 vlib_validate_simple_counter (&mm->icmp_relayed, 0);
2329 vlib_zero_simple_counter (&mm->icmp_relayed, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002330
2331 /* IP4 virtual reassembly */
2332 mm->ip4_reass_hash_table = 0;
2333 mm->ip4_reass_pool = 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002334 mm->ip4_reass_lock =
2335 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002336 mm->ip4_reass_conf_ht_ratio = MAP_IP4_REASS_HT_RATIO_DEFAULT;
2337 mm->ip4_reass_conf_lifetime_ms = MAP_IP4_REASS_LIFETIME_DEFAULT;
2338 mm->ip4_reass_conf_pool_size = MAP_IP4_REASS_POOL_SIZE_DEFAULT;
2339 mm->ip4_reass_conf_buffers = MAP_IP4_REASS_BUFFERS_DEFAULT;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002340 mm->ip4_reass_ht_log2len =
2341 map_get_ht_log2len (mm->ip4_reass_conf_ht_ratio,
2342 mm->ip4_reass_conf_pool_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002343 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002344 map_ip4_reass_reinit (NULL, NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002345
2346 /* IP6 virtual reassembly */
2347 mm->ip6_reass_hash_table = 0;
2348 mm->ip6_reass_pool = 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002349 mm->ip6_reass_lock =
2350 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002351 mm->ip6_reass_conf_ht_ratio = MAP_IP6_REASS_HT_RATIO_DEFAULT;
2352 mm->ip6_reass_conf_lifetime_ms = MAP_IP6_REASS_LIFETIME_DEFAULT;
2353 mm->ip6_reass_conf_pool_size = MAP_IP6_REASS_POOL_SIZE_DEFAULT;
2354 mm->ip6_reass_conf_buffers = MAP_IP6_REASS_BUFFERS_DEFAULT;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002355 mm->ip6_reass_ht_log2len =
2356 map_get_ht_log2len (mm->ip6_reass_conf_ht_ratio,
2357 mm->ip6_reass_conf_pool_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002358 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002359 map_ip6_reass_reinit (NULL, NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002360
Neale Ranns80823802017-02-20 18:23:41 -08002361#ifdef MAP_SKIP_IP6_LOOKUP
2362 fib_node_register_type (FIB_NODE_TYPE_MAP_E, &map_vft);
2363#endif
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002364 map_dpo_module_init ();
2365
Ed Warnickecb9cada2015-12-08 15:45:58 -07002366 return 0;
2367}
2368
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002369VLIB_INIT_FUNCTION (map_init);
2370
2371/*
2372 * fd.io coding-style-patch-verification: ON
2373 *
2374 * Local Variables:
2375 * eval: (c-set-style "gnu")
2376 * End:
2377 */