config: remove 'ignore' config option
authorDainis Jonitis <dainis.jonitis@ubnt.com>
Fri, 12 Apr 2019 10:46:43 +0000 (13:46 +0300)
committerHans Dedecker <dedeckeh@gmail.com>
Wed, 15 May 2019 08:57:12 +0000 (10:57 +0200)
1. Remove 'ignore' config option for interface. Interface is still used if
   any of 'ra', 'ndp', 'dhcpv4' or 'dhcpv6' options are different from
   default 'disabled' mode. In config_parse_interface() update runtime
   ignore value according to all mode values.
2. Reset ignore, ra, ndp, dhcpv4 and dhcpv6 values in set_interface_defaults().
   Otherwise if option is deleted from config file and config is reloaded,
   it will use the value from old config, potentiallly not turning services off.
3. Do not use implicit checks that 0 server mode means MODE_DISABLED.
4. Simplify code of all service setup functions if passed enabled = true
   argument, but service mode is disabled.

Signed-off-by: Dainis Jonitis <dainis.jonitis@ubnt.com>
Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
README
src/config.c
src/dhcpv4.c
src/dhcpv6-ia.c
src/dhcpv6.c
src/ndp.c
src/router.c

diff --git a/README b/README
index 49e1c51a0711eb6dafe883a8b072d4b2c30262b3..a379c4cfbf5a028a3d05ad4ce3ab5d27b32af144 100644 (file)
--- a/README
+++ b/README
@@ -73,9 +73,6 @@ Option                        Type    Default                 Description
 interface              string  <name of UCI section>   logical OpenWrt interface
 ifname                 string  <resolved from logical> physical network interface
 networkid              string  same as ifname          compat. alias for ifname
-ignore                 bool    0                       do not serve this interface
-                                                       unless overridden by ra, ndp,
-                                                       dhcpv4 or dhcpv6 options
 master                 bool    0                       is a master interface
                                                        for relaying
 
