blob: 720d13c2879b97b62819689986e9630766cf391f [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include "map.h"
16
17#include "../ip/ip_frag.h"
Matus Fabiana774b532017-05-02 03:15:22 -070018#include <vnet/ip/ip4_to_ip6.h>
19#include <vnet/ip/ip6_to_ip4.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070020
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070021enum ip6_map_next_e
22{
Ed Warnickecb9cada2015-12-08 15:45:58 -070023 IP6_MAP_NEXT_IP4_LOOKUP,
24#ifdef MAP_SKIP_IP6_LOOKUP
25 IP6_MAP_NEXT_IP4_REWRITE,
26#endif
27 IP6_MAP_NEXT_IP6_REASS,
28 IP6_MAP_NEXT_IP4_REASS,
29 IP6_MAP_NEXT_IP4_FRAGMENT,
30 IP6_MAP_NEXT_IP6_ICMP_RELAY,
31 IP6_MAP_NEXT_IP6_LOCAL,
32 IP6_MAP_NEXT_DROP,
Ole Troancda94822016-01-07 14:37:25 +010033 IP6_MAP_NEXT_ICMP,
Ed Warnickecb9cada2015-12-08 15:45:58 -070034 IP6_MAP_N_NEXT,
35};
36
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070037enum ip6_map_ip6_reass_next_e
38{
Ed Warnickecb9cada2015-12-08 15:45:58 -070039 IP6_MAP_IP6_REASS_NEXT_IP6_MAP,
40 IP6_MAP_IP6_REASS_NEXT_DROP,
41 IP6_MAP_IP6_REASS_N_NEXT,
42};
43
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070044enum ip6_map_ip4_reass_next_e
45{
Ed Warnickecb9cada2015-12-08 15:45:58 -070046 IP6_MAP_IP4_REASS_NEXT_IP4_LOOKUP,
47 IP6_MAP_IP4_REASS_NEXT_IP4_FRAGMENT,
48 IP6_MAP_IP4_REASS_NEXT_DROP,
49 IP6_MAP_IP4_REASS_N_NEXT,
50};
51
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070052enum ip6_icmp_relay_next_e
53{
Ed Warnickecb9cada2015-12-08 15:45:58 -070054 IP6_ICMP_RELAY_NEXT_IP4_LOOKUP,
55 IP6_ICMP_RELAY_NEXT_DROP,
56 IP6_ICMP_RELAY_N_NEXT,
57};
58
59vlib_node_registration_t ip6_map_ip4_reass_node;
60vlib_node_registration_t ip6_map_ip6_reass_node;
61static vlib_node_registration_t ip6_map_icmp_relay_node;
62
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070063typedef struct
64{
Ed Warnickecb9cada2015-12-08 15:45:58 -070065 u32 map_domain_index;
66 u16 port;
67 u8 cached;
68} map_ip6_map_ip4_reass_trace_t;
69
70u8 *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070071format_ip6_map_ip4_reass_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070072{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070073 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
74 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
75 map_ip6_map_ip4_reass_trace_t *t =
76 va_arg (*args, map_ip6_map_ip4_reass_trace_t *);
77 return format (s, "MAP domain index: %d L4 port: %u Status: %s",
78 t->map_domain_index, t->port,
79 t->cached ? "cached" : "forwarded");
Ed Warnickecb9cada2015-12-08 15:45:58 -070080}
81
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070082typedef struct
83{
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 u16 offset;
85 u16 frag_len;
86 u8 out;
87} map_ip6_map_ip6_reass_trace_t;
88
89u8 *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070090format_ip6_map_ip6_reass_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070091{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070092 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
93 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
94 map_ip6_map_ip6_reass_trace_t *t =
95 va_arg (*args, map_ip6_map_ip6_reass_trace_t *);
96 return format (s, "Offset: %d Fragment length: %d Status: %s", t->offset,
97 t->frag_len, t->out ? "out" : "in");
Ed Warnickecb9cada2015-12-08 15:45:58 -070098}
99
100/*
101 * ip6_map_sec_check
102 */
103static_always_inline bool
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700104ip6_map_sec_check (map_domain_t * d, u16 port, ip4_header_t * ip4,
105 ip6_header_t * ip6)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700107 u16 sp4 = clib_net_to_host_u16 (port);
108 u32 sa4 = clib_net_to_host_u32 (ip4->src_address.as_u32);
109 u64 sal6 = map_get_pfx (d, sa4, sp4);
110 u64 sar6 = map_get_sfx (d, sa4, sp4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700112 if (PREDICT_FALSE
113 (sal6 != clib_net_to_host_u64 (ip6->src_address.as_u64[0])
114 || sar6 != clib_net_to_host_u64 (ip6->src_address.as_u64[1])))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115 return (false);
116 return (true);
117}
118
119static_always_inline void
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700120ip6_map_security_check (map_domain_t * d, ip4_header_t * ip4,
121 ip6_header_t * ip6, u32 * next, u8 * error)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122{
123 map_main_t *mm = &map_main;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700124 if (d->ea_bits_len || d->rules)
125 {
126 if (d->psid_length > 0)
127 {
128 if (!ip4_is_fragment (ip4))
129 {
Matus Fabiana774b532017-05-02 03:15:22 -0700130 u16 port = ip4_get_port (ip4, 1);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700131 if (port)
132 {
133 if (mm->sec_check)
134 *error =
135 ip6_map_sec_check (d, port, ip4,
136 ip6) ? MAP_ERROR_NONE :
137 MAP_ERROR_DECAP_SEC_CHECK;
138 }
139 else
140 {
141 *error = MAP_ERROR_BAD_PROTOCOL;
142 }
143 }
144 else
145 {
146 *next = mm->sec_check_frag ? IP6_MAP_NEXT_IP4_REASS : *next;
147 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150}
151
152static_always_inline bool
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700153ip6_map_ip4_lookup_bypass (vlib_buffer_t * p0, ip4_header_t * ip)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154{
155#ifdef MAP_SKIP_IP6_LOOKUP
Neale Ranns80823802017-02-20 18:23:41 -0800156 if (FIB_NODE_INDEX_INVALID != pre_resolved[FIB_PROTOCOL_IP4].fei)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700157 {
Neale Ranns80823802017-02-20 18:23:41 -0800158 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
159 pre_resolved[FIB_PROTOCOL_IP4].dpo.dpoi_index;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700160 return (true);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162#endif
163 return (false);
164}
165
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166/*
167 * ip6_map
168 */
169static uword
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700170ip6_map (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171{
172 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700173 vlib_node_runtime_t *error_node =
174 vlib_node_get_runtime (vm, ip6_map_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175 map_main_t *mm = &map_main;
176 vlib_combined_counter_main_t *cm = mm->domain_counters;
Damjan Marion586afd72017-04-05 19:18:20 +0200177 u32 thread_index = vlib_get_thread_index ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700179 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180 n_left_from = frame->n_vectors;
181 next_index = node->cached_next_index;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700182 while (n_left_from > 0)
183 {
184 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700186 /* Dual loop */
187 while (n_left_from >= 4 && n_left_to_next >= 2)
188 {
189 u32 pi0, pi1;
190 vlib_buffer_t *p0, *p1;
191 u8 error0 = MAP_ERROR_NONE;
192 u8 error1 = MAP_ERROR_NONE;
193 map_domain_t *d0 = 0, *d1 = 0;
194 ip4_header_t *ip40, *ip41;
195 ip6_header_t *ip60, *ip61;
196 u16 port0 = 0, port1 = 0;
197 u32 map_domain_index0 = ~0, map_domain_index1 = ~0;
198 u32 next0 = IP6_MAP_NEXT_IP4_LOOKUP;
199 u32 next1 = IP6_MAP_NEXT_IP4_LOOKUP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700201 /* Prefetch next iteration. */
202 {
203 vlib_buffer_t *p2, *p3;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700205 p2 = vlib_get_buffer (vm, from[2]);
206 p3 = vlib_get_buffer (vm, from[3]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700208 vlib_prefetch_buffer_header (p2, LOAD);
209 vlib_prefetch_buffer_header (p3, LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700211 /* IPv6 + IPv4 header + 8 bytes of ULP */
212 CLIB_PREFETCH (p2->data, 68, LOAD);
213 CLIB_PREFETCH (p3->data, 68, LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700215
216 pi0 = to_next[0] = from[0];
217 pi1 = to_next[1] = from[1];
218 from += 2;
219 n_left_from -= 2;
220 to_next += 2;
221 n_left_to_next -= 2;
222
223 p0 = vlib_get_buffer (vm, pi0);
224 p1 = vlib_get_buffer (vm, pi1);
225 ip60 = vlib_buffer_get_current (p0);
226 ip61 = vlib_buffer_get_current (p1);
227 vlib_buffer_advance (p0, sizeof (ip6_header_t));
228 vlib_buffer_advance (p1, sizeof (ip6_header_t));
229 ip40 = vlib_buffer_get_current (p0);
230 ip41 = vlib_buffer_get_current (p1);
231
232 /*
233 * Encapsulated IPv4 packet
234 * - IPv4 fragmented -> Pass to virtual reassembly unless security check disabled
235 * - Lookup/Rewrite or Fragment node in case of packet > MTU
236 * Fragmented IPv6 packet
237 * ICMP IPv6 packet
238 * - Error -> Pass to ICMPv6/ICMPv4 relay
239 * - Info -> Pass to IPv6 local
240 * Anything else -> drop
241 */
242 if (PREDICT_TRUE
243 (ip60->protocol == IP_PROTOCOL_IP_IN_IP
244 && clib_net_to_host_u16 (ip60->payload_length) > 20))
245 {
246 d0 =
247 ip6_map_get_domain (vnet_buffer (p0)->ip.adj_index[VLIB_TX],
Matus Fabiana774b532017-05-02 03:15:22 -0700248 (ip4_address_t *) & ip40->
249 src_address.as_u32, &map_domain_index0,
250 &error0);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700251 }
252 else if (ip60->protocol == IP_PROTOCOL_ICMP6 &&
253 clib_net_to_host_u16 (ip60->payload_length) >
254 sizeof (icmp46_header_t))
255 {
256 icmp46_header_t *icmp = (void *) (ip60 + 1);
257 next0 = (icmp->type == ICMP6_echo_request
258 || icmp->type ==
259 ICMP6_echo_reply) ? IP6_MAP_NEXT_IP6_LOCAL :
260 IP6_MAP_NEXT_IP6_ICMP_RELAY;
261 }
262 else if (ip60->protocol == IP_PROTOCOL_IPV6_FRAGMENTATION)
263 {
264 next0 = IP6_MAP_NEXT_IP6_REASS;
265 }
266 else
267 {
268 error0 = MAP_ERROR_BAD_PROTOCOL;
269 }
270 if (PREDICT_TRUE
271 (ip61->protocol == IP_PROTOCOL_IP_IN_IP
272 && clib_net_to_host_u16 (ip61->payload_length) > 20))
273 {
274 d1 =
275 ip6_map_get_domain (vnet_buffer (p1)->ip.adj_index[VLIB_TX],
Matus Fabiana774b532017-05-02 03:15:22 -0700276 (ip4_address_t *) & ip41->
277 src_address.as_u32, &map_domain_index1,
278 &error1);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700279 }
280 else if (ip61->protocol == IP_PROTOCOL_ICMP6 &&
281 clib_net_to_host_u16 (ip61->payload_length) >
282 sizeof (icmp46_header_t))
283 {
284 icmp46_header_t *icmp = (void *) (ip61 + 1);
285 next1 = (icmp->type == ICMP6_echo_request
286 || icmp->type ==
287 ICMP6_echo_reply) ? IP6_MAP_NEXT_IP6_LOCAL :
288 IP6_MAP_NEXT_IP6_ICMP_RELAY;
289 }
290 else if (ip61->protocol == IP_PROTOCOL_IPV6_FRAGMENTATION)
291 {
292 next1 = IP6_MAP_NEXT_IP6_REASS;
293 }
294 else
295 {
296 error1 = MAP_ERROR_BAD_PROTOCOL;
297 }
298
299 if (d0)
300 {
301 /* MAP inbound security check */
302 ip6_map_security_check (d0, ip40, ip60, &next0, &error0);
303
304 if (PREDICT_TRUE (error0 == MAP_ERROR_NONE &&
305 next0 == IP6_MAP_NEXT_IP4_LOOKUP))
306 {
307 if (PREDICT_FALSE
308 (d0->mtu
309 && (clib_host_to_net_u16 (ip40->length) > d0->mtu)))
310 {
311 vnet_buffer (p0)->ip_frag.header_offset = 0;
312 vnet_buffer (p0)->ip_frag.flags = 0;
313 vnet_buffer (p0)->ip_frag.next_index =
314 IP4_FRAG_NEXT_IP4_LOOKUP;
315 vnet_buffer (p0)->ip_frag.mtu = d0->mtu;
316 next0 = IP6_MAP_NEXT_IP4_FRAGMENT;
317 }
318 else
319 {
320 next0 =
321 ip6_map_ip4_lookup_bypass (p0,
322 ip40) ?
323 IP6_MAP_NEXT_IP4_REWRITE : next0;
324 }
325 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +0200326 thread_index,
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700327 map_domain_index0, 1,
328 clib_net_to_host_u16
329 (ip40->length));
330 }
331 }
332 if (d1)
333 {
334 /* MAP inbound security check */
335 ip6_map_security_check (d1, ip41, ip61, &next1, &error1);
336
337 if (PREDICT_TRUE (error1 == MAP_ERROR_NONE &&
338 next1 == IP6_MAP_NEXT_IP4_LOOKUP))
339 {
340 if (PREDICT_FALSE
341 (d1->mtu
342 && (clib_host_to_net_u16 (ip41->length) > d1->mtu)))
343 {
344 vnet_buffer (p1)->ip_frag.header_offset = 0;
345 vnet_buffer (p1)->ip_frag.flags = 0;
346 vnet_buffer (p1)->ip_frag.next_index =
347 IP4_FRAG_NEXT_IP4_LOOKUP;
348 vnet_buffer (p1)->ip_frag.mtu = d1->mtu;
349 next1 = IP6_MAP_NEXT_IP4_FRAGMENT;
350 }
351 else
352 {
353 next1 =
354 ip6_map_ip4_lookup_bypass (p1,
355 ip41) ?
356 IP6_MAP_NEXT_IP4_REWRITE : next1;
357 }
358 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +0200359 thread_index,
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700360 map_domain_index1, 1,
361 clib_net_to_host_u16
362 (ip41->length));
363 }
364 }
365
366 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
367 {
368 map_trace_t *tr = vlib_add_trace (vm, node, p0, sizeof (*tr));
369 tr->map_domain_index = map_domain_index0;
370 tr->port = port0;
371 }
372
373 if (PREDICT_FALSE (p1->flags & VLIB_BUFFER_IS_TRACED))
374 {
375 map_trace_t *tr = vlib_add_trace (vm, node, p1, sizeof (*tr));
376 tr->map_domain_index = map_domain_index1;
377 tr->port = port1;
378 }
379
380 if (error0 == MAP_ERROR_DECAP_SEC_CHECK && mm->icmp6_enabled)
381 {
382 /* Set ICMP parameters */
383 vlib_buffer_advance (p0, -sizeof (ip6_header_t));
384 icmp6_error_set_vnet_buffer (p0, ICMP6_destination_unreachable,
385 ICMP6_destination_unreachable_source_address_failed_policy,
386 0);
387 next0 = IP6_MAP_NEXT_ICMP;
388 }
389 else
390 {
391 next0 = (error0 == MAP_ERROR_NONE) ? next0 : IP6_MAP_NEXT_DROP;
392 }
393
394 if (error1 == MAP_ERROR_DECAP_SEC_CHECK && mm->icmp6_enabled)
395 {
396 /* Set ICMP parameters */
397 vlib_buffer_advance (p1, -sizeof (ip6_header_t));
398 icmp6_error_set_vnet_buffer (p1, ICMP6_destination_unreachable,
399 ICMP6_destination_unreachable_source_address_failed_policy,
400 0);
401 next1 = IP6_MAP_NEXT_ICMP;
402 }
403 else
404 {
405 next1 = (error1 == MAP_ERROR_NONE) ? next1 : IP6_MAP_NEXT_DROP;
406 }
407
408 /* Reset packet */
409 if (next0 == IP6_MAP_NEXT_IP6_LOCAL)
410 vlib_buffer_advance (p0, -sizeof (ip6_header_t));
411 if (next1 == IP6_MAP_NEXT_IP6_LOCAL)
412 vlib_buffer_advance (p1, -sizeof (ip6_header_t));
413
414 p0->error = error_node->errors[error0];
415 p1->error = error_node->errors[error1];
416 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
417 n_left_to_next, pi0, pi1, next0,
418 next1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700421 /* Single loop */
422 while (n_left_from > 0 && n_left_to_next > 0)
423 {
424 u32 pi0;
425 vlib_buffer_t *p0;
426 u8 error0 = MAP_ERROR_NONE;
427 map_domain_t *d0 = 0;
428 ip4_header_t *ip40;
429 ip6_header_t *ip60;
430 i32 port0 = 0;
431 u32 map_domain_index0 = ~0;
432 u32 next0 = IP6_MAP_NEXT_IP4_LOOKUP;
433
434 pi0 = to_next[0] = from[0];
435 from += 1;
436 n_left_from -= 1;
437 to_next += 1;
438 n_left_to_next -= 1;
439
440 p0 = vlib_get_buffer (vm, pi0);
441 ip60 = vlib_buffer_get_current (p0);
442 vlib_buffer_advance (p0, sizeof (ip6_header_t));
443 ip40 = vlib_buffer_get_current (p0);
444
445 /*
446 * Encapsulated IPv4 packet
447 * - IPv4 fragmented -> Pass to virtual reassembly unless security check disabled
448 * - Lookup/Rewrite or Fragment node in case of packet > MTU
449 * Fragmented IPv6 packet
450 * ICMP IPv6 packet
451 * - Error -> Pass to ICMPv6/ICMPv4 relay
452 * - Info -> Pass to IPv6 local
453 * Anything else -> drop
454 */
455 if (PREDICT_TRUE
456 (ip60->protocol == IP_PROTOCOL_IP_IN_IP
457 && clib_net_to_host_u16 (ip60->payload_length) > 20))
458 {
459 d0 =
460 ip6_map_get_domain (vnet_buffer (p0)->ip.adj_index[VLIB_TX],
Matus Fabiana774b532017-05-02 03:15:22 -0700461 (ip4_address_t *) & ip40->
462 src_address.as_u32, &map_domain_index0,
463 &error0);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700464 }
465 else if (ip60->protocol == IP_PROTOCOL_ICMP6 &&
466 clib_net_to_host_u16 (ip60->payload_length) >
467 sizeof (icmp46_header_t))
468 {
469 icmp46_header_t *icmp = (void *) (ip60 + 1);
470 next0 = (icmp->type == ICMP6_echo_request
471 || icmp->type ==
472 ICMP6_echo_reply) ? IP6_MAP_NEXT_IP6_LOCAL :
473 IP6_MAP_NEXT_IP6_ICMP_RELAY;
474 }
475 else if (ip60->protocol == IP_PROTOCOL_IPV6_FRAGMENTATION &&
476 (((ip6_frag_hdr_t *) (ip60 + 1))->next_hdr ==
477 IP_PROTOCOL_IP_IN_IP))
478 {
479 next0 = IP6_MAP_NEXT_IP6_REASS;
480 }
481 else
482 {
483 error0 = MAP_ERROR_BAD_PROTOCOL;
484 }
485
486 if (d0)
487 {
488 /* MAP inbound security check */
489 ip6_map_security_check (d0, ip40, ip60, &next0, &error0);
490
491 if (PREDICT_TRUE (error0 == MAP_ERROR_NONE &&
492 next0 == IP6_MAP_NEXT_IP4_LOOKUP))
493 {
494 if (PREDICT_FALSE
495 (d0->mtu
496 && (clib_host_to_net_u16 (ip40->length) > d0->mtu)))
497 {
498 vnet_buffer (p0)->ip_frag.header_offset = 0;
499 vnet_buffer (p0)->ip_frag.flags = 0;
500 vnet_buffer (p0)->ip_frag.next_index =
501 IP4_FRAG_NEXT_IP4_LOOKUP;
502 vnet_buffer (p0)->ip_frag.mtu = d0->mtu;
503 next0 = IP6_MAP_NEXT_IP4_FRAGMENT;
504 }
505 else
506 {
507 next0 =
508 ip6_map_ip4_lookup_bypass (p0,
509 ip40) ?
510 IP6_MAP_NEXT_IP4_REWRITE : next0;
511 }
512 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +0200513 thread_index,
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700514 map_domain_index0, 1,
515 clib_net_to_host_u16
516 (ip40->length));
517 }
518 }
519
520 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
521 {
522 map_trace_t *tr = vlib_add_trace (vm, node, p0, sizeof (*tr));
523 tr->map_domain_index = map_domain_index0;
524 tr->port = (u16) port0;
525 }
526
527 if (mm->icmp6_enabled &&
528 (error0 == MAP_ERROR_DECAP_SEC_CHECK
529 || error0 == MAP_ERROR_NO_DOMAIN))
530 {
531 /* Set ICMP parameters */
532 vlib_buffer_advance (p0, -sizeof (ip6_header_t));
533 icmp6_error_set_vnet_buffer (p0, ICMP6_destination_unreachable,
534 ICMP6_destination_unreachable_source_address_failed_policy,
535 0);
536 next0 = IP6_MAP_NEXT_ICMP;
537 }
538 else
539 {
540 next0 = (error0 == MAP_ERROR_NONE) ? next0 : IP6_MAP_NEXT_DROP;
541 }
542
543 /* Reset packet */
544 if (next0 == IP6_MAP_NEXT_IP6_LOCAL)
545 vlib_buffer_advance (p0, -sizeof (ip6_header_t));
546
547 p0->error = error_node->errors[error0];
548 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
549 n_left_to_next, pi0, next0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700551 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552 }
553
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554 return frame->n_vectors;
555}
556
557
558static_always_inline void
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700559ip6_map_ip6_reass_prepare (vlib_main_t * vm, vlib_node_runtime_t * node,
560 map_ip6_reass_t * r, u32 ** fragments_ready,
561 u32 ** fragments_to_drop)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562{
563 ip4_header_t *ip40;
564 ip6_header_t *ip60;
565 ip6_frag_hdr_t *frag0;
566 vlib_buffer_t *p0;
567
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700568 if (!r->ip4_header.ip_version_and_header_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569 return;
570
571 //The IP header is here, we need to check for packets
572 //that can be forwarded
573 int i;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700574 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
575 {
576 if (r->fragments[i].pi == ~0 ||
577 ((!r->fragments[i].next_data_len)
578 && (r->fragments[i].next_data_offset != (0xffff))))
579 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700581 p0 = vlib_get_buffer (vm, r->fragments[i].pi);
582 ip60 = vlib_buffer_get_current (p0);
583 frag0 = (ip6_frag_hdr_t *) (ip60 + 1);
584 ip40 = (ip4_header_t *) (frag0 + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700586 if (ip6_frag_hdr_offset (frag0))
587 {
588 //Not first fragment, add the IPv4 header
589 clib_memcpy (ip40, &r->ip4_header, 20);
590 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591
592#ifdef MAP_IP6_REASS_COUNT_BYTES
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700593 r->forwarded +=
594 clib_net_to_host_u16 (ip60->payload_length) - sizeof (*frag0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595#endif
596
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700597 if (ip6_frag_hdr_more (frag0))
598 {
599 //Not last fragment, we copy end of next
600 clib_memcpy (u8_ptr_add (ip60, p0->current_length),
601 r->fragments[i].next_data, 20);
602 p0->current_length += 20;
603 ip60->payload_length = u16_net_add (ip60->payload_length, 20);
604 }
605
606 if (!ip4_is_fragment (ip40))
607 {
608 ip40->fragment_id = frag_id_6to4 (frag0->identification);
609 ip40->flags_and_fragment_offset =
610 clib_host_to_net_u16 (ip6_frag_hdr_offset (frag0));
611 }
612 else
613 {
614 ip40->flags_and_fragment_offset =
615 clib_host_to_net_u16 (ip4_get_fragment_offset (ip40) +
616 ip6_frag_hdr_offset (frag0));
617 }
618
619 if (ip6_frag_hdr_more (frag0))
620 ip40->flags_and_fragment_offset |=
621 clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
622
623 ip40->length =
624 clib_host_to_net_u16 (p0->current_length - sizeof (*ip60) -
625 sizeof (*frag0));
626 ip40->checksum = ip4_header_checksum (ip40);
627
628 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
629 {
630 map_ip6_map_ip6_reass_trace_t *tr =
631 vlib_add_trace (vm, node, p0, sizeof (*tr));
632 tr->offset = ip4_get_fragment_offset (ip40);
633 tr->frag_len = clib_net_to_host_u16 (ip40->length) - sizeof (*ip40);
634 tr->out = 1;
635 }
636
637 vec_add1 (*fragments_ready, r->fragments[i].pi);
638 r->fragments[i].pi = ~0;
639 r->fragments[i].next_data_len = 0;
640 r->fragments[i].next_data_offset = 0;
641 map_main.ip6_reass_buffered_counter--;
642
643 //TODO: Best solution would be that ip6_map handles extension headers
644 // and ignores atomic fragment. But in the meantime, let's just copy the header.
645
646 u8 protocol = frag0->next_hdr;
647 memmove (u8_ptr_add (ip40, -sizeof (*ip60)), ip60, sizeof (*ip60));
648 ((ip6_header_t *) u8_ptr_add (ip40, -sizeof (*ip60)))->protocol =
649 protocol;
650 vlib_buffer_advance (p0, sizeof (*frag0));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700652}
653
654void
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700655map_ip6_drop_pi (u32 pi)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700657 vlib_main_t *vm = vlib_get_main ();
658 vlib_node_runtime_t *n =
659 vlib_node_get_runtime (vm, ip6_map_ip6_reass_node.index);
660 vlib_set_next_frame_buffer (vm, n, IP6_MAP_IP6_REASS_NEXT_DROP, pi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661}
662
663void
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700664map_ip4_drop_pi (u32 pi)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700666 vlib_main_t *vm = vlib_get_main ();
667 vlib_node_runtime_t *n =
668 vlib_node_get_runtime (vm, ip6_map_ip4_reass_node.index);
669 vlib_set_next_frame_buffer (vm, n, IP6_MAP_IP4_REASS_NEXT_DROP, pi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670}
671
672/*
673 * ip6_reass
674 * TODO: We should count the number of successfully
675 * transmitted fragment bytes and compare that to the last fragment
676 * offset such that we can free the reassembly structure when all fragments
677 * have been forwarded.
678 */
679static uword
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700680ip6_map_ip6_reass (vlib_main_t * vm,
681 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682{
683 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700684 vlib_node_runtime_t *error_node =
685 vlib_node_get_runtime (vm, ip6_map_ip6_reass_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686 u32 *fragments_to_drop = NULL;
687 u32 *fragments_ready = NULL;
688
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700689 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690 n_left_from = frame->n_vectors;
691 next_index = node->cached_next_index;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700692 while (n_left_from > 0)
693 {
694 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700696 /* Single loop */
697 while (n_left_from > 0 && n_left_to_next > 0)
698 {
699 u32 pi0;
700 vlib_buffer_t *p0;
701 u8 error0 = MAP_ERROR_NONE;
702 ip6_header_t *ip60;
703 ip6_frag_hdr_t *frag0;
704 u16 offset;
705 u16 next_offset;
706 u16 frag_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700708 pi0 = to_next[0] = from[0];
709 from += 1;
710 n_left_from -= 1;
711 to_next += 1;
712 n_left_to_next -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700714 p0 = vlib_get_buffer (vm, pi0);
715 ip60 = vlib_buffer_get_current (p0);
716 frag0 = (ip6_frag_hdr_t *) (ip60 + 1);
717 offset =
718 clib_host_to_net_u16 (frag0->fragment_offset_and_more) & (~7);
719 frag_len =
720 clib_net_to_host_u16 (ip60->payload_length) - sizeof (*frag0);
721 next_offset =
722 ip6_frag_hdr_more (frag0) ? (offset + frag_len) : (0xffff);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700724 //FIXME: Support other extension headers, maybe
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700726 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
727 {
728 map_ip6_map_ip6_reass_trace_t *tr =
729 vlib_add_trace (vm, node, p0, sizeof (*tr));
730 tr->offset = offset;
731 tr->frag_len = frag_len;
732 tr->out = 0;
733 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700735 map_ip6_reass_lock ();
736 map_ip6_reass_t *r =
737 map_ip6_reass_get (&ip60->src_address, &ip60->dst_address,
738 frag0->identification, frag0->next_hdr,
739 &fragments_to_drop);
740 //FIXME: Use better error codes
741 if (PREDICT_FALSE (!r))
742 {
743 // Could not create a caching entry
744 error0 = MAP_ERROR_FRAGMENT_MEMORY;
745 }
746 else if (PREDICT_FALSE ((frag_len <= 20 &&
747 (ip6_frag_hdr_more (frag0) || (!offset)))))
748 {
749 //Very small fragment are restricted to the last one and
750 //can't be the first one
751 error0 = MAP_ERROR_FRAGMENT_MALFORMED;
752 }
753 else
754 if (map_ip6_reass_add_fragment
755 (r, pi0, offset, next_offset, (u8 *) (frag0 + 1), frag_len))
756 {
757 map_ip6_reass_free (r, &fragments_to_drop);
758 error0 = MAP_ERROR_FRAGMENT_MEMORY;
759 }
760 else
761 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762#ifdef MAP_IP6_REASS_COUNT_BYTES
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700763 if (!ip6_frag_hdr_more (frag0))
764 r->expected_total = offset + frag_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765#endif
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700766 ip6_map_ip6_reass_prepare (vm, node, r, &fragments_ready,
767 &fragments_to_drop);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768#ifdef MAP_IP6_REASS_COUNT_BYTES
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700769 if (r->forwarded >= r->expected_total)
770 map_ip6_reass_free (r, &fragments_to_drop);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771#endif
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700772 }
773 map_ip6_reass_unlock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700775 if (error0 == MAP_ERROR_NONE)
776 {
777 if (frag_len > 20)
778 {
779 //Dequeue the packet
780 n_left_to_next++;
781 to_next--;
782 }
783 else
784 {
785 //All data from that packet was copied no need to keep it, but this is not an error
786 p0->error = error_node->errors[MAP_ERROR_NONE];
787 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
788 to_next, n_left_to_next,
789 pi0,
790 IP6_MAP_IP6_REASS_NEXT_DROP);
791 }
792 }
793 else
794 {
795 p0->error = error_node->errors[error0];
796 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
797 n_left_to_next, pi0,
798 IP6_MAP_IP6_REASS_NEXT_DROP);
799 }
800 }
801 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700802 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700803
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700804 map_send_all_to_node (vm, fragments_ready, node,
805 &error_node->errors[MAP_ERROR_NONE],
806 IP6_MAP_IP6_REASS_NEXT_IP6_MAP);
807 map_send_all_to_node (vm, fragments_to_drop, node,
808 &error_node->errors[MAP_ERROR_FRAGMENT_DROPPED],
809 IP6_MAP_IP6_REASS_NEXT_DROP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700810
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700811 vec_free (fragments_to_drop);
812 vec_free (fragments_ready);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700813 return frame->n_vectors;
814}
815
816/*
817 * ip6_ip4_virt_reass
818 */
819static uword
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700820ip6_map_ip4_reass (vlib_main_t * vm,
821 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700822{
823 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700824 vlib_node_runtime_t *error_node =
825 vlib_node_get_runtime (vm, ip6_map_ip4_reass_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700826 map_main_t *mm = &map_main;
827 vlib_combined_counter_main_t *cm = mm->domain_counters;
Damjan Marion586afd72017-04-05 19:18:20 +0200828 u32 thread_index = vlib_get_thread_index ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700829 u32 *fragments_to_drop = NULL;
830 u32 *fragments_to_loopback = NULL;
831
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700832 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700833 n_left_from = frame->n_vectors;
834 next_index = node->cached_next_index;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700835 while (n_left_from > 0)
836 {
837 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700838
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700839 /* Single loop */
840 while (n_left_from > 0 && n_left_to_next > 0)
841 {
842 u32 pi0;
843 vlib_buffer_t *p0;
844 u8 error0 = MAP_ERROR_NONE;
845 map_domain_t *d0;
846 ip4_header_t *ip40;
847 ip6_header_t *ip60;
848 i32 port0 = 0;
849 u32 map_domain_index0 = ~0;
850 u32 next0 = IP6_MAP_IP4_REASS_NEXT_IP4_LOOKUP;
851 u8 cached = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700852
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700853 pi0 = to_next[0] = from[0];
854 from += 1;
855 n_left_from -= 1;
856 to_next += 1;
857 n_left_to_next -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700858
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700859 p0 = vlib_get_buffer (vm, pi0);
860 ip40 = vlib_buffer_get_current (p0);
861 ip60 = ((ip6_header_t *) ip40) - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700862
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700863 d0 =
864 ip6_map_get_domain (vnet_buffer (p0)->ip.adj_index[VLIB_TX],
865 (ip4_address_t *) & ip40->src_address.as_u32,
866 &map_domain_index0, &error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700868 map_ip4_reass_lock ();
869 //This node only deals with fragmented ip4
Ed Warnicke853e7202016-08-12 11:42:26 -0700870 map_ip4_reass_t *r = map_ip4_reass_get (ip40->src_address.as_u32,
871 ip40->dst_address.as_u32,
872 ip40->fragment_id,
873 ip40->protocol,
874 &fragments_to_drop);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700875 if (PREDICT_FALSE (!r))
876 {
877 // Could not create a caching entry
878 error0 = MAP_ERROR_FRAGMENT_MEMORY;
879 }
880 else if (PREDICT_TRUE (ip4_get_fragment_offset (ip40)))
881 {
882 // This is a fragment
883 if (r->port >= 0)
884 {
885 // We know the port already
886 port0 = r->port;
887 }
888 else if (map_ip4_reass_add_fragment (r, pi0))
889 {
890 // Not enough space for caching
891 error0 = MAP_ERROR_FRAGMENT_MEMORY;
892 map_ip4_reass_free (r, &fragments_to_drop);
893 }
894 else
895 {
896 cached = 1;
897 }
898 }
Matus Fabiana774b532017-05-02 03:15:22 -0700899 else if ((port0 = ip4_get_port (ip40, 1)) == 0)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700900 {
901 // Could not find port from first fragment. Stop reassembling.
902 error0 = MAP_ERROR_BAD_PROTOCOL;
903 port0 = 0;
904 map_ip4_reass_free (r, &fragments_to_drop);
905 }
906 else
907 {
908 // Found port. Remember it and loopback saved fragments
909 r->port = port0;
910 map_ip4_reass_get_fragments (r, &fragments_to_loopback);
911 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700912
913#ifdef MAP_IP4_REASS_COUNT_BYTES
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700914 if (!cached && r)
915 {
916 r->forwarded += clib_host_to_net_u16 (ip40->length) - 20;
917 if (!ip4_get_fragment_more (ip40))
918 r->expected_total =
919 ip4_get_fragment_offset (ip40) * 8 +
920 clib_host_to_net_u16 (ip40->length) - 20;
921 if (r->forwarded >= r->expected_total)
922 map_ip4_reass_free (r, &fragments_to_drop);
923 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924#endif
925
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700926 map_ip4_reass_unlock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700927
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700928 if (PREDICT_TRUE (error0 == MAP_ERROR_NONE))
929 error0 =
930 ip6_map_sec_check (d0, port0, ip40,
931 ip60) ? MAP_ERROR_NONE :
932 MAP_ERROR_DECAP_SEC_CHECK;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700933
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700934 if (PREDICT_FALSE
935 (d0->mtu && (clib_host_to_net_u16 (ip40->length) > d0->mtu)
936 && error0 == MAP_ERROR_NONE && !cached))
937 {
938 vnet_buffer (p0)->ip_frag.header_offset = 0;
939 vnet_buffer (p0)->ip_frag.flags = 0;
940 vnet_buffer (p0)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP;
941 vnet_buffer (p0)->ip_frag.mtu = d0->mtu;
942 next0 = IP6_MAP_IP4_REASS_NEXT_IP4_FRAGMENT;
943 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700944
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700945 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
946 {
947 map_ip6_map_ip4_reass_trace_t *tr =
948 vlib_add_trace (vm, node, p0, sizeof (*tr));
949 tr->map_domain_index = map_domain_index0;
950 tr->port = port0;
951 tr->cached = cached;
952 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700953
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700954 if (cached)
955 {
956 //Dequeue the packet
957 n_left_to_next++;
958 to_next--;
959 }
960 else
961 {
962 if (error0 == MAP_ERROR_NONE)
963 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +0200964 thread_index,
965 map_domain_index0, 1,
Ed Warnicke853e7202016-08-12 11:42:26 -0700966 clib_net_to_host_u16
967 (ip40->length));
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700968 next0 =
969 (error0 ==
970 MAP_ERROR_NONE) ? next0 : IP6_MAP_IP4_REASS_NEXT_DROP;
971 p0->error = error_node->errors[error0];
972 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
973 n_left_to_next, pi0, next0);
974 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700976 //Loopback when we reach the end of the inpu vector
977 if (n_left_from == 0 && vec_len (fragments_to_loopback))
978 {
979 from = vlib_frame_vector_args (frame);
980 u32 len = vec_len (fragments_to_loopback);
981 if (len <= VLIB_FRAME_SIZE)
982 {
983 clib_memcpy (from, fragments_to_loopback,
984 sizeof (u32) * len);
985 n_left_from = len;
986 vec_reset_length (fragments_to_loopback);
987 }
988 else
989 {
990 clib_memcpy (from,
991 fragments_to_loopback + (len -
992 VLIB_FRAME_SIZE),
993 sizeof (u32) * VLIB_FRAME_SIZE);
994 n_left_from = VLIB_FRAME_SIZE;
995 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
996 }
997 }
998 }
999 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001001 map_send_all_to_node (vm, fragments_to_drop, node,
1002 &error_node->errors[MAP_ERROR_FRAGMENT_DROPPED],
1003 IP6_MAP_IP4_REASS_NEXT_DROP);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001004
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001005 vec_free (fragments_to_drop);
1006 vec_free (fragments_to_loopback);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001007 return frame->n_vectors;
1008}
1009
1010/*
1011 * ip6_icmp_relay
1012 */
1013static uword
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001014ip6_map_icmp_relay (vlib_main_t * vm,
1015 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016{
1017 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001018 vlib_node_runtime_t *error_node =
1019 vlib_node_get_runtime (vm, ip6_map_icmp_relay_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001020 map_main_t *mm = &map_main;
Damjan Marion586afd72017-04-05 19:18:20 +02001021 u32 thread_index = vlib_get_thread_index ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001022 u16 *fragment_ids, *fid;
1023
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001024 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025 n_left_from = frame->n_vectors;
1026 next_index = node->cached_next_index;
1027
1028 /* Get random fragment IDs for replies. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001029 fid = fragment_ids =
1030 clib_random_buffer_get_data (&vm->random_buffer,
1031 n_left_from * sizeof (fragment_ids[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001032
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001033 while (n_left_from > 0)
1034 {
1035 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001036
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001037 /* Single loop */
1038 while (n_left_from > 0 && n_left_to_next > 0)
1039 {
1040 u32 pi0;
1041 vlib_buffer_t *p0;
1042 u8 error0 = MAP_ERROR_NONE;
1043 ip6_header_t *ip60;
1044 u32 next0 = IP6_ICMP_RELAY_NEXT_IP4_LOOKUP;
1045 u32 mtu;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001046
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001047 pi0 = to_next[0] = from[0];
1048 from += 1;
1049 n_left_from -= 1;
1050 to_next += 1;
1051 n_left_to_next -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001052
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001053 p0 = vlib_get_buffer (vm, pi0);
1054 ip60 = vlib_buffer_get_current (p0);
1055 u16 tlen = clib_net_to_host_u16 (ip60->payload_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001056
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001057 /*
1058 * In:
1059 * IPv6 header (40)
Damjan Marion607de1a2016-08-16 22:53:54 +02001060 * ICMPv6 header (8)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001061 * IPv6 header (40)
1062 * Original IPv4 header / packet
1063 * Out:
1064 * New IPv4 header
1065 * New ICMP header
1066 * Original IPv4 header / packet
1067 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001068
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001069 /* Need at least ICMP(8) + IPv6(40) + IPv4(20) + L4 header(8) */
1070 if (tlen < 76)
1071 {
1072 error0 = MAP_ERROR_ICMP_RELAY;
1073 goto error;
1074 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001075
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001076 icmp46_header_t *icmp60 = (icmp46_header_t *) (ip60 + 1);
1077 ip6_header_t *inner_ip60 = (ip6_header_t *) (icmp60 + 2);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001078
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001079 if (inner_ip60->protocol != IP_PROTOCOL_IP_IN_IP)
1080 {
1081 error0 = MAP_ERROR_ICMP_RELAY;
1082 goto error;
1083 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001085 ip4_header_t *inner_ip40 = (ip4_header_t *) (inner_ip60 + 1);
1086 vlib_buffer_advance (p0, 60); /* sizeof ( IPv6 + ICMP + IPv6 - IPv4 - ICMP ) */
1087 ip4_header_t *new_ip40 = vlib_buffer_get_current (p0);
1088 icmp46_header_t *new_icmp40 = (icmp46_header_t *) (new_ip40 + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001089
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001090 /*
1091 * Relay according to RFC2473, section 8.3
1092 */
1093 switch (icmp60->type)
1094 {
1095 case ICMP6_destination_unreachable:
1096 case ICMP6_time_exceeded:
1097 case ICMP6_parameter_problem:
1098 /* Type 3 - destination unreachable, Code 1 - host unreachable */
1099 new_icmp40->type = ICMP4_destination_unreachable;
1100 new_icmp40->code =
1101 ICMP4_destination_unreachable_destination_unreachable_host;
1102 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001103
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001104 case ICMP6_packet_too_big:
1105 /* Type 3 - destination unreachable, Code 4 - packet too big */
1106 /* Potential TODO: Adjust domain tunnel MTU based on the value received here */
1107 mtu = clib_net_to_host_u32 (*((u32 *) (icmp60 + 1)));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001108
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001109 /* Check DF flag */
1110 if (!
Ed Warnicke853e7202016-08-12 11:42:26 -07001111 (inner_ip40->flags_and_fragment_offset &
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001112 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT)))
1113 {
1114 error0 = MAP_ERROR_ICMP_RELAY;
1115 goto error;
1116 }
1117
1118 new_icmp40->type = ICMP4_destination_unreachable;
1119 new_icmp40->code =
1120 ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set;
1121 *((u32 *) (new_icmp40 + 1)) =
1122 clib_host_to_net_u32 (mtu < 1280 ? 1280 : mtu);
1123 break;
1124
1125 default:
1126 error0 = MAP_ERROR_ICMP_RELAY;
1127 break;
1128 }
1129
1130 /*
1131 * Ensure the total ICMP packet is no longer than 576 bytes (RFC1812)
1132 */
1133 new_ip40->ip_version_and_header_length = 0x45;
1134 new_ip40->tos = 0;
1135 u16 nlen = (tlen - 20) > 576 ? 576 : tlen - 20;
1136 new_ip40->length = clib_host_to_net_u16 (nlen);
1137 new_ip40->fragment_id = fid[0];
1138 fid++;
1139 new_ip40->ttl = 64;
1140 new_ip40->protocol = IP_PROTOCOL_ICMP;
1141 new_ip40->src_address = mm->icmp4_src_address;
1142 new_ip40->dst_address = inner_ip40->src_address;
1143 new_ip40->checksum = ip4_header_checksum (new_ip40);
1144
1145 new_icmp40->checksum = 0;
1146 ip_csum_t sum = ip_incremental_checksum (0, new_icmp40, nlen - 20);
1147 new_icmp40->checksum = ~ip_csum_fold (sum);
1148
Damjan Marion586afd72017-04-05 19:18:20 +02001149 vlib_increment_simple_counter (&mm->icmp_relayed, thread_index, 0,
1150 1);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001151
1152 error:
1153 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
1154 {
1155 map_trace_t *tr = vlib_add_trace (vm, node, p0, sizeof (*tr));
1156 tr->map_domain_index = 0;
1157 tr->port = 0;
1158 }
1159
1160 next0 =
1161 (error0 == MAP_ERROR_NONE) ? next0 : IP6_ICMP_RELAY_NEXT_DROP;
1162 p0->error = error_node->errors[error0];
1163 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1164 n_left_to_next, pi0, next0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001165 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001166 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001167 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001168
1169 return frame->n_vectors;
1170
1171}
1172
1173static char *map_error_strings[] = {
1174#define _(sym,string) string,
1175 foreach_map_error
1176#undef _
1177};
1178
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001179/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001180VLIB_REGISTER_NODE(ip6_map_node) = {
1181 .function = ip6_map,
1182 .name = "ip6-map",
1183 .vector_size = sizeof(u32),
1184 .format_trace = format_map_trace,
1185 .type = VLIB_NODE_TYPE_INTERNAL,
1186
1187 .n_errors = MAP_N_ERROR,
1188 .error_strings = map_error_strings,
1189
1190 .n_next_nodes = IP6_MAP_N_NEXT,
1191 .next_nodes = {
1192 [IP6_MAP_NEXT_IP4_LOOKUP] = "ip4-lookup",
1193#ifdef MAP_SKIP_IP6_LOOKUP
Neale Ranns80823802017-02-20 18:23:41 -08001194 [IP6_MAP_NEXT_IP4_REWRITE] = "ip4-load-balance",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001195#endif
1196 [IP6_MAP_NEXT_IP6_REASS] = "ip6-map-ip6-reass",
1197 [IP6_MAP_NEXT_IP4_REASS] = "ip6-map-ip4-reass",
1198 [IP6_MAP_NEXT_IP4_FRAGMENT] = "ip4-frag",
1199 [IP6_MAP_NEXT_IP6_ICMP_RELAY] = "ip6-map-icmp-relay",
1200 [IP6_MAP_NEXT_IP6_LOCAL] = "ip6-local",
1201 [IP6_MAP_NEXT_DROP] = "error-drop",
Ole Troancda94822016-01-07 14:37:25 +01001202 [IP6_MAP_NEXT_ICMP] = "ip6-icmp-error",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001203 },
1204};
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001205/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001206
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001207/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001208VLIB_REGISTER_NODE(ip6_map_ip6_reass_node) = {
1209 .function = ip6_map_ip6_reass,
1210 .name = "ip6-map-ip6-reass",
1211 .vector_size = sizeof(u32),
1212 .format_trace = format_ip6_map_ip6_reass_trace,
1213 .type = VLIB_NODE_TYPE_INTERNAL,
1214 .n_errors = MAP_N_ERROR,
1215 .error_strings = map_error_strings,
1216 .n_next_nodes = IP6_MAP_IP6_REASS_N_NEXT,
1217 .next_nodes = {
1218 [IP6_MAP_IP6_REASS_NEXT_IP6_MAP] = "ip6-map",
1219 [IP6_MAP_IP6_REASS_NEXT_DROP] = "error-drop",
1220 },
1221};
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001222/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001223
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001224/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001225VLIB_REGISTER_NODE(ip6_map_ip4_reass_node) = {
1226 .function = ip6_map_ip4_reass,
1227 .name = "ip6-map-ip4-reass",
1228 .vector_size = sizeof(u32),
1229 .format_trace = format_ip6_map_ip4_reass_trace,
1230 .type = VLIB_NODE_TYPE_INTERNAL,
1231 .n_errors = MAP_N_ERROR,
1232 .error_strings = map_error_strings,
1233 .n_next_nodes = IP6_MAP_IP4_REASS_N_NEXT,
1234 .next_nodes = {
1235 [IP6_MAP_IP4_REASS_NEXT_IP4_LOOKUP] = "ip4-lookup",
1236 [IP6_MAP_IP4_REASS_NEXT_IP4_FRAGMENT] = "ip4-frag",
1237 [IP6_MAP_IP4_REASS_NEXT_DROP] = "error-drop",
1238 },
1239};
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001240/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001241
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001242/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001243VLIB_REGISTER_NODE(ip6_map_icmp_relay_node, static) = {
1244 .function = ip6_map_icmp_relay,
1245 .name = "ip6-map-icmp-relay",
1246 .vector_size = sizeof(u32),
1247 .format_trace = format_map_trace, //FIXME
1248 .type = VLIB_NODE_TYPE_INTERNAL,
1249 .n_errors = MAP_N_ERROR,
1250 .error_strings = map_error_strings,
1251 .n_next_nodes = IP6_ICMP_RELAY_N_NEXT,
1252 .next_nodes = {
1253 [IP6_ICMP_RELAY_NEXT_IP4_LOOKUP] = "ip4-lookup",
1254 [IP6_ICMP_RELAY_NEXT_DROP] = "error-drop",
1255 },
1256};
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001257/* *INDENT-ON* */
1258
1259/*
1260 * fd.io coding-style-patch-verification: ON
1261 *
1262 * Local Variables:
1263 * eval: (c-set-style "gnu")
1264 * End:
1265 */