aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/snat/nat64_out2in.c
diff options
context:
space:
mode:
authorMatus Fabian <matfabia@cisco.com>2017-06-06 04:53:28 -0700
committerOle Trøan <otroan@employees.org>2017-06-08 11:39:53 +0000
commit06596c54dc51e35222737b01c617cc675505a260 (patch)
tree0ee02baf650e9ab217ee4c3c64913dfac6ce68ff /src/plugins/snat/nat64_out2in.c
parent2fcc49d9c6a66c49148d5790f6af38ab2f3b4be6 (diff)
NAT64: Add NAT64 support for snat plugin (VPP-699)
Basic NAT64 feature (no hairpinning, no multi-thread). Change-Id: I392fccbce93e70c117f4a9a7ec7cf08d6c537f2d Signed-off-by: Matus Fabian <matfabia@cisco.com>
Diffstat (limited to 'src/plugins/snat/nat64_out2in.c')
-rw-r--r--src/plugins/snat/nat64_out2in.c348
1 files changed, 348 insertions, 0 deletions
diff --git a/src/plugins/snat/nat64_out2in.c b/src/plugins/snat/nat64_out2in.c
new file mode 100644
index 00000000000..3eed9974af2
--- /dev/null
+++ b/src/plugins/snat/nat64_out2in.c
@@ -0,0 +1,348 @@
+/*
+ * 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.
+ */
+/**
+ * @file
+ * @brief NAT64 IPv4 to IPv6 translation (otside to inside network)
+ */
+
+#include <snat/nat64.h>
+#include <vnet/ip/ip4_to_ip6.h>
+#include <vnet/fib/ip4_fib.h>
+
+/* *INDENT-OFF* */
+static u8 well_known_prefix[] = {
+ 0x00, 0x64, 0xff, 0x9b,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00
+ };
+/* *INDENT-ON* */
+typedef struct
+{
+ u32 sw_if_index;
+ u32 next_index;
+} nat64_out2in_trace_t;
+
+static u8 *
+format_nat64_out2in_trace (u8 * s, va_list * args)
+{
+ CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
+ CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
+ nat64_out2in_trace_t *t = va_arg (*args, nat64_out2in_trace_t *);
+
+ s =
+ format (s, "NAT64-out2in: sw_if_index %d, next index %d", t->sw_if_index,
+ t->next_index);
+
+ return s;
+}
+
+vlib_node_registration_t nat64_out2in_node;
+
+#define foreach_nat64_out2in_error \
+_(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
+_(OUT2IN_PACKETS, "Good out2in packets processed") \
+_(NO_TRANSLATION, "No translation") \
+_(UNKNOWN, "unknown")
+
+typedef enum
+{
+#define _(sym,str) NAT64_OUT2IN_ERROR_##sym,
+ foreach_nat64_out2in_error
+#undef _
+ NAT64_OUT2IN_N_ERROR,
+} nat64_out2in_error_t;
+
+static char *nat64_out2in_error_strings[] = {
+#define _(sym,string) string,
+ foreach_nat64_out2in_error
+#undef _
+};
+
+typedef enum
+{
+ NAT64_OUT2IN_NEXT_LOOKUP,
+ NAT64_OUT2IN_NEXT_DROP,
+ NAT64_OUT2IN_N_NEXT,
+} nat64_out2in_next_t;
+
+typedef struct nat64_out2in_set_ctx_t_
+{
+ vlib_buffer_t *b;
+ vlib_main_t *vm;
+} nat64_out2in_set_ctx_t;
+
+static int
+nat64_out2in_tcp_udp_set_cb (ip4_header_t * ip4, ip6_header_t * ip6,
+ void *arg)
+{
+ nat64_main_t *nm = &nat64_main;
+ nat64_out2in_set_ctx_t *ctx = arg;
+ nat64_db_bib_entry_t *bibe;
+ nat64_db_st_entry_t *ste;
+ ip46_address_t saddr, daddr;
+ ip6_address_t ip6_saddr;
+ udp_header_t *udp = ip4_next_header (ip4);
+ snat_protocol_t proto = ip_proto_to_snat_proto (ip4->protocol);
+ u16 dport = udp->dst_port;
+ u16 sport = udp->src_port;
+ u32 sw_if_index, fib_index;
+
+ sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
+ fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
+
+ memset (&saddr, 0, sizeof (saddr));
+ saddr.ip4.as_u32 = ip4->src_address.as_u32;
+ memset (&daddr, 0, sizeof (daddr));
+ daddr.ip4.as_u32 = ip4->dst_address.as_u32;
+
+ memcpy (&ip6_saddr, well_known_prefix, sizeof (ip6_saddr));
+ ip6_saddr.as_u32[3] = ip4->src_address.as_u32;
+
+ ste =
+ nat64_db_st_entry_find (&nm->db, &daddr, &saddr, dport, sport, proto,
+ fib_index, 0);
+ if (ste)
+ {
+ bibe = nat64_db_bib_entry_by_index (&nm->db, proto, ste->bibe_index);
+ if (!bibe)
+ return -1;
+ }
+ else
+ {
+ bibe =
+ nat64_db_bib_entry_find (&nm->db, &daddr, dport, proto, fib_index, 0);
+
+ if (!bibe)
+ return -1;
+
+ ste =
+ nat64_db_st_entry_create (&nm->db, bibe, &ip6_saddr, &saddr.ip4,
+ sport);
+ }
+
+ nat64_session_reset_timeout (ste, ctx->vm);
+
+ ip6->src_address.as_u64[0] = ip6_saddr.as_u64[0];
+ ip6->src_address.as_u64[1] = ip6_saddr.as_u64[1];
+
+ ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
+ ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
+ udp->dst_port = bibe->in_port;
+
+ return 0;
+}
+
+static int
+nat64_out2in_icmp_set_cb (ip4_header_t * ip4, ip6_header_t * ip6, void *arg)
+{
+ nat64_main_t *nm = &nat64_main;
+ nat64_out2in_set_ctx_t *ctx = arg;
+ nat64_db_bib_entry_t *bibe;
+ nat64_db_st_entry_t *ste;
+ ip46_address_t saddr, daddr;
+ ip6_address_t ip6_saddr;
+ u32 sw_if_index, fib_index;
+ icmp46_header_t *icmp = ip4_next_header (ip4);
+
+ sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
+ fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
+
+ memset (&saddr, 0, sizeof (saddr));
+ saddr.ip4.as_u32 = ip4->src_address.as_u32;
+ memset (&daddr, 0, sizeof (daddr));
+ daddr.ip4.as_u32 = ip4->dst_address.as_u32;
+
+ memcpy (&ip6_saddr, well_known_prefix, sizeof (ip6_saddr));
+ ip6_saddr.as_u32[3] = ip4->src_address.as_u32;
+
+ if (icmp->type == ICMP6_echo_request || icmp->type == ICMP6_echo_reply)
+ {
+ u16 out_id = ((u16 *) (icmp))[2];
+ ste =
+ nat64_db_st_entry_find (&nm->db, &daddr, &saddr, out_id, 0,
+ SNAT_PROTOCOL_ICMP, fib_index, 0);
+
+ if (ste)
+ {
+ bibe =
+ nat64_db_bib_entry_by_index (&nm->db, SNAT_PROTOCOL_ICMP,
+ ste->bibe_index);
+ if (!bibe)
+ return -1;
+ }
+ else
+ {
+ bibe =
+ nat64_db_bib_entry_find (&nm->db, &daddr, out_id,
+ SNAT_PROTOCOL_ICMP, fib_index, 0);
+ if (!bibe)
+ return -1;
+
+ ste =
+ nat64_db_st_entry_create (&nm->db, bibe, &ip6_saddr, &saddr.ip4,
+ 0);
+ }
+
+ nat64_session_reset_timeout (ste, ctx->vm);
+
+ ip6->src_address.as_u64[0] = ip6_saddr.as_u64[0];
+ ip6->src_address.as_u64[1] = ip6_saddr.as_u64[1];
+
+ ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
+ ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
+ ((u16 *) (icmp))[2] = bibe->in_port;
+
+ }
+ else
+ {
+ //TODO: ICMP error
+ clib_warning ("not ICMP echo request/reply, %u", icmp->type);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int
+nat64_out2in_inner_icmp_set_cb (ip4_header_t * ip4, ip6_header_t * ip6,
+ void *ctx)
+{
+ //TODO:
+ return -1;
+}
+
+static uword
+nat64_out2in_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
+ vlib_frame_t * frame)
+{
+ u32 n_left_from, *from, *to_next;
+ nat64_out2in_next_t next_index;
+ u32 pkts_processed = 0;
+
+ 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;
+ ip4_header_t *ip40;
+ u32 proto0;
+ nat64_out2in_set_ctx_t ctx0;
+
+ /* 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);
+
+ ctx0.b = b0;
+ ctx0.vm = vm;
+
+ next0 = NAT64_OUT2IN_NEXT_LOOKUP;
+
+ proto0 = ip_proto_to_snat_proto (ip40->protocol);
+ if (PREDICT_FALSE (proto0 == ~0))
+ {
+ next0 = NAT64_OUT2IN_NEXT_DROP;
+ b0->error =
+ node->errors[NAT64_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
+ goto trace0;
+ }
+
+ if (proto0 == SNAT_PROTOCOL_ICMP)
+ {
+ if (icmp_to_icmp6
+ (b0, nat64_out2in_icmp_set_cb, &ctx0,
+ nat64_out2in_inner_icmp_set_cb, &ctx0))
+ {
+ next0 = NAT64_OUT2IN_NEXT_DROP;
+ b0->error = node->errors[NAT64_OUT2IN_ERROR_NO_TRANSLATION];
+ goto trace0;
+ }
+ }
+ else
+ {
+ if (ip4_to_ip6_tcp_udp (b0, nat64_out2in_tcp_udp_set_cb, &ctx0))
+ {
+ next0 = NAT64_OUT2IN_NEXT_DROP;
+ b0->error = node->errors[NAT64_OUT2IN_ERROR_NO_TRANSLATION];
+ goto trace0;
+ }
+ }
+
+ trace0:
+ if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
+ && (b0->flags & VLIB_BUFFER_IS_TRACED)))
+ {
+ nat64_out2in_trace_t *t =
+ vlib_add_trace (vm, node, b0, sizeof (*t));
+ t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
+ t->next_index = next0;
+ }
+
+ pkts_processed += next0 != NAT64_OUT2IN_NEXT_DROP;
+
+ /* verify speculative enqueue, maybe switch current next frame */
+ vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
+ n_left_to_next, bi0, next0);
+ }
+ vlib_put_next_frame (vm, node, next_index, n_left_to_next);
+ }
+ vlib_node_increment_counter (vm, nat64_out2in_node.index,
+ NAT64_OUT2IN_ERROR_OUT2IN_PACKETS,
+ pkts_processed);
+ return frame->n_vectors;
+}
+
+/* *INDENT-OFF* */
+VLIB_REGISTER_NODE (nat64_out2in_node) = {
+ .function = nat64_out2in_node_fn,
+ .name = "nat64-out2in",
+ .vector_size = sizeof (u32),
+ .format_trace = format_nat64_out2in_trace,
+ .type = VLIB_NODE_TYPE_INTERNAL,
+ .n_errors = ARRAY_LEN (nat64_out2in_error_strings),
+ .error_strings = nat64_out2in_error_strings,.n_next_nodes = 2,
+ /* edit / add dispositions here */
+ .next_nodes = {
+ [NAT64_OUT2IN_NEXT_DROP] = "error-drop",
+ [NAT64_OUT2IN_NEXT_LOOKUP] = "ip6-lookup",
+ },
+};
+/* *INDENT-ON* */
+
+VLIB_NODE_FUNCTION_MULTIARCH (nat64_out2in_node, nat64_out2in_node_fn);
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
">f->min_root); } static void fheap_validate (fheap_t * f) { fheap_node_t *n, *m; uword ni, si; if (!CLIB_DEBUG || !f->enable_validate) return; vec_foreach_index (ni, f->nodes) { n = vec_elt_at_index (f->nodes, ni); if (!n->is_valid) continue; /* Min root must have minimal key. */ m = vec_elt_at_index (f->nodes, f->min_root); ASSERT (n->key >= m->key); /* Min root must have no parent. */ if (ni == f->min_root) ASSERT (n->parent == ~0); /* Check sibling linkages. */ if (n->next_sibling == ~0) ASSERT (n->prev_sibling == ~0); else if (n->prev_sibling == ~0) ASSERT (n->next_sibling == ~0); else { fheap_node_t *prev, *next; u32 si = n->next_sibling, si_start = si; do { m = vec_elt_at_index (f->nodes, si); prev = vec_elt_at_index (f->nodes, m->prev_sibling); next = vec_elt_at_index (f->nodes, m->next_sibling); ASSERT (prev->next_sibling == si); ASSERT (next->prev_sibling == si); si = m->next_sibling; } while (si != si_start); } /* Loop through all siblings. */ { u32 n_siblings = 0; foreach_fheap_node_sibling (f, si, n->next_sibling, ( { m = vec_elt_at_index (f->nodes, si); /* All siblings must have same parent. */ ASSERT (m->parent == n-> parent); n_siblings += 1;} )); /* Either parent is non-empty or there are siblings present. */ if (n->parent == ~0 && ni != f->min_root) ASSERT (n_siblings > 0); } /* Loop through all children. */ { u32 found_first_child = n->first_child == ~0; u32 n_children = 0; foreach_fheap_node_sibling (f, si, n->first_child, ( { m = vec_elt_at_index (f->nodes, si); /* Children must have larger keys than their parent. */ ASSERT (m->key >= n->key); if (!found_first_child) found_first_child = si == n->first_child; n_children += 1;} )); /* Check that first child is present on list. */ ASSERT (found_first_child); /* Make sure rank is correct. */ ASSERT (n->rank == n_children); } } /* Increment serial number for each successful validate. Failure can be used as condition for gdb breakpoints. */ f->validate_serial++; } always_inline void fheap_node_add_sibling (fheap_t * f, u32 ni, u32 ni_to_add) { fheap_node_t *n = vec_elt_at_index (f->nodes, ni); fheap_node_t *n_to_add = vec_elt_at_index (f->nodes, ni_to_add); fheap_node_t *n_next = fheap_get_node (f, n->next_sibling); fheap_node_t *parent; /* Empty list? */ if (n->next_sibling == ~0) { ASSERT (n->prev_sibling == ~0); n->next_sibling = n->prev_sibling = ni_to_add; n_to_add->next_sibling = n_to_add->prev_sibling = ni; } else { /* Add node after existing node. */ n_to_add->prev_sibling = ni; n_to_add->next_sibling = n->next_sibling; n->next_sibling = ni_to_add; n_next->prev_sibling = ni_to_add; } n_to_add->parent = n->parent; parent = fheap_get_node (f, n->parent); if (parent) parent->rank += 1; } void fheap_add (fheap_t * f, u32 ni, u32 key) { fheap_node_t *r, *n; u32 ri; n = vec_elt_at_index (f->nodes, ni); memset (n, 0, sizeof (n[0])); n->parent = n->first_child = n->next_sibling = n->prev_sibling = ~0; n->key = key; r = fheap_get_root (f); ri = f->min_root; if (!r) { /* No root? Add node as new root. */ f->min_root = ni; } else { /* Add node as sibling of current root. */ fheap_node_add_sibling (f, ri, ni); /* New node may become new root. */ if (r->key > n->key) f->min_root = ni; } fheap_validate (f); } always_inline u32 fheap_node_remove_internal (fheap_t * f, u32 ni, u32 invalidate) { fheap_node_t *n = vec_elt_at_index (f->nodes, ni); u32 prev_ni = n->prev_sibling; u32 next_ni = n->next_sibling; u32 list_has_single_element = prev_ni == ni; fheap_node_t *prev = fheap_get_node (f, prev_ni); fheap_node_t *next = fheap_get_node (f, next_ni); fheap_node_t *p = fheap_get_node (f, n->parent); if (p) { ASSERT (p->rank > 0); p->rank -= 1; p->first_child = list_has_single_element ? ~0 : next_ni; } if (prev) { ASSERT (prev->next_sibling == ni); prev->next_sibling = next_ni; } if (next) { ASSERT (next->prev_sibling == ni); next->prev_sibling = prev_ni; } n->prev_sibling = n->next_sibling = ni; n->parent = ~0; n->is_valid = invalidate == 0; return list_has_single_element ? ~0 : next_ni; } always_inline u32 fheap_node_remove (fheap_t * f, u32 ni) { return fheap_node_remove_internal (f, ni, /* invalidate */ 0); } always_inline u32 fheap_node_remove_and_invalidate (fheap_t * f, u32 ni) { return fheap_node_remove_internal (f, ni, /* invalidate */ 1); } static void fheap_link_root (fheap_t * f, u32 ni) { fheap_node_t *n = vec_elt_at_index (f->nodes, ni); fheap_node_t *r, *lo, *hi; u32 ri, lo_i, hi_i, k; while (1) { k = n->rank; vec_validate_init_empty (f->root_list_by_rank, k, ~0); ri = f->root_list_by_rank[k]; r = fheap_get_node (f, ri); if (!r) { f->root_list_by_rank[k] = ni; return; } f->root_list_by_rank[k] = ~0; /* Sort n/r into lo/hi by their keys. */ lo = r, lo_i = ri; hi = n, hi_i = ni; if (hi->key < lo->key) { u32 ti; fheap_node_t *tn; ti = lo_i, tn = lo; lo = hi, lo_i = hi_i; hi = tn, hi_i = ti; } /* Remove larger key. */ fheap_node_remove (f, hi_i); /* Add larger key as child of smaller one. */ if (lo->first_child == ~0) { hi->parent = lo_i; lo->first_child = hi_i; lo->rank = 1; } else fheap_node_add_sibling (f, lo->first_child, hi_i); /* Following Fredman & Trajan: "When making a root node X a child of another node in a linking step, we unmark X". */ hi->is_marked = 0; ni = lo_i; n = lo; } } u32 fheap_del_min (fheap_t * f, u32 * min_key) { fheap_node_t *r = fheap_get_root (f); u32 to_delete_min_ri = f->min_root; u32 ri, ni; /* Empty heap? */ if (!r) return ~0; /* Root's children become siblings. Call this step a; see below. */ if (r->first_child != ~0) { u32 ci, cni, rni; fheap_node_t *c, *cn, *rn; /* Splice child & root circular lists together. */ ci = r->first_child; c = vec_elt_at_index (f->nodes, ci); cni = c->next_sibling; rni = r->next_sibling; cn = vec_elt_at_index (f->nodes, cni); rn = vec_elt_at_index (f->nodes, rni); r->next_sibling = cni; c->next_sibling = rni; cn->prev_sibling = to_delete_min_ri; rn->prev_sibling = ci; } /* Remove min root. */ ri = fheap_node_remove_and_invalidate (f, to_delete_min_ri); /* Find new min root from among siblings including the ones we've just added. */ f->min_root = ~0; if (ri != ~0) { u32 ri_last, ri_next, i, min_ds; r = fheap_get_node (f, ri); ri_last = r->prev_sibling; while (1) { /* Step a above can put children (with r->parent != ~0) on root list. */ r->parent = ~0; ri_next = r->next_sibling; fheap_link_root (f, ri); if (ri == ri_last) break; ri = ri_next; r = fheap_get_node (f, ri); } min_ds = ~0; vec_foreach_index (i, f->root_list_by_rank) { ni = f->root_list_by_rank[i]; if (ni == ~0) continue; f->root_list_by_rank[i] = ~0; r = fheap_get_node (f, ni); if (r->key < min_ds) { f->min_root = ni; min_ds = r->key; ASSERT (r->parent == ~0); } } } /* Return deleted min root. */ r = vec_elt_at_index (f->nodes, to_delete_min_ri); if (min_key) *min_key = r->key; fheap_validate (f); return to_delete_min_ri; } static void fheap_mark_parent (fheap_t * f, u32 pi) { fheap_node_t *p = vec_elt_at_index (f->nodes, pi); /* Parent is a root: do nothing. */ if (p->parent == ~0) return; /* If not marked, mark it. */ if (!p->is_marked) { p->is_marked = 1; return; } /* Its a previously marked, non-root parent. Cut edge to its parent and add to root list. */ fheap_node_remove (f, pi); fheap_node_add_sibling (f, f->min_root, pi); /* Unmark it since its now a root node. */ p->is_marked = 0; /* "Cascading cuts": check parent. */ if (p->parent != ~0) fheap_mark_parent (f, p->parent); } /* Set key to new smaller value. */ void fheap_decrease_key (fheap_t * f, u32 ni, u32 new_key) { fheap_node_t *n = vec_elt_at_index (f->nodes, ni); fheap_node_t *r = fheap_get_root (f); n->key = new_key; if (n->parent != ~0) { fheap_mark_parent (f, n->parent); /* Remove node and add to root list. */ fheap_node_remove (f, ni); fheap_node_add_sibling (f, f->min_root, ni); } if (n->key < r->key) f->min_root = ni; fheap_validate (f); } void fheap_del (fheap_t * f, u32 ni) { fheap_node_t *n; n = vec_elt_at_index (f->nodes, ni); if (n->parent == ~0) { ASSERT (ni == f->min_root); fheap_del_min (f, 0); } else { u32 ci; fheap_mark_parent (f, n->parent); /* Add children to root list. */ foreach_fheap_node_sibling (f, ci, n->first_child, ( { fheap_node_remove (f, ci); fheap_node_add_sibling (f, f->min_root, ci);} )); fheap_node_remove_and_invalidate (f, ni); } fheap_validate (f); } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */