/* * Copyright (c) 2015 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 "map.h" #include #include #include typedef enum { IP6_MAPT_NEXT_MAPT_TCP_UDP, IP6_MAPT_NEXT_MAPT_ICMP, IP6_MAPT_NEXT_MAPT_FRAGMENTED, IP6_MAPT_NEXT_DROP, IP6_MAPT_N_NEXT } ip6_mapt_next_t; typedef enum { IP6_MAPT_ICMP_NEXT_IP4_LOOKUP, IP6_MAPT_ICMP_NEXT_IP4_FRAG, IP6_MAPT_ICMP_NEXT_DROP, IP6_MAPT_ICMP_N_NEXT } ip6_mapt_icmp_next_t; typedef enum { IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP, IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG, IP6_MAPT_TCP_UDP_NEXT_DROP, IP6_MAPT_TCP_UDP_N_NEXT } ip6_mapt_tcp_udp_next_t; typedef enum { IP6_MAPT_FRAGMENTED_NEXT_IP4_LOOKUP, IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG, IP6_MAPT_FRAGMENTED_NEXT_DROP, IP6_MAPT_FRAGMENTED_N_NEXT } ip6_mapt_fragmented_next_t; static_always_inline int ip6_map_fragment_cache (ip6_header_t * ip6, ip6_frag_hdr_t * frag, map_domain_t * d, u16 port) { u32 *ignore = NULL; map_ip4_reass_lock (); map_ip4_reass_t *r = map_ip4_reass_get (map_get_ip4 (&ip6->src_address, d->ip6_src_len), ip6_map_t_embedded_address (d, &ip6->dst_address), frag_id_6to4 (frag->identification), (ip6->protocol == IP_PROTOCOL_ICMP6) ? IP_PROTOCOL_ICMP : ip6->protocol, &ignore); if (r) r->port = port; map_ip4_reass_unlock (); return !r; } /* Returns the associated port or -1 */ static_always_inline i32 ip6_map_fragment_get (ip6_header_t * ip6, ip6_frag_hdr_t * frag, map_domain_t * d) { u32 *ignore = NULL; map_ip4_reass_lock (); map_ip4_reass_t *r = map_ip4_reass_get (map_get_ip4 (&ip6->src_address, d->ip6_src_len), ip6_map_t_embedded_address (d, &ip6->dst_address), frag_id_6to4 (frag->identification), (ip6->protocol == IP_PROTOCOL_ICMP6) ? IP_PROTOCOL_ICMP : ip6->protocol, &ignore); i32 ret = r ? r->port : -1; map_ip4_reass_unlock (); return ret; } typedef struct { map_domain_t *d; u16 sender_port; } icmp6_to_icmp_ctx_t; static int ip6_to_ip4_set_icmp_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *arg) { icmp6_to_icmp_ctx_t *ctx = arg; u32 ip4_sadr; // Security check // Note that this prevents an intermediate IPv6 router from answering // the request. ip4_sadr = map_get_ip4 (&ip6->src_address, ctx->d->flags); if (ip6->src_address.as_u64[0] != map_get_pfx_net (ctx->d, ip4_sadr, ctx->sender_port) || ip6->src_address.as_u64[1] != map_get_sfx_net (ctx->d, ip4_sadr, ctx->sender_port)) return -1; ip4->dst_address.as_u32 = ip6_map_t_embedded_address (ctx->d, &ip6->dst_address); ip4->src_address.as_u32 = ip4_sadr; return 0; } static int ip6_to_ip4_set_inner_icmp_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *arg) { icmp6_to_icmp_ctx_t *ctx = arg; u32 inner_ip4_dadr; //Security check of inner packet inner_ip4_dadr = map_get_ip4 (&ip6->dst_address, ctx->d->flags); if (ip6->dst_address.as_u64[0] != map_get_pfx_net (ctx->d, inner_ip4_dadr, ctx->sender_port) || ip6->dst_address.as_u64[1] != map_get_sfx_net (ctx->d, inner_ip4_dadr, ctx->sender_port)) return -1; ip4->dst_address.as_u32 = inner_ip4_dadr; ip4->src_address.as_u32 = ip6_map_t_embedded_address (ctx->d, &ip6->src_address); return 0; } static uword ip6_map_t_icmp (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { u32 n_left_from, *from, next_index, *to_next, n_left_to_next; vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, ip6_map_t_icmp_node.index); from = vlib_frame_vector_args (frame); n_left_from = frame->n_vectors; next_index = node->cached_next_index; vlib_combined_counter_main_t *cm = map_main.domain_counters; u32 thread_index = vm->thread_index; while (n_left_from > 0) { 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 pi0; vlib_buffer_t *p0; u8 error0; ip6_mapt_icmp_next_t next0; map_domain_t *d0; u16 len0; icmp6_to_icmp_ctx_t ctx0; ip6_header_t *ip60; pi0 = to_next[0] = from[0]; from += 1; n_left_from -= 1; to_next += 1; n_left_to_next -= 1; error0 = MAP_ERROR_NONE; next0 = IP6_MAPT_ICMP_NEXT_IP4_LOOKUP; p0 = vlib_get_buffer (vm, pi0); ip60 = vlib_buffer_get_current (p0); len0 = clib_net_to_host_u16 (ip60->payload_length); d0 = pool_elt_at_index (map_main.domains, vnet_buffer (p0)->map_t.map_domain_index); ctx0.sender_port = ip6_get_port (ip60, 0, p0->current_length); ctx0.d = d0; if (ctx0.sender_port == 0) { // In case of 1:1 mapping, we don't care about the port if (!(d0->ea_bits_len == 0 && d0->rules)) { error0 = MAP_ERROR_ICMP; goto err0; } } if (icmp6_to_icmp (p0, ip6_to_ip4_set_icmp_cb, &ctx0, ip6_to_ip4_set_inner_icmp_cb, &ctx0)) { error0 = MAP_ERROR_ICMP; goto err0; } if (vnet_buffer (p0)->map_t.mtu < p0->current_length) { // Send to fragmentation node if necessary vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu; vnet_buffer (p0)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP; next0 = IP6_MAPT_ICMP_NEXT_IP4_FRAG; } err0: if (PREDICT_TRUE (error0 == MAP_ERROR_NONE)) { vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX, thread_index, vnet_buffer (p0)-> map_t.map_domain_index, 1, len0); } else { next0 = IP6_MAPT_IC
/*
 * builtinurl.api - binary API skeleton
 *
 * Copyright (c) <current-year> <your-organization>
 * 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.
 */

/**
 * @file builtinurl.api
 * @brief VPP control-plane API messages.
 *
 * This file defines VPP control-plane binary API messages which are generally
 * called through a shared memory interface.
 */

/* Version and type recitations */

option version = "1.0.0";

/** @brief API to enable / disable builtinurl on an interface
    @param client_index - opaque cookie to identify the sender
    @param context - sender context, to match reply w/ request
    @param enable_disable - 1 to enable, 0 to disable the feature
    @param sw_if_index - interface handle
*/

autoreply define builtinurl_enable {
    /* Client identifier, set from api_main.my_client_index */
    u32 client_index;

    /* Arbitrary context, so client can match reply to request */
    u32 context;
};
offset + 6)); } else { // TODO: In case of 1:1 mapping, it might be possible to // do something with those packets. error0 = MAP_ERROR_BAD_PROTOCOL; } if (PREDICT_FALSE (map_port0 != -1) && (ip60->src_address.as_u64[0] != map_get_pfx_net (d0, vnet_buffer (p0)->map_t.v6.saddr, map_port0) || ip60->src_address.as_u64[1] != map_get_sfx_net (d0, vnet_buffer (p0)->map_t. v6.saddr, map_port0))) { // Security check when map_port0 is not zero (non-first // fragment, UDP or TCP) error0 = error0 == MAP_ERROR_NONE ? MAP_ERROR_SEC_CHECK : error0; } // Fragmented first packet needs to be cached for following packets if (PREDICT_FALSE (vnet_buffer (p0)->map_t.v6.frag_offset && !ip6_frag_hdr_offset ((ip6_frag_hdr_t *) u8_ptr_add (ip60, vnet_buffer (p0)->map_t. v6.frag_offset))) && (map_port0 != -1) && (d0->ea_bits_len != 0 || !d0->rules) && (error0 == MAP_ERROR_NONE)) { ip6_map_fragment_cache (ip60, (ip6_frag_hdr_t *) u8_ptr_add (ip60, vnet_buffer (p0)-> map_t.v6. frag_offset), d0, map_port0); } if (PREDICT_TRUE (error0 == MAP_ERROR_NONE && next0 != IP6_MAPT_NEXT_MAPT_ICMP)) { vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX, thread_index, vnet_buffer (p0)->map_t. map_domain_index, 1, clib_net_to_host_u16 (ip60-> payload_length)); } next0 = (error0 != MAP_ERROR_NONE) ? IP6_MAPT_NEXT_DROP : next0; p0->error = error_node->errors[error0]; exit: vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, n_left_to_next, pi0, next0); } vlib_put_next_frame (vm, node, next_index, n_left_to_next); } return frame->n_vectors; } static char *map_t_error_strings[] = { #define _(sym, string) string, foreach_map_error #undef _ }; /* *INDENT-OFF* */ VLIB_REGISTER_NODE(ip6_map_t_fragmented_node) = { .function = ip6_map_t_fragmented, .name = "ip6-map-t-fragmented", .vector_size = sizeof (u32), .format_trace = format_map_trace, .type = VLIB_NODE_TYPE_INTERNAL, .n_errors = MAP_N_ERROR, .error_strings = map_t_error_strings, .n_next_nodes = IP6_MAPT_FRAGMENTED_N_NEXT, .next_nodes = { [IP6_MAPT_FRAGMENTED_NEXT_IP4_LOOKUP] = "ip4-lookup", [IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME, [IP6_MAPT_FRAGMENTED_NEXT_DROP] = "error-drop", }, }; /* *INDENT-ON* */ /* *INDENT-OFF* */ VLIB_REGISTER_NODE(ip6_map_t_icmp_node) = { .function = ip6_map_t_icmp, .name = "ip6-map-t-icmp", .vector_size = sizeof (u32), .format_trace = format_map_trace, .type = VLIB_NODE_TYPE_INTERNAL, .n_errors = MAP_N_ERROR, .error_strings = map_t_error_strings, .n_next_nodes = IP6_MAPT_ICMP_N_NEXT, .next_nodes = { [IP6_MAPT_ICMP_NEXT_IP4_LOOKUP] = "ip4-lookup", [IP6_MAPT_ICMP_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME, [IP6_MAPT_ICMP_NEXT_DROP] = "error-drop", }, }; /* *INDENT-ON* */ /* *INDENT-OFF* */ VLIB_REGISTER_NODE(ip6_map_t_tcp_udp_node) = { .function = ip6_map_t_tcp_udp, .name = "ip6-map-t-tcp-udp", .vector_size = sizeof (u32), .format_trace = format_map_trace, .type = VLIB_NODE_TYPE_INTERNAL, .n_errors = MAP_N_ERROR, .error_strings = map_t_error_strings, .n_next_nodes = IP6_MAPT_TCP_UDP_N_NEXT, .next_nodes = { [IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP] = "ip4-lookup", [IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME, [IP6_MAPT_TCP_UDP_NEXT_DROP] = "error-drop", }, }; /* *INDENT-ON* */ /* *INDENT-OFF* */ VNET_FEATURE_INIT(ip4_map_t_feature, static) = { .arc_name = "ip6-unicast", .node_name = "ip6-map-t", .runs_before = VNET_FEATURES("ip6-flow-classify"), }; VLIB_REGISTER_NODE(ip6_map_t_node) = { .function = ip6_map_t, .name = "ip6-map-t", .vector_size = sizeof(u32), .format_trace = format_map_trace, .type = VLIB_NODE_TYPE_INTERNAL, .n_errors = MAP_N_ERROR, .error_strings = map_t_error_strings, .n_next_nodes = IP6_MAPT_N_NEXT, .next_nodes = { [IP6_MAPT_NEXT_MAPT_TCP_UDP] = "ip6-map-t-tcp-udp", [IP6_MAPT_NEXT_MAPT_ICMP] = "ip6-map-t-icmp", [IP6_MAPT_NEXT_MAPT_FRAGMENTED] = "ip6-map-t-fragmented", [IP6_MAPT_NEXT_DROP] = "error-drop", }, }; /* *INDENT-ON* */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */