From: Roy Marples Date: Tue, 30 Aug 2022 20:32:04 +0000 (+0100) Subject: dhcp: allow static options to be removed by not setting a value X-Git-Tag: v10.0.0~39 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b3c41d25a3c715a3d605950fd97bee9c6f99584f;p=thirdparty%2Fdhcpcd.git dhcp: allow static options to be removed by not setting a value 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= --- diff --git a/src/dhcpcd.conf.5.in b/src/dhcpcd.conf.5.in index 742f39f2..0e976b63 100644 --- a/src/dhcpcd.conf.5.in +++ b/src/dhcpcd.conf.5.in @@ -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 diff --git a/src/if-options.c b/src/if-options.c index fd517c09..6d11a6f7 100644 --- a/src/if-options.c +++ b/src/if-options.c @@ -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;