secchan: Log switch datapath id at startup.
[sliver-openvswitch.git] / secchan / secchan.c
index a937d09..78ccf30 100644 (file)
@@ -62,6 +62,7 @@
 #include "openflow.h"
 #include "packets.h"
 #include "poll-loop.h"
+#include "port-array.h"
 #include "rconn.h"
 #include "stp.h"
 #include "timeval.h"
@@ -69,6 +70,7 @@
 #include "vconn-ssl.h"
 #include "vconn.h"
 #include "vlog-socket.h"
+#include "xtoxll.h"
 
 #include "vlog.h"
 #define THIS_MODULE VLM_secchan
@@ -193,11 +195,19 @@ static struct hook port_watcher_create(struct rconn *local,
                                        struct rconn *remote,
                                        struct port_watcher **);
 static uint32_t port_watcher_get_config(const struct port_watcher *,
-                                       int port_no);
-static void port_watcher_set_flags(struct port_watcher *, int port_no, 
+                                        uint16_t port_no);
+static const char *port_watcher_get_name(const struct port_watcher *,
+                                         uint16_t port_no) UNUSED;
+static const uint8_t *port_watcher_get_hwaddr(const struct port_watcher *,
+                                              uint16_t port_no);
+static void port_watcher_set_flags(struct port_watcher *, uint16_t port_no, 
                                    uint32_t config, uint32_t c_mask,
                                    uint32_t state, uint32_t s_mask);
 
+#ifdef SUPPORT_SNAT
+static struct hook snat_hook_create(struct port_watcher *pw);
+#endif
+
 static struct hook stp_hook_create(const struct settings *,
                                    struct port_watcher *,
                                    struct rconn *local, struct rconn *remote);
@@ -291,6 +301,9 @@ main(int argc, char *argv[])
     /* Set up hooks. */
     hooks[n_hooks++] = port_watcher_create(local_rconn, remote_rconn, &pw);
     discovery = s.discovery ? discovery_init(&s, pw, switch_status) : NULL;
+#ifdef SUPPORT_SNAT
+    hooks[n_hooks++] = snat_hook_create(pw);
+#endif
     if (s.enable_stp) {
         hooks[n_hooks++] = stp_hook_create(&s, pw, local_rconn, remote_rconn);
     }
@@ -622,13 +635,14 @@ struct port_watcher_local_cb {
 struct port_watcher {
     struct rconn *local_rconn;
     struct rconn *remote_rconn;
-    struct ofp_phy_port ports[OFPP_MAX + 1];
+    struct port_array ports;
     time_t last_feature_request;
     bool got_feature_reply;
+    uint64_t datapath_id;
     int n_txq;
     struct port_watcher_cb cbs[2];
     int n_cbs;
-    struct port_watcher_local_cb local_cbs[2];
+    struct port_watcher_local_cb local_cbs[4];
     int n_local_cbs;
     char local_port_name[OFP_MAX_PORT_NAME_LEN + 1];
 };
@@ -663,25 +677,15 @@ sanitize_opp(struct ofp_phy_port *opp)
     opp->name[sizeof opp->name - 1] = '\0';
 }
 
-static int
-port_no_to_pw_idx(int port_no)
-{
-    return (port_no < OFPP_MAX ? port_no
-            : port_no == OFPP_LOCAL ? OFPP_MAX
-            : -1);
-}
-
 static void
 call_port_changed_callbacks(struct port_watcher *pw, int port_no,
                             const struct ofp_phy_port *old,
                             const struct ofp_phy_port *new)
 {
-    if (opp_differs(old, new)) {
-        int i;
-        for (i = 0; i < pw->n_cbs; i++) {
-            port_changed_cb_func *port_changed = pw->cbs[i].port_changed;
-            (port_changed)(port_no, old, new, pw->cbs[i].aux);
-        }
+    int i;
+    for (i = 0; i < pw->n_cbs; i++) {
+        port_changed_cb_func *port_changed = pw->cbs[i].port_changed;
+        (port_changed)(port_no, old, new, pw->cbs[i].aux);
     }
 }
 
@@ -699,6 +703,12 @@ get_port_name(const struct ofp_phy_port *port, char *name, size_t name_size)
     }
 }
 
