From: Roy Marples Date: Fri, 3 Nov 2017 16:38:18 +0000 (+0000) Subject: dhcpcd-run-hooks(8) should not attempt to guess the protocol. X-Git-Tag: v7.0.0-rc4~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=96e8dee4c29ef8c5262f9d0c27bfae61703b5984;p=thirdparty%2Fdhcpcd.git dhcpcd-run-hooks(8) should not attempt to guess the protocol. --- diff --git a/hooks/01-test b/hooks/01-test index d4cf8281..6732af79 100644 --- a/hooks/01-test +++ b/hooks/01-test @@ -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 diff --git a/hooks/dhcpcd-run-hooks.8.in b/hooks/dhcpcd-run-hooks.8.in index b4b1d301..3b4f2893 100644 --- a/hooks/dhcpcd-run-hooks.8.in +++ b/hooks/dhcpcd-run-hooks.8.in @@ -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 @@ ones. .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 diff --git a/hooks/dhcpcd-run-hooks.in b/hooks/dhcpcd-run-hooks.in index ba882abb..c1d01256 100644 --- a/hooks/dhcpcd-run-hooks.in +++ b/hooks/dhcpcd-run-hooks.in @@ -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" diff --git a/src/script.c b/src/script.c index f8daf893..8efd5f6c 100644 --- a/src/script.c +++ b/src/script.c @@ -60,6 +60,7 @@ static const char * const if_params[] = { "interface", + "protocol", "reason", "pid", "ifcarrier", @@ -217,9 +218,25 @@ arraytostr(const char *const *argv, char **s) 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 @@ make_env(const struct interface *ifp, const char *reason, char ***argv) 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 @@ make_env(const struct interface *ifp, const char *reason, char ***argv) #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 @@ make_env(const struct interface *ifp, const char *reason, char ***argv) 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 @@ make_env(const struct interface *ifp, const char *reason, char ***argv) #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 @@ make_env(const struct interface *ifp, const char *reason, char ***argv) 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 @@ make_env(const struct interface *ifp, const char *reason, char ***argv) } 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 @@ make_env(const struct interface *ifp, const char *reason, char ***argv) } } #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 @@ make_env(const struct interface *ifp, const char *reason, char ***argv) } #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 @@ make_env(const struct interface *ifp, const char *reason, char ***argv) 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 @@ dumplease: } } #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 @@ dumplease: } #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 @@ dumplease: 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 @@ dumplease: elen += (size_t)n; } } - if (ra) { + if (protocol == PROTO_RA) { n = ipv6nd_env(NULL, NULL, ifp); if (n > 0) { nenv = realloc(env, sizeof(char *) *