Skip to content

Commit

Permalink
Fix dhcpcd sending wrong message when RENEWING/REBINDING
Browse files Browse the repository at this point in the history
When RENEWING/REBINDING, the DHCP client should send messages with
ciaddr set and requested-ip unset. However when dhcpcd 10 is configured
with --noconfigure, the messages sent are the opposite: ciaddr unset
and requested-ip set - these messages are for INIT-REBOOT, not RENEWING
or REBINDING.

The reason for it is that when --noconfigure is enabled, dhcpcd won't
setup the state->addr property of the interface when the lease is bound.
When dhcpcd renews or rebinds the lease later, it needs state->addr to
be set to correctly configure ciaddr and requested-ip. Since state->addr
is NULL, dhcpcd sends wrong messages (for the detail see
github.com/NetworkConfiguration/issues/355).

This patch fixes this by also setting up state->addr with
ipv4_applyaddr() when --noconfigure is enabled. Note that this function
also configure the IPv4 address and build routes in the system, so we
need to disable these in the function.

Change-Id: I94bfffd95b62066995bd436d3b8bf185eafda398
  • Loading branch information
acst1223 committed Aug 31, 2024
1 parent ab5ec18 commit bb968ea
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 31 deletions.
29 changes: 4 additions & 25 deletions src/dhcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2374,24 +2374,6 @@ dhcp_bind(struct interface *ifp)

old_state = state->added;

if (!(ifo->options & DHCPCD_CONFIGURE)) {
struct ipv4_addr *ia;

script_runreason(ifp, state->reason);
dhcpcd_daemonise(ifp->ctx);

/* We we are not configuring the address, we need to keep
* the BPF socket open if the address does not exist. */
ia = ipv4_iffindaddr(ifp, &state->lease.addr, NULL);
if (ia != NULL) {
state->addr = ia;
state->added = STATE_ADDED;
dhcp_closebpf(ifp);
goto openudp;
}
return;
}

/* Add the address */
if (ipv4_applyaddr(ifp) == NULL) {
/* There was an error adding the address.
Expand All @@ -2403,6 +2385,9 @@ dhcp_bind(struct interface *ifp)
return;
}

if (!(ifo->options & DHCPCD_CONFIGURE))
return;

/* Close the BPF filter as we can now receive DHCP messages
* on a UDP socket. */
dhcp_closebpf(ifp);
Expand Down Expand Up @@ -2848,13 +2833,7 @@ dhcp_drop(struct interface *ifp, const char *reason)
state->new = NULL;
state->new_len = 0;
state->reason = reason;
if (ifp->options->options & DHCPCD_CONFIGURE)
ipv4_applyaddr(ifp);
else {
state->addr = NULL;
state->added = 0;
script_runreason(ifp, state->reason);
}
ipv4_applyaddr(ifp);
free(state->old);
state->old = NULL;
state->old_len = 0;
Expand Down
15 changes: 9 additions & 6 deletions src/ipv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,13 @@ ipv4_deladdr(struct ipv4_addr *addr, int keeparp)
logdebugx("%s: deleting IP address %s",
addr->iface->name, addr->saddr);

r = if_address(RTM_DELADDR, addr);
if (r == -1 &&
errno != EADDRNOTAVAIL && errno != ESRCH &&
errno != ENXIO && errno != ENODEV)
logerr("%s: %s", addr->iface->name, __func__);
if (addr->iface->options->options & DHCPCD_CONFIGURE) {
r = if_address(RTM_DELADDR, addr);
if (r == -1 &&
errno != EADDRNOTAVAIL && errno != ESRCH &&
errno != ENXIO && errno != ENODEV)
logerr("%s: %s", addr->iface->name, __func__);
}

#ifdef ARP
if (!keeparp)
Expand Down Expand Up @@ -721,7 +723,8 @@ ipv4_addaddr(struct interface *ifp, const struct in_addr *addr,
ifp->name, ia->saddr,
ifp->flags & IFF_POINTOPOINT ? "destination" : "broadcast",
inet_ntoa(*bcast));
if (if_address(RTM_NEWADDR, ia) == -1) {
if (ifp->options->options & DHCPCD_CONFIGURE &&
if_address(RTM_NEWADDR, ia) == -1) {
if (errno != EEXIST)
logerr("%s: if_addaddress",
__func__);
Expand Down
4 changes: 4 additions & 0 deletions src/route.c
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,10 @@ rt_doroute(rb_tree_t *kroutes, struct rt *rt)
void
rt_build(struct dhcpcd_ctx *ctx, int af)
{
if (!(ctx->options & DHCPCD_CONFIGURE)) {
return;
}

rb_tree_t routes, added, kroutes;
struct rt *rt, *rtn;
unsigned long long o;
Expand Down

0 comments on commit bb968ea

Please sign in to comment.