diff options
author | Matus Fabian <matfabia@cisco.com> | 2017-10-24 04:13:49 -0700 |
---|---|---|
committer | Ole Trøan <otroan@employees.org> | 2017-10-25 16:18:54 +0000 |
commit | 36ea2d6d3a67a60534a7c2b58551688858a1ce7f (patch) | |
tree | 1cb424c70e1f1557e2354eb50559a346adb3314b | |
parent | ac1f96d7cdfb845c08400d27ec566b991e32e24b (diff) |
One armed NAT (VPP-1035)
Use a single physical interface in order to accomplish NAT44/NAT64.
Change-Id: I0c8138953a7a4075df306172e125abad771315e4
Signed-off-by: Matus Fabian <matfabia@cisco.com>
-rwxr-xr-x | src/plugins/nat/in2out.c | 4 | ||||
-rw-r--r-- | src/plugins/nat/nat.api | 2 | ||||
-rw-r--r-- | src/plugins/nat/nat.c | 266 | ||||
-rw-r--r-- | src/plugins/nat/nat.h | 8 | ||||
-rw-r--r-- | src/plugins/nat/nat64.c | 22 | ||||
-rw-r--r-- | src/plugins/nat/nat64_cli.c | 4 | ||||
-rw-r--r-- | src/plugins/nat/nat_api.c | 14 | ||||
-rw-r--r-- | src/plugins/nat/nat_det.c | 2 | ||||
-rw-r--r-- | test/test_nat.py | 142 |
9 files changed, 429 insertions, 35 deletions
diff --git a/src/plugins/nat/in2out.c b/src/plugins/nat/in2out.c index 24ff38602c5..9f668d89f97 100755 --- a/src/plugins/nat/in2out.c +++ b/src/plugins/nat/in2out.c @@ -182,7 +182,7 @@ snat_not_translate_fast (snat_main_t * sm, vlib_node_runtime_t *node, pool_foreach (i, sm->interfaces, ({ /* NAT packet aimed at outside interface */ - if ((i->is_inside == 0) && (sw_if_index == i->sw_if_index)) + if ((nat_interface_is_outside(i)) && (sw_if_index == i->sw_if_index)) return 0; })); } @@ -3523,7 +3523,7 @@ snat_hairpin_src_fn (vlib_main_t * vm, pool_foreach (i, sm->output_feature_interfaces, ({ /* Only packets from NAT inside interface */ - if ((i->is_inside == 1) && (sw_if_index0 == i->sw_if_index)) + if ((nat_interface_is_inside(i)) && (sw_if_index0 == i->sw_if_index)) { if (PREDICT_FALSE ((vnet_buffer (b0)->snat.flags) & SNAT_FLAG_HAIRPINNING)) diff --git a/src/plugins/nat/nat.api b/src/plugins/nat/nat.api index 84187578c9d..fe408211d83 100644 --- a/src/plugins/nat/nat.api +++ b/src/plugins/nat/nat.api @@ -827,7 +827,7 @@ define nat44_interface_dump { /** \brief NAT44 interface details response @param context - sender context, to match reply w/ request - @param is_inside - 1 if inside, 0 if outside + @param is_inside - 1 if inside, 0 if outside, 2 if inside and outside @param sw_if_index - software index of the interface */ define nat44_interface_details { diff --git a/src/plugins/nat/nat.c b/src/plugins/nat/nat.c index 887fab78779..50ab317759e 100644 --- a/src/plugins/nat/nat.c +++ b/src/plugins/nat/nat.c @@ -42,6 +42,11 @@ VNET_FEATURE_INIT (ip4_snat_out2in, static) = { .node_name = "nat44-out2in", .runs_before = VNET_FEATURES ("ip4-lookup"), }; +VNET_FEATURE_INIT (ip4_nat_classify, static) = { + .arc_name = "ip4-unicast", + .node_name = "nat44-classify", + .runs_before = VNET_FEATURES ("ip4-lookup"), +}; VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = { .arc_name = "ip4-unicast", .node_name = "nat44-det-in2out", @@ -52,6 +57,11 @@ VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = { .node_name = "nat44-det-out2in", .runs_before = VNET_FEATURES ("ip4-lookup"), }; +VNET_FEATURE_INIT (ip4_nat_det_classify, static) = { + .arc_name = "ip4-unicast", + .node_name = "nat44-det-classify", + .runs_before = VNET_FEATURES ("ip4-lookup"), +}; VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = { .arc_name = "ip4-unicast", .node_name = "nat44-in2out-worker-handoff", @@ -62,6 +72,11 @@ VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = { .node_name = "nat44-out2in-worker-handoff", .runs_before = VNET_FEATURES ("ip4-lookup"), }; +VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = { + .arc_name = "ip4-unicast", + .node_name = "nat44-handoff-classify", + .runs_before = VNET_FEATURES ("ip4-lookup"), +}; VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = { .arc_name = "ip4-unicast", .node_name = "nat44-in2out-fast", @@ -111,6 +126,159 @@ VLIB_PLUGIN_REGISTER () = { }; /* *INDENT-ON* */ +vlib_node_registration_t nat44_classify_node; +vlib_node_registration_t nat44_det_classify_node; +vlib_node_registration_t nat44_handoff_classify_node; + +typedef enum { + NAT44_CLASSIFY_NEXT_IN2OUT, + NAT44_CLASSIFY_NEXT_OUT2IN, + NAT44_CLASSIFY_N_NEXT, +} nat44_classify_next_t; + +static inline uword +nat44_classify_node_fn_inline (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + u32 n_left_from, * from, * to_next; + nat44_classify_next_t next_index; + snat_main_t *sm = &snat_main; + + 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 = NAT44_CLASSIFY_NEXT_IN2OUT; + ip4_header_t *ip0; + snat_address_t *ap; + snat_session_key_t m_key0; + clib_bihash_kv_8_8_t kv0, value0; + + /* 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); + ip0 = vlib_buffer_get_current (b0); + + vec_foreach (ap, sm->addresses) + { + if (ip0->dst_address.as_u32 == ap->addr.as_u32) + { + next0 = NAT44_CLASSIFY_NEXT_OUT2IN; + break; + } + } + + if (PREDICT_FALSE (pool_elts (sm->static_mappings))) + { + m_key0.addr = ip0->dst_address; + m_key0.port = 0; + m_key0.protocol = 0; + m_key0.fib_index = sm->outside_fib_index; + kv0.key = m_key0.as_u64; + if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv0, &value0)) + { + next0 = NAT44_CLASSIFY_NEXT_OUT2IN; + } + } + /* 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); + } + + return frame->n_vectors; +} + +static uword +nat44_classify_node_fn (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + return nat44_classify_node_fn_inline (vm, node, frame); +}; + +VLIB_REGISTER_NODE (nat44_classify_node) = { + .function = nat44_classify_node_fn, + .name = "nat44-classify", + .vector_size = sizeof (u32), + .type = VLIB_NODE_TYPE_INTERNAL, + .n_next_nodes = NAT44_CLASSIFY_N_NEXT, + .next_nodes = { + [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-in2out", + [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-out2in", + }, +}; + +VLIB_NODE_FUNCTION_MULTIARCH (nat44_classify_node, + nat44_classify_node_fn); + +static uword +nat44_det_classify_node_fn (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + return nat44_classify_node_fn_inline (vm, node, frame); +}; + +VLIB_REGISTER_NODE (nat44_det_classify_node) = { + .function = nat44_det_classify_node_fn, + .name = "nat44-det-classify", + .vector_size = sizeof (u32), + .type = VLIB_NODE_TYPE_INTERNAL, + .n_next_nodes = NAT44_CLASSIFY_N_NEXT, + .next_nodes = { + [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-det-in2out", + [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-det-out2in", + }, +}; + +VLIB_NODE_FUNCTION_MULTIARCH (nat44_det_classify_node, + nat44_det_classify_node_fn); + +static uword +nat44_handoff_classify_node_fn (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + return nat44_classify_node_fn_inline (vm, node, frame); +}; + +VLIB_REGISTER_NODE (nat44_handoff_classify_node) = { + .function = nat44_handoff_classify_node_fn, + .name = "nat44-handoff-classify", + .vector_size = sizeof (u32), + .type = VLIB_NODE_TYPE_INTERNAL, + .n_next_nodes = NAT44_CLASSIFY_N_NEXT, + .next_nodes = { + [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-in2out-worker-handoff", + [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-out2in-worker-handoff", + }, +}; + +VLIB_NODE_FUNCTION_MULTIARCH (nat44_handoff_classify_node, + nat44_handoff_classify_node_fn); + /** * @brief Add/del NAT address to FIB. * @@ -190,7 +358,7 @@ void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id) /* Add external address to FIB */ pool_foreach (i, sm->interfaces, ({ - if (i->is_inside) + if (nat_interface_is_inside(i)) continue; snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1); @@ -198,7 +366,7 @@ void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id) })); pool_foreach (i, sm->output_feature_interfaces, ({ - if (i->is_inside) + if (nat_interface_is_inside(i)) continue; snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1); @@ -569,7 +737,7 @@ delete: /* Add/delete external address to FIB */ pool_foreach (interface, sm->interfaces, ({ - if (interface->is_inside) + if (nat_interface_is_inside(interface)) continue; snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add); @@ -577,7 +745,7 @@ delete: })); pool_foreach (interface, sm->output_feature_interfaces, ({ - if (interface->is_inside) + if (nat_interface_is_inside(interface)) continue; snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add); @@ -930,7 +1098,7 @@ int snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm) /* Delete external address from FIB */ pool_foreach (interface, sm->interfaces, ({ - if (interface->is_inside) + if (nat_interface_is_inside(interface)) continue; snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0); @@ -938,7 +1106,7 @@ int snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm) })); pool_foreach (interface, sm->output_feature_interfaces, ({ - if (interface->is_inside) + if (nat_interface_is_inside(interface)) continue; snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0); @@ -952,7 +1120,7 @@ int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del) { snat_main_t *sm = &snat_main; snat_interface_t *i; - const char * feature_name; + const char * feature_name, *del_feature_name; snat_address_t * ap; snat_static_mapping_t * m; snat_det_map_t * dm; @@ -969,9 +1137,6 @@ int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del) feature_name = is_inside ? "nat44-in2out" : "nat44-out2in"; } - vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, - !is_del, 0, 0); - if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1) sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0); @@ -983,9 +1148,63 @@ int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del) if (i->sw_if_index == sw_if_index) { if (is_del) - pool_put (sm->interfaces, i); + { + if (nat_interface_is_inside(i) && nat_interface_is_outside(i)) + { + if (is_inside) + i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE; + else + i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE; + + if (sm->num_workers > 1 && !sm->deterministic) + del_feature_name = "nat44-handoff-classify"; + else if (sm->deterministic) + del_feature_name = "nat44-det-classify"; + else + del_feature_name = "nat44-classify"; + + vnet_feature_enable_disable ("ip4-unicast", del_feature_name, + sw_if_index, 0, 0, 0); + vnet_feature_enable_disable ("ip4-unicast", feature_name, + sw_if_index, 1, 0, 0); + } + else + { + vnet_feature_enable_disable ("ip4-unicast", feature_name, + sw_if_index, 0, 0, 0); + pool_put (sm->interfaces, i); + } + } else - return VNET_API_ERROR_VALUE_EXIST; + { + if ((nat_interface_is_inside(i) && is_inside) || + (nat_interface_is_outside(i) && !is_inside)) + return 0; + + if (sm->num_workers > 1 && !sm->deterministic) + { + del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" : + "nat44-out2in-worker-handoff"; + feature_name = "nat44-handoff-classify"; + } + else if (sm->deterministic) + { + del_feature_name = !is_inside ? "nat44-det-in2out" : + "nat44-det-out2in"; + feature_name = "nat44-det-classify"; + } + else + { + del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in"; + feature_name = "nat44-classify"; + } + + vnet_feature_enable_disable ("ip4-unicast", del_feature_name, + sw_if_index, 0, 0, 0); + vnet_feature_enable_disable ("ip4-unicast", feature_name, + sw_if_index, 1, 0, 0); + goto set_flags; + } goto fib; } @@ -996,7 +1215,14 @@ int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del) pool_get (sm->interfaces, i); i->sw_if_index = sw_if_index; - i->is_inside = is_inside; + i->flags = 0; + vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0, 0); + +set_flags: + if (is_inside) + i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE; + else + i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE; /* Add/delete external addresses to FIB */ fib: @@ -1090,7 +1316,11 @@ fq: pool_get (sm->output_feature_interfaces, i); i->sw_if_index = sw_if_index; - i->is_inside = is_inside; + i->flags = 0; + if (is_inside) + i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE; + else + i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE; /* Add/delete external addresses to FIB */ fib: @@ -2486,7 +2716,9 @@ show_snat_command_fn (vlib_main_t * vm, ({ vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm, vnet_get_sw_interface (vnm, i->sw_if_index), - i->is_inside ? "in" : "out"); + (nat_interface_is_inside(i) && + nat_interface_is_outside(i)) ? "in out" : + (nat_interface_is_inside(i) ? "in" : "out")); })); pool_foreach (i, sm->output_feature_interfaces, @@ -2494,7 +2726,9 @@ show_snat_command_fn (vlib_main_t * vm, vlib_cli_output (vm, "%U output-feature %s", format_vnet_sw_interface_name, vnm, vnet_get_sw_interface (vnm, i->sw_if_index), - i->is_inside ? "in" : "out"); + (nat_interface_is_inside(i) && + nat_interface_is_outside(i)) ? "in out" : + (nat_interface_is_inside(i) ? "in" : "out")); })); if (vec_len (sm->auto_add_sw_if_indices)) diff --git a/src/plugins/nat/nat.h b/src/plugins/nat/nat.h index e53e924ff03..d4ad72520da 100644 --- a/src/plugins/nat/nat.h +++ b/src/plugins/nat/nat.h @@ -129,6 +129,9 @@ typedef enum { #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4 +#define NAT_INTERFACE_FLAG_IS_INSIDE 1 +#define NAT_INTERFACE_FLAG_IS_OUTSIDE 2 + typedef CLIB_PACKED(struct { snat_session_key_t out2in; /* 0-15 */ @@ -217,7 +220,7 @@ typedef struct { typedef struct { u32 sw_if_index; - u8 is_inside; + u8 flags; } snat_interface_t; typedef struct { @@ -414,6 +417,9 @@ typedef struct { */ #define snat_is_unk_proto_session(s) s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO +#define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE +#define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE + /* * Why is this here? Because we don't need to touch this layer to * simply reply to an icmp. We need to change id to a unique diff --git a/src/plugins/nat/nat64.c b/src/plugins/nat/nat64.c index bfcfa9b3620..952ca8fb11b 100644 --- a/src/plugins/nat/nat64.c +++ b/src/plugins/nat/nat64.c @@ -136,7 +136,7 @@ nat64_add_del_pool_addr (ip4_address_t * addr, u32 vrf_id, u8 is_add) /* *INDENT-OFF* */ pool_foreach (interface, nm->interfaces, ({ - if (interface->is_inside) + if (nat_interface_is_inside(interface)) continue; snat_add_del_addr_to_fib (addr, 32, interface->sw_if_index, is_add); @@ -170,7 +170,7 @@ nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add) snat_address_t *ap; const char *feature_name, *arc_name; - /* Check if address already exists */ + /* Check if interface already exists */ /* *INDENT-OFF* */ pool_foreach (i, nm->interfaces, ({ @@ -185,19 +185,29 @@ nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add) if (is_add) { if (interface) - return VNET_API_ERROR_VALUE_EXIST; + goto set_flags; pool_get (nm->interfaces, interface); interface->sw_if_index = sw_if_index; - interface->is_inside = is_inside; - + interface->flags = 0; + set_flags: + if (is_inside) + interface->flags |= NAT_INTERFACE_FLAG_IS_INSIDE; + else + interface->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE; } else { if (!interface) return VNET_API_ERROR_NO_SUCH_ENTRY; - pool_put (nm->interfaces, interface); + if ((nat_interface_is_inside (interface) + && nat_interface_is_outside (interface))) + interface->flags &= + is_inside ? ~NAT_INTERFACE_FLAG_IS_INSIDE : + ~NAT_INTERFACE_FLAG_IS_OUTSIDE; + else + pool_put (nm->interfaces, interface); } if (!is_inside) diff --git a/src/plugins/nat/nat64_cli.c b/src/plugins/nat/nat64_cli.c index f3645bbbe0b..4128eda5a38 100644 --- a/src/plugins/nat/nat64_cli.c +++ b/src/plugins/nat/nat64_cli.c @@ -268,7 +268,9 @@ nat64_cli_interface_walk (snat_interface_t * i, void *ctx) vlib_cli_output (vm, " %U %s", format_vnet_sw_interface_name, vnm, vnet_get_sw_interface (vnm, i->sw_if_index), - i->is_inside ? "in" : "out"); + (nat_interface_is_inside (i) + && nat_interface_is_outside (i)) ? "in out" : + nat_interface_is_inside (i) ? "in" : "out"); return 0; } diff --git a/src/plugins/nat/nat_api.c b/src/plugins/nat/nat_api.c index 3bacdfed051..96f69eba186 100644 --- a/src/plugins/nat/nat_api.c +++ b/src/plugins/nat/nat_api.c @@ -224,7 +224,7 @@ static void memset (rmp, 0, sizeof (*rmp)); rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_DETAILS + sm->msg_id_base); rmp->sw_if_index = ntohl (i->sw_if_index); - rmp->is_inside = i->is_inside; + rmp->is_inside = nat_interface_is_inside (i); rmp->context = context; vl_msg_api_send_shmem (q, (u8 *) & rmp); @@ -306,7 +306,7 @@ send_snat_interface_output_feature_details (snat_interface_t * i, ntohs (VL_API_SNAT_INTERFACE_OUTPUT_FEATURE_DETAILS + sm->msg_id_base); rmp->sw_if_index = ntohl (i->sw_if_index); rmp->context = context; - rmp->is_inside = i->is_inside; + rmp->is_inside = nat_interface_is_inside (i); vl_msg_api_send_shmem (q, (u8 *) & rmp); } @@ -1658,7 +1658,9 @@ send_nat44_interface_details (snat_interface_t * i, memset (rmp, 0, sizeof (*rmp)); rmp->_vl_msg_id = ntohs (VL_API_NAT44_INTERFACE_DETAILS + sm->msg_id_base); rmp->sw_if_index = ntohl (i->sw_if_index); - rmp->is_inside = i->is_inside; + rmp->is_inside = (nat_interface_is_inside (i) + && nat_interface_is_outside (i)) ? 2 : + nat_interface_is_inside (i); rmp->context = context; vl_msg_api_send_shmem (q, (u8 *) & rmp); @@ -1741,7 +1743,7 @@ send_nat44_interface_output_feature_details (snat_interface_t * i, ntohs (VL_API_NAT44_INTERFACE_OUTPUT_FEATURE_DETAILS + sm->msg_id_base); rmp->sw_if_index = ntohl (i->sw_if_index); rmp->context = context; - rmp->is_inside = i->is_inside; + rmp->is_inside = nat_interface_is_inside (i); vl_msg_api_send_shmem (q, (u8 *) & rmp); } @@ -2867,7 +2869,9 @@ nat64_api_interface_walk (snat_interface_t * i, void *arg) memset (rmp, 0, sizeof (*rmp)); rmp->_vl_msg_id = ntohs (VL_API_NAT64_INTERFACE_DETAILS + sm->msg_id_base); rmp->sw_if_index = ntohl (i->sw_if_index); - rmp->is_inside = i->is_inside; + rmp->is_inside = (nat_interface_is_inside (i) + && nat_interface_is_outside (i)) ? 2 : + nat_interface_is_inside (i); rmp->context = ctx->context; vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp); diff --git a/src/plugins/nat/nat_det.c b/src/plugins/nat/nat_det.c index 3af6698c09a..1b00d40b4ae 100644 --- a/src/plugins/nat/nat_det.c +++ b/src/plugins/nat/nat_det.c @@ -94,7 +94,7 @@ snat_det_add_map (snat_main_t * sm, ip4_address_t * in_addr, u8 in_plen, /* *INDENT-OFF* */ pool_foreach (i, sm->interfaces, ({ - if (i->is_inside) + if (nat_interface_is_inside(i)) continue; snat_add_del_addr_to_fib(out_addr, out_plen, i->sw_if_index, is_add); diff --git a/test/test_nat.py b/test/test_nat.py index 73e9e217348..2afa1dd26df 100644 --- a/test/test_nat.py +++ b/test/test_nat.py @@ -537,7 +537,7 @@ class TestNAT44(MethodHolder): cls.ipfix_src_port = 4739 cls.ipfix_domain_id = 1 - cls.create_pg_interfaces(range(9)) + cls.create_pg_interfaces(range(10)) cls.interfaces = list(cls.pg_interfaces[0:4]) for i in cls.interfaces: @@ -572,6 +572,18 @@ class TestNAT44(MethodHolder): cls.pg7.admin_up() cls.pg8.admin_up() + cls.pg9.generate_remote_hosts(2) + cls.pg9.config_ip4() + ip_addr_n = socket.inet_pton(socket.AF_INET, "10.0.0.1") + cls.vapi.sw_interface_add_del_address(cls.pg9.sw_if_index, + ip_addr_n, + 24) + cls.pg9.admin_up() + cls.pg9.resolve_arp() + cls.pg9._remote_hosts[1]._ip4 = cls.pg9._remote_hosts[0]._ip4 + cls.pg4._remote_ip4 = cls.pg9._remote_hosts[0]._ip4 = "10.0.0.2" + cls.pg9.resolve_arp() + except Exception: super(TestNAT44, cls).tearDownClass() raise @@ -614,6 +626,10 @@ class TestNAT44(MethodHolder): interfaces = self.vapi.nat44_interface_dump() for intf in interfaces: + if intf.is_inside > 1: + self.vapi.nat44_interface_add_del_feature(intf.sw_if_index, + 0, + is_add=0) self.vapi.nat44_interface_add_del_feature(intf.sw_if_index, intf.is_inside, is_add=0) @@ -2377,6 +2393,62 @@ class TestNAT44(MethodHolder): self.logger.error(ppp("Unexpected or invalid packet:"), p) raise + def test_one_armed_nat44(self): + """ One armed NAT44 """ + remote_host = self.pg9.remote_hosts[0] + local_host = self.pg9.remote_hosts[1] + external_port = 0 + + self.nat44_add_address(self.nat_addr) + self.vapi.nat44_interface_add_del_feature(self.pg9.sw_if_index) + self.vapi.nat44_interface_add_del_feature(self.pg9.sw_if_index, + is_inside=0) + + # in2out + p = (Ether(src=self.pg9.remote_mac, dst=self.pg9.local_mac) / + IP(src=local_host.ip4, dst=remote_host.ip4) / + TCP(sport=12345, dport=80)) + self.pg9.add_stream(p) + self.pg_enable_capture(self.pg_interfaces) + self.pg_start() + capture = self.pg9.get_capture(1) + p = capture[0] + try: + ip = p[IP] + tcp = p[TCP] + self.assertEqual(ip.src, self.nat_addr) + self.assertEqual(ip.dst, remote_host.ip4) + self.assertNotEqual(tcp.sport, 12345) + external_port = tcp.sport + self.assertEqual(tcp.dport, 80) + self.check_tcp_checksum(p) + self.check_ip_checksum(p) + except: + self.logger.error(ppp("Unexpected or invalid packet:", p)) + raise + + # out2in + p = (Ether(src=self.pg9.remote_mac, dst=self.pg9.local_mac) / + IP(src=remote_host.ip4, dst=self.nat_addr) / + TCP(sport=80, dport=external_port)) + self.pg9.add_stream(p) + self.pg_enable_capture(self.pg_interfaces) + self.pg_start() + capture = self.pg9.get_capture(1) + p = capture[0] + try: + ip = p[IP] + tcp = p[TCP] + self.assertEqual(ip.src, remote_host.ip4) + self.assertEqual(ip.dst, local_host.ip4) + self.assertEqual(tcp.sport, 80) + self.assertEqual(tcp.dport, 12345) + self.check_tcp_checksum(p) + self.check_ip_checksum(p) + except: + self.logger.error(ppp("Unexpected or invalid packet:", p)) + raise + def tearDown(self): super(TestNAT44, self).tearDown() if not self.vpp_dead: @@ -3048,7 +3120,7 @@ class TestNAT64(MethodHolder): cls.vrf1_nat_addr_n = socket.inet_pton(socket.AF_INET, cls.vrf1_nat_addr) - cls.create_pg_interfaces(range(3)) + cls.create_pg_interfaces(range(4)) cls.ip6_interfaces = list(cls.pg_interfaces[0:1]) cls.ip6_interfaces.append(cls.pg_interfaces[2]) cls.ip4_interfaces = list(cls.pg_interfaces[1:2]) @@ -3069,6 +3141,12 @@ class TestNAT64(MethodHolder): i.config_ip4() i.resolve_arp() + cls.pg3.admin_up() + cls.pg3.config_ip4() + cls.pg3.resolve_arp() + cls.pg3.config_ip6() + cls.pg3.configure_ipv6_neighbors() + except Exception: super(TestNAT64, cls).tearDownClass() raise @@ -3763,6 +3841,62 @@ class TestNAT64(MethodHolder): self.logger.error(ppp("Unexpected or invalid packet:", packet)) raise + def test_one_armed_nat64(self): + """ One armed NAT64 """ + external_port = 0 + remote_host_ip6 = self.compose_ip6(self.pg3.remote_ip4, + '64:ff9b::', + 96) + + self.vapi.nat64_add_del_pool_addr_range(self.nat_addr_n, + self.nat_addr_n) + self.vapi.nat64_add_del_interface(self.pg3.sw_if_index) + self.vapi.nat64_add_del_interface(self.pg3.sw_if_index, is_inside=0) + + # in2out + p = (Ether(src=self.pg3.remote_mac, dst=self.pg3.local_mac) / + IPv6(src=self.pg3.remote_ip6, dst=remote_host_ip6) / + TCP(sport=12345, dport=80)) + self.pg3.add_stream(p) + self.pg_enable_capture(self.pg_interfaces) + self.pg_start() + capture = self.pg3.get_capture(1) + p = capture[0] + try: + ip = p[IP] + tcp = p[TCP] + self.assertEqual(ip.src, self.nat_addr) + self.assertEqual(ip.dst, self.pg3.remote_ip4) + self.assertNotEqual(tcp.sport, 12345) + external_port = tcp.sport + self.assertEqual(tcp.dport, 80) + self.check_tcp_checksum(p) + self.check_ip_checksum(p) + except: + self.logger.error(ppp("Unexpected or invalid packet:", p)) + raise + + # out2in + p = (Ether(src=self.pg3.remote_mac, dst=self.pg3.local_mac) / + IP(src=self.pg3.remote_ip4, dst=self.nat_addr) / + TCP(sport=80, dport=external_port)) + self.pg3.add_stream(p) + self.pg_enable_capture(self.pg_interfaces) + self.pg_start() + capture = self.pg3.get_capture(1) + p = capture[0] + try: + ip = p[IPv6] + tcp = p[TCP] + self.assertEqual(ip.src, remote_host_ip6) + self.assertEqual(ip.dst, self.pg3.remote_ip6) + self.assertEqual(tcp.sport, 80) + self.assertEqual(tcp.dport, 12345) + self.check_tcp_checksum(p) + except: + self.logger.error(ppp("Unexpected or invalid packet:", p)) + raise + def nat64_get_ses_num(self): """ Return number of active NAT64 sessions. @@ -3778,6 +3912,10 @@ class TestNAT64(MethodHolder): interfaces = self.vapi.nat64_interface_dump() for intf in interfaces: + if intf.is_inside > 1: + self.vapi.nat64_add_del_interface(intf.sw_if_index, + 0, + is_add=0) self.vapi.nat64_add_del_interface(intf.sw_if_index, intf.is_inside, is_add=0) |