changeset 4176:612d80156afd draft

dhcpcd-run-hooks(8) should not attempt to guess the protocol.
author Roy Marples <roy@marples.name>
date Fri, 03 Nov 2017 16:38:18 +0000
parents 2a14528ebe96
children bce060648873
files hooks/01-test hooks/dhcpcd-run-hooks.8.in hooks/dhcpcd-run-hooks.in src/script.c
diffstat 4 files changed, 52 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/hooks/01-test	Wed Oct 25 14:56:23 2017 +0100
+++ b/hooks/01-test	Fri Nov 03 16:38:18 2017 +0000
@@ -1,7 +1,8 @@
 # Echo the interface flags, reason and message options
 
 if [ "$reason" = "TEST" ]; then
-	set | grep "^\(interface\|pid\|reason\|profile\|skip_hooks\)=" | sort
+	set | grep \
+	    "^\(interface\|pid\|reason\|protocol\|profile\|skip_hooks\)=" | sort
 	set | grep "^if\(carrier\|flags\|mtu\|wireless\|ssid\)=" | sort
 	set | grep "^\(new_\|old_\|nd[0-9]*_\)" | sort
 	exit 0
--- a/hooks/dhcpcd-run-hooks.8.in	Wed Oct 25 14:56:23 2017 +0100
+++ b/hooks/dhcpcd-run-hooks.8.in	Fri Nov 03 16:38:18 2017 +0000
@@ -22,7 +22,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd January 23, 2017
+.Dd November 3, 2017
 .Dt DHCPCD-RUN-HOOKS 8
 .Os
 .Sh NAME
@@ -139,6 +139,8 @@
 .Bl -tag -width xnew_delegated_dhcp6_prefix
 .It Ev $interface
 the name of the interface.
+.It Ev $protocol
+the protocol that triggered the event.
 .It Ev $reason
 as described above.
 .It Ev $pid
--- a/hooks/dhcpcd-run-hooks.in	Wed Oct 25 14:56:23 2017 +0100
+++ b/hooks/dhcpcd-run-hooks.in	Fri Nov 03 16:38:18 2017 +0000
@@ -2,22 +2,7 @@
 # dhcpcd client configuration script 
 
 # Handy variables and functions for our hooks to use
-case "$reason" in
-	PREINIT|CARRIER|NOCARRIER|DEPARTED|STOPPED|DUMP|TEST)
-		ifsuffix=;; #unset
-	ROUTERADVERT)
-		ifsuffix=".ra";;
-	INFORM6|BOUND6|RENEW6|REBIND6|REBOOT6|EXPIRE6|RELEASE6|STOP6)
-		ifsuffix=".dhcp6";;
-	IPV4LL)
-		ifsuffix=".ipv4ll";;
-	INFORM|BOUND|RENEW|REBIND|REBOOT|EXPIRE|RELEASE|STOP)
-		ifsuffix=".dhcp";;
-	*)
-		ifsuffix=;; #unset
-esac
-ifname="$interface$ifsuffix"
-
+ifname="$interface${protocol+.}$protocol"
 from=from
 signature_base="# Generated by dhcpcd"
 signature="$signature_base $from $ifname"
--- a/src/script.c	Wed Oct 25 14:56:23 2017 +0100
+++ b/src/script.c	Fri Nov 03 16:38:18 2017 +0000
@@ -60,6 +60,7 @@
 
 static const char * const if_params[] = {
 	"interface",
+	"protocol",
 	"reason",
 	"pid",
 	"ifcarrier",
@@ -217,9 +218,25 @@
 	return (ssize_t)len;
 }
 
