blob: 7006b1db8f190371abc11344ff231fad44a05631 [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 fib_node_index_t fei;
181 map_domain_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 /* Sanity check on the src prefix length */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700184 if (flags & MAP_DOMAIN_TRANSLATION)
185 {
186 if (ip6_src_len != 96)
187 {
188 clib_warning ("MAP-T only supports ip6_src_len = 96 for now.");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189 return -1;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700190 }
191 }
192 else
193 {
194 if (ip6_src_len != 128)
195 {
196 clib_warning
Ole Troan07e7eab2016-08-25 00:24:08 +0200197 ("MAP-E requires a BR address, not a prefix (ip6_src_len should "
198 "be 128).");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199 return -1;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700200 }
201 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202
Ole Troan07e7eab2016-08-25 00:24:08 +0200203 /* How many, and which bits to grab from the IPv4 DA */
204 if (ip4_prefix_len + ea_bits_len < 32)
205 {
206 flags |= MAP_DOMAIN_PREFIX;
Ole Troand575e692016-08-25 12:26:47 +0200207 suffix_shift = 32 - ip4_prefix_len - ea_bits_len;
208 suffix_len = ea_bits_len;
Ole Troan07e7eab2016-08-25 00:24:08 +0200209 }
210 else
211 {
212 suffix_shift = 0;
213 suffix_len = 32 - ip4_prefix_len;
214 }
215
216 /* EA bits must be within the first 64 bits */
217 if (ea_bits_len > 0 && ((ip6_prefix_len + ea_bits_len) > 64 ||
218 ip6_prefix_len + suffix_len + psid_length > 64))
219 {
220 clib_warning
221 ("Embedded Address bits must be within the first 64 bits of "
222 "the IPv6 prefix");
223 return -1;
224 }
225
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226 /* Get domain index */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700227 pool_get_aligned (mm->domains, d, CLIB_CACHE_LINE_BYTES);
228 memset (d, 0, sizeof (*d));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229 *map_domain_index = d - mm->domains;
230
231 /* Init domain struct */
232 d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
233 d->ip4_prefix_len = ip4_prefix_len;
234 d->ip6_prefix = *ip6_prefix;
235 d->ip6_prefix_len = ip6_prefix_len;
236 d->ip6_src = *ip6_src;
237 d->ip6_src_len = ip6_src_len;
238 d->ea_bits_len = ea_bits_len;
239 d->psid_offset = psid_offset;
240 d->psid_length = psid_length;
241 d->mtu = mtu;
242 d->flags = flags;
Ole Troan07e7eab2016-08-25 00:24:08 +0200243 d->suffix_shift = suffix_shift;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700244 d->suffix_mask = (1 << suffix_len) - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245
246 d->psid_shift = 16 - psid_length - psid_offset;
247 d->psid_mask = (1 << d->psid_length) - 1;
248 d->ea_shift = 64 - ip6_prefix_len - suffix_len - d->psid_length;
249
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100250 /* MAP data-plane object */
251 if (d->flags & MAP_DOMAIN_TRANSLATION)
252 map_t_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700253 else
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100254 map_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
255
256 /* Create ip4 route */
257 fib_prefix_t pfx = {
258 .fp_proto = FIB_PROTOCOL_IP4,
259 .fp_len = d->ip4_prefix_len,
260 .fp_addr = {
261 .ip4 = d->ip4_prefix,
262 }
263 ,
264 };
265 fib_table_entry_special_dpo_add (0, &pfx,
266 FIB_SOURCE_MAP,
267 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
268 dpo_reset (&dpo_v4);
269
270 /*
271 * Multiple MAP domains may share same source IPv6 TEP.
272 * In this case the route will exist and be MAP sourced.
273 * Find the adj (if any) already contributed and modify it
274 */
275 fib_prefix_t pfx6 = {
276 .fp_proto = FIB_PROTOCOL_IP6,
277 .fp_len = d->ip6_src_len,
278 .fp_addr = {
279 .ip6 = d->ip6_src,
280 }
281 ,
282 };
283 fei = fib_table_lookup_exact_match (0, &pfx6);
284
285 if (FIB_NODE_INDEX_INVALID != fei)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700286 {
Neale Ranns948e00f2016-10-20 13:39:34 +0100287 dpo_id_t dpo = DPO_INVALID;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100288
289 if (fib_entry_get_dpo_for_source (fei, FIB_SOURCE_MAP, &dpo))
290 {
291 /*
292 * modify the existing MAP to indicate it's shared
293 * skip to route add.
294 */
295 const dpo_id_t *md_dpo;
296 map_dpo_t *md;
297
298 ASSERT (DPO_LOAD_BALANCE == dpo.dpoi_type);
299
300 md_dpo = load_balance_get_bucket (dpo.dpoi_index, 0);
301 md = map_dpo_get (md_dpo->dpoi_index);
302
303 md->md_domain = ~0;
304 dpo_copy (&dpo_v6, md_dpo);
305 dpo_reset (&dpo);
306
307 goto route_add;
308 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700309 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100311 if (d->flags & MAP_DOMAIN_TRANSLATION)
312 map_t_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
313 else
314 map_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
315
316route_add:
317 /*
318 * Create ip6 route. This is a reference counted add. If the prefix
319 * already exists and is MAP sourced, it is now MAP source n+1 times
320 * and will need to be removed n+1 times.
321 */
322 fib_table_entry_special_dpo_add (0, &pfx6,
323 FIB_SOURCE_MAP,
324 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v6);
325 dpo_reset (&dpo_v6);
326
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327 /* Validate packet/byte counters */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700328 map_domain_counter_lock (mm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700330 for (i = 0; i < vec_len (mm->simple_domain_counters); i++)
331 {
332 vlib_validate_simple_counter (&mm->simple_domain_counters[i],
333 *map_domain_index);
334 vlib_zero_simple_counter (&mm->simple_domain_counters[i],
335 *map_domain_index);
336 }
337 for (i = 0; i < vec_len (mm->domain_counters); i++)
338 {
339 vlib_validate_combined_counter (&mm->domain_counters[i],
340 *map_domain_index);
341 vlib_zero_combined_counter (&mm->domain_counters[i], *map_domain_index);
342 }
343 map_domain_counter_unlock (mm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344
345 return 0;
346}
347
348/*
349 * map_delete_domain
350 */
351int
352map_delete_domain (u32 map_domain_index)
353{
354 map_main_t *mm = &map_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355 map_domain_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700357 if (pool_is_free_index (mm->domains, map_domain_index))
358 {
359 clib_warning ("MAP domain delete: domain does not exist: %d",
360 map_domain_index);
361 return -1;
362 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700364 d = pool_elt_at_index (mm->domains, map_domain_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100366 fib_prefix_t pfx = {
367 .fp_proto = FIB_PROTOCOL_IP4,
368 .fp_len = d->ip4_prefix_len,
369 .fp_addr = {
370 .ip4 = d->ip4_prefix,
371 }
372 ,
373 };
374 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_MAP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100376 fib_prefix_t pfx6 = {
377 .fp_proto = FIB_PROTOCOL_IP6,
378 .fp_len = d->ip6_src_len,
379 .fp_addr = {
380 .ip6 = d->ip6_src,
381 }
382 ,
383 };
384 fib_table_entry_special_remove (0, &pfx6, FIB_SOURCE_MAP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386 /* Deleting rules */
387 if (d->rules)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700388 clib_mem_free (d->rules);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700390 pool_put (mm->domains, d);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391
392 return 0;
393}
394
395int
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700396map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700397 u8 is_add)
398{
399 map_domain_t *d;
400 map_main_t *mm = &map_main;
401
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700402 if (pool_is_free_index (mm->domains, map_domain_index))
403 {
404 clib_warning ("MAP rule: domain does not exist: %d", map_domain_index);
405 return -1;
406 }
407 d = pool_elt_at_index (mm->domains, map_domain_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408
409 /* Rules are only used in 1:1 independent case */
410 if (d->ea_bits_len > 0)
411 return (-1);
412
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700413 if (!d->rules)
414 {
415 u32 l = (0x1 << d->psid_length) * sizeof (ip6_address_t);
416 d->rules = clib_mem_alloc_aligned (l, CLIB_CACHE_LINE_BYTES);
417 if (!d->rules)
418 return -1;
419 memset (d->rules, 0, l);
420 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700422 if (psid >= (0x1 << d->psid_length))
423 {
424 clib_warning ("MAP rule: PSID outside bounds: %d [%d]", psid,
425 0x1 << d->psid_length);
426 return -1;
427 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700429 if (is_add)
430 {
431 d->rules[psid] = *tep;
432 }
433 else
434 {
435 memset (&d->rules[psid], 0, sizeof (ip6_address_t));
436 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700437 return 0;
438}
439
440#ifdef MAP_SKIP_IP6_LOOKUP
Neale Ranns80823802017-02-20 18:23:41 -0800441/**
442 * Pre-resolvd per-protocol global next-hops
443 */
444map_main_pre_resolved_t pre_resolved[FIB_PROTOCOL_MAX];
445
446static void
447map_pre_resolve_init (map_main_pre_resolved_t * pr)
448{
449 pr->fei = FIB_NODE_INDEX_INVALID;
450 fib_node_init (&pr->node, FIB_NODE_TYPE_MAP_E);
451}
452
453static u8 *
454format_map_pre_resolve (u8 * s, va_list ap)
455{
456 map_main_pre_resolved_t *pr = va_arg (ap, map_main_pre_resolved_t *);
457
458 if (FIB_NODE_INDEX_INVALID != pr->fei)
459 {
460 fib_prefix_t pfx;
461
462 fib_entry_get_prefix (pr->fei, &pfx);
463
464 return (format (s, "%U (%u)",
465 format_ip46_address, &pfx.fp_addr, IP46_TYPE_ANY,
466 pr->dpo.dpoi_index));
467 }
468 else
469 {
470 return (format (s, "un-set"));
471 }
472}
473
474
475/**
476 * Function definition to inform the FIB node that its last lock has gone.
477 */
478static void
479map_last_lock_gone (fib_node_t * node)
480{
481 /*
482 * The MAP is a root of the graph. As such
483 * it never has children and thus is never locked.
484 */
485 ASSERT (0);
486}
487
488static map_main_pre_resolved_t *
489map_from_fib_node (fib_node_t * node)
490{
491#if (CLIB_DEBUG > 0)
492 ASSERT (FIB_NODE_TYPE_MAP_E == node->fn_type);
493#endif
494 return ((map_main_pre_resolved_t *)
495 (((char *) node) -
496 STRUCT_OFFSET_OF (map_main_pre_resolved_t, node)));
497}
498
499static void
500map_stack (map_main_pre_resolved_t * pr)
501{
502 const dpo_id_t *dpo;
503
504 dpo = fib_entry_contribute_ip_forwarding (pr->fei);
505
506 dpo_copy (&pr->dpo, dpo);
507}
508
509/**
510 * Function definition to backwalk a FIB node
511 */
512static fib_node_back_walk_rc_t
513map_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
514{
515 map_stack (map_from_fib_node (node));
516
517 return (FIB_NODE_BACK_WALK_CONTINUE);
518}
519
520/**
521 * Function definition to get a FIB node from its index
522 */
523static fib_node_t *
524map_fib_node_get (fib_node_index_t index)
525{
526 return (&pre_resolved[index].node);
527}
528
529/*
530 * Virtual function table registered by MPLS GRE tunnels
531 * for participation in the FIB object graph.
532 */
533const static fib_node_vft_t map_vft = {
534 .fnv_get = map_fib_node_get,
535 .fnv_last_lock = map_last_lock_gone,
536 .fnv_back_walk = map_back_walk,
537};
538
539static void
540map_fib_resolve (map_main_pre_resolved_t * pr,
541 fib_protocol_t proto, u8 len, const ip46_address_t * addr)
542{
543 fib_prefix_t pfx = {
544 .fp_proto = proto,
545 .fp_len = len,
546 .fp_addr = *addr,
547 };
548
549 pr->fei = fib_table_entry_special_add (0, // default fib
550 &pfx,
551 FIB_SOURCE_RR,
552 FIB_ENTRY_FLAG_NONE,
553 ADJ_INDEX_INVALID);
554 pr->sibling = fib_entry_child_add (pr->fei, FIB_NODE_TYPE_MAP_E, proto);
555 map_stack (pr);
556}
557
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558static void
Neale Ranns69b7aa42017-03-10 03:04:12 -0800559map_fib_unresolve (map_main_pre_resolved_t * pr,
560 fib_protocol_t proto, u8 len, const ip46_address_t * addr)
561{
562 fib_prefix_t pfx = {
563 .fp_proto = proto,
564 .fp_len = len,
565 .fp_addr = *addr,
566 };
567
568 fib_entry_child_remove (pr->fei, pr->sibling);
569
570 fib_table_entry_special_remove (0, // default fib
571 &pfx, FIB_SOURCE_RR);
572 dpo_reset (&pr->dpo);
573
574 pr->fei = FIB_NODE_INDEX_INVALID;
575 pr->sibling = FIB_NODE_INDEX_INVALID;
576}
577
578static void
579map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6, int is_del)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580{
Neale Ranns80823802017-02-20 18:23:41 -0800581 if (ip6 && (ip6->as_u64[0] != 0 || ip6->as_u64[1] != 0))
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700582 {
Neale Ranns80823802017-02-20 18:23:41 -0800583 ip46_address_t addr = {
584 .ip6 = *ip6,
585 };
Neale Ranns69b7aa42017-03-10 03:04:12 -0800586 if (is_del)
587 map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP6],
588 FIB_PROTOCOL_IP6, 128, &addr);
589 else
590 map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP6],
591 FIB_PROTOCOL_IP6, 128, &addr);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700592 }
Neale Ranns80823802017-02-20 18:23:41 -0800593 if (ip4 && (ip4->as_u32 != 0))
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700594 {
Neale Ranns80823802017-02-20 18:23:41 -0800595 ip46_address_t addr = {
596 .ip4 = *ip4,
597 };
Neale Ranns69b7aa42017-03-10 03:04:12 -0800598 if (is_del)
599 map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP4],
600 FIB_PROTOCOL_IP4, 32, &addr);
601 else
602 map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP4],
603 FIB_PROTOCOL_IP4, 32, &addr);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700604 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605}
606#endif
607
608static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700609map_security_check_command_fn (vlib_main_t * vm,
610 unformat_input_t * input,
611 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612{
613 unformat_input_t _line_input, *line_input = &_line_input;
614 map_main_t *mm = &map_main;
Billy McFalla9a20e72017-02-15 11:39:12 -0500615 clib_error_t *error = NULL;
616
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700618 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700619 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700620
621 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
622 {
623 if (unformat (line_input, "off"))
624 mm->sec_check = false;
625 else if (unformat (line_input, "on"))
626 mm->sec_check = true;
627 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500628 {
629 error = clib_error_return (0, "unknown input `%U'",
630 format_unformat_error, line_input);
631 goto done;
632 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700633 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500634
635done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700636 unformat_free (line_input);
Billy McFalla9a20e72017-02-15 11:39:12 -0500637
638 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639}
640
641static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700642map_security_check_frag_command_fn (vlib_main_t * vm,
643 unformat_input_t * input,
644 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645{
646 unformat_input_t _line_input, *line_input = &_line_input;
647 map_main_t *mm = &map_main;
Billy McFalla9a20e72017-02-15 11:39:12 -0500648 clib_error_t *error = NULL;
649
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700651 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700652 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700653
654 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
655 {
656 if (unformat (line_input, "off"))
657 mm->sec_check_frag = false;
658 else if (unformat (line_input, "on"))
659 mm->sec_check_frag = true;
660 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500661 {
662 error = clib_error_return (0, "unknown input `%U'",
663 format_unformat_error, line_input);
664 goto done;
665 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700666 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500667
668done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700669 unformat_free (line_input);
Billy McFalla9a20e72017-02-15 11:39:12 -0500670
671 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672}
673
674static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700675map_add_domain_command_fn (vlib_main_t * vm,
676 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677{
678 unformat_input_t _line_input, *line_input = &_line_input;
679 ip4_address_t ip4_prefix;
680 ip6_address_t ip6_prefix;
681 ip6_address_t ip6_src;
Dave Barach042ffb42016-08-12 09:26:47 -0400682 u32 ip6_prefix_len = 0, ip4_prefix_len = 0, map_domain_index, ip6_src_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 u32 num_m_args = 0;
684 /* Optional arguments */
Dave Barach042ffb42016-08-12 09:26:47 -0400685 u32 ea_bits_len = 0, psid_offset = 0, psid_length = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686 u32 mtu = 0;
687 u8 flags = 0;
688 ip6_src_len = 128;
Billy McFalla9a20e72017-02-15 11:39:12 -0500689 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690
691 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700692 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700694
695 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
696 {
697 if (unformat
698 (line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix,
699 &ip4_prefix_len))
700 num_m_args++;
701 else
702 if (unformat
703 (line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix,
704 &ip6_prefix_len))
705 num_m_args++;
706 else
707 if (unformat
708 (line_input, "ip6-src %U/%d", unformat_ip6_address, &ip6_src,
709 &ip6_src_len))
710 num_m_args++;
711 else
712 if (unformat
713 (line_input, "ip6-src %U", unformat_ip6_address, &ip6_src))
714 num_m_args++;
715 else if (unformat (line_input, "ea-bits-len %d", &ea_bits_len))
716 num_m_args++;
717 else if (unformat (line_input, "psid-offset %d", &psid_offset))
718 num_m_args++;
719 else if (unformat (line_input, "psid-len %d", &psid_length))
720 num_m_args++;
721 else if (unformat (line_input, "mtu %d", &mtu))
722 num_m_args++;
723 else if (unformat (line_input, "map-t"))
724 flags |= MAP_DOMAIN_TRANSLATION;
725 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500726 {
727 error = clib_error_return (0, "unknown input `%U'",
728 format_unformat_error, line_input);
729 goto done;
730 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700731 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732
733 if (num_m_args < 3)
Billy McFalla9a20e72017-02-15 11:39:12 -0500734 {
735 error = clib_error_return (0, "mandatory argument(s) missing");
736 goto done;
737 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700738
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700739 map_create_domain (&ip4_prefix, ip4_prefix_len,
740 &ip6_prefix, ip6_prefix_len, &ip6_src, ip6_src_len,
741 ea_bits_len, psid_offset, psid_length, &map_domain_index,
742 mtu, flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700743
Billy McFalla9a20e72017-02-15 11:39:12 -0500744done:
745 unformat_free (line_input);
746
747 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748}
749
750static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700751map_del_domain_command_fn (vlib_main_t * vm,
752 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753{
754 unformat_input_t _line_input, *line_input = &_line_input;
755 u32 num_m_args = 0;
756 u32 map_domain_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500757 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700758
759 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700760 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700762
763 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
764 {
765 if (unformat (line_input, "index %d", &map_domain_index))
766 num_m_args++;
767 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500768 {
769 error = clib_error_return (0, "unknown input `%U'",
770 format_unformat_error, line_input);
771 goto done;
772 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700773 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774
775 if (num_m_args != 1)
Billy McFalla9a20e72017-02-15 11:39:12 -0500776 {
777 error = clib_error_return (0, "mandatory argument(s) missing");
778 goto done;
779 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700781 map_delete_domain (map_domain_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782
Billy McFalla9a20e72017-02-15 11:39:12 -0500783done:
784 unformat_free (line_input);
785
786 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787}
788
789static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700790map_add_rule_command_fn (vlib_main_t * vm,
791 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700792{
793 unformat_input_t _line_input, *line_input = &_line_input;
794 ip6_address_t tep;
795 u32 num_m_args = 0;
Dave Barach042ffb42016-08-12 09:26:47 -0400796 u32 psid = 0, map_domain_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500797 clib_error_t *error = NULL;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700798
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700800 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801 return 0;
802
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700803 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
804 {
805 if (unformat (line_input, "index %d", &map_domain_index))
806 num_m_args++;
807 else if (unformat (line_input, "psid %d", &psid))
808 num_m_args++;
809 else
810 if (unformat (line_input, "ip6-dst %U", unformat_ip6_address, &tep))
811 num_m_args++;
812 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500813 {
814 error = clib_error_return (0, "unknown input `%U'",
815 format_unformat_error, line_input);
816 goto done;
817 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700818 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700819
820 if (num_m_args != 3)
Billy McFalla9a20e72017-02-15 11:39:12 -0500821 {
822 error = clib_error_return (0, "mandatory argument(s) missing");
823 goto done;
824 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700825
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700826 if (map_add_del_psid (map_domain_index, psid, &tep, 1) != 0)
827 {
Billy McFalla9a20e72017-02-15 11:39:12 -0500828 error = clib_error_return (0, "Failing to add Mapping Rule");
829 goto done;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700830 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500831
832done:
833 unformat_free (line_input);
834
835 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700836}
837
838#if MAP_SKIP_IP6_LOOKUP
839static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700840map_pre_resolve_command_fn (vlib_main_t * vm,
841 unformat_input_t * input,
842 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700843{
844 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns80823802017-02-20 18:23:41 -0800845 ip4_address_t ip4nh, *p_v4 = NULL;
846 ip6_address_t ip6nh, *p_v6 = NULL;
Billy McFalla9a20e72017-02-15 11:39:12 -0500847 clib_error_t *error = NULL;
Neale Ranns69b7aa42017-03-10 03:04:12 -0800848 int is_del = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700850 memset (&ip4nh, 0, sizeof (ip4nh));
851 memset (&ip6nh, 0, sizeof (ip6nh));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700852
853 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700854 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700855 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700856
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700857 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
858 {
859 if (unformat (line_input, "ip4-nh %U", unformat_ip4_address, &ip4nh))
Neale Ranns80823802017-02-20 18:23:41 -0800860 p_v4 = &ip4nh;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700861 else
862 if (unformat (line_input, "ip6-nh %U", unformat_ip6_address, &ip6nh))
Neale Ranns80823802017-02-20 18:23:41 -0800863 p_v6 = &ip6nh;
Neale Ranns69b7aa42017-03-10 03:04:12 -0800864 else if (unformat (line_input, "del"))
865 is_del = 1;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700866 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500867 {
868 error = clib_error_return (0, "unknown input `%U'",
869 format_unformat_error, line_input);
870 goto done;
871 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700872 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700873
Neale Ranns69b7aa42017-03-10 03:04:12 -0800874 map_pre_resolve (p_v4, p_v6, is_del);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700875
Billy McFalla9a20e72017-02-15 11:39:12 -0500876done:
877 unformat_free (line_input);
878
879 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880}
881#endif
882
883static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700884map_icmp_relay_source_address_command_fn (vlib_main_t * vm,
885 unformat_input_t * input,
886 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700887{
888 unformat_input_t _line_input, *line_input = &_line_input;
889 ip4_address_t icmp_src_address;
890 map_main_t *mm = &map_main;
Billy McFalla9a20e72017-02-15 11:39:12 -0500891 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892
Ole Troancda94822016-01-07 14:37:25 +0100893 mm->icmp4_src_address.as_u32 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700894
895 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700896 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700897 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700898
899 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
900 {
901 if (unformat
902 (line_input, "%U", unformat_ip4_address, &icmp_src_address))
903 mm->icmp4_src_address = icmp_src_address;
904 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500905 {
906 error = clib_error_return (0, "unknown input `%U'",
907 format_unformat_error, line_input);
908 goto done;
909 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700910 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500911
912done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700913 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914
Billy McFalla9a20e72017-02-15 11:39:12 -0500915 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916}
917
918static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700919map_icmp_unreachables_command_fn (vlib_main_t * vm,
920 unformat_input_t * input,
921 vlib_cli_command_t * cmd)
Ole Troancda94822016-01-07 14:37:25 +0100922{
923 unformat_input_t _line_input, *line_input = &_line_input;
924 map_main_t *mm = &map_main;
925 int num_m_args = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500926 clib_error_t *error = NULL;
Ole Troancda94822016-01-07 14:37:25 +0100927
928 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700929 if (!unformat_user (input, unformat_line_input, line_input))
Ole Troancda94822016-01-07 14:37:25 +0100930 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700931
932 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
933 {
934 num_m_args++;
935 if (unformat (line_input, "on"))
936 mm->icmp6_enabled = true;
937 else if (unformat (line_input, "off"))
938 mm->icmp6_enabled = false;
939 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500940 {
941 error = clib_error_return (0, "unknown input `%U'",
942 format_unformat_error, line_input);
943 goto done;
944 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700945 }
Ole Troancda94822016-01-07 14:37:25 +0100946
947
948 if (num_m_args != 1)
Billy McFalla9a20e72017-02-15 11:39:12 -0500949 error = clib_error_return (0, "mandatory argument(s) missing");
Ole Troancda94822016-01-07 14:37:25 +0100950
Billy McFalla9a20e72017-02-15 11:39:12 -0500951done:
952 unformat_free (line_input);
953
954 return error;
Ole Troancda94822016-01-07 14:37:25 +0100955}
956
957static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700958map_fragment_command_fn (vlib_main_t * vm,
959 unformat_input_t * input, vlib_cli_command_t * cmd)
Ole Troan9fb87552016-01-13 22:30:43 +0100960{
961 unformat_input_t _line_input, *line_input = &_line_input;
962 map_main_t *mm = &map_main;
Billy McFalla9a20e72017-02-15 11:39:12 -0500963 clib_error_t *error = NULL;
Ole Troan9fb87552016-01-13 22:30:43 +0100964
965 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700966 if (!unformat_user (input, unformat_line_input, line_input))
Ole Troan9fb87552016-01-13 22:30:43 +0100967 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700968
969 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
970 {
971 if (unformat (line_input, "inner"))
972 mm->frag_inner = true;
973 else if (unformat (line_input, "outer"))
974 mm->frag_inner = false;
975 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500976 {
977 error = clib_error_return (0, "unknown input `%U'",
978 format_unformat_error, line_input);
979 goto done;
980 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700981 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500982
983done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700984 unformat_free (line_input);
Ole Troan9fb87552016-01-13 22:30:43 +0100985
Billy McFalla9a20e72017-02-15 11:39:12 -0500986 return error;
Ole Troan9fb87552016-01-13 22:30:43 +0100987}
988
989static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700990map_fragment_df_command_fn (vlib_main_t * vm,
991 unformat_input_t * input,
992 vlib_cli_command_t * cmd)
Ole Troan9fb87552016-01-13 22:30:43 +0100993{
994 unformat_input_t _line_input, *line_input = &_line_input;
995 map_main_t *mm = &map_main;
Billy McFalla9a20e72017-02-15 11:39:12 -0500996 clib_error_t *error = NULL;
Ole Troan9fb87552016-01-13 22:30:43 +0100997
998 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700999 if (!unformat_user (input, unformat_line_input, line_input))
Ole Troan9fb87552016-01-13 22:30:43 +01001000 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001001
1002 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1003 {
1004 if (unformat (line_input, "on"))
1005 mm->frag_ignore_df = true;
1006 else if (unformat (line_input, "off"))
1007 mm->frag_ignore_df = false;
1008 else
Billy McFalla9a20e72017-02-15 11:39:12 -05001009 {
1010 error = clib_error_return (0, "unknown input `%U'",
1011 format_unformat_error, line_input);
1012 goto done;
1013 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001014 }
Billy McFalla9a20e72017-02-15 11:39:12 -05001015
1016done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001017 unformat_free (line_input);
Ole Troan9fb87552016-01-13 22:30:43 +01001018
Billy McFalla9a20e72017-02-15 11:39:12 -05001019 return error;
Ole Troan9fb87552016-01-13 22:30:43 +01001020}
1021
1022static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001023map_traffic_class_command_fn (vlib_main_t * vm,
1024 unformat_input_t * input,
1025 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001026{
1027 unformat_input_t _line_input, *line_input = &_line_input;
1028 map_main_t *mm = &map_main;
1029 u32 tc = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -05001030 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001031
1032 mm->tc_copy = false;
1033
1034 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001035 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001036 return 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001037
1038 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1039 {
1040 if (unformat (line_input, "copy"))
1041 mm->tc_copy = true;
1042 else if (unformat (line_input, "%x", &tc))
1043 mm->tc = tc & 0xff;
1044 else
Billy McFalla9a20e72017-02-15 11:39:12 -05001045 {
1046 error = clib_error_return (0, "unknown input `%U'",
1047 format_unformat_error, line_input);
1048 goto done;
1049 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001050 }
Billy McFalla9a20e72017-02-15 11:39:12 -05001051
1052done:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001053 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001054
Billy McFalla9a20e72017-02-15 11:39:12 -05001055 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001056}
1057
1058static u8 *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001059format_map_domain (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001060{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001061 map_domain_t *d = va_arg (*args, map_domain_t *);
1062 bool counters = va_arg (*args, int);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001063 map_main_t *mm = &map_main;
1064 ip6_address_t ip6_prefix;
1065
1066 if (d->rules)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001067 memset (&ip6_prefix, 0, sizeof (ip6_prefix));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001068 else
1069 ip6_prefix = d->ip6_prefix;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001070
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001071 s = format (s,
1072 "[%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",
1073 d - mm->domains,
1074 format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
1075 format_ip6_address, &ip6_prefix, d->ip6_prefix_len,
1076 format_ip6_address, &d->ip6_src, d->ip6_src_len,
1077 d->ea_bits_len, d->psid_offset, d->psid_length, d->mtu,
1078 (d->flags & MAP_DOMAIN_TRANSLATION) ? "map-t" : "");
Ole Troan366ac6e2016-01-06 12:40:28 +01001079
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001080 if (counters)
1081 {
1082 map_domain_counter_lock (mm);
1083 vlib_counter_t v;
1084 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_TX],
1085 d - mm->domains, &v);
1086 s = format (s, " TX: %lld/%lld", v.packets, v.bytes);
1087 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_RX],
1088 d - mm->domains, &v);
1089 s = format (s, " RX: %lld/%lld", v.packets, v.bytes);
1090 map_domain_counter_unlock (mm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001091 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001092 s = format (s, "\n");
1093
1094 if (d->rules)
1095 {
1096 int i;
1097 ip6_address_t dst;
1098 for (i = 0; i < (0x1 << d->psid_length); i++)
1099 {
1100 dst = d->rules[i];
1101 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
1102 continue;
1103 s = format (s,
1104 " rule psid: %d ip6-dst %U\n", i, format_ip6_address,
1105 &dst);
1106 }
1107 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001108 return s;
1109}
1110
1111static u8 *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001112format_map_ip4_reass (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001113{
1114 map_main_t *mm = &map_main;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001115 map_ip4_reass_t *r = va_arg (*args, map_ip4_reass_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001116 map_ip4_reass_key_t *k = &r->key;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001117 f64 now = vlib_time_now (mm->vlib_main);
1118 f64 lifetime = (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001120 s = format (s,
1121 "ip4-reass src=%U dst=%U protocol=%d identifier=%d port=%d lifetime=%.3lf\n",
1122 format_ip4_address, &k->src.as_u8, format_ip4_address,
1123 &k->dst.as_u8, k->protocol,
1124 clib_net_to_host_u16 (k->fragment_id),
1125 (r->port >= 0) ? clib_net_to_host_u16 (r->port) : -1, dt);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001126 return s;
1127}
1128
1129static u8 *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001130format_map_ip6_reass (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131{
1132 map_main_t *mm = &map_main;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001133 map_ip6_reass_t *r = va_arg (*args, map_ip6_reass_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001134 map_ip6_reass_key_t *k = &r->key;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001135 f64 now = vlib_time_now (mm->vlib_main);
1136 f64 lifetime = (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001137 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001138 s = format (s,
1139 "ip6-reass src=%U dst=%U protocol=%d identifier=%d lifetime=%.3lf\n",
1140 format_ip6_address, &k->src.as_u8, format_ip6_address,
1141 &k->dst.as_u8, k->protocol,
1142 clib_net_to_host_u32 (k->fragment_id), dt);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001143 return s;
1144}
1145
1146static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001147show_map_domain_command_fn (vlib_main_t * vm, unformat_input_t * input,
1148 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001149{
1150 unformat_input_t _line_input, *line_input = &_line_input;
1151 map_main_t *mm = &map_main;
1152 map_domain_t *d;
1153 bool counters = false;
1154 u32 map_domain_index = ~0;
Billy McFalla9a20e72017-02-15 11:39:12 -05001155 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001156
1157 /* Get a line of input. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001158 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001159 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001160
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001161 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1162 {
1163 if (unformat (line_input, "counters"))
1164 counters = true;
1165 else if (unformat (line_input, "index %d", &map_domain_index))
1166 ;
1167 else
Billy McFalla9a20e72017-02-15 11:39:12 -05001168 {
1169 error = clib_error_return (0, "unknown input `%U'",
1170 format_unformat_error, line_input);
1171 goto done;
1172 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001173 }
1174
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001175 if (pool_elts (mm->domains) == 0)
1176 vlib_cli_output (vm, "No MAP domains are configured...");
1177
1178 if (map_domain_index == ~0)
1179 {
1180 /* *INDENT-OFF* */
1181 pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
1182 /* *INDENT-ON* */
1183 }
1184 else
1185 {
1186 if (pool_is_free_index (mm->domains, map_domain_index))
1187 {
Billy McFalla9a20e72017-02-15 11:39:12 -05001188 error = clib_error_return (0, "MAP domain does not exists %d",
1189 map_domain_index);
1190 goto done;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001191 }
1192
1193 d = pool_elt_at_index (mm->domains, map_domain_index);
1194 vlib_cli_output (vm, "%U", format_map_domain, d, counters);
1195 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001196
Billy McFalla9a20e72017-02-15 11:39:12 -05001197done:
1198 unformat_free (line_input);
1199
1200 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001201}
1202
1203static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001204show_map_fragments_command_fn (vlib_main_t * vm, unformat_input_t * input,
1205 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001206{
1207 map_main_t *mm = &map_main;
1208 map_ip4_reass_t *f4;
1209 map_ip6_reass_t *f6;
1210
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001211 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001212 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 -07001213 /* *INDENT-ON* */
1214 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001215 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 -07001216 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001217 return (0);
1218}
1219
1220u64
1221map_error_counter_get (u32 node_index, map_error_t map_error)
1222{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001223 vlib_main_t *vm = vlib_get_main ();
1224 vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001225 vlib_error_main_t *em = &vm->error_main;
1226 vlib_error_t e = error_node->errors[map_error];
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001227 vlib_node_t *n = vlib_get_node (vm, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001228 u32 ci;
1229
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001230 ci = vlib_error_get_code (e);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001231 ASSERT (ci < n->n_errors);
1232 ci += n->error_heap_index;
1233
1234 return (em->counters[ci]);
1235}
1236
1237static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001238show_map_stats_command_fn (vlib_main_t * vm, unformat_input_t * input,
1239 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001240{
1241 map_main_t *mm = &map_main;
1242 map_domain_t *d;
1243 int domains = 0, rules = 0, domaincount = 0, rulecount = 0;
1244 if (pool_elts (mm->domains) == 0)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001245 vlib_cli_output (vm, "No MAP domains are configured...");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001246
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001247 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248 pool_foreach(d, mm->domains, ({
1249 if (d->rules) {
1250 rulecount+= 0x1 << d->psid_length;
1251 rules += sizeof(ip6_address_t) * 0x1 << d->psid_length;
1252 }
1253 domains += sizeof(*d);
1254 domaincount++;
1255 }));
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001256 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001257
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001258 vlib_cli_output (vm, "MAP domains structure: %d\n", sizeof (map_domain_t));
1259 vlib_cli_output (vm, "MAP domains: %d (%d bytes)\n", domaincount, domains);
1260 vlib_cli_output (vm, "MAP rules: %d (%d bytes)\n", rulecount, rules);
1261 vlib_cli_output (vm, "Total: %d bytes)\n", rules + domains);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001262
1263#if MAP_SKIP_IP6_LOOKUP
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001264 vlib_cli_output (vm,
Neale Ranns80823802017-02-20 18:23:41 -08001265 "MAP pre-resolve: IP6 next-hop: %U, IP4 next-hop: %U\n",
1266 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP6],
1267 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP4]);
1268
Ed Warnickecb9cada2015-12-08 15:45:58 -07001269#endif
1270
1271 if (mm->tc_copy)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001272 vlib_cli_output (vm, "MAP traffic-class: copy");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001273 else
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001274 vlib_cli_output (vm, "MAP traffic-class: %x", mm->tc);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001275
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001276 vlib_cli_output (vm,
1277 "MAP IPv6 inbound security check: %s, fragmented packet security check: %s",
1278 mm->sec_check ? "enabled" : "disabled",
1279 mm->sec_check_frag ? "enabled" : "disabled");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001280
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001281 vlib_cli_output (vm, "ICMP-relay IPv4 source address: %U\n",
1282 format_ip4_address, &mm->icmp4_src_address);
1283 vlib_cli_output (vm, "ICMP6 unreachables sent for unmatched packets: %s\n",
1284 mm->icmp6_enabled ? "enabled" : "disabled");
1285 vlib_cli_output (vm, "Inner fragmentation: %s\n",
1286 mm->frag_inner ? "enabled" : "disabled");
1287 vlib_cli_output (vm, "Fragment packets regardless of DF flag: %s\n",
1288 mm->frag_ignore_df ? "enabled" : "disabled");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001289
1290 /*
1291 * Counters
1292 */
1293 vlib_combined_counter_main_t *cm = mm->domain_counters;
1294 u64 total_pkts[MAP_N_DOMAIN_COUNTER];
1295 u64 total_bytes[MAP_N_DOMAIN_COUNTER];
1296 int which, i;
1297 vlib_counter_t v;
1298
1299 memset (total_pkts, 0, sizeof (total_pkts));
1300 memset (total_bytes, 0, sizeof (total_bytes));
1301
1302 map_domain_counter_lock (mm);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001303 vec_foreach (cm, mm->domain_counters)
1304 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001305 which = cm - mm->domain_counters;
1306
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001307 for (i = 0; i < vec_len (cm->maxi); i++)
1308 {
1309 vlib_get_combined_counter (cm, i, &v);
1310 total_pkts[which] += v.packets;
1311 total_bytes[which] += v.bytes;
1312 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001313 }
1314 map_domain_counter_unlock (mm);
1315
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001316 vlib_cli_output (vm, "Encapsulated packets: %lld bytes: %lld\n",
1317 total_pkts[MAP_DOMAIN_COUNTER_TX],
1318 total_bytes[MAP_DOMAIN_COUNTER_TX]);
1319 vlib_cli_output (vm, "Decapsulated packets: %lld bytes: %lld\n",
1320 total_pkts[MAP_DOMAIN_COUNTER_RX],
1321 total_bytes[MAP_DOMAIN_COUNTER_RX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001322
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001323 vlib_cli_output (vm, "ICMP relayed packets: %d\n",
1324 vlib_get_simple_counter (&mm->icmp_relayed, 0));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001325
1326 return 0;
1327}
1328
1329static clib_error_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001330map_params_reass_command_fn (vlib_main_t * vm, unformat_input_t * input,
1331 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001332{
1333 unformat_input_t _line_input, *line_input = &_line_input;
1334 u32 lifetime = ~0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001335 f64 ht_ratio = (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001336 u32 pool_size = ~0;
1337 u64 buffers = ~(0ull);
1338 u8 ip4 = 0, ip6 = 0;
1339
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001340 if (!unformat_user (input, unformat_line_input, line_input))
1341 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001342
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001343 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1344 {
Ole Troan58a90a42016-08-04 10:19:17 +02001345 if (unformat (line_input, "lifetime %u", &lifetime))
1346 ;
1347 else if (unformat (line_input, "ht-ratio %lf", &ht_ratio))
1348 ;
1349 else if (unformat (line_input, "pool-size %u", &pool_size))
1350 ;
1351 else if (unformat (line_input, "buffers %llu", &buffers))
1352 ;
1353 else if (unformat (line_input, "ip4"))
1354 ip4 = 1;
1355 else if (unformat (line_input, "ip6"))
1356 ip6 = 1;
1357 else
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001358 {
1359 unformat_free (line_input);
1360 return clib_error_return (0, "invalid input");
1361 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001362 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001363 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001364
1365 if (!ip4 && !ip6)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001366 return clib_error_return (0, "must specify ip4 and/or ip6");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001367
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001368 if (ip4)
1369 {
1370 if (pool_size != ~0 && pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1371 return clib_error_return (0, "invalid ip4-reass pool-size ( > %d)",
1372 MAP_IP4_REASS_CONF_POOL_SIZE_MAX);
1373 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1374 && ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1375 return clib_error_return (0, "invalid ip4-reass ht-ratio ( > %d)",
1376 MAP_IP4_REASS_CONF_HT_RATIO_MAX);
1377 if (lifetime != ~0 && lifetime > MAP_IP4_REASS_CONF_LIFETIME_MAX)
1378 return clib_error_return (0, "invalid ip4-reass lifetime ( > %d)",
1379 MAP_IP4_REASS_CONF_LIFETIME_MAX);
1380 if (buffers != ~(0ull) && buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
1381 return clib_error_return (0, "invalid ip4-reass buffers ( > %ld)",
1382 MAP_IP4_REASS_CONF_BUFFERS_MAX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001383 }
1384
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001385 if (ip6)
1386 {
1387 if (pool_size != ~0 && pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1388 return clib_error_return (0, "invalid ip6-reass pool-size ( > %d)",
1389 MAP_IP6_REASS_CONF_POOL_SIZE_MAX);
1390 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1391 && ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1392 return clib_error_return (0, "invalid ip6-reass ht-log2len ( > %d)",
1393 MAP_IP6_REASS_CONF_HT_RATIO_MAX);
1394 if (lifetime != ~0 && lifetime > MAP_IP6_REASS_CONF_LIFETIME_MAX)
1395 return clib_error_return (0, "invalid ip6-reass lifetime ( > %d)",
1396 MAP_IP6_REASS_CONF_LIFETIME_MAX);
1397 if (buffers != ~(0ull) && buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
1398 return clib_error_return (0, "invalid ip6-reass buffers ( > %ld)",
1399 MAP_IP6_REASS_CONF_BUFFERS_MAX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001400 }
1401
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001402 if (ip4)
1403 {
1404 u32 reass = 0, packets = 0;
1405 if (pool_size != ~0)
1406 {
1407 if (map_ip4_reass_conf_pool_size (pool_size, &reass, &packets))
1408 {
1409 vlib_cli_output (vm, "Could not set ip4-reass pool-size");
1410 }
1411 else
1412 {
1413 vlib_cli_output (vm,
1414 "Setting ip4-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)",
1415 reass, packets);
1416 }
1417 }
1418 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
1419 {
1420 if (map_ip4_reass_conf_ht_ratio (ht_ratio, &reass, &packets))
1421 {
1422 vlib_cli_output (vm, "Could not set ip4-reass ht-log2len");
1423 }
1424 else
1425 {
1426 vlib_cli_output (vm,
1427 "Setting ip4-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)",
1428 reass, packets);
1429 }
1430 }
1431 if (lifetime != ~0)
1432 {
1433 if (map_ip4_reass_conf_lifetime (lifetime))
1434 vlib_cli_output (vm, "Could not set ip4-reass lifetime");
1435 else
1436 vlib_cli_output (vm, "Setting ip4-reass lifetime");
1437 }
1438 if (buffers != ~(0ull))
1439 {
1440 if (map_ip4_reass_conf_buffers (buffers))
1441 vlib_cli_output (vm, "Could not set ip4-reass buffers");
1442 else
1443 vlib_cli_output (vm, "Setting ip4-reass buffers");
1444 }
1445
1446 if (map_main.ip4_reass_conf_buffers >
1447 map_main.ip4_reass_conf_pool_size *
1448 MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
1449 {
1450 vlib_cli_output (vm,
1451 "Note: 'ip4-reass buffers' > pool-size * max-fragments-per-reassembly.");
1452 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001453 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001454
1455 if (ip6)
1456 {
1457 u32 reass = 0, packets = 0;
1458 if (pool_size != ~0)
1459 {
1460 if (map_ip6_reass_conf_pool_size (pool_size, &reass, &packets))
1461 {
1462 vlib_cli_output (vm, "Could not set ip6-reass pool-size");
1463 }
1464 else
1465 {
1466 vlib_cli_output (vm,
1467 "Setting ip6-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)",
1468 reass, packets);
1469 }
1470 }
1471 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
1472 {
1473 if (map_ip6_reass_conf_ht_ratio (ht_ratio, &reass, &packets))
1474 {
1475 vlib_cli_output (vm, "Could not set ip6-reass ht-log2len");
1476 }
1477 else
1478 {
1479 vlib_cli_output (vm,
1480 "Setting ip6-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)",
1481 reass, packets);
1482 }
1483 }
1484 if (lifetime != ~0)
1485 {
1486 if (map_ip6_reass_conf_lifetime (lifetime))
1487 vlib_cli_output (vm, "Could not set ip6-reass lifetime");
1488 else
1489 vlib_cli_output (vm, "Setting ip6-reass lifetime");
1490 }
1491 if (buffers != ~(0ull))
1492 {
1493 if (map_ip6_reass_conf_buffers (buffers))
1494 vlib_cli_output (vm, "Could not set ip6-reass buffers");
1495 else
1496 vlib_cli_output (vm, "Setting ip6-reass buffers");
1497 }
1498
1499 if (map_main.ip6_reass_conf_buffers >
1500 map_main.ip6_reass_conf_pool_size *
1501 MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
1502 {
1503 vlib_cli_output (vm,
1504 "Note: 'ip6-reass buffers' > pool-size * max-fragments-per-reassembly.");
1505 }
1506 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001507
1508 return 0;
1509}
1510
1511
1512/*
1513 * packet trace format function
1514 */
1515u8 *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001516format_map_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001517{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001518 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1519 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001520 map_trace_t *t = va_arg (*args, map_trace_t *);
1521 u32 map_domain_index = t->map_domain_index;
1522 u16 port = t->port;
1523
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001524 s =
1525 format (s, "MAP domain index: %d L4 port: %u", map_domain_index,
1526 clib_net_to_host_u16 (port));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001527
1528 return s;
1529}
1530
1531static_always_inline map_ip4_reass_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001532map_ip4_reass_lookup (map_ip4_reass_key_t * k, u32 bucket, f64 now)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001533{
1534 map_main_t *mm = &map_main;
1535 u32 ri = mm->ip4_reass_hash_table[bucket];
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001536 while (ri != MAP_REASS_INDEX_NONE)
1537 {
1538 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1539 if (r->key.as_u64[0] == k->as_u64[0] &&
1540 r->key.as_u64[1] == k->as_u64[1] &&
1541 now < r->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000))
1542 {
1543 return r;
1544 }
1545 ri = r->bucket_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001546 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001547 return NULL;
1548}
1549
1550#define map_ip4_reass_pool_index(r) (r - map_main.ip4_reass_pool)
1551
1552void
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001553map_ip4_reass_free (map_ip4_reass_t * r, u32 ** pi_to_drop)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001554{
1555 map_main_t *mm = &map_main;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001556 map_ip4_reass_get_fragments (r, pi_to_drop);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001557
1558 // Unlink in hash bucket
1559 map_ip4_reass_t *r2 = NULL;
1560 u32 r2i = mm->ip4_reass_hash_table[r->bucket];
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001561 while (r2i != map_ip4_reass_pool_index (r))
1562 {
1563 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1564 r2 = pool_elt_at_index (mm->ip4_reass_pool, r2i);
1565 r2i = r2->bucket_next;
1566 }
1567 if (r2)
1568 {
1569 r2->bucket_next = r->bucket_next;
1570 }
1571 else
1572 {
1573 mm->ip4_reass_hash_table[r->bucket] = r->bucket_next;
1574 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001575
1576 // Unlink in list
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001577 if (r->fifo_next == map_ip4_reass_pool_index (r))
1578 {
1579 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1580 }
1581 else
1582 {
1583 if (mm->ip4_reass_fifo_last == map_ip4_reass_pool_index (r))
1584 mm->ip4_reass_fifo_last = r->fifo_prev;
1585 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next =
1586 r->fifo_next;
1587 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev =
1588 r->fifo_prev;
1589 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001590
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001591 pool_put (mm->ip4_reass_pool, r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001592 mm->ip4_reass_allocated--;
1593}
1594
1595map_ip4_reass_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001596map_ip4_reass_get (u32 src, u32 dst, u16 fragment_id,
1597 u8 protocol, u32 ** pi_to_drop)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001598{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001599 map_ip4_reass_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001600 map_main_t *mm = &map_main;
1601 map_ip4_reass_key_t k = {.src.data_u32 = src,
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001602 .dst.data_u32 = dst,
1603 .fragment_id = fragment_id,
1604 .protocol = protocol
1605 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07001606
1607 u32 h = 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001608 h = crc_u32 (k.as_u32[0], h);
1609 h = crc_u32 (k.as_u32[1], h);
1610 h = crc_u32 (k.as_u32[2], h);
1611 h = crc_u32 (k.as_u32[3], h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001612 h = h >> (32 - mm->ip4_reass_ht_log2len);
1613
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001614 f64 now = vlib_time_now (mm->vlib_main);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001615
1616 //Cache garbage collection
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001617 while (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1618 {
1619 map_ip4_reass_t *last =
1620 pool_elt_at_index (mm->ip4_reass_pool, mm->ip4_reass_fifo_last);
1621 if (last->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000) < now)
1622 map_ip4_reass_free (last, pi_to_drop);
1623 else
1624 break;
1625 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001626
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001627 if ((r = map_ip4_reass_lookup (&k, h, now)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001628 return r;
1629
1630 if (mm->ip4_reass_allocated >= mm->ip4_reass_conf_pool_size)
1631 return NULL;
1632
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001633 pool_get (mm->ip4_reass_pool, r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001634 mm->ip4_reass_allocated++;
1635 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001636 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001637 r->fragments[i] = ~0;
1638
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001639 u32 ri = map_ip4_reass_pool_index (r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001640
1641 //Link in new bucket
1642 r->bucket = h;
1643 r->bucket_next = mm->ip4_reass_hash_table[h];
1644 mm->ip4_reass_hash_table[h] = ri;
1645
1646 //Link in fifo
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001647 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1648 {
1649 r->fifo_next =
1650 pool_elt_at_index (mm->ip4_reass_pool,
1651 mm->ip4_reass_fifo_last)->fifo_next;
1652 r->fifo_prev = mm->ip4_reass_fifo_last;
1653 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next = ri;
1654 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev = ri;
1655 }
1656 else
1657 {
1658 r->fifo_next = r->fifo_prev = ri;
1659 mm->ip4_reass_fifo_last = ri;
1660 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001661
1662 //Set other fields
1663 r->ts = now;
1664 r->key = k;
1665 r->port = -1;
1666#ifdef MAP_IP4_REASS_COUNT_BYTES
1667 r->expected_total = 0xffff;
1668 r->forwarded = 0;
1669#endif
1670
1671 return r;
1672}
1673
1674int
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001675map_ip4_reass_add_fragment (map_ip4_reass_t * r, u32 pi)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001676{
1677 if (map_main.ip4_reass_buffered_counter >= map_main.ip4_reass_conf_buffers)
1678 return -1;
1679
1680 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001681 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1682 if (r->fragments[i] == ~0)
1683 {
1684 r->fragments[i] = pi;
1685 map_main.ip4_reass_buffered_counter++;
1686 return 0;
1687 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001688 return -1;
1689}
1690
1691static_always_inline map_ip6_reass_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001692map_ip6_reass_lookup (map_ip6_reass_key_t * k, u32 bucket, f64 now)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001693{
1694 map_main_t *mm = &map_main;
1695 u32 ri = mm->ip6_reass_hash_table[bucket];
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001696 while (ri != MAP_REASS_INDEX_NONE)
1697 {
1698 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1699 if (now < r->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) &&
1700 r->key.as_u64[0] == k->as_u64[0] &&
1701 r->key.as_u64[1] == k->as_u64[1] &&
1702 r->key.as_u64[2] == k->as_u64[2] &&
1703 r->key.as_u64[3] == k->as_u64[3] &&
1704 r->key.as_u64[4] == k->as_u64[4])
1705 return r;
1706 ri = r->bucket_next;
1707 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001708 return NULL;
1709}
1710
1711#define map_ip6_reass_pool_index(r) (r - map_main.ip6_reass_pool)
1712
1713void
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001714map_ip6_reass_free (map_ip6_reass_t * r, u32 ** pi_to_drop)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001715{
1716 map_main_t *mm = &map_main;
1717 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001718 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1719 if (r->fragments[i].pi != ~0)
1720 {
1721 vec_add1 (*pi_to_drop, r->fragments[i].pi);
1722 r->fragments[i].pi = ~0;
1723 map_main.ip6_reass_buffered_counter--;
1724 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001725
1726 // Unlink in hash bucket
1727 map_ip6_reass_t *r2 = NULL;
1728 u32 r2i = mm->ip6_reass_hash_table[r->bucket];
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001729 while (r2i != map_ip6_reass_pool_index (r))
1730 {
1731 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1732 r2 = pool_elt_at_index (mm->ip6_reass_pool, r2i);
1733 r2i = r2->bucket_next;
1734 }
1735 if (r2)
1736 {
1737 r2->bucket_next = r->bucket_next;
1738 }
1739 else
1740 {
1741 mm->ip6_reass_hash_table[r->bucket] = r->bucket_next;
1742 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001743
1744 // Unlink in list
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001745 if (r->fifo_next == map_ip6_reass_pool_index (r))
1746 {
1747 //Single element in the list, list is now empty
1748 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1749 }
1750 else
1751 {
1752 if (mm->ip6_reass_fifo_last == map_ip6_reass_pool_index (r)) //First element
1753 mm->ip6_reass_fifo_last = r->fifo_prev;
1754 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next =
1755 r->fifo_next;
1756 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev =
1757 r->fifo_prev;
1758 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001759
1760 // Free from pool if necessary
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001761 pool_put (mm->ip6_reass_pool, r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001762 mm->ip6_reass_allocated--;
1763}
1764
1765map_ip6_reass_t *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001766map_ip6_reass_get (ip6_address_t * src, ip6_address_t * dst, u32 fragment_id,
1767 u8 protocol, u32 ** pi_to_drop)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001768{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001769 map_ip6_reass_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001770 map_main_t *mm = &map_main;
1771 map_ip6_reass_key_t k = {
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001772 .src = *src,
1773 .dst = *dst,
1774 .fragment_id = fragment_id,
1775 .protocol = protocol
1776 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07001777
1778 u32 h = 0;
1779 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001780 for (i = 0; i < 10; i++)
1781 h = crc_u32 (k.as_u32[i], h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001782 h = h >> (32 - mm->ip6_reass_ht_log2len);
1783
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001784 f64 now = vlib_time_now (mm->vlib_main);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001785
1786 //Cache garbage collection
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001787 while (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1788 {
1789 map_ip6_reass_t *last =
1790 pool_elt_at_index (mm->ip6_reass_pool, mm->ip6_reass_fifo_last);
1791 if (last->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) < now)
1792 map_ip6_reass_free (last, pi_to_drop);
1793 else
1794 break;
1795 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001796
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001797 if ((r = map_ip6_reass_lookup (&k, h, now)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001798 return r;
1799
1800 if (mm->ip6_reass_allocated >= mm->ip6_reass_conf_pool_size)
1801 return NULL;
1802
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001803 pool_get (mm->ip6_reass_pool, r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001804 mm->ip6_reass_allocated++;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001805 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1806 {
1807 r->fragments[i].pi = ~0;
1808 r->fragments[i].next_data_len = 0;
1809 r->fragments[i].next_data_offset = 0;
1810 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001811
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001812 u32 ri = map_ip6_reass_pool_index (r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001813
1814 //Link in new bucket
1815 r->bucket = h;
1816 r->bucket_next = mm->ip6_reass_hash_table[h];
1817 mm->ip6_reass_hash_table[h] = ri;
1818
1819 //Link in fifo
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001820 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1821 {
1822 r->fifo_next =
1823 pool_elt_at_index (mm->ip6_reass_pool,
1824 mm->ip6_reass_fifo_last)->fifo_next;
1825 r->fifo_prev = mm->ip6_reass_fifo_last;
1826 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next = ri;
1827 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev = ri;
1828 }
1829 else
1830 {
1831 r->fifo_next = r->fifo_prev = ri;
1832 mm->ip6_reass_fifo_last = ri;
1833 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001834
1835 //Set other fields
1836 r->ts = now;
1837 r->key = k;
1838 r->ip4_header.ip_version_and_header_length = 0;
1839#ifdef MAP_IP6_REASS_COUNT_BYTES
1840 r->expected_total = 0xffff;
1841 r->forwarded = 0;
1842#endif
1843 return r;
1844}
1845
1846int
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001847map_ip6_reass_add_fragment (map_ip6_reass_t * r, u32 pi,
1848 u16 data_offset, u16 next_data_offset,
1849 u8 * data_start, u16 data_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001850{
1851 map_ip6_fragment_t *f = NULL, *prev_f = NULL;
1852 u16 copied_len = (data_len > 20) ? 20 : data_len;
1853
1854 if (map_main.ip6_reass_buffered_counter >= map_main.ip6_reass_conf_buffers)
1855 return -1;
1856
1857 //Lookup for fragments for the current buffer
1858 //and the one before that
1859 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001860 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1861 {
1862 if (data_offset && r->fragments[i].next_data_offset == data_offset)
1863 {
1864 prev_f = &r->fragments[i]; // This is buffer for previous packet
1865 }
1866 else if (r->fragments[i].next_data_offset == next_data_offset)
1867 {
1868 f = &r->fragments[i]; // This is a buffer for the current packet
1869 }
1870 else if (r->fragments[i].next_data_offset == 0)
1871 { //Available
1872 if (f == NULL)
1873 f = &r->fragments[i];
1874 else if (prev_f == NULL)
1875 prev_f = &r->fragments[i];
1876 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001877 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001878
1879 if (!f || f->pi != ~0)
1880 return -1;
1881
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001882 if (data_offset)
1883 {
1884 if (!prev_f)
1885 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001886
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001887 clib_memcpy (prev_f->next_data, data_start, copied_len);
1888 prev_f->next_data_len = copied_len;
1889 prev_f->next_data_offset = data_offset;
1890 }
1891 else
1892 {
1893 if (((ip4_header_t *) data_start)->ip_version_and_header_length != 0x45)
1894 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001895
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001896 if (r->ip4_header.ip_version_and_header_length == 0)
1897 clib_memcpy (&r->ip4_header, data_start, sizeof (ip4_header_t));
1898 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001899
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001900 if (data_len > 20)
1901 {
1902 f->next_data_offset = next_data_offset;
1903 f->pi = pi;
1904 map_main.ip6_reass_buffered_counter++;
1905 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001906 return 0;
1907}
1908
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001909void
1910map_ip4_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001911{
1912 map_main_t *mm = &map_main;
1913 int i;
1914
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001915 if (dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001916 *dropped_packets = mm->ip4_reass_buffered_counter;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001917 if (trashed_reass)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001918 *trashed_reass = mm->ip4_reass_allocated;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001919 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1920 {
1921 u16 ri = mm->ip4_reass_fifo_last;
1922 do
1923 {
1924 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1925 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1926 if (r->fragments[i] != ~0)
1927 map_ip4_drop_pi (r->fragments[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001928
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001929 ri = r->fifo_next;
1930 pool_put (mm->ip4_reass_pool, r);
1931 }
1932 while (ri != mm->ip4_reass_fifo_last);
1933 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001934
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001935 vec_free (mm->ip4_reass_hash_table);
1936 vec_resize (mm->ip4_reass_hash_table, 1 << mm->ip4_reass_ht_log2len);
1937 for (i = 0; i < (1 << mm->ip4_reass_ht_log2len); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001938 mm->ip4_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001939 pool_free (mm->ip4_reass_pool);
1940 pool_alloc (mm->ip4_reass_pool, mm->ip4_reass_conf_pool_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001941
1942 mm->ip4_reass_allocated = 0;
1943 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1944 mm->ip4_reass_buffered_counter = 0;
1945}
1946
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001947u8
1948map_get_ht_log2len (f32 ht_ratio, u16 pool_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001949{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001950 u32 desired_size = (u32) (pool_size * ht_ratio);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001951 u8 i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001952 for (i = 1; i < 31; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001953 if ((1 << i) >= desired_size)
1954 return i;
1955 return 4;
1956}
1957
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001958int
1959map_ip4_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1960 u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001961{
1962 map_main_t *mm = &map_main;
1963 if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1964 return -1;
1965
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001966 map_ip4_reass_lock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001967 mm->ip4_reass_conf_ht_ratio = ht_ratio;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001968 mm->ip4_reass_ht_log2len =
1969 map_get_ht_log2len (ht_ratio, mm->ip4_reass_conf_pool_size);
1970 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1971 map_ip4_reass_unlock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001972 return 0;
1973}
1974
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001975int
1976map_ip4_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1977 u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001978{
1979 map_main_t *mm = &map_main;
1980 if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1981 return -1;
1982
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001983 map_ip4_reass_lock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001984 mm->ip4_reass_conf_pool_size = pool_size;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001985 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1986 map_ip4_reass_unlock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001987 return 0;
1988}
1989
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001990int
1991map_ip4_reass_conf_lifetime (u16 lifetime_ms)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001992{
1993 map_main.ip4_reass_conf_lifetime_ms = lifetime_ms;
1994 return 0;
1995}
1996
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001997int
1998map_ip4_reass_conf_buffers (u32 buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001999{
2000 map_main.ip4_reass_conf_buffers = buffers;
2001 return 0;
2002}
2003
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002004void
2005map_ip6_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002006{
2007 map_main_t *mm = &map_main;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002008 if (dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002009 *dropped_packets = mm->ip6_reass_buffered_counter;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002010 if (trashed_reass)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002011 *trashed_reass = mm->ip6_reass_allocated;
2012 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002013 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
2014 {
2015 u16 ri = mm->ip6_reass_fifo_last;
2016 do
2017 {
2018 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
2019 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
2020 if (r->fragments[i].pi != ~0)
2021 map_ip6_drop_pi (r->fragments[i].pi);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002022
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002023 ri = r->fifo_next;
2024 pool_put (mm->ip6_reass_pool, r);
2025 }
2026 while (ri != mm->ip6_reass_fifo_last);
2027 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
2028 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002029
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002030 vec_free (mm->ip6_reass_hash_table);
2031 vec_resize (mm->ip6_reass_hash_table, 1 << mm->ip6_reass_ht_log2len);
2032 for (i = 0; i < (1 << mm->ip6_reass_ht_log2len); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002033 mm->ip6_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002034 pool_free (mm->ip6_reass_pool);
2035 pool_alloc (mm->ip6_reass_pool, mm->ip4_reass_conf_pool_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002036
2037 mm->ip6_reass_allocated = 0;
2038 mm->ip6_reass_buffered_counter = 0;
2039}
2040
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002041int
2042map_ip6_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
2043 u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002044{
2045 map_main_t *mm = &map_main;
2046 if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
2047 return -1;
2048
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002049 map_ip6_reass_lock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07002050 mm->ip6_reass_conf_ht_ratio = ht_ratio;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002051 mm->ip6_reass_ht_log2len =
2052 map_get_ht_log2len (ht_ratio, mm->ip6_reass_conf_pool_size);
2053 map_ip6_reass_reinit (trashed_reass, dropped_packets);
2054 map_ip6_reass_unlock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07002055 return 0;
2056}
2057
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002058int
2059map_ip6_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
2060 u32 * dropped_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002061{
2062 map_main_t *mm = &map_main;
2063 if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
2064 return -1;
2065
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002066 map_ip6_reass_lock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07002067 mm->ip6_reass_conf_pool_size = pool_size;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002068 map_ip6_reass_reinit (trashed_reass, dropped_packets);
2069 map_ip6_reass_unlock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07002070 return 0;
2071}
2072
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002073int
2074map_ip6_reass_conf_lifetime (u16 lifetime_ms)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002075{
2076 map_main.ip6_reass_conf_lifetime_ms = lifetime_ms;
2077 return 0;
2078}
2079
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002080int
2081map_ip6_reass_conf_buffers (u32 buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002082{
2083 map_main.ip6_reass_conf_buffers = buffers;
2084 return 0;
2085}
2086
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002087/* *INDENT-OFF* */
Ole Troan96eefd42016-09-12 12:25:30 +02002088
2089/*?
2090 * Configure MAP reassembly behaviour
2091 *
2092 * @cliexpar
2093 * @cliexstart{map params reassembly}
2094 * @cliexend
2095 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002096VLIB_CLI_COMMAND(map_ip4_reass_lifetime_command, static) = {
2097 .path = "map params reassembly",
Ole Troan96eefd42016-09-12 12:25:30 +02002098 .short_help = "map params reassembly [ip4 | ip6] [lifetime <lifetime-ms>] "
2099 "[pool-size <pool-size>] [buffers <buffers>] "
2100 "[ht-ratio <ht-ratio>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002101 .function = map_params_reass_command_fn,
2102};
2103
Ole Troan96eefd42016-09-12 12:25:30 +02002104/*?
2105 * Set or copy the IP TOS/Traffic Class field
2106 *
2107 * @cliexpar
2108 * @cliexstart{map params traffic-class}
2109 *
2110 * This command is used to set the traffic-class field in translated
2111 * or encapsulated packets. If copy is specifed (the default) then the
2112 * traffic-class/TOS field is copied from the original packet to the
2113 * translated / encapsulating header.
2114 * @cliexend
2115 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002116VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
2117 .path = "map params traffic-class",
Ole Troan96eefd42016-09-12 12:25:30 +02002118 .short_help = "map params traffic-class {0x0-0xff | copy}",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002119 .function = map_traffic_class_command_fn,
2120};
2121
Ole Troan96eefd42016-09-12 12:25:30 +02002122/*?
2123 * Bypass IP4/IP6 lookup
2124 *
2125 * @cliexpar
2126 * @cliexstart{map params pre-resolve}
2127 *
2128 * Bypass a second FIB lookup of the translated or encapsulated
2129 * packet, and forward the packet directly to the specified
2130 * next-hop. This optimization trades forwarding flexibility for
2131 * performance.
2132 * @cliexend
2133 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002134VLIB_CLI_COMMAND(map_pre_resolve_command, static) = {
2135 .path = "map params pre-resolve",
Ole Troan96eefd42016-09-12 12:25:30 +02002136 .short_help = " map params pre-resolve {ip4-nh <address>} "
2137 "| {ip6-nh <address>}",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002138 .function = map_pre_resolve_command_fn,
2139};
2140
Ole Troan96eefd42016-09-12 12:25:30 +02002141/*?
2142 * Enable or disable the MAP-E inbound security check
2143 *
2144 * @cliexpar
2145 * @cliexstart{map params security-check}
2146 *
2147 * By default, a decapsulated packet's IPv4 source address will be
2148 * verified against the outer header's IPv6 source address. Disabling
2149 * this feature will allow IPv4 source address spoofing.
2150 * @cliexend
2151 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002152VLIB_CLI_COMMAND(map_security_check_command, static) = {
2153 .path = "map params security-check",
Ole Troan96eefd42016-09-12 12:25:30 +02002154 .short_help = "map params security-check on|off",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002155 .function = map_security_check_command_fn,
2156};
2157
Ole Troan96eefd42016-09-12 12:25:30 +02002158/*?
2159 * Specifiy the IPv4 source address used for relayed ICMP error messages
2160 *
2161 * @cliexpar
2162 * @cliexstart{map params icmp source-address}
2163 *
2164 * This command specifies which IPv4 source address (must be local to
2165 * the system), that is used for relayed received IPv6 ICMP error
2166 * messages.
2167 * @cliexend
2168 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002169VLIB_CLI_COMMAND(map_icmp_relay_source_address_command, static) = {
Ole Troancda94822016-01-07 14:37:25 +01002170 .path = "map params icmp source-address",
Ole Troan96eefd42016-09-12 12:25:30 +02002171 .short_help = "map params icmp source-address <ip4-address>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002172 .function = map_icmp_relay_source_address_command_fn,
2173};
2174
Ole Troan96eefd42016-09-12 12:25:30 +02002175/*?
2176 * Send IPv6 ICMP unreachables
2177 *
2178 * @cliexpar
2179 * @cliexstart{map params icmp6 unreachables}
2180 *
2181 * Send IPv6 ICMP unreachable messages back if security check fails or
2182 * no MAP domain exists.
2183 * @cliexend
2184 ?*/
Ole Troancda94822016-01-07 14:37:25 +01002185VLIB_CLI_COMMAND(map_icmp_unreachables_command, static) = {
Ole Troan9fb87552016-01-13 22:30:43 +01002186 .path = "map params icmp6 unreachables",
Ole Troan96eefd42016-09-12 12:25:30 +02002187 .short_help = "map params icmp6 unreachables {on|off}",
Ole Troancda94822016-01-07 14:37:25 +01002188 .function = map_icmp_unreachables_command_fn,
2189};
2190
Ole Troan96eefd42016-09-12 12:25:30 +02002191/*?
2192 * Configure MAP fragmentation behaviour
2193 *
2194 * @cliexpar
2195 * @cliexstart{map params fragment}
2196 * @cliexend
2197 ?*/
Ole Troan9fb87552016-01-13 22:30:43 +01002198VLIB_CLI_COMMAND(map_fragment_command, static) = {
2199 .path = "map params fragment",
Ole Troan96eefd42016-09-12 12:25:30 +02002200 .short_help = "map params fragment inner|outer",
Ole Troan9fb87552016-01-13 22:30:43 +01002201 .function = map_fragment_command_fn,
2202};
2203
Ole Troan96eefd42016-09-12 12:25:30 +02002204/*?
2205 * Ignore the IPv4 Don't fragment bit
2206 *
2207 * @cliexpar
2208 * @cliexstart{map params fragment ignore-df}
2209 *
2210 * Allows fragmentation of the IPv4 packet even if the DF bit is
2211 * set. The choice between inner or outer fragmentation of tunnel
2212 * packets is complicated. The benefit of inner fragmentation is that
2213 * the ultimate endpoint must reassemble, instead of the tunnel
2214 * endpoint.
2215 * @cliexend
2216 ?*/
Ole Troan9fb87552016-01-13 22:30:43 +01002217VLIB_CLI_COMMAND(map_fragment_df_command, static) = {
2218 .path = "map params fragment ignore-df",
Ole Troan96eefd42016-09-12 12:25:30 +02002219 .short_help = "map params fragment ignore-df on|off",
Ole Troan9fb87552016-01-13 22:30:43 +01002220 .function = map_fragment_df_command_fn,
2221};
2222
Ole Troan96eefd42016-09-12 12:25:30 +02002223/*?
2224 * Specifiy if the inbound security check should be done on fragments
2225 *
2226 * @cliexpar
2227 * @cliexstart{map params security-check fragments}
2228 *
2229 * Typically the inbound on-decapsulation security check is only done
2230 * on the first packet. The packet that contains the L4
2231 * information. While a security check on every fragment is possible,
2232 * it has a cost. State must be created on the first fragment.
2233 * @cliexend
2234 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002235VLIB_CLI_COMMAND(map_security_check_frag_command, static) = {
2236 .path = "map params security-check fragments",
Ole Troan96eefd42016-09-12 12:25:30 +02002237 .short_help = "map params security-check fragments on|off",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002238 .function = map_security_check_frag_command_fn,
2239};
2240
Ole Troan96eefd42016-09-12 12:25:30 +02002241/*?
2242 * Add MAP domain
2243 *
2244 * @cliexpar
2245 * @cliexstart{map add domain}
2246 * @cliexend
2247 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002248VLIB_CLI_COMMAND(map_add_domain_command, static) = {
2249 .path = "map add domain",
Ole Troan96eefd42016-09-12 12:25:30 +02002250 .short_help = "map add domain ip4-pfx <ip4-pfx> ip6-pfx <ip6-pfx> "
2251 "ip6-src <ip6-pfx> ea-bits-len <n> psid-offset <n> psid-len <n> "
2252 "[map-t] [mtu <mtu>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002253 .function = map_add_domain_command_fn,
2254};
2255
Ole Troan96eefd42016-09-12 12:25:30 +02002256/*?
2257 * Add MAP rule to a domain
2258 *
2259 * @cliexpar
2260 * @cliexstart{map add rule}
2261 * @cliexend
2262 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002263VLIB_CLI_COMMAND(map_add_rule_command, static) = {
2264 .path = "map add rule",
Ole Troan96eefd42016-09-12 12:25:30 +02002265 .short_help = "map add rule index <domain> psid <psid> ip6-dst <ip6-addr>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002266 .function = map_add_rule_command_fn,
2267};
2268
Ole Troan96eefd42016-09-12 12:25:30 +02002269/*?
2270 * Delete MAP domain
2271 *
2272 * @cliexpar
2273 * @cliexstart{map del domain}
2274 * @cliexend
2275 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002276VLIB_CLI_COMMAND(map_del_command, static) = {
2277 .path = "map del domain",
Ole Troan96eefd42016-09-12 12:25:30 +02002278 .short_help = "map del domain index <domain>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002279 .function = map_del_domain_command_fn,
2280};
2281
Ole Troan96eefd42016-09-12 12:25:30 +02002282/*?
2283 * Show MAP domains
2284 *
2285 * @cliexpar
2286 * @cliexstart{show map domain}
2287 * @cliexend
2288 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002289VLIB_CLI_COMMAND(show_map_domain_command, static) = {
2290 .path = "show map domain",
Ole Troan96eefd42016-09-12 12:25:30 +02002291 .short_help = "show map domain index <n> [counters]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002292 .function = show_map_domain_command_fn,
2293};
2294
Ole Troan96eefd42016-09-12 12:25:30 +02002295/*?
2296 * Show MAP statistics
2297 *
2298 * @cliexpar
2299 * @cliexstart{show map stats}
2300 * @cliexend
2301 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002302VLIB_CLI_COMMAND(show_map_stats_command, static) = {
2303 .path = "show map stats",
Ole Troan96eefd42016-09-12 12:25:30 +02002304 .short_help = "show map stats",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002305 .function = show_map_stats_command_fn,
2306};
2307
Ole Troan96eefd42016-09-12 12:25:30 +02002308/*?
2309 * Show MAP fragmentation information
2310 *
2311 * @cliexpar
2312 * @cliexstart{show map fragments}
2313 * @cliexend
2314 ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002315VLIB_CLI_COMMAND(show_map_fragments_command, static) = {
2316 .path = "show map fragments",
Ole Troan96eefd42016-09-12 12:25:30 +02002317 .short_help = "show map fragments",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002318 .function = show_map_fragments_command_fn,
2319};
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002320/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002321
2322/*
2323 * map_init
2324 */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002325clib_error_t *
2326map_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002327{
2328 map_main_t *mm = &map_main;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002329 mm->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07002330 mm->vlib_main = vm;
2331
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002332#ifdef MAP_SKIP_IP6_LOOKUP
Neale Ranns80823802017-02-20 18:23:41 -08002333 fib_protocol_t proto;
2334
2335 FOR_EACH_FIB_PROTOCOL (proto)
2336 {
2337 map_pre_resolve_init (&pre_resolved[proto]);
2338 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002339#endif
2340
2341 /* traffic class */
2342 mm->tc = 0;
2343 mm->tc_copy = true;
2344
2345 /* Inbound security check */
2346 mm->sec_check = true;
2347 mm->sec_check_frag = false;
2348
Ole Troancda94822016-01-07 14:37:25 +01002349 /* ICMP6 Type 1, Code 5 for security check failure */
2350 mm->icmp6_enabled = false;
2351
Ole Troan9fb87552016-01-13 22:30:43 +01002352 /* Inner or outer fragmentation */
2353 mm->frag_inner = false;
2354 mm->frag_ignore_df = false;
2355
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002356 vec_validate (mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002357 mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "rx";
2358 mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "tx";
2359
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002360 vlib_validate_simple_counter (&mm->icmp_relayed, 0);
2361 vlib_zero_simple_counter (&mm->icmp_relayed, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002362
2363 /* IP4 virtual reassembly */
2364 mm->ip4_reass_hash_table = 0;
2365 mm->ip4_reass_pool = 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002366 mm->ip4_reass_lock =
2367 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002368 mm->ip4_reass_conf_ht_ratio = MAP_IP4_REASS_HT_RATIO_DEFAULT;
2369 mm->ip4_reass_conf_lifetime_ms = MAP_IP4_REASS_LIFETIME_DEFAULT;
2370 mm->ip4_reass_conf_pool_size = MAP_IP4_REASS_POOL_SIZE_DEFAULT;
2371 mm->ip4_reass_conf_buffers = MAP_IP4_REASS_BUFFERS_DEFAULT;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002372 mm->ip4_reass_ht_log2len =
2373 map_get_ht_log2len (mm->ip4_reass_conf_ht_ratio,
2374 mm->ip4_reass_conf_pool_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002375 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002376 map_ip4_reass_reinit (NULL, NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002377
2378 /* IP6 virtual reassembly */
2379 mm->ip6_reass_hash_table = 0;
2380 mm->ip6_reass_pool = 0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002381 mm->ip6_reass_lock =
2382 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002383 mm->ip6_reass_conf_ht_ratio = MAP_IP6_REASS_HT_RATIO_DEFAULT;
2384 mm->ip6_reass_conf_lifetime_ms = MAP_IP6_REASS_LIFETIME_DEFAULT;
2385 mm->ip6_reass_conf_pool_size = MAP_IP6_REASS_POOL_SIZE_DEFAULT;
2386 mm->ip6_reass_conf_buffers = MAP_IP6_REASS_BUFFERS_DEFAULT;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002387 mm->ip6_reass_ht_log2len =
2388 map_get_ht_log2len (mm->ip6_reass_conf_ht_ratio,
2389 mm->ip6_reass_conf_pool_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002390 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002391 map_ip6_reass_reinit (NULL, NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002392
Neale Ranns80823802017-02-20 18:23:41 -08002393#ifdef MAP_SKIP_IP6_LOOKUP
2394 fib_node_register_type (FIB_NODE_TYPE_MAP_E, &map_vft);
2395#endif
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002396 map_dpo_module_init ();
2397
Ed Warnickecb9cada2015-12-08 15:45:58 -07002398 return 0;
2399}
2400
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07002401VLIB_INIT_FUNCTION (map_init);
2402
2403/*
2404 * fd.io coding-style-patch-verification: ON
2405 *
2406 * Local Variables:
2407 * eval: (c-set-style "gnu")
2408 * End:
2409 */