From: Roy Marples Date: Thu, 22 Mar 2018 18:22:04 +0000 (+0000) Subject: For IPv4, override any existing address when adding it. X-Git-Tag: v7.0.2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a9b676cca2a3e30b3fc174abe853a301f7ee8b40;p=thirdparty%2Fdhcpcd.git For IPv4, override any existing address when adding it. When deleting stale addresses, report our pid as the deletor. --- diff --git a/src/ipv4.c b/src/ipv4.c index 4ba13d53..d40b3f63 100644 --- a/src/ipv4.c +++ b/src/ipv4.c @@ -589,6 +589,7 @@ ipv4_addaddr(struct interface *ifp, const struct in_addr *addr, { struct ipv4_state *state; struct ipv4_addr *ia; + bool is_new = false; #ifdef ALIAS_ADDR int replaced, blank; struct ipv4_addr *replaced_ia; @@ -607,18 +608,23 @@ ipv4_addaddr(struct interface *ifp, const struct in_addr *addr, } } - if ((ia = malloc(sizeof(*ia))) == NULL) { - logerr(__func__); - return NULL; + ia = ipv4_iffindaddr(ifp, addr, NULL); + if (ia == NULL) { + ia = malloc(sizeof(*ia)); + if (ia == NULL) { + logerr(__func__); + return NULL; + } + ia->iface = ifp; + ia->addr = *addr; +#ifdef IN_IFF_TENTATIVE + ia->addr_flags = IN_IFF_TENTATIVE; +#endif + is_new = true; } - ia->iface = ifp; - ia->addr = *addr; ia->mask = *mask; ia->brd = *bcast; -#ifdef IN_IFF_TENTATIVE - ia->addr_flags = IN_IFF_TENTATIVE; -#endif snprintf(ia->saddr, sizeof(ia->saddr), "%s/%d", inet_ntoa(*addr), inet_ntocidr(*mask)); @@ -650,7 +656,8 @@ ipv4_addaddr(struct interface *ifp, const struct in_addr *addr, } #endif - TAILQ_INSERT_TAIL(&state->addrs, ia, next); + if (is_new) + TAILQ_INSERT_TAIL(&state->addrs, ia, next); return ia; } @@ -787,10 +794,11 @@ ipv4_deletestaleaddrs(struct interface *ifp) return; TAILQ_FOREACH_SAFE(ia, &state->addrs, next, ia1) { - if (ia->flags & IPV4_AF_STALE) - ipv4_handleifa(ifp->ctx, RTM_DELADDR, - ifp->ctx->ifaces, ifp->name, - &ia->addr, &ia->mask, &ia->brd, 0, 0); + if (!(ia->flags & IPV4_AF_STALE)) + continue; + ipv4_handleifa(ifp->ctx, RTM_DELADDR, + ifp->ctx->ifaces, ifp->name, + &ia->addr, &ia->mask, &ia->brd, 0, getpid()); } } @@ -805,6 +813,12 @@ ipv4_handleifa(struct dhcpcd_ctx *ctx, struct ipv4_addr *ia; bool ia_is_new; +#if 0 + logdebugx("%s: %s %s/%d %d", ifname, + cmd == RTM_NEWADDR ? "RTM_NEWADDR" : cmd == RTM_DELADDR ? "RTM_DELADDR" : "???", + inet_ntoa(*addr), inet_ntocidr(*mask), addrflags); +#endif + if (ifs == NULL) ifs = ctx->ifaces; if (ifs == NULL) { diff --git a/src/ipv6.c b/src/ipv6.c index b88757af..82cf71b6 100644 --- a/src/ipv6.c +++ b/src/ipv6.c @@ -2124,7 +2124,7 @@ ipv6_deletestaleaddrs(struct interface *ifp) if (ia->flags & IPV6_AF_STALE) ipv6_handleifa(ifp->ctx, RTM_DELADDR, ifp->ctx->ifaces, ifp->name, - &ia->addr, ia->prefix_len, 0, 0); + &ia->addr, ia->prefix_len, 0, getpid()); } }