ebb439
From df2c57d43d758f26204c3048b278fa546a4858f8 Mon Sep 17 00:00:00 2001
ebb439
From: Dumitru Ceara <dceara@redhat.com>
ebb439
Date: Tue, 20 Oct 2020 16:54:32 +0200
ebb439
Subject: [PATCH] ovn-northd: Handle IPv6 addresses with prefixes for port
ebb439
 security.
ebb439
ebb439
Reported-by: Rodolfo Alonso <ralonsoh@redhat.com>
ebb439
Reported-at: https://bugzilla.redhat.com/1856898
ebb439
CC: Numan Siddique <numans@ovn.org>
ebb439
Fixes: f631376bf75d ("ovn-northd: Handle IPv4 addresses with prefixes in lport port security")
ebb439
Signed-off-by: Dumitru Ceara <dceara@redhat.com>
ebb439
Signed-off-by: Numan Siddique <numans@ovn.org>
ebb439
ebb439
(cherry picked from upstream commit 45ed5a3cd500bf94706507d3d10f63ea133cbc29)
ebb439
ebb439
Change-Id: Icaa1b299a0a2a7006532e37575f20d5a1f25787c
ebb439
---
ebb439
 lib/ovn-l7.h        | 11 +++++++++++
ebb439
 northd/ovn-northd.c | 35 ++++++++++++++++++++++++++++-------
ebb439
 tests/ovn.at        | 40 +++++++++++++++++++++++++++++++---------
ebb439
 3 files changed, 70 insertions(+), 16 deletions(-)
ebb439
ebb439
diff --git a/lib/ovn-l7.h b/lib/ovn-l7.h
ebb439
index 30a7955..9b729db 100644
ebb439
--- a/lib/ovn-l7.h
ebb439
+++ b/lib/ovn-l7.h
ebb439
@@ -428,6 +428,17 @@ ipv6_addr_is_routable_multicast(const struct in6_addr *ip) {
ebb439
     }
ebb439
 }
ebb439
 
ebb439
+static inline bool
ebb439
+ipv6_addr_is_host_zero(const struct in6_addr *prefix,
ebb439
+                       const struct in6_addr *mask)
ebb439
+{
ebb439
+    /* host-bits-non-zero <=> (prefix ^ mask) & prefix. */
ebb439
+    struct in6_addr tmp = ipv6_addr_bitxor(prefix, mask);
ebb439
+
ebb439
+    tmp = ipv6_addr_bitand(&tmp, prefix);
ebb439
+    return ipv6_is_zero(&tmp);
ebb439
+}
ebb439
+
ebb439
 #define IPV6_EXT_HEADER_LEN 8
