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");
73 vlib_node_registration_t nat64_in2out_node;
74 vlib_node_registration_t nat64_in2out_slowpath_node;
75 vlib_node_registration_t nat64_in2out_reass_node;
76 vlib_node_registration_t nat64_in2out_handoff_node;
78 #define foreach_nat64_in2out_error \
79 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
80 _(IN2OUT_PACKETS, "good in2out packets processed") \
81 _(NO_TRANSLATION, "no translation") \
82 _(UNKNOWN, "unknown") \
83 _(DROP_FRAGMENT, "drop fragment") \
84 _(MAX_REASS, "maximum reassemblies exceeded") \
85 _(MAX_FRAG, "maximum fragments per reassembly exceeded") \
86 _(TCP_PACKETS, "TCP packets") \
87 _(UDP_PACKETS, "UDP packets") \
88 _(ICMP_PACKETS, "ICMP packets") \
89 _(OTHER_PACKETS, "other protocol packets") \
90 _(FRAGMENTS, "fragments") \
91 _(CACHED_FRAGMENTS, "cached fragments") \
92 _(PROCESSED_FRAGMENTS, "processed fragments")
97 #define _(sym,str) NAT64_IN2OUT_ERROR_##sym,
98 foreach_nat64_in2out_error
100 NAT64_IN2OUT_N_ERROR,
101 } nat64_in2out_error_t;
103 static char *nat64_in2out_error_strings[] = {
104 #define _(sym,string) string,
105 foreach_nat64_in2out_error
111 NAT64_IN2OUT_NEXT_IP4_LOOKUP,
112 NAT64_IN2OUT_NEXT_IP6_LOOKUP,
113 NAT64_IN2OUT_NEXT_DROP,
114 NAT64_IN2OUT_NEXT_SLOWPATH,
115 NAT64_IN2OUT_NEXT_REASS,
117 } nat64_in2out_next_t;
119 typedef struct nat64_in2out_set_ctx_t_
124 } nat64_in2out_set_ctx_t;
127 nat64_not_translate (u32 sw_if_index, ip6_address_t ip6_addr)
130 ip6_main_t *im6 = &ip6_main;
131 ip_lookup_main_t *lm6 = &im6->lookup_main;
132 ip_interface_address_t *ia = 0;
135 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
137 addr = ip_interface_address_get_address (lm6, ia);
138 if (0 == ip6_address_compare (addr, &ip6_addr))
147 * @brief Check whether is a hairpinning.
149 * If the destination IP address of the packet is an IPv4 address assigned to
150 * the NAT64 itself, then the packet is a hairpin packet.
152 * param dst_addr Destination address of the packet.
154 * @returns 1 if hairpinning, otherwise 0.
156 static_always_inline int
157 is_hairpinning (ip6_address_t * dst_addr)
159 nat64_main_t *nm = &nat64_main;
162 for (i = 0; i < vec_len (nm->addr_pool); i++)
164 if (nm->addr_pool[i].addr.as_u32 == dst_addr->as_u32[3])
172 nat64_in2out_tcp_udp_set_cb (ip6_header_t * ip6, ip4_header_t * ip4,
175 nat64_main_t *nm = &nat64_main;
176 nat64_in2out_set_ctx_t *ctx = arg;
177 nat64_db_bib_entry_t *bibe;
178 nat64_db_st_entry_t *ste;
179 ip46_address_t saddr, daddr;
180 u32 sw_if_index, fib_index;
181 udp_header_t *udp = ip6_next_header (ip6);
182 u8 proto = ip6->protocol;
183 u16 sport = udp->src_port;
184 u16 dport = udp->dst_port;
185 nat64_db_t *db = &nm->db[ctx->thread_index];
187 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
189 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
191 saddr.as_u64[0] = ip6->src_address.as_u64[0];
192 saddr.as_u64[1] = ip6->src_address.as_u64[1];
193 daddr.as_u64[0] = ip6->dst_address.as_u64[0];
194 daddr.as_u64[1] = ip6->dst_address.as_u64[1];
197 nat64_db_st_entry_find (db, &saddr, &daddr, sport, dport, proto,
202 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
208 bibe = nat64_db_bib_entry_find (db, &saddr, sport, proto, fib_index, 1);
213 ip4_address_t out_addr;
214 if (nat64_alloc_out_addr_and_port
215 (fib_index, ip_proto_to_snat_proto (proto), &out_addr,
216 &out_port, ctx->thread_index))
220 nat64_db_bib_entry_create (db, &ip6->src_address, &out_addr,
221 sport, out_port, fib_index, proto, 0);
225 vlib_set_simple_counter (&nm->total_bibs, ctx->thread_index, 0,
226 db->bib.bib_entries_num);
229 nat64_extract_ip4 (&ip6->dst_address, &daddr.ip4, fib_index);
231 nat64_db_st_entry_create (db, bibe, &ip6->dst_address,
236 vlib_set_simple_counter (&nm->total_sessions, ctx->thread_index, 0,
237 db->st.st_entries_num);
240 ip4->src_address.as_u32 = bibe->out_addr.as_u32;
241 udp->src_port = bibe->out_port;
243 ip4->dst_address.as_u32 = ste->out_r_addr.as_u32;
245 if (proto == IP_PROTOCOL_TCP)
249 tcp_header_t *tcp = ip6_next_header (ip6);
251 nat64_tcp_session_set_state (ste, tcp, 1);
252 checksum = &tcp->checksum;
253 csum = ip_csum_sub_even (*checksum, sport);
254 csum = ip_csum_add_even (csum, udp->src_port);
255 mss_clamping (nm->sm, tcp, &csum);
256 *checksum = ip_csum_fold (csum);
259 nat64_session_reset_timeout (ste, ctx->vm);
265 nat64_in2out_icmp_set_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *arg)
267 nat64_main_t *nm = &nat64_main;
268 nat64_in2out_set_ctx_t *ctx = arg;
269 nat64_db_bib_entry_t *bibe;
270 nat64_db_st_entry_t *ste;
271 ip46_address_t saddr, daddr;
272 u32 sw_if_index, fib_index;
273 icmp46_header_t *icmp = ip6_next_header (ip6);
274 nat64_db_t *db = &nm->db[ctx->thread_index];
276 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
278 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
280 saddr.as_u64[0] = ip6->src_address.as_u64[0];
281 saddr.as_u64[1] = ip6->src_address.as_u64[1];
282 daddr.as_u64[0] = ip6->dst_address.as_u64[0];
283 daddr.as_u64[1] = ip6->dst_address.as_u64[1];
285 if (icmp->type == ICMP4_echo_request || icmp->type == ICMP4_echo_reply)
287 u16 in_id = ((u16 *) (icmp))[2];
289 nat64_db_st_entry_find (db, &saddr, &daddr, in_id, 0,
290 IP_PROTOCOL_ICMP, fib_index, 1);
295 nat64_db_bib_entry_by_index (db, IP_PROTOCOL_ICMP,
303 nat64_db_bib_entry_find (db, &saddr, in_id,
304 IP_PROTOCOL_ICMP, fib_index, 1);
309 ip4_address_t out_addr;
310 if (nat64_alloc_out_addr_and_port
311 (fib_index, SNAT_PROTOCOL_ICMP, &out_addr, &out_id,
316 nat64_db_bib_entry_create (db, &ip6->src_address,
317 &out_addr, in_id, out_id,
318 fib_index, IP_PROTOCOL_ICMP, 0);
322 vlib_set_simple_counter (&nm->total_bibs, ctx->thread_index, 0,
323 db->bib.bib_entries_num);
326 nat64_extract_ip4 (&ip6->dst_address, &daddr.ip4, fib_index);
328 nat64_db_st_entry_create (db, bibe, &ip6->dst_address,
333 vlib_set_simple_counter (&nm->total_sessions, ctx->thread_index, 0,
334 db->st.st_entries_num);
337 nat64_session_reset_timeout (ste, ctx->vm);
339 ip4->src_address.as_u32 = bibe->out_addr.as_u32;
340 ((u16 *) (icmp))[2] = bibe->out_port;
342 ip4->dst_address.as_u32 = ste->out_r_addr.as_u32;
346 if (!vec_len (nm->addr_pool))
349 ip4->src_address.as_u32 = nm->addr_pool[0].addr.as_u32;
350 nat64_extract_ip4 (&ip6->dst_address, &ip4->dst_address, fib_index);
357 nat64_in2out_inner_icmp_set_cb (ip6_header_t * ip6, ip4_header_t * ip4,
360 nat64_main_t *nm = &nat64_main;
361 nat64_in2out_set_ctx_t *ctx = arg;
362 nat64_db_st_entry_t *ste;
363 nat64_db_bib_entry_t *bibe;
364 ip46_address_t saddr, daddr;
365 u32 sw_if_index, fib_index;
366 u8 proto = ip6->protocol;
367 nat64_db_t *db = &nm->db[ctx->thread_index];
369 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
371 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
373 saddr.as_u64[0] = ip6->src_address.as_u64[0];
374 saddr.as_u64[1] = ip6->src_address.as_u64[1];
375 daddr.as_u64[0] = ip6->dst_address.as_u64[0];
376 daddr.as_u64[1] = ip6->dst_address.as_u64[1];
378 if (proto == IP_PROTOCOL_ICMP6)
380 icmp46_header_t *icmp = ip6_next_header (ip6);
381 u16 in_id = ((u16 *) (icmp))[2];
382 proto = IP_PROTOCOL_ICMP;
385 (icmp->type == ICMP4_echo_request
386 || icmp->type == ICMP4_echo_reply))
390 nat64_db_st_entry_find (db, &daddr, &saddr, in_id, 0, proto,
395 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
399 ip4->dst_address.as_u32 = bibe->out_addr.as_u32;
400 ((u16 *) (icmp))[2] = bibe->out_port;
401 ip4->src_address.as_u32 = ste->out_r_addr.as_u32;
405 udp_header_t *udp = ip6_next_header (ip6);
406 tcp_header_t *tcp = ip6_next_header (ip6);
410 u16 sport = udp->src_port;
411 u16 dport = udp->dst_port;
414 nat64_db_st_entry_find (db, &daddr, &saddr, dport, sport, proto,
419 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
423 ip4->dst_address.as_u32 = bibe->out_addr.as_u32;
424 udp->dst_port = bibe->out_port;
425 ip4->src_address.as_u32 = ste->out_r_addr.as_u32;
427 if (proto == IP_PROTOCOL_TCP)
428 checksum = &tcp->checksum;
430 checksum = &udp->checksum;
431 csum = ip_csum_sub_even (*checksum, dport);
432 csum = ip_csum_add_even (csum, udp->dst_port);
433 *checksum = ip_csum_fold (csum);
439 typedef struct unk_proto_st_walk_ctx_t_
441 ip6_address_t src_addr;
442 ip6_address_t dst_addr;
443 ip4_address_t out_addr;
447 } unk_proto_st_walk_ctx_t;
450 unk_proto_st_walk (nat64_db_st_entry_t * ste, void *arg)
452 nat64_main_t *nm = &nat64_main;
453 unk_proto_st_walk_ctx_t *ctx = arg;
454 nat64_db_bib_entry_t *bibe;
455 ip46_address_t saddr, daddr;
456 nat64_db_t *db = &nm->db[ctx->thread_index];
458 if (ip46_address_is_equal (&ste->in_r_addr, &ctx->dst_addr))
460 bibe = nat64_db_bib_entry_by_index (db, ste->proto, ste->bibe_index);
464 if (ip46_address_is_equal (&bibe->in_addr, &ctx->src_addr)
465 && bibe->fib_index == ctx->fib_index)
467 clib_memset (&saddr, 0, sizeof (saddr));
468 saddr.ip4.as_u32 = bibe->out_addr.as_u32;
469 clib_memset (&daddr, 0, sizeof (daddr));
470 nat64_extract_ip4 (&ctx->dst_addr, &daddr.ip4, ctx->fib_index);
472 if (nat64_db_st_entry_find
473 (db, &daddr, &saddr, 0, 0, ctx->proto, ctx->fib_index, 0))
476 ctx->out_addr.as_u32 = bibe->out_addr.as_u32;
485 nat64_in2out_unk_proto_set_cb (ip6_header_t * ip6, ip4_header_t * ip4,
488 nat64_main_t *nm = &nat64_main;
489 nat64_in2out_set_ctx_t *s_ctx = arg;
490 nat64_db_bib_entry_t *bibe;
491 nat64_db_st_entry_t *ste;
492 ip46_address_t saddr, daddr, addr;
493 u32 sw_if_index, fib_index;
494 u8 proto = ip6->protocol;
496 nat64_db_t *db = &nm->db[s_ctx->thread_index];
498 sw_if_index = vnet_buffer (s_ctx->b)->sw_if_index[VLIB_RX];
500 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
502 saddr.as_u64[0] = ip6->src_address.as_u64[0];
503 saddr.as_u64[1] = ip6->src_address.as_u64[1];
504 daddr.as_u64[0] = ip6->dst_address.as_u64[0];
505 daddr.as_u64[1] = ip6->dst_address.as_u64[1];
508 nat64_db_st_entry_find (db, &saddr, &daddr, 0, 0, proto, fib_index, 1);
512 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
518 bibe = nat64_db_bib_entry_find (db, &saddr, 0, proto, fib_index, 1);
522 /* Choose same out address as for TCP/UDP session to same dst */
523 unk_proto_st_walk_ctx_t ctx = {
524 .src_addr.as_u64[0] = ip6->src_address.as_u64[0],
525 .src_addr.as_u64[1] = ip6->src_address.as_u64[1],
526 .dst_addr.as_u64[0] = ip6->dst_address.as_u64[0],
527 .dst_addr.as_u64[1] = ip6->dst_address.as_u64[1],
528 .out_addr.as_u32 = 0,
529 .fib_index = fib_index,
531 .thread_index = s_ctx->thread_index,
534 nat64_db_st_walk (db, IP_PROTOCOL_TCP, unk_proto_st_walk, &ctx);
536 if (!ctx.out_addr.as_u32)
537 nat64_db_st_walk (db, IP_PROTOCOL_UDP, unk_proto_st_walk, &ctx);
539 /* Verify if out address is not already in use for protocol */
540 clib_memset (&addr, 0, sizeof (addr));
541 addr.ip4.as_u32 = ctx.out_addr.as_u32;
542 if (nat64_db_bib_entry_find (db, &addr, 0, proto, 0, 0))
543 ctx.out_addr.as_u32 = 0;
545 if (!ctx.out_addr.as_u32)
547 for (i = 0; i < vec_len (nm->addr_pool); i++)
549 addr.ip4.as_u32 = nm->addr_pool[i].addr.as_u32;
550 if (!nat64_db_bib_entry_find (db, &addr, 0, proto, 0, 0))
555 if (!ctx.out_addr.as_u32)
559 nat64_db_bib_entry_create (db, &ip6->src_address,
560 &ctx.out_addr, 0, 0, fib_index, proto,
565 vlib_set_simple_counter (&nm->total_bibs, s_ctx->thread_index, 0,
566 db->bib.bib_entries_num);
569 nat64_extract_ip4 (&ip6->dst_address, &daddr.ip4, fib_index);
571 nat64_db_st_entry_create (db, bibe, &ip6->dst_address, &daddr.ip4, 0);
575 vlib_set_simple_counter (&nm->total_sessions, s_ctx->thread_index, 0,
576 db->st.st_entries_num);
579 nat64_session_reset_timeout (ste, s_ctx->vm);
581 ip4->src_address.as_u32 = bibe->out_addr.as_u32;
582 ip4->dst_address.as_u32 = ste->out_r_addr.as_u32;
590 nat64_in2out_tcp_udp_hairpinning (vlib_main_t * vm, vlib_buffer_t * b,
591 ip6_header_t * ip6, u32 thread_index)
593 nat64_main_t *nm = &nat64_main;
594 nat64_db_bib_entry_t *bibe;
595 nat64_db_st_entry_t *ste;
596 ip46_address_t saddr, daddr;
597 u32 sw_if_index, fib_index;
598 udp_header_t *udp = ip6_next_header (ip6);
599 tcp_header_t *tcp = ip6_next_header (ip6);
600 u8 proto = ip6->protocol;
601 u16 sport = udp->src_port;
602 u16 dport = udp->dst_port;
605 nat64_db_t *db = &nm->db[thread_index];
607 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
609 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
611 saddr.as_u64[0] = ip6->src_address.as_u64[0];
612 saddr.as_u64[1] = ip6->src_address.as_u64[1];
613 daddr.as_u64[0] = ip6->dst_address.as_u64[0];
614 daddr.as_u64[1] = ip6->dst_address.as_u64[1];
616 if (proto == IP_PROTOCOL_UDP)
617 checksum = &udp->checksum;
619 checksum = &tcp->checksum;
621 csum = ip_csum_sub_even (*checksum, ip6->src_address.as_u64[0]);
622 csum = ip_csum_sub_even (csum, ip6->src_address.as_u64[1]);
623 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[0]);
624 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[1]);
625 csum = ip_csum_sub_even (csum, sport);
626 csum = ip_csum_sub_even (csum, dport);
629 nat64_db_st_entry_find (db, &saddr, &daddr, sport, dport, proto,
634 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
640 bibe = nat64_db_bib_entry_find (db, &saddr, sport, proto, fib_index, 1);
645 ip4_address_t out_addr;
646 if (nat64_alloc_out_addr_and_port
647 (fib_index, ip_proto_to_snat_proto (proto), &out_addr,
648 &out_port, thread_index))
652 nat64_db_bib_entry_create (db, &ip6->src_address, &out_addr,
653 sport, out_port, fib_index, proto, 0);
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 (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 (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 (db, bibe, &ip6->dst_address, &daddr.ip4, 0);
928 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
929 db->st.st_entries_num);
932 nat64_session_reset_timeout (ste, vm);
934 nat64_compose_ip6 (&ip6->src_address, &bibe->out_addr, fib_index);
936 clib_memset (&daddr, 0, sizeof (daddr));
937 daddr.ip4.as_u32 = ste->out_r_addr.as_u32;
941 vec_foreach (db, nm->db)
943 bibe = nat64_db_bib_entry_find (db, &daddr, 0, proto, 0, 0);
953 ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
954 ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
960 nat64_in2out_node_fn_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
961 vlib_frame_t * frame, u8 is_slow_path)
963 u32 n_left_from, *from, *to_next;
964 nat64_in2out_next_t next_index;
965 u32 pkts_processed = 0;
966 u32 stats_node_index;
967 u32 thread_index = vm->thread_index;
968 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
972 is_slow_path ? nat64_in2out_slowpath_node.index : nat64_in2out_node.index;
974 from = vlib_frame_vector_args (frame);
975 n_left_from = frame->n_vectors;
976 next_index = node->cached_next_index;
978 while (n_left_from > 0)
982 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
984 while (n_left_from > 0 && n_left_to_next > 0)
990 u16 l4_offset0, frag_offset0;
993 nat64_in2out_set_ctx_t ctx0;
996 /* speculatively enqueue b0 to the current next frame */
1002 n_left_to_next -= 1;
1004 b0 = vlib_get_buffer (vm, bi0);
1005 ip60 = vlib_buffer_get_current (b0);
1009 ctx0.thread_index = thread_index;
1011 next0 = NAT64_IN2OUT_NEXT_IP4_LOOKUP;
1015 (ip60, b0->current_length, &l4_protocol0, &l4_offset0,
1018 next0 = NAT64_IN2OUT_NEXT_DROP;
1019 b0->error = node->errors[NAT64_IN2OUT_ERROR_UNKNOWN];
1023 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1025 if (nat64_not_translate (sw_if_index0, ip60->dst_address))
1027 next0 = NAT64_IN2OUT_NEXT_IP6_LOOKUP;
1031 proto0 = ip_proto_to_snat_proto (l4_protocol0);
1035 if (PREDICT_TRUE (proto0 == ~0))
1038 if (is_hairpinning (&ip60->dst_address))
1040 next0 = NAT64_IN2OUT_NEXT_IP6_LOOKUP;
1041 if (nat64_in2out_unk_proto_hairpinning
1042 (vm, b0, ip60, thread_index))
1044 next0 = NAT64_IN2OUT_NEXT_DROP;
1046 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1051 if (ip6_to_ip4 (b0, nat64_in2out_unk_proto_set_cb, &ctx0))
1053 next0 = NAT64_IN2OUT_NEXT_DROP;
1055 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1063 if (PREDICT_FALSE (proto0 == ~0))
1065 next0 = NAT64_IN2OUT_NEXT_SLOWPATH;
1071 (ip60->protocol == IP_PROTOCOL_IPV6_FRAGMENTATION))
1073 next0 = NAT64_IN2OUT_NEXT_REASS;
1078 if (proto0 == SNAT_PROTOCOL_ICMP)
1081 if (is_hairpinning (&ip60->dst_address))
1083 next0 = NAT64_IN2OUT_NEXT_IP6_LOOKUP;
1084 if (nat64_in2out_icmp_hairpinning
1085 (vm, b0, ip60, thread_index))
1087 next0 = NAT64_IN2OUT_NEXT_DROP;
1089 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1095 (b0, nat64_in2out_icmp_set_cb, &ctx0,
1096 nat64_in2out_inner_icmp_set_cb, &ctx0))
1098 next0 = NAT64_IN2OUT_NEXT_DROP;
1099 b0->error = node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1103 else if (proto0 == SNAT_PROTOCOL_TCP || proto0 == SNAT_PROTOCOL_UDP)
1105 if (proto0 == SNAT_PROTOCOL_TCP)
1110 if (is_hairpinning (&ip60->dst_address))
1112 next0 = NAT64_IN2OUT_NEXT_IP6_LOOKUP;
1113 if (nat64_in2out_tcp_udp_hairpinning
1114 (vm, b0, ip60, thread_index))
1116 next0 = NAT64_IN2OUT_NEXT_DROP;
1118 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1123 if (ip6_to_ip4_tcp_udp
1124 (b0, nat64_in2out_tcp_udp_set_cb, &ctx0, 0))
1126 next0 = NAT64_IN2OUT_NEXT_DROP;
1127 b0->error = node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1133 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1134 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1136 nat64_in2out_trace_t *t =
1137 vlib_add_trace (vm, node, b0, sizeof (*t));
1138 t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1139 t->next_index = next0;
1140 t->is_slow_path = is_slow_path;
1143 pkts_processed += next0 == NAT64_IN2OUT_NEXT_IP4_LOOKUP;
1145 /* verify speculative enqueue, maybe switch current next frame */
1146 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1147 n_left_to_next, bi0, next0);
1149 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1151 vlib_node_increment_counter (vm, stats_node_index,
1152 NAT64_IN2OUT_ERROR_IN2OUT_PACKETS,
1154 vlib_node_increment_counter (vm, stats_node_index,
1155 NAT64_IN2OUT_ERROR_TCP_PACKETS, tcp_packets);
1156 vlib_node_increment_counter (vm, stats_node_index,
1157 NAT64_IN2OUT_ERROR_UDP_PACKETS, tcp_packets);
1158 vlib_node_increment_counter (vm, stats_node_index,
1159 NAT64_IN2OUT_ERROR_ICMP_PACKETS, icmp_packets);
1160 vlib_node_increment_counter (vm, stats_node_index,
1161 NAT64_IN2OUT_ERROR_OTHER_PACKETS,
1163 vlib_node_increment_counter (vm, stats_node_index,
1164 NAT64_IN2OUT_ERROR_FRAGMENTS, fragments);
1166 return frame->n_vectors;
1170 nat64_in2out_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1171 vlib_frame_t * frame)
1173 return nat64_in2out_node_fn_inline (vm, node, frame, 0);
1177 VLIB_REGISTER_NODE (nat64_in2out_node) = {
1178 .function = nat64_in2out_node_fn,
1179 .name = "nat64-in2out",
1180 .vector_size = sizeof (u32),
1181 .format_trace = format_nat64_in2out_trace,
1182 .type = VLIB_NODE_TYPE_INTERNAL,
1183 .n_errors = ARRAY_LEN (nat64_in2out_error_strings),
1184 .error_strings = nat64_in2out_error_strings,
1185 .n_next_nodes = NAT64_IN2OUT_N_NEXT,
1186 /* edit / add dispositions here */
1188 [NAT64_IN2OUT_NEXT_DROP] = "error-drop",
1189 [NAT64_IN2OUT_NEXT_IP4_LOOKUP] = "ip4-lookup",
1190 [NAT64_IN2OUT_NEXT_IP6_LOOKUP] = "ip6-lookup",
1191 [NAT64_IN2OUT_NEXT_SLOWPATH] = "nat64-in2out-slowpath",
1192 [NAT64_IN2OUT_NEXT_REASS] = "nat64-in2out-reass",
1197 VLIB_NODE_FUNCTION_MULTIARCH (nat64_in2out_node, nat64_in2out_node_fn);
1200 nat64_in2out_slowpath_node_fn (vlib_main_t * vm, 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 .function = nat64_in2out_slowpath_node_fn,
1209 .name = "nat64-in2out-slowpath",
1210 .vector_size = sizeof (u32),
1211 .format_trace = format_nat64_in2out_trace,
1212 .type = VLIB_NODE_TYPE_INTERNAL,
1213 .n_errors = ARRAY_LEN (nat64_in2out_error_strings),
1214 .error_strings = nat64_in2out_error_strings,
1215 .n_next_nodes = NAT64_IN2OUT_N_NEXT,
1216 /* edit / add dispositions here */
1218 [NAT64_IN2OUT_NEXT_DROP] = "error-drop",
1219 [NAT64_IN2OUT_NEXT_IP4_LOOKUP] = "ip4-lookup",
1220 [NAT64_IN2OUT_NEXT_IP6_LOOKUP] = "ip6-lookup",
1221 [NAT64_IN2OUT_NEXT_SLOWPATH] = "nat64-in2out-slowpath",
1222 [NAT64_IN2OUT_NEXT_REASS] = "nat64-in2out-reass",
1227 VLIB_NODE_FUNCTION_MULTIARCH (nat64_in2out_slowpath_node,
1228 nat64_in2out_slowpath_node_fn);
1230 typedef struct nat64_in2out_frag_set_ctx_t_
1238 } nat64_in2out_frag_set_ctx_t;
1241 nat64_in2out_frag_set_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *arg)
1243 nat64_main_t *nm = &nat64_main;
1244 nat64_in2out_frag_set_ctx_t *ctx = arg;
1245 nat64_db_st_entry_t *ste;
1246 nat64_db_bib_entry_t *bibe;
1248 nat64_db_t *db = &nm->db[ctx->thread_index];
1250 ste = nat64_db_st_entry_by_index (db, ctx->proto, ctx->sess_index);
1254 bibe = nat64_db_bib_entry_by_index (db, ctx->proto, ste->bibe_index);
1258 nat64_session_reset_timeout (ste, ctx->vm);
1260 if (ctx->first_frag)
1262 udp = (udp_header_t *) u8_ptr_add (ip6, ctx->l4_offset);
1264 if (ctx->proto == IP_PROTOCOL_TCP)
1268 tcp_header_t *tcp = (tcp_header_t *) udp;
1270 nat64_tcp_session_set_state (ste, tcp, 1);
1271 checksum = &tcp->checksum;
1272 csum = ip_csum_sub_even (*checksum, tcp->src_port);
1273 csum = ip_csum_sub_even (csum, ip6->src_address.as_u64[0]);
1274 csum = ip_csum_sub_even (csum, ip6->src_address.as_u64[1]);
1275 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[0]);
1276 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[1]);
1277 csum = ip_csum_add_even (csum, bibe->out_port);
1278 csum = ip_csum_add_even (csum, bibe->out_addr.as_u32);
1279 csum = ip_csum_add_even (csum, ste->out_r_addr.as_u32);
1280 *checksum = ip_csum_fold (csum);
1283 udp->src_port = bibe->out_port;
1286 ip4->src_address.as_u32 = bibe->out_addr.as_u32;
1287 ip4->dst_address.as_u32 = ste->out_r_addr.as_u32;
1293 nat64_in2out_frag_hairpinning (vlib_buffer_t * b, ip6_header_t * ip6,
1294 nat64_in2out_frag_set_ctx_t * ctx)
1296 nat64_main_t *nm = &nat64_main;
1297 nat64_db_st_entry_t *ste;
1298 nat64_db_bib_entry_t *bibe;
1299 udp_header_t *udp = (udp_header_t *) u8_ptr_add (ip6, ctx->l4_offset);
1300 tcp_header_t *tcp = (tcp_header_t *) udp;
1301 u16 sport = udp->src_port;
1302 u16 dport = udp->dst_port;
1305 ip46_address_t daddr;
1306 nat64_db_t *db = &nm->db[ctx->thread_index];
1308 if (ctx->first_frag)
1310 if (ctx->proto == IP_PROTOCOL_UDP)
1311 checksum = &udp->checksum;
1313 checksum = &tcp->checksum;
1315 csum = ip_csum_sub_even (*checksum, ip6->src_address.as_u64[0]);
1316 csum = ip_csum_sub_even (csum, ip6->src_address.as_u64[1]);
1317 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[0]);
1318 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[1]);
1319 csum = ip_csum_sub_even (csum, sport);
1320 csum = ip_csum_sub_even (csum, dport);
1323 ste = nat64_db_st_entry_by_index (db, ctx->proto, ctx->sess_index);
1327 bibe = nat64_db_bib_entry_by_index (db, ctx->proto, ste->bibe_index);
1331 if (ctx->proto == IP_PROTOCOL_TCP)
1332 nat64_tcp_session_set_state (ste, tcp, 1);
1334 nat64_session_reset_timeout (ste, ctx->vm);
1336 sport = bibe->out_port;
1337 dport = ste->r_port;
1339 nat64_compose_ip6 (&ip6->src_address, &bibe->out_addr, bibe->fib_index);
1341 clib_memset (&daddr, 0, sizeof (daddr));
1342 daddr.ip4.as_u32 = ste->out_r_addr.as_u32;
1346 vec_foreach (db, nm->db)
1348 bibe = nat64_db_bib_entry_find (db, &daddr, dport, ctx->proto, 0, 0);
1358 ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
1359 ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
1361 if (ctx->first_frag)
1363 udp->dst_port = bibe->in_port;
1364 udp->src_port = sport;
1365 csum = ip_csum_add_even (csum, ip6->src_address.as_u64[0]);
1366 csum = ip_csum_add_even (csum, ip6->src_address.as_u64[1]);
1367 csum = ip_csum_add_even (csum, ip6->dst_address.as_u64[0]);
1368 csum = ip_csum_add_even (csum, ip6->dst_address.as_u64[1]);
1369 csum = ip_csum_add_even (csum, udp->src_port);
1370 csum = ip_csum_add_even (csum, udp->dst_port);
1371 *checksum = ip_csum_fold (csum);
1378 nat64_in2out_reass_node_fn (vlib_main_t * vm,
1379 vlib_node_runtime_t * node, vlib_frame_t * frame)
1381 u32 n_left_from, *from, *to_next;
1382 nat64_in2out_next_t next_index;
1383 u32 pkts_processed = 0, cached_fragments = 0;
1384 u32 *fragments_to_drop = 0;
1385 u32 *fragments_to_loopback = 0;
1386 nat64_main_t *nm = &nat64_main;
1387 u32 thread_index = vm->thread_index;
1389 from = vlib_frame_vector_args (frame);
1390 n_left_from = frame->n_vectors;
1391 next_index = node->cached_next_index;
1393 while (n_left_from > 0)
1397 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1399 while (n_left_from > 0 && n_left_to_next > 0)
1406 u16 l4_offset0, frag_offset0;
1408 nat_reass_ip6_t *reass0;
1409 ip6_frag_hdr_t *frag0;
1410 nat64_db_bib_entry_t *bibe0;
1411 nat64_db_st_entry_t *ste0;
1413 snat_protocol_t proto0;
1414 u32 sw_if_index0, fib_index0;
1415 ip46_address_t saddr0, daddr0;
1416 nat64_in2out_frag_set_ctx_t ctx0;
1417 nat64_db_t *db = &nm->db[thread_index];
1419 /* speculatively enqueue b0 to the current next frame */
1425 n_left_to_next -= 1;
1427 b0 = vlib_get_buffer (vm, bi0);
1428 next0 = NAT64_IN2OUT_NEXT_IP4_LOOKUP;
1430 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1432 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6,
1435 ctx0.thread_index = thread_index;
1437 if (PREDICT_FALSE (nat_reass_is_drop_frag (1)))
1439 next0 = NAT64_IN2OUT_NEXT_DROP;
1440 b0->error = node->errors[NAT64_IN2OUT_ERROR_DROP_FRAGMENT];
1444 ip60 = (ip6_header_t *) vlib_buffer_get_current (b0);
1448 (ip60, b0->current_length, &l4_protocol0, &l4_offset0,
1451 next0 = NAT64_IN2OUT_NEXT_DROP;
1452 b0->error = node->errors[NAT64_IN2OUT_ERROR_UNKNOWN];
1457 (!(l4_protocol0 == IP_PROTOCOL_TCP
1458 || l4_protocol0 == IP_PROTOCOL_UDP)))
1460 next0 = NAT64_IN2OUT_NEXT_DROP;
1461 b0->error = node->errors[NAT64_IN2OUT_ERROR_DROP_FRAGMENT];
1465 udp0 = (udp_header_t *) u8_ptr_add (ip60, l4_offset0);
1466 frag0 = (ip6_frag_hdr_t *) u8_ptr_add (ip60, frag_offset0);
1467 proto0 = ip_proto_to_snat_proto (l4_protocol0);
1469 reass0 = nat_ip6_reass_find_or_create (ip60->src_address,
1471 frag0->identification,
1473 1, &fragments_to_drop);
1475 if (PREDICT_FALSE (!reass0))
1477 next0 = NAT64_IN2OUT_NEXT_DROP;
1478 b0->error = node->errors[NAT64_IN2OUT_ERROR_MAX_REASS];
1482 if (PREDICT_TRUE (ip6_frag_hdr_offset (frag0)))
1484 ctx0.first_frag = 0;
1485 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1487 if (nat_ip6_reass_add_fragment
1488 (reass0, bi0, &fragments_to_drop))
1490 b0->error = node->errors[NAT64_IN2OUT_ERROR_MAX_FRAG];
1491 next0 = NAT64_IN2OUT_NEXT_DROP;
1500 ctx0.first_frag = 1;
1502 saddr0.as_u64[0] = ip60->src_address.as_u64[0];
1503 saddr0.as_u64[1] = ip60->src_address.as_u64[1];
1504 daddr0.as_u64[0] = ip60->dst_address.as_u64[0];
1505 daddr0.as_u64[1] = ip60->dst_address.as_u64[1];
1508 nat64_db_st_entry_find (db, &saddr0, &daddr0,
1509 udp0->src_port, udp0->dst_port,
1510 l4_protocol0, fib_index0, 1);
1514 nat64_db_bib_entry_find (db, &saddr0, udp0->src_port,
1515 l4_protocol0, fib_index0, 1);
1519 ip4_address_t out_addr0;
1520 if (nat64_alloc_out_addr_and_port
1521 (fib_index0, proto0, &out_addr0, &out_port0,
1524 next0 = NAT64_IN2OUT_NEXT_DROP;
1526 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1531 nat64_db_bib_entry_create (db,
1533 &out_addr0, udp0->src_port,
1534 out_port0, fib_index0,
1538 next0 = NAT64_IN2OUT_NEXT_DROP;
1540 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1543 vlib_set_simple_counter (&nm->total_bibs, thread_index,
1544 0, db->bib.bib_entries_num);
1546 nat64_extract_ip4 (&ip60->dst_address, &daddr0.ip4,
1549 nat64_db_st_entry_create (db, bibe0,
1550 &ip60->dst_address, &daddr0.ip4,
1554 next0 = NAT64_IN2OUT_NEXT_DROP;
1556 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1560 vlib_set_simple_counter (&nm->total_sessions, thread_index,
1561 0, db->st.st_entries_num);
1563 reass0->sess_index = nat64_db_st_entry_get_index (db, ste0);
1565 nat_ip6_reass_get_frags (reass0, &fragments_to_loopback);
1568 ctx0.sess_index = reass0->sess_index;
1569 ctx0.proto = l4_protocol0;
1571 ctx0.l4_offset = l4_offset0;
1573 if (PREDICT_FALSE (is_hairpinning (&ip60->dst_address)))
1575 next0 = NAT64_IN2OUT_NEXT_IP6_LOOKUP;
1576 if (nat64_in2out_frag_hairpinning (b0, ip60, &ctx0))
1578 next0 = NAT64_IN2OUT_NEXT_DROP;
1579 b0->error = node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1585 if (ip6_to_ip4_fragmented (b0, nat64_in2out_frag_set_cb, &ctx0))
1587 next0 = NAT64_IN2OUT_NEXT_DROP;
1588 b0->error = node->errors[NAT64_IN2OUT_ERROR_UNKNOWN];
1595 ((node->flags & VLIB_NODE_FLAG_TRACE)
1596 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1598 nat64_in2out_reass_trace_t *t =
1599 vlib_add_trace (vm, node, b0, sizeof (*t));
1600 t->cached = cached0;
1601 t->sw_if_index = sw_if_index0;
1602 t->next_index = next0;
1613 pkts_processed += next0 != NAT64_IN2OUT_NEXT_DROP;
1615 /* verify speculative enqueue, maybe switch current next frame */
1616 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1617 to_next, n_left_to_next,
1621 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1623 from = vlib_frame_vector_args (frame);
1624 u32 len = vec_len (fragments_to_loopback);
1625 if (len <= VLIB_FRAME_SIZE)
1627 clib_memcpy_fast (from, fragments_to_loopback,
1628 sizeof (u32) * len);
1630 vec_reset_length (fragments_to_loopback);
1634 clib_memcpy_fast (from, fragments_to_loopback +
1635 (len - VLIB_FRAME_SIZE),
1636 sizeof (u32) * VLIB_FRAME_SIZE);
1637 n_left_from = VLIB_FRAME_SIZE;
1638 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1643 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1646 vlib_node_increment_counter (vm, nat64_in2out_reass_node.index,
1647 NAT64_IN2OUT_ERROR_PROCESSED_FRAGMENTS,
1649 vlib_node_increment_counter (vm, nat64_in2out_reass_node.index,
1650 NAT64_IN2OUT_ERROR_CACHED_FRAGMENTS,
1653 nat_send_all_to_node (vm, fragments_to_drop, node,
1654 &node->errors[NAT64_IN2OUT_ERROR_DROP_FRAGMENT],
1655 NAT64_IN2OUT_NEXT_DROP);
1657 vec_free (fragments_to_drop);
1658 vec_free (fragments_to_loopback);
1659 return frame->n_vectors;
1663 VLIB_REGISTER_NODE (nat64_in2out_reass_node) = {
1664 .function = nat64_in2out_reass_node_fn,
1665 .name = "nat64-in2out-reass",
1666 .vector_size = sizeof (u32),
1667 .format_trace = format_nat64_in2out_reass_trace,
1668 .type = VLIB_NODE_TYPE_INTERNAL,
1669 .n_errors = ARRAY_LEN (nat64_in2out_error_strings),
1670 .error_strings = nat64_in2out_error_strings,
1671 .n_next_nodes = NAT64_IN2OUT_N_NEXT,
1672 /* edit / add dispositions here */
1674 [NAT64_IN2OUT_NEXT_DROP] = "error-drop",
1675 [NAT64_IN2OUT_NEXT_IP4_LOOKUP] = "ip4-lookup",
1676 [NAT64_IN2OUT_NEXT_IP6_LOOKUP] = "ip6-lookup",
1677 [NAT64_IN2OUT_NEXT_SLOWPATH] = "nat64-in2out-slowpath",
1678 [NAT64_IN2OUT_NEXT_REASS] = "nat64-in2out-reass",
1683 VLIB_NODE_FUNCTION_MULTIARCH (nat64_in2out_reass_node,
1684 nat64_in2out_reass_node_fn);
1686 #define foreach_nat64_in2out_handoff_error \
1687 _(CONGESTION_DROP, "congestion drop") \
1688 _(SAME_WORKER, "same worker") \
1689 _(DO_HANDOFF, "do handoff")
1693 #define _(sym,str) NAT64_IN2OUT_HANDOFF_ERROR_##sym,
1694 foreach_nat64_in2out_handoff_error
1696 NAT64_IN2OUT_HANDOFF_N_ERROR,
1697 } nat64_in2out_handoff_error_t;
1699 static char *nat64_in2out_handoff_error_strings[] = {
1700 #define _(sym,string) string,
1701 foreach_nat64_in2out_handoff_error
1707 u32 next_worker_index;
1708 } nat64_in2out_handoff_trace_t;
1711 format_nat64_in2out_handoff_trace (u8 * s, va_list * args)
1713 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1714 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1715 nat64_in2out_handoff_trace_t *t =
1716 va_arg (*args, nat64_in2out_handoff_trace_t *);
1719 format (s, "NAT64-IN2OUT-HANDOFF: next-worker %d", t->next_worker_index);
1725 nat64_in2out_handoff_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1726 vlib_frame_t * frame)
1728 nat64_main_t *nm = &nat64_main;
1729 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1730 u32 n_enq, n_left_from, *from;
1731 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1733 u32 thread_index = vm->thread_index;
1734 u32 do_handoff = 0, same_worker = 0;
1736 from = vlib_frame_vector_args (frame);
1737 n_left_from = frame->n_vectors;
1738 vlib_get_buffers (vm, from, bufs, n_left_from);
1741 ti = thread_indices;
1743 fq_index = nm->fq_in2out_index;
1745 while (n_left_from > 0)
1749 ip0 = vlib_buffer_get_current (b[0]);
1750 ti[0] = nat64_get_worker_in2out (&ip0->src_address);
1752 if (ti[0] != thread_index)
1758 ((node->flags & VLIB_NODE_FLAG_TRACE)
1759 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1761 nat64_in2out_handoff_trace_t *t =
1762 vlib_add_trace (vm, node, b[0], sizeof (*t));
1763 t->next_worker_index = ti[0];
1772 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1773 frame->n_vectors, 1);
1775 if (n_enq < frame->n_vectors)
1776 vlib_node_increment_counter (vm, node->node_index,
1777 NAT64_IN2OUT_HANDOFF_ERROR_CONGESTION_DROP,
1778 frame->n_vectors - n_enq);
1779 vlib_node_increment_counter (vm, node->node_index,
1780 NAT64_IN2OUT_HANDOFF_ERROR_SAME_WORKER,
1782 vlib_node_increment_counter (vm, node->node_index,
1783 NAT64_IN2OUT_HANDOFF_ERROR_DO_HANDOFF,
1786 return frame->n_vectors;
1790 VLIB_REGISTER_NODE (nat64_in2out_handoff_node) = {
1791 .function = nat64_in2out_handoff_node_fn,
1792 .name = "nat64-in2out-handoff",
1793 .vector_size = sizeof (u32),
1794 .format_trace = format_nat64_in2out_handoff_trace,
1795 .type = VLIB_NODE_TYPE_INTERNAL,
1796 .n_errors = ARRAY_LEN(nat64_in2out_handoff_error_strings),
1797 .error_strings = nat64_in2out_handoff_error_strings,
1807 VLIB_NODE_FUNCTION_MULTIARCH (nat64_in2out_handoff_node,
1808 nat64_in2out_handoff_node_fn);
1811 * fd.io coding-style-patch-verification: ON
1814 * eval: (c-set-style "gnu")