- 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 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
- goto trace0;
- }
-
- proto0 = ip_proto_to_snat_proto (ip0->protocol);
-
- if (PREDICT_FALSE (proto0 == ~0))
- {
- snat_out2in_unknown_proto(sm, b0, ip0, rx_fib_index0,
- thread_index, now, vm);
- goto trace0;
- }
-
- if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
- {
- next0 = icmp_out2in_slow_path
- (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
- next0, now, thread_index, &s0);
- goto trace0;
- }
-
- key0.addr = ip0->dst_address;
- key0.port = udp0->dst_port;
- key0.protocol = proto0;
- key0.fib_index = rx_fib_index0;
-
- kv0.key = key0.as_u64;
-
- if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in,
- &kv0, &value0))
- {
- /* Try to match static mapping by external address and port,
- destination address and port in packet */
- if (snat_static_mapping_match(sm, key0, &sm0, 1, 0))
- {
- b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
- /*
- * Send DHCP packets to the ipv4 stack, or we won't
- * be able to use dhcp client on the outside interface
- */
- if (proto0 != SNAT_PROTOCOL_UDP
- || (udp0->dst_port
- != clib_host_to_net_u16(UDP_DST_PORT_dhcp_to_client)))
- next0 = SNAT_OUT2IN_NEXT_DROP;
- goto trace0;
- }
-
- /* Create session initiated by host from external network */
- s0 = create_session_for_static_mapping(sm, b0, sm0, key0, node,
- thread_index);
- if (!s0)
- {
- b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
- next0 = SNAT_OUT2IN_NEXT_DROP;
- goto trace0;
- }
- }
- else
- {
- if (PREDICT_FALSE (value0.value == ~0ULL))
- {
- s0 = snat_out2in_lb(sm, b0, ip0, rx_fib_index0, thread_index, now,
- vm);
- goto trace0;
- }
- else
- {
- s0 = pool_elt_at_index (
- sm->per_thread_data[thread_index].sessions,
- value0.value);
- }
- }
-
- old_addr0 = ip0->dst_address.as_u32;
- ip0->dst_address = s0->in2out.addr;
- new_addr0 = ip0->dst_address.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 /* changed member */);
- ip0->checksum = ip_csum_fold (sum0);
-
- if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
- {
- old_port0 = tcp0->dst_port;
- tcp0->dst_port = s0->in2out.port;
- new_port0 = tcp0->dst_port;
-
- sum0 = tcp0->checksum;
- sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
- ip4_header_t,
- dst_address /* changed member */);
-
- sum0 = ip_csum_update (sum0, old_port0, new_port0,
- ip4_header_t /* cheat */,
- length /* changed member */);
- tcp0->checksum = ip_csum_fold(sum0);
- }
- else
- {
- old_port0 = udp0->dst_port;
- udp0->dst_port = s0->in2out.port;
- udp0->checksum = 0;
- }
-
- /* Accounting */
- s0->last_heard = now;
- s0->total_pkts++;
- s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
- /* Per-user LRU list maintenance for dynamic translation */
- if (!snat_is_session_static (s0))
- {
- clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
- s0->per_user_index);
- clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
- s0->per_user_list_head_index,
- s0->per_user_index);
- }
- trace0:
-
- if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
- && (b0->flags & VLIB_BUFFER_IS_TRACED)))
- {
- snat_out2in_trace_t *t =
- vlib_add_trace (vm, node, b0, sizeof (*t));
- t->sw_if_index = sw_if_index0;
- t->next_index = next0;
- t->session_index = ~0;
- if (s0)
- t->session_index = s0 - sm->per_thread_data[thread_index].sessions;
- }
-
- pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
-
-
- ip1 = vlib_buffer_get_current (b1);
- udp1 = ip4_next_header (ip1);
- tcp1 = (tcp_header_t *) udp1;
- icmp1 = (icmp46_header_t *) udp1;
-
- sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
- rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
- 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 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
- goto trace1;
- }
-
- proto1 = ip_proto_to_snat_proto (ip1->protocol);
-
- if (PREDICT_FALSE (proto1 == ~0))
- {
- snat_out2in_unknown_proto(sm, b1, ip1, rx_fib_index1,
- thread_index, now, vm);
- goto trace1;
- }
-
- if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
- {
- next1 = icmp_out2in_slow_path
- (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
- next1, now, thread_index, &s1);
- goto trace1;
- }
-
- key1.addr = ip1->dst_address;
- key1.port = udp1->dst_port;
- key1.protocol = proto1;
- key1.fib_index = rx_fib_index1;
-
- kv1.key = key1.as_u64;
-
- if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in,
- &kv1, &value1))
- {
- /* Try to match static mapping by external address and port,
- destination address and port in packet */
- if (snat_static_mapping_match(sm, key1, &sm1, 1, 0))
- {
- b1->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
- /*
- * Send DHCP packets to the ipv4 stack, or we won't
- * be able to use dhcp client on the outside interface
- */
- if (proto1 != SNAT_PROTOCOL_UDP
- || (udp1->dst_port
- != clib_host_to_net_u16(UDP_DST_PORT_dhcp_to_client)))
- next1 = SNAT_OUT2IN_NEXT_DROP;
- goto trace1;
- }
-
- /* Create session initiated by host from external network */
- s1 = create_session_for_static_mapping(sm, b1, sm1, key1, node,
- thread_index);
- if (!s1)
- {
- b1->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
- next1 = SNAT_OUT2IN_NEXT_DROP;
- goto trace1;
- }
- }
- else
- {
- if (PREDICT_FALSE (value1.value == ~0ULL))
- {
- s1 = snat_out2in_lb(sm, b1, ip1, rx_fib_index1, thread_index, now,
- vm);
- goto trace1;
- }
- else
- {
- s1 = pool_elt_at_index (
- sm->per_thread_data[thread_index].sessions,
- value1.value);
- }
- }
-
- old_addr1 = ip1->dst_address.as_u32;
- ip1->dst_address = s1->in2out.addr;
- new_addr1 = ip1->dst_address.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 /* changed member */);
- ip1->checksum = ip_csum_fold (sum1);
-
- if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
- {
- old_port1 = tcp1->dst_port;
- tcp1->dst_port = s1->in2out.port;
- new_port1 = tcp1->dst_port;
-
- sum1 = tcp1->checksum;
- sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
- ip4_header_t,
- dst_address /* changed member */);
-
- sum1 = ip_csum_update (sum1, old_port1, new_port1,
- ip4_header_t /* cheat */,
- length /* changed member */);
- tcp1->checksum = ip_csum_fold(sum1);
- }
- else
- {
- old_port1 = udp1->dst_port;
- udp1->dst_port = s1->in2out.port;
- udp1->checksum = 0;
- }
-
- /* Accounting */
- s1->last_heard = now;
- s1->total_pkts++;
- s1->total_bytes += vlib_buffer_length_in_chain (vm, b1);
- /* Per-user LRU list maintenance for dynamic translation */
- if (!snat_is_session_static (s1))
- {
- clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
- s1->per_user_index);
- clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
- s1->per_user_list_head_index,
- s1->per_user_index);
- }
- trace1:
-
- if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
- && (b1->flags & VLIB_BUFFER_IS_TRACED)))
- {
- snat_out2in_trace_t *t =
- vlib_add_trace (vm, node, b1, sizeof (*t));
- t->sw_if_index = sw_if_index1;
- t->next_index = next1;
- t->session_index = ~0;
- if (s1)
- t->session_index = s1 - sm->per_thread_data[thread_index].sessions;
- }
-
- pkts_processed += next1 != SNAT_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);
- }