ebb439
 struct ipv6_ext_header {
ebb439
     uint8_t ip6_nxt_proto;
ebb439
diff --git a/northd/ovn-northd.c b/northd/ovn-northd.c
ebb439
index 3a71d0e..7cd2f9f 100644
ebb439
--- a/northd/ovn-northd.c
ebb439
+++ b/northd/ovn-northd.c
ebb439
@@ -4285,10 +4285,20 @@ build_port_security_ipv6_nd_flow(
ebb439
     ipv6_string_mapped(ip6_str, &lla);
ebb439
     ds_put_format(match, " && (nd.target == %s", ip6_str);
ebb439
 
ebb439
-    for(int i = 0; i < n_ipv6_addrs; i++) {
ebb439
-        memset(ip6_str, 0, sizeof(ip6_str));
ebb439
-        ipv6_string_mapped(ip6_str, &ipv6_addrs[i].addr);
ebb439
-        ds_put_format(match, " || nd.target == %s", ip6_str);
ebb439
+    for (size_t i = 0; i < n_ipv6_addrs; i++) {
ebb439
+        /* When the netmask is applied, if the host portion is
ebb439
+         * non-zero, the host can only use the specified
ebb439
+         * address in the nd.target.  If zero, the host is allowed
ebb439
+         * to use any address in the subnet.
ebb439
+         */
ebb439
+        if (ipv6_addrs[i].plen == 128
ebb439
+            || !ipv6_addr_is_host_zero(&ipv6_addrs[i].addr,
ebb439
+                                       &ipv6_addrs[i].mask)) {
ebb439
+            ds_put_format(match, " || nd.target == %s", ipv6_addrs[i].addr_s);
ebb439
+        } else {
ebb439
+            ds_put_format(match, " || nd.target == %s/%d",
ebb439
+                          ipv6_addrs[i].network_s, ipv6_addrs[i].plen);
ebb439
+        }
ebb439
     }
ebb439
 
ebb439
     ds_put_format(match, ")))");
ebb439
@@ -4314,9 +4324,20 @@ build_port_security_ipv6_flow(
ebb439
     if (pipeline == P_OUT) {
ebb439
         ds_put_cstr(match, "ff00::/8, ");
ebb439
     }
ebb439
-    for(int i = 0; i < n_ipv6_addrs; i++) {
ebb439
-        ipv6_string_mapped(ip6_str, &ipv6_addrs[i].addr);
ebb439
-        ds_put_format(match, "%s, ", ip6_str);
ebb439
+    for (size_t i = 0; i < n_ipv6_addrs; i++) {
ebb439
+        /* When the netmask is applied, if the host portion is
ebb439
+         * non-zero, the host can only use the specified
ebb439
+         * address.  If zero, the host is allowed to use any
ebb439
+         * address in the subnet.
ebb439
+         */
ebb439
+        if (ipv6_addrs[i].plen == 128
ebb439
+            || !ipv6_addr_is_host_zero(&ipv6_addrs[i].addr,
ebb439
+                                       &ipv6_addrs[i].mask)) {
ebb439
+            ds_put_format(match, "%s, ", ipv6_addrs[i].addr_s);
ebb439
+        } else {
ebb439
+            ds_put_format(match, "%s/%d, ", ipv6_addrs[i].network_s,
ebb439
+                          ipv6_addrs[i].plen);
ebb439
+        }
ebb439
     }
ebb439
     /* Replace ", " by "}". */
ebb439
     ds_chomp(match, ' ');
ebb439
diff --git a/tests/ovn.at b/tests/ovn.at
ebb439
index 337ab4e..2c6f7cc 100644
ebb439
--- a/tests/ovn.at
ebb439
+++ b/tests/ovn.at
ebb439
@@ -4133,10 +4133,10 @@ for i in 1 2 3; do
ebb439
         if test $j = 1; then
ebb439
             ovn-nbctl lsp-set-addresses lp$i$j "f0:00:00:00:00:$i$j 192.168.0.$i$j" unknown
ebb439
         elif test $j = 2; then
ebb439
-            ovn-nbctl lsp-set-addresses lp$i$j "f0:00:00:00:00:$i$j 192.168.0.$i$j"
ebb439
+            ovn-nbctl lsp-set-addresses lp$i$j "f0:00:00:00:00:$i$j 192.168.0.$i$j 4343::00$i$j"
ebb439
             ovn-nbctl lsp-set-port-security lp$i$j f0:00:00:00:00:$i$j
ebb439
         else
ebb439
-            extra_addr="f0:00:00:00:0$i:$i$j fe80::ea2a:eaff:fe28:$i$j"
ebb439
+            extra_addr="f0:00:00:00:0$i:$i$j fe80::ea2a:eaff:fe28:$i$j 4242::00$i$j"
ebb439
             ovn-nbctl lsp-set-addresses lp$i$j "f0:00:00:00:00:$i$j 192.168.0.$i$j" "$extra_addr"
ebb439
             ovn-nbctl lsp-set-port-security lp$i$j "f0:00:00:00:00:$i$j 192.168.0.$i$j" "$extra_addr"
ebb439
         fi
ebb439
@@ -4352,7 +4352,7 @@ for i in 1 2 3; do
ebb439
 done
ebb439
 
ebb439
 # lp13 has extra port security with mac f0000000113 and ipv6 addr
ebb439
-# fe80::ea2a:eaff:fe28:0012
ebb439
+# fe80::ea2a:eaff:fe28:0012 and 4242::0013
ebb439
 
ebb439
 # ipv4 packet should be dropped for lp13 with mac f0000000113
ebb439
 sip=`ip_to_hex 192 168 0 13`
ebb439
@@ -4366,20 +4366,24 @@ sip=ee800000000000000000000000000000
ebb439
 for i in 1 2 3; do
ebb439
     tip=fe80000000000000ea2aeafffe2800${i}3
ebb439
     test_ipv6 11 f00000000011 f00000000${i}${i}3 $sip $tip ${i}3
ebb439
+    tip=424200000000000000000000000000${i}3
ebb439
+    test_ipv6 11 f00000000011 f00000000${i}${i}3 $sip $tip ${i}3
ebb439
 done
ebb439
 
ebb439
 
ebb439
 # ipv6 packet should not be received by lp33 with mac f0000000333
ebb439
-# and ip6.dst as fe80::ea2a:eaff:fe28:0023 as it is
ebb439
-# configured with fe80::ea2a:eaff:fe28:0033
ebb439
+# and ip6.dst as fe80::ea2a:eaff:fe28:0023 or 4242::0023 as it is
ebb439
+# configured with fe80::ea2a:eaff:fe28:0033 and 4242::0033
ebb439
 # lp11 can send ipv6 traffic as there is no port security
ebb439
 
ebb439
 sip=ee800000000000000000000000000000
ebb439
 tip=fe80000000000000ea2aeafffe280023
ebb439
 test_ipv6 11 f00000000011 f00000000333 $sip $tip
ebb439
+tip=42420000000000000000000000000023
ebb439
+test_ipv6 11 f00000000011 f00000000333 $sip $tip
ebb439
 
ebb439
 # ipv6 packet should be allowed for lp[123]3 with mac f0000000${i}${i}3
ebb439
-# and ip6.src fe80::ea2a:eaff:fe28:0${i}${i}3 and ip6.src ::.
ebb439
+# and ip6.src fe80::ea2a:eaff:fe28:0${i}${i}3, 4242::00${i}3 and ip6.src ::.
ebb439
 # and should be dropped for any other ip6.src
ebb439
 # lp21 can receive ipv6 traffic as there is no port security
ebb439
 
ebb439
@@ -4387,6 +4391,8 @@ tip=ee800000000000000000000000000000
ebb439
 for i in 1 2 3; do
ebb439
     sip=fe80000000000000ea2aeafffe2800${i}3
ebb439
     test_ipv6 ${i}3 f00000000${i}${i}3 f00000000021 $sip $tip 21
ebb439
+    sip=424200000000000000000000000000${i}3
ebb439
+    test_ipv6 ${i}3 f00000000${i}${i}3 f00000000021 $sip $tip 21
ebb439
 
ebb439
     # Test ICMPv6 MLD reports (v1 and v2) and NS for DAD
ebb439
     sip=00000000000000000000000000000000
ebb439
@@ -4404,9 +4410,7 @@ for i in 1 2 3; do
ebb439
 done
ebb439
 
ebb439
 # configure lsp13 to send and received IPv4 packets with an address range
ebb439
-ovn-nbctl lsp-set-port-security lp13 "f0:00:00:00:00:13 192.168.0.13 20.0.0.4/24 10.0.0.0/24"
ebb439
-
ebb439
-sleep 2
ebb439
+ovn-nbctl --wait=hv lsp-set-port-security lp13 "f0:00:00:00:00:13 192.168.0.13 20.0.0.4/24 10.0.0.0/24 4242::/64"
ebb439
 
ebb439
 sip=`ip_to_hex 10 0 0 13`
ebb439
 tip=`ip_to_hex 192 168 0 22`
ebb439
@@ -4419,12 +4423,24 @@ tip=`ip_to_hex 192 168 0 23`
ebb439
 # with dst ip 192.168.0.23 should be allowed
ebb439
 test_ip 13 f00000000013 f00000000023 $sip $tip 23
ebb439
 
ebb439
+sip=42420000000000000000000000000014
ebb439
+tip=42420000000000000000000000000023
ebb439
+# IPv6 packet from lsp13 with src ip 4242::14 destined to lsp23
ebb439
+# with dst ip 4242::23 should be received by lsp23
ebb439
+test_ipv6 13 f00000000013 f00000000223 $sip $tip 23
ebb439
+
ebb439
 sip=`ip_to_hex 192 168 0 33`
ebb439
 tip=`ip_to_hex 10 0 0 15`
ebb439
 # IPv4 packet from lsp33 with src ip 192.168.0.33 destined to lsp13
ebb439
 # with dst ip 10.0.0.15 should be received by lsp13
ebb439
 test_ip 33 f00000000033 f00000000013 $sip $tip 13
ebb439
 
ebb439
+sip=42420000000000000000000000000033
ebb439
+tip=42420000000000000000000000000013
ebb439
+# IPv6 packet from lsp33 with src ip 4242::33 destined to lsp13
ebb439
+# with dst ip 4242::13 should be received by lsp13
ebb439
+test_ipv6 33 f00000000333 f00000000013 $sip $tip 13
ebb439
+
ebb439
 sip=`ip_to_hex 192 168 0 33`
ebb439
 tip=`ip_to_hex 20 0 0 4`
ebb439
 # IPv4 packet from lsp33 with src ip 192.168.0.33 destined to lsp13
ebb439
@@ -4437,6 +4453,12 @@ tip=`ip_to_hex 20 0 0 5`
ebb439
 # with dst ip 20.0.0.5 should not be received by lsp13
ebb439
 test_ip 33 f00000000033 f00000000013 $sip $tip
ebb439
 
ebb439
+sip=42420000000000000000000000000033
ebb439
+tip=42420000000000000000000000000005
ebb439
+# IPv6 packet from lsp33 with src ip 4242::33 destined to lsp13
ebb439
+# with dst ip 4242::5 should not be received by lsp13
ebb439
+test_ipv6 33 f00000000333 f00000000013 $sip $tip 13
ebb439
+
ebb439
 sip=`ip_to_hex 192 168 0 33`
ebb439
 tip=`ip_to_hex 20 0 0 255`
ebb439
 # IPv4 packet from lsp33 with src ip 192.168.0.33 destined to lsp13
ebb439
-- 
ebb439
1.8.3.1
ebb439