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_log_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_log_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_log_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_log_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_log_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_log_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 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
898 old_port0 = tcp0->dst_port;
899 tcp0->dst_port = s0->in2out.port;
900 new_port0 = tcp0->dst_port;
902 sum0 = tcp0->checksum;
903 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
905 dst_address /* changed member */ );
907 sum0 = ip_csum_update (sum0, old_port0, new_port0,
908 ip4_header_t /* cheat */ ,
909 length /* changed member */ );
910 tcp0->checksum = ip_csum_fold (sum0);
915 old_port0 = udp0->dst_port;
916 udp0->dst_port = s0->in2out.port;
922 nat44_session_update_counters (s0, now,
923 vlib_buffer_length_in_chain (vm, b0),
925 /* Per-user LRU list maintenance */
926 nat44_session_update_lru (sm, s0, thread_index);
929 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
930 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
932 snat_out2in_trace_t *t =
933 vlib_add_trace (vm, node, b0, sizeof (*t));
934 t->sw_if_index = sw_if_index0;
935 t->next_index = next0;
936 t->session_index = ~0;
939 s0 - sm->per_thread_data[thread_index].sessions;
942 pkts_processed += next0 == SNAT_OUT2IN_NEXT_LOOKUP;
945 ip1 = vlib_buffer_get_current (b1);
946 udp1 = ip4_next_header (ip1);
947 tcp1 = (tcp_header_t *) udp1;
948 icmp1 = (icmp46_header_t *) udp1;
950 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
951 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
954 if (PREDICT_FALSE (ip1->ttl == 1))
956 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
957 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
958 ICMP4_time_exceeded_ttl_exceeded_in_transit,
960 next1 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
964 proto1 = ip_proto_to_snat_proto (ip1->protocol);
966 if (PREDICT_FALSE (proto1 == ~0))
968 if (nat_out2in_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
970 if (!sm->forwarding_enabled)
973 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
974 next1 = SNAT_OUT2IN_NEXT_DROP;
981 if (PREDICT_FALSE (ip4_is_fragment (ip1)))
983 next1 = SNAT_OUT2IN_NEXT_REASS;
988 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
990 next1 = icmp_out2in_slow_path
991 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
992 next1, now, thread_index, &s1);
997 key1.addr = ip1->dst_address;
998 key1.port = udp1->dst_port;
999 key1.protocol = proto1;
1000 key1.fib_index = rx_fib_index1;
1002 kv1.key = key1.as_u64;
1004 if (clib_bihash_search_8_8
1005 (&sm->per_thread_data[thread_index].out2in, &kv1, &value1))
1007 /* Try to match static mapping by external address and port,
1008 destination address and port in packet */
1009 if (snat_static_mapping_match
1010 (sm, key1, &sm1, 1, 0, 0, 0, 0, &identity_nat1))
1013 * Send DHCP packets to the ipv4 stack, or we won't
1014 * be able to use dhcp client on the outside interface
1016 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_UDP
1017 && (udp1->dst_port ==
1018 clib_host_to_net_u16
1019 (UDP_DST_PORT_dhcp_to_client))))
1021 vnet_feature_next (&next1, b1);
1025 if (!sm->forwarding_enabled)
1028 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1029 next1 = SNAT_OUT2IN_NEXT_DROP;
1034 if (PREDICT_FALSE (identity_nat1))
1037 /* Create session initiated by host from external network */
1038 s1 = create_session_for_static_mapping (sm, b1, sm1, key1, node,
1042 next1 = SNAT_OUT2IN_NEXT_DROP;
1048 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1051 old_addr1 = ip1->dst_address.as_u32;
1052 ip1->dst_address = s1->in2out.addr;
1053 new_addr1 = ip1->dst_address.as_u32;
1054 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1056 sum1 = ip1->checksum;
1057 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1059 dst_address /* changed member */ );
1060 ip1->checksum = ip_csum_fold (sum1);
1062 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1064 old_port1 = tcp1->dst_port;
1065 tcp1->dst_port = s1->in2out.port;
1066 new_port1 = tcp1->dst_port;
1068 sum1 = tcp1->checksum;
1069 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1071 dst_address /* changed member */ );
1073 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1074 ip4_header_t /* cheat */ ,
1075 length /* changed member */ );
1076 tcp1->checksum = ip_csum_fold (sum1);
1081 old_port1 = udp1->dst_port;
1082 udp1->dst_port = s1->in2out.port;
1088 nat44_session_update_counters (s1, now,
1089 vlib_buffer_length_in_chain (vm, b1),
1091 /* Per-user LRU list maintenance */
1092 nat44_session_update_lru (sm, s1, thread_index);
1095 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1096 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1098 snat_out2in_trace_t *t =
1099 vlib_add_trace (vm, node, b1, sizeof (*t));
1100 t->sw_if_index = sw_if_index1;
1101 t->next_index = next1;
1102 t->session_index = ~0;
1105 s1 - sm->per_thread_data[thread_index].sessions;
1108 pkts_processed += next1 == SNAT_OUT2IN_NEXT_LOOKUP;
1110 /* verify speculative enqueues, maybe switch current next frame */
1111 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1112 to_next, n_left_to_next,
1113 bi0, bi1, next0, next1);
1116 while (n_left_from > 0 && n_left_to_next > 0)
1120 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1124 u32 new_addr0, old_addr0;
1125 u16 new_port0, old_port0;
1128 icmp46_header_t *icmp0;
1129 snat_session_key_t key0, sm0;
1132 snat_session_t *s0 = 0;
1133 clib_bihash_kv_8_8_t kv0, value0;
1136 /* speculatively enqueue b0 to the current next frame */
1142 n_left_to_next -= 1;
1144 b0 = vlib_get_buffer (vm, bi0);
1146 vnet_buffer (b0)->snat.flags = 0;
1148 ip0 = vlib_buffer_get_current (b0);
1149 udp0 = ip4_next_header (ip0);
1150 tcp0 = (tcp_header_t *) udp0;
1151 icmp0 = (icmp46_header_t *) udp0;
1153 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1154 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1157 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1159 if (PREDICT_FALSE (proto0 == ~0))
1161 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1163 if (!sm->forwarding_enabled)
1166 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
1167 next0 = SNAT_OUT2IN_NEXT_DROP;
1174 if (PREDICT_FALSE (ip0->ttl == 1))
1176 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1177 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1178 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1180 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1184 if (PREDICT_FALSE (ip4_is_fragment (ip0)))
1186 next0 = SNAT_OUT2IN_NEXT_REASS;
1191 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1193 next0 = icmp_out2in_slow_path
1194 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1195 next0, now, thread_index, &s0);
1200 key0.addr = ip0->dst_address;
1201 key0.port = udp0->dst_port;
1202 key0.protocol = proto0;
1203 key0.fib_index = rx_fib_index0;
1205 kv0.key = key0.as_u64;
1207 if (clib_bihash_search_8_8
1208 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
1210 /* Try to match static mapping by external address and port,
1211 destination address and port in packet */
1212 if (snat_static_mapping_match
1213 (sm, key0, &sm0, 1, 0, 0, 0, 0, &identity_nat0))
1216 * Send DHCP packets to the ipv4 stack, or we won't
1217 * be able to use dhcp client on the outside interface
1219 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1220 && (udp0->dst_port ==
1221 clib_host_to_net_u16
1222 (UDP_DST_PORT_dhcp_to_client))))
1224 vnet_feature_next (&next0, b0);
1228 if (!sm->forwarding_enabled)
1231 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1232 next0 = SNAT_OUT2IN_NEXT_DROP;
1237 if (PREDICT_FALSE (identity_nat0))
1240 /* Create session initiated by host from external network */
1241 s0 = create_session_for_static_mapping (sm, b0, sm0, key0, node,
1245 next0 = SNAT_OUT2IN_NEXT_DROP;
1251 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1254 old_addr0 = ip0->dst_address.as_u32;
1255 ip0->dst_address = s0->in2out.addr;
1256 new_addr0 = ip0->dst_address.as_u32;
1257 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1259 sum0 = ip0->checksum;
1260 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1262 dst_address /* changed member */ );
1263 ip0->checksum = ip_csum_fold (sum0);
1265 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1267 old_port0 = tcp0->dst_port;
1268 tcp0->dst_port = s0->in2out.port;
1269 new_port0 = tcp0->dst_port;
1271 sum0 = tcp0->checksum;
1272 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1274 dst_address /* changed member */ );
1276 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1277 ip4_header_t /* cheat */ ,
1278 length /* changed member */ );
1279 tcp0->checksum = ip_csum_fold (sum0);
1284 old_port0 = udp0->dst_port;
1285 udp0->dst_port = s0->in2out.port;
1291 nat44_session_update_counters (s0, now,
1292 vlib_buffer_length_in_chain (vm, b0),
1294 /* Per-user LRU list maintenance */
1295 nat44_session_update_lru (sm, s0, thread_index);
1298 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1299 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1301 snat_out2in_trace_t *t =
1302 vlib_add_trace (vm, node, b0, sizeof (*t));
1303 t->sw_if_index = sw_if_index0;
1304 t->next_index = next0;
1305 t->session_index = ~0;
1308 s0 - sm->per_thread_data[thread_index].sessions;
1311 pkts_processed += next0 == SNAT_OUT2IN_NEXT_LOOKUP;
1313 /* verify speculative enqueue, maybe switch current next frame */
1314 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1315 to_next, n_left_to_next,
1319 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1322 vlib_node_increment_counter (vm, sm->out2in_node_index,
1323 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1325 vlib_node_increment_counter (vm, sm->out2in_node_index,
1326 SNAT_OUT2IN_ERROR_TCP_PACKETS, tcp_packets);
1327 vlib_node_increment_counter (vm, sm->out2in_node_index,
1328 SNAT_OUT2IN_ERROR_UDP_PACKETS, udp_packets);
1329 vlib_node_increment_counter (vm, sm->out2in_node_index,
1330 SNAT_OUT2IN_ERROR_ICMP_PACKETS, icmp_packets);
1331 vlib_node_increment_counter (vm, sm->out2in_node_index,
1332 SNAT_OUT2IN_ERROR_OTHER_PACKETS,
1334 vlib_node_increment_counter (vm, sm->out2in_node_index,
1335 SNAT_OUT2IN_ERROR_FRAGMENTS, fragments);
1337 return frame->n_vectors;
1341 VLIB_REGISTER_NODE (snat_out2in_node) = {
1342 .name = "nat44-out2in",
1343 .vector_size = sizeof (u32),
1344 .format_trace = format_snat_out2in_trace,
1345 .type = VLIB_NODE_TYPE_INTERNAL,
1347 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1348 .error_strings = snat_out2in_error_strings,
1350 .runtime_data_bytes = sizeof (snat_runtime_t),
1352 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1354 /* edit / add dispositions here */
1356 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1357 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1358 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1359 [SNAT_OUT2IN_NEXT_REASS] = "nat44-out2in-reass",
1364 VLIB_NODE_FN (nat44_out2in_reass_node) (vlib_main_t * vm,
1365 vlib_node_runtime_t * node,
1366 vlib_frame_t * frame)
1368 u32 n_left_from, *from, *to_next;
1369 snat_out2in_next_t next_index;
1370 u32 pkts_processed = 0, cached_fragments = 0;
1371 snat_main_t *sm = &snat_main;
1372 f64 now = vlib_time_now (vm);
1373 u32 thread_index = vm->thread_index;
1374 snat_main_per_thread_data_t *per_thread_data =
1375 &sm->per_thread_data[thread_index];
1376 u32 *fragments_to_drop = 0;
1377 u32 *fragments_to_loopback = 0;
1379 from = vlib_frame_vector_args (frame);
1380 n_left_from = frame->n_vectors;
1381 next_index = node->cached_next_index;
1383 while (n_left_from > 0)
1387 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1389 while (n_left_from > 0 && n_left_to_next > 0)
1391 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1396 nat_reass_ip4_t *reass0;
1399 icmp46_header_t *icmp0;
1400 snat_session_key_t key0, sm0;
1401 clib_bihash_kv_8_8_t kv0, value0;
1402 snat_session_t *s0 = 0;
1403 u16 old_port0, new_port0;
1407 /* speculatively enqueue b0 to the current next frame */
1413 n_left_to_next -= 1;
1415 b0 = vlib_get_buffer (vm, bi0);
1416 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1418 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1420 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1423 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1425 next0 = SNAT_OUT2IN_NEXT_DROP;
1426 b0->error = node->errors[SNAT_OUT2IN_ERROR_DROP_FRAGMENT];
1430 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1431 udp0 = ip4_next_header (ip0);
1432 tcp0 = (tcp_header_t *) udp0;
1433 icmp0 = (icmp46_header_t *) udp0;
1434 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1436 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1440 1, &fragments_to_drop);
1442 if (PREDICT_FALSE (!reass0))
1444 next0 = SNAT_OUT2IN_NEXT_DROP;
1445 b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_REASS];
1446 nat_log_notice ("maximum reassemblies exceeded");
1450 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1452 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1454 next0 = icmp_out2in_slow_path
1455 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1456 next0, now, thread_index, &s0);
1458 if (PREDICT_TRUE (next0 != SNAT_OUT2IN_NEXT_DROP))
1461 reass0->sess_index = s0 - per_thread_data->sessions;
1463 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1464 reass0->thread_index = thread_index;
1465 nat_ip4_reass_get_frags (reass0,
1466 &fragments_to_loopback);
1472 key0.addr = ip0->dst_address;
1473 key0.port = udp0->dst_port;
1474 key0.protocol = proto0;
1475 key0.fib_index = rx_fib_index0;
1476 kv0.key = key0.as_u64;
1478 if (clib_bihash_search_8_8
1479 (&per_thread_data->out2in, &kv0, &value0))
1481 /* Try to match static mapping by external address and port,
1482 destination address and port in packet */
1483 if (snat_static_mapping_match
1484 (sm, key0, &sm0, 1, 0, 0, 0, 0, &identity_nat0))
1487 * Send DHCP packets to the ipv4 stack, or we won't
1488 * be able to use dhcp client on the outside interface
1490 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1493 clib_host_to_net_u16
1494 (UDP_DST_PORT_dhcp_to_client))))
1496 vnet_feature_next (&next0, b0);
1500 if (!sm->forwarding_enabled)
1503 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1504 next0 = SNAT_OUT2IN_NEXT_DROP;
1508 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1509 nat_ip4_reass_get_frags (reass0,
1510 &fragments_to_loopback);
1515 if (PREDICT_FALSE (identity_nat0))
1518 /* Create session initiated by host from external network */
1520 create_session_for_static_mapping (sm, b0, sm0, key0,
1526 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1527 next0 = SNAT_OUT2IN_NEXT_DROP;
1530 reass0->sess_index = s0 - per_thread_data->sessions;
1531 reass0->thread_index = thread_index;
1535 s0 = pool_elt_at_index (per_thread_data->sessions,
1537 reass0->sess_index = value0.value;
1539 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1543 if (reass0->flags & NAT_REASS_FLAG_ED_DONT_TRANSLATE)
1545 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1547 if (nat_ip4_reass_add_fragment
1548 (thread_index, reass0, bi0, &fragments_to_drop))
1550 b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_FRAG];
1552 ("maximum fragments per reassembly exceeded");
1553 next0 = SNAT_OUT2IN_NEXT_DROP;
1559 s0 = pool_elt_at_index (per_thread_data->sessions,
1560 reass0->sess_index);
1563 old_addr0 = ip0->dst_address.as_u32;
1564 ip0->dst_address = s0->in2out.addr;
1565 new_addr0 = ip0->dst_address.as_u32;
1566 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1568 sum0 = ip0->checksum;
1569 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1571 dst_address /* changed member */ );
1572 ip0->checksum = ip_csum_fold (sum0);
1574 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1576 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1578 old_port0 = tcp0->dst_port;
1579 tcp0->dst_port = s0->in2out.port;
1580 new_port0 = tcp0->dst_port;
1582 sum0 = tcp0->checksum;
1583 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1585 dst_address /* changed member */ );
1587 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1588 ip4_header_t /* cheat */ ,
1589 length /* changed member */ );
1590 tcp0->checksum = ip_csum_fold (sum0);
1594 old_port0 = udp0->dst_port;
1595 udp0->dst_port = s0->in2out.port;
1601 nat44_session_update_counters (s0, now,
1602 vlib_buffer_length_in_chain (vm, b0),
1604 /* Per-user LRU list maintenance */
1605 nat44_session_update_lru (sm, s0, thread_index);
1608 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1609 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1611 nat44_reass_trace_t *t =
1612 vlib_add_trace (vm, node, b0, sizeof (*t));
1613 t->cached = cached0;
1614 t->sw_if_index = sw_if_index0;
1615 t->next_index = next0;
1626 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
1628 /* verify speculative enqueue, maybe switch current next frame */
1629 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1630 to_next, n_left_to_next,
1634 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1636 from = vlib_frame_vector_args (frame);
1637 u32 len = vec_len (fragments_to_loopback);
1638 if (len <= VLIB_FRAME_SIZE)
1640 clib_memcpy_fast (from, fragments_to_loopback,
1641 sizeof (u32) * len);
1643 vec_reset_length (fragments_to_loopback);
1647 clib_memcpy_fast (from, fragments_to_loopback +
1648 (len - VLIB_FRAME_SIZE),
1649 sizeof (u32) * VLIB_FRAME_SIZE);
1650 n_left_from = VLIB_FRAME_SIZE;
1651 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1656 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1659 vlib_node_increment_counter (vm, sm->out2in_reass_node_index,
1660 SNAT_OUT2IN_ERROR_PROCESSED_FRAGMENTS,
1662 vlib_node_increment_counter (vm, sm->out2in_reass_node_index,
1663 SNAT_OUT2IN_ERROR_CACHED_FRAGMENTS,
1666 nat_send_all_to_node (vm, fragments_to_drop, node,
1667 &node->errors[SNAT_OUT2IN_ERROR_DROP_FRAGMENT],
1668 SNAT_OUT2IN_NEXT_DROP);
1670 vec_free (fragments_to_drop);
1671 vec_free (fragments_to_loopback);
1672 return frame->n_vectors;
1676 VLIB_REGISTER_NODE (nat44_out2in_reass_node) = {
1677 .name = "nat44-out2in-reass",
1678 .vector_size = sizeof (u32),
1679 .format_trace = format_nat44_reass_trace,
1680 .type = VLIB_NODE_TYPE_INTERNAL,
1682 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1683 .error_strings = snat_out2in_error_strings,
1685 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1687 /* edit / add dispositions here */
1689 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1690 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1691 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1692 [SNAT_OUT2IN_NEXT_REASS] = "nat44-out2in-reass",
1697 VLIB_NODE_FN (snat_out2in_fast_node) (vlib_main_t * vm,
1698 vlib_node_runtime_t * node,
1699 vlib_frame_t * frame)
1701 u32 n_left_from, *from, *to_next;
1702 snat_out2in_next_t next_index;
1703 u32 pkts_processed = 0;
1704 snat_main_t *sm = &snat_main;
1706 from = vlib_frame_vector_args (frame);
1707 n_left_from = frame->n_vectors;
1708 next_index = node->cached_next_index;
1710 while (n_left_from > 0)
1714 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1716 while (n_left_from > 0 && n_left_to_next > 0)
1720 u32 next0 = SNAT_OUT2IN_NEXT_DROP;
1724 u32 new_addr0, old_addr0;
1725 u16 new_port0, old_port0;
1728 icmp46_header_t *icmp0;
1729 snat_session_key_t key0, sm0;
1733 /* speculatively enqueue b0 to the current next frame */
1739 n_left_to_next -= 1;
1741 b0 = vlib_get_buffer (vm, bi0);
1743 ip0 = vlib_buffer_get_current (b0);
1744 udp0 = ip4_next_header (ip0);
1745 tcp0 = (tcp_header_t *) udp0;
1746 icmp0 = (icmp46_header_t *) udp0;
1748 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1750 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1752 vnet_feature_next (&next0, b0);
1754 if (PREDICT_FALSE (ip0->ttl == 1))
1756 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1757 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1758 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1760 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1764 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1766 if (PREDICT_FALSE (proto0 == ~0))
1769 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1771 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0,
1772 rx_fib_index0, node, next0, ~0, 0, 0);
1776 key0.addr = ip0->dst_address;
1777 key0.port = udp0->dst_port;
1778 key0.fib_index = rx_fib_index0;
1780 if (snat_static_mapping_match (sm, key0, &sm0, 1, 0, 0, 0, 0, 0))
1782 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1786 new_addr0 = sm0.addr.as_u32;
1787 new_port0 = sm0.port;
1788 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1789 old_addr0 = ip0->dst_address.as_u32;
1790 ip0->dst_address.as_u32 = new_addr0;
1792 sum0 = ip0->checksum;
1793 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1795 dst_address /* changed member */ );
1796 ip0->checksum = ip_csum_fold (sum0);
1798 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1800 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1802 old_port0 = tcp0->dst_port;
1803 tcp0->dst_port = new_port0;
1805 sum0 = tcp0->checksum;
1806 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1808 dst_address /* changed member */ );
1810 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1811 ip4_header_t /* cheat */ ,
1812 length /* changed member */ );
1813 tcp0->checksum = ip_csum_fold (sum0);
1817 old_port0 = udp0->dst_port;
1818 udp0->dst_port = new_port0;
1824 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1826 sum0 = tcp0->checksum;
1827 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1829 dst_address /* changed member */ );
1831 tcp0->checksum = ip_csum_fold (sum0);
1837 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1838 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1840 snat_out2in_trace_t *t =
1841 vlib_add_trace (vm, node, b0, sizeof (*t));
1842 t->sw_if_index = sw_if_index0;
1843 t->next_index = next0;
1846 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
1848 /* verify speculative enqueue, maybe switch current next frame */
1849 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1850 to_next, n_left_to_next,
1854 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1857 vlib_node_increment_counter (vm, sm->out2in_fast_node_index,
1858 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1860 return frame->n_vectors;
1864 VLIB_REGISTER_NODE (snat_out2in_fast_node) = {
1865 .name = "nat44-out2in-fast",
1866 .vector_size = sizeof (u32),
1867 .format_trace = format_snat_out2in_fast_trace,
1868 .type = VLIB_NODE_TYPE_INTERNAL,
1870 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1871 .error_strings = snat_out2in_error_strings,
1873 .runtime_data_bytes = sizeof (snat_runtime_t),
1875 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1877 /* edit / add dispositions here */
1879 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1880 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1881 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1882 [SNAT_OUT2IN_NEXT_REASS] = "nat44-out2in-reass",
1888 * fd.io coding-style-patch-verification: ON
1891 * eval: (c-set-style "gnu")