/* * Copyright (c) 2017 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include vlib_node_registration_t dslite_out2in_node; typedef enum { DSLITE_OUT2IN_NEXT_IP4_LOOKUP, DSLITE_OUT2IN_NEXT_IP6_LOOKUP, DSLITE_OUT2IN_NEXT_DROP, DSLITE_OUT2IN_N_NEXT, } dslite_out2in_next_t; static char *dslite_out2in_error_strings[] = { #define _(sym,string) string, foreach_dslite_error #undef _ }; static inline u32 dslite_icmp_out2in (dslite_main_t * dm, ip4_header_t * ip4, dslite_session_t ** sp, u32 next, u8 * error, u32 thread_index) { dslite_session_t *s = 0; icmp46_header_t *icmp = ip4_next_header (ip4); clib_bihash_kv_8_8_t kv, value; snat_session_key_t key; u32 n = next; icmp_echo_header_t *echo; u32 new_addr, old_addr; u16 old_id, new_id; ip_csum_t sum; echo = (icmp_echo_header_t *) (icmp + 1); if (icmp_is_error_message (icmp) || (icmp->type != ICMP4_echo_reply)) { n = DSLITE_OUT2IN_NEXT_DROP; *error = DSLITE_ERROR_BAD_ICMP_TYPE; goto done; } key.addr = ip4->dst_address; key.port = echo->identifier; key.protocol = SNAT_PROTOCOL_ICMP; key.fib_index = 0; kv.key = key.as_u64; if (clib_bihash_search_8_8 (&dm->per_thread_data[thread_index].out2in, &kv, &value)) { next = DSLITE_OUT2IN_NEXT_DROP; *error = DSLITE_ERROR_NO_TRANSLATION; goto done; } else { s = pool_elt_at_index (dm->per_thread_data[thread_index].sessions, value.value); } old_id = echo->identifier; echo->identifier = new_id = s->in2out.port; sum = icmp->checksum; sum = ip_csum_update (sum, old_id, new_id, icmp_echo_header_t, identifier); icmp->checksum = ip_csum_fold (sum); old_addr = ip4->dst_address.as_u32; ip4->dst_address = s->in2out.addr; new_addr = ip4->dst_address.as_u32; sum = ip4->checksum; sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address); ip4->checksum = ip_csum_fold (sum); done: *sp = s; return n; } static uword dslite_out2in_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { u32 n_left_from, *from, *to_next; dslite_out2in_next_t next_index; vlib_node_runtime_t *error_node; u32 thread_index = vm->thread_index; f64 now = vlib_time_now (vm); dslite_main_t *dm = &dslite_main; error_node = vlib_node_get_runtime (vm, dslite_out2in_node.index); from = vlib_frame_vector_args (frame); n_left_from = frame->n_vectors; next_index = node->cached_next_index; while (n_left_from > 0) { u32 n_left_to_next; vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); while (n_left_from > 0 && n_left_to_next > 0) { u32 bi0; vlib_buffer_t *b0; u32 next0 = DSLITE_OUT2IN_NEXT_IP6_LOOKUP; u8 error0 = DSLITE_ERROR_OUT2IN; ip4_header_t *ip40; ip6_header_t *ip60; u32 proto0; udp_header_t *udp0; tcp_header_t *tcp0; clib_bihash_kv_8_8_t kv0, value0; snat_session_key_t key0; dslite_session_t *s0 = 0; ip_csum_t sum0; u32 new_addr0, old_addr0; u16 new_port0, old_port0; /* speculatively enqueue b0 to the current next frame */ bi0 = from[0]; to_next[0] = bi0; from += 1; to_next += 1; n_left_from -= 1; n_left_to_next -= 1; b0 = vlib_get_buffer (vm, bi0); ip40 = vlib_buffer_get_current (b0); proto0 = ip_proto_to_snat_proto (ip40->protocol); if (PREDICT_FALSE (proto0 == ~0)) { error0 = DSLITE_ERROR_UNSUPPORTED_PROTOCOL; next0 = DSLITE_OUT2IN_NEXT_DROP; goto trace0; } if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP)) { next0 = dslite_icmp_out2in (dm, ip40, &s0, next0, &error0, thread_index); if (PREDICT_FALSE (next0 == DSLITE_OUT2IN_NEXT_DROP)) goto trace0; goto encap0; } udp0 = ip4_next_header (ip40); tcp0 = (tcp_header_t *) udp0; key0.addr = ip40->dst_address; key0.port = udp0->dst_port; key0.protocol = proto0; key0.fib_index = 0; kv0.key = key0.as_u64; if (clib_bihash_search_8_8 (&dm->per_thread_data[thread_index].out2in, &kv0, &value0)) { next0 = DSLITE_OUT2IN_NEXT_DROP; error0 = DSLITE_ERROR_NO_TRANSLATION; goto trace0; } else { s0 = pool_elt_at_index (dm->per_thread_data[thread_index].sessions, value0.value); } old_addr0 = ip40->dst_address.as_u32; ip40->dst_address = s0->in2out.addr; new_addr0 = ip40->dst_address.as_u32; sum0 = ip40->checksum; sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address); ip40->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); sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t, length); tcp0->checksum = ip_csum_fold (sum0); } else { old_port0 = udp0->dst_port; udp0->dst_port = s0->in2out.port; udp0->checksum = 0; } encap0: /* Construct IPv6 header */ vlib_buffer_advance (b0, -(sizeof (ip6_header