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.
17 * @brief NAT44 endpoint-dependent outside to inside network translation
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/pg/pg.h>
24 #include <vnet/ip/ip.h>
25 #include <vnet/udp/udp.h>
26 #include <vnet/ethernet/ethernet.h>
27 #include <vnet/fib/ip4_fib.h>
29 #include <nat/nat_ipfix_logging.h>
30 #include <nat/nat_reass.h>
31 #include <nat/nat_inlines.h>
32 #include <nat/nat_syslog.h>
33 #include <nat/nat_ha.h>
35 #include <vppinfra/hash.h>
36 #include <vppinfra/error.h>
37 #include <vppinfra/elog.h>
44 } snat_out2in_trace_t;
46 /* packet trace format function */
48 format_snat_out2in_trace (u8 * s, va_list * args)
50 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
51 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
52 snat_out2in_trace_t *t = va_arg (*args, snat_out2in_trace_t *);
56 "NAT44_OUT2IN: sw_if_index %d, next index %d, session index %d",
57 t->sw_if_index, t->next_index, t->session_index);
62 format_snat_out2in_fast_trace (u8 * s, va_list * args)
64 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
65 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
66 snat_out2in_trace_t *t = va_arg (*args, snat_out2in_trace_t *);
68 s = format (s, "NAT44_OUT2IN_FAST: sw_if_index %d, next index %d",
69 t->sw_if_index, t->next_index);
73 #define foreach_snat_out2in_error \
74 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
75 _(OUT2IN_PACKETS, "good out2in packets processed") \
76 _(OUT_OF_PORTS, "out of ports") \
77 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
78 _(NO_TRANSLATION, "no translation") \
79 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
80 _(DROP_FRAGMENT, "drop fragment") \
81 _(MAX_REASS, "maximum reassemblies exceeded") \
82 _(MAX_FRAG, "maximum fragments per reassembly exceeded")\
83 _(TCP_PACKETS, "TCP packets") \
84 _(UDP_PACKETS, "UDP packets") \
85 _(ICMP_PACKETS, "ICMP packets") \
86 _(OTHER_PACKETS, "other protocol packets") \
87 _(FRAGMENTS, "fragments") \
88 _(CACHED_FRAGMENTS, "cached fragments") \
89 _(PROCESSED_FRAGMENTS, "processed fragments")
93 #define _(sym,str) SNAT_OUT2IN_ERROR_##sym,
94 foreach_snat_out2in_error
97 } snat_out2in_error_t;
99 static char *snat_out2in_error_strings[] = {
100 #define _(sym,string) string,
101 foreach_snat_out2in_error
107 SNAT_OUT2IN_NEXT_DROP,
108 SNAT_OUT2IN_NEXT_LOOKUP,
109 SNAT_OUT2IN_NEXT_ICMP_ERROR,
110 SNAT_OUT2IN_NEXT_REASS,
112 } snat_out2in_next_t;
114 #ifndef CLIB_MARCH_VARIANT
116 nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
118 snat_main_t *sm = &snat_main;
119 nat44_is_idle_session_ctx_t *ctx = arg;
121 u64 sess_timeout_time;
122 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
124 clib_bihash_kv_8_8_t s_kv;
126 s = pool_elt_at_index (tsm->sessions, kv->value);
127 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
128 if (ctx->now >= sess_timeout_time)
130 s_kv.key = s->in2out.as_u64;
131 if (clib_bihash_add_del_8_8 (&tsm->in2out, &s_kv, 0))
132 nat_elog_warn ("out2in key del failed");
134 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
135 s->in2out.addr.as_u32,
136 s->out2in.addr.as_u32,
140 s->in2out.fib_index);
142 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
143 &s->in2out.addr, s->in2out.port,
144 &s->out2in.addr, s->out2in.port,
147 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
148 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
151 if (!snat_is_session_static (s))
152 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
155 nat44_delete_session (sm, s, ctx->thread_index);
164 * @brief Create session for static mapping.
166 * Create NAT session initiated by host from external network with static
169 * @param sm NAT main.
170 * @param b0 Vlib buffer.
171 * @param in2out In2out NAT44 session key.
172 * @param out2in Out2in NAT44 session key.
173 * @param node Vlib node.
175 * @returns SNAT session if successfully created otherwise 0.
177 static inline snat_session_t *
178 create_session_for_static_mapping (snat_main_t * sm,
180 snat_session_key_t in2out,
181 snat_session_key_t out2in,
182 vlib_node_runtime_t * node,
183 u32 thread_index, f64 now)
187 clib_bihash_kv_8_8_t kv0;
190 nat44_is_idle_session_ctx_t ctx0;
192 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
194 b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_SESSIONS_EXCEEDED];
195 nat_elog_notice ("maximum sessions exceeded");
199 ip0 = vlib_buffer_get_current (b0);
200 udp0 = ip4_next_header (ip0);
203 nat_user_get_or_create (sm, &in2out.addr, in2out.fib_index, thread_index);
206 nat_elog_warn ("create NAT user failed");
210 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
213 nat44_delete_user_with_no_session (sm, u, thread_index);
214 nat_elog_warn ("create NAT session failed");
218 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
219 s->ext_host_addr.as_u32 = ip0->src_address.as_u32;
220 s->ext_host_port = udp0->src_port;
221 user_session_increment (sm, u, 1 /* static */ );
224 s->in2out.protocol = out2in.protocol;
226 /* Add to translation hashes */
228 ctx0.thread_index = thread_index;
229 kv0.key = s->in2out.as_u64;
230 kv0.value = s - sm->per_thread_data[thread_index].sessions;
231 if (clib_bihash_add_or_overwrite_stale_8_8
232 (&sm->per_thread_data[thread_index].in2out, &kv0,
233 nat44_i2o_is_idle_session_cb, &ctx0))
234 nat_elog_notice ("in2out key add failed");
236 kv0.key = s->out2in.as_u64;
238 if (clib_bihash_add_or_overwrite_stale_8_8
239 (&sm->per_thread_data[thread_index].out2in, &kv0,
240 nat44_o2i_is_idle_session_cb, &ctx0))
241 nat_elog_notice ("out2in key add failed");
244 snat_ipfix_logging_nat44_ses_create (thread_index,
245 s->in2out.addr.as_u32,
246 s->out2in.addr.as_u32,
249 s->out2in.port, s->in2out.fib_index);
251 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
252 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
253 s->out2in.port, s->in2out.protocol);
255 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
256 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
257 &s->ext_host_nat_addr, s->ext_host_nat_port,
258 s->in2out.protocol, s->in2out.fib_index, s->flags,
264 #ifndef CLIB_MARCH_VARIANT
266 snat_out2in_error_t icmp_get_key (ip4_header_t * ip0,
267 snat_session_key_t * p_key0)
269 icmp46_header_t *icmp0;
270 snat_session_key_t key0;
271 icmp_echo_header_t *echo0, *inner_echo0 = 0;
272 ip4_header_t *inner_ip0;
274 icmp46_header_t *inner_icmp0;
276 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
277 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
279 if (!icmp_is_error_message (icmp0))
281 key0.protocol = SNAT_PROTOCOL_ICMP;
282 key0.addr = ip0->dst_address;
283 key0.port = echo0->identifier;
287 inner_ip0 = (ip4_header_t *) (echo0 + 1);
288 l4_header = ip4_next_header (inner_ip0);
289 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
290 key0.addr = inner_ip0->src_address;
291 switch (key0.protocol)
293 case SNAT_PROTOCOL_ICMP:
294 inner_icmp0 = (icmp46_header_t *) l4_header;
295 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
296 key0.port = inner_echo0->identifier;
298 case SNAT_PROTOCOL_UDP:
299 case SNAT_PROTOCOL_TCP:
300 key0.port = ((tcp_udp_header_t *) l4_header)->src_port;
303 return SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL;
307 return -1; /* success */
311 * Get address and port values to be used for ICMP packet translation
312 * and create session if needed
314 * @param[in,out] sm NAT main
315 * @param[in,out] node NAT node runtime
316 * @param[in] thread_index thread index
317 * @param[in,out] b0 buffer containing packet to be translated
318 * @param[out] p_proto protocol used for matching
319 * @param[out] p_value address and port after NAT translation
320 * @param[out] p_dont_translate if packet should not be translated
321 * @param d optional parameter
322 * @param e optional parameter
325 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
326 u32 thread_index, vlib_buffer_t * b0,
327 ip4_header_t * ip0, u8 * p_proto,
328 snat_session_key_t * p_value,
329 u8 * p_dont_translate, void *d, void *e)
331 icmp46_header_t *icmp0;
334 snat_session_key_t key0;
335 snat_session_key_t sm0;
336 snat_session_t *s0 = 0;
337 u8 dont_translate = 0;
338 clib_bihash_kv_8_8_t kv0, value0;
344 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
345 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
346 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
350 err = icmp_get_key (ip0, &key0);
353 b0->error = node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
354 next0 = SNAT_OUT2IN_NEXT_DROP;
357 key0.fib_index = rx_fib_index0;
359 kv0.key = key0.as_u64;
361 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
364 /* Try to match static mapping by external address and port,
365 destination address and port in packet */
366 if (snat_static_mapping_match
367 (sm, key0, &sm0, 1, &is_addr_only, 0, 0, 0, &identity_nat))
369 if (!sm->forwarding_enabled)
371 /* Don't NAT packet aimed at the intfc address */
372 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
373 ip0->dst_address.as_u32)))
378 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
379 next0 = SNAT_OUT2IN_NEXT_DROP;
389 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_reply &&
390 (icmp0->type != ICMP4_echo_request
393 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
394 next0 = SNAT_OUT2IN_NEXT_DROP;
398 if (PREDICT_FALSE (identity_nat))
403 /* Create session initiated by host from external network */
404 s0 = create_session_for_static_mapping (sm, b0, sm0, key0,
406 vlib_time_now (sm->vlib_main));
410 next0 = SNAT_OUT2IN_NEXT_DROP;
416 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_reply &&
417 icmp0->type != ICMP4_echo_request &&
418 !icmp_is_error_message (icmp0)))
420 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
421 next0 = SNAT_OUT2IN_NEXT_DROP;
425 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
430 *p_proto = key0.protocol;
432 *p_value = s0->in2out;
433 *p_dont_translate = dont_translate;
435 *(snat_session_t **) d = s0;
440 #ifndef CLIB_MARCH_VARIANT
442 * Get address and port values to be used for ICMP packet translation
444 * @param[in] sm NAT main
445 * @param[in,out] node NAT node runtime
446 * @param[in] thread_index thread index
447 * @param[in,out] b0 buffer containing packet to be translated
448 * @param[out] p_proto protocol used for matching
449 * @param[out] p_value address and port after NAT translation
450 * @param[out] p_dont_translate if packet should not be translated
451 * @param d optional parameter
452 * @param e optional parameter
455 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
456 u32 thread_index, vlib_buffer_t * b0,
457 ip4_header_t * ip0, u8 * p_proto,
458 snat_session_key_t * p_value,
459 u8 * p_dont_translate, void *d, void *e)
461 icmp46_header_t *icmp0;
464 snat_session_key_t key0;
465 snat_session_key_t sm0;
466 u8 dont_translate = 0;
471 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
472 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
473 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
475 err = icmp_get_key (ip0, &key0);
478 b0->error = node->errors[err];
479 next0 = SNAT_OUT2IN_NEXT_DROP;
482 key0.fib_index = rx_fib_index0;
484 if (snat_static_mapping_match
485 (sm, key0, &sm0, 1, &is_addr_only, 0, 0, 0, 0))
487 /* Don't NAT packet aimed at the intfc address */
488 if (is_interface_addr (sm, node, sw_if_index0, ip0->dst_address.as_u32))
493 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
494 next0 = SNAT_OUT2IN_NEXT_DROP;
498 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_reply &&
499 (icmp0->type != ICMP4_echo_request || !is_addr_only) &&
500 !icmp_is_error_message (icmp0)))
502 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
503 next0 = SNAT_OUT2IN_NEXT_DROP;
510 *p_proto = key0.protocol;
511 *p_dont_translate = dont_translate;
516 #ifndef CLIB_MARCH_VARIANT
518 icmp_out2in (snat_main_t * sm,
521 icmp46_header_t * icmp0,
524 vlib_node_runtime_t * node,
525 u32 next0, u32 thread_index, void *d, void *e)
527 snat_session_key_t sm0;
529 icmp_echo_header_t *echo0, *inner_echo0 = 0;
530 ip4_header_t *inner_ip0 = 0;
532 icmp46_header_t *inner_icmp0;
534 u32 new_addr0, old_addr0;
535 u16 old_id0, new_id0;
540 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
542 next0_tmp = sm->icmp_match_out2in_cb (sm, node, thread_index, b0, ip0,
543 &protocol, &sm0, &dont_translate, d,
547 if (next0 == SNAT_OUT2IN_NEXT_DROP || dont_translate)
550 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
552 sum0 = ip_incremental_checksum_buffer (sm->vlib_main, b0, (u8 *) icmp0 -
554 vlib_buffer_get_current (b0),
555 ntohs (ip0->length) -
556 ip4_header_bytes (ip0), 0);
557 checksum0 = ~ip_csum_fold (sum0);
558 if (checksum0 != 0 && checksum0 != 0xffff)
560 next0 = SNAT_OUT2IN_NEXT_DROP;
565 old_addr0 = ip0->dst_address.as_u32;
566 new_addr0 = ip0->dst_address.as_u32 = sm0.addr.as_u32;
567 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
569 sum0 = ip0->checksum;
570 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
571 dst_address /* changed member */ );
572 ip0->checksum = ip_csum_fold (sum0);
574 if (icmp0->checksum == 0)
575 icmp0->checksum = 0xffff;
577 if (!icmp_is_error_message (icmp0))
580 if (PREDICT_FALSE (new_id0 != echo0->identifier))
582 old_id0 = echo0->identifier;
584 echo0->identifier = new_id0;
586 sum0 = icmp0->checksum;
587 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
588 identifier /* changed member */ );
589 icmp0->checksum = ip_csum_fold (sum0);
594 inner_ip0 = (ip4_header_t *) (echo0 + 1);
595 l4_header = ip4_next_header (inner_ip0);
597 if (!ip4_header_checksum_is_valid (inner_ip0))
599 next0 = SNAT_OUT2IN_NEXT_DROP;
603 old_addr0 = inner_ip0->src_address.as_u32;
604 inner_ip0->src_address = sm0.addr;
605 new_addr0 = inner_ip0->src_address.as_u32;
607 sum0 = icmp0->checksum;
608 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
609 src_address /* changed member */ );
610 icmp0->checksum = ip_csum_fold (sum0);
614 case SNAT_PROTOCOL_ICMP:
615 inner_icmp0 = (icmp46_header_t *) l4_header;
616 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
618 old_id0 = inner_echo0->identifier;
620 inner_echo0->identifier = new_id0;
622 sum0 = icmp0->checksum;
623 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
625 icmp0->checksum = ip_csum_fold (sum0);
627 case SNAT_PROTOCOL_UDP:
628 case SNAT_PROTOCOL_TCP:
629 old_id0 = ((tcp_udp_header_t *) l4_header)->src_port;
631 ((tcp_udp_header_t *) l4_header)->src_port = new_id0;
633 sum0 = icmp0->checksum;
634 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
636 icmp0->checksum = ip_csum_fold (sum0);
649 icmp_out2in_slow_path (snat_main_t * sm,
652 icmp46_header_t * icmp0,
655 vlib_node_runtime_t * node,
657 u32 thread_index, snat_session_t ** p_s0)
659 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
660 next0, thread_index, p_s0, 0);
661 snat_session_t *s0 = *p_s0;
662 if (PREDICT_TRUE (next0 != SNAT_OUT2IN_NEXT_DROP && s0))
665 nat44_session_update_counters (s0, now,
666 vlib_buffer_length_in_chain
667 (sm->vlib_main, b0), thread_index);
668 /* Per-user LRU list maintenance */
669 nat44_session_update_lru (sm, s0, thread_index);
675 nat_out2in_sm_unknown_proto (snat_main_t * sm,
677 ip4_header_t * ip, u32 rx_fib_index)
679 clib_bihash_kv_8_8_t kv, value;
680 snat_static_mapping_t *m;
681 snat_session_key_t m_key;
682 u32 old_addr, new_addr;
685 m_key.addr = ip->dst_address;
689 kv.key = m_key.as_u64;
690 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
693 m = pool_elt_at_index (sm->static_mappings, value.value);
695 old_addr = ip->dst_address.as_u32;
696 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
698 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
699 ip->checksum = ip_csum_fold (sum);
701 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
705 VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm,
706 vlib_node_runtime_t * node,
707 vlib_frame_t * frame)
709 u32 n_left_from, *from, *to_next;
710 snat_out2in_next_t next_index;
711 u32 pkts_processed = 0;
712 snat_main_t *sm = &snat_main;
713 f64 now = vlib_time_now (vm);
714 u32 thread_index = vm->thread_index;
715 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
718 from = vlib_frame_vector_args (frame);
719 n_left_from = frame->n_vectors;
720 next_index = node->cached_next_index;
722 while (n_left_from > 0)
726 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
728 while (n_left_from >= 4 && n_left_to_next >= 2)
731 vlib_buffer_t *b0, *b1;
732 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
733 u32 next1 = SNAT_OUT2IN_NEXT_LOOKUP;
734 u32 sw_if_index0, sw_if_index1;
735 ip4_header_t *ip0, *ip1;
736 ip_csum_t sum0, sum1;
737 u32 new_addr0, old_addr0;
738 u16 new_port0, old_port0;
739 u32 new_addr1, old_addr1;
740 u16 new_port1, old_port1;
741 udp_header_t *udp0, *udp1;
742 tcp_header_t *tcp0, *tcp1;
743 icmp46_header_t *icmp0, *icmp1;
744 snat_session_key_t key0, key1, sm0, sm1;
745 u32 rx_fib_index0, rx_fib_index1;
747 snat_session_t *s0 = 0, *s1 = 0;
748 clib_bihash_kv_8_8_t kv0, kv1, value0, value1;
749 u8 identity_nat0, identity_nat1;
751 /* Prefetch next iteration. */
753 vlib_buffer_t *p2, *p3;
755 p2 = vlib_get_buffer (vm, from[2]);
756 p3 = vlib_get_buffer (vm, from[3]);
758 vlib_prefetch_buffer_header (p2, LOAD);
759 vlib_prefetch_buffer_header (p3, LOAD);
761 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
762 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
765 /* speculatively enqueue b0 and b1 to the current next frame */
766 to_next[0] = bi0 = from[0];
767 to_next[1] = bi1 = from[1];
773 b0 = vlib_get_buffer (vm, bi0);
774 b1 = vlib_get_buffer (vm, bi1);
776 vnet_buffer (b0)->snat.flags = 0;
777 vnet_buffer (b1)->snat.flags = 0;
779 ip0 = vlib_buffer_get_current (b0);
780 udp0 = ip4_next_header (ip0);
781 tcp0 = (tcp_header_t *) udp0;
782 icmp0 = (icmp46_header_t *) udp0;
784 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
785 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
788 if (PREDICT_FALSE (ip0->ttl == 1))
790 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
791 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
792 ICMP4_time_exceeded_ttl_exceeded_in_transit,
794 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
798 proto0 = ip_proto_to_snat_proto (ip0->protocol);
800 if (PREDICT_FALSE (proto0 == ~0))
802 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
804 if (!sm->forwarding_enabled)
807 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
808 next0 = SNAT_OUT2IN_NEXT_DROP;
815 if (PREDICT_FALSE (ip4_is_fragment (ip0)))
817 next0 = SNAT_OUT2IN_NEXT_REASS;
822 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
824 next0 = icmp_out2in_slow_path
825 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
826 next0, now, thread_index, &s0);
831 key0.addr = ip0->dst_address;
832 key0.port = udp0->dst_port;
833 key0.protocol = proto0;
834 key0.fib_index = rx_fib_index0;
836 kv0.key = key0.as_u64;
838 if (clib_bihash_search_8_8
839 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
841 /* Try to match static mapping by external address and port,
842 destination address and port in packet */
843 if (snat_static_mapping_match
844 (sm, key0, &sm0, 1, 0, 0, 0, 0, &identity_nat0))
847 * Send DHCP packets to the ipv4 stack, or we won't
848 * be able to use dhcp client on the outside interface
850 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
851 && (udp0->dst_port ==
853 (UDP_DST_PORT_dhcp_to_client))))
855 vnet_feature_next (&next0, b0);
859 if (!sm->forwarding_enabled)
862 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
863 next0 = SNAT_OUT2IN_NEXT_DROP;
868 if (PREDICT_FALSE (identity_nat0))
871 /* Create session initiated by host from external network */
872 s0 = create_session_for_static_mapping (sm, b0, sm0, key0, node,
876 next0 = SNAT_OUT2IN_NEXT_DROP;
882 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
885 old_addr0 = ip0->dst_address.as_u32;
886 ip0->dst_address = s0->in2out.addr;
887 new_addr0 = ip0->dst_address.as_u32;
888 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
890 sum0 = ip0->checksum;
891 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
893 dst_address /* changed member */ );
894 ip0->checksum = ip_csum_fold (sum0);
896 old_port0 = udp0->dst_port;
897 new_port0 = udp0->dst_port = s0->in2out.port;
899 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
901 sum0 = tcp0->checksum;
902 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
904 dst_address /* changed member */ );
906 sum0 = ip_csum_update (sum0, old_port0, new_port0,
907 ip4_header_t /* cheat */ ,
908 length /* changed member */ );
909 tcp0->checksum = ip_csum_fold (sum0);
914 if (PREDICT_FALSE (udp0->checksum))
916 sum0 = udp0->checksum;
917 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
919 dst_address /* changed member */ );
920 sum0 = ip_csum_update (sum0, old_port0, new_port0,
921 ip4_header_t /* cheat */ ,
922 length /* changed member */ );
923 udp0->checksum = ip_csum_fold (sum0);
929 nat44_session_update_counters (s0, now,
930 vlib_buffer_length_in_chain (vm, b0),
932 /* Per-user LRU list maintenance */
933 nat44_session_update_lru (sm, s0, thread_index);
936 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
937 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
939 snat_out2in_trace_t *t =
940 vlib_add_trace (vm, node, b0, sizeof (*t));
941 t->sw_if_index = sw_if_index0;
942 t->next_index = next0;
943 t->session_index = ~0;
946 s0 - sm->per_thread_data[thread_index].sessions;
949 pkts_processed += next0 == SNAT_OUT2IN_NEXT_LOOKUP;
952 ip1 = vlib_buffer_get_current (b1);
953 udp1 = ip4_next_header (ip1);
954 tcp1 = (tcp_header_t *) udp1;
955 icmp1 = (icmp46_header_t *) udp1;
957 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
958 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
961 if (PREDICT_FALSE (ip1->ttl == 1))
963 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
964 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
965 ICMP4_time_exceeded_ttl_exceeded_in_transit,
967 next1 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
971 proto1 = ip_proto_to_snat_proto (ip1->protocol);
973 if (PREDICT_FALSE (proto1 == ~0))
975 if (nat_out2in_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
977 if (!sm->forwarding_enabled)
980 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
981 next1 = SNAT_OUT2IN_NEXT_DROP;
988 if (PREDICT_FALSE (ip4_is_fragment (ip1)))
990 next1 = SNAT_OUT2IN_NEXT_REASS;
995 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
997 next1 = icmp_out2in_slow_path
998 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
999 next1, now, thread_index, &s1);
1004 key1.addr = ip1->dst_address;
1005 key1.port = udp1->dst_port;
1006 key1.protocol = proto1;
1007 key1.fib_index = rx_fib_index1;
1009 kv1.key = key1.as_u64;
1011 if (clib_bihash_search_8_8
1012 (&sm->per_thread_data[thread_index].out2in, &kv1, &value1))
1014 /* Try to match static mapping by external address and port,
1015 destination address and port in packet */
1016 if (snat_static_mapping_match
1017 (sm, key1, &sm1, 1, 0, 0, 0, 0, &identity_nat1))
1020 * Send DHCP packets to the ipv4 stack, or we won't
1021 * be able to use dhcp client on the outside interface
1023 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_UDP
1024 && (udp1->dst_port ==
1025 clib_host_to_net_u16
1026 (UDP_DST_PORT_dhcp_to_client))))
1028 vnet_feature_next (&next1, b1);
1032 if (!sm->forwarding_enabled)
1035 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1036 next1 = SNAT_OUT2IN_NEXT_DROP;
1041 if (PREDICT_FALSE (identity_nat1))
1044 /* Create session initiated by host from external network */
1045 s1 = create_session_for_static_mapping (sm, b1, sm1, key1, node,
1049 next1 = SNAT_OUT2IN_NEXT_DROP;
1055 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1058 old_addr1 = ip1->dst_address.as_u32;
1059 ip1->dst_address = s1->in2out.addr;
1060 new_addr1 = ip1->dst_address.as_u32;
1061 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1063 sum1 = ip1->checksum;
1064 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1066 dst_address /* changed member */ );
1067 ip1->checksum = ip_csum_fold (sum1);
1069 old_port1 = udp1->dst_port;
1070 new_port1 = udp1->dst_port = s1->in2out.port;
1072 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1074 sum1 = tcp1->checksum;
1075 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1077 dst_address /* changed member */ );
1079 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1080 ip4_header_t /* cheat */ ,
1081 length /* changed member */ );
1082 tcp1->checksum = ip_csum_fold (sum1);
1087 if (PREDICT_FALSE (udp1->checksum))
1089 sum1 = udp1->checksum;
1090 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1092 dst_address /* changed member */ );
1093 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1094 ip4_header_t /* cheat */ ,
1095 length /* changed member */ );
1096 udp1->checksum = ip_csum_fold (sum1);
1102 nat44_session_update_counters (s1, now,
1103 vlib_buffer_length_in_chain (vm, b1),
1105 /* Per-user LRU list maintenance */
1106 nat44_session_update_lru (sm, s1, thread_index);
1109 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1110 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1112 snat_out2in_trace_t *t =
1113 vlib_add_trace (vm, node, b1, sizeof (*t));
1114 t->sw_if_index = sw_if_index1;
1115 t->next_index = next1;
1116 t->session_index = ~0;
1119 s1 - sm->per_thread_data[thread_index].sessions;
1122 pkts_processed += next1 == SNAT_OUT2IN_NEXT_LOOKUP;
1124 /* verify speculative enqueues, maybe switch current next frame */
1125 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1126 to_next, n_left_to_next,
1127 bi0, bi1, next0, next1);
1130 while (n_left_from > 0 && n_left_to_next > 0)
1134 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1138 u32 new_addr0, old_addr0;
1139 u16 new_port0, old_port0;
1142 icmp46_header_t *icmp0;
1143 snat_session_key_t key0, sm0;
1146 snat_session_t *s0 = 0;
1147 clib_bihash_kv_8_8_t kv0, value0;
1150 /* speculatively enqueue b0 to the current next frame */
1156 n_left_to_next -= 1;
1158 b0 = vlib_get_buffer (vm, bi0);
1160 vnet_buffer (b0)->snat.flags = 0;
1162 ip0 = vlib_buffer_get_current (b0);
1163 udp0 = ip4_next_header (ip0);
1164 tcp0 = (tcp_header_t *) udp0;
1165 icmp0 = (icmp46_header_t *) udp0;
1167 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1168 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1171 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1173 if (PREDICT_FALSE (proto0 == ~0))
1175 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1177 if (!sm->forwarding_enabled)
1180 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
1181 next0 = SNAT_OUT2IN_NEXT_DROP;
1188 if (PREDICT_FALSE (ip0->ttl == 1))
1190 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1191 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1192 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1194 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1198 if (PREDICT_FALSE (ip4_is_fragment (ip0)))
1200 next0 = SNAT_OUT2IN_NEXT_REASS;
1205 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1207 next0 = icmp_out2in_slow_path
1208 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1209 next0, now, thread_index, &s0);
1214 key0.addr = ip0->dst_address;
1215 key0.port = udp0->dst_port;
1216 key0.protocol = proto0;
1217 key0.fib_index = rx_fib_index0;
1219 kv0.key = key0.as_u64;
1221 if (clib_bihash_search_8_8
1222 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
1224 /* Try to match static mapping by external address and port,
1225 destination address and port in packet */
1226 if (snat_static_mapping_match
1227 (sm, key0, &sm0, 1, 0, 0, 0, 0, &identity_nat0))
1230 * Send DHCP packets to the ipv4 stack, or we won't
1231 * be able to use dhcp client on the outside interface
1233 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1234 && (udp0->dst_port ==
1235 clib_host_to_net_u16
1236 (UDP_DST_PORT_dhcp_to_client))))
1238 vnet_feature_next (&next0, b0);
1242 if (!sm->forwarding_enabled)
1245 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1246 next0 = SNAT_OUT2IN_NEXT_DROP;
1251 if (PREDICT_FALSE (identity_nat0))
1254 /* Create session initiated by host from external network */
1255 s0 = create_session_for_static_mapping (sm, b0, sm0, key0, node,
1259 next0 = SNAT_OUT2IN_NEXT_DROP;
1265 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1268 old_addr0 = ip0->dst_address.as_u32;
1269 ip0->dst_address = s0->in2out.addr;
1270 new_addr0 = ip0->dst_address.as_u32;
1271 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1273 sum0 = ip0->checksum;
1274 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1276 dst_address /* changed member */ );
1277 ip0->checksum = ip_csum_fold (sum0);
1279 old_port0 = udp0->dst_port;
1280 new_port0 = udp0->dst_port = s0->in2out.port;
1282 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1284 sum0 = tcp0->checksum;
1285 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1287 dst_address /* changed member */ );
1289 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1290 ip4_header_t /* cheat */ ,
1291 length /* changed member */ );
1292 tcp0->checksum = ip_csum_fold (sum0);
1297 if (PREDICT_FALSE (udp0->checksum))
1299 sum0 = udp0->checksum;
1300 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1302 dst_address /* changed member */ );
1303 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1304 ip4_header_t /* cheat */ ,
1305 length /* changed member */ );
1306 udp0->checksum = ip_csum_fold (sum0);
1312 nat44_session_update_counters (s0, now,
1313 vlib_buffer_length_in_chain (vm, b0),
1315 /* Per-user LRU list maintenance */
1316 nat44_session_update_lru (sm, s0, thread_index);
1319 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1320 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1322 snat_out2in_trace_t *t =
1323 vlib_add_trace (vm, node, b0, sizeof (*t));
1324 t->sw_if_index = sw_if_index0;
1325 t->next_index = next0;
1326 t->session_index = ~0;
1329 s0 - sm->per_thread_data[thread_index].sessions;
1332 pkts_processed += next0 == SNAT_OUT2IN_NEXT_LOOKUP;
1334 /* verify speculative enqueue, maybe switch current next frame */
1335 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1336 to_next, n_left_to_next,
1340 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1343 vlib_node_increment_counter (vm, sm->out2in_node_index,
1344 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1346 vlib_node_increment_counter (vm, sm->out2in_node_index,
1347 SNAT_OUT2IN_ERROR_TCP_PACKETS, tcp_packets);
1348 vlib_node_increment_counter (vm, sm->out2in_node_index,
1349 SNAT_OUT2IN_ERROR_UDP_PACKETS, udp_packets);
1350 vlib_node_increment_counter (vm, sm->out2in_node_index,
1351 SNAT_OUT2IN_ERROR_ICMP_PACKETS, icmp_packets);
1352 vlib_node_increment_counter (vm, sm->out2in_node_index,
1353 SNAT_OUT2IN_ERROR_OTHER_PACKETS,
1355 vlib_node_increment_counter (vm, sm->out2in_node_index,
1356 SNAT_OUT2IN_ERROR_FRAGMENTS, fragments);
1358 return frame->n_vectors;
1362 VLIB_REGISTER_NODE (snat_out2in_node) = {
1363 .name = "nat44-out2in",
1364 .vector_size = sizeof (u32),
1365 .format_trace = format_snat_out2in_trace,
1366 .type = VLIB_NODE_TYPE_INTERNAL,
1368 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1369 .error_strings = snat_out2in_error_strings,
1371 .runtime_data_bytes = sizeof (snat_runtime_t),
1373 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1375 /* edit / add dispositions here */
1377 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1378 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1379 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1380 [SNAT_OUT2IN_NEXT_REASS] = "nat44-out2in-reass",
1385 VLIB_NODE_FN (nat44_out2in_reass_node) (vlib_main_t * vm,
1386 vlib_node_runtime_t * node,
1387 vlib_frame_t * frame)
1389 u32 n_left_from, *from, *to_next;
1390 snat_out2in_next_t next_index;
1391 u32 pkts_processed = 0, cached_fragments = 0;
1392 snat_main_t *sm = &snat_main;
1393 f64 now = vlib_time_now (vm);
1394 u32 thread_index = vm->thread_index;
1395 snat_main_per_thread_data_t *per_thread_data =
1396 &sm->per_thread_data[thread_index];
1397 u32 *fragments_to_drop = 0;
1398 u32 *fragments_to_loopback = 0;
1400 from = vlib_frame_vector_args (frame);
1401 n_left_from = frame->n_vectors;
1402 next_index = node->cached_next_index;
1404 while (n_left_from > 0)
1408 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1410 while (n_left_from > 0 && n_left_to_next > 0)
1412 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1417 nat_reass_ip4_t *reass0;
1420 icmp46_header_t *icmp0;
1421 snat_session_key_t key0, sm0;
1422 clib_bihash_kv_8_8_t kv0, value0;
1423 snat_session_t *s0 = 0;
1424 u16 old_port0, new_port0;
1428 /* speculatively enqueue b0 to the current next frame */
1434 n_left_to_next -= 1;
1436 b0 = vlib_get_buffer (vm, bi0);
1437 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1439 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1441 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1444 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1446 next0 = SNAT_OUT2IN_NEXT_DROP;
1447 b0->error = node->errors[SNAT_OUT2IN_ERROR_DROP_FRAGMENT];
1451 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1452 udp0 = ip4_next_header (ip0);
1453 tcp0 = (tcp_header_t *) udp0;
1454 icmp0 = (icmp46_header_t *) udp0;
1455 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1457 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1461 1, &fragments_to_drop);
1463 if (PREDICT_FALSE (!reass0))
1465 next0 = SNAT_OUT2IN_NEXT_DROP;
1466 b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_REASS];
1467 nat_elog_notice ("maximum reassemblies exceeded");
1471 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1473 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1475 next0 = icmp_out2in_slow_path
1476 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1477 next0, now, thread_index, &s0);
1479 if (PREDICT_TRUE (next0 != SNAT_OUT2IN_NEXT_DROP))
1482 reass0->sess_index = s0 - per_thread_data->sessions;
1484 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1485 reass0->thread_index = thread_index;
1486 nat_ip4_reass_get_frags (reass0,
1487 &fragments_to_loopback);
1493 key0.addr = ip0->dst_address;
1494 key0.port = udp0->dst_port;
1495 key0.protocol = proto0;
1496 key0.fib_index = rx_fib_index0;
1497 kv0.key = key0.as_u64;
1499 if (clib_bihash_search_8_8
1500 (&per_thread_data->out2in, &kv0, &value0))
1502 /* Try to match static mapping by external address and port,
1503 destination address and port in packet */
1504 if (snat_static_mapping_match
1505 (sm, key0, &sm0, 1, 0, 0, 0, 0, &identity_nat0))
1508 * Send DHCP packets to the ipv4 stack, or we won't
1509 * be able to use dhcp client on the outside interface
1511 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1514 clib_host_to_net_u16
1515 (UDP_DST_PORT_dhcp_to_client))))
1517 vnet_feature_next (&next0, b0);
1521 if (!sm->forwarding_enabled)
1524 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1525 next0 = SNAT_OUT2IN_NEXT_DROP;
1529 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1530 nat_ip4_reass_get_frags (reass0,
1531 &fragments_to_loopback);
1536 if (PREDICT_FALSE (identity_nat0))
1539 /* Create session initiated by host from external network */
1541 create_session_for_static_mapping (sm, b0, sm0, key0,
1547 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1548 next0 = SNAT_OUT2IN_NEXT_DROP;
1551 reass0->sess_index = s0 - per_thread_data->sessions;
1552 reass0->thread_index = thread_index;
1556 s0 = pool_elt_at_index (per_thread_data->sessions,
1558 reass0->sess_index = value0.value;
1560 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1564 if (reass0->flags & NAT_REASS_FLAG_ED_DONT_TRANSLATE)
1566 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1568 if (nat_ip4_reass_add_fragment
1569 (thread_index, reass0, bi0, &fragments_to_drop))
1571 b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_FRAG];
1573 ("maximum fragments per reassembly exceeded");
1574 next0 = SNAT_OUT2IN_NEXT_DROP;
1580 s0 = pool_elt_at_index (per_thread_data->sessions,
1581 reass0->sess_index);
1584 old_addr0 = ip0->dst_address.as_u32;
1585 ip0->dst_address = s0->in2out.addr;
1586 new_addr0 = ip0->dst_address.as_u32;
1587 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1589 sum0 = ip0->checksum;
1590 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1592 dst_address /* changed member */ );
1593 ip0->checksum = ip_csum_fold (sum0);
1595 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1597 old_port0 = udp0->dst_port;
1598 new_port0 = udp0->dst_port = s0->in2out.port;
1600 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1602 sum0 = tcp0->checksum;
1603 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1605 dst_address /* changed member */ );
1607 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1608 ip4_header_t /* cheat */ ,
1609 length /* changed member */ );
1610 tcp0->checksum = ip_csum_fold (sum0);
1612 else if (udp0->checksum)
1614 sum0 = udp0->checksum;
1615 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1617 dst_address /* changed member */ );
1618 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1619 ip4_header_t /* cheat */ ,
1620 length /* changed member */ );
1621 udp0->checksum = ip_csum_fold (sum0);
1626 nat44_session_update_counters (s0, now,
1627 vlib_buffer_length_in_chain (vm, b0),
1629 /* Per-user LRU list maintenance */
1630 nat44_session_update_lru (sm, s0, thread_index);
1633 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1634 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1636 nat44_reass_trace_t *t =
1637 vlib_add_trace (vm, node, b0, sizeof (*t));
1638 t->cached = cached0;
1639 t->sw_if_index = sw_if_index0;
1640 t->next_index = next0;
1651 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
1653 /* verify speculative enqueue, maybe switch current next frame */
1654 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1655 to_next, n_left_to_next,
1659 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1661 from = vlib_frame_vector_args (frame);
1662 u32 len = vec_len (fragments_to_loopback);
1663 if (len <= VLIB_FRAME_SIZE)
1665 clib_memcpy_fast (from, fragments_to_loopback,
1666 sizeof (u32) * len);
1668 vec_reset_length (fragments_to_loopback);
1672 clib_memcpy_fast (from, fragments_to_loopback +
1673 (len - VLIB_FRAME_SIZE),
1674 sizeof (u32) * VLIB_FRAME_SIZE);
1675 n_left_from = VLIB_FRAME_SIZE;
1676 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1681 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1684 vlib_node_increment_counter (vm, sm->out2in_reass_node_index,
1685 SNAT_OUT2IN_ERROR_PROCESSED_FRAGMENTS,
1687 vlib_node_increment_counter (vm, sm->out2in_reass_node_index,
1688 SNAT_OUT2IN_ERROR_CACHED_FRAGMENTS,
1691 nat_send_all_to_node (vm, fragments_to_drop, node,
1692 &node->errors[SNAT_OUT2IN_ERROR_DROP_FRAGMENT],
1693 SNAT_OUT2IN_NEXT_DROP);
1695 vec_free (fragments_to_drop);
1696 vec_free (fragments_to_loopback);
1697 return frame->n_vectors;
1701 VLIB_REGISTER_NODE (nat44_out2in_reass_node) = {
1702 .name = "nat44-out2in-reass",
1703 .vector_size = sizeof (u32),
1704 .format_trace = format_nat44_reass_trace,
1705 .type = VLIB_NODE_TYPE_INTERNAL,
1707 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1708 .error_strings = snat_out2in_error_strings,
1710 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1712 /* edit / add dispositions here */
1714 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1715 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1716 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1717 [SNAT_OUT2IN_NEXT_REASS] = "nat44-out2in-reass",
1722 VLIB_NODE_FN (snat_out2in_fast_node) (vlib_main_t * vm,
1723 vlib_node_runtime_t * node,
1724 vlib_frame_t * frame)
1726 u32 n_left_from, *from, *to_next;
1727 snat_out2in_next_t next_index;
1728 u32 pkts_processed = 0;
1729 snat_main_t *sm = &snat_main;
1731 from = vlib_frame_vector_args (frame);
1732 n_left_from = frame->n_vectors;
1733 next_index = node->cached_next_index;
1735 while (n_left_from > 0)
1739 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1741 while (n_left_from > 0 && n_left_to_next > 0)
1745 u32 next0 = SNAT_OUT2IN_NEXT_DROP;
1749 u32 new_addr0, old_addr0;
1750 u16 new_port0, old_port0;
1753 icmp46_header_t *icmp0;
1754 snat_session_key_t key0, sm0;
1758 /* speculatively enqueue b0 to the current next frame */
1764 n_left_to_next -= 1;
1766 b0 = vlib_get_buffer (vm, bi0);
1768 ip0 = vlib_buffer_get_current (b0);
1769 udp0 = ip4_next_header (ip0);
1770 tcp0 = (tcp_header_t *) udp0;
1771 icmp0 = (icmp46_header_t *) udp0;
1773 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1775 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1777 vnet_feature_next (&next0, b0);
1779 if (PREDICT_FALSE (ip0->ttl == 1))
1781 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1782 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1783 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1785 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1789 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1791 if (PREDICT_FALSE (proto0 == ~0))
1794 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1796 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0,
1797 rx_fib_index0, node, next0, ~0, 0, 0);
1801 key0.addr = ip0->dst_address;
1802 key0.port = udp0->dst_port;
1803 key0.fib_index = rx_fib_index0;
1805 if (snat_static_mapping_match (sm, key0, &sm0, 1, 0, 0, 0, 0, 0))
1807 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1811 new_addr0 = sm0.addr.as_u32;
1812 new_port0 = sm0.port;
1813 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1814 old_addr0 = ip0->dst_address.as_u32;
1815 ip0->dst_address.as_u32 = new_addr0;
1817 sum0 = ip0->checksum;
1818 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1820 dst_address /* changed member */ );
1821 ip0->checksum = ip_csum_fold (sum0);
1823 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1825 old_port0 = udp0->dst_port;
1826 udp0->dst_port = new_port0;
1828 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1830 sum0 = tcp0->checksum;
1831 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1833 dst_address /* changed member */ );
1834 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1835 ip4_header_t /* cheat */ ,
1836 length /* changed member */ );
1837 tcp0->checksum = ip_csum_fold (sum0);
1839 else if (udp0->checksum)
1841 sum0 = udp0->checksum;
1842 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1844 dst_address /* changed member */ );
1845 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1846 ip4_header_t /* cheat */ ,
1847 length /* changed member */ );
1848 udp0->checksum = ip_csum_fold (sum0);
1853 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1855 sum0 = tcp0->checksum;
1856 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1858 dst_address /* changed member */ );
1859 tcp0->checksum = ip_csum_fold (sum0);
1861 else if (udp0->checksum)
1863 sum0 = udp0->checksum;
1864 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1866 dst_address /* changed member */ );
1867 udp0->checksum = ip_csum_fold (sum0);
1873 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1874 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1876 snat_out2in_trace_t *t =
1877 vlib_add_trace (vm, node, b0, sizeof (*t));
1878 t->sw_if_index = sw_if_index0;
1879 t->next_index = next0;
1882 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
1884 /* verify speculative enqueue, maybe switch current next frame */
1885 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1886 to_next, n_left_to_next,
1890 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1893 vlib_node_increment_counter (vm, sm->out2in_fast_node_index,
1894 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1896 return frame->n_vectors;
1900 VLIB_REGISTER_NODE (snat_out2in_fast_node) = {
1901 .name = "nat44-out2in-fast",
1902 .vector_size = sizeof (u32),
1903 .format_trace = format_snat_out2in_fast_trace,
1904 .type = VLIB_NODE_TYPE_INTERNAL,
1906 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1907 .error_strings = snat_out2in_error_strings,
1909 .runtime_data_bytes = sizeof (snat_runtime_t),
1911 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1913 /* edit / add dispositions here */
1915 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1916 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1917 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1918 [SNAT_OUT2IN_NEXT_REASS] = "nat44-out2in-reass",
1924 * fd.io coding-style-patch-verification: ON
1927 * eval: (c-set-style "gnu")