ebb439
From c0bb41fb85934203616758871a690093009e1d1d Mon Sep 17 00:00:00 2001
ebb439
Message-Id: <c0bb41fb85934203616758871a690093009e1d1d.1603130608.git.lorenzo.bianconi@redhat.com>
ebb439
From: Lorenzo Bianconi <lorenzo.bianconi@redhat.com>
ebb439
Date: Mon, 12 Oct 2020 11:05:54 +0200
ebb439
Subject: [PATCH] controller: IPv6 Prefix-Delegation: introduce RENEW/REBIND
ebb439
 msg support
ebb439
ebb439
Introduce RENEW/REBIND message support to OVN IPv6 PD support
ebb439
according to RFC-3633 [0] in order to renew IPv6 prefixes when
ebb439
T1/T2 are elapsed
ebb439
ebb439
[0] https://tools.ietf.org/html/rfc3633
ebb439
ebb439
Acked-by: Mark Michelson <mmichels@redhat.com>
ebb439
Signed-off-by: Lorenzo Bianconi <lorenzo.bianconi@redhat.com>
ebb439
Signed-off-by: Numan Siddique <numans@ovn.org>
ebb439
---
ebb439
 controller/pinctrl.c | 133 +++++++++++++++++++++++++++++++++----------
ebb439
 lib/ovn-l7.h         |   3 +
ebb439
 tests/system-ovn.at  |  18 ++++--
ebb439
 3 files changed, 120 insertions(+), 34 deletions(-)
ebb439
ebb439
--- a/controller/pinctrl.c
ebb439
+++ b/controller/pinctrl.c
ebb439
@@ -573,11 +573,22 @@ enum {
ebb439
     PREFIX_REQUEST,
ebb439
     PREFIX_PENDING,
ebb439
     PREFIX_DONE,
ebb439
+    PREFIX_RENEW,
ebb439
+    PREFIX_REBIND,
ebb439
 };
ebb439
 
ebb439
 struct ipv6_prefixd_state {
ebb439
     long long int next_announce;
ebb439
+    long long int last_complete;
ebb439
     long long int last_used;
ebb439
+    /* IPv6 PD server info */
ebb439
+    struct in6_addr server_addr;
ebb439
+    struct eth_addr sa;
ebb439
+    /* server_id_info */
ebb439
+    struct {
ebb439
+        uint8_t *data;
ebb439
+        uint8_t len;
ebb439
+    } uuid;
ebb439
     struct in6_addr ipv6_addr;
ebb439
     struct eth_addr ea;
ebb439
     struct eth_addr cmac;
ebb439
@@ -781,20 +792,26 @@ out:
ebb439
 static void
ebb439
 pinctrl_prefixd_state_handler(const struct flow *ip_flow,
ebb439
                               struct in6_addr addr, unsigned aid,
ebb439
+                              struct eth_addr sa, struct in6_addr server_addr,
ebb439
                               char prefix_len, unsigned t1, unsigned t2,
ebb439
-                              unsigned plife_time, unsigned vlife_time)
ebb439
+                              unsigned plife_time, unsigned vlife_time,
ebb439
+                              uint8_t *uuid, uint8_t uuid_len)
ebb439
 {
ebb439
     struct ipv6_prefixd_state *pfd;
ebb439
 
ebb439
     pfd = pinctrl_find_prefixd_state(ip_flow, aid);
ebb439
     if (pfd) {
ebb439
         pfd->state = PREFIX_PENDING;
ebb439
-        pfd->plife_time = plife_time;
ebb439
-        pfd->vlife_time = vlife_time;
ebb439
+        pfd->server_addr = server_addr;
ebb439
+        pfd->sa = sa;
ebb439
+        pfd->uuid.data = uuid;
ebb439
+        pfd->uuid.len = uuid_len;
ebb439
+        pfd->plife_time = plife_time * 1000;
ebb439
+        pfd->vlife_time = vlife_time * 1000;
ebb439
         pfd->plen = prefix_len;
ebb439
         pfd->prefix = addr;
ebb439
-        pfd->t1 = t1;
ebb439
-        pfd->t2 = t2;
ebb439
+        pfd->t1 = t1 * 1000;
ebb439
+        pfd->t2 = t2 * 1000;
ebb439
         notify_pinctrl_main();
ebb439
     }
ebb439
 }
ebb439
@@ -804,19 +821,21 @@ pinctrl_parse_dhcpv6_reply(struct dp_pac
ebb439
                            const struct flow *ip_flow)
ebb439
     OVS_REQUIRES(pinctrl_mutex)
ebb439
 {
ebb439
+    struct eth_header *eth = dp_packet_eth(pkt_in);
ebb439
+    struct ip6_hdr *in_ip = dp_packet_l3(pkt_in);
ebb439
     struct udp_header *udp_in = dp_packet_l4(pkt_in);
ebb439
     unsigned char *in_dhcpv6_data = (unsigned char *)(udp_in + 1);
ebb439
     size_t dlen = MIN(ntohs(udp_in->udp_len), dp_packet_l4_size(pkt_in));
ebb439
     unsigned t1 = 0, t2 = 0, vlife_time = 0, plife_time = 0;
ebb439
-    uint8_t *end = (uint8_t *)udp_in + dlen;
ebb439
-    uint8_t prefix_len = 0;
ebb439
-    struct in6_addr ipv6;
ebb439
+    uint8_t *end = (uint8_t *)udp_in + dlen, *uuid = NULL;
ebb439
+    uint8_t prefix_len = 0, uuid_len = 0;
ebb439
+    struct in6_addr ipv6 = in6addr_any;
ebb439
     bool status = false;
ebb439
     unsigned aid = 0;
ebb439
 
ebb439
-    memset(&ipv6, 0, sizeof (struct in6_addr));
ebb439
     /* skip DHCPv6 common header */
ebb439
     in_dhcpv6_data += 4;
ebb439
+
ebb439
     while (in_dhcpv6_data < end) {
ebb439
         struct dhcpv6_opt_header *in_opt =
ebb439
              (struct dhcpv6_opt_header *)in_dhcpv6_data;
ebb439
@@ -867,14 +886,22 @@ pinctrl_parse_dhcpv6_reply(struct dp_pac
ebb439
             }
ebb439
             break;
ebb439
         }
ebb439
+        case DHCPV6_OPT_SERVER_ID_CODE:
ebb439
+            uuid_len = ntohs(in_opt->len);
ebb439
+            uuid = xmalloc(uuid_len);
ebb439
+            memcpy(uuid, in_opt + 1, uuid_len);
ebb439
+            break;
ebb439
         default:
ebb439
             break;
ebb439
         }
ebb439
         in_dhcpv6_data += opt_len;
ebb439
     }
ebb439
     if (status) {
ebb439
-        pinctrl_prefixd_state_handler(ip_flow, ipv6, aid, prefix_len,
ebb439
-                                      t1, t2, plife_time, vlife_time);
ebb439
+        pinctrl_prefixd_state_handler(ip_flow, ipv6, aid, eth->eth_src,
ebb439
+                                      in_ip->ip6_src, prefix_len, t1, t2,
ebb439
+                                      plife_time, vlife_time, uuid, uuid_len);
ebb439
+    } else if (uuid) {
ebb439
+        free(uuid);
ebb439
     }
ebb439
 }
ebb439
 
ebb439
@@ -904,27 +931,42 @@ pinctrl_handle_dhcp6_server(struct rconn
ebb439
 }
ebb439
 
ebb439
 static void
