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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @brief NAT64 IPv6 to IPv4 translation (inside to outside network)
20 #include <nat/nat64.h>
21 #include <nat/nat_reass.h>
22 #include <nat/nat_inlines.h>
23 #include <vnet/ip/ip6_to_ip4.h>
24 #include <vnet/fib/fib_table.h>
31 } nat64_in2out_trace_t;
34 format_nat64_in2out_trace (u8 * s, va_list * args)
36 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
37 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
38 nat64_in2out_trace_t *t = va_arg (*args, nat64_in2out_trace_t *);
41 tag = t->is_slow_path ? "NAT64-in2out-slowpath" : "NAT64-in2out";
44 format (s, "%s: sw_if_index %d, next index %d", tag, t->sw_if_index,
55 } nat64_in2out_reass_trace_t;
58 format_nat64_in2out_reass_trace (u8 * s, va_list * args)
60 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
61 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
62 nat64_in2out_reass_trace_t *t =
63 va_arg (*args, nat64_in2out_reass_trace_t *);
66 format (s, "NAT64-in2out-reass: sw_if_index %d, next index %d, status %s",
67 t->sw_if_index, t->next_index,
68 t->cached ? "cached" : "translated");
74 #define foreach_nat64_in2out_error \
75 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
76 _(IN2OUT_PACKETS, "good in2out packets processed") \
77 _(NO_TRANSLATION, "no translation") \
78 _(UNKNOWN, "unknown") \
79 _(DROP_FRAGMENT, "drop fragment") \
80 _(MAX_REASS, "maximum reassemblies exceeded") \
81 _(MAX_FRAG, "maximum fragments per reassembly exceeded") \
82 _(TCP_PACKETS, "TCP packets") \
83 _(UDP_PACKETS, "UDP packets") \
84 _(ICMP_PACKETS, "ICMP packets") \
85 _(OTHER_PACKETS, "other protocol packets") \
86 _(FRAGMENTS, "fragments") \
87 _(CACHED_FRAGMENTS, "cached fragments") \
88 _(PROCESSED_FRAGMENTS, "processed fragments")
93 #define _(sym,str) NAT64_IN2OUT_ERROR_##sym,
94 foreach_nat64_in2out_error
97 } nat64_in2out_error_t;
99 static char *nat64_in2out_error_strings[] = {
100 #define _(sym,string) string,
101 foreach_nat64_in2out_error
107 NAT64_IN2OUT_NEXT_IP4_LOOKUP,
108 NAT64_IN2OUT_NEXT_IP6_LOOKUP,
109 NAT64_IN2OUT_NEXT_DROP,
110 NAT64_IN2OUT_NEXT_SLOWPATH,
111 NAT64_IN2OUT_NEXT_REASS,
113 } nat64_in2out_next_t;
115 typedef struct nat64_in2out_set_ctx_t_
120 } nat64_in2out_set_ctx_t;
123 nat64_not_translate (u32 sw_if_index, ip6_address_t ip6_addr)
126 ip6_main_t *im6 = &ip6_main;
127 ip_lookup_main_t *lm6 = &im6->lookup_main;
128 ip_interface_address_t *ia = 0;
131 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
133 addr = ip_interface_address_get_address (lm6, ia);
134 if (0 == ip6_address_compare (addr, &ip6_addr))
143 * @brief Check whether is a hairpinning.
145 * If the destination IP address of the packet is an IPv4 address assigned to
146 * the NAT64 itself, then the packet is a hairpin packet.
148 * param dst_addr Destination address of the packet.
150 * @returns 1 if hairpinning, otherwise 0.
152 static_always_inline int
153 is_hairpinning (ip6_address_t * dst_addr)
155 nat64_main_t *nm = &nat64_main;
158 for (i = 0; i < vec_len (nm->addr_pool); i++)
160 if (nm->addr_pool[i].addr.as_u32 == dst_addr->as_u32[3])
168 nat64_in2out_tcp_udp_set_cb (ip6_header_t * ip6, ip4_header_t * ip4,
171 nat64_main_t *nm = &nat64_main;
172 nat64_in2out_set_ctx_t *ctx = arg;
173 nat64_db_bib_entry_t *bibe;
174 nat64_db_st_entry_t *ste;
175 ip46_address_t saddr, daddr;
176 u32 sw_if_index, fib_index;
177 udp_header_t *udp = ip6_next_header (ip6);
178 u8 proto = ip6->protocol;
179 u16 sport = udp->src_port;
180 u16 dport = udp->dst_port;
181 nat64_db_t *db = &nm->db[ctx->thread_index];
183 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
185 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
187 saddr.as_u64[0] = ip6->src_address.as_u64[0];
188 saddr.as_u64[1] = ip6->src_address.as_u64[1];
189 daddr.as_u64[0] = ip6->dst_address.as_u64[0];
190 daddr.as_u64[1] = ip6->dst_address.as_u64[1];
193 nat64_db_st_entry_find (db, &saddr, &daddr, sport, dport, proto,
198 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
204 bibe = nat64_db_bib_entry_find (db, &saddr, sport, proto, fib_index, 1);
209 ip4_address_t out_addr;
210 if (nat64_alloc_out_addr_and_port
211 (fib_index, ip_proto_to_snat_proto (proto), &out_addr,
212 &out_port, ctx->thread_index))
216 nat64_db_bib_entry_create (ctx->thread_index, db,
217 &ip6->src_address, &out_addr, sport,
218 out_port, fib_index, proto, 0);
222 vlib_set_simple_counter (&nm->total_bibs, ctx->thread_index, 0,
223 db->bib.bib_entries_num);
226 nat64_extract_ip4 (&ip6->dst_address, &daddr.ip4, fib_index);
228 nat64_db_st_entry_create (ctx->thread_index, db, bibe,
229 &ip6->dst_address, &daddr.ip4, dport);
233 vlib_set_simple_counter (&nm->total_sessions, ctx->thread_index, 0,
234 db->st.st_entries_num);
237 ip4->src_address.as_u32 = bibe->out_addr.as_u32;
238 udp->src_port = bibe->out_port;
240 ip4->dst_address.as_u32 = ste->out_r_addr.as_u32;
242 if (proto == IP_PROTOCOL_TCP)
246 tcp_header_t *tcp = ip6_next_header (ip6);
248 nat64_tcp_session_set_state (ste, tcp, 1);
249 checksum = &tcp->checksum;
250 csum = ip_csum_sub_even (*checksum, sport);
251 csum = ip_csum_add_even (csum, udp->src_port);
252 mss_clamping (nm->sm, tcp, &csum);
253 *checksum = ip_csum_fold (csum);
256 nat64_session_reset_timeout (ste, ctx->vm);
262 nat64_in2out_icmp_set_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *arg)
264 nat64_main_t *nm = &nat64_main;
265 nat64_in2out_set_ctx_t *ctx = arg;
266 nat64_db_bib_entry_t *bibe;
267 nat64_db_st_entry_t *ste;
268 ip46_address_t saddr, daddr;
269 u32 sw_if_index, fib_index;
270 icmp46_header_t *icmp = ip6_next_header (ip6);
271 nat64_db_t *db = &nm->db[ctx->thread_index];
273 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
275 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
277 saddr.as_u64[0] = ip6->src_address.as_u64[0];
278 saddr.as_u64[1] = ip6->src_address.as_u64[1];
279 daddr.as_u64[0] = ip6->dst_address.as_u64[0];
280 daddr.as_u64[1] = ip6->dst_address.as_u64[1];
282 if (icmp->type == ICMP4_echo_request || icmp->type == ICMP4_echo_reply)
284 u16 in_id = ((u16 *) (icmp))[2];
286 nat64_db_st_entry_find (db, &saddr, &daddr, in_id, 0,
287 IP_PROTOCOL_ICMP, fib_index, 1);
292 nat64_db_bib_entry_by_index (db, IP_PROTOCOL_ICMP,
300 nat64_db_bib_entry_find (db, &saddr, in_id,
301 IP_PROTOCOL_ICMP, fib_index, 1);
306 ip4_address_t out_addr;
307 if (nat64_alloc_out_addr_and_port
308 (fib_index, SNAT_PROTOCOL_ICMP, &out_addr, &out_id,
313 nat64_db_bib_entry_create (ctx->thread_index, db,
314 &ip6->src_address, &out_addr,
315 in_id, out_id, fib_index,
316 IP_PROTOCOL_ICMP, 0);
320 vlib_set_simple_counter (&nm->total_bibs, ctx->thread_index, 0,
321 db->bib.bib_entries_num);
324 nat64_extract_ip4 (&ip6->dst_address, &daddr.ip4, fib_index);
326 nat64_db_st_entry_create (ctx->thread_index, db, bibe,
327 &ip6->dst_address, &daddr.ip4, 0);
331 vlib_set_simple_counter (&nm->total_sessions, ctx->thread_index, 0,
332 db->st.st_entries_num);
335 nat64_session_reset_timeout (ste, ctx->vm);
337 ip4->src_address.as_u32 = bibe->out_addr.as_u32;
338 ((u16 *) (icmp))[2] = bibe->out_port;
340 ip4->dst_address.as_u32 = ste->out_r_addr.as_u32;
344 if (!vec_len (nm->addr_pool))
347 ip4->src_address.as_u32 = nm->addr_pool[0].addr.as_u32;
348 nat64_extract_ip4 (&ip6->dst_address, &ip4->dst_address, fib_index);
355 nat64_in2out_inner_icmp_set_cb (ip6_header_t * ip6, ip4_header_t * ip4,
358 nat64_main_t *nm = &nat64_main;
359 nat64_in2out_set_ctx_t *ctx = arg;
360 nat64_db_st_entry_t *ste;
361 nat64_db_bib_entry_t *bibe;
362 ip46_address_t saddr, daddr;
363 u32 sw_if_index, fib_index;
364 u8 proto = ip6->protocol;
365 nat64_db_t *db = &nm->db[ctx->thread_index];
367 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
369 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
371 saddr.as_u64[0] = ip6->src_address.as_u64[0];
372 saddr.as_u64[1] = ip6->src_address.as_u64[1];
373 daddr.as_u64[0] = ip6->dst_address.as_u64[0];
374 daddr.as_u64[1] = ip6->dst_address.as_u64[1];
376 if (proto == IP_PROTOCOL_ICMP6)
378 icmp46_header_t *icmp = ip6_next_header (ip6);
379 u16 in_id = ((u16 *) (icmp))[2];
380 proto = IP_PROTOCOL_ICMP;
383 (icmp->type == ICMP4_echo_request
384 || icmp->type == ICMP4_echo_reply))
388 nat64_db_st_entry_find (db, &daddr, &saddr, in_id, 0, proto,
393 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
397 ip4->dst_address.as_u32 = bibe->out_addr.as_u32;
398 ((u16 *) (icmp))[2] = bibe->out_port;
399 ip4->src_address.as_u32 = ste->out_r_addr.as_u32;
403 udp_header_t *udp = ip6_next_header (ip6);
404 tcp_header_t *tcp = ip6_next_header (ip6);
408 u16 sport = udp->src_port;
409 u16 dport = udp->dst_port;
412 nat64_db_st_entry_find (db, &daddr, &saddr, dport, sport, proto,
417 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
421 ip4->dst_address.as_u32 = bibe->out_addr.as_u32;
422 udp->dst_port = bibe->out_port;
423 ip4->src_address.as_u32 = ste->out_r_addr.as_u32;
425 if (proto == IP_PROTOCOL_TCP)
426 checksum = &tcp->checksum;
428 checksum = &udp->checksum;
429 csum = ip_csum_sub_even (*checksum, dport);
430 csum = ip_csum_add_even (csum, udp->dst_port);
431 *checksum = ip_csum_fold (csum);
437 typedef struct unk_proto_st_walk_ctx_t_
439 ip6_address_t src_addr;
440 ip6_address_t dst_addr;
441 ip4_address_t out_addr;
445 } unk_proto_st_walk_ctx_t;
448 unk_proto_st_walk (nat64_db_st_entry_t * ste, void *arg)
450 nat64_main_t *nm = &nat64_main;
451 unk_proto_st_walk_ctx_t *ctx = arg;
452 nat64_db_bib_entry_t *bibe;
453 ip46_address_t saddr, daddr;
454 nat64_db_t *db = &nm->db[ctx->thread_index];
456 if (ip46_address_is_equal (&ste->in_r_addr, &ctx->dst_addr))
458 bibe = nat64_db_bib_entry_by_index (db, ste->proto, ste->bibe_index);
462 if (ip46_address_is_equal (&bibe->in_addr, &ctx->src_addr)
463 && bibe->fib_index == ctx->fib_index)
465 clib_memset (&saddr, 0, sizeof (saddr));
466 saddr.ip4.as_u32 = bibe->out_addr.as_u32;
467 clib_memset (&daddr, 0, sizeof (daddr));
468 nat64_extract_ip4 (&ctx->dst_addr, &daddr.ip4, ctx->fib_index);
470 if (nat64_db_st_entry_find
471 (db, &daddr, &saddr, 0, 0, ctx->proto, ctx->fib_index, 0))
474 ctx->out_addr.as_u32 = bibe->out_addr.as_u32;
483 nat64_in2out_unk_proto_set_cb (ip6_header_t * ip6, ip4_header_t * ip4,
486 nat64_main_t *nm = &nat64_main;
487 nat64_in2out_set_ctx_t *s_ctx = arg;
488 nat64_db_bib_entry_t *bibe;
489 nat64_db_st_entry_t *ste;
490 ip46_address_t saddr, daddr, addr;
491 u32 sw_if_index, fib_index;
492 u8 proto = ip6->protocol;
494 nat64_db_t *db = &nm->db[s_ctx->thread_index];
496 sw_if_index = vnet_buffer (s_ctx->b)->sw_if_index[VLIB_RX];
498 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
500 saddr.as_u64[0] = ip6->src_address.as_u64[0];
501 saddr.as_u64[1] = ip6->src_address.as_u64[1];
502 daddr.as_u64[0] = ip6->dst_address.as_u64[0];
503 daddr.as_u64[1] = ip6->dst_address.as_u64[1];
506 nat64_db_st_entry_find (db, &saddr, &daddr, 0, 0, proto, fib_index, 1);
510 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
516 bibe = nat64_db_bib_entry_find (db, &saddr, 0, proto, fib_index, 1);
520 /* Choose same out address as for TCP/UDP session to same dst */
521 unk_proto_st_walk_ctx_t ctx = {
522 .src_addr.as_u64[0] = ip6->src_address.as_u64[0],
523 .src_addr.as_u64[1] = ip6->src_address.as_u64[1],
524 .dst_addr.as_u64[0] = ip6->dst_address.as_u64[0],
525 .dst_addr.as_u64[1] = ip6->dst_address.as_u64[1],
526 .out_addr.as_u32 = 0,
527 .fib_index = fib_index,
529 .thread_index = s_ctx->thread_index,
532 nat64_db_st_walk (db, IP_PROTOCOL_TCP, unk_proto_st_walk, &ctx);
534 if (!ctx.out_addr.as_u32)
535 nat64_db_st_walk (db, IP_PROTOCOL_UDP, unk_proto_st_walk, &ctx);
537 /* Verify if out address is not already in use for protocol */
538 clib_memset (&addr, 0, sizeof (addr));
539 addr.ip4.as_u32 = ctx.out_addr.as_u32;
540 if (nat64_db_bib_entry_find (db, &addr, 0, proto, 0, 0))
541 ctx.out_addr.as_u32 = 0;
543 if (!ctx.out_addr.as_u32)
545 for (i = 0; i < vec_len (nm->addr_pool); i++)
547 addr.ip4.as_u32 = nm->addr_pool[i].addr.as_u32;
548 if (!nat64_db_bib_entry_find (db, &addr, 0, proto, 0, 0))
553 if (!ctx.out_addr.as_u32)
557 nat64_db_bib_entry_create (s_ctx->thread_index, db,
558 &ip6->src_address, &ctx.out_addr,
559 0, 0, fib_index, proto, 0);
563 vlib_set_simple_counter (&nm->total_bibs, s_ctx->thread_index, 0,
564 db->bib.bib_entries_num);
567 nat64_extract_ip4 (&ip6->dst_address, &daddr.ip4, fib_index);
569 nat64_db_st_entry_create (s_ctx->thread_index, db, bibe,
570 &ip6->dst_address, &daddr.ip4, 0);
574 vlib_set_simple_counter (&nm->total_sessions, s_ctx->thread_index, 0,
575 db->st.st_entries_num);
578 nat64_session_reset_timeout (ste, s_ctx->vm);
580 ip4->src_address.as_u32 = bibe->out_addr.as_u32;
581 ip4->dst_address.as_u32 = ste->out_r_addr.as_u32;
589 nat64_in2out_tcp_udp_hairpinning (vlib_main_t * vm, vlib_buffer_t * b,
590 ip6_header_t * ip6, u32 thread_index)
592 nat64_main_t *nm = &nat64_main;
593 nat64_db_bib_entry_t *bibe;
594 nat64_db_st_entry_t *ste;
595 ip46_address_t saddr, daddr;
596 u32 sw_if_index, fib_index;
597 udp_header_t *udp = ip6_next_header (ip6);
598 tcp_header_t *tcp = ip6_next_header (ip6);
599 u8 proto = ip6->protocol;
600 u16 sport = udp->src_port;
601 u16 dport = udp->dst_port;
604 nat64_db_t *db = &nm->db[thread_index];
606 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
608 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
610 saddr.as_u64[0] = ip6->src_address.as_u64[0];
611 saddr.as_u64[1] = ip6->src_address.as_u64[1];
612 daddr.as_u64[0] = ip6->dst_address.as_u64[0];
613 daddr.as_u64[1] = ip6->dst_address.as_u64[1];
615 if (proto == IP_PROTOCOL_UDP)
616 checksum = &udp->checksum;
618 checksum = &tcp->checksum;
620 csum = ip_csum_sub_even (*checksum, ip6->src_address.as_u64[0]);
621 csum = ip_csum_sub_even (csum, ip6->src_address.as_u64[1]);
622 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[0]);
623 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[1]);
624 csum = ip_csum_sub_even (csum, sport);
625 csum = ip_csum_sub_even (csum, dport);
628 nat64_db_st_entry_find (db, &saddr, &daddr, sport, dport, proto,
633 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
639 bibe = nat64_db_bib_entry_find (db, &saddr, sport, proto, fib_index, 1);
644 ip4_address_t out_addr;
645 if (nat64_alloc_out_addr_and_port
646 (fib_index, ip_proto_to_snat_proto (proto), &out_addr,
647 &out_port, thread_index))
651 nat64_db_bib_entry_create (thread_index, db, &ip6->src_address,
652 &out_addr, sport, out_port, fib_index,
657 vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
658 db->bib.bib_entries_num);
661 nat64_extract_ip4 (&ip6->dst_address, &daddr.ip4, fib_index);
663 nat64_db_st_entry_create (thread_index, db, bibe, &ip6->dst_address,
668 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
669 db->st.st_entries_num);
672 if (proto == IP_PROTOCOL_TCP)
673 nat64_tcp_session_set_state (ste, tcp, 1);
675 nat64_session_reset_timeout (ste, vm);
677 sport = udp->src_port = bibe->out_port;
678 nat64_compose_ip6 (&ip6->src_address, &bibe->out_addr, fib_index);
680 clib_memset (&daddr, 0, sizeof (daddr));
681 daddr.ip4.as_u32 = ste->out_r_addr.as_u32;
685 vec_foreach (db, nm->db)
687 bibe = nat64_db_bib_entry_find (db, &daddr, dport, proto, 0, 0);
697 ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
698 ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
699 udp->dst_port = bibe->in_port;
701 csum = ip_csum_add_even (csum, ip6->src_address.as_u64[0]);
702 csum = ip_csum_add_even (csum, ip6->src_address.as_u64[1]);
703 csum = ip_csum_add_even (csum, ip6->dst_address.as_u64[0]);
704 csum = ip_csum_add_even (csum, ip6->dst_address.as_u64[1]);
705 csum = ip_csum_add_even (csum, udp->src_port);
706 csum = ip_csum_add_even (csum, udp->dst_port);
707 *checksum = ip_csum_fold (csum);
713 nat64_in2out_icmp_hairpinning (vlib_main_t * vm, vlib_buffer_t * b,
714 ip6_header_t * ip6, u32 thread_index)
716 nat64_main_t *nm = &nat64_main;
717 nat64_db_bib_entry_t *bibe;
718 nat64_db_st_entry_t *ste;
719 icmp46_header_t *icmp = ip6_next_header (ip6);
720 ip6_header_t *inner_ip6;
721 ip46_address_t saddr, daddr;
722 u32 sw_if_index, fib_index;
726 u16 *checksum, sport, dport;
728 nat64_db_t *db = &nm->db[thread_index];
730 if (icmp->type == ICMP6_echo_request || icmp->type == ICMP6_echo_reply)
733 inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
735 proto = inner_ip6->protocol;
737 if (proto == IP_PROTOCOL_ICMP6)
740 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
742 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
744 saddr.as_u64[0] = inner_ip6->src_address.as_u64[0];
745 saddr.as_u64[1] = inner_ip6->src_address.as_u64[1];
746 daddr.as_u64[0] = inner_ip6->dst_address.as_u64[0];
747 daddr.as_u64[1] = inner_ip6->dst_address.as_u64[1];
749 udp = ip6_next_header (inner_ip6);
750 tcp = ip6_next_header (inner_ip6);
752 sport = udp->src_port;
753 dport = udp->dst_port;
755 if (proto == IP_PROTOCOL_UDP)
756 checksum = &udp->checksum;
758 checksum = &tcp->checksum;
760 csum = ip_csum_sub_even (*checksum, inner_ip6->src_address.as_u64[0]);
761 csum = ip_csum_sub_even (csum, inner_ip6->src_address.as_u64[1]);
762 csum = ip_csum_sub_even (csum, inner_ip6->dst_address.as_u64[0]);
763 csum = ip_csum_sub_even (csum, inner_ip6->dst_address.as_u64[1]);
764 csum = ip_csum_sub_even (csum, sport);
765 csum = ip_csum_sub_even (csum, dport);
768 nat64_db_st_entry_find (db, &daddr, &saddr, dport, sport, proto,
773 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
777 dport = udp->dst_port = bibe->out_port;
778 nat64_compose_ip6 (&inner_ip6->dst_address, &bibe->out_addr, fib_index);
780 clib_memset (&saddr, 0, sizeof (saddr));
781 clib_memset (&daddr, 0, sizeof (daddr));
782 saddr.ip4.as_u32 = ste->out_r_addr.as_u32;
783 daddr.ip4.as_u32 = bibe->out_addr.as_u32;
787 vec_foreach (db, nm->db)
789 ste = nat64_db_st_entry_find (db, &saddr, &daddr, sport, dport, proto,
800 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
804 inner_ip6->src_address.as_u64[0] = bibe->in_addr.as_u64[0];
805 inner_ip6->src_address.as_u64[1] = bibe->in_addr.as_u64[1];
806 udp->src_port = bibe->in_port;
808 csum = ip_csum_add_even (csum, inner_ip6->src_address.as_u64[0]);
809 csum = ip_csum_add_even (csum, inner_ip6->src_address.as_u64[1]);
810 csum = ip_csum_add_even (csum, inner_ip6->dst_address.as_u64[0]);
811 csum = ip_csum_add_even (csum, inner_ip6->dst_address.as_u64[1]);
812 csum = ip_csum_add_even (csum, udp->src_port);
813 csum = ip_csum_add_even (csum, udp->dst_port);
814 *checksum = ip_csum_fold (csum);
816 if (!vec_len (nm->addr_pool))
819 nat64_compose_ip6 (&ip6->src_address, &nm->addr_pool[0].addr, fib_index);
820 ip6->dst_address.as_u64[0] = inner_ip6->src_address.as_u64[0];
821 ip6->dst_address.as_u64[1] = inner_ip6->src_address.as_u64[1];
824 csum = ip_csum_with_carry (0, ip6->payload_length);
825 csum = ip_csum_with_carry (csum, clib_host_to_net_u16 (ip6->protocol));
826 csum = ip_csum_with_carry (csum, ip6->src_address.as_u64[0]);
827 csum = ip_csum_with_carry (csum, ip6->src_address.as_u64[1]);
828 csum = ip_csum_with_carry (csum, ip6->dst_address.as_u64[0]);
829 csum = ip_csum_with_carry (csum, ip6->dst_address.as_u64[1]);
831 ip_incremental_checksum (csum, icmp,
832 clib_net_to_host_u16 (ip6->payload_length));
833 icmp->checksum = ~ip_csum_fold (csum);
839 nat64_in2out_unk_proto_hairpinning (vlib_main_t * vm, vlib_buffer_t * b,
840 ip6_header_t * ip6, u32 thread_index)
842 nat64_main_t *nm = &nat64_main;
843 nat64_db_bib_entry_t *bibe;
844 nat64_db_st_entry_t *ste;
845 ip46_address_t saddr, daddr, addr;
846 u32 sw_if_index, fib_index;
847 u8 proto = ip6->protocol;
849 nat64_db_t *db = &nm->db[thread_index];
851 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
853 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
855 saddr.as_u64[0] = ip6->src_address.as_u64[0];
856 saddr.as_u64[1] = ip6->src_address.as_u64[1];
857 daddr.as_u64[0] = ip6->dst_address.as_u64[0];
858 daddr.as_u64[1] = ip6->dst_address.as_u64[1];
861 nat64_db_st_entry_find (db, &saddr, &daddr, 0, 0, proto, fib_index, 1);
865 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
871 bibe = nat64_db_bib_entry_find (db, &saddr, 0, proto, fib_index, 1);
875 /* Choose same out address as for TCP/UDP session to same dst */
876 unk_proto_st_walk_ctx_t ctx = {
877 .src_addr.as_u64[0] = ip6->src_address.as_u64[0],
878 .src_addr.as_u64[1] = ip6->src_address.as_u64[1],
879 .dst_addr.as_u64[0] = ip6->dst_address.as_u64[0],
880 .dst_addr.as_u64[1] = ip6->dst_address.as_u64[1],
881 .out_addr.as_u32 = 0,
882 .fib_index = fib_index,
884 .thread_index = thread_index,
887 nat64_db_st_walk (db, IP_PROTOCOL_TCP, unk_proto_st_walk, &ctx);
889 if (!ctx.out_addr.as_u32)
890 nat64_db_st_walk (db, IP_PROTOCOL_UDP, unk_proto_st_walk, &ctx);
892 /* Verify if out address is not already in use for protocol */
893 clib_memset (&addr, 0, sizeof (addr));
894 addr.ip4.as_u32 = ctx.out_addr.as_u32;
895 if (nat64_db_bib_entry_find (db, &addr, 0, proto, 0, 0))
896 ctx.out_addr.as_u32 = 0;
898 if (!ctx.out_addr.as_u32)
900 for (i = 0; i < vec_len (nm->addr_pool); i++)
902 addr.ip4.as_u32 = nm->addr_pool[i].addr.as_u32;
903 if (!nat64_db_bib_entry_find (db, &addr, 0, proto, 0, 0))
908 if (!ctx.out_addr.as_u32)
912 nat64_db_bib_entry_create (thread_index, db, &ip6->src_address,
913 &ctx.out_addr, 0, 0, fib_index, proto,
918 vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
919 db->bib.bib_entries_num);
922 nat64_extract_ip4 (&ip6->dst_address, &daddr.ip4, fib_index);
924 nat64_db_st_entry_create (thread_index, db, bibe, &ip6->dst_address,
929 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
930 db->st.st_entries_num);
933 nat64_session_reset_timeout (ste, vm);
935 nat64_compose_ip6 (&ip6->src_address, &bibe->out_addr, fib_index);
937 clib_memset (&daddr, 0, sizeof (daddr));
938 daddr.ip4.as_u32 = ste->out_r_addr.as_u32;
942 vec_foreach (db, nm->db)
944 bibe = nat64_db_bib_entry_find (db, &daddr, 0, proto, 0, 0);
954 ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
955 ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
961 nat64_in2out_node_fn_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
962 vlib_frame_t * frame, u8 is_slow_path)
964 u32 n_left_from, *from, *to_next;
965 nat64_in2out_next_t next_index;
966 u32 pkts_processed = 0;
967 u32 stats_node_index;
968 u32 thread_index = vm->thread_index;
969 nat64_main_t *nm = &nat64_main;
971 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
975 is_slow_path ? nm->in2out_slowpath_node_index : nm->in2out_node_index;
977 from = vlib_frame_vector_args (frame);
978 n_left_from = frame->n_vectors;
979 next_index = node->cached_next_index;
981 while (n_left_from > 0)
985 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
987 while (n_left_from > 0 && n_left_to_next > 0)
993 u16 l4_offset0, frag_offset0;
996 nat64_in2out_set_ctx_t ctx0;
999 /* speculatively enqueue b0 to the current next frame */
1005 n_left_to_next -= 1;
1007 b0 = vlib_get_buffer (vm, bi0);
1008 ip60 = vlib_buffer_get_current (b0);
1012 ctx0.thread_index = thread_index;
1014 next0 = NAT64_IN2OUT_NEXT_IP4_LOOKUP;
1018 (ip60, b0->current_length, &l4_protocol0, &l4_offset0,
1021 next0 = NAT64_IN2OUT_NEXT_DROP;
1022 b0->error = node->errors[NAT64_IN2OUT_ERROR_UNKNOWN];
1026 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1028 if (nat64_not_translate (sw_if_index0, ip60->dst_address))
1030 next0 = NAT64_IN2OUT_NEXT_IP6_LOOKUP;
1034 proto0 = ip_proto_to_snat_proto (l4_protocol0);
1038 if (PREDICT_TRUE (proto0 == ~0))
1041 if (is_hairpinning (&ip60->dst_address))
1043 next0 = NAT64_IN2OUT_NEXT_IP6_LOOKUP;
1044 if (nat64_in2out_unk_proto_hairpinning
1045 (vm, b0, ip60, thread_index))
1047 next0 = NAT64_IN2OUT_NEXT_DROP;
1049 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1054 if (ip6_to_ip4 (b0, nat64_in2out_unk_proto_set_cb, &ctx0))
1056 next0 = NAT64_IN2OUT_NEXT_DROP;
1058 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1066 if (PREDICT_FALSE (proto0 == ~0))
1068 next0 = NAT64_IN2OUT_NEXT_SLOWPATH;
1074 (ip60->protocol == IP_PROTOCOL_IPV6_FRAGMENTATION))
1076 next0 = NAT64_IN2OUT_NEXT_REASS;
1081 if (proto0 == SNAT_PROTOCOL_ICMP)
1084 if (is_hairpinning (&ip60->dst_address))
1086 next0 = NAT64_IN2OUT_NEXT_IP6_LOOKUP;
1087 if (nat64_in2out_icmp_hairpinning
1088 (vm, b0, ip60, thread_index))
1090 next0 = NAT64_IN2OUT_NEXT_DROP;
1092 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1098 (b0, nat64_in2out_icmp_set_cb, &ctx0,
1099 nat64_in2out_inner_icmp_set_cb, &ctx0))
1101 next0 = NAT64_IN2OUT_NEXT_DROP;
1102 b0->error = node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1106 else if (proto0 == SNAT_PROTOCOL_TCP || proto0 == SNAT_PROTOCOL_UDP)
1108 if (proto0 == SNAT_PROTOCOL_TCP)
1113 if (is_hairpinning (&ip60->dst_address))
1115 next0 = NAT64_IN2OUT_NEXT_IP6_LOOKUP;
1116 if (nat64_in2out_tcp_udp_hairpinning
1117 (vm, b0, ip60, thread_index))
1119 next0 = NAT64_IN2OUT_NEXT_DROP;
1121 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1126 if (ip6_to_ip4_tcp_udp
1127 (b0, nat64_in2out_tcp_udp_set_cb, &ctx0, 0))
1129 next0 = NAT64_IN2OUT_NEXT_DROP;
1130 b0->error = node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1136 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1137 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1139 nat64_in2out_trace_t *t =
1140 vlib_add_trace (vm, node, b0, sizeof (*t));
1141 t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1142 t->next_index = next0;
1143 t->is_slow_path = is_slow_path;
1146 pkts_processed += next0 == NAT64_IN2OUT_NEXT_IP4_LOOKUP;
1148 /* verify speculative enqueue, maybe switch current next frame */
1149 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1150 n_left_to_next, bi0, next0);
1152 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1154 vlib_node_increment_counter (vm, stats_node_index,
1155 NAT64_IN2OUT_ERROR_IN2OUT_PACKETS,
1157 vlib_node_increment_counter (vm, stats_node_index,
1158 NAT64_IN2OUT_ERROR_TCP_PACKETS, tcp_packets);
1159 vlib_node_increment_counter (vm, stats_node_index,
1160 NAT64_IN2OUT_ERROR_UDP_PACKETS, tcp_packets);
1161 vlib_node_increment_counter (vm, stats_node_index,
1162 NAT64_IN2OUT_ERROR_ICMP_PACKETS, icmp_packets);
1163 vlib_node_increment_counter (vm, stats_node_index,
1164 NAT64_IN2OUT_ERROR_OTHER_PACKETS,
1166 vlib_node_increment_counter (vm, stats_node_index,
1167 NAT64_IN2OUT_ERROR_FRAGMENTS, fragments);
1169 return frame->n_vectors;
1172 VLIB_NODE_FN (nat64_in2out_node) (vlib_main_t * vm,
1173 vlib_node_runtime_t * node,
1174 vlib_frame_t * frame)
1176 return nat64_in2out_node_fn_inline (vm, node, frame, 0);
1180 VLIB_REGISTER_NODE (nat64_in2out_node) = {
1181 .name = "nat64-in2out",
1182 .vector_size = sizeof (u32),
1183 .format_trace = format_nat64_in2out_trace,
1184 .type = VLIB_NODE_TYPE_INTERNAL,
1185 .n_errors = ARRAY_LEN (nat64_in2out_error_strings),
1186 .error_strings = nat64_in2out_error_strings,
1187 .n_next_nodes = NAT64_IN2OUT_N_NEXT,
1188 /* edit / add dispositions here */
1190 [NAT64_IN2OUT_NEXT_DROP] = "error-drop",
1191 [NAT64_IN2OUT_NEXT_IP4_LOOKUP] = "ip4-lookup",
1192 [NAT64_IN2OUT_NEXT_IP6_LOOKUP] = "ip6-lookup",
1193 [NAT64_IN2OUT_NEXT_SLOWPATH] = "nat64-in2out-slowpath",
1194 [NAT64_IN2OUT_NEXT_REASS] = "nat64-in2out-reass",
1199 VLIB_NODE_FN (nat64_in2out_slowpath_node) (vlib_main_t * vm,
1200 vlib_node_runtime_t * node,
1201 vlib_frame_t * frame)
1203 return nat64_in2out_node_fn_inline (vm, node, frame, 1);
1207 VLIB_REGISTER_NODE (nat64_in2out_slowpath_node) = {
1208 .name = "nat64-in2out-slowpath",
1209 .vector_size = sizeof (u32),
1210 .format_trace = format_nat64_in2out_trace,
1211 .type = VLIB_NODE_TYPE_INTERNAL,
1212 .n_errors = ARRAY_LEN (nat64_in2out_error_strings),
1213 .error_strings = nat64_in2out_error_strings,
1214 .n_next_nodes = NAT64_IN2OUT_N_NEXT,
1215 /* edit / add dispositions here */
1217 [NAT64_IN2OUT_NEXT_DROP] = "error-drop",
1218 [NAT64_IN2OUT_NEXT_IP4_LOOKUP] = "ip4-lookup",
1219 [NAT64_IN2OUT_NEXT_IP6_LOOKUP] = "ip6-lookup",
1220 [NAT64_IN2OUT_NEXT_SLOWPATH] = "nat64-in2out-slowpath",
1221 [NAT64_IN2OUT_NEXT_REASS] = "nat64-in2out-reass",
1226 typedef struct nat64_in2out_frag_set_ctx_t_
1234 } nat64_in2out_frag_set_ctx_t;
1237 nat64_in2out_frag_set_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *arg)
1239 nat64_main_t *nm = &nat64_main;
1240 nat64_in2out_frag_set_ctx_t *ctx = arg;
1241 nat64_db_st_entry_t *ste;
1242 nat64_db_bib_entry_t *bibe;
1244 nat64_db_t *db = &nm->db[ctx->thread_index];
1246 ste = nat64_db_st_entry_by_index (db, ctx->proto, ctx->sess_index);
1250 bibe = nat64_db_bib_entry_by_index (db, ctx->proto, ste->bibe_index);
1254 nat64_session_reset_timeout (ste, ctx->vm);
1256 if (ctx->first_frag)
1258 udp = (udp_header_t *) u8_ptr_add (ip6, ctx->l4_offset);
1260 if (ctx->proto == IP_PROTOCOL_TCP)
1264 tcp_header_t *tcp = (tcp_header_t *) udp;
1266 nat64_tcp_session_set_state (ste, tcp, 1);
1267 checksum = &tcp->checksum;
1268 csum = ip_csum_sub_even (*checksum, tcp->src_port);
1269 csum = ip_csum_sub_even (csum, ip6->src_address.as_u64[0]);
1270 csum = ip_csum_sub_even (csum, ip6->src_address.as_u64[1]);
1271 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[0]);
1272 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[1]);
1273 csum = ip_csum_add_even (csum, bibe->out_port);
1274 csum = ip_csum_add_even (csum, bibe->out_addr.as_u32);
1275 csum = ip_csum_add_even (csum, ste->out_r_addr.as_u32);
1276 *checksum = ip_csum_fold (csum);
1279 udp->src_port = bibe->out_port;
1282 ip4->src_address.as_u32 = bibe->out_addr.as_u32;
1283 ip4->dst_address.as_u32 = ste->out_r_addr.as_u32;
1289 nat64_in2out_frag_hairpinning (vlib_buffer_t * b, ip6_header_t * ip6,
1290 nat64_in2out_frag_set_ctx_t * ctx)
1292 nat64_main_t *nm = &nat64_main;
1293 nat64_db_st_entry_t *ste;
1294 nat64_db_bib_entry_t *bibe;
1295 udp_header_t *udp = (udp_header_t *) u8_ptr_add (ip6, ctx->l4_offset);
1296 tcp_header_t *tcp = (tcp_header_t *) udp;
1297 u16 sport = udp->src_port;
1298 u16 dport = udp->dst_port;
1301 ip46_address_t daddr;
1302 nat64_db_t *db = &nm->db[ctx->thread_index];
1304 if (ctx->first_frag)
1306 if (ctx->proto == IP_PROTOCOL_UDP)
1307 checksum = &udp->checksum;
1309 checksum = &tcp->checksum;
1311 csum = ip_csum_sub_even (*checksum, ip6->src_address.as_u64[0]);
1312 csum = ip_csum_sub_even (csum, ip6->src_address.as_u64[1]);
1313 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[0]);
1314 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[1]);
1315 csum = ip_csum_sub_even (csum, sport);
1316 csum = ip_csum_sub_even (csum, dport);
1319 ste = nat64_db_st_entry_by_index (db, ctx->proto, ctx->sess_index);
1323 bibe = nat64_db_bib_entry_by_index (db, ctx->proto, ste->bibe_index);
1327 if (ctx->proto == IP_PROTOCOL_TCP)
1328 nat64_tcp_session_set_state (ste, tcp, 1);
1330 nat64_session_reset_timeout (ste, ctx->vm);
1332 sport = bibe->out_port;
1333 dport = ste->r_port;
1335 nat64_compose_ip6 (&ip6->src_address, &bibe->out_addr, bibe->fib_index);
1337 clib_memset (&daddr, 0, sizeof (daddr));
1338 daddr.ip4.as_u32 = ste->out_r_addr.as_u32;
1342 vec_foreach (db, nm->db)
1344 bibe = nat64_db_bib_entry_find (db, &daddr, dport, ctx->proto, 0, 0);
1354 ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
1355 ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
1357 if (ctx->first_frag)
1359 udp->dst_port = bibe->in_port;
1360 udp->src_port = sport;
1361 csum = ip_csum_add_even (csum, ip6->src_address.as_u64[0]);
1362 csum = ip_csum_add_even (csum, ip6->src_address.as_u64[1]);
1363 csum = ip_csum_add_even (csum, ip6->dst_address.as_u64[0]);
1364 csum = ip_csum_add_even (csum, ip6->dst_address.as_u64[1]);
1365 csum = ip_csum_add_even (csum, udp->src_port);
1366 csum = ip_csum_add_even (csum, udp->dst_port);
1367 *checksum = ip_csum_fold (csum);
1373 VLIB_NODE_FN (nat64_in2out_reass_node) (vlib_main_t * vm,
1374 vlib_node_runtime_t * node,
1375 vlib_frame_t * frame)
1377 u32 n_left_from, *from, *to_next;
1378 nat64_in2out_next_t next_index;
1379 u32 pkts_processed = 0, cached_fragments = 0;
1380 u32 *fragments_to_drop = 0;
1381 u32 *fragments_to_loopback = 0;
1382 nat64_main_t *nm = &nat64_main;
1383 u32 thread_index = vm->thread_index;
1385 from = vlib_frame_vector_args (frame);
1386 n_left_from = frame->n_vectors;
1387 next_index = node->cached_next_index;
1389 while (n_left_from > 0)
1393 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1395 while (n_left_from > 0 && n_left_to_next > 0)
1402 u16 l4_offset0, frag_offset0;
1404 nat_reass_ip6_t *reass0;
1405 ip6_frag_hdr_t *frag0;
1406 nat64_db_bib_entry_t *bibe0;
1407 nat64_db_st_entry_t *ste0;
1409 snat_protocol_t proto0;
1410 u32 sw_if_index0, fib_index0;
1411 ip46_address_t saddr0, daddr0;
1412 nat64_in2out_frag_set_ctx_t ctx0;
1413 nat64_db_t *db = &nm->db[thread_index];
1415 /* speculatively enqueue b0 to the current next frame */
1421 n_left_to_next -= 1;
1423 b0 = vlib_get_buffer (vm, bi0);
1424 next0 = NAT64_IN2OUT_NEXT_IP4_LOOKUP;
1426 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1428 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6,
1431 ctx0.thread_index = thread_index;
1433 if (PREDICT_FALSE (nat_reass_is_drop_frag (1)))
1435 next0 = NAT64_IN2OUT_NEXT_DROP;
1436 b0->error = node->errors[NAT64_IN2OUT_ERROR_DROP_FRAGMENT];
1440 ip60 = (ip6_header_t *) vlib_buffer_get_current (b0);
1444 (ip60, b0->current_length, &l4_protocol0, &l4_offset0,
1447 next0 = NAT64_IN2OUT_NEXT_DROP;
1448 b0->error = node->errors[NAT64_IN2OUT_ERROR_UNKNOWN];
1453 (!(l4_protocol0 == IP_PROTOCOL_TCP
1454 || l4_protocol0 == IP_PROTOCOL_UDP)))
1456 next0 = NAT64_IN2OUT_NEXT_DROP;
1457 b0->error = node->errors[NAT64_IN2OUT_ERROR_DROP_FRAGMENT];
1461 udp0 = (udp_header_t *) u8_ptr_add (ip60, l4_offset0);
1462 frag0 = (ip6_frag_hdr_t *) u8_ptr_add (ip60, frag_offset0);
1463 proto0 = ip_proto_to_snat_proto (l4_protocol0);
1465 reass0 = nat_ip6_reass_find_or_create (ip60->src_address,
1467 frag0->identification,
1469 1, &fragments_to_drop);
1471 if (PREDICT_FALSE (!reass0))
1473 next0 = NAT64_IN2OUT_NEXT_DROP;
1474 b0->error = node->errors[NAT64_IN2OUT_ERROR_MAX_REASS];
1478 if (PREDICT_TRUE (ip6_frag_hdr_offset (frag0)))
1480 ctx0.first_frag = 0;
1481 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1483 if (nat_ip6_reass_add_fragment
1484 (thread_index, reass0, bi0, &fragments_to_drop))
1486 b0->error = node->errors[NAT64_IN2OUT_ERROR_MAX_FRAG];
1487 next0 = NAT64_IN2OUT_NEXT_DROP;
1496 ctx0.first_frag = 1;
1498 saddr0.as_u64[0] = ip60->src_address.as_u64[0];
1499 saddr0.as_u64[1] = ip60->src_address.as_u64[1];
1500 daddr0.as_u64[0] = ip60->dst_address.as_u64[0];
1501 daddr0.as_u64[1] = ip60->dst_address.as_u64[1];
1504 nat64_db_st_entry_find (db, &saddr0, &daddr0,
1505 udp0->src_port, udp0->dst_port,
1506 l4_protocol0, fib_index0, 1);
1510 nat64_db_bib_entry_find (db, &saddr0, udp0->src_port,
1511 l4_protocol0, fib_index0, 1);
1515 ip4_address_t out_addr0;
1516 if (nat64_alloc_out_addr_and_port
1517 (fib_index0, proto0, &out_addr0, &out_port0,
1520 next0 = NAT64_IN2OUT_NEXT_DROP;
1522 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1527 nat64_db_bib_entry_create (thread_index, db,
1529 &out_addr0, udp0->src_port,
1530 out_port0, fib_index0,
1534 next0 = NAT64_IN2OUT_NEXT_DROP;
1536 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1539 vlib_set_simple_counter (&nm->total_bibs, thread_index,
1540 0, db->bib.bib_entries_num);
1542 nat64_extract_ip4 (&ip60->dst_address, &daddr0.ip4,
1545 nat64_db_st_entry_create (thread_index, db, bibe0,
1546 &ip60->dst_address, &daddr0.ip4,
1550 next0 = NAT64_IN2OUT_NEXT_DROP;
1552 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1556 vlib_set_simple_counter (&nm->total_sessions, thread_index,
1557 0, db->st.st_entries_num);
1559 reass0->sess_index = nat64_db_st_entry_get_index (db, ste0);
1561 nat_ip6_reass_get_frags (reass0, &fragments_to_loopback);
1564 ctx0.sess_index = reass0->sess_index;
1565 ctx0.proto = l4_protocol0;
1567 ctx0.l4_offset = l4_offset0;
1569 if (PREDICT_FALSE (is_hairpinning (&ip60->dst_address)))
1571 next0 = NAT64_IN2OUT_NEXT_IP6_LOOKUP;
1572 if (nat64_in2out_frag_hairpinning (b0, ip60, &ctx0))
1574 next0 = NAT64_IN2OUT_NEXT_DROP;
1575 b0->error = node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1581 if (ip6_to_ip4_fragmented (b0, nat64_in2out_frag_set_cb, &ctx0))
1583 next0 = NAT64_IN2OUT_NEXT_DROP;
1584 b0->error = node->errors[NAT64_IN2OUT_ERROR_UNKNOWN];
1591 ((node->flags & VLIB_NODE_FLAG_TRACE)
1592 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1594 nat64_in2out_reass_trace_t *t =
1595 vlib_add_trace (vm, node, b0, sizeof (*t));
1596 t->cached = cached0;
1597 t->sw_if_index = sw_if_index0;
1598 t->next_index = next0;
1609 pkts_processed += next0 != NAT64_IN2OUT_NEXT_DROP;
1611 /* verify speculative enqueue, maybe switch current next frame */
1612 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1613 to_next, n_left_to_next,
1617 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1619 from = vlib_frame_vector_args (frame);
1620 u32 len = vec_len (fragments_to_loopback);
1621 if (len <= VLIB_FRAME_SIZE)
1623 clib_memcpy_fast (from, fragments_to_loopback,
1624 sizeof (u32) * len);
1626 vec_reset_length (fragments_to_loopback);
1630 clib_memcpy_fast (from, fragments_to_loopback +
1631 (len - VLIB_FRAME_SIZE),
1632 sizeof (u32) * VLIB_FRAME_SIZE);
1633 n_left_from = VLIB_FRAME_SIZE;
1634 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1639 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1642 vlib_node_increment_counter (vm, nm->in2out_reass_node_index,
1643 NAT64_IN2OUT_ERROR_PROCESSED_FRAGMENTS,
1645 vlib_node_increment_counter (vm, nm->in2out_reass_node_index,
1646 NAT64_IN2OUT_ERROR_CACHED_FRAGMENTS,
1649 nat_send_all_to_node (vm, fragments_to_drop, node,
1650 &node->errors[NAT64_IN2OUT_ERROR_DROP_FRAGMENT],
1651 NAT64_IN2OUT_NEXT_DROP);
1653 vec_free (fragments_to_drop);
1654 vec_free (fragments_to_loopback);
1655 return frame->n_vectors;
1659 VLIB_REGISTER_NODE (nat64_in2out_reass_node) = {
1660 .name = "nat64-in2out-reass",
1661 .vector_size = sizeof (u32),
1662 .format_trace = format_nat64_in2out_reass_trace,
1663 .type = VLIB_NODE_TYPE_INTERNAL,
1664 .n_errors = ARRAY_LEN (nat64_in2out_error_strings),
1665 .error_strings = nat64_in2out_error_strings,
1666 .n_next_nodes = NAT64_IN2OUT_N_NEXT,
1667 /* edit / add dispositions here */
1669 [NAT64_IN2OUT_NEXT_DROP] = "error-drop",
1670 [NAT64_IN2OUT_NEXT_IP4_LOOKUP] = "ip4-lookup",
1671 [NAT64_IN2OUT_NEXT_IP6_LOOKUP] = "ip6-lookup",
1672 [NAT64_IN2OUT_NEXT_SLOWPATH] = "nat64-in2out-slowpath",
1673 [NAT64_IN2OUT_NEXT_REASS] = "nat64-in2out-reass",
1678 #define foreach_nat64_in2out_handoff_error \
1679 _(CONGESTION_DROP, "congestion drop") \
1680 _(SAME_WORKER, "same worker") \
1681 _(DO_HANDOFF, "do handoff")
1685 #define _(sym,str) NAT64_IN2OUT_HANDOFF_ERROR_##sym,
1686 foreach_nat64_in2out_handoff_error
1688 NAT64_IN2OUT_HANDOFF_N_ERROR,
1689 } nat64_in2out_handoff_error_t;
1691 static char *nat64_in2out_handoff_error_strings[] = {
1692 #define _(sym,string) string,
1693 foreach_nat64_in2out_handoff_error
1699 u32 next_worker_index;
1700 } nat64_in2out_handoff_trace_t;
1703 format_nat64_in2out_handoff_trace (u8 * s, va_list * args)
1705 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1706 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1707 nat64_in2out_handoff_trace_t *t =
1708 va_arg (*args, nat64_in2out_handoff_trace_t *);
1711 format (s, "NAT64-IN2OUT-HANDOFF: next-worker %d", t->next_worker_index);
1716 VLIB_NODE_FN (nat64_in2out_handoff_node) (vlib_main_t * vm,
1717 vlib_node_runtime_t * node,
1718 vlib_frame_t * frame)
1720 nat64_main_t *nm = &nat64_main;
1721 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1722 u32 n_enq, n_left_from, *from;
1723 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1725 u32 thread_index = vm->thread_index;
1726 u32 do_handoff = 0, same_worker = 0;
1728 from = vlib_frame_vector_args (frame);
1729 n_left_from = frame->n_vectors;
1730 vlib_get_buffers (vm, from, bufs, n_left_from);
1733 ti = thread_indices;
1735 fq_index = nm->fq_in2out_index;
1737 while (n_left_from > 0)
1741 ip0 = vlib_buffer_get_current (b[0]);
1742 ti[0] = nat64_get_worker_in2out (&ip0->src_address);
1744 if (ti[0] != thread_index)
1750 ((node->flags & VLIB_NODE_FLAG_TRACE)
1751 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1753 nat64_in2out_handoff_trace_t *t =
1754 vlib_add_trace (vm, node, b[0], sizeof (*t));
1755 t->next_worker_index = ti[0];
1764 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1765 frame->n_vectors, 1);
1767 if (n_enq < frame->n_vectors)
1768 vlib_node_increment_counter (vm, node->node_index,
1769 NAT64_IN2OUT_HANDOFF_ERROR_CONGESTION_DROP,
1770 frame->n_vectors - n_enq);
1771 vlib_node_increment_counter (vm, node->node_index,
1772 NAT64_IN2OUT_HANDOFF_ERROR_SAME_WORKER,
1774 vlib_node_increment_counter (vm, node->node_index,
1775 NAT64_IN2OUT_HANDOFF_ERROR_DO_HANDOFF,
1778 return frame->n_vectors;
1782 VLIB_REGISTER_NODE (nat64_in2out_handoff_node) = {
1783 .name = "nat64-in2out-handoff",
1784 .vector_size = sizeof (u32),
1785 .format_trace = format_nat64_in2out_handoff_trace,
1786 .type = VLIB_NODE_TYPE_INTERNAL,
1787 .n_errors = ARRAY_LEN(nat64_in2out_handoff_error_strings),
1788 .error_strings = nat64_in2out_handoff_error_strings,
1799 * fd.io coding-style-patch-verification: ON
1802 * eval: (c-set-style "gnu")