index 40f18a4b27371f538ccd884d7cb07d505bf88734..3f64d16bd2f33fa47410c5bf97653fa2d6bac307 100644 (file)
@@ -40,7 +40,6 @@ enum {
        IFACE_ATTR_IFNAME,
        IFACE_ATTR_NETWORKID,
        IFACE_ATTR_DYNAMICDHCP,
-       IFACE_ATTR_IGNORE,
        IFACE_ATTR_LEASETIME,
        IFACE_ATTR_LIMIT,
        IFACE_ATTR_START,
@@ -86,7 +85,6 @@ static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
        [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
        [IFACE_ATTR_NETWORKID] = { .name = "networkid", .type = BLOBMSG_TYPE_STRING },
        [IFACE_ATTR_DYNAMICDHCP] = { .name = "dynamicdhcp", .type = BLOBMSG_TYPE_BOOL },
-       [IFACE_ATTR_IGNORE] = { .name = "ignore", .type = BLOBMSG_TYPE_BOOL },
        [IFACE_ATTR_LEASETIME] = { .name = "leasetime", .type = BLOBMSG_TYPE_STRING },
        [IFACE_ATTR_START] = { .name = "start", .type = BLOBMSG_TYPE_INT32 },
        [IFACE_ATTR_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
@@ -211,6 +209,11 @@ static int mkdir_p(char *dir, mode_t mask)
 
 static void set_interface_defaults(struct interface *iface)
 {
+       iface->ignore = true;
+       iface->dhcpv4 = MODE_DISABLED;
+       iface->dhcpv6 = MODE_DISABLED;
+       iface->ra = MODE_DISABLED;
+       iface->ndp = MODE_DISABLED;
        iface->learn_routes = 1;
        iface->dhcpv4_leasetime = 43200;
        iface->dhcpv4_start.s_addr = htonl(START_DEFAULT);
@@ -496,9 +499,6 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
        if ((c = tb[IFACE_ATTR_DYNAMICDHCP]))
                iface->no_dynamic_dhcp = !blobmsg_get_bool(c);
 
-       if (overwrite && (c = tb[IFACE_ATTR_IGNORE]))
-               iface->ignore = blobmsg_get_bool(c);
-
        if ((c = tb[IFACE_ATTR_LEASETIME])) {
                double time = parse_leasetime(c);
                if (time < 0)
@@ -543,32 +543,45 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
 
        int mode;
        if ((c = tb[IFACE_ATTR_RA])) {
-               if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
+               if ((mode = parse_mode(blobmsg_get_string(c))) >= 0) {
                        iface->ra = mode;
-               else
+
+                       if (iface->ra != MODE_DISABLED)
+                               iface->ignore = false;
+               } else
                        goto err;
        }
 
        if ((c = tb[IFACE_ATTR_DHCPV4])) {
                if ((mode = parse_mode(blobmsg_get_string(c))) >= 0) {
-                       if (config.main_dhcpv4)
+                       if (config.main_dhcpv4) {
                                iface->dhcpv4 = mode;
+
+                               if (iface->dhcpv4 != MODE_DISABLED)
+                                       iface->ignore = false;
+                       }
                }
                else
                        goto err;
        }
 
        if ((c = tb[IFACE_ATTR_DHCPV6])) {
-               if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
+               if ((mode = parse_mode(blobmsg_get_string(c))) >= 0) {
                        iface->dhcpv6 = mode;
-               else
+
+                       if (iface->dhcpv6 != MODE_DISABLED)
+                               iface->ignore = false;
+               } else
                        goto err;
        }
 
        if ((c = tb[IFACE_ATTR_NDP])) {
-               if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
+               if ((mode = parse_mode(blobmsg_get_string(c))) >= 0) {
                        iface->ndp = mode;
-               else
+
+                       if (iface->ndp != MODE_DISABLED)
+                               iface->ignore = false;
+               } else
                        goto err;
        }
 
@@ -1067,11 +1080,11 @@ void odhcpd_reload(void)
                                i->ndp = (master && master->ndp == MODE_RELAY) ?
                                                MODE_RELAY : MODE_DISABLED;
 
-                       router_setup_interface(i, !i->ignore || i->ra != MODE_DISABLED);
-                       dhcpv6_setup_interface(i, !i->ignore || i->dhcpv6 != MODE_DISABLED);
-                       ndp_setup_interface(i, !i->ignore || i->ndp != MODE_DISABLED);
+                       router_setup_interface(i, i->ra != MODE_DISABLED);
+                       dhcpv6_setup_interface(i, i->dhcpv6 != MODE_DISABLED);
+                       ndp_setup_interface(i, i->ndp != MODE_DISABLED);
 #ifdef DHCPV4_SUPPORT
-                       dhcpv4_setup_interface(i, !i->ignore || i->dhcpv4 != MODE_DISABLED);
+                       dhcpv4_setup_interface(i, i->dhcpv4 != MODE_DISABLED);
 #endif
                } else
                        close_interface(i);
index 2634f65a69e997c5292c7c7f3679709c45cf62d4..34d925c4266c069283c3fbd9ed37783610c93f8e 100644 (file)
@@ -77,13 +77,15 @@ int dhcpv4_setup_interface(struct interface *iface, bool enable)
 {
        int ret = 0;
 
+       enable = enable && (iface->dhcpv4 != MODE_DISABLED);
+
        if (iface->dhcpv4_event.uloop.fd >= 0) {
                uloop_fd_delete(&iface->dhcpv4_event.uloop);
                close(iface->dhcpv4_event.uloop.fd);
                iface->dhcpv4_event.uloop.fd = -1;
        }
 
-       if (iface->dhcpv4 && enable) {
+       if (enable) {
                struct sockaddr_in bind_addr = {AF_INET, htons(DHCPV4_SERVER_PORT),
                                        {INADDR_ANY}, {0}};
                int val = 1;
@@ -592,7 +594,7 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
 {
        struct dhcpv4_message *req = data;
 
-       if (!iface->dhcpv4)
+       if (iface->dhcpv4 == MODE_DISABLED)
                return;
 
        if (len < offsetof(struct dhcpv4_message, options) + 4 ||
index 9559b9655a9ce4e26d49cbedd0429687919da02c..c0dfa2d7d18cf3b05ce98742fe310fae5b9736f2 100644 (file)
@@ -62,16 +62,9 @@ int dhcpv6_ia_init(void)
 
 int dhcpv6_ia_setup_interface(struct interface *iface, bool enable)
 {
-       if (!enable) {
-               struct dhcp_assignment *c;
-
-               while (!list_empty(&iface->ia_assignments)) {
-                       c = list_first_entry(&iface->ia_assignments, struct dhcp_assignment, head);
-                       free_assignment(c);
-               }
-       }
+       enable = enable && (iface->dhcpv6 == MODE_SERVER);
 
-       if (enable && iface->dhcpv6 == MODE_SERVER) {
+       if (enable) {
                struct dhcp_assignment *border;
 
                if (list_empty(&iface->ia_assignments)) {
@@ -88,7 +81,15 @@ int dhcpv6_ia_setup_interface(struct interface *iface, bool enable)
                        border = list_last_entry(&iface->ia_assignments, struct dhcp_assignment, head);
 
                set_border_assignment_size(iface, border);
+       } else {
+               struct dhcp_assignment *c;
+
+               while (!list_empty(&iface->ia_assignments)) {
+                       c = list_first_entry(&iface->ia_assignments, struct dhcp_assignment, head);
+                       free_assignment(c);
+               }
        }
+
        return 0;
 }
 
index f71418d9e05fe5df9ed41cddd277bd56410d44db..c61a8aa5c03e2c17ec5c91848bf7af46caa31f0d 100644 (file)
@@ -47,6 +47,8 @@ int dhcpv6_setup_interface(struct interface *iface, bool enable)
 {
        int ret = 0;
 
+       enable = enable && (iface->dhcpv6 != MODE_DISABLED);
+
        if (iface->dhcpv6_event.uloop.fd >= 0) {
                uloop_fd_delete(&iface->dhcpv6_event.uloop);
                close(iface->dhcpv6_event.uloop.fd);
@@ -54,7 +56,7 @@ int dhcpv6_setup_interface(struct interface *iface, bool enable)
        }
 
        /* Configure multicast settings */
-       if (enable && iface->dhcpv6) {
+       if (enable) {
                struct sockaddr_in6 bind_addr = {AF_INET6, htons(DHCPV6_SERVER_PORT),
                                        0, IN6ADDR_ANY_INIT, 0};
                struct ipv6_mreq mreq;
index 64a6c1c5ce40fcf1face31391943bc59f36eb6a0..aaaa69c15501564fc42a4b32215546ff0887e959 100644 (file)
--- a/src/ndp.c
+++ b/src/ndp.c
@@ -71,6 +71,8 @@ int ndp_setup_interface(struct interface *iface, bool enable)
        bool dump_neigh = false;
        char procbuf[64];
 
+       enable = enable && (iface->ndp == MODE_RELAY);
+
        snprintf(procbuf, sizeof(procbuf), "/proc/sys/net/ipv6/conf/%s/proxy_ndp", iface->ifname);
        procfd = open(procbuf, O_WRONLY);
 
@@ -89,13 +91,13 @@ int ndp_setup_interface(struct interface *iface, bool enable)
                close(iface->ndp_event.uloop.fd);
                iface->ndp_event.uloop.fd = -1;
 
-               if (!enable || iface->ndp != MODE_RELAY)
+               if (!enable)
                        if (write(procfd, "0\n", 2) < 0) {}
 
                dump_neigh = true;
        }
 
-       if (enable && iface->ndp == MODE_RELAY) {
+       if (enable) {
                struct sockaddr_ll ll;
                struct packet_mreq mreq;
                struct icmp6_filter filt;
index 3a6b12154ae4af1e818452696b2321a1ba553e0a..63571a3f025623242a187b20c7944fd66d3aea1f 100644 (file)
@@ -74,13 +74,15 @@ int router_setup_interface(struct interface *iface, bool enable)
 {
        int ret = 0;
 
+       enable = enable && (iface->ra != MODE_DISABLED);
+
        if (!fp_route) {
                ret = -1;
                goto out;
        }
 
 
-       if ((!enable || iface->ra == MODE_DISABLED) && iface->router_event.uloop.fd >= 0) {
+       if (!enable && iface->router_event.uloop.fd >= 0) {
                if (!iface->master) {
                        uloop_timeout_cancel(&iface->timer_rs);
                        iface->timer_rs.cb = NULL;
@@ -91,7 +93,7 @@ int router_setup_interface(struct interface *iface, bool enable)
                uloop_fd_delete(&iface->router_event.uloop);
                close(iface->router_event.uloop.fd);
                iface->router_event.uloop.fd = -1;
-       } else if (enable && iface->ra != MODE_DISABLED) {
+       } else if (enable) {
                struct icmp6_filter filt;
                struct ipv6_mreq mreq;
                int val = 2;
@@ -434,7 +436,7 @@ static int send_router_advert(struct interface *iface, const struct in6_addr *fr
        if (hlim > 0)
                adv.h.nd_ra_curhoplimit = hlim;
 
-       if (iface->dhcpv6) {
+       if (iface->dhcpv6 != MODE_DISABLED) {
                adv.h.nd_ra_flags_reserved = ND_RA_FLAG_OTHER;
 
                if (iface->ra_managed >= RA_MANAGED_MFLAG)