ebb439
-compose_prefixd_solicit(struct dp_packet *b,
ebb439
-                        struct ipv6_prefixd_state *pfd,
ebb439
-                        const struct eth_addr eth_dst,
ebb439
-                        const struct in6_addr *ipv6_dst)
ebb439
+compose_prefixd_packet(struct dp_packet *b, struct ipv6_prefixd_state *pfd)
ebb439
 {
ebb439
-    eth_compose(b, eth_dst, pfd->ea, ETH_TYPE_IPV6, IPV6_HEADER_LEN);
ebb439
+    struct in6_addr ipv6_dst;
ebb439
+    struct eth_addr eth_dst;
ebb439
 
ebb439
     int payload = sizeof(struct dhcpv6_opt_server_id) +
ebb439
                   sizeof(struct dhcpv6_opt_ia_na);
ebb439
+    if (pfd->uuid.len) {
ebb439
+        payload += pfd->uuid.len + sizeof(struct dhcpv6_opt_header);
ebb439
+        ipv6_dst = pfd->server_addr;
ebb439
+        eth_dst = pfd->sa;
ebb439
+    } else {
ebb439
+        eth_dst = (struct eth_addr) ETH_ADDR_C(33,33,00,01,00,02);
ebb439
+        ipv6_parse("ff02::1:2", &ipv6_dst);
ebb439
+    }
ebb439
     if (ipv6_addr_is_set(&pfd->prefix)) {
ebb439
         payload += sizeof(struct dhcpv6_opt_ia_prefix);
ebb439
     }
ebb439
+
ebb439
+    eth_compose(b, eth_dst, pfd->ea, ETH_TYPE_IPV6, IPV6_HEADER_LEN);
ebb439
+
ebb439
     int len = UDP_HEADER_LEN + 4 + payload;
ebb439
     struct udp_header *udp_h = compose_ipv6(b, IPPROTO_UDP, &pfd->ipv6_addr,
ebb439
-                                            ipv6_dst, 0, 0, 255, len);
ebb439
+                                            &ipv6_dst, 0, 0, 255, len);
ebb439
     udp_h->udp_len = htons(len);
ebb439
     udp_h->udp_csum = 0;
ebb439
     packet_set_udp_port(b, htons(546), htons(547));
ebb439
 
ebb439
     unsigned char *dhcp_hdr = (unsigned char *)(udp_h + 1);
ebb439
-    *dhcp_hdr = DHCPV6_MSG_TYPE_SOLICIT;
ebb439
+    if (pfd->state == PREFIX_RENEW) {
ebb439
+        *dhcp_hdr = DHCPV6_MSG_TYPE_RENEW;
ebb439
+    } else if (pfd->state == PREFIX_REBIND) {
ebb439
+        *dhcp_hdr = DHCPV6_MSG_TYPE_REBIND;
ebb439
+    } else {
ebb439
+        *dhcp_hdr = DHCPV6_MSG_TYPE_SOLICIT;
ebb439
+    }
ebb439
 
ebb439
     struct dhcpv6_opt_server_id *opt_client_id =
ebb439
         (struct dhcpv6_opt_server_id *)(dhcp_hdr + 4);
