2 * Copyright (c) 2016 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.
16 #include <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vnet/pg/pg.h>
19 #include <vnet/handoff.h>
21 #include <vnet/ip/ip.h>
22 #include <vnet/udp/udp.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/fib/ip4_fib.h>
26 #include <nat/nat_ipfix_logging.h>
27 #include <nat/nat_det.h>
28 #include <nat/nat_reass.h>
30 #include <vppinfra/hash.h>
31 #include <vppinfra/error.h>
32 #include <vppinfra/elog.h>
38 } snat_out2in_trace_t;
41 u32 next_worker_index;
43 } snat_out2in_worker_handoff_trace_t;
45 /* packet trace format function */
46 static u8 * format_snat_out2in_trace (u8 * s, va_list * args)
48 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
49 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
50 snat_out2in_trace_t * t = va_arg (*args, snat_out2in_trace_t *);
52 s = format (s, "NAT44_OUT2IN: sw_if_index %d, next index %d, session index %d",
53 t->sw_if_index, t->next_index, t->session_index);
57 static u8 * format_snat_out2in_fast_trace (u8 * s, va_list * args)
59 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
60 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
61 snat_out2in_trace_t * t = va_arg (*args, snat_out2in_trace_t *);
63 s = format (s, "NAT44_OUT2IN_FAST: sw_if_index %d, next index %d",
64 t->sw_if_index, t->next_index);
68 static u8 * format_snat_out2in_worker_handoff_trace (u8 * s, va_list * args)
70 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
71 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
72 snat_out2in_worker_handoff_trace_t * t =
73 va_arg (*args, snat_out2in_worker_handoff_trace_t *);
76 m = t->do_handoff ? "next worker" : "same worker";
77 s = format (s, "NAT44_OUT2IN_WORKER_HANDOFF: %s %d", m, t->next_worker_index);
86 } nat44_out2in_reass_trace_t;
88 static u8 * format_nat44_out2in_reass_trace (u8 * s, va_list * args)
90 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
91 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
92 nat44_out2in_reass_trace_t * t = va_arg (*args, nat44_out2in_reass_trace_t *);
94 s = format (s, "NAT44_OUT2IN_REASS: sw_if_index %d, next index %d, status %s",
95 t->sw_if_index, t->next_index,
96 t->cached ? "cached" : "translated");
101 vlib_node_registration_t snat_out2in_node;
102 vlib_node_registration_t snat_out2in_fast_node;
103 vlib_node_registration_t snat_out2in_worker_handoff_node;
104 vlib_node_registration_t snat_det_out2in_node;
105 vlib_node_registration_t nat44_out2in_reass_node;
107 #define foreach_snat_out2in_error \
108 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
109 _(OUT2IN_PACKETS, "Good out2in packets processed") \
110 _(OUT_OF_PORTS, "Out of ports") \
111 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
112 _(NO_TRANSLATION, "No translation") \
113 _(MAX_SESSIONS_EXCEEDED, "Maximum sessions exceeded") \
114 _(DROP_FRAGMENT, "Drop fragment") \
115 _(MAX_REASS, "Maximum reassemblies exceeded") \
116 _(MAX_FRAG, "Maximum fragments per reassembly exceeded")
119 #define _(sym,str) SNAT_OUT2IN_ERROR_##sym,
120 foreach_snat_out2in_error
123 } snat_out2in_error_t;
125 static char * snat_out2in_error_strings[] = {
126 #define _(sym,string) string,
127 foreach_snat_out2in_error
132 SNAT_OUT2IN_NEXT_DROP,
133 SNAT_OUT2IN_NEXT_LOOKUP,
134 SNAT_OUT2IN_NEXT_ICMP_ERROR,
135 SNAT_OUT2IN_NEXT_REASS,
136 SNAT_OUT2IN_NEXT_IN2OUT,
138 } snat_out2in_next_t;
141 * @brief Create session for static mapping.
143 * Create NAT session initiated by host from external network with static
146 * @param sm NAT main.
147 * @param b0 Vlib buffer.
148 * @param in2out In2out NAT44 session key.
149 * @param out2in Out2in NAT44 session key.
150 * @param node Vlib node.
152 * @returns SNAT session if successfully created otherwise 0.
154 static inline snat_session_t *
155 create_session_for_static_mapping (snat_main_t *sm,
157 snat_session_key_t in2out,
158 snat_session_key_t out2in,
159 vlib_node_runtime_t * node,
164 clib_bihash_kv_8_8_t kv0;
168 if (PREDICT_FALSE (maximum_sessions_exceeded(sm, thread_index)))
170 b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_SESSIONS_EXCEEDED];
174 ip0 = vlib_buffer_get_current (b0);
175 udp0 = ip4_next_header (ip0);
177 u = nat_user_get_or_create (sm, &in2out.addr, in2out.fib_index, thread_index);
180 clib_warning ("create NAT user failed");
184 s = nat_session_alloc_or_recycle (sm, u, thread_index);
187 clib_warning ("create NAT session failed");
191 s->outside_address_index = ~0;
192 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
193 s->ext_host_addr.as_u32 = ip0->src_address.as_u32;
194 s->ext_host_port = udp0->src_port;
195 user_session_increment (sm, u, 1 /* static */);
198 s->in2out.protocol = out2in.protocol;
200 /* Add to translation hashes */
201 kv0.key = s->in2out.as_u64;
202 kv0.value = s - sm->per_thread_data[thread_index].sessions;
203 if (clib_bihash_add_del_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
205 clib_warning ("in2out key add failed");
207 kv0.key = s->out2in.as_u64;
209 if (clib_bihash_add_del_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
211 clib_warning ("out2in key add failed");
214 snat_ipfix_logging_nat44_ses_create(s->in2out.addr.as_u32,
215 s->out2in.addr.as_u32,
219 s->in2out.fib_index);
224 snat_out2in_error_t icmp_get_key(ip4_header_t *ip0,
225 snat_session_key_t *p_key0)
227 icmp46_header_t *icmp0;
228 snat_session_key_t key0;
229 icmp_echo_header_t *echo0, *inner_echo0 = 0;
230 ip4_header_t *inner_ip0;
232 icmp46_header_t *inner_icmp0;
234 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
235 echo0 = (icmp_echo_header_t *)(icmp0+1);
237 if (!icmp_is_error_message (icmp0))
239 key0.protocol = SNAT_PROTOCOL_ICMP;
240 key0.addr = ip0->dst_address;
241 key0.port = echo0->identifier;
245 inner_ip0 = (ip4_header_t *)(echo0+1);
246 l4_header = ip4_next_header (inner_ip0);
247 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
248 key0.addr = inner_ip0->src_address;
249 switch (key0.protocol)
251 case SNAT_PROTOCOL_ICMP:
252 inner_icmp0 = (icmp46_header_t*)l4_header;
253 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
254 key0.port = inner_echo0->identifier;
256 case SNAT_PROTOCOL_UDP:
257 case SNAT_PROTOCOL_TCP:
258 key0.port = ((tcp_udp_header_t*)l4_header)->src_port;
261 return SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL;
265 return -1; /* success */
268 static_always_inline int
269 icmp_get_ed_key(ip4_header_t *ip0, nat_ed_ses_key_t *p_key0)
271 icmp46_header_t *icmp0;
272 nat_ed_ses_key_t key0;
273 icmp_echo_header_t *echo0, *inner_echo0 = 0;
274 ip4_header_t *inner_ip0;
276 icmp46_header_t *inner_icmp0;
278 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
279 echo0 = (icmp_echo_header_t *)(icmp0+1);
281 if (!icmp_is_error_message (icmp0))
283 key0.proto = IP_PROTOCOL_ICMP;
284 key0.l_addr = ip0->dst_address;
285 key0.r_addr = ip0->src_address;
286 key0.l_port = key0.r_port = echo0->identifier;
290 inner_ip0 = (ip4_header_t *)(echo0+1);
291 l4_header = ip4_next_header (inner_ip0);
292 key0.proto = inner_ip0->protocol;
293 key0.l_addr = inner_ip0->src_address;
294 key0.r_addr = inner_ip0->dst_address;
295 switch (ip_proto_to_snat_proto (inner_ip0->protocol))
297 case SNAT_PROTOCOL_ICMP:
298 inner_icmp0 = (icmp46_header_t*)l4_header;
299 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
300 key0.l_port = key0.r_port = inner_echo0->identifier;
302 case SNAT_PROTOCOL_UDP:
303 case SNAT_PROTOCOL_TCP:
304 key0.l_port = ((tcp_udp_header_t*)l4_header)->src_port;
305 key0.r_port = ((tcp_udp_header_t*)l4_header)->dst_port;
316 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u32 proto, u16 src_port,
319 snat_session_key_t key;
320 clib_bihash_kv_8_8_t kv, value;
322 key.addr = ip->src_address;
324 key.protocol = proto;
325 key.fib_index = sm->inside_fib_index;
328 if (!clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out, &kv,
336 create_bypass_for_fwd(snat_main_t * sm, ip4_header_t * ip)
338 nat_ed_ses_key_t key;
339 clib_bihash_kv_16_8_t kv;
342 if (ip->protocol == IP_PROTOCOL_ICMP)
344 if (icmp_get_ed_key (ip, &key))
347 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
349 udp = ip4_next_header(ip);
350 key.r_addr = ip->src_address;
351 key.l_addr = ip->dst_address;
352 key.proto = ip->protocol;
353 key.l_port = udp->dst_port;
354 key.r_port = udp->src_port;
358 key.r_addr = ip->src_address;
359 key.l_addr = ip->dst_address;
360 key.proto = ip->protocol;
361 key.l_port = key.r_port = 0;
364 kv.key[0] = key.as_u64[0];
365 kv.key[1] = key.as_u64[1];
368 if (clib_bihash_add_del_16_8 (&sm->in2out_ed, &kv, 1))
369 clib_warning ("in2out_ed key add failed");
373 * Get address and port values to be used for ICMP packet translation
374 * and create session if needed
376 * @param[in,out] sm NAT main
377 * @param[in,out] node NAT node runtime
378 * @param[in] thread_index thread index
379 * @param[in,out] b0 buffer containing packet to be translated
380 * @param[out] p_proto protocol used for matching
381 * @param[out] p_value address and port after NAT translation
382 * @param[out] p_dont_translate if packet should not be translated
383 * @param d optional parameter
384 * @param e optional parameter
386 u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
387 u32 thread_index, vlib_buffer_t *b0,
388 ip4_header_t *ip0, u8 *p_proto,
389 snat_session_key_t *p_value,
390 u8 *p_dont_translate, void *d, void *e)
392 icmp46_header_t *icmp0;
395 snat_session_key_t key0;
396 snat_session_key_t sm0;
397 snat_session_t *s0 = 0;
398 u8 dont_translate = 0;
399 clib_bihash_kv_8_8_t kv0, value0;
404 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
405 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
406 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
410 err = icmp_get_key (ip0, &key0);
413 b0->error = node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
414 next0 = SNAT_OUT2IN_NEXT_DROP;
417 key0.fib_index = rx_fib_index0;
419 kv0.key = key0.as_u64;
421 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
424 /* Try to match static mapping by external address and port,
425 destination address and port in packet */
426 if (snat_static_mapping_match(sm, key0, &sm0, 1, &is_addr_only, 0))
428 if (!sm->forwarding_enabled)
430 /* Don't NAT packet aimed at the intfc address */
431 if (PREDICT_FALSE(is_interface_addr(sm, node, sw_if_index0,
432 ip0->dst_address.as_u32)))
437 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
438 next0 = SNAT_OUT2IN_NEXT_DROP;
444 if (next_src_nat(sm, ip0, key0.protocol, key0.port, thread_index))
446 next0 = SNAT_OUT2IN_NEXT_IN2OUT;
449 create_bypass_for_fwd(sm, ip0);
454 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_reply &&
455 (icmp0->type != ICMP4_echo_request || !is_addr_only)))
457 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
458 next0 = SNAT_OUT2IN_NEXT_DROP;
462 /* Create session initiated by host from external network */
463 s0 = create_session_for_static_mapping(sm, b0, sm0, key0,
468 next0 = SNAT_OUT2IN_NEXT_DROP;
474 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_reply &&
475 icmp0->type != ICMP4_echo_request &&
476 !icmp_is_error_message (icmp0)))
478 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
479 next0 = SNAT_OUT2IN_NEXT_DROP;
483 if (PREDICT_FALSE (value0.value == ~0ULL))
485 nat_ed_ses_key_t key;
486 clib_bihash_kv_16_8_t s_kv, s_value;
490 if (icmp_get_ed_key (ip0, &key))
492 b0->error = node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
493 next0 = SNAT_OUT2IN_NEXT_DROP;
496 key.fib_index = rx_fib_index0;
497 s_kv.key[0] = key.as_u64[0];
498 s_kv.key[1] = key.as_u64[1];
499 if (!clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
500 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
504 next0 = SNAT_OUT2IN_NEXT_DROP;
509 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
514 *p_proto = key0.protocol;
516 *p_value = s0->in2out;
517 *p_dont_translate = dont_translate;
519 *(snat_session_t**)d = s0;
524 * Get address and port values to be used for ICMP packet translation
526 * @param[in] sm NAT main
527 * @param[in,out] node NAT node runtime
528 * @param[in] thread_index thread index
529 * @param[in,out] b0 buffer containing packet to be translated
530 * @param[out] p_proto protocol used for matching
531 * @param[out] p_value address and port after NAT translation
532 * @param[out] p_dont_translate if packet should not be translated
533 * @param d optional parameter
534 * @param e optional parameter
536 u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node,
537 u32 thread_index, vlib_buffer_t *b0,
538 ip4_header_t *ip0, u8 *p_proto,
539 snat_session_key_t *p_value,
540 u8 *p_dont_translate, void *d, void *e)
542 icmp46_header_t *icmp0;
545 snat_session_key_t key0;
546 snat_session_key_t sm0;
547 u8 dont_translate = 0;
552 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
553 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
554 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
556 err = icmp_get_key (ip0, &key0);
559 b0->error = node->errors[err];
560 next0 = SNAT_OUT2IN_NEXT_DROP;
563 key0.fib_index = rx_fib_index0;
565 if (snat_static_mapping_match(sm, key0, &sm0, 1, &is_addr_only, 0))
567 /* Don't NAT packet aimed at the intfc address */
568 if (is_interface_addr(sm, node, sw_if_index0, ip0->dst_address.as_u32))
573 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
574 next0 = SNAT_OUT2IN_NEXT_DROP;
578 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_reply &&
579 (icmp0->type != ICMP4_echo_request || !is_addr_only) &&
580 !icmp_is_error_message (icmp0)))
582 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
583 next0 = SNAT_OUT2IN_NEXT_DROP;
590 *p_proto = key0.protocol;
591 *p_dont_translate = dont_translate;
595 static inline u32 icmp_out2in (snat_main_t *sm,
598 icmp46_header_t * icmp0,
601 vlib_node_runtime_t * node,
607 snat_session_key_t sm0;
609 icmp_echo_header_t *echo0, *inner_echo0 = 0;
610 ip4_header_t *inner_ip0 = 0;
612 icmp46_header_t *inner_icmp0;
614 u32 new_addr0, old_addr0;
615 u16 old_id0, new_id0;
620 echo0 = (icmp_echo_header_t *)(icmp0+1);
622 next0_tmp = sm->icmp_match_out2in_cb(sm, node, thread_index, b0, ip0,
623 &protocol, &sm0, &dont_translate, d, e);
626 if (next0 == SNAT_OUT2IN_NEXT_DROP || dont_translate)
629 sum0 = ip_incremental_checksum (0, icmp0,
630 ntohs(ip0->length) - ip4_header_bytes (ip0));
631 checksum0 = ~ip_csum_fold (sum0);
632 if (checksum0 != 0 && checksum0 != 0xffff)
634 next0 = SNAT_OUT2IN_NEXT_DROP;
638 old_addr0 = ip0->dst_address.as_u32;
639 new_addr0 = ip0->dst_address.as_u32 = sm0.addr.as_u32;
640 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
642 sum0 = ip0->checksum;
643 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
644 dst_address /* changed member */);
645 ip0->checksum = ip_csum_fold (sum0);
647 if (icmp0->checksum == 0)
648 icmp0->checksum = 0xffff;
650 if (!icmp_is_error_message (icmp0))
653 if (PREDICT_FALSE(new_id0 != echo0->identifier))
655 old_id0 = echo0->identifier;
657 echo0->identifier = new_id0;
659 sum0 = icmp0->checksum;
660 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
661 identifier /* changed member */);
662 icmp0->checksum = ip_csum_fold (sum0);
667 inner_ip0 = (ip4_header_t *)(echo0+1);
668 l4_header = ip4_next_header (inner_ip0);
670 if (!ip4_header_checksum_is_valid (inner_ip0))
672 next0 = SNAT_OUT2IN_NEXT_DROP;
676 old_addr0 = inner_ip0->src_address.as_u32;
677 inner_ip0->src_address = sm0.addr;
678 new_addr0 = inner_ip0->src_address.as_u32;
680 sum0 = icmp0->checksum;
681 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
682 src_address /* changed member */);
683 icmp0->checksum = ip_csum_fold (sum0);
687 case SNAT_PROTOCOL_ICMP:
688 inner_icmp0 = (icmp46_header_t*)l4_header;
689 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
691 old_id0 = inner_echo0->identifier;
693 inner_echo0->identifier = new_id0;
695 sum0 = icmp0->checksum;
696 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
698 icmp0->checksum = ip_csum_fold (sum0);
700 case SNAT_PROTOCOL_UDP:
701 case SNAT_PROTOCOL_TCP:
702 old_id0 = ((tcp_udp_header_t*)l4_header)->src_port;
704 ((tcp_udp_header_t*)l4_header)->src_port = new_id0;
706 sum0 = icmp0->checksum;
707 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
709 icmp0->checksum = ip_csum_fold (sum0);
721 static inline u32 icmp_out2in_slow_path (snat_main_t *sm,
724 icmp46_header_t * icmp0,
727 vlib_node_runtime_t * node,
730 snat_session_t ** p_s0)
732 next0 = icmp_out2in(sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
733 next0, thread_index, p_s0, 0);
734 snat_session_t * s0 = *p_s0;
735 if (PREDICT_TRUE(next0 != SNAT_OUT2IN_NEXT_DROP && s0))
738 s0->last_heard = now;
740 s0->total_bytes += vlib_buffer_length_in_chain (sm->vlib_main, b0);
741 /* Per-user LRU list maintenance */
742 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
744 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
745 s0->per_user_list_head_index,
751 static snat_session_t *
752 snat_out2in_unknown_proto (snat_main_t *sm,
759 vlib_node_runtime_t * node)
761 clib_bihash_kv_8_8_t kv, value;
762 clib_bihash_kv_16_8_t s_kv, s_value;
763 snat_static_mapping_t *m;
764 snat_session_key_t m_key;
765 u32 old_addr, new_addr;
767 nat_ed_ses_key_t key;
769 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
772 old_addr = ip->dst_address.as_u32;
774 key.l_addr = ip->dst_address;
775 key.r_addr = ip->src_address;
776 key.fib_index = rx_fib_index;
777 key.proto = ip->protocol;
780 s_kv.key[0] = key.as_u64[0];
781 s_kv.key[1] = key.as_u64[1];
783 if (!clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
785 s = pool_elt_at_index (tsm->sessions, s_value.value);
786 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
790 if (PREDICT_FALSE (maximum_sessions_exceeded(sm, thread_index)))
792 b->error = node->errors[SNAT_OUT2IN_ERROR_MAX_SESSIONS_EXCEEDED];
796 m_key.addr = ip->dst_address;
799 m_key.fib_index = rx_fib_index;
800 kv.key = m_key.as_u64;
801 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
803 b->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
807 m = pool_elt_at_index (sm->static_mappings, value.value);
809 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
811 u = nat_user_get_or_create (sm, &ip->src_address, m->fib_index,
815 clib_warning ("create NAT user failed");
819 /* Create a new session */
820 s = nat_session_alloc_or_recycle (sm, u, thread_index);
823 clib_warning ("create NAT session failed");
827 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
828 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
829 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
830 s->outside_address_index = ~0;
831 s->out2in.addr.as_u32 = old_addr;
832 s->out2in.fib_index = rx_fib_index;
833 s->in2out.addr.as_u32 = new_addr;
834 s->in2out.fib_index = m->fib_index;
835 s->in2out.port = s->out2in.port = ip->protocol;
836 user_session_increment (sm, u, 1 /* static */);
838 /* Add to lookup tables */
839 s_kv.value = s - tsm->sessions;
840 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &s_kv, 1))
841 clib_warning ("out2in key add failed");
843 key.l_addr = ip->dst_address;
844 key.fib_index = m->fib_index;
845 s_kv.key[0] = key.as_u64[0];
846 s_kv.key[1] = key.as_u64[1];
847 if (clib_bihash_add_del_16_8 (&sm->in2out_ed, &s_kv, 1))
848 clib_warning ("in2out key add failed");
851 /* Update IP checksum */
853 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
854 ip->checksum = ip_csum_fold (sum);
856 vnet_buffer(b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
861 s->total_bytes += vlib_buffer_length_in_chain (vm, b);
862 /* Per-user LRU list maintenance */
863 clib_dlist_remove (tsm->list_pool, s->per_user_index);
864 clib_dlist_addtail (tsm->list_pool, s->per_user_list_head_index,
870 static snat_session_t *
871 snat_out2in_lb (snat_main_t *sm,
878 vlib_node_runtime_t * node)
880 nat_ed_ses_key_t key;
881 clib_bihash_kv_16_8_t s_kv, s_value;
882 udp_header_t *udp = ip4_next_header (ip);
883 tcp_header_t *tcp = (tcp_header_t *) udp;
884 snat_session_t *s = 0;
885 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
886 snat_session_key_t e_key, l_key;
887 u32 old_addr, new_addr;
888 u32 proto = ip_proto_to_snat_proto (ip->protocol);
889 u16 new_port, old_port;
893 snat_session_key_t eh_key;
896 old_addr = ip->dst_address.as_u32;
898 key.l_addr = ip->dst_address;
899 key.r_addr = ip->src_address;
900 key.fib_index = rx_fib_index;
901 key.proto = ip->protocol;
902 key.r_port = udp->src_port;
903 key.l_port = udp->dst_port;
904 s_kv.key[0] = key.as_u64[0];
905 s_kv.key[1] = key.as_u64[1];
907 if (!clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
909 s = pool_elt_at_index (tsm->sessions, s_value.value);
913 if (PREDICT_FALSE (maximum_sessions_exceeded(sm, thread_index)))
915 b->error = node->errors[SNAT_OUT2IN_ERROR_MAX_SESSIONS_EXCEEDED];
919 e_key.addr = ip->dst_address;
920 e_key.port = udp->dst_port;
921 e_key.protocol = proto;
922 e_key.fib_index = rx_fib_index;
923 if (snat_static_mapping_match(sm, e_key, &l_key, 1, 0, &twice_nat))
926 u = nat_user_get_or_create (sm, &l_key.addr, l_key.fib_index,
930 clib_warning ("create NAT user failed");
934 s = nat_session_alloc_or_recycle (sm, u, thread_index);
937 clib_warning ("create NAT session failed");
941 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
942 s->ext_host_port = udp->src_port;
943 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
944 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
945 s->outside_address_index = ~0;
948 user_session_increment (sm, u, 1 /* static */);
950 /* Add to lookup tables */
951 s_kv.value = s - tsm->sessions;
952 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &s_kv, 1))
953 clib_warning ("out2in-ed key add failed");
957 eh_key.protocol = proto;
958 if (snat_alloc_outside_address_and_port (sm->twice_nat_addresses, 0,
959 thread_index, &eh_key,
962 sm->per_thread_data[thread_index].snat_thread_index))
964 b->error = node->errors[SNAT_OUT2IN_ERROR_OUT_OF_PORTS];
967 key.r_addr.as_u32 = s->ext_host_nat_addr.as_u32 = eh_key.addr.as_u32;
968 key.r_port = s->ext_host_nat_port = eh_key.port;
969 s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
971 key.l_addr = l_key.addr;
972 key.fib_index = l_key.fib_index;
973 key.l_port = l_key.port;
974 s_kv.key[0] = key.as_u64[0];
975 s_kv.key[1] = key.as_u64[1];
976 if (clib_bihash_add_del_16_8 (&sm->in2out_ed, &s_kv, 1))
977 clib_warning ("in2out-ed key add failed");
980 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
982 /* Update IP checksum */
984 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
985 if (is_twice_nat_session (s))
986 sum = ip_csum_update (sum, ip->src_address.as_u32,
987 s->ext_host_nat_addr.as_u32, ip4_header_t,
989 ip->checksum = ip_csum_fold (sum);
991 if (PREDICT_TRUE(proto == SNAT_PROTOCOL_TCP))
993 old_port = tcp->dst_port;
994 tcp->dst_port = s->in2out.port;
995 new_port = tcp->dst_port;
998 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
999 sum = ip_csum_update (sum, old_port, new_port, ip4_header_t, length);
1000 if (is_twice_nat_session (s))
1002 sum = ip_csum_update (sum, ip->src_address.as_u32,
1003 s->ext_host_nat_addr.as_u32, ip4_header_t,
1005 sum = ip_csum_update (sum, tcp->src_port, s->ext_host_nat_port,
1006 ip4_header_t, length);
1007 tcp->src_port = s->ext_host_nat_port;
1008 ip->src_address.as_u32 = s->ext_host_nat_addr.as_u32;
1010 tcp->checksum = ip_csum_fold(sum);
1014 udp->dst_port = s->in2out.port;
1015 if (is_twice_nat_session (s))
1017 udp->src_port = s->ext_host_nat_port;
1018 ip->src_address.as_u32 = s->ext_host_nat_addr.as_u32;
1023 vnet_buffer(b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
1026 s->last_heard = now;
1028 s->total_bytes += vlib_buffer_length_in_chain (vm, b);
1029 /* Per-user LRU list maintenance */
1030 clib_dlist_remove (tsm->list_pool, s->per_user_index);
1031 clib_dlist_addtail (tsm->list_pool, s->per_user_list_head_index,
1038 snat_out2in_node_fn (vlib_main_t * vm,
1039 vlib_node_runtime_t * node,
1040 vlib_frame_t * frame)
1042 u32 n_left_from, * from, * to_next;
1043 snat_out2in_next_t next_index;
1044 u32 pkts_processed = 0;
1045 snat_main_t * sm = &snat_main;
1046 f64 now = vlib_time_now (vm);
1047 u32 thread_index = vlib_get_thread_index ();
1049 from = vlib_frame_vector_args (frame);
1050 n_left_from = frame->n_vectors;
1051 next_index = node->cached_next_index;
1053 while (n_left_from > 0)
1057 vlib_get_next_frame (vm, node, next_index,
1058 to_next, n_left_to_next);
1060 while (n_left_from >= 4 && n_left_to_next >= 2)
1063 vlib_buffer_t * b0, * b1;
1064 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1065 u32 next1 = SNAT_OUT2IN_NEXT_LOOKUP;
1066 u32 sw_if_index0, sw_if_index1;
1067 ip4_header_t * ip0, *ip1;
1068 ip_csum_t sum0, sum1;
1069 u32 new_addr0, old_addr0;
1070 u16 new_port0, old_port0;
1071 u32 new_addr1, old_addr1;
1072 u16 new_port1, old_port1;
1073 udp_header_t * udp0, * udp1;
1074 tcp_header_t * tcp0, * tcp1;
1075 icmp46_header_t * icmp0, * icmp1;
1076 snat_session_key_t key0, key1, sm0, sm1;
1077 u32 rx_fib_index0, rx_fib_index1;
1079 snat_session_t * s0 = 0, * s1 = 0;
1080 clib_bihash_kv_8_8_t kv0, kv1, value0, value1;
1082 /* Prefetch next iteration. */
1084 vlib_buffer_t * p2, * p3;
1086 p2 = vlib_get_buffer (vm, from[2]);
1087 p3 = vlib_get_buffer (vm, from[3]);
1089 vlib_prefetch_buffer_header (p2, LOAD);
1090 vlib_prefetch_buffer_header (p3, LOAD);
1092 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
1093 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
1096 /* speculatively enqueue b0 and b1 to the current next frame */
1097 to_next[0] = bi0 = from[0];
1098 to_next[1] = bi1 = from[1];
1102 n_left_to_next -= 2;
1104 b0 = vlib_get_buffer (vm, bi0);
1105 b1 = vlib_get_buffer (vm, bi1);
1107 vnet_buffer (b0)->snat.flags = 0;
1108 vnet_buffer (b1)->snat.flags = 0;
1110 ip0 = vlib_buffer_get_current (b0);
1111 udp0 = ip4_next_header (ip0);
1112 tcp0 = (tcp_header_t *) udp0;
1113 icmp0 = (icmp46_header_t *) udp0;
1115 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1116 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1119 if (PREDICT_FALSE(ip0->ttl == 1))
1121 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1122 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1123 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1125 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1129 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1131 if (PREDICT_FALSE (proto0 == ~0))
1133 s0 = snat_out2in_unknown_proto(sm, b0, ip0, rx_fib_index0,
1134 thread_index, now, vm, node);
1135 if (!sm->forwarding_enabled)
1137 next0 = SNAT_OUT2IN_NEXT_DROP;
1141 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1143 next0 = icmp_out2in_slow_path
1144 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1145 next0, now, thread_index, &s0);
1149 if (PREDICT_FALSE (ip4_is_fragment (ip0)))
1151 next0 = SNAT_OUT2IN_NEXT_REASS;
1155 key0.addr = ip0->dst_address;
1156 key0.port = udp0->dst_port;
1157 key0.protocol = proto0;
1158 key0.fib_index = rx_fib_index0;
1160 kv0.key = key0.as_u64;
1162 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in,
1165 /* Try to match static mapping by external address and port,
1166 destination address and port in packet */
1167 if (snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0))
1169 if (!sm->forwarding_enabled)
1171 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1173 * Send DHCP packets to the ipv4 stack, or we won't
1174 * be able to use dhcp client on the outside interface
1176 if (PREDICT_TRUE (proto0 != SNAT_PROTOCOL_UDP
1178 != clib_host_to_net_u16(UDP_DST_PORT_dhcp_to_client))))
1179 next0 = SNAT_OUT2IN_NEXT_DROP;
1182 (vnet_buffer (b0)->sw_if_index[VLIB_RX],
1188 if (next_src_nat(sm, ip0, proto0, udp0->src_port, thread_index))
1190 next0 = SNAT_OUT2IN_NEXT_IN2OUT;
1193 create_bypass_for_fwd(sm, ip0);
1198 /* Create session initiated by host from external network */
1199 s0 = create_session_for_static_mapping(sm, b0, sm0, key0, node,
1203 next0 = SNAT_OUT2IN_NEXT_DROP;
1209 if (PREDICT_FALSE (value0.value == ~0ULL))
1211 s0 = snat_out2in_lb(sm, b0, ip0, rx_fib_index0, thread_index,
1214 next0 = SNAT_OUT2IN_NEXT_DROP;
1219 s0 = pool_elt_at_index (
1220 sm->per_thread_data[thread_index].sessions,
1225 old_addr0 = ip0->dst_address.as_u32;
1226 ip0->dst_address = s0->in2out.addr;
1227 new_addr0 = ip0->dst_address.as_u32;
1228 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1230 sum0 = ip0->checksum;
1231 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1233 dst_address /* changed member */);
1234 ip0->checksum = ip_csum_fold (sum0);
1236 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1238 old_port0 = tcp0->dst_port;
1239 tcp0->dst_port = s0->in2out.port;
1240 new_port0 = tcp0->dst_port;
1242 sum0 = tcp0->checksum;
1243 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1245 dst_address /* changed member */);
1247 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1248 ip4_header_t /* cheat */,
1249 length /* changed member */);
1250 tcp0->checksum = ip_csum_fold(sum0);
1254 old_port0 = udp0->dst_port;
1255 udp0->dst_port = s0->in2out.port;
1260 s0->last_heard = now;
1262 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
1263 /* Per-user LRU list maintenance */
1264 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1265 s0->per_user_index);
1266 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1267 s0->per_user_list_head_index,
1268 s0->per_user_index);
1271 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1272 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1274 snat_out2in_trace_t *t =
1275 vlib_add_trace (vm, node, b0, sizeof (*t));
1276 t->sw_if_index = sw_if_index0;
1277 t->next_index = next0;
1278 t->session_index = ~0;
1280 t->session_index = s0 - sm->per_thread_data[thread_index].sessions;
1283 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
1286 ip1 = vlib_buffer_get_current (b1);
1287 udp1 = ip4_next_header (ip1);
1288 tcp1 = (tcp_header_t *) udp1;
1289 icmp1 = (icmp46_header_t *) udp1;
1291 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
1292 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1295 if (PREDICT_FALSE(ip1->ttl == 1))
1297 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1298 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1299 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1301 next1 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1305 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1307 if (PREDICT_FALSE (proto1 == ~0))
1309 s1 = snat_out2in_unknown_proto(sm, b1, ip1, rx_fib_index1,
1310 thread_index, now, vm, node);
1311 if (!sm->forwarding_enabled)
1313 next1 = SNAT_OUT2IN_NEXT_DROP;
1317 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1319 next1 = icmp_out2in_slow_path
1320 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1321 next1, now, thread_index, &s1);
1325 if (PREDICT_FALSE (ip4_is_fragment (ip1)))
1327 next1 = SNAT_OUT2IN_NEXT_REASS;
1331 key1.addr = ip1->dst_address;
1332 key1.port = udp1->dst_port;
1333 key1.protocol = proto1;
1334 key1.fib_index = rx_fib_index1;
1336 kv1.key = key1.as_u64;
1338 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in,
1341 /* Try to match static mapping by external address and port,
1342 destination address and port in packet */
1343 if (snat_static_mapping_match(sm, key1, &sm1, 1, 0, 0))
1345 if (!sm->forwarding_enabled)
1347 b1->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1349 * Send DHCP packets to the ipv4 stack, or we won't
1350 * be able to use dhcp client on the outside interface
1352 if (PREDICT_TRUE (proto1 != SNAT_PROTOCOL_UDP
1354 != clib_host_to_net_u16(UDP_DST_PORT_dhcp_to_client))))
1355 next1 = SNAT_OUT2IN_NEXT_DROP;
1358 (vnet_buffer (b1)->sw_if_index[VLIB_RX],
1364 if (next_src_nat(sm, ip1, proto1, udp1->src_port, thread_index))
1366 next1 = SNAT_OUT2IN_NEXT_IN2OUT;
1369 create_bypass_for_fwd(sm, ip1);
1374 /* Create session initiated by host from external network */
1375 s1 = create_session_for_static_mapping(sm, b1, sm1, key1, node,
1379 next1 = SNAT_OUT2IN_NEXT_DROP;
1385 if (PREDICT_FALSE (value1.value == ~0ULL))
1387 s1 = snat_out2in_lb(sm, b1, ip1, rx_fib_index1, thread_index,
1390 next1 = SNAT_OUT2IN_NEXT_DROP;
1395 s1 = pool_elt_at_index (
1396 sm->per_thread_data[thread_index].sessions,
1401 old_addr1 = ip1->dst_address.as_u32;
1402 ip1->dst_address = s1->in2out.addr;
1403 new_addr1 = ip1->dst_address.as_u32;
1404 vnet_buffer(b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1406 sum1 = ip1->checksum;
1407 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1409 dst_address /* changed member */);
1410 ip1->checksum = ip_csum_fold (sum1);
1412 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
1414 old_port1 = tcp1->dst_port;
1415 tcp1->dst_port = s1->in2out.port;
1416 new_port1 = tcp1->dst_port;
1418 sum1 = tcp1->checksum;
1419 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1421 dst_address /* changed member */);
1423 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1424 ip4_header_t /* cheat */,
1425 length /* changed member */);
1426 tcp1->checksum = ip_csum_fold(sum1);
1430 old_port1 = udp1->dst_port;
1431 udp1->dst_port = s1->in2out.port;
1436 s1->last_heard = now;
1438 s1->total_bytes += vlib_buffer_length_in_chain (vm, b1);
1439 /* Per-user LRU list maintenance */
1440 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1441 s1->per_user_index);
1442 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1443 s1->per_user_list_head_index,
1444 s1->per_user_index);
1447 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1448 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1450 snat_out2in_trace_t *t =
1451 vlib_add_trace (vm, node, b1, sizeof (*t));
1452 t->sw_if_index = sw_if_index1;
1453 t->next_index = next1;
1454 t->session_index = ~0;
1456 t->session_index = s1 - sm->per_thread_data[thread_index].sessions;
1459 pkts_processed += next1 != SNAT_OUT2IN_NEXT_DROP;
1461 /* verify speculative enqueues, maybe switch current next frame */
1462 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1463 to_next, n_left_to_next,
1464 bi0, bi1, next0, next1);
1467 while (n_left_from > 0 && n_left_to_next > 0)
1471 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1475 u32 new_addr0, old_addr0;
1476 u16 new_port0, old_port0;
1477 udp_header_t * udp0;
1478 tcp_header_t * tcp0;
1479 icmp46_header_t * icmp0;
1480 snat_session_key_t key0, sm0;
1483 snat_session_t * s0 = 0;
1484 clib_bihash_kv_8_8_t kv0, value0;
1486 /* speculatively enqueue b0 to the current next frame */
1492 n_left_to_next -= 1;
1494 b0 = vlib_get_buffer (vm, bi0);
1496 vnet_buffer (b0)->snat.flags = 0;
1498 ip0 = vlib_buffer_get_current (b0);
1499 udp0 = ip4_next_header (ip0);
1500 tcp0 = (tcp_header_t *) udp0;
1501 icmp0 = (icmp46_header_t *) udp0;
1503 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1504 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1507 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1509 if (PREDICT_FALSE (proto0 == ~0))
1511 s0 = snat_out2in_unknown_proto(sm, b0, ip0, rx_fib_index0,
1512 thread_index, now, vm, node);
1513 if (!sm->forwarding_enabled)
1515 next0 = SNAT_OUT2IN_NEXT_DROP;
1519 if (PREDICT_FALSE(ip0->ttl == 1))
1521 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1522 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1523 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1525 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1529 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1531 next0 = icmp_out2in_slow_path
1532 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1533 next0, now, thread_index, &s0);
1537 if (PREDICT_FALSE (ip4_is_fragment (ip0)))
1539 next0 = SNAT_OUT2IN_NEXT_REASS;
1543 key0.addr = ip0->dst_address;
1544 key0.port = udp0->dst_port;
1545 key0.protocol = proto0;
1546 key0.fib_index = rx_fib_index0;
1548 kv0.key = key0.as_u64;
1550 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in,
1553 /* Try to match static mapping by external address and port,
1554 destination address and port in packet */
1555 if (snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0))
1557 if (!sm->forwarding_enabled)
1559 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1561 * Send DHCP packets to the ipv4 stack, or we won't
1562 * be able to use dhcp client on the outside interface
1564 if (PREDICT_TRUE (proto0 != SNAT_PROTOCOL_UDP
1566 != clib_host_to_net_u16(UDP_DST_PORT_dhcp_to_client))))
1567 next0 = SNAT_OUT2IN_NEXT_DROP;
1570 (vnet_buffer (b0)->sw_if_index[VLIB_RX],
1576 if (next_src_nat(sm, ip0, proto0, udp0->src_port, thread_index))
1578 next0 = SNAT_OUT2IN_NEXT_IN2OUT;
1581 create_bypass_for_fwd(sm, ip0);
1586 /* Create session initiated by host from external network */
1587 s0 = create_session_for_static_mapping(sm, b0, sm0, key0, node,
1591 next0 = SNAT_OUT2IN_NEXT_DROP;
1597 if (PREDICT_FALSE (value0.value == ~0ULL))
1599 s0 = snat_out2in_lb(sm, b0, ip0, rx_fib_index0, thread_index,
1602 next0 = SNAT_OUT2IN_NEXT_DROP;
1607 s0 = pool_elt_at_index (
1608 sm->per_thread_data[thread_index].sessions,
1613 old_addr0 = ip0->dst_address.as_u32;
1614 ip0->dst_address = s0->in2out.addr;
1615 new_addr0 = ip0->dst_address.as_u32;
1616 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1618 sum0 = ip0->checksum;
1619 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1621 dst_address /* changed member */);
1622 ip0->checksum = ip_csum_fold (sum0);
1624 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1626 old_port0 = tcp0->dst_port;
1627 tcp0->dst_port = s0->in2out.port;
1628 new_port0 = tcp0->dst_port;
1630 sum0 = tcp0->checksum;
1631 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1633 dst_address /* changed member */);
1635 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1636 ip4_header_t /* cheat */,
1637 length /* changed member */);
1638 tcp0->checksum = ip_csum_fold(sum0);
1642 old_port0 = udp0->dst_port;
1643 udp0->dst_port = s0->in2out.port;
1648 s0->last_heard = now;
1650 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
1651 /* Per-user LRU list maintenance */
1652 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1653 s0->per_user_index);
1654 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1655 s0->per_user_list_head_index,
1656 s0->per_user_index);
1659 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1660 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1662 snat_out2in_trace_t *t =
1663 vlib_add_trace (vm, node, b0, sizeof (*t));
1664 t->sw_if_index = sw_if_index0;
1665 t->next_index = next0;
1666 t->session_index = ~0;
1668 t->session_index = s0 - sm->per_thread_data[thread_index].sessions;
1671 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
1673 /* verify speculative enqueue, maybe switch current next frame */
1674 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1675 to_next, n_left_to_next,
1679 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1682 vlib_node_increment_counter (vm, snat_out2in_node.index,
1683 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1685 return frame->n_vectors;
1688 VLIB_REGISTER_NODE (snat_out2in_node) = {
1689 .function = snat_out2in_node_fn,
1690 .name = "nat44-out2in",
1691 .vector_size = sizeof (u32),
1692 .format_trace = format_snat_out2in_trace,
1693 .type = VLIB_NODE_TYPE_INTERNAL,
1695 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1696 .error_strings = snat_out2in_error_strings,
1698 .runtime_data_bytes = sizeof (snat_runtime_t),
1700 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1702 /* edit / add dispositions here */
1704 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1705 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1706 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1707 [SNAT_OUT2IN_NEXT_REASS] = "nat44-out2in-reass",
1708 [SNAT_OUT2IN_NEXT_IN2OUT] = "nat44-in2out",
1711 VLIB_NODE_FUNCTION_MULTIARCH (snat_out2in_node, snat_out2in_node_fn);
1714 nat44_out2in_reass_node_fn (vlib_main_t * vm,
1715 vlib_node_runtime_t * node,
1716 vlib_frame_t * frame)
1718 u32 n_left_from, *from, *to_next;
1719 snat_out2in_next_t next_index;
1720 u32 pkts_processed = 0;
1721 snat_main_t *sm = &snat_main;
1722 f64 now = vlib_time_now (vm);
1723 u32 thread_index = vlib_get_thread_index ();
1724 snat_main_per_thread_data_t *per_thread_data =
1725 &sm->per_thread_data[thread_index];
1726 u32 *fragments_to_drop = 0;
1727 u32 *fragments_to_loopback = 0;
1729 from = vlib_frame_vector_args (frame);
1730 n_left_from = frame->n_vectors;
1731 next_index = node->cached_next_index;
1733 while (n_left_from > 0)
1737 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1739 while (n_left_from > 0 && n_left_to_next > 0)
1741 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1746 nat_reass_ip4_t *reass0;
1747 udp_header_t * udp0;
1748 tcp_header_t * tcp0;
1749 snat_session_key_t key0, sm0;
1750 clib_bihash_kv_8_8_t kv0, value0;
1751 snat_session_t * s0 = 0;
1752 u16 old_port0, new_port0;
1755 /* speculatively enqueue b0 to the current next frame */
1761 n_left_to_next -= 1;
1763 b0 = vlib_get_buffer (vm, bi0);
1764 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1766 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1767 rx_fib_index0 = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1770 if (PREDICT_FALSE (nat_reass_is_drop_frag(0)))
1772 next0 = SNAT_OUT2IN_NEXT_DROP;
1773 b0->error = node->errors[SNAT_OUT2IN_ERROR_DROP_FRAGMENT];
1777 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1778 udp0 = ip4_next_header (ip0);
1779 tcp0 = (tcp_header_t *) udp0;
1780 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1782 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1787 &fragments_to_drop);
1789 if (PREDICT_FALSE (!reass0))
1791 next0 = SNAT_OUT2IN_NEXT_DROP;
1792 b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_REASS];
1796 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1798 key0.addr = ip0->dst_address;
1799 key0.port = udp0->dst_port;
1800 key0.protocol = proto0;
1801 key0.fib_index = rx_fib_index0;
1802 kv0.key = key0.as_u64;
1804 if (clib_bihash_search_8_8 (&per_thread_data->out2in, &kv0, &value0))
1806 /* Try to match static mapping by external address and port,
1807 destination address and port in packet */
1808 if (snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0))
1810 if (!sm->forwarding_enabled)
1812 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1814 * Send DHCP packets to the ipv4 stack, or we won't
1815 * be able to use dhcp client on the outside interface
1817 if (PREDICT_TRUE (proto0 != SNAT_PROTOCOL_UDP
1819 != clib_host_to_net_u16(UDP_DST_PORT_dhcp_to_client))))
1820 next0 = SNAT_OUT2IN_NEXT_DROP;
1823 (vnet_buffer (b0)->sw_if_index[VLIB_RX],
1829 if (next_src_nat(sm, ip0, proto0, udp0->src_port, thread_index))
1831 next0 = SNAT_OUT2IN_NEXT_IN2OUT;
1834 create_bypass_for_fwd(sm, ip0);
1839 /* Create session initiated by host from external network */
1840 s0 = create_session_for_static_mapping(sm, b0, sm0, key0, node,
1844 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1845 next0 = SNAT_OUT2IN_NEXT_DROP;
1848 reass0->sess_index = s0 - per_thread_data->sessions;
1849 reass0->thread_index = thread_index;
1853 s0 = pool_elt_at_index (per_thread_data->sessions,
1855 reass0->sess_index = value0.value;
1857 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1861 if (PREDICT_FALSE (reass0->sess_index == (u32) ~0))
1863 if (nat_ip4_reass_add_fragment (reass0, bi0))
1865 b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_FRAG];
1866 next0 = SNAT_OUT2IN_NEXT_DROP;
1872 s0 = pool_elt_at_index (per_thread_data->sessions,
1873 reass0->sess_index);
1876 old_addr0 = ip0->dst_address.as_u32;
1877 ip0->dst_address = s0->in2out.addr;
1878 new_addr0 = ip0->dst_address.as_u32;
1879 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1881 sum0 = ip0->checksum;
1882 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1884 dst_address /* changed member */);
1885 ip0->checksum = ip_csum_fold (sum0);
1887 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1889 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1891 old_port0 = tcp0->dst_port;
1892 tcp0->dst_port = s0->in2out.port;
1893 new_port0 = tcp0->dst_port;
1895 sum0 = tcp0->checksum;
1896 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1898 dst_address /* changed member */);
1900 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1901 ip4_header_t /* cheat */,
1902 length /* changed member */);
1903 tcp0->checksum = ip_csum_fold(sum0);
1907 old_port0 = udp0->dst_port;
1908 udp0->dst_port = s0->in2out.port;
1914 s0->last_heard = now;
1916 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
1917 /* Per-user LRU list maintenance */
1918 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1919 s0->per_user_index);
1920 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1921 s0->per_user_list_head_index,
1922 s0->per_user_index);
1925 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1926 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1928 nat44_out2in_reass_trace_t *t =
1929 vlib_add_trace (vm, node, b0, sizeof (*t));
1930 t->cached = cached0;
1931 t->sw_if_index = sw_if_index0;
1932 t->next_index = next0;
1942 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
1944 /* verify speculative enqueue, maybe switch current next frame */
1945 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1946 to_next, n_left_to_next,
1950 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1952 from = vlib_frame_vector_args (frame);
1953 u32 len = vec_len (fragments_to_loopback);
1954 if (len <= VLIB_FRAME_SIZE)
1956 clib_memcpy (from, fragments_to_loopback, sizeof (u32) * len);
1958 vec_reset_length (fragments_to_loopback);
1963 fragments_to_loopback + (len - VLIB_FRAME_SIZE),
1964 sizeof (u32) * VLIB_FRAME_SIZE);
1965 n_left_from = VLIB_FRAME_SIZE;
1966 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1971 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1974 vlib_node_increment_counter (vm, nat44_out2in_reass_node.index,
1975 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1978 nat_send_all_to_node (vm, fragments_to_drop, node,
1979 &node->errors[SNAT_OUT2IN_ERROR_DROP_FRAGMENT],
1980 SNAT_OUT2IN_NEXT_DROP);
1982 vec_free (fragments_to_drop);
1983 vec_free (fragments_to_loopback);
1984 return frame->n_vectors;
1987 VLIB_REGISTER_NODE (nat44_out2in_reass_node) = {
1988 .function = nat44_out2in_reass_node_fn,
1989 .name = "nat44-out2in-reass",
1990 .vector_size = sizeof (u32),
1991 .format_trace = format_nat44_out2in_reass_trace,
1992 .type = VLIB_NODE_TYPE_INTERNAL,
1994 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1995 .error_strings = snat_out2in_error_strings,
1997 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1999 /* edit / add dispositions here */
2001 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
2002 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
2003 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2004 [SNAT_OUT2IN_NEXT_REASS] = "nat44-out2in-reass",
2005 [SNAT_OUT2IN_NEXT_IN2OUT] = "nat44-in2out",
2008 VLIB_NODE_FUNCTION_MULTIARCH (nat44_out2in_reass_node,
2009 nat44_out2in_reass_node_fn);
2011 /**************************/
2012 /*** deterministic mode ***/
2013 /**************************/
2015 snat_det_out2in_node_fn (vlib_main_t * vm,
2016 vlib_node_runtime_t * node,
2017 vlib_frame_t * frame)
2019 u32 n_left_from, * from, * to_next;
2020 snat_out2in_next_t next_index;
2021 u32 pkts_processed = 0;
2022 snat_main_t * sm = &snat_main;
2023 u32 thread_index = vlib_get_thread_index ();
2025 from = vlib_frame_vector_args (frame);
2026 n_left_from = frame->n_vectors;
2027 next_index = node->cached_next_index;
2029 while (n_left_from > 0)
2033 vlib_get_next_frame (vm, node, next_index,
2034 to_next, n_left_to_next);
2036 while (n_left_from >= 4 && n_left_to_next >= 2)
2039 vlib_buffer_t * b0, * b1;
2040 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
2041 u32 next1 = SNAT_OUT2IN_NEXT_LOOKUP;
2042 u32 sw_if_index0, sw_if_index1;
2043 ip4_header_t * ip0, * ip1;
2044 ip_csum_t sum0, sum1;
2045 ip4_address_t new_addr0, old_addr0, new_addr1, old_addr1;
2046 u16 new_port0, old_port0, old_port1, new_port1;
2047 udp_header_t * udp0, * udp1;
2048 tcp_header_t * tcp0, * tcp1;
2050 snat_det_out_key_t key0, key1;
2051 snat_det_map_t * dm0, * dm1;
2052 snat_det_session_t * ses0 = 0, * ses1 = 0;
2053 u32 rx_fib_index0, rx_fib_index1;
2054 icmp46_header_t * icmp0, * icmp1;
2056 /* Prefetch next iteration. */
2058 vlib_buffer_t * p2, * p3;
2060 p2 = vlib_get_buffer (vm, from[2]);
2061 p3 = vlib_get_buffer (vm, from[3]);
2063 vlib_prefetch_buffer_header (p2, LOAD);
2064 vlib_prefetch_buffer_header (p3, LOAD);
2066 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
2067 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
2070 /* speculatively enqueue b0 and b1 to the current next frame */
2071 to_next[0] = bi0 = from[0];
2072 to_next[1] = bi1 = from[1];
2076 n_left_to_next -= 2;
2078 b0 = vlib_get_buffer (vm, bi0);
2079 b1 = vlib_get_buffer (vm, bi1);
2081 ip0 = vlib_buffer_get_current (b0);
2082 udp0 = ip4_next_header (ip0);
2083 tcp0 = (tcp_header_t *) udp0;
2085 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2087 if (PREDICT_FALSE(ip0->ttl == 1))
2089 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2090 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2091 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2093 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
2097 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2099 if (PREDICT_FALSE(proto0 == SNAT_PROTOCOL_ICMP))
2101 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
2102 icmp0 = (icmp46_header_t *) udp0;
2104 next0 = icmp_out2in(sm, b0, ip0, icmp0, sw_if_index0,
2105 rx_fib_index0, node, next0, thread_index,
2110 key0.ext_host_addr = ip0->src_address;
2111 key0.ext_host_port = tcp0->src;
2112 key0.out_port = tcp0->dst;
2114 dm0 = snat_det_map_by_out(sm, &ip0->dst_address);
2115 if (PREDICT_FALSE(!dm0))
2117 clib_warning("unknown dst address: %U",
2118 format_ip4_address, &ip0->dst_address);
2119 next0 = SNAT_OUT2IN_NEXT_DROP;
2120 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
2124 snat_det_reverse(dm0, &ip0->dst_address,
2125 clib_net_to_host_u16(tcp0->dst), &new_addr0);
2127 ses0 = snat_det_get_ses_by_out (dm0, &new_addr0, key0.as_u64);
2128 if (PREDICT_FALSE(!ses0))
2130 clib_warning("no match src %U:%d dst %U:%d for user %U",
2131 format_ip4_address, &ip0->src_address,
2132 clib_net_to_host_u16 (tcp0->src),
2133 format_ip4_address, &ip0->dst_address,
2134 clib_net_to_host_u16 (tcp0->dst),
2135 format_ip4_address, &new_addr0);
2136 next0 = SNAT_OUT2IN_NEXT_DROP;
2137 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
2140 new_port0 = ses0->in_port;
2142 old_addr0 = ip0->dst_address;
2143 ip0->dst_address = new_addr0;
2144 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm->inside_fib_index;
2146 sum0 = ip0->checksum;
2147 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
2149 dst_address /* changed member */);
2150 ip0->checksum = ip_csum_fold (sum0);
2152 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2154 if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
2155 ses0->state = SNAT_SESSION_TCP_CLOSE_WAIT;
2156 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_LAST_ACK)
2157 snat_det_ses_close(dm0, ses0);
2159 old_port0 = tcp0->dst;
2160 tcp0->dst = new_port0;
2162 sum0 = tcp0->checksum;
2163 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
2165 dst_address /* changed member */);
2167 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2168 ip4_header_t /* cheat */,
2169 length /* changed member */);
2170 tcp0->checksum = ip_csum_fold(sum0);
2174 old_port0 = udp0->dst_port;
2175 udp0->dst_port = new_port0;
2181 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2182 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2184 snat_out2in_trace_t *t =
2185 vlib_add_trace (vm, node, b0, sizeof (*t));
2186 t->sw_if_index = sw_if_index0;
2187 t->next_index = next0;
2188 t->session_index = ~0;
2190 t->session_index = ses0 - dm0->sessions;
2193 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
2195 b1 = vlib_get_buffer (vm, bi1);
2197 ip1 = vlib_buffer_get_current (b1);
2198 udp1 = ip4_next_header (ip1);
2199 tcp1 = (tcp_header_t *) udp1;
2201 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
2203 if (PREDICT_FALSE(ip1->ttl == 1))
2205 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2206 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
2207 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2209 next1 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
2213 proto1 = ip_proto_to_snat_proto (ip1->protocol);
2215 if (PREDICT_FALSE(proto1 == SNAT_PROTOCOL_ICMP))
2217 rx_fib_index1 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index1);
2218 icmp1 = (icmp46_header_t *) udp1;
2220 next1 = icmp_out2in(sm, b1, ip1, icmp1, sw_if_index1,
2221 rx_fib_index1, node, next1, thread_index,
2226 key1.ext_host_addr = ip1->src_address;
2227 key1.ext_host_port = tcp1->src;
2228 key1.out_port = tcp1->dst;
2230 dm1 = snat_det_map_by_out(sm, &ip1->dst_address);
2231 if (PREDICT_FALSE(!dm1))
2233 clib_warning("unknown dst address: %U",
2234 format_ip4_address, &ip1->dst_address);
2235 next1 = SNAT_OUT2IN_NEXT_DROP;
2236 b1->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
2240 snat_det_reverse(dm1, &ip1->dst_address,
2241 clib_net_to_host_u16(tcp1->dst), &new_addr1);
2243 ses1 = snat_det_get_ses_by_out (dm1, &new_addr1, key1.as_u64);
2244 if (PREDICT_FALSE(!ses1))
2246 clib_warning("no match src %U:%d dst %U:%d for user %U",
2247 format_ip4_address, &ip1->src_address,
2248 clib_net_to_host_u16 (tcp1->src),
2249 format_ip4_address, &ip1->dst_address,
2250 clib_net_to_host_u16 (tcp1->dst),
2251 format_ip4_address, &new_addr1);
2252 next1 = SNAT_OUT2IN_NEXT_DROP;
2253 b1->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
2256 new_port1 = ses1->in_port;
2258 old_addr1 = ip1->dst_address;
2259 ip1->dst_address = new_addr1;
2260 vnet_buffer(b1)->sw_if_index[VLIB_TX] = sm->inside_fib_index;
2262 sum1 = ip1->checksum;
2263 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
2265 dst_address /* changed member */);
2266 ip1->checksum = ip_csum_fold (sum1);
2268 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
2270 if (tcp1->flags & TCP_FLAG_FIN && ses1->state == SNAT_SESSION_TCP_ESTABLISHED)
2271 ses1->state = SNAT_SESSION_TCP_CLOSE_WAIT;
2272 else if (tcp1->flags & TCP_FLAG_ACK && ses1->state == SNAT_SESSION_TCP_LAST_ACK)
2273 snat_det_ses_close(dm1, ses1);
2275 old_port1 = tcp1->dst;
2276 tcp1->dst = new_port1;
2278 sum1 = tcp1->checksum;
2279 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
2281 dst_address /* changed member */);
2283 sum1 = ip_csum_update (sum1, old_port1, new_port1,
2284 ip4_header_t /* cheat */,
2285 length /* changed member */);
2286 tcp1->checksum = ip_csum_fold(sum1);
2290 old_port1 = udp1->dst_port;
2291 udp1->dst_port = new_port1;
2297 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2298 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
2300 snat_out2in_trace_t *t =
2301 vlib_add_trace (vm, node, b1, sizeof (*t));
2302 t->sw_if_index = sw_if_index1;
2303 t->next_index = next1;
2304 t->session_index = ~0;
2306 t->session_index = ses1 - dm1->sessions;
2309 pkts_processed += next1 != SNAT_OUT2IN_NEXT_DROP;
2311 /* verify speculative enqueues, maybe switch current next frame */
2312 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2313 to_next, n_left_to_next,
2314 bi0, bi1, next0, next1);
2317 while (n_left_from > 0 && n_left_to_next > 0)
2321 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
2325 ip4_address_t new_addr0, old_addr0;
2326 u16 new_port0, old_port0;
2327 udp_header_t * udp0;
2328 tcp_header_t * tcp0;
2330 snat_det_out_key_t key0;
2331 snat_det_map_t * dm0;
2332 snat_det_session_t * ses0 = 0;
2334 icmp46_header_t * icmp0;
2336 /* speculatively enqueue b0 to the current next frame */
2342 n_left_to_next -= 1;
2344 b0 = vlib_get_buffer (vm, bi0);
2346 ip0 = vlib_buffer_get_current (b0);
2347 udp0 = ip4_next_header (ip0);
2348 tcp0 = (tcp_header_t *) udp0;
2350 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2352 if (PREDICT_FALSE(ip0->ttl == 1))
2354 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2355 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2356 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2358 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
2362 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2364 if (PREDICT_FALSE(proto0 == SNAT_PROTOCOL_ICMP))
2366 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
2367 icmp0 = (icmp46_header_t *) udp0;
2369 next0 = icmp_out2in(sm, b0, ip0, icmp0, sw_if_index0,
2370 rx_fib_index0, node, next0, thread_index,
2375 key0.ext_host_addr = ip0->src_address;
2376 key0.ext_host_port = tcp0->src;
2377 key0.out_port = tcp0->dst;
2379 dm0 = snat_det_map_by_out(sm, &ip0->dst_address);
2380 if (PREDICT_FALSE(!dm0))
2382 clib_warning("unknown dst address: %U",
2383 format_ip4_address, &ip0->dst_address);
2384 next0 = SNAT_OUT2IN_NEXT_DROP;
2385 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
2389 snat_det_reverse(dm0, &ip0->dst_address,
2390 clib_net_to_host_u16(tcp0->dst), &new_addr0);
2392 ses0 = snat_det_get_ses_by_out (dm0, &new_addr0, key0.as_u64);
2393 if (PREDICT_FALSE(!ses0))
2395 clib_warning("no match src %U:%d dst %U:%d for user %U",
2396 format_ip4_address, &ip0->src_address,
2397 clib_net_to_host_u16 (tcp0->src),
2398 format_ip4_address, &ip0->dst_address,
2399 clib_net_to_host_u16 (tcp0->dst),
2400 format_ip4_address, &new_addr0);
2401 next0 = SNAT_OUT2IN_NEXT_DROP;
2402 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
2405 new_port0 = ses0->in_port;
2407 old_addr0 = ip0->dst_address;
2408 ip0->dst_address = new_addr0;
2409 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm->inside_fib_index;
2411 sum0 = ip0->checksum;
2412 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
2414 dst_address /* changed member */);
2415 ip0->checksum = ip_csum_fold (sum0);
2417 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2419 if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
2420 ses0->state = SNAT_SESSION_TCP_CLOSE_WAIT;
2421 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_LAST_ACK)
2422 snat_det_ses_close(dm0, ses0);
2424 old_port0 = tcp0->dst;
2425 tcp0->dst = new_port0;
2427 sum0 = tcp0->checksum;
2428 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
2430 dst_address /* changed member */);
2432 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2433 ip4_header_t /* cheat */,
2434 length /* changed member */);
2435 tcp0->checksum = ip_csum_fold(sum0);
2439 old_port0 = udp0->dst_port;
2440 udp0->dst_port = new_port0;
2446 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2447 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2449 snat_out2in_trace_t *t =
2450 vlib_add_trace (vm, node, b0, sizeof (*t));
2451 t->sw_if_index = sw_if_index0;
2452 t->next_index = next0;
2453 t->session_index = ~0;
2455 t->session_index = ses0 - dm0->sessions;
2458 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
2460 /* verify speculative enqueue, maybe switch current next frame */
2461 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2462 to_next, n_left_to_next,
2466 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2469 vlib_node_increment_counter (vm, snat_det_out2in_node.index,
2470 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
2472 return frame->n_vectors;
2475 VLIB_REGISTER_NODE (snat_det_out2in_node) = {
2476 .function = snat_det_out2in_node_fn,
2477 .name = "nat44-det-out2in",
2478 .vector_size = sizeof (u32),
2479 .format_trace = format_snat_out2in_trace,
2480 .type = VLIB_NODE_TYPE_INTERNAL,
2482 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
2483 .error_strings = snat_out2in_error_strings,
2485 .runtime_data_bytes = sizeof (snat_runtime_t),
2487 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
2489 /* edit / add dispositions here */
2491 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
2492 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
2493 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2494 [SNAT_OUT2IN_NEXT_REASS] = "nat44-out2in-reass",
2495 [SNAT_OUT2IN_NEXT_IN2OUT] = "nat44-in2out",
2498 VLIB_NODE_FUNCTION_MULTIARCH (snat_det_out2in_node, snat_det_out2in_node_fn);
2501 * Get address and port values to be used for ICMP packet translation
2502 * and create session if needed
2504 * @param[in,out] sm NAT main
2505 * @param[in,out] node NAT node runtime
2506 * @param[in] thread_index thread index
2507 * @param[in,out] b0 buffer containing packet to be translated
2508 * @param[out] p_proto protocol used for matching
2509 * @param[out] p_value address and port after NAT translation
2510 * @param[out] p_dont_translate if packet should not be translated
2511 * @param d optional parameter
2512 * @param e optional parameter
2514 u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node,
2515 u32 thread_index, vlib_buffer_t *b0,
2516 ip4_header_t *ip0, u8 *p_proto,
2517 snat_session_key_t *p_value,
2518 u8 *p_dont_translate, void *d, void *e)
2520 icmp46_header_t *icmp0;
2523 snat_det_out_key_t key0;
2524 u8 dont_translate = 0;
2526 icmp_echo_header_t *echo0, *inner_echo0 = 0;
2527 ip4_header_t *inner_ip0;
2528 void *l4_header = 0;
2529 icmp46_header_t *inner_icmp0;
2530 snat_det_map_t * dm0 = 0;
2531 ip4_address_t new_addr0 = {{0}};
2532 snat_det_session_t * ses0 = 0;
2533 ip4_address_t out_addr;
2535 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
2536 echo0 = (icmp_echo_header_t *)(icmp0+1);
2537 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2539 if (!icmp_is_error_message (icmp0))
2541 protocol = SNAT_PROTOCOL_ICMP;
2542 key0.ext_host_addr = ip0->src_address;
2543 key0.ext_host_port = 0;
2544 key0.out_port = echo0->identifier;
2545 out_addr = ip0->dst_address;
2549 inner_ip0 = (ip4_header_t *)(echo0+1);
2550 l4_header = ip4_next_header (inner_ip0);
2551 protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
2552 key0.ext_host_addr = inner_ip0->dst_address;
2553 out_addr = inner_ip0->src_address;
2556 case SNAT_PROTOCOL_ICMP:
2557 inner_icmp0 = (icmp46_header_t*)l4_header;
2558 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
2559 key0.ext_host_port = 0;
2560 key0.out_port = inner_echo0->identifier;
2562 case SNAT_PROTOCOL_UDP:
2563 case SNAT_PROTOCOL_TCP:
2564 key0.ext_host_port = ((tcp_udp_header_t*)l4_header)->dst_port;
2565 key0.out_port = ((tcp_udp_header_t*)l4_header)->src_port;
2568 b0->error = node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
2569 next0 = SNAT_OUT2IN_NEXT_DROP;
2574 dm0 = snat_det_map_by_out(sm, &out_addr);
2575 if (PREDICT_FALSE(!dm0))
2577 /* Don't NAT packet aimed at the intfc address */
2578 if (PREDICT_FALSE(is_interface_addr(sm, node, sw_if_index0,
2579 ip0->dst_address.as_u32)))
2584 clib_warning("unknown dst address: %U",
2585 format_ip4_address, &ip0->dst_address);
2589 snat_det_reverse(dm0, &ip0->dst_address,
2590 clib_net_to_host_u16(key0.out_port), &new_addr0);
2592 ses0 = snat_det_get_ses_by_out (dm0, &new_addr0, key0.as_u64);
2593 if (PREDICT_FALSE(!ses0))
2595 /* Don't NAT packet aimed at the intfc address */
2596 if (PREDICT_FALSE(is_interface_addr(sm, node, sw_if_index0,
2597 ip0->dst_address.as_u32)))
2602 clib_warning("no match src %U:%d dst %U:%d for user %U",
2603 format_ip4_address, &key0.ext_host_addr,
2604 clib_net_to_host_u16 (key0.ext_host_port),
2605 format_ip4_address, &out_addr,
2606 clib_net_to_host_u16 (key0.out_port),
2607 format_ip4_address, &new_addr0);
2608 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
2609 next0 = SNAT_OUT2IN_NEXT_DROP;
2613 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_reply &&
2614 !icmp_is_error_message (icmp0)))
2616 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
2617 next0 = SNAT_OUT2IN_NEXT_DROP;
2624 *p_proto = protocol;
2627 p_value->addr = new_addr0;
2628 p_value->fib_index = sm->inside_fib_index;
2629 p_value->port = ses0->in_port;
2631 *p_dont_translate = dont_translate;
2633 *(snat_det_session_t**)d = ses0;
2635 *(snat_det_map_t**)e = dm0;
2639 /**********************/
2640 /*** worker handoff ***/
2641 /**********************/
2643 snat_out2in_worker_handoff_fn (vlib_main_t * vm,
2644 vlib_node_runtime_t * node,
2645 vlib_frame_t * frame)
2647 snat_main_t *sm = &snat_main;
2648 vlib_thread_main_t *tm = vlib_get_thread_main ();
2649 u32 n_left_from, *from, *to_next = 0;
2650 static __thread vlib_frame_queue_elt_t **handoff_queue_elt_by_worker_index;
2651 static __thread vlib_frame_queue_t **congested_handoff_queue_by_worker_index
2653 vlib_frame_queue_elt_t *hf = 0;
2654 vlib_frame_t *f = 0;
2656 u32 n_left_to_next_worker = 0, *to_next_worker = 0;
2657 u32 next_worker_index = 0;
2658 u32 current_worker_index = ~0;
2659 u32 thread_index = vlib_get_thread_index ();
2661 ASSERT (vec_len (sm->workers));
2663 if (PREDICT_FALSE (handoff_queue_elt_by_worker_index == 0))
2665 vec_validate (handoff_queue_elt_by_worker_index, tm->n_vlib_mains - 1);
2667 vec_validate_init_empty (congested_handoff_queue_by_worker_index,
2668 sm->first_worker_index + sm->num_workers - 1,
2669 (vlib_frame_queue_t *) (~0));
2672 from = vlib_frame_vector_args (frame);
2673 n_left_from = frame->n_vectors;
2675 while (n_left_from > 0)
2688 b0 = vlib_get_buffer (vm, bi0);
2690 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2691 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
2693 ip0 = vlib_buffer_get_current (b0);
2695 next_worker_index = sm->worker_out2in_cb(ip0, rx_fib_index0);
2697 if (PREDICT_FALSE (next_worker_index != thread_index))
2701 if (next_worker_index != current_worker_index)
2704 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
2706 hf = vlib_get_worker_handoff_queue_elt (sm->fq_out2in_index,
2708 handoff_queue_elt_by_worker_index);
2710 n_left_to_next_worker = VLIB_FRAME_SIZE - hf->n_vectors;
2711 to_next_worker = &hf->buffer_index[hf->n_vectors];
2712 current_worker_index = next_worker_index;
2715 /* enqueue to correct worker thread */
2716 to_next_worker[0] = bi0;
2718 n_left_to_next_worker--;
2720 if (n_left_to_next_worker == 0)
2722 hf->n_vectors = VLIB_FRAME_SIZE;
2723 vlib_put_frame_queue_elt (hf);
2724 current_worker_index = ~0;
2725 handoff_queue_elt_by_worker_index[next_worker_index] = 0;
2732 /* if this is 1st frame */
2735 f = vlib_get_frame_to_node (vm, sm->out2in_node_index);
2736 to_next = vlib_frame_vector_args (f);
2744 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2745 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2747 snat_out2in_worker_handoff_trace_t *t =
2748 vlib_add_trace (vm, node, b0, sizeof (*t));
2749 t->next_worker_index = next_worker_index;
2750 t->do_handoff = do_handoff;
2755 vlib_put_frame_to_node (vm, sm->out2in_node_index, f);
2758 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
2760 /* Ship frames to the worker nodes */
2761 for (i = 0; i < vec_len (handoff_queue_elt_by_worker_index); i++)
2763 if (handoff_queue_elt_by_worker_index[i])
2765 hf = handoff_queue_elt_by_worker_index[i];
2767 * It works better to let the handoff node
2768 * rate-adapt, always ship the handoff queue element.
2770 if (1 || hf->n_vectors == hf->last_n_vectors)
2772 vlib_put_frame_queue_elt (hf);
2773 handoff_queue_elt_by_worker_index[i] = 0;
2776 hf->last_n_vectors = hf->n_vectors;
2778 congested_handoff_queue_by_worker_index[i] =
2779 (vlib_frame_queue_t *) (~0);
2782 current_worker_index = ~0;
2783 return frame->n_vectors;
2786 VLIB_REGISTER_NODE (snat_out2in_worker_handoff_node) = {
2787 .function = snat_out2in_worker_handoff_fn,
2788 .name = "nat44-out2in-worker-handoff",
2789 .vector_size = sizeof (u32),
2790 .format_trace = format_snat_out2in_worker_handoff_trace,
2791 .type = VLIB_NODE_TYPE_INTERNAL,
2800 VLIB_NODE_FUNCTION_MULTIARCH (snat_out2in_worker_handoff_node, snat_out2in_worker_handoff_fn);
2803 snat_out2in_fast_node_fn (vlib_main_t * vm,
2804 vlib_node_runtime_t * node,
2805 vlib_frame_t * frame)
2807 u32 n_left_from, * from, * to_next;
2808 snat_out2in_next_t next_index;
2809 u32 pkts_processed = 0;
2810 snat_main_t * sm = &snat_main;
2812 from = vlib_frame_vector_args (frame);
2813 n_left_from = frame->n_vectors;
2814 next_index = node->cached_next_index;
2816 while (n_left_from > 0)
2820 vlib_get_next_frame (vm, node, next_index,
2821 to_next, n_left_to_next);
2823 while (n_left_from > 0 && n_left_to_next > 0)
2827 u32 next0 = SNAT_OUT2IN_NEXT_DROP;
2831 u32 new_addr0, old_addr0;
2832 u16 new_port0, old_port0;
2833 udp_header_t * udp0;
2834 tcp_header_t * tcp0;
2835 icmp46_header_t * icmp0;
2836 snat_session_key_t key0, sm0;
2840 /* speculatively enqueue b0 to the current next frame */
2846 n_left_to_next -= 1;
2848 b0 = vlib_get_buffer (vm, bi0);
2850 ip0 = vlib_buffer_get_current (b0);
2851 udp0 = ip4_next_header (ip0);
2852 tcp0 = (tcp_header_t *) udp0;
2853 icmp0 = (icmp46_header_t *) udp0;
2855 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2856 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
2858 vnet_feature_next (sw_if_index0, &next0, b0);
2860 if (PREDICT_FALSE(ip0->ttl == 1))
2862 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2863 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2864 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2866 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
2870 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2872 if (PREDICT_FALSE (proto0 == ~0))
2875 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
2877 next0 = icmp_out2in(sm, b0, ip0, icmp0, sw_if_index0,
2878 rx_fib_index0, node, next0, ~0, 0, 0);
2882 key0.addr = ip0->dst_address;
2883 key0.port = udp0->dst_port;
2884 key0.fib_index = rx_fib_index0;
2886 if (snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0))
2888 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
2892 new_addr0 = sm0.addr.as_u32;
2893 new_port0 = sm0.port;
2894 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
2895 old_addr0 = ip0->dst_address.as_u32;
2896 ip0->dst_address.as_u32 = new_addr0;
2898 sum0 = ip0->checksum;
2899 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2901 dst_address /* changed member */);
2902 ip0->checksum = ip_csum_fold (sum0);
2904 if (PREDICT_FALSE(new_port0 != udp0->dst_port))
2906 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2908 old_port0 = tcp0->dst_port;
2909 tcp0->dst_port = new_port0;
2911 sum0 = tcp0->checksum;
2912 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2914 dst_address /* changed member */);
2916 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2917 ip4_header_t /* cheat */,
2918 length /* changed member */);
2919 tcp0->checksum = ip_csum_fold(sum0);
2923 old_port0 = udp0->dst_port;
2924 udp0->dst_port = new_port0;
2930 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2932 sum0 = tcp0->checksum;
2933 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2935 dst_address /* changed member */);
2937 tcp0->checksum = ip_csum_fold(sum0);
2943 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2944 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2946 snat_out2in_trace_t *t =
2947 vlib_add_trace (vm, node, b0, sizeof (*t));
2948 t->sw_if_index = sw_if_index0;
2949 t->next_index = next0;
2952 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
2954 /* verify speculative enqueue, maybe switch current next frame */
2955 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2956 to_next, n_left_to_next,
2960 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2963 vlib_node_increment_counter (vm, snat_out2in_fast_node.index,
2964 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
2966 return frame->n_vectors;
2969 VLIB_REGISTER_NODE (snat_out2in_fast_node) = {
2970 .function = snat_out2in_fast_node_fn,
2971 .name = "nat44-out2in-fast",
2972 .vector_size = sizeof (u32),
2973 .format_trace = format_snat_out2in_fast_trace,
2974 .type = VLIB_NODE_TYPE_INTERNAL,
2976 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
2977 .error_strings = snat_out2in_error_strings,
2979 .runtime_data_bytes = sizeof (snat_runtime_t),
2981 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
2983 /* edit / add dispositions here */
2985 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
2986 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
2987 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2988 [SNAT_OUT2IN_NEXT_REASS] = "nat44-out2in-reass",
2989 [SNAT_OUT2IN_NEXT_IN2OUT] = "nat44-in2out",
2992 VLIB_NODE_FUNCTION_MULTIARCH (snat_out2in_fast_node, snat_out2in_fast_node_fn);