+#define	PROTO_NONE	0
+#define	PROTO_DHCP	1
+#define	PROTO_IPV4LL	2
+#define	PROTO_RA	3
+#define	PROTO_DHCP6	4
+#define	PROTO_STATIC6	5
+static const char *protocols[] = {
+	NULL,
+	"dhcp",
+	"ipv4ll",
+	"ra",
+	"dhcp6",
+	"static6"
+};
+
 static ssize_t
 make_env(const struct interface *ifp, const char *reason, char ***argv)
 {
+	int protocol, r;
 	char **env, **nenv, *p;
 	size_t e, elen, l;
 #if defined(INET) || defined(INET6)
@@ -229,7 +246,6 @@
 	const struct interface *ifp2;
 	int af;
 #ifdef INET
-	int dhcp, ipv4ll;
 	const struct dhcp_state *state;
 #ifdef IPV4LL
 	const struct ipv4ll_state *istate;
@@ -237,44 +253,41 @@
 #endif
 #ifdef INET6
 	const struct dhcp6_state *d6_state;
-	int static6, dhcp6, ra;
 #endif
 
 #ifdef INET
-	dhcp = ipv4ll = 0;
 	state = D_STATE(ifp);
 #ifdef IPV4LL
 	istate = IPV4LL_CSTATE(ifp);
 #endif
 #endif
 #ifdef INET6
-	static6 = dhcp6 = ra = 0;
 	d6_state = D6_CSTATE(ifp);
 #endif
 	if (strcmp(reason, "TEST") == 0) {
 		if (1 == 2) {}
 #ifdef INET6
 		else if (d6_state && d6_state->new)
-			dhcp6 = 1;
+			protocol = PROTO_DHCP6;
 		else if (ipv6nd_hasra(ifp))
-			ra = 1;
+			protocol = PROTO_RA;
 #endif
 #ifdef INET
 #ifdef IPV4LL
 		else if (istate && istate->addr != NULL)
-			ipv4ll = 1;
+			protocol = PROTO_IPV4LL;
 #endif
 		else
-			dhcp = 1;
+			protocol = PROTO_DHCP;
 #endif
 	}
 #ifdef INET6
 	else if (strcmp(reason, "STATIC6") == 0)
-		static6 = 1;
+		protocol = PROTO_STATIC6;
 	else if (reason[strlen(reason) - 1] == '6')
-		dhcp6 = 1;
+		protocol = PROTO_DHCP6;
 	else if (strcmp(reason, "ROUTERADVERT") == 0)
-		ra = 1;
+		protocol = PROTO_RA;
 #endif
 	else if (strcmp(reason, "PREINIT") == 0 ||
 	    strcmp(reason, "CARRIER") == 0 ||
@@ -282,16 +295,14 @@
 	    strcmp(reason, "UNKNOWN") == 0 ||
 	    strcmp(reason, "DEPARTED") == 0 ||
 	    strcmp(reason, "STOPPED") == 0)
-	{
-		/* This space left intentionally blank */
-	}
+		protocol = PROTO_NONE;
 #ifdef INET
 #ifdef IPV4LL
 	else if (strcmp(reason, "IPV4LL") == 0)
-		ipv4ll = 1;
+		protocol = PROTO_IPV4LL;
 #endif
 	else
-		dhcp = 1;
+		protocol = PROTO_DHCP;
 #endif
 
 	/* When dumping the lease, we only want to report interface and
@@ -303,7 +314,7 @@
 
 #define EMALLOC(i, l) if ((env[(i)] = malloc((l))) == NULL) goto eexit;
 	/* Make our env + space for profile, wireless and debug */
-	env = calloc(1, sizeof(char *) * (elen + 4 + 1));
+	env = calloc(1, sizeof(char *) * (elen + 5 + 1));
 	if (env == NULL)
 		goto eexit;
 	e = strlen("interface") + strlen(ifp->name) + 2;
@@ -361,15 +372,15 @@
 		env[10] = strdup("if_down=false");
 	} else if (1 == 2 /* appease ifdefs */
 #ifdef INET
-	    || (dhcp && state && state->new)
+	    || (protocol == PROTO_DHCP && state && state->new)
 #ifdef IPV4LL
-	    || (ipv4ll && IPV4LL_STATE_RUNNING(ifp))
+	    || (protocol == PROTO_IPV4LL && IPV4LL_STATE_RUNNING(ifp))
 #endif
 #endif
 #ifdef INET6
-	    || (static6 && IPV6_STATE_RUNNING(ifp))
-	    || (dhcp6 && d6_state && d6_state->new)
-	    || (ra && ipv6nd_hasra(ifp))
+	    || (protocol == PROTO_STATIC6 && IPV6_STATE_RUNNING(ifp))
+	    || (protocol == PROTO_DHCP6 && d6_state && d6_state->new)
+	    || (protocol == PROTO_RA && ipv6nd_hasra(ifp))
 #endif
 	    )
 	{
@@ -381,6 +392,12 @@
 	}
 	if (env[9] == NULL || env[10] == NULL)
 		goto eexit;
+	if (protocols[protocol] != NULL) {
+		r = asprintf(&env[elen], "protocol=%s", protocols[protocol]);
+		if (r == -1)
+			goto eexit;
+		elen++;
+	}
 	if ((af = dhcpcd_ifafwaiting(ifp)) != AF_MAX) {
 		e = 20;
 		EMALLOC(elen, e);
@@ -425,7 +442,7 @@
 		}
 	}
 #ifdef INET
-	if (dhcp && state && state->old) {
+	if (protocol == PROTO_DHCP && state && state->old) {
 		n = dhcp_env(NULL, NULL, state->old, state->old_len, ifp);
 		if (n == -1)
 			goto eexit;
@@ -447,7 +464,7 @@
 	}
 #endif
 #ifdef INET6
-	if (dhcp6 && d6_state && d6_state->old) {
+	if (protocol == PROTO_DHCP6 && d6_state && d6_state->old) {
 		n = dhcp6_env(NULL, NULL, ifp,
 		    d6_state->old, d6_state->old_len);
 		if (n > 0) {
@@ -468,7 +485,7 @@
 dumplease:
 #ifdef INET
 #ifdef IPV4LL
-	if (ipv4ll) {
+	if (protocol == PROTO_IPV4LL) {
 		n = ipv4ll_env(NULL, NULL, ifp);
 		if (n > 0) {
 			nenv = realloc(env, sizeof(char *) *
@@ -483,7 +500,7 @@
 		}
 	}
 #endif
-	if (dhcp && state && state->new) {
+	if (protocol == PROTO_DHCP && state && state->new) {
 		n = dhcp_env(NULL, NULL, state->new, state->new_len, ifp);
 		if (n > 0) {
 			nenv = realloc(env, sizeof(char *) *
@@ -503,7 +520,7 @@
 	}
 #endif
 #ifdef INET6
-	if (static6) {
+	if (protocol == PROTO_STATIC6) {
 		n = ipv6_env(NULL, NULL, ifp);
 		if (n > 0) {
 			nenv = realloc(env, sizeof(char *) *
@@ -517,7 +534,7 @@
 			elen += (size_t)n;
 		}
 	}
-	if (dhcp6 && D6_STATE_RUNNING(ifp)) {
+	if (protocol == PROTO_DHCP6 && D6_STATE_RUNNING(ifp)) {
 		n = dhcp6_env(NULL, NULL, ifp,
 		    d6_state->new, d6_state->new_len);
 		if (n > 0) {
@@ -533,7 +550,7 @@
 			elen += (size_t)n;
 		}
 	}
-	if (ra) {
+	if (protocol == PROTO_RA) {
 		n = ipv6nd_env(NULL, NULL, ifp);
 		if (n > 0) {
 			nenv = realloc(env, sizeof(char *) *