ebb439
@@ -935,11 +977,21 @@ compose_prefixd_solicit(struct dp_packet
ebb439
     opt_client_id->hw_type = htons(DHCPV6_HW_TYPE_ETH);
ebb439
     opt_client_id->mac = pfd->cmac;
ebb439
 
ebb439
+    unsigned char *ptr = (unsigned char *)(opt_client_id + 1);
ebb439
+    if (pfd->uuid.len) {
ebb439
+        struct dhcpv6_opt_header *in_opt = (struct dhcpv6_opt_header *)ptr;
ebb439
+        in_opt->code = htons(DHCPV6_OPT_SERVER_ID_CODE);
ebb439
+        in_opt->len = htons(pfd->uuid.len);
ebb439
+
ebb439
+        ptr += sizeof *in_opt;
ebb439
+        memcpy(ptr, pfd->uuid.data, pfd->uuid.len);
ebb439
+        ptr += pfd->uuid.len;
ebb439
+    }
ebb439
+
ebb439
     if (!ipv6_addr_is_set(&pfd->prefix)) {
ebb439
         pfd->aid = random_uint16();
ebb439
     }
ebb439
-    struct dhcpv6_opt_ia_na *ia_pd =
ebb439
-            (struct dhcpv6_opt_ia_na *)(opt_client_id + 1);
ebb439
+    struct dhcpv6_opt_ia_na *ia_pd = (struct dhcpv6_opt_ia_na *)ptr;
ebb439
     ia_pd->opt.code = htons(DHCPV6_OPT_IA_PD);
ebb439
     int opt_len = sizeof(struct dhcpv6_opt_ia_na) -
ebb439
                   sizeof(struct dhcpv6_opt_header);
ebb439
@@ -981,16 +1033,15 @@ ipv6_prefixd_send(struct rconn *swconn,
ebb439
         return pfd->next_announce;
ebb439
     }
ebb439
 
ebb439
+    if (pfd->state == PREFIX_DONE) {
ebb439
+        goto out;
ebb439
+    }
ebb439
+
ebb439
     uint64_t packet_stub[256 / 8];
ebb439
     struct dp_packet packet;
ebb439
 
ebb439
-    struct eth_addr eth_dst;
ebb439
-    eth_dst = (struct eth_addr) ETH_ADDR_C(33,33,00,01,00,02);
ebb439
-    struct in6_addr ipv6_dst;
ebb439
-    ipv6_parse("ff02::1:2", &ipv6_dst);
ebb439
-
ebb439
     dp_packet_use_stub(&packet, packet_stub, sizeof packet_stub);
ebb439
-    compose_prefixd_solicit(&packet, pfd, eth_dst, &ipv6_dst);
ebb439
+    compose_prefixd_packet(&packet, pfd);
ebb439
 
ebb439
     uint64_t ofpacts_stub[4096 / 8];
ebb439
     struct ofpbuf ofpacts = OFPBUF_STUB_INITIALIZER(ofpacts_stub);
ebb439
@@ -1019,8 +1070,9 @@ ipv6_prefixd_send(struct rconn *swconn,
ebb439
     queue_msg(swconn, ofputil_encode_packet_out(&po, proto));
ebb439
     dp_packet_uninit(&packet);
ebb439
     ofpbuf_uninit(&ofpacts);
ebb439
+
ebb439
+out:
ebb439
     pfd->next_announce = cur_time + random_range(IPV6_PREFIXD_TIMEOUT);
ebb439
-    pfd->state = PREFIX_SOLICIT;
ebb439
 
ebb439
     return pfd->next_announce;
ebb439
 }
ebb439
@@ -1031,10 +1083,28 @@ static bool ipv6_prefixd_should_inject(v
ebb439
 
ebb439
     SHASH_FOR_EACH (iter, &ipv6_prefixd) {
ebb439
         struct ipv6_prefixd_state *pfd = iter->data;
ebb439
+        long long int cur_time = time_msec();
ebb439
+
ebb439
         if (pfd->state == PREFIX_SOLICIT) {
ebb439
             return true;
ebb439
         }
ebb439
-        if (pfd->state && pfd->next_announce < time_msec()) {
ebb439
+        if (pfd->state == PREFIX_DONE &&
ebb439
+            cur_time > pfd->last_complete + pfd->t1) {
ebb439
+            pfd->state = PREFIX_RENEW;
ebb439
+            return true;
ebb439
+        }
ebb439
+        if (pfd->state == PREFIX_RENEW &&
ebb439
+            cur_time > pfd->last_complete + pfd->t2) {
ebb439
+            pfd->state = PREFIX_REBIND;
ebb439
+            if (pfd->uuid.len) {
ebb439
+                free(pfd->uuid.data);
ebb439
+                pfd->uuid.len = 0;
ebb439
+            }
ebb439
+            return true;
ebb439
+        }
ebb439
+        if (pfd->state == PREFIX_REBIND &&
ebb439
+            cur_time > pfd->last_complete + pfd->vlife_time) {
ebb439
+            pfd->state = PREFIX_SOLICIT;
ebb439
             return true;
ebb439
         }
ebb439
     }
ebb439
@@ -1120,7 +1190,8 @@ fill_ipv6_prefix_state(struct ovsdb_idl_
ebb439
             struct smap options;
ebb439
 
ebb439
             pfd->state = PREFIX_DONE;
ebb439
-            pfd->next_announce = time_msec() + pfd->t1 * 1000;
ebb439
+            pfd->last_complete = time_msec();
ebb439
+            pfd->next_announce = pfd->last_complete + pfd->t1;
ebb439
             ipv6_string_mapped(prefix_str, &pfd->prefix);
ebb439
             smap_clone(&options, &pb->options);
ebb439
             smap_add_format(&options, "ipv6_ra_pd_list", "%d:%s/%d",
ebb439
@@ -1219,6 +1290,10 @@ prepare_ipv6_prefixd(struct ovsdb_idl_tx
ebb439
     SHASH_FOR_EACH_SAFE (iter, next, &ipv6_prefixd) {
ebb439
         struct ipv6_prefixd_state *pfd = iter->data;
ebb439
         if (pfd->last_used + IPV6_PREFIXD_STALE_TIMEOUT < time_msec()) {
ebb439
+            if (pfd->uuid.len) {
ebb439
+                free(pfd->uuid.data);
ebb439
+                pfd->uuid.len = 0;
ebb439
+            }
ebb439
             free(pfd);
ebb439
             shash_delete(&ipv6_prefixd, iter);
ebb439
         }
ebb439
--- a/lib/ovn-l7.h
ebb439
+++ b/lib/ovn-l7.h
ebb439
@@ -189,6 +189,9 @@ struct dhcp_opt6_header {
ebb439
 #define DHCPV6_MSG_TYPE_ADVT        2
ebb439
 #define DHCPV6_MSG_TYPE_REQUEST     3
ebb439
 #define DHCPV6_MSG_TYPE_CONFIRM     4
ebb439
+#define DHCPV6_MSG_TYPE_RENEW       5
ebb439
+#define DHCPV6_MSG_TYPE_REBIND      6
ebb439
+
ebb439
 #define DHCPV6_MSG_TYPE_REPLY       7
ebb439
 #define DHCPV6_MSG_TYPE_DECLINE     9
ebb439
 #define DHCPV6_MSG_TYPE_INFO_REQ    11
ebb439
--- a/tests/system-ovn.at
ebb439
+++ b/tests/system-ovn.at
ebb439
@@ -4768,19 +4768,28 @@ AT_CHECK([ovn-nbctl get logical_router_p
ebb439
 [2001:1db8:3333]
ebb439
 ])
ebb439
 
ebb439
-kill $(pidof dibbler-server)
ebb439
-
ebb439
 prefix=$(ovn-nbctl list logical_router_port rp-public | awk -F/ '/ipv6_prefix/{print substr($1,25,9)}' | sed 's/://g')
ebb439
 ovn-nbctl set logical_router_port rp-sw0 options:prefix=false
ebb439
 ovn-nbctl set logical_router_port rp-sw1 options:prefix=false
ebb439
 
ebb439
-NS_CHECK_EXEC([server], [tcpdump -c 1 -nni s1 ip6[[95:4]]=0x${prefix} > public.pcap &])
ebb439
+# Renew message
ebb439
+NS_CHECK_EXEC([server], [tcpdump -c 1 -nni s1 ip6[[48:1]]=0x05 and ip6[[113:4]]=0x${prefix} > renew.pcap &])
ebb439
+# Reply message with Status OK
ebb439
+NS_CHECK_EXEC([server], [tcpdump -c 1 -nni s1 ip6[[48:1]]=0x07 and ip6[[81:4]]=0x${prefix} and ip6[[98:1]]=0x0d and ip6[[101:2]]=0x0000 > reply.pcap &])
ebb439
+
ebb439
+OVS_WAIT_UNTIL([
ebb439
+    total_pkts=$(cat renew.pcap | wc -l)
ebb439
+    test "${total_pkts}" = "1"
ebb439
+])
ebb439
 
ebb439
 OVS_WAIT_UNTIL([
ebb439
-    total_pkts=$(cat public.pcap | wc -l)
ebb439
+    total_pkts=$(cat reply.pcap | wc -l)
ebb439
     test "${total_pkts}" = "1"
ebb439
 ])
ebb439
 
ebb439
+kill $(pidof dibbler-server)
ebb439
+kill $(pidof tcpdump)
ebb439
+
ebb439
 ovn-nbctl set logical_router_port rp-sw0 options:prefix=false
ebb439
 ovn-nbctl clear logical_router_port rp-sw0 ipv6_prefix
ebb439
 OVS_WAIT_WHILE([test "$(ovn-nbctl get logical_router_port rp-sw0 ipv6_prefix | cut -c3-16)" = "[2001:1db8:3333]"])
ebb439
@@ -4788,7 +4797,6 @@ AT_CHECK([ovn-nbctl get logical_router_p
ebb439
 []
ebb439
 ])
ebb439
 
ebb439
-kill $(pidof tcpdump)
ebb439
 kill $(pidof ovn-controller)
ebb439
 
ebb439
 as ovn-sb