]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
if: Keep the PF_LINK socket open throughout
authorRoy Marples <roy@marples.name>
Fri, 5 Jun 2020 11:23:51 +0000 (12:23 +0100)
committerRoy Marples <roy@marples.name>
Fri, 5 Jun 2020 11:23:51 +0000 (12:23 +0100)
Saves opening it and closing it each time we discover interfaces.

src/dhcpcd.c
src/dhcpcd.h
src/if.c

index dddf328107b07ac33aaf0b1edd679898af3e281e..2dbfb4ed278f3142d2947a2d02ccc86c6301ede6 100644 (file)
@@ -1820,6 +1820,9 @@ main(int argc, char **argv)
        ctx.script = UNCONST(dhcpcd_default_script);
        ctx.control_fd = ctx.control_unpriv_fd = ctx.link_fd = -1;
        ctx.pf_inet_fd = -1;
+#ifdef PF_LINK
+       ctx.pf_link_fd = -1;
+#endif
 
        TAILQ_INIT(&ctx.control_fds);
 #ifdef USE_SIGNALS
index f3ce6c4b5cb578a71e4b7205d286f39534c8c01e..21497559a112b00187310ff15c4d78f7c5b2aeb8 100644 (file)
@@ -151,6 +151,9 @@ struct dhcpcd_ctx {
        size_t rt_order;        /* route order storage */
 
        int pf_inet_fd;
+#ifdef PF_LINK
+       int pf_link_fd;
+#endif
        void *priv;
        int link_fd;
 #ifndef SMALL
index acf8dcac259d69025996f1100c5d9c57f0e0d774..d0012771de126c3ce2fc481ae5af1cf4449385fb 100644 (file)
--- a/src/if.c
+++ b/src/if.c
@@ -107,6 +107,12 @@ if_opensockets(struct dhcpcd_ctx *ctx)
        if (if_opensockets_os(ctx) == -1)
                return -1;
 
+#ifdef PF_LINK
+       ctx->pf_link_fd = xsocket(PF_LINK, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+       if (ctx->pf_link_fd == -1)
+               return -1;
+#endif
+
        /* We use this socket for some operations without INET. */
        ctx->pf_inet_fd = xsocket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
        if (ctx->pf_inet_fd == -1)
@@ -121,6 +127,8 @@ if_closesockets(struct dhcpcd_ctx *ctx)
 
        if (ctx->pf_inet_fd != -1)
                close(ctx->pf_inet_fd);
+       if (ctx->pf_link_fd != -1)
+               close(ctx->pf_link_fd);
 
        if (ctx->priv) {
                if_closesockets_os(ctx);
@@ -379,7 +387,6 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs,
        const struct sockaddr_dl *sdl;
 #ifdef IFLR_ACTIVE
        struct if_laddrreq iflr = { .flags = IFLR_PREFIX };
-       int link_fd;
 #endif
 #elif defined(AF_PACKET)
        const struct sockaddr_ll *sll;
@@ -409,15 +416,6 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs,
                return NULL;
        }
 
-#ifdef IFLR_ACTIVE
-       link_fd = xsocket(PF_LINK, SOCK_DGRAM | SOCK_CLOEXEC, 0);
-       if (link_fd == -1) {
-               logerr(__func__);
-               free(ifs);
-               return NULL;
-       }
-#endif
-
        for (ifa = *ifaddrs; ifa; ifa = ifa->ifa_next) {
                if (ifa->ifa_addr != NULL) {
 #ifdef AF_LINK
@@ -514,7 +512,7 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs,
                            MIN(ifa->ifa_addr->sa_len, sizeof(iflr.addr)));
                        iflr.flags = IFLR_PREFIX;
                        iflr.prefixlen = (unsigned int)sdl->sdl_alen * NBBY;
-                       if (ioctl(link_fd, SIOCGLIFADDR, &iflr) == -1 ||
+                       if (ioctl(ctx->pf_link_fd, SIOCGLIFADDR, &iflr) == -1 ||
                            !(iflr.flags & IFLR_ACTIVE))
                        {
                                if_free(ifp);
@@ -648,9 +646,6 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs,
                TAILQ_INSERT_TAIL(ifs, ifp, next);
        }
 
-#ifdef IFLR_ACTIVE
-       close(link_fd);
-#endif
        return ifs;
 }