nat: use SVR
[vpp.git] / src / plugins / nat / nat64_out2in.c
1 /*
2  * Copyright (c) 2017 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 /**
16  * @file
17  * @brief NAT64 IPv4 to IPv6 translation (otside to inside network)
18  */
19
20 #include <nat/nat64.h>
21 #include <nat/nat_inlines.h>
22 #include <vnet/ip/ip4_to_ip6.h>
23 #include <vnet/fib/ip4_fib.h>
24 #include <vnet/udp/udp.h>
25
26 typedef struct
27 {
28   u32 sw_if_index;
29   u32 next_index;
30 } nat64_out2in_trace_t;
31
32 static u8 *
33 format_nat64_out2in_trace (u8 * s, va_list * args)
34 {
35   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
36   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
37   nat64_out2in_trace_t *t = va_arg (*args, nat64_out2in_trace_t *);
38
39   s =
40     format (s, "NAT64-out2in: sw_if_index %d, next index %d", t->sw_if_index,
41             t->next_index);
42
43   return s;
44 }
45
46 #define foreach_nat64_out2in_error                       \
47 _(UNSUPPORTED_PROTOCOL, "unsupported protocol")          \
48 _(OUT2IN_PACKETS, "good out2in packets processed")       \
49 _(NO_TRANSLATION, "no translation")                      \
50 _(UNKNOWN, "unknown")                                    \
51 _(DROP_FRAGMENT, "drop fragment")                        \
52 _(TCP_PACKETS, "TCP packets")                            \
53 _(UDP_PACKETS, "UDP packets")                            \
54 _(ICMP_PACKETS, "ICMP packets")                          \
55 _(OTHER_PACKETS, "other protocol packets")               \
56 _(FRAGMENTS, "fragments")                                \
57 _(CACHED_FRAGMENTS, "cached fragments")                  \
58 _(PROCESSED_FRAGMENTS, "processed fragments")
59
60
61 typedef enum
62 {
63 #define _(sym,str) NAT64_OUT2IN_ERROR_##sym,
64   foreach_nat64_out2in_error
65 #undef _
66     NAT64_OUT2IN_N_ERROR,
67 } nat64_out2in_error_t;
68
69 static char *nat64_out2in_error_strings[] = {
70 #define _(sym,string) string,
71   foreach_nat64_out2in_error
72 #undef _
73 };
74
75 typedef enum
76 {
77   NAT64_OUT2IN_NEXT_IP6_LOOKUP,
78   NAT64_OUT2IN_NEXT_IP4_LOOKUP,
79   NAT64_OUT2IN_NEXT_DROP,
80   NAT64_OUT2IN_N_NEXT,
81 } nat64_out2in_next_t;
82
83 typedef struct nat64_out2in_set_ctx_t_
84 {
85   vlib_buffer_t *b;
86   vlib_main_t *vm;
87   u32 thread_index;
88 } nat64_out2in_set_ctx_t;
89
90 static int
91 nat64_out2in_tcp_udp (vlib_main_t * vm, vlib_buffer_t * b,
92                       nat64_out2in_set_ctx_t * ctx)
93 {
94   ip4_header_t *ip4;
95   ip6_header_t *ip6;
96   ip_csum_t csum;
97   u16 *checksum = NULL;
98   ip6_frag_hdr_t *frag;
99   u32 frag_id;
100   ip4_address_t old_src, old_dst;
101
102   nat64_main_t *nm = &nat64_main;
103   nat64_db_bib_entry_t *bibe;
104   nat64_db_st_entry_t *ste;
105   ip46_address_t saddr;
106   ip46_address_t daddr;
107   ip6_address_t ip6_saddr;
108   u8 proto = vnet_buffer (b)->ip.reass.ip_proto;
109   u16 dport = vnet_buffer (b)->ip.reass.l4_dst_port;
110   u16 sport = vnet_buffer (b)->ip.reass.l4_src_port;
111   u32 sw_if_index, fib_index;
112   nat64_db_t *db = &nm->db[ctx->thread_index];
113
114   ip4 = vlib_buffer_get_current (b);
115
116   udp_header_t *udp = ip4_next_header (ip4);
117   tcp_header_t *tcp = ip4_next_header (ip4);
118   if (!vnet_buffer (b)->ip.reass.is_non_first_fragment)
119     {
120       if (ip4->protocol == IP_PROTOCOL_UDP)
121         {
122           checksum = &udp->checksum;
123           //UDP checksum is optional over IPv4 but mandatory for IPv6
124           //We do not check udp->length sanity but use our safe computed value instead
125           if (PREDICT_FALSE (!*checksum))
126             {
127               u16 udp_len =
128                 clib_host_to_net_u16 (ip4->length) - sizeof (*ip4);
129               csum = ip_incremental_checksum (0, udp, udp_len);
130               csum =
131                 ip_csum_with_carry (csum, clib_host_to_net_u16 (udp_len));
132               csum =
133                 ip_csum_with_carry (csum,
134                                     clib_host_to_net_u16 (IP_PROTOCOL_UDP));
135               csum =
136                 ip_csum_with_carry (csum, *((u64 *) (&ip4->src_address)));
137               *checksum = ~ip_csum_fold (csum);
138             }
139         }
140       else
141         {
142           checksum = &tcp->checksum;
143         }
144     }
145
146   old_src.as_u32 = ip4->src_address.as_u32;
147   old_dst.as_u32 = ip4->dst_address.as_u32;
148
149   // Deal with fragmented packets
150   u16 frag_offset = ip4_get_fragment_offset (ip4);
151   if (PREDICT_FALSE (ip4_get_fragment_more (ip4) || frag_offset))
152     {
153       ip6 =
154         (ip6_header_t *) u8_ptr_add (ip4,
155                                      sizeof (*ip4) - sizeof (*ip6) -
156                                      sizeof (*frag));
157       frag =
158         (ip6_frag_hdr_t *) u8_ptr_add (ip4, sizeof (*ip4) - sizeof (*frag));
159       frag_id = frag_id_4to6 (ip4->fragment_id);
160       vlib_buffer_advance (b, sizeof (*ip4) - sizeof (*ip6) - sizeof (*frag));
161     }
162   else
163     {
164       ip6 = (ip6_header_t *) (((u8 *) ip4) + sizeof (*ip4) - sizeof (*ip6));
165       vlib_buffer_advance (b, sizeof (*ip4) - sizeof (*ip6));
166       frag = NULL;
167     }
168
169   ip6->ip_version_traffic_class_and_flow_label =
170     clib_host_to_net_u32 ((6 << 28) + (ip4->tos << 20));
171   ip6->payload_length = u16_net_add (ip4->length, -sizeof (*ip4));
172   ip6->hop_limit = ip4->ttl;
173   ip6->protocol = ip4->protocol;
174
175   sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
176   fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
177
178   clib_memset (&saddr, 0, sizeof (saddr));
179   saddr.ip4.as_u32 = ip4->src_address.as_u32;
180   clib_memset (&daddr, 0, sizeof (daddr));
181   daddr.ip4.as_u32 = ip4->dst_address.as_u32;
182
183   ste =
184     nat64_db_st_entry_find (db, &daddr, &saddr, dport, sport, proto,
185                             fib_index, 0);
186   if (ste)
187     {
188       bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
189       if (!bibe)
190         return -1;
191     }
192   else
193     {
194       bibe = nat64_db_bib_entry_find (db, &daddr, dport, proto, fib_index, 0);
195
196       if (!bibe)
197         return -1;
198
199       nat64_compose_ip6 (&ip6_saddr, &old_src, bibe->fib_index);
200       ste =
201         nat64_db_st_entry_create (ctx->thread_index, db, bibe, &ip6_saddr,
202                                   &saddr.ip4, sport);
203
204       if (!ste)
205         return -1;
206
207       vlib_set_simple_counter (&nm->total_sessions, ctx->thread_index, 0,
208                                db->st.st_entries_num);
209     }
210
211   ip6->src_address.as_u64[0] = ste->in_r_addr.as_u64[0];
212   ip6->src_address.as_u64[1] = ste->in_r_addr.as_u64[1];
213
214   ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
215   ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
216
217   vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
218
219   nat64_session_reset_timeout (ste, ctx->vm);
220
221   if (PREDICT_FALSE (frag != NULL))
222     {
223       frag->next_hdr = ip6->protocol;
224       frag->identification = frag_id;
225       frag->rsv = 0;
226       frag->fragment_offset_and_more =
227         ip6_frag_hdr_offset_and_more (frag_offset, 1);
228       ip6->protocol = IP_PROTOCOL_IPV6_FRAGMENTATION;
229       ip6->payload_length = u16_net_add (ip6->payload_length, sizeof (*frag));
230     }
231
232   if (!vnet_buffer (b)->ip.reass.is_non_first_fragment)
233     {
234       udp->dst_port = bibe->in_port;
235
236       if (proto == IP_PROTOCOL_TCP)
237         {
238           nat64_tcp_session_set_state (ste, tcp, 0);
239         }
240
241       csum = ip_csum_sub_even (*checksum, dport);
242       csum = ip_csum_add_even (csum, udp->dst_port);
243       csum = ip_csum_sub_even (csum, old_src.as_u32);
244       csum = ip_csum_sub_even (csum, old_dst.as_u32);
245       csum = ip_csum_add_even (csum, ip6->src_address.as_u64[0]);
246       csum = ip_csum_add_even (csum, ip6->src_address.as_u64[1]);
247       csum = ip_csum_add_even (csum, ip6->dst_address.as_u64[0]);
248       csum = ip_csum_add_even (csum, ip6->dst_address.as_u64[1]);
249       *checksum = ip_csum_fold (csum);
250     }
251
252   return 0;
253 }
254
255 static int
256 nat64_out2in_icmp_set_cb (vlib_buffer_t * b, ip4_header_t * ip4,
257                           ip6_header_t * ip6, void *arg)
258 {
259   nat64_main_t *nm = &nat64_main;
260   nat64_out2in_set_ctx_t *ctx = arg;
261   nat64_db_bib_entry_t *bibe;
262   nat64_db_st_entry_t *ste;
263   ip46_address_t saddr, daddr;
264   ip6_address_t ip6_saddr;
265   u32 sw_if_index, fib_index;
266   icmp46_header_t *icmp = ip4_next_header (ip4);
267   nat64_db_t *db = &nm->db[ctx->thread_index];
268
269   sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
270   fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
271
272   clib_memset (&saddr, 0, sizeof (saddr));
273   saddr.ip4.as_u32 = ip4->src_address.as_u32;
274   clib_memset (&daddr, 0, sizeof (daddr));
275   daddr.ip4.as_u32 = ip4->dst_address.as_u32;
276
277   if (icmp->type == ICMP6_echo_request || icmp->type == ICMP6_echo_reply)
278     {
279       u16 out_id = ((u16 *) (icmp))[2];
280       ste =
281         nat64_db_st_entry_find (db, &daddr, &saddr, out_id, 0,
282                                 IP_PROTOCOL_ICMP, fib_index, 0);
283
284       if (ste)
285         {
286           bibe =
287             nat64_db_bib_entry_by_index (db, IP_PROTOCOL_ICMP,
288                                          ste->bibe_index);
289           if (!bibe)
290             return -1;
291         }
292       else
293         {
294           bibe =
295             nat64_db_bib_entry_find (db, &daddr, out_id,
296                                      IP_PROTOCOL_ICMP, fib_index, 0);
297           if (!bibe)
298             return -1;
299
300           nat64_compose_ip6 (&ip6_saddr, &ip4->src_address, bibe->fib_index);
301           ste =
302             nat64_db_st_entry_create (ctx->thread_index, db,
303                                       bibe, &ip6_saddr, &saddr.ip4, 0);
304
305           if (!ste)
306             return -1;
307
308           vlib_set_simple_counter (&nm->total_sessions, ctx->thread_index, 0,
309                                    db->st.st_entries_num);
310         }
311
312       nat64_session_reset_timeout (ste, ctx->vm);
313
314       ip6->src_address.as_u64[0] = ste->in_r_addr.as_u64[0];
315       ip6->src_address.as_u64[1] = ste->in_r_addr.as_u64[1];
316
317       ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
318       ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
319       ((u16 *) (icmp))[2] = bibe->in_port;
320
321       vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
322     }
323   else
324     {
325       ip6_header_t *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
326
327       nat64_compose_ip6 (&ip6->src_address, &ip4->src_address,
328                          vnet_buffer (ctx->b)->sw_if_index[VLIB_TX]);
329       ip6->dst_address.as_u64[0] = inner_ip6->src_address.as_u64[0];
330       ip6->dst_address.as_u64[1] = inner_ip6->src_address.as_u64[1];
331     }
332
333   return 0;
334 }
335
336 static int
337 nat64_out2in_inner_icmp_set_cb (vlib_buffer_t * b, ip4_header_t * ip4,
338                                 ip6_header_t * ip6, void *arg)
339 {
340   nat64_main_t *nm = &nat64_main;
341   nat64_out2in_set_ctx_t *ctx = arg;
342   nat64_db_bib_entry_t *bibe;
343   nat64_db_st_entry_t *ste;
344   ip46_address_t saddr, daddr;
345   u32 sw_if_index, fib_index;
346   u8 proto = ip4->protocol;
347   nat64_db_t *db = &nm->db[ctx->thread_index];
348
349   sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
350   fib_index =
351     fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
352
353   clib_memset (&saddr, 0, sizeof (saddr));
354   saddr.ip4.as_u32 = ip4->src_address.as_u32;
355   clib_memset (&daddr, 0, sizeof (daddr));
356   daddr.ip4.as_u32 = ip4->dst_address.as_u32;
357
358   if (proto == IP_PROTOCOL_ICMP6)
359     {
360       icmp46_header_t *icmp = ip4_next_header (ip4);
361       u16 out_id = ((u16 *) (icmp))[2];
362       proto = IP_PROTOCOL_ICMP;
363
364       if (!
365           (icmp->type == ICMP6_echo_request
366            || icmp->type == ICMP6_echo_reply))
367         return -1;
368
369       ste =
370         nat64_db_st_entry_find (db, &saddr, &daddr, out_id, 0, proto,
371                                 fib_index, 0);
372       if (!ste)
373         return -1;
374
375       bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
376       if (!bibe)
377         return -1;
378
379       ip6->dst_address.as_u64[0] = ste->in_r_addr.as_u64[0];
380       ip6->dst_address.as_u64[1] = ste->in_r_addr.as_u64[1];
381       ip6->src_address.as_u64[0] = bibe->in_addr.as_u64[0];
382       ip6->src_address.as_u64[1] = bibe->in_addr.as_u64[1];
383       ((u16 *) (icmp))[2] = bibe->in_port;
384
385       vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
386     }
387   else
388     {
389       udp_header_t *udp = ip4_next_header (ip4);
390       tcp_header_t *tcp = ip4_next_header (ip4);
391       u16 dport = udp->dst_port;
392       u16 sport = udp->src_port;
393       u16 *checksum;
394       ip_csum_t csum;
395
396       ste =
397         nat64_db_st_entry_find (db, &saddr, &daddr, sport, dport, proto,
398                                 fib_index, 0);
399       if (!ste)
400         return -1;
401
402       bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
403       if (!bibe)
404         return -1;
405
406       nat64_compose_ip6 (&ip6->dst_address, &daddr.ip4, bibe->fib_index);
407       ip6->src_address.as_u64[0] = bibe->in_addr.as_u64[0];
408       ip6->src_address.as_u64[1] = bibe->in_addr.as_u64[1];
409       udp->src_port = bibe->in_port;
410
411       if (proto == IP_PROTOCOL_UDP)
412         checksum = &udp->checksum;
413       else
414         checksum = &tcp->checksum;
415       if (*checksum)
416         {
417           csum = ip_csum_sub_even (*checksum, sport);
418           csum = ip_csum_add_even (csum, udp->src_port);
419           *checksum = ip_csum_fold (csum);
420         }
421
422       vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
423     }
424
425   return 0;
426 }
427
428 static int
429 nat64_out2in_unk_proto (vlib_main_t * vm, vlib_buffer_t * p,
430                         nat64_out2in_set_ctx_t * ctx)
431 {
432   ip4_header_t *ip4 = vlib_buffer_get_current (p);
433   ip6_header_t *ip6;
434   ip6_frag_hdr_t *frag;
435   u32 frag_id;
436
437   nat64_main_t *nm = &nat64_main;
438   nat64_db_bib_entry_t *bibe;
439   nat64_db_st_entry_t *ste;
440   ip46_address_t saddr, daddr;
441   ip6_address_t ip6_saddr;
442   u32 sw_if_index, fib_index;
443   u8 proto = ip4->protocol;
444   nat64_db_t *db = &nm->db[ctx->thread_index];
445
446   // Deal with fragmented packets
447   u16 frag_offset = ip4_get_fragment_offset (ip4);
448   if (PREDICT_FALSE (ip4_get_fragment_more (ip4) || frag_offset))
449     {
450       ip6 =
451         (ip6_header_t *) u8_ptr_add (ip4,
452                                      sizeof (*ip4) - sizeof (*ip6) -
453                                      sizeof (*frag));
454       frag =
455         (ip6_frag_hdr_t *) u8_ptr_add (ip4, sizeof (*ip4) - sizeof (*frag));
456       frag_id = frag_id_4to6 (ip4->fragment_id);
457       vlib_buffer_advance (p, sizeof (*ip4) - sizeof (*ip6) - sizeof (*frag));
458     }
459   else
460     {
461       ip6 = (ip6_header_t *) (((u8 *) ip4) + sizeof (*ip4) - sizeof (*ip6));
462       vlib_buffer_advance (p, sizeof (*ip4) - sizeof (*ip6));
463       frag = NULL;
464     }
465
466   ip6->ip_version_traffic_class_and_flow_label =
467     clib_host_to_net_u32 ((6 << 28) + (ip4->tos << 20));
468   ip6->payload_length = u16_net_add (ip4->length, -sizeof (*ip4));
469   ip6->hop_limit = ip4->ttl;
470   ip6->protocol = ip4->protocol;
471
472   if (PREDICT_FALSE (frag != NULL))
473     {
474       frag->next_hdr = ip6->protocol;
475       frag->identification = frag_id;
476       frag->rsv = 0;
477       frag->fragment_offset_and_more =
478         ip6_frag_hdr_offset_and_more (frag_offset, 1);
479       ip6->protocol = IP_PROTOCOL_IPV6_FRAGMENTATION;
480       ip6->payload_length = u16_net_add (ip6->payload_length, sizeof (*frag));
481     }
482
483   sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
484   fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
485
486   clib_memset (&saddr, 0, sizeof (saddr));
487   saddr.ip4.as_u32 = ip4->src_address.as_u32;
488   clib_memset (&daddr, 0, sizeof (daddr));
489   daddr.ip4.as_u32 = ip4->dst_address.as_u32;
490
491   ste =
492     nat64_db_st_entry_find (db, &daddr, &saddr, 0, 0, proto, fib_index, 0);
493   if (ste)
494     {
495       bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
496       if (!bibe)
497         return -1;
498     }
499   else
500     {
501       bibe = nat64_db_bib_entry_find (db, &daddr, 0, proto, fib_index, 0);
502
503       if (!bibe)
504         return -1;
505
506       nat64_compose_ip6 (&ip6_saddr, &ip4->src_address, bibe->fib_index);
507       ste = nat64_db_st_entry_create (ctx->thread_index, db,
508                                       bibe, &ip6_saddr, &saddr.ip4, 0);
509
510       if (!ste)
511         return -1;
512
513       vlib_set_simple_counter (&nm->total_sessions, ctx->thread_index, 0,
514                                db->st.st_entries_num);
515     }
516
517   nat64_session_reset_timeout (ste, ctx->vm);
518
519   ip6->src_address.as_u64[0] = ste->in_r_addr.as_u64[0];
520   ip6->src_address.as_u64[1] = ste->in_r_addr.as_u64[1];
521
522   ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
523   ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
524
525   vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
526
527   return 0;
528 }
529
530 VLIB_NODE_FN (nat64_out2in_node) (vlib_main_t * vm,
531                                   vlib_node_runtime_t * node,
532                                   vlib_frame_t * frame)
533 {
534   u32 n_left_from, *from, *to_next;
535   nat64_out2in_next_t next_index;
536   nat64_main_t *nm = &nat64_main;
537   u32 pkts_processed = 0;
538   u32 thread_index = vm->thread_index;
539   u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
540     0, fragments = 0;
541
542   from = vlib_frame_vector_args (frame);
543   n_left_from = frame->n_vectors;
544   next_index = node->cached_next_index;
545   while (n_left_from > 0)
546     {
547       u32 n_left_to_next;
548
549       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
550
551       while (n_left_from > 0 && n_left_to_next > 0)
552         {
553           u32 bi0;
554           vlib_buffer_t *b0;
555           u32 next0;
556           ip4_header_t *ip40;
557           u32 proto0;
558           nat64_out2in_set_ctx_t ctx0;
559           udp_header_t *udp0;
560
561           /* speculatively enqueue b0 to the current next frame */
562           bi0 = from[0];
563           to_next[0] = bi0;
564           from += 1;
565           to_next += 1;
566           n_left_from -= 1;
567           n_left_to_next -= 1;
568
569           b0 = vlib_get_buffer (vm, bi0);
570           ip40 = vlib_buffer_get_current (b0);
571
572           ctx0.b = b0;
573           ctx0.vm = vm;
574           ctx0.thread_index = thread_index;
575
576           next0 = NAT64_OUT2IN_NEXT_IP6_LOOKUP;
577
578           proto0 = ip_proto_to_snat_proto (ip40->protocol);
579
580           if (PREDICT_FALSE (proto0 == ~0))
581             {
582               if (nat64_out2in_unk_proto (vm, b0, &ctx0))
583                 {
584                   next0 = NAT64_OUT2IN_NEXT_DROP;
585                   b0->error = node->errors[NAT64_OUT2IN_ERROR_NO_TRANSLATION];
586                 }
587               other_packets++;
588               goto trace0;
589             }
590
591           if (proto0 == SNAT_PROTOCOL_ICMP)
592             {
593               icmp_packets++;
594               if (icmp_to_icmp6
595                   (b0, nat64_out2in_icmp_set_cb, &ctx0,
596                    nat64_out2in_inner_icmp_set_cb, &ctx0))
597                 {
598                   next0 = NAT64_OUT2IN_NEXT_DROP;
599                   b0->error = node->errors[NAT64_OUT2IN_ERROR_NO_TRANSLATION];
600                   goto trace0;
601                 }
602             }
603           else
604             {
605               if (proto0 == SNAT_PROTOCOL_TCP)
606                 tcp_packets++;
607               else
608                 udp_packets++;
609
610               if (nat64_out2in_tcp_udp (vm, b0, &ctx0))
611                 {
612                   udp0 = ip4_next_header (ip40);
613                   /*
614                    * Send DHCP packets to the ipv4 stack, or we won't
615                    * be able to use dhcp client on the outside interface
616                    */
617                   if ((proto0 == SNAT_PROTOCOL_UDP)
618                       && (udp0->dst_port ==
619                           clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client)))
620                     {
621                       next0 = NAT64_OUT2IN_NEXT_IP4_LOOKUP;
622                       goto trace0;
623                     }
624                   next0 = NAT64_OUT2IN_NEXT_DROP;
625                   b0->error = node->errors[NAT64_OUT2IN_ERROR_NO_TRANSLATION];
626                   goto trace0;
627                 }
628             }
629
630         trace0:
631           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
632                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
633             {
634               nat64_out2in_trace_t *t =
635                 vlib_add_trace (vm, node, b0, sizeof (*t));
636               t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
637               t->next_index = next0;
638             }
639
640           pkts_processed += next0 == NAT64_OUT2IN_NEXT_IP6_LOOKUP;
641
642           /* verify speculative enqueue, maybe switch current next frame */
643           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
644                                            n_left_to_next, bi0, next0);
645         }
646       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
647     }
648   vlib_node_increment_counter (vm, nm->out2in_node_index,
649                                NAT64_OUT2IN_ERROR_OUT2IN_PACKETS,
650                                pkts_processed);
651   vlib_node_increment_counter (vm, nm->out2in_node_index,
652                                NAT64_OUT2IN_ERROR_TCP_PACKETS, tcp_packets);
653   vlib_node_increment_counter (vm, nm->out2in_node_index,
654                                NAT64_OUT2IN_ERROR_UDP_PACKETS, udp_packets);
655   vlib_node_increment_counter (vm, nm->out2in_node_index,
656                                NAT64_OUT2IN_ERROR_ICMP_PACKETS, icmp_packets);
657   vlib_node_increment_counter (vm, nm->out2in_node_index,
658                                NAT64_OUT2IN_ERROR_OTHER_PACKETS,
659                                other_packets);
660   vlib_node_increment_counter (vm, nm->out2in_node_index,
661                                NAT64_OUT2IN_ERROR_FRAGMENTS, fragments);
662
663   return frame->n_vectors;
664 }
665
666 /* *INDENT-OFF* */
667 VLIB_REGISTER_NODE (nat64_out2in_node) = {
668   .name = "nat64-out2in",
669   .vector_size = sizeof (u32),
670   .format_trace = format_nat64_out2in_trace,
671   .type = VLIB_NODE_TYPE_INTERNAL,
672   .n_errors = ARRAY_LEN (nat64_out2in_error_strings),
673   .error_strings = nat64_out2in_error_strings,
674   .n_next_nodes = NAT64_OUT2IN_N_NEXT,
675   /* edit / add dispositions here */
676   .next_nodes = {
677     [NAT64_OUT2IN_NEXT_DROP] = "error-drop",
678     [NAT64_OUT2IN_NEXT_IP6_LOOKUP] = "ip6-lookup",
679     [NAT64_OUT2IN_NEXT_IP4_LOOKUP] = "ip4-lookup",
680   },
681 };
682 /* *INDENT-ON* */
683
684 typedef struct nat64_out2in_frag_set_ctx_t_
685 {
686   vlib_main_t *vm;
687   vlib_buffer_t *b;
688   u32 sess_index;
689   u32 thread_index;
690   u8 proto;
691   u8 first_frag;
692 } nat64_out2in_frag_set_ctx_t;
693
694 #define foreach_nat64_out2in_handoff_error                       \
695 _(CONGESTION_DROP, "congestion drop")                            \
696 _(SAME_WORKER, "same worker")                                    \
697 _(DO_HANDOFF, "do handoff")
698
699 typedef enum
700 {
701 #define _(sym,str) NAT64_OUT2IN_HANDOFF_ERROR_##sym,
702   foreach_nat64_out2in_handoff_error
703 #undef _
704     NAT64_OUT2IN_HANDOFF_N_ERROR,
705 } nat64_out2in_handoff_error_t;
706
707 static char *nat64_out2in_handoff_error_strings[] = {
708 #define _(sym,string) string,
709   foreach_nat64_out2in_handoff_error
710 #undef _
711 };
712
713 typedef struct
714 {
715   u32 next_worker_index;
716 } nat64_out2in_handoff_trace_t;
717
718 static u8 *
719 format_nat64_out2in_handoff_trace (u8 * s, va_list * args)
720 {
721   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
722   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
723   nat64_out2in_handoff_trace_t *t =
724     va_arg (*args, nat64_out2in_handoff_trace_t *);
725
726   s =
727     format (s, "NAT64-OUT2IN-HANDOFF: next-worker %d", t->next_worker_index);
728
729   return s;
730 }
731
732 VLIB_NODE_FN (nat64_out2in_handoff_node) (vlib_main_t * vm,
733                                           vlib_node_runtime_t * node,
734                                           vlib_frame_t * frame)
735 {
736   nat64_main_t *nm = &nat64_main;
737   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
738   u32 n_enq, n_left_from, *from;
739   u16 thread_indices[VLIB_FRAME_SIZE], *ti;
740   u32 fq_index;
741   u32 thread_index = vm->thread_index;
742   u32 do_handoff = 0, same_worker = 0;
743
744   from = vlib_frame_vector_args (frame);
745   n_left_from = frame->n_vectors;
746   vlib_get_buffers (vm, from, bufs, n_left_from);
747
748   b = bufs;
749   ti = thread_indices;
750
751   fq_index = nm->fq_out2in_index;
752
753   while (n_left_from > 0)
754     {
755       ip4_header_t *ip0;
756
757       ip0 = vlib_buffer_get_current (b[0]);
758       ti[0] = nat64_get_worker_out2in (b[0], ip0);
759
760       if (ti[0] != thread_index)
761         do_handoff++;
762       else
763         same_worker++;
764
765       if (PREDICT_FALSE
766           ((node->flags & VLIB_NODE_FLAG_TRACE)
767            && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
768         {
769           nat64_out2in_handoff_trace_t *t =
770             vlib_add_trace (vm, node, b[0], sizeof (*t));
771           t->next_worker_index = ti[0];
772         }
773
774       n_left_from -= 1;
775       ti += 1;
776       b += 1;
777     }
778
779   n_enq =
780     vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
781                                    frame->n_vectors, 1);
782
783   if (n_enq < frame->n_vectors)
784     vlib_node_increment_counter (vm, node->node_index,
785                                  NAT64_OUT2IN_HANDOFF_ERROR_CONGESTION_DROP,
786                                  frame->n_vectors - n_enq);
787   vlib_node_increment_counter (vm, node->node_index,
788                                NAT64_OUT2IN_HANDOFF_ERROR_SAME_WORKER,
789                                same_worker);
790   vlib_node_increment_counter (vm, node->node_index,
791                                NAT64_OUT2IN_HANDOFF_ERROR_DO_HANDOFF,
792                                do_handoff);
793
794   return frame->n_vectors;
795 }
796
797 /* *INDENT-OFF* */
798 VLIB_REGISTER_NODE (nat64_out2in_handoff_node) = {
799   .name = "nat64-out2in-handoff",
800   .vector_size = sizeof (u32),
801   .format_trace = format_nat64_out2in_handoff_trace,
802   .type = VLIB_NODE_TYPE_INTERNAL,
803   .n_errors = ARRAY_LEN(nat64_out2in_handoff_error_strings),
804   .error_strings = nat64_out2in_handoff_error_strings,
805
806   .n_next_nodes = 1,
807
808   .next_nodes = {
809     [0] = "error-drop",
810   },
811 };
812 /* *INDENT-ON* */
813
814 /*
815  * fd.io coding-style-patch-verification: ON
816  *
817  * Local Variables:
818  * eval: (c-set-style "gnu")
819  * End:
820  */