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