e205c60e29a248281e928fdeeafc491367a09961
[vpp.git] / src / plugins / map / ip6_map_t.c
1 /*
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 <vnet/ip/ip4_to_ip6.h>
18 #include <vnet/ip/ip6_to_ip4.h>
19 #include <vnet/ip/ip_frag.h>
20
21 typedef enum
22 {
23   IP6_MAPT_NEXT_MAPT_TCP_UDP,
24   IP6_MAPT_NEXT_MAPT_ICMP,
25   IP6_MAPT_NEXT_MAPT_FRAGMENTED,
26   IP6_MAPT_NEXT_DROP,
27   IP6_MAPT_N_NEXT
28 } ip6_mapt_next_t;
29
30 typedef enum
31 {
32   IP6_MAPT_ICMP_NEXT_IP4_LOOKUP,
33   IP6_MAPT_ICMP_NEXT_IP4_FRAG,
34   IP6_MAPT_ICMP_NEXT_DROP,
35   IP6_MAPT_ICMP_N_NEXT
36 } ip6_mapt_icmp_next_t;
37
38 typedef enum
39 {
40   IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP,
41   IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG,
42   IP6_MAPT_TCP_UDP_NEXT_DROP,
43   IP6_MAPT_TCP_UDP_N_NEXT
44 } ip6_mapt_tcp_udp_next_t;
45
46 typedef enum
47 {
48   IP6_MAPT_FRAGMENTED_NEXT_IP4_LOOKUP,
49   IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG,
50   IP6_MAPT_FRAGMENTED_NEXT_DROP,
51   IP6_MAPT_FRAGMENTED_N_NEXT
52 } ip6_mapt_fragmented_next_t;
53
54 typedef struct
55 {
56   map_domain_t *d;
57   u16 sender_port;
58 } icmp6_to_icmp_ctx_t;
59
60 static int
61 ip6_to_ip4_set_icmp_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *arg)
62 {
63   icmp6_to_icmp_ctx_t *ctx = arg;
64   u32 ip4_sadr;
65
66   // Security check
67   // Note that this prevents an intermediate IPv6 router from answering
68   // the request.
69   ip4_sadr = map_get_ip4 (&ip6->src_address, ctx->d->ip6_src_len);
70   if (ip6->src_address.as_u64[0] !=
71       map_get_pfx_net (ctx->d, ip4_sadr, ctx->sender_port)
72       || ip6->src_address.as_u64[1] != map_get_sfx_net (ctx->d, ip4_sadr,
73                                                         ctx->sender_port))
74     return -1;
75
76   ip4->dst_address.as_u32 =
77     ip6_map_t_embedded_address (ctx->d, &ip6->dst_address);
78   ip4->src_address.as_u32 = ip4_sadr;
79
80   return 0;
81 }
82
83 static int
84 ip6_to_ip4_set_inner_icmp_cb (ip6_header_t * ip6, ip4_header_t * ip4,
85                               void *arg)
86 {
87   icmp6_to_icmp_ctx_t *ctx = arg;
88   u32 inner_ip4_dadr;
89
90   //Security check of inner packet
91   inner_ip4_dadr = map_get_ip4 (&ip6->dst_address, ctx->d->ip6_src_len);
92   if (ip6->dst_address.as_u64[0] !=
93       map_get_pfx_net (ctx->d, inner_ip4_dadr, ctx->sender_port)
94       || ip6->dst_address.as_u64[1] != map_get_sfx_net (ctx->d,
95                                                         inner_ip4_dadr,
96                                                         ctx->sender_port))
97     return -1;
98
99   ip4->dst_address.as_u32 = inner_ip4_dadr;
100   ip4->src_address.as_u32 =
101     ip6_map_t_embedded_address (ctx->d, &ip6->src_address);
102
103   return 0;
104 }
105
106 static uword
107 ip6_map_t_icmp (vlib_main_t * vm,
108                 vlib_node_runtime_t * node, vlib_frame_t * frame)
109 {
110   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
111   vlib_node_runtime_t *error_node =
112     vlib_node_get_runtime (vm, ip6_map_t_icmp_node.index);
113   from = vlib_frame_vector_args (frame);
114   n_left_from = frame->n_vectors;
115   next_index = node->cached_next_index;
116   vlib_combined_counter_main_t *cm = map_main.domain_counters;
117   u32 thread_index = vm->thread_index;
118
119   while (n_left_from > 0)
120     {
121       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
122
123       while (n_left_from > 0 && n_left_to_next > 0)
124         {
125           u32 pi0;
126           vlib_buffer_t *p0;
127           u8 error0;
128           ip6_mapt_icmp_next_t next0;
129           map_domain_t *d0;
130           u16 len0;
131           icmp6_to_icmp_ctx_t ctx0;
132           ip6_header_t *ip60;
133
134           pi0 = to_next[0] = from[0];
135           from += 1;
136           n_left_from -= 1;
137           to_next += 1;
138           n_left_to_next -= 1;
139           error0 = MAP_ERROR_NONE;
140           next0 = IP6_MAPT_ICMP_NEXT_IP4_LOOKUP;
141
142           p0 = vlib_get_buffer (vm, pi0);
143           ip60 = vlib_buffer_get_current (p0);
144           len0 = clib_net_to_host_u16 (ip60->payload_length);
145           d0 =
146             pool_elt_at_index (map_main.domains,
147                                vnet_buffer (p0)->map_t.map_domain_index);
148           ctx0.d = d0;
149           ctx0.sender_port = 0;
150           if (!ip6_get_port
151               (vm, p0, ip60, p0->current_length, NULL, &ctx0.sender_port,
152                NULL, NULL, NULL, NULL))
153             {
154               // In case of 1:1 mapping, we don't care about the port
155               if (!(d0->ea_bits_len == 0 && d0->rules))
156                 {
157                   error0 = MAP_ERROR_ICMP;
158                   goto err0;
159                 }
160             }
161
162           if (icmp6_to_icmp (vm, p0, ip6_to_ip4_set_icmp_cb, &ctx0,
163                              ip6_to_ip4_set_inner_icmp_cb, &ctx0))
164             {
165               error0 = MAP_ERROR_ICMP;
166               goto err0;
167             }
168
169           if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
170             {
171               // Send to fragmentation node if necessary
172               vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
173               vnet_buffer (p0)->ip_frag.next_index = IP_FRAG_NEXT_IP4_LOOKUP;
174               next0 = IP6_MAPT_ICMP_NEXT_IP4_FRAG;
175             }
176         err0:
177           if (PREDICT_TRUE (error0 == MAP_ERROR_NONE))
178             {
179               vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
180                                                thread_index,
181                                                vnet_buffer (p0)->
182                                                map_t.map_domain_index, 1,
183                                                len0);
184             }
185           else
186             {
187               next0 = IP6_MAPT_ICMP_NEXT_DROP;
188             }
189
190           p0->error = error_node->errors[error0];
191           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
192                                            to_next, n_left_to_next, pi0,
193                                            next0);
194         }
195       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
196     }
197   return frame->n_vectors;
198 }
199
200 /*
201  * Translate IPv6 fragmented packet to IPv4.
202  */
203 always_inline int
204 map_ip6_to_ip4_fragmented (vlib_main_t * vm, vlib_buffer_t * p)
205 {
206   ip6_header_t *ip6;
207   ip6_frag_hdr_t *frag;
208   ip4_header_t *ip4;
209   u16 frag_id;
210   u8 frag_more;
211   u16 frag_offset;
212   u8 l4_protocol;
213   u16 l4_offset;
214
215   ip6 = vlib_buffer_get_current (p);
216
217   if (ip6_parse
218       (vm, p, ip6, p->current_length, &l4_protocol, &l4_offset, &frag_offset))
219     return -1;
220
221   frag = (ip6_frag_hdr_t *) u8_ptr_add (ip6, frag_offset);
222   ip4 = (ip4_header_t *) u8_ptr_add (ip6, l4_offset - sizeof (*ip4));
223   vlib_buffer_advance (p, l4_offset - sizeof (*ip4));
224
225   frag_id = frag_id_6to4 (frag->identification);
226   frag_more = ip6_frag_hdr_more (frag);
227   frag_offset = ip6_frag_hdr_offset (frag);
228
229   ip4->dst_address.as_u32 = vnet_buffer (p)->map_t.v6.daddr;
230   ip4->src_address.as_u32 = vnet_buffer (p)->map_t.v6.saddr;
231
232   ip4->ip_version_and_header_length =
233     IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
234   ip4->tos = ip6_translate_tos (ip6->ip_version_traffic_class_and_flow_label);
235   ip4->length =
236     u16_net_add (ip6->payload_length,
237                  sizeof (*ip4) - l4_offset + sizeof (*ip6));
238   ip4->fragment_id = frag_id;
239   ip4->flags_and_fragment_offset =
240     clib_host_to_net_u16 (frag_offset |
241                           (frag_more ? IP4_HEADER_FLAG_MORE_FRAGMENTS : 0));
242   ip4->ttl = ip6->hop_limit;
243   ip4->protocol =
244     (l4_protocol == IP_PROTOCOL_ICMP6) ? IP_PROTOCOL_ICMP : l4_protocol;
245   ip4->checksum = ip4_header_checksum (ip4);
246
247   return 0;
248 }
249
250 static uword
251 ip6_map_t_fragmented (vlib_main_t * vm,
252                       vlib_node_runtime_t * node, vlib_frame_t * frame)
253 {
254   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
255   from = vlib_frame_vector_args (frame);
256   n_left_from = frame->n_vectors;
257   next_index = node->cached_next_index;
258   vlib_node_runtime_t *error_node =
259     vlib_node_get_runtime (vm, ip6_map_t_fragmented_node.index);
260
261   while (n_left_from > 0)
262     {
263       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
264
265       while (n_left_from > 0 && n_left_to_next > 0)
266         {
267           u32 pi0;
268           vlib_buffer_t *p0;
269           u32 next0;
270
271           pi0 = to_next[0] = from[0];
272           from += 1;
273           n_left_from -= 1;
274           to_next += 1;
275           n_left_to_next -= 1;
276
277           next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
278           p0 = vlib_get_buffer (vm, pi0);
279
280           if (map_ip6_to_ip4_fragmented (vm, p0))
281             {
282               p0->error = error_node->errors[MAP_ERROR_FRAGMENT_DROPPED];
283               next0 = IP6_MAPT_FRAGMENTED_NEXT_DROP;
284             }
285           else
286             {
287               if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
288                 {
289                   // Send to fragmentation node if necessary
290                   vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
291                   vnet_buffer (p0)->ip_frag.next_index =
292                     IP_FRAG_NEXT_IP4_LOOKUP;
293                   next0 = IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG;
294                 }
295             }
296
297           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
298                                            to_next, n_left_to_next, pi0,
299                                            next0);
300         }
301       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
302     }
303   return frame->n_vectors;
304 }
305
306 /*
307  * Translate IPv6 UDP/TCP packet to IPv4.
308  * Returns 0 on success.
309  * Returns a non-zero error code on error.
310  */
311 always_inline int
312 map_ip6_to_ip4_tcp_udp (vlib_main_t * vm, vlib_buffer_t * p,
313                         bool udp_checksum)
314 {
315   map_main_t *mm = &map_main;
316   ip6_header_t *ip6;
317   u16 *checksum;
318   ip_csum_t csum = 0;
319   ip4_header_t *ip4;
320   u16 fragment_id;
321   u16 flags;
322   u16 frag_offset;
323   u8 l4_protocol;
324   u16 l4_offset;
325   ip6_address_t old_src, old_dst;
326
327   ip6 = vlib_buffer_get_current (p);
328
329   if (ip6_parse
330       (vm, p, ip6, p->current_length, &l4_protocol, &l4_offset, &frag_offset))
331     return -1;
332
333   if (l4_protocol == IP_PROTOCOL_TCP)
334     {
335       tcp_header_t *tcp = ip6_next_header (ip6);
336       if (mm->tcp_mss > 0)
337         {
338           csum = tcp->checksum;
339           map_mss_clamping (tcp, &csum, mm->tcp_mss);
340           tcp->checksum = ip_csum_fold (csum);
341         }
342       checksum = &tcp->checksum;
343     }
344   else
345     {
346       udp_header_t *udp = ip6_next_header (ip6);
347       checksum = &udp->checksum;
348     }
349
350   old_src.as_u64[0] = ip6->src_address.as_u64[0];
351   old_src.as_u64[1] = ip6->src_address.as_u64[1];
352   old_dst.as_u64[0] = ip6->dst_address.as_u64[0];
353   old_dst.as_u64[1] = ip6->dst_address.as_u64[1];
354
355   ip4 = (ip4_header_t *) u8_ptr_add (ip6, l4_offset - sizeof (*ip4));
356
357   vlib_buffer_advance (p, l4_offset - sizeof (*ip4));
358
359   if (PREDICT_FALSE (frag_offset))
360     {
361       // Only the first fragment
362       ip6_frag_hdr_t *hdr = (ip6_frag_hdr_t *) u8_ptr_add (ip6, frag_offset);
363       fragment_id = frag_id_6to4 (hdr->identification);
364       flags = clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
365     }
366   else
367     {
368       fragment_id = 0;
369       flags = 0;
370     }
371
372   ip4->dst_address.as_u32 = vnet_buffer (p)->map_t.v6.daddr;
373   ip4->src_address.as_u32 = vnet_buffer (p)->map_t.v6.saddr;
374
375   /*
376    * Drop spoofed packets that from a known domain source.
377    */
378   u32 map_domain_index = -1;
379   u8 error = 0;
380
381   ip4_map_get_domain (&ip4->src_address, &map_domain_index, &error);
382   if (error)
383     return error;
384
385   ip4->ip_version_and_header_length =
386     IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
387   ip4->tos = ip6_translate_tos (ip6->ip_version_traffic_class_and_flow_label);
388   ip4->length =
389     u16_net_add (ip6->payload_length,
390                  sizeof (*ip4) + sizeof (*ip6) - l4_offset);
391   ip4->fragment_id = fragment_id;
392   ip4->flags_and_fragment_offset = flags;
393   ip4->ttl = ip6->hop_limit;
394   ip4->protocol = l4_protocol;
395   ip4->checksum = ip4_header_checksum (ip4);
396
397   // UDP checksum is optional over IPv4
398   if (!udp_checksum && l4_protocol == IP_PROTOCOL_UDP)
399     {
400       *checksum = 0;
401     }
402   else
403     {
404       csum = ip_csum_sub_even (*checksum, old_src.as_u64[0]);
405       csum = ip_csum_sub_even (csum, old_src.as_u64[1]);
406       csum = ip_csum_sub_even (csum, old_dst.as_u64[0]);
407       csum = ip_csum_sub_even (csum, old_dst.as_u64[1]);
408       csum = ip_csum_add_even (csum, ip4->dst_address.as_u32);
409       csum = ip_csum_add_even (csum, ip4->src_address.as_u32);
410       *checksum = ip_csum_fold (csum);
411     }
412
413   return 0;
414 }
415
416 static uword
417 ip6_map_t_tcp_udp (vlib_main_t * vm,
418                    vlib_node_runtime_t * node, vlib_frame_t * frame)
419 {
420   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
421   vlib_node_runtime_t *error_node =
422     vlib_node_get_runtime (vm, ip6_map_t_tcp_udp_node.index);
423
424   from = vlib_frame_vector_args (frame);
425   n_left_from = frame->n_vectors;
426   next_index = node->cached_next_index;
427   while (n_left_from > 0)
428     {
429       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
430
431       while (n_left_from > 0 && n_left_to_next > 0)
432         {
433           u32 pi0;
434           vlib_buffer_t *p0;
435           ip6_mapt_tcp_udp_next_t next0;
436
437           pi0 = to_next[0] = from[0];
438           from += 1;
439           n_left_from -= 1;
440           to_next += 1;
441           n_left_to_next -= 1;
442           next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
443
444           p0 = vlib_get_buffer (vm, pi0);
445
446           if (map_ip6_to_ip4_tcp_udp (vm, p0, true))
447             {
448               p0->error = error_node->errors[MAP_ERROR_UNKNOWN];
449               next0 = IP6_MAPT_TCP_UDP_NEXT_DROP;
450             }
451           else
452             {
453               if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
454                 {
455                   // Send to fragmentation node if necessary
456                   vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
457                   vnet_buffer (p0)->ip_frag.next_index =
458                     IP_FRAG_NEXT_IP4_LOOKUP;
459                   next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG;
460                 }
461             }
462
463           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
464                                            to_next, n_left_to_next, pi0,
465                                            next0);
466         }
467       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
468     }
469   return frame->n_vectors;
470 }
471
472 static uword
473 ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
474 {
475   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
476   vlib_node_runtime_t *error_node =
477     vlib_node_get_runtime (vm, ip6_map_t_node.index);
478   vlib_combined_counter_main_t *cm = map_main.domain_counters;
479   u32 thread_index = vm->thread_index;
480
481   from = vlib_frame_vector_args (frame);
482   n_left_from = frame->n_vectors;
483   next_index = node->cached_next_index;
484   while (n_left_from > 0)
485     {
486       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
487
488       while (n_left_from > 0 && n_left_to_next > 0)
489         {
490           u32 pi0;
491           vlib_buffer_t *p0;
492           ip6_header_t *ip60;
493           u8 error0;
494           u32 l4_len0;
495           i32 map_port0;
496           map_domain_t *d0;
497           ip6_frag_hdr_t *frag0;
498           ip6_mapt_next_t next0 = 0;
499           u32 saddr;
500
501           pi0 = to_next[0] = from[0];
502           from += 1;
503           n_left_from -= 1;
504           to_next += 1;
505           n_left_to_next -= 1;
506           error0 = MAP_ERROR_NONE;
507           p0 = vlib_get_buffer (vm, pi0);
508           u16 l4_src_port = vnet_buffer (p0)->ip.reass.l4_src_port;
509
510           ip60 = vlib_buffer_get_current (p0);
511
512           d0 =
513             ip6_map_get_domain (&ip60->dst_address,
514                                 &vnet_buffer (p0)->map_t.map_domain_index,
515                                 &error0);
516           if (!d0)
517             {                   /* Guess it wasn't for us */
518               vnet_feature_next (&next0, p0);
519               goto exit;
520             }
521
522           saddr = map_get_ip4 (&ip60->src_address, d0->ip6_src_len);
523           vnet_buffer (p0)->map_t.v6.saddr = saddr;
524           vnet_buffer (p0)->map_t.v6.daddr =
525             ip6_map_t_embedded_address (d0, &ip60->dst_address);
526           vnet_buffer (p0)->map_t.mtu = d0->mtu ? d0->mtu : ~0;
527
528           if (PREDICT_FALSE
529               (ip6_parse (vm, p0, ip60, p0->current_length,
530                           &(vnet_buffer (p0)->map_t.v6.l4_protocol),
531                           &(vnet_buffer (p0)->map_t.v6.l4_offset),
532                           &(vnet_buffer (p0)->map_t.v6.frag_offset))))
533             {
534               error0 =
535                 error0 == MAP_ERROR_NONE ? MAP_ERROR_MALFORMED : error0;
536             }
537
538           map_port0 = -1;
539           l4_len0 =
540             (u32) clib_net_to_host_u16 (ip60->payload_length) +
541             sizeof (*ip60) - vnet_buffer (p0)->map_t.v6.l4_offset;
542           frag0 =
543             (ip6_frag_hdr_t *) u8_ptr_add (ip60,
544                                            vnet_buffer (p0)->map_t.v6.
545                                            frag_offset);
546
547           if (PREDICT_FALSE
548               (vnet_buffer (p0)->map_t.v6.frag_offset
549                && ip6_frag_hdr_offset (frag0)))
550             {
551               map_port0 = l4_src_port;
552               next0 = IP6_MAPT_NEXT_MAPT_FRAGMENTED;
553             }
554           else
555             if (PREDICT_TRUE
556                 (vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_TCP))
557             {
558               error0 =
559                 l4_len0 <
560                 sizeof (tcp_header_t) ? MAP_ERROR_MALFORMED : error0;
561               vnet_buffer (p0)->map_t.checksum_offset =
562                 vnet_buffer (p0)->map_t.v6.l4_offset + 16;
563               next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
564               map_port0 = l4_src_port;
565             }
566           else
567             if (PREDICT_TRUE
568                 (vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_UDP))
569             {
570               error0 =
571                 l4_len0 <
572                 sizeof (udp_header_t) ? MAP_ERROR_MALFORMED : error0;
573               vnet_buffer (p0)->map_t.checksum_offset =
574                 vnet_buffer (p0)->map_t.v6.l4_offset + 6;
575               next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
576               map_port0 = l4_src_port;
577             }
578           else if (vnet_buffer (p0)->map_t.v6.l4_protocol ==
579                    IP_PROTOCOL_ICMP6)
580             {
581               error0 =
582                 l4_len0 <
583                 sizeof (icmp46_header_t) ? MAP_ERROR_MALFORMED : error0;
584               next0 = IP6_MAPT_NEXT_MAPT_ICMP;
585               if (((icmp46_header_t *)
586                    u8_ptr_add (ip60,
587                                vnet_buffer (p0)->map_t.v6.l4_offset))->code ==
588                   ICMP6_echo_reply
589                   || ((icmp46_header_t *)
590                       u8_ptr_add (ip60,
591                                   vnet_buffer (p0)->map_t.v6.l4_offset))->
592                   code == ICMP6_echo_request)
593                 map_port0 = l4_src_port;
594             }
595           else
596             {
597               // TODO: In case of 1:1 mapping, it might be possible to
598               // do something with those packets.
599               error0 = MAP_ERROR_BAD_PROTOCOL;
600             }
601
602           if (PREDICT_FALSE (map_port0 != -1) &&
603               (ip60->src_address.as_u64[0] !=
604                map_get_pfx_net (d0, vnet_buffer (p0)->map_t.v6.saddr,
605                                 map_port0)
606                || ip60->src_address.as_u64[1] != map_get_sfx_net (d0,
607                                                                   vnet_buffer
608                                                                   (p0)->map_t.
609                                                                   v6.saddr,
610                                                                   map_port0)))
611             {
612               // Security check when map_port0 is not zero (non-first
613               // fragment, UDP or TCP)
614               error0 =
615                 error0 == MAP_ERROR_NONE ? MAP_ERROR_SEC_CHECK : error0;
616             }
617
618           if (PREDICT_TRUE
619               (error0 == MAP_ERROR_NONE && next0 != IP6_MAPT_NEXT_MAPT_ICMP))
620             {
621               vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
622                                                thread_index,
623                                                vnet_buffer (p0)->map_t.
624                                                map_domain_index, 1,
625                                                clib_net_to_host_u16 (ip60->
626                                                                      payload_length));
627             }
628
629           next0 = (error0 != MAP_ERROR_NONE) ? IP6_MAPT_NEXT_DROP : next0;
630           p0->error = error_node->errors[error0];
631           if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
632             {
633               map_add_trace (vm, node, p0,
634                              vnet_buffer (p0)->map_t.map_domain_index,
635                              map_port0);
636             }
637         exit:
638           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
639                                            to_next, n_left_to_next, pi0,
640                                            next0);
641         }
642       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
643     }
644   return frame->n_vectors;
645 }
646
647 static char *map_t_error_strings[] = {
648 #define _(sym, string) string,
649   foreach_map_error
650 #undef _
651 };
652
653 /* *INDENT-OFF* */
654 VLIB_REGISTER_NODE(ip6_map_t_fragmented_node) = {
655   .function = ip6_map_t_fragmented,
656   .name = "ip6-map-t-fragmented",
657   .vector_size = sizeof (u32),
658   .format_trace = format_map_trace,
659   .type = VLIB_NODE_TYPE_INTERNAL,
660
661   .n_errors = MAP_N_ERROR,
662   .error_strings = map_t_error_strings,
663
664   .n_next_nodes = IP6_MAPT_FRAGMENTED_N_NEXT,
665   .next_nodes =
666   {
667     [IP6_MAPT_FRAGMENTED_NEXT_IP4_LOOKUP] = "ip4-lookup",
668     [IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
669     [IP6_MAPT_FRAGMENTED_NEXT_DROP] = "error-drop",
670   },
671 };
672 /* *INDENT-ON* */
673
674 /* *INDENT-OFF* */
675 VLIB_REGISTER_NODE(ip6_map_t_icmp_node) = {
676   .function = ip6_map_t_icmp,
677   .name = "ip6-map-t-icmp",
678   .vector_size = sizeof (u32),
679   .format_trace = format_map_trace,
680   .type = VLIB_NODE_TYPE_INTERNAL,
681
682   .n_errors = MAP_N_ERROR,
683   .error_strings = map_t_error_strings,
684
685   .n_next_nodes = IP6_MAPT_ICMP_N_NEXT,
686   .next_nodes =
687   {
688     [IP6_MAPT_ICMP_NEXT_IP4_LOOKUP] = "ip4-lookup",
689     [IP6_MAPT_ICMP_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
690     [IP6_MAPT_ICMP_NEXT_DROP] = "error-drop",
691   },
692 };
693 /* *INDENT-ON* */
694
695 /* *INDENT-OFF* */
696 VLIB_REGISTER_NODE(ip6_map_t_tcp_udp_node) = {
697   .function = ip6_map_t_tcp_udp,
698   .name = "ip6-map-t-tcp-udp",
699   .vector_size = sizeof (u32),
700   .format_trace = format_map_trace,
701   .type = VLIB_NODE_TYPE_INTERNAL,
702
703   .n_errors = MAP_N_ERROR,
704   .error_strings = map_t_error_strings,
705
706   .n_next_nodes = IP6_MAPT_TCP_UDP_N_NEXT,
707   .next_nodes =
708   {
709     [IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP] = "ip4-lookup",
710     [IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
711     [IP6_MAPT_TCP_UDP_NEXT_DROP] = "error-drop",
712   },
713 };
714 /* *INDENT-ON* */
715
716 /* *INDENT-OFF* */
717 VNET_FEATURE_INIT (ip6_map_t_feature, static) = {
718     .arc_name = "ip6-unicast",
719     .node_name = "ip6-map-t",
720     .runs_before = VNET_FEATURES ("ip6-flow-classify"),
721     .runs_after = VNET_FEATURES ("ip6-sv-reassembly-feature"),
722 };
723
724 VLIB_REGISTER_NODE(ip6_map_t_node) = {
725   .function = ip6_map_t,
726   .name = "ip6-map-t",
727   .vector_size = sizeof(u32),
728   .format_trace = format_map_trace,
729   .type = VLIB_NODE_TYPE_INTERNAL,
730
731   .n_errors = MAP_N_ERROR,
732   .error_strings = map_t_error_strings,
733
734   .n_next_nodes = IP6_MAPT_N_NEXT,
735   .next_nodes =
736   {
737     [IP6_MAPT_NEXT_MAPT_TCP_UDP] = "ip6-map-t-tcp-udp",
738     [IP6_MAPT_NEXT_MAPT_ICMP] = "ip6-map-t-icmp",
739     [IP6_MAPT_NEXT_MAPT_FRAGMENTED] = "ip6-map-t-fragmented",
740     [IP6_MAPT_NEXT_DROP] = "error-drop",
741   },
742 };
743 /* *INDENT-ON* */
744
745 /*
746  * fd.io coding-style-patch-verification: ON
747  *
748  * Local Variables:
749  * eval: (c-set-style "gnu")
750  * End:
751  */