+static struct ofp_phy_port *
+lookup_port(const struct port_watcher *pw, uint16_t port_no)
+{
+    return port_array_get(&pw->ports, port_no);
+}
+
 static void
 call_local_port_changed_callbacks(struct port_watcher *pw)
 {
@@ -708,10 +718,7 @@ call_local_port_changed_callbacks(struct port_watcher *pw)
 
     /* Pass the local port to the callbacks, if it exists.
        Pass a null pointer if there is no local port. */
-    port = &pw->ports[port_no_to_pw_idx(OFPP_LOCAL)];
-    if (port->port_no != htons(OFPP_LOCAL)) {
-        port = NULL;
-    }
+    port = lookup_port(pw, OFPP_LOCAL);
 
     /* Log the name of the local port. */
     if (port) {
@@ -737,33 +744,29 @@ call_local_port_changed_callbacks(struct port_watcher *pw)
 
 static void
 update_phy_port(struct port_watcher *pw, struct ofp_phy_port *opp,
-                uint8_t reason, bool seen[OFPP_MAX + 1])
+                uint8_t reason)
 {
-    struct ofp_phy_port *pw_opp;
-    struct ofp_phy_port old;
+    struct ofp_phy_port *old;
     uint16_t port_no;
-    int idx;
 
     port_no = ntohs(opp->port_no);
-    idx = port_no_to_pw_idx(port_no);
-    if (idx < 0) {
-        return;
-    }
-
-    if (seen) {
-        seen[idx] = true;
-    }
-
-    pw_opp = &pw->ports[idx];
-    old = *pw_opp;
-    if (reason == OFPPR_DELETE) {
-        memset(pw_opp, 0, sizeof *pw_opp);
-        pw_opp->port_no = htons(OFPP_NONE);
-    } else if (reason == OFPPR_MODIFY || reason == OFPPR_ADD) {
-        *pw_opp = *opp;
-        sanitize_opp(pw_opp);
+    old = lookup_port(pw, port_no);
+
+    if (reason == OFPPR_DELETE && old) {
+        call_port_changed_callbacks(pw, port_no, old, NULL);
+        free(old);
+        port_array_set(&pw->ports, port_no, NULL);
+    } else if ((reason == OFPPR_MODIFY || reason == OFPPR_ADD)
+               && (!old || opp_differs(opp, old))) {
+        struct ofp_phy_port new = *opp;
+        sanitize_opp(&new);
+        call_port_changed_callbacks(pw, port_no, old, &new);
+        if (old) {
+            *old = new;
+        } else {
+            port_array_set(&pw->ports, port_no, xmemdup(&new, sizeof new));
+        }
     }
-    call_port_changed_callbacks(pw, port_no, &old, pw_opp);
 }
 
 static bool
@@ -776,25 +779,33 @@ port_watcher_local_packet_cb(struct relay *r, void *pw_)
     if (oh->type == OFPT_FEATURES_REPLY
         && msg->size >= offsetof(struct ofp_switch_features, ports)) {
         struct ofp_switch_features *osf = msg->data;
-        bool seen[ARRAY_SIZE(pw->ports)];
+        bool seen[PORT_ARRAY_SIZE];
+        struct ofp_phy_port *p;
+        unsigned int port_no;
         size_t n_ports;
         size_t i;
 
         pw->got_feature_reply = true;
+        if (pw->datapath_id != osf->datapath_id) {
+            pw->datapath_id = osf->datapath_id;
+            VLOG_WARN("Datapath id is %012"PRIx64, ntohll(pw->datapath_id));
+        }
 
         /* Update each port included in the message. */
-        memset(seen, 0, sizeof seen);
+        memset(seen, false, sizeof seen);
         n_ports = ((msg->size - offsetof(struct ofp_switch_features, ports))
                    / sizeof *osf->ports);
         for (i = 0; i < n_ports; i++) {
             struct ofp_phy_port *opp = &osf->ports[i];
-            update_phy_port(pw, opp, OFPPR_MODIFY, seen);
+            update_phy_port(pw, opp, OFPPR_MODIFY);
+            seen[ntohs(opp->port_no)] = true;
         }
 
         /* Delete all the ports not included in the message. */
-        for (i = 0; i < ARRAY_SIZE(pw->ports); i++) {
-            if (!seen[i]) {
-                update_phy_port(pw, &pw->ports[i], OFPPR_DELETE, NULL);
+        for (p = port_array_first(&pw->ports, &port_no); p;
+             p = port_array_next(&pw->ports, &port_no)) {
+            if (!seen[port_no]) {
+                update_phy_port(pw, p, OFPPR_DELETE);
             }
         }
 
@@ -802,7 +813,7 @@ port_watcher_local_packet_cb(struct relay *r, void *pw_)
     } else if (oh->type == OFPT_PORT_STATUS
                && msg->size >= sizeof(struct ofp_port_status)) {
         struct ofp_port_status *ops = msg->data;
-        update_phy_port(pw, &ops->desc, ops->reason, NULL);
+        update_phy_port(pw, &ops->desc, ops->reason);
         if (ops->desc.port_no == htons(OFPP_LOCAL)) {
             call_local_port_changed_callbacks(pw);
         }
@@ -821,17 +832,14 @@ port_watcher_remote_packet_cb(struct relay *r, void *pw_)
         && msg->size >= sizeof(struct ofp_port_mod)) {
         struct ofp_port_mod *opm = msg->data;
         uint16_t port_no = ntohs(opm->port_no);
-        int idx = port_no_to_pw_idx(port_no);
-        if (idx >= 0) {
-            struct ofp_phy_port *pw_opp = &pw->ports[idx];
-            if (pw_opp->port_no != htons(OFPP_NONE)) {
-                struct ofp_phy_port old = *pw_opp;
-                pw_opp->config = ((pw_opp->config & ~opm->mask)
-                                 | (opm->config & opm->mask));
-                call_port_changed_callbacks(pw, port_no, &old, pw_opp);
-                if (pw_opp->port_no == htons(OFPP_LOCAL)) {
-                    call_local_port_changed_callbacks(pw);
-                }
+        struct ofp_phy_port *pw_opp = lookup_port(pw, port_no);
+        if (pw_opp->port_no != htons(OFPP_NONE)) {
+            struct ofp_phy_port old = *pw_opp;
+            pw_opp->config = ((pw_opp->config & ~opm->mask)
+                              | (opm->config & opm->mask));
+            call_port_changed_callbacks(pw, port_no, &old, pw_opp);
+            if (pw_opp->port_no == htons(OFPP_LOCAL)) {
+                call_local_port_changed_callbacks(pw);
             }
         }
     }
@@ -914,37 +922,31 @@ log_port_status(uint16_t port_no,
                 void *aux)
 {
     if (VLOG_IS_DBG_ENABLED()) {
-        bool was_enabled = old->port_no != htons(OFPP_NONE);
-        bool now_enabled = new->port_no != htons(OFPP_NONE);
-        uint32_t curr = ntohl(new->curr);
-        uint32_t supported = ntohl(new->supported);
-        struct ds ds;
-
-        if (((old->config != new->config) || (old->state != new->state))
-                && opp_differs(old, new) == 1) {
-            /* Don't care if only flags changed. */
-            return;
-        }
-
-        ds_init(&ds);
-        ds_put_format(&ds, "\"%s\", "ETH_ADDR_FMT, new->name,
-                      ETH_ADDR_ARGS(new->hw_addr));
-        if (curr) {
-            put_features(&ds, ", current", curr);
-        }
-        if (supported) {
-            put_features(&ds, ", supports", supported);
-        }
-        if (was_enabled != now_enabled) {
-            if (now_enabled) {
-                VLOG_DBG("Port %d added: %s", port_no, ds_cstr(&ds));
-            } else {
+        if (old && new && (opp_differs(old, new)
+                           == ((old->config != new->config)
+                               + (old->state != new->state))))
+        {
+            /* Don't care if only state or config changed. */
+        } else if (!new) {
+            if (old) {
                 VLOG_DBG("Port %d deleted", port_no);
             }
         } else {
-            VLOG_DBG("Port %d changed: %s", port_no, ds_cstr(&ds));
+            struct ds ds = DS_EMPTY_INITIALIZER;
+            uint32_t curr = ntohl(new->curr);
+            uint32_t supported = ntohl(new->supported);
+            ds_put_format(&ds, "\"%s\", "ETH_ADDR_FMT, new->name,
+                          ETH_ADDR_ARGS(new->hw_addr));
+            if (curr) {
+                put_features(&ds, ", current", curr);
+            }
+            if (supported) {
+                put_features(&ds, ", supports", supported);
+            }
+            VLOG_DBG("Port %d %s: %s",
+                     port_no, old ? "changed" : "added", ds_cstr(&ds));
+            ds_destroy(&ds);
         }
-        ds_destroy(&ds);
     }
 }
 
@@ -971,14 +973,28 @@ port_watcher_register_local_port_callback(struct port_watcher *pw,
 }
 
 static uint32_t
-port_watcher_get_config(const struct port_watcher *pw, int port_no)
+port_watcher_get_config(const struct port_watcher *pw, uint16_t port_no)
+{
+    struct ofp_phy_port *p = lookup_port(pw, port_no);
+    return p ? ntohl(p->config) : 0;
+}
+
+static const char *
+port_watcher_get_name(const struct port_watcher *pw, uint16_t port_no)
+{
+    struct ofp_phy_port *p = lookup_port(pw, port_no);
+    return p ? (const char *) p->name : NULL;
+}
+
+static const uint8_t *
+port_watcher_get_hwaddr(const struct port_watcher *pw, uint16_t port_no) 
 {
-    int idx = port_no_to_pw_idx(port_no);
-    return idx >= 0 ? ntohl(pw->ports[idx].config) : 0;
+    struct ofp_phy_port *p = lookup_port(pw, port_no);
+    return p ? p->hw_addr : NULL;
 }
 
 static void
-port_watcher_set_flags(struct port_watcher *pw, int port_no, 
+port_watcher_set_flags(struct port_watcher *pw, uint16_t port_no, 
                        uint32_t config, uint32_t c_mask,
                        uint32_t state, uint32_t s_mask)
 {
@@ -987,14 +1003,12 @@ port_watcher_set_flags(struct port_watcher *pw, int port_no,
     struct ofp_port_mod *opm;
     struct ofp_port_status *ops;
     struct ofpbuf *b;
-    int idx;
 
-    idx = port_no_to_pw_idx(port_no);
-    if (idx < 0) {
+    p = lookup_port(pw, port_no);
+    if (!p) {
         return;
     }
 
-    p = &pw->ports[idx];
     if (!((ntohl(p->state) ^ state) & s_mask) 
             && (!((ntohl(p->config) ^ config) & c_mask))) {
         return;
@@ -1033,15 +1047,12 @@ port_watcher_create(struct rconn *local_rconn, struct rconn *remote_rconn,
                     struct port_watcher **pwp)
 {
     struct port_watcher *pw;
-    int i;
 
     pw = *pwp = xcalloc(1, sizeof *pw);
     pw->local_rconn = local_rconn;
     pw->remote_rconn = remote_rconn;
     pw->last_feature_request = TIME_MIN;
-    for (i = 0; i < OFPP_MAX; i++) {
-        pw->ports[i].port_no = htons(OFPP_NONE);
-    }
+    port_array_init(&pw->ports);
     pw->local_port_name[0] = '\0';
     port_watcher_register_callback(pw, log_port_status, NULL);
     return make_hook(port_watcher_local_packet_cb,
@@ -1050,6 +1061,248 @@ port_watcher_create(struct rconn *local_rconn, struct rconn *remote_rconn,
                      port_watcher_wait_cb, pw);
 }
 \f
+#ifdef SUPPORT_SNAT
+struct snat_port_conf {
+    struct list node;
+    struct nx_snat_config config;
+};
+
+struct snat_data {
+    struct port_watcher *pw;
+    struct list port_list;
+};
+
+
+/* Source-NAT configuration monitor. */
+#define SNAT_CMD_LEN 1024
+
+/* Commands to configure iptables.  There is no programmatic interface
+ * to iptables from the kernel, so we're stuck making command-line calls
+ * in user-space. */
+#define SNAT_FLUSH_ALL_CMD "/sbin/iptables -t nat -F"
+#define SNAT_FLUSH_CHAIN_CMD "/sbin/iptables -t nat -F of-snat-%s"
+
+#define SNAT_ADD_CHAIN_CMD "/sbin/iptables -t nat -N of-snat-%s"
+#define SNAT_CONF_CHAIN_CMD "/sbin/iptables -t nat -A POSTROUTING -o %s -j of-snat-%s"
+
+#define SNAT_ADD_IP_CMD "/sbin/iptables -t nat -A of-snat-%s -j SNAT --to %s-%s"
+#define SNAT_ADD_TCP_CMD "/sbin/iptables -t nat -A of-snat-%s -j SNAT -p TCP --to %s-%s:%d-%d"
+#define SNAT_ADD_UDP_CMD "/sbin/iptables -t nat -A of-snat-%s -j SNAT -p UDP --to %s-%s:%d-%d"
+
+#define SNAT_UNSET_CHAIN_CMD "/sbin/iptables -t nat -D POSTROUTING -o %s -j of-snat-%s"
+#define SNAT_DEL_CHAIN_CMD "/sbin/iptables -t nat -X of-snat-%s"
+
+static void 
+snat_add_rules(const struct nx_snat_config *sc, const uint8_t *dev_name)
+{
+    char command[SNAT_CMD_LEN];
+    char ip_str_start[16];
+    char ip_str_end[16];
+
+
+    snprintf(ip_str_start, sizeof ip_str_start, IP_FMT, 
+            IP_ARGS(&sc->ip_addr_start));
+    snprintf(ip_str_end, sizeof ip_str_end, IP_FMT, 
+            IP_ARGS(&sc->ip_addr_end));
+
+    /* We always attempt to remove existing entries, so that we know
+     * there's a pristine state for SNAT on the interface.  We just ignore 
+     * the results of these calls, since iptables will complain about 
+     * any non-existent entries. */
+
+    /* Flush the chain that does the SNAT. */
+    snprintf(command, sizeof(command), SNAT_FLUSH_CHAIN_CMD, dev_name);
+    system(command);
+
+    /* We always try to create the a new chain. */
+    snprintf(command, sizeof(command), SNAT_ADD_CHAIN_CMD, dev_name);
+    system(command);
+
+    /* Disassociate any old SNAT chain from the POSTROUTING chain. */
+    snprintf(command, sizeof(command), SNAT_UNSET_CHAIN_CMD, dev_name, 
+            dev_name);
+    system(command);
+
+    /* Associate the new chain with the POSTROUTING hook. */
+    snprintf(command, sizeof(command), SNAT_CONF_CHAIN_CMD, dev_name, 
+            dev_name);
+    if (system(command) != 0) {
+        VLOG_ERR("SNAT: problem flushing chain for add");
+        return;
+    }
+
+    /* If configured, restrict TCP source port ranges. */
+    if ((sc->tcp_start != 0) && (sc->tcp_end != 0)) {
+        snprintf(command, sizeof(command), SNAT_ADD_TCP_CMD, 
+                dev_name, ip_str_start, ip_str_end,
+                ntohs(sc->tcp_start), ntohs(sc->tcp_end));
+        if (system(command) != 0) {
+            VLOG_ERR("SNAT: problem adding TCP rule");
+            return;
+        }
+    }
+
+    /* If configured, restrict UDP source port ranges. */
+    if ((sc->udp_start != 0) && (sc->udp_end != 0)) {
+        snprintf(command, sizeof(command), SNAT_ADD_UDP_CMD, 
+                dev_name, ip_str_start, ip_str_end,
+                ntohs(sc->udp_start), ntohs(sc->udp_end));
+        if (system(command) != 0) {
+            VLOG_ERR("SNAT: problem adding UDP rule");
+            return;
+        }
+    }
+
+    /* Add a rule that covers all IP traffic that would not be covered
+     * by the prior TCP or UDP ranges. */
+    snprintf(command, sizeof(command), SNAT_ADD_IP_CMD, 
+            dev_name, ip_str_start, ip_str_end);
+    if (system(command) != 0) {
+        VLOG_ERR("SNAT: problem adding base rule");
+        return;
+    }
+}
+
+static void 
+snat_del_rules(const uint8_t *dev_name)
+{
+    char command[SNAT_CMD_LEN];
+
+    /* Flush the chain that does the SNAT. */
+    snprintf(command, sizeof(command), SNAT_FLUSH_CHAIN_CMD, dev_name);
+    if (system(command) != 0) {
+        VLOG_ERR("SNAT: problem flushing chain for deletion");
+        return;
+    }
+
+    /* Disassociate the SNAT chain from the POSTROUTING chain. */
+    snprintf(command, sizeof(command), SNAT_UNSET_CHAIN_CMD, dev_name, 
+            dev_name);
+    if (system(command) != 0) {
+        VLOG_ERR("SNAT: problem unsetting chain");
+        return;
+    }
+
+    /* Now we can finally delete our SNAT chain. */
+    snprintf(command, sizeof(command), SNAT_DEL_CHAIN_CMD, dev_name);
+    if (system(command) != 0) {
+        VLOG_ERR("SNAT: problem deleting chain");
+        return;
+    }
+}
+
+static void 
+snat_config(const struct nx_snat_config *sc, struct snat_data *snat)
+{
+    struct snat_port_conf *c, *spc=NULL;
+    const uint8_t *netdev_name;
+
+    netdev_name = (const uint8_t *) port_watcher_get_name(snat->pw,
+                                                          ntohs(sc->port));
+    if (!netdev_name) {
+        return;
+    }
+
+    LIST_FOR_EACH(c, struct snat_port_conf, node, &snat->port_list) {
+        if (c->config.port == sc->port) {
+            spc = c;
+            break;
+        }
+    }
+
+    if (sc->command == NXSC_ADD) {
+        if (!spc) {
+            spc = xmalloc(sizeof(*c));
+            if (!spc) {
+                VLOG_ERR("SNAT: no memory for new entry");
+                return;
+            }
+            list_push_back(&snat->port_list, &spc->node);
+        }
+        memcpy(&spc->config, sc, sizeof(spc->config));
+        snat_add_rules(sc, netdev_name);
+    } else if (spc) {
+        snat_del_rules(netdev_name);
+        list_remove(&spc->node);
+    }
+}
+
+static bool
+snat_remote_packet_cb(struct relay *r, void *snat_)
+{
+    struct snat_data *snat = snat_;
+    struct ofpbuf *msg = r->halves[HALF_REMOTE].rxbuf;
+    struct nicira_header *request = msg->data;
+    struct nx_act_config *nac = msg->data;
+    int n_configs, i;
+
+
+    if (msg->size < sizeof(struct nx_act_config)) {
+        return false;
+    }
+    request = msg->data;
+    if (request->header.type != OFPT_VENDOR
+        || request->vendor != htonl(NX_VENDOR_ID)
+        || request->subtype != htonl(NXT_ACT_SET_CONFIG)) {
+        return false;
+    }
+
+    /* We're only interested in attempts to configure SNAT */
+    if (nac->type != htons(NXAST_SNAT)) {
+        return false;
+    }
+
+    n_configs = (msg->size - sizeof *nac) / sizeof *nac->snat;
+    for (i=0; i<n_configs; i++) {
+        snat_config(&nac->snat[i], snat);
+    }
+
+    return false;
+}
+
+static void
+snat_port_changed_cb(uint16_t port_no,
+                    const struct ofp_phy_port *old,
+                    const struct ofp_phy_port *new,
+                    void *snat_)
+{
+    struct snat_data *snat = snat_;
+    struct snat_port_conf *c;
+
+    /* We're only interested in ports that went away */
+    if (old && !new) {
+        return;
+    }
+
+    LIST_FOR_EACH(c, struct snat_port_conf, node, &snat->port_list) {
+        if (c->config.port == old->port_no) {
+            snat_del_rules(old->name);
+            list_remove(&c->node);
+            return;
+        }
+    }
+}
+
+static struct hook
+snat_hook_create(struct port_watcher *pw)
+{
+    int ret;
+    struct snat_data *snat;
+
+    ret = system(SNAT_FLUSH_ALL_CMD); 
+    if (ret != 0) {
+        VLOG_ERR("SNAT: problem flushing tables");
+    }
+
+    snat = xcalloc(1, sizeof *snat);
+    snat->pw = pw;
+    list_init(&snat->port_list);
+
+    port_watcher_register_callback(pw, snat_port_changed_cb, snat);
+    return make_hook(NULL, snat_remote_packet_cb, NULL, NULL, snat);
+}
+#endif /* SUPPORT_SNAT */
+\f
 /* Spanning tree protocol. */
 
 /* Extra time, in seconds, at boot before going into fail-open, to give the
@@ -1210,10 +1463,17 @@ static void
 send_bpdu(const void *bpdu, size_t bpdu_size, int port_no, void *stp_)
 {
     struct stp_data *stp = stp_;
+    const uint8_t *port_mac;
     struct eth_header *eth;
     struct llc_header *llc;
     struct ofpbuf pkt, *opo;
 
+    port_mac = port_watcher_get_hwaddr(stp->pw, port_no);
+    if (!port_mac) {
+        VLOG_WARN_RL(&vrl, "cannot send BPDU on missing port %d", port_no);
+        return;
+    }
+
     /* Packet skeleton. */
     ofpbuf_init(&pkt, ETH_HEADER_LEN + LLC_HEADER_LEN + bpdu_size);
     eth = ofpbuf_put_uninit(&pkt, sizeof *eth);
@@ -1222,7 +1482,7 @@ send_bpdu(const void *bpdu, size_t bpdu_size, int port_no, void *stp_)
 
     /* 802.2 header. */
     memcpy(eth->eth_dst, stp_eth_addr, ETH_ADDR_LEN);
-    memcpy(eth->eth_src, stp->pw->ports[port_no].hw_addr, ETH_ADDR_LEN);
+    memcpy(eth->eth_src, port_mac, ETH_ADDR_LEN);
     eth->eth_type = htons(pkt.size - ETH_HEADER_LEN);
 
     /* LLC header. */
@@ -1238,9 +1498,6 @@ send_bpdu(const void *bpdu, size_t bpdu_size, int port_no, void *stp_)
 static bool
 stp_is_port_supported(uint16_t port_no)
 {
-    /* We should be able to support STP on all possible OpenFlow physical
-     * ports.  (But we don't support STP on OFPP_LOCAL.)  */
-    BUILD_ASSERT_DECL(STP_MAX_PORTS >= OFPP_MAX);
     return port_no < STP_MAX_PORTS;
 }
 
@@ -1258,7 +1515,7 @@ stp_port_changed_cb(uint16_t port_no,
     }
 
     p = stp_get_port(stp->stp, port_no);
-    if (new->port_no == htons(OFPP_NONE)
+    if (!new
         || new->config & htonl(OFPPC_NO_STP | OFPPC_PORT_DOWN)
         || new->state & htonl(OFPPS_LINK_DOWN)) {
         stp_port_disable(p);
@@ -1579,7 +1836,7 @@ fail_open_periodic_cb(void *fail_open_)
     if (time_now() < fail_open->boot_deadline) {
         return;
     }
-    disconn_secs = rconn_disconnected_duration(fail_open->remote_rconn);
+    disconn_secs = rconn_failure_duration(fail_open->remote_rconn);
     open = disconn_secs >= fail_open->s->probe_interval * 3;
     if (open != (fail_open->lswitch != NULL)) {
         if (!open) {
@@ -1620,7 +1877,7 @@ fail_open_status_cb(struct status_reply *sr, void *fail_open_)
     struct fail_open_data *fail_open = fail_open_;
     const struct settings *s = fail_open->s;
     int trigger_duration = s->probe_interval * 3;
-    int cur_duration = rconn_disconnected_duration(fail_open->remote_rconn);
+    int cur_duration = rconn_failure_duration(fail_open->remote_rconn);
 
     status_reply_put(sr, "trigger-duration=%d", trigger_duration);
     status_reply_put(sr, "current-duration=%d", cur_duration);
@@ -1890,7 +2147,7 @@ switch_status_remote_packet_cb(struct relay *r, void *ss_)
     }
     request = msg->data;
     if (request->header.type != OFPT_VENDOR
-        || request->vendor_id != htonl(NX_VENDOR_ID)
+        || request->vendor != htonl(NX_VENDOR_ID)
         || request->subtype != htonl(NXT_STATUS_REQUEST)) {
         return false;
     }
@@ -1907,7 +2164,7 @@ switch_status_remote_packet_cb(struct relay *r, void *ss_)
     }
     reply = make_openflow_xid(sizeof *reply + sr.output.length,
                               OFPT_VENDOR, request->header.xid, &b);
-    reply->vendor_id = htonl(NX_VENDOR_ID);
+    reply->vendor = htonl(NX_VENDOR_ID);
     reply->subtype = htonl(NXT_STATUS_REPLY);
     memcpy(reply + 1, sr.output.string, sr.output.length);
     retval = rconn_send(rc, b, NULL);