- u32 bi0, bi1;
- vlib_buffer_t *b0, *b1;
- u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0, new_addr0;
- u32 next1, sw_if_index1, rx_fib_index1, proto1, old_addr1, new_addr1;
- u16 old_port0, new_port0, old_port1, new_port1;
- ip4_header_t *ip0, *ip1;
- udp_header_t *udp0, *udp1;
- tcp_header_t *tcp0, *tcp1;
- icmp46_header_t *icmp0, *icmp1;
- snat_session_t *s0 = 0, *s1 = 0;
- clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
- ip_csum_t sum0, sum1;
- snat_session_key_t e_key0, l_key0, e_key1, l_key1;
- lb_nat_type_t lb_nat0, lb_nat1;
- twice_nat_type_t twice_nat0, twice_nat1;
-
- /* Prefetch next iteration. */
- {
- vlib_buffer_t * p2, * p3;
-
- p2 = vlib_get_buffer (vm, from[2]);
- p3 = vlib_get_buffer (vm, from[3]);
-
- vlib_prefetch_buffer_header (p2, LOAD);
- vlib_prefetch_buffer_header (p3, LOAD);
-
- CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
- CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
- }
-
- /* speculatively enqueue b0 and b1 to the current next frame */
- to_next[0] = bi0 = from[0];
- to_next[1] = bi1 = from[1];
- from += 2;
- to_next += 2;
- n_left_from -= 2;
- n_left_to_next -= 2;
-
- b0 = vlib_get_buffer (vm, bi0);
- b1 = vlib_get_buffer (vm, bi1);
-
- next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
- vnet_buffer (b0)->snat.flags = 0;
- ip0 = vlib_buffer_get_current (b0);
-
- sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
- rx_fib_index0 = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
- sw_if_index0);
-
- if (PREDICT_FALSE(ip0->ttl == 1))
- {
- vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
- icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
- ICMP4_time_exceeded_ttl_exceeded_in_transit,
- 0);
- next0 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
- goto trace00;
- }
-
- udp0 = ip4_next_header (ip0);
- tcp0 = (tcp_header_t *) udp0;
- icmp0 = (icmp46_header_t *) udp0;
- proto0 = ip_proto_to_snat_proto (ip0->protocol);
-
- if (is_slow_path)
- {
- if (PREDICT_FALSE (proto0 == ~0))
- {
- s0 = nat44_ed_out2in_unknown_proto(sm, b0, ip0, rx_fib_index0,
- thread_index, now, vm, node);
- if (!sm->forwarding_enabled)
- {
- if (!s0)
- next0 = NAT44_ED_OUT2IN_NEXT_DROP;
- goto trace00;
- }
- }
-
- if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
- {
- next0 = icmp_out2in_ed_slow_path
- (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
- next0, now, thread_index, &s0);
- goto trace00;
- }
- }
- else
- {
- if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
- {
- next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
- goto trace00;
- }
-
- if (ip4_is_fragment (ip0))
- {
- b0->error = node->errors[SNAT_OUT2IN_ERROR_DROP_FRAGMENT];
- next0 = NAT44_ED_OUT2IN_NEXT_DROP;
- goto trace00;
- }
- }
-
- make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address, ip0->protocol,
- rx_fib_index0, udp0->dst_port, udp0->src_port);
-
- if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
- {
- if (is_slow_path)
- {
- /* Try to match static mapping by external address and port,
- destination address and port in packet */
- e_key0.addr = ip0->dst_address;
- e_key0.port = udp0->dst_port;
- e_key0.protocol = proto0;
- e_key0.fib_index = rx_fib_index0;
- if (snat_static_mapping_match(sm, e_key0, &l_key0, 1, 0,
- &twice_nat0, &lb_nat0, &ip0->src_address))
- {
- /*
- * Send DHCP packets to the ipv4 stack, or we won't
- * be able to use dhcp client on the outside interface
- */
- if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
- && (udp0->dst_port ==
- clib_host_to_net_u16(UDP_DST_PORT_dhcp_to_client))))
- {
- vnet_feature_next (&next0, b0);
- goto trace00;
- }
-
- if (!sm->forwarding_enabled)
- {
- b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
- next0 = NAT44_ED_OUT2IN_NEXT_DROP;
- }
- else
- {
- if (next_src_nat(sm, ip0, ip0->protocol,
- udp0->src_port, udp0->dst_port,
- thread_index, rx_fib_index0))
- {
- next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
- goto trace00;
- }
- create_bypass_for_fwd(sm, ip0, rx_fib_index0,
- thread_index);
- }
- goto trace00;
- }
-
- /* Create session initiated by host from external network */
- s0 = create_session_for_static_mapping_ed(sm, b0, l_key0,
- e_key0, node,
- thread_index,
- twice_nat0,
- lb_nat0,
- now);
-
- if (!s0)
- {
- next0 = NAT44_ED_OUT2IN_NEXT_DROP;
- goto trace00;
- }
- }
- else
- {
- next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
- goto trace00;
- }
- }
- else
- {
- s0 = pool_elt_at_index (tsm->sessions, value0.value);
- }
-
- old_addr0 = ip0->dst_address.as_u32;
- new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
- vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
-
- sum0 = ip0->checksum;
- sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
- dst_address);
- if (PREDICT_FALSE (is_twice_nat_session (s0)))
- sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
- s0->ext_host_nat_addr.as_u32, ip4_header_t,
- src_address);
- ip0->checksum = ip_csum_fold (sum0);
-
- if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
- {
- old_port0 = tcp0->dst_port;
- new_port0 = tcp0->dst_port = s0->in2out.port;
-
- sum0 = tcp0->checksum;
- sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
- dst_address);
- sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
- length);
- if (is_twice_nat_session (s0))
- {
- sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
- s0->ext_host_nat_addr.as_u32,
- ip4_header_t, dst_address);
- sum0 = ip_csum_update (sum0, tcp0->src_port,
- s0->ext_host_nat_port, ip4_header_t,
- length);
- tcp0->src_port = s0->ext_host_nat_port;
- ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
- }
- tcp0->checksum = ip_csum_fold(sum0);
- if (nat44_set_tcp_session_state_o2i (sm, s0, tcp0, thread_index))
- goto trace00;
- }
- else
- {
- udp0->dst_port = s0->in2out.port;
- if (is_twice_nat_session (s0))
- {
- udp0->src_port = s0->ext_host_nat_port;
- ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
- }
- udp0->checksum = 0;
- }
-
- /* Accounting */
- nat44_session_update_counters (s0, now,
- vlib_buffer_length_in_chain (vm, b0));
-
- trace00:
- if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
- && (b0->flags & VLIB_BUFFER_IS_TRACED)))
- {
- nat44_ed_out2in_trace_t *t =
- vlib_add_trace (vm, node, b0, sizeof (*t));
- t->is_slow_path = is_slow_path;
- t->sw_if_index = sw_if_index0;
- t->next_index = next0;
- t->session_index = ~0;
- if (s0)
- t->session_index = s0 - tsm->sessions;
- }
-
- pkts_processed += next0 != NAT44_ED_OUT2IN_NEXT_DROP;
-
- next1 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
- vnet_buffer (b1)->snat.flags = 0;
- ip1 = vlib_buffer_get_current (b1);
-
- sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
- rx_fib_index1 = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
- sw_if_index1);
-
- if (PREDICT_FALSE(ip1->ttl == 1))
- {
- vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
- icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
- ICMP4_time_exceeded_ttl_exceeded_in_transit,
- 0);
- next1 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
- goto trace01;
- }
-
- udp1 = ip4_next_header (ip1);
- tcp1 = (tcp_header_t *) udp1;
- icmp1 = (icmp46_header_t *) udp1;
- proto1 = ip_proto_to_snat_proto (ip1->protocol);
-
- if (is_slow_path)
- {
- if (PREDICT_FALSE (proto1 == ~0))
- {
- s1 = nat44_ed_out2in_unknown_proto(sm, b1, ip1, rx_fib_index1,
- thread_index, now, vm, node);
- if (!sm->forwarding_enabled)
- {
- if (!s1)
- next1 = NAT44_ED_OUT2IN_NEXT_DROP;
- goto trace01;
- }
- }
-
- if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
- {
- next1 = icmp_out2in_ed_slow_path
- (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
- next1, now, thread_index, &s1);
- goto trace01;
- }
- }
- else
- {
- if (PREDICT_FALSE (proto1 == ~0 || proto1 == SNAT_PROTOCOL_ICMP))
- {
- next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
- goto trace01;
- }
-
- if (ip4_is_fragment (ip1))
- {
- b1->error = node->errors[SNAT_OUT2IN_ERROR_DROP_FRAGMENT];
- next1 = NAT44_ED_OUT2IN_NEXT_DROP;
- goto trace01;
- }
- }
-
- make_ed_kv (&kv1, &ip1->dst_address, &ip1->src_address, ip1->protocol,
- rx_fib_index1, udp1->dst_port, udp1->src_port);
-
- if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv1, &value1))
- {
- if (is_slow_path)
- {
- /* Try to match static mapping by external address and port,
- destination address and port in packet */
- e_key1.addr = ip1->dst_address;
- e_key1.port = udp1->dst_port;
- e_key1.protocol = proto1;
- e_key1.fib_index = rx_fib_index1;
- if (snat_static_mapping_match(sm, e_key1, &l_key1, 1, 0,
- &twice_nat1, &lb_nat1, &ip1->src_address))
- {
- /*
- * Send DHCP packets to the ipv4 stack, or we won't
- * be able to use dhcp client on the outside interface
- */
- if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_UDP
- && (udp1->dst_port ==
- clib_host_to_net_u16(UDP_DST_PORT_dhcp_to_client))))
- {
- vnet_feature_next (&next1, b1);
- goto trace01;
- }
-
- if (!sm->forwarding_enabled)
- {
- b1->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
- next1 = NAT44_ED_OUT2IN_NEXT_DROP;
- }
- else
- {
- if (next_src_nat(sm, ip1, ip1->protocol,
- udp1->src_port, udp1->dst_port,
- thread_index, rx_fib_index1))
- {
- next1 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
- goto trace01;
- }
- create_bypass_for_fwd(sm, ip1, rx_fib_index1,
- thread_index);
- }
- goto trace01;
- }
-
- /* Create session initiated by host from external network */
- s1 = create_session_for_static_mapping_ed(sm, b1, l_key1,
- e_key1, node,
- thread_index,
- twice_nat1,
- lb_nat1,
- now);
-
- if (!s1)
- {
- next1 = NAT44_ED_OUT2IN_NEXT_DROP;
- goto trace01;
- }
- }
- else
- {
- next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
- goto trace01;
- }
- }
- else
- {
- s1 = pool_elt_at_index (tsm->sessions, value1.value);
- }
-
- old_addr1 = ip1->dst_address.as_u32;
- new_addr1 = ip1->dst_address.as_u32 = s1->in2out.addr.as_u32;
- vnet_buffer(b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
-
- sum1 = ip1->checksum;
- sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
- dst_address);
- if (PREDICT_FALSE (is_twice_nat_session (s1)))
- sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
- s1->ext_host_nat_addr.as_u32, ip4_header_t,
- src_address);
- ip1->checksum = ip_csum_fold (sum1);
-
- if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
- {
- old_port1 = tcp1->dst_port;
- new_port1 = tcp1->dst_port = s1->in2out.port;
-
- sum1 = tcp1->checksum;
- sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
- dst_address);
- sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
- length);
- if (is_twice_nat_session (s1))
- {
- sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
- s1->ext_host_nat_addr.as_u32,
- ip4_header_t, dst_address);
- sum1 = ip_csum_update (sum1, tcp1->src_port,
- s1->ext_host_nat_port, ip4_header_t,
- length);
- tcp1->src_port = s1->ext_host_nat_port;
- ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
- }
- tcp1->checksum = ip_csum_fold(sum1);
- if (nat44_set_tcp_session_state_o2i (sm, s1, tcp1, thread_index))
- goto trace01;
- }
- else
- {
- udp1->dst_port = s1->in2out.port;
- if (is_twice_nat_session (s1))
- {
- udp1->src_port = s1->ext_host_nat_port;
- ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
- }
- udp1->checksum = 0;
- }
-
- /* Accounting */
- nat44_session_update_counters (s1, now,
- vlib_buffer_length_in_chain (vm, b1));
-
- trace01:
- if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
- && (b1->flags & VLIB_BUFFER_IS_TRACED)))
- {
- nat44_ed_out2in_trace_t *t =
- vlib_add_trace (vm, node, b1, sizeof (*t));
- t->is_slow_path = is_slow_path;
- t->sw_if_index = sw_if_index1;
- t->next_index = next1;
- t->session_index = ~0;
- if (s1)
- t->session_index = s1 - tsm->sessions;
- }
-
- pkts_processed += next1 != NAT44_ED_OUT2IN_NEXT_DROP;
-
- /* verify speculative enqueues, maybe switch current next frame */
- vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
- to_next, n_left_to_next,
- bi0, bi1, next0, next1);
- }
-
- while (n_left_from > 0 && n_left_to_next > 0)