]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
dhcpcd-run-hooks(8) should not attempt to guess the protocol.
authorRoy Marples <roy@marples.name>
Fri, 3 Nov 2017 16:38:18 +0000 (16:38 +0000)
committerRoy Marples <roy@marples.name>
Fri, 3 Nov 2017 16:38:18 +0000 (16:38 +0000)
hooks/01-test
hooks/dhcpcd-run-hooks.8.in
hooks/dhcpcd-run-hooks.in
src/script.c

index d4cf8281527f8c3001ecbd2c3cb8db9f94d397c8..6732af79e607fd1c5464f9fa00ecb65d571db765 100644 (file)
@@ -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
index b4b1d301c75b80db97a5cc5d63ef45b5bf7c4cab..3b4f289360160231a41bf506590224170eed5b3f 100644 (file)
@@ -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
index ba882abbb4f93846d98fe1821666c2bf92848dea..c1d012569ed6f4590a3be44336e80f83356c84e5 100644 (file)
@@ -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"
index f8daf893d4f11126a300f0c419b701136563a908..8efd5f6c8f8b1701509e9b25e2f61a02513ad2ea 100644 (file)
@@ -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 *) *