changeset 4241:616f73cad975 draft

For IPv4, override any existing address when adding it. When deleting stale addresses, report our pid as the deletor.
author Roy Marples <roy@marples.name>
date Thu, 22 Mar 2018 18:22:04 +0000
parents 989526071625
children 9f4dc610f30c
files src/ipv4.c src/ipv6.c
diffstat 2 files changed, 28 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/ipv4.c	Thu Mar 22 18:21:46 2018 +0000
+++ b/src/ipv4.c	Thu Mar 22 18:22:04 2018 +0000
@@ -589,6 +589,7 @@
 {
 	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 @@
 		}
 	}
 
-	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 @@
 	}
 #endif
 
-	TAILQ_INSERT_TAIL(&state->addrs, ia, next);
+	if (is_new)
+		TAILQ_INSERT_TAIL(&state->addrs, ia, next);
 	return ia;
 }
 
@@ -787,10 +794,11 @@
 		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 @@
 	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) {
--- a/src/ipv6.c	Thu Mar 22 18:21:46 2018 +0000
+++ b/src/ipv6.c	Thu Mar 22 18:22:04 2018 +0000
@@ -2124,7 +2124,7 @@
 		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());
 	}
 }