]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
dhcp: allow static options to be removed by not setting a value
authorRoy Marples <roy@marples.name>
Tue, 30 Aug 2022 20:32:04 +0000 (21:32 +0100)
committerRoy Marples <roy@marples.name>
Tue, 30 Aug 2022 20:35:08 +0000 (21:35 +0100)
This allows this config:

interface eth0
arping 1.2.3.4
static ip_address=5.6.7.8/24

profile 1.2.3.4
# Allow DHCP
static ip_address=

src/dhcpcd.conf.5.in
src/if-options.c

index 742f39f2b06f7f4cb1acb77fd061af63afee6dd4..0e976b6367ea36658d6b0b7e60e603e6136ce3a4 100644 (file)
@@ -668,6 +668,13 @@ then
 .Nm dhcpcd
 will not attempt to obtain a lease and will just use the value for the address
 with an infinite lease time.
+If you set an empty value this removes all prior static allocations to
+the same value.
+This is useful when using profiles and in the case of
+.Ic ip_address
+it will remove the static allocation.
+Note that setting 0.0.0.0 keeps the static allocation but waits for a 3rdparty
+to configure the address.
 If you set
 .Ic ip6_address ,
 .Nm dhcpcd
@@ -695,7 +702,7 @@ It uses the special
 keyword to insert the destination address
 into the value.
 .D1 interface ppp0
-.D1 static ip_address=
+.D1 static ip_address=0.0.0.0
 .D1 destination routers
 .It Ic timeout Ar seconds
 Time out after
index fd517c09867f54211c6458c771a3dc99afd8cb12..6d11a6f759a9edaa9076191baf821fc12fcfbd47 100644 (file)
@@ -1110,8 +1110,13 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo,
                        logerrx("static assignment required");
                        return -1;
                }
-               p++;
+               p = strskipwhite(++p);
                if (strncmp(arg, "ip_address=", strlen("ip_address=")) == 0) {
+                       if (p == NULL) {
+                               ifo->options &= ~DHCPCD_STATIC;
+                               ifo->req_addr.s_addr = INADDR_ANY;
+                               break;
+                       }
                        if (parse_addr(&ifo->req_addr,
                            ifo->req_mask.s_addr == 0 ? &ifo->req_mask : NULL,
                            p) != 0)
@@ -1122,11 +1127,19 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo,
                } else if (strncmp(arg, "subnet_mask=",
                    strlen("subnet_mask=")) == 0)
                {
+                       if (p == NULL) {
+                               ifo->req_mask.s_addr = INADDR_ANY;
+                               break;
+                       }
                        if (parse_addr(&ifo->req_mask, NULL, p) != 0)
                                return -1;
                } else if (strncmp(arg, "broadcast_address=",
                    strlen("broadcast_address=")) == 0)
                {
+                       if (p == NULL) {
+                               ifo->req_brd.s_addr = INADDR_ANY;
+                               break;
+                       }
                        if (parse_addr(&ifo->req_brd, NULL, p) != 0)
                                return -1;
                } else if (strncmp(arg, "routes=", strlen("routes=")) == 0 ||
@@ -1139,6 +1152,12 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo,
                {
                        struct in_addr addr3;
 
+                       if (p == NULL) {
+                               rt_headclear(&ifo->routes, AF_INET);
+                               add_environ(&ifo->config, arg, 1);
+                               break;
+                       }
+
                        fp = np = strwhite(p);
                        if (np == NULL) {
                                logerrx("all routes need a gateway");
@@ -1161,6 +1180,11 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo,
                        if (rt_proto_add_ctx(&ifo->routes, rt, ctx))
                                add_environ(&ifo->config, arg, 0);
                } else if (strncmp(arg, "routers=", strlen("routers=")) == 0) {
+                       if (p == NULL) {
+                               rt_headclear(&ifo->routes, AF_INET);
+                               add_environ(&ifo->config, arg, 1);
+                               break;
+                       }
                        if (parse_addr(&addr, NULL, p) == -1)
                                return -1;
                        if ((rt = rt_new0(ctx)) == NULL)
@@ -1175,6 +1199,8 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo,
                    strlen("interface_mtu=")) == 0 ||
                    strncmp(arg, "mtu=", strlen("mtu=")) == 0)
                {
+                       if (p == NULL)
+                               break;
                        ifo->mtu = (unsigned int)strtou(p, NULL, 0,
                            MTU_MIN, MTU_MAX, &e);
                        if (e) {
@@ -1182,6 +1208,12 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo,
                                return -1;
                        }
                } else if (strncmp(arg, "ip6_address=", strlen("ip6_address=")) == 0) {
+                       if (p == NULL) {
+                               memset(&ifo->req_addr6, 0,
+                                   sizeof(ifo->req_addr6));
+                               break;
+                       }
+
                        np = strchr(p, '/');
                        if (np)
                                *np++ = '\0';
@@ -1207,8 +1239,9 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo,
                                return -1;
                        }
                } else
-                       add_environ(&ifo->config, arg, 1);
+                       add_environ(&ifo->config, arg, p == NULL ? 1 : 0);
                break;
+
        case 'W':
                if (parse_addr(&addr, &addr2, arg) != 0)
                        return -1;