summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xsrc/plugins/nat/in2out.c87
-rw-r--r--src/plugins/nat/nat.c2
-rw-r--r--test/test_nat.py99
3 files changed, 173 insertions, 15 deletions
diff --git a/src/plugins/nat/in2out.c b/src/plugins/nat/in2out.c
index a453328c38f..003b1ad3858 100755
--- a/src/plugins/nat/in2out.c
+++ b/src/plugins/nat/in2out.c
@@ -252,6 +252,27 @@ snat_not_translate (snat_main_t * sm, vlib_node_runtime_t *node,
rx_fib_index0);
}
+static inline int
+nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
+ u32 proto0, u32 thread_index)
+{
+ udp_header_t * udp0 = ip4_next_header (ip0);
+ snat_session_key_t key0;
+ clib_bihash_kv_8_8_t kv0, value0;
+
+ key0.addr = ip0->src_address;
+ key0.port = udp0->src_port;
+ key0.protocol = proto0;
+ key0.fib_index = sm->outside_fib_index;
+ kv0.key = key0.as_u64;
+
+ if (!clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
+ &value0))
+ return 1;
+
+ return 0;
+}
+
static u32 slow_path (snat_main_t *sm, vlib_buffer_t *b0,
ip4_header_t * ip0,
u32 rx_fib_index0,
@@ -497,12 +518,23 @@ u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
&value0))
{
- if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0, ip0,
- IP_PROTOCOL_ICMP, rx_fib_index0, thread_index) &&
- vnet_buffer(b0)->sw_if_index[VLIB_TX] == ~0))
+ if (vnet_buffer(b0)->sw_if_index[VLIB_TX] != ~0)
{
- dont_translate = 1;
- goto out;
+ if (PREDICT_FALSE(nat_not_translate_output_feature(sm,
+ ip0, IP_PROTOCOL_ICMP, thread_index)))
+ {
+ dont_translate = 1;
+ goto out;
+ }
+ }
+ else
+ {
+ if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
+ ip0, IP_PROTOCOL_ICMP, rx_fib_index0, thread_index)))
+ {
+ dont_translate = 1;
+ goto out;
+ }
}
if (PREDICT_FALSE(icmp_is_error_message (icmp0)))
@@ -1517,9 +1549,18 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
{
if (is_slow_path)
{
- if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
- ip0, proto0, rx_fib_index0, thread_index)) && !is_output_feature)
- goto trace00;
+ if (is_output_feature)
+ {
+ if (PREDICT_FALSE(nat_not_translate_output_feature(sm,
+ ip0, proto0, thread_index)))
+ goto trace00;
+ }
+ else
+ {
+ if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
+ ip0, proto0, rx_fib_index0, thread_index)))
+ goto trace00;
+ }
next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
&s0, node, next0, thread_index);
@@ -1696,9 +1737,18 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
{
if (is_slow_path)
{
- if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index1,
- ip1, proto1, rx_fib_index1, thread_index)) && !is_output_feature)
- goto trace01;
+ if (is_output_feature)
+ {
+ if (PREDICT_FALSE(nat_not_translate_output_feature(sm,
+ ip1, proto1, thread_index)))
+ goto trace00;
+ }
+ else
+ {
+ if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index1,
+ ip1, proto1, rx_fib_index1, thread_index)))
+ goto trace01;
+ }
next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
&s1, node, next1, thread_index);
@@ -1907,9 +1957,18 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
{
if (is_slow_path)
{
- if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
- ip0, proto0, rx_fib_index0, thread_index)) && !is_output_feature)
- goto trace0;
+ if (is_output_feature)
+ {
+ if (PREDICT_FALSE(nat_not_translate_output_feature(sm,
+ ip0, proto0, thread_index)))
+ goto trace0;
+ }
+ else
+ {
+ if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
+ ip0, proto0, rx_fib_index0, thread_index)))
+ goto trace0;
+ }
next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
&s0, node, next0, thread_index);
diff --git a/src/plugins/nat/nat.c b/src/plugins/nat/nat.c
index 38caae43376..ef740d9d12b 100644
--- a/src/plugins/nat/nat.c
+++ b/src/plugins/nat/nat.c
@@ -774,7 +774,7 @@ int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
}
}
/* External address must be allocated */
- if (!a)
+ if (!a && (l_addr.as_u32 != e_addr.as_u32))
return VNET_API_ERROR_NO_SUCH_ENTRY;
}
diff --git a/test/test_nat.py b/test/test_nat.py
index 34726ea41b2..e3b44859f61 100644
--- a/test/test_nat.py
+++ b/test/test_nat.py
@@ -3270,6 +3270,105 @@ class TestNAT44(MethodHolder):
self.logger.error(ppp("Unexpected or invalid packet:", p))
raise
+ def test_output_feature_and_service(self):
+ """ NAT44 interface output feature and services """
+ external_addr = '1.2.3.4'
+ external_port = 80
+ local_port = 8080
+
+ self.vapi.nat44_forwarding_enable_disable(1)
+ self.nat44_add_address(self.nat_addr)
+ self.nat44_add_static_mapping(self.pg0.remote_ip4, external_addr,
+ local_port, external_port,
+ proto=IP_PROTOS.tcp, out2in_only=1)
+ self.vapi.nat44_interface_add_del_feature(self.pg0.sw_if_index)
+ self.vapi.nat44_interface_add_del_output_feature(self.pg1.sw_if_index,
+ is_inside=0)
+
+ # from client to service
+ p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
+ IP(src=self.pg1.remote_ip4, dst=external_addr) /
+ TCP(sport=12345, dport=external_port))
+ self.pg1.add_stream(p)
+ self.pg_enable_capture(self.pg_interfaces)
+ self.pg_start()
+ capture = self.pg0.get_capture(1)
+ p = capture[0]
+ server = None
+ try:
+ ip = p[IP]
+ tcp = p[TCP]
+ self.assertEqual(ip.dst, self.pg0.remote_ip4)
+ self.assertEqual(tcp.dport, local_port)
+ self.check_tcp_checksum(p)
+ self.check_ip_checksum(p)
+ except:
+ self.logger.error(ppp("Unexpected or invalid packet:", p))
+ raise
+
+ # from service back to client
+ p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
+ IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
+ TCP(sport=local_port, dport=12345))
+ self.pg0.add_stream(p)
+ self.pg_enable_capture(self.pg_interfaces)
+ self.pg_start()
+ capture = self.pg1.get_capture(1)
+ p = capture[0]
+ try:
+ ip = p[IP]
+ tcp = p[TCP]
+ self.assertEqual(ip.src, external_addr)
+ self.assertEqual(tcp.sport, external_port)
+ self.check_tcp_checksum(p)
+ self.check_ip_checksum(p)
+ except:
+ self.logger.error(ppp("Unexpected or invalid packet:", p))
+ raise
+
+ # from local network host to external network
+ ext_port = 0
+ p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
+ IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
+ TCP(sport=23456, dport=34567))
+ self.pg0.add_stream(p)
+ self.pg_enable_capture(self.pg_interfaces)
+ self.pg_start()
+ capture = self.pg1.get_capture(1)
+ p = capture[0]
+ try:
+ ip = p[IP]
+ tcp = p[TCP]
+ self.assertEqual(ip.src, self.nat_addr)
+ self.assertNotEqual(tcp.sport, 23456)
+ ext_port = tcp.sport
+ self.check_tcp_checksum(p)
+ self.check_ip_checksum(p)
+ except:
+ self.logger.error(ppp("Unexpected or invalid packet:", p))
+ raise
+
+ # from external network back to local network host
+ p = (Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) /
+ IP(src=self.pg1.remote_ip4, dst=self.nat_addr) /
+ TCP(sport=34567, dport=ext_port))
+ self.pg1.add_stream(p)
+ self.pg_enable_capture(self.pg_interfaces)
+ self.pg_start()
+ capture = self.pg0.get_capture(1)
+ p = capture[0]
+ server = None
+ try:
+ ip = p[IP]
+ tcp = p[TCP]
+ self.assertEqual(ip.dst, self.pg0.remote_ip4)
+ self.assertEqual(tcp.dport, 23456)
+ self.check_tcp_checksum(p)
+ self.check_ip_checksum(p)
+ except:
+ 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]