]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Linux: Use the persistent route socket for writing netlink msgs
authorRoy Marples <roy@marples.name>
Thu, 16 Jan 2020 17:33:24 +0000 (17:33 +0000)
committerRoy Marples <roy@marples.name>
Thu, 16 Jan 2020 17:33:24 +0000 (17:33 +0000)
This reverts 2c8dc9ee7.
This *should* be fine because we can detect ourself and privsep
sending messages so we can ignore them.

src/if-linux.c

index d42383b58754ec3fecb6ff0b599494e6dd02b458..4765a7f74679fb34d66fe99db2cc83eeb8a0aff5 100644 (file)
@@ -653,6 +653,11 @@ link_route(struct dhcpcd_ctx *ctx, __unused struct interface *ifp,
        }
 
        /* Ignore messages we sent. */
+#ifdef PRIVSEP
+       if (ctx->ps_root_pid != 0 &&
+           nlm->nlmsg_pid == (uint32_t)ctx->ps_root_pid)
+               return 0;
+#endif
        priv = (struct priv *)ctx->priv;
        if (nlm->nlmsg_pid == priv->route_pid)
                return 0;
@@ -688,6 +693,11 @@ link_addr(struct dhcpcd_ctx *ctx, struct interface *ifp, struct nlmsghdr *nlm)
        }
 
        /* Ignore messages we sent. */
+#ifdef PRIVSEP
+       if (ctx->ps_root_pid != 0 &&
+           nlm->nlmsg_pid == (uint32_t)ctx->ps_root_pid)
+               return 0;
+#endif
        priv = (struct priv*)ctx->priv;
        if (nlm->nlmsg_pid == priv->route_pid)
                return 0;
@@ -978,6 +988,9 @@ if_sendnetlink(struct dhcpcd_ctx *ctx, int protocol, struct nlmsghdr *hdr,
            .msg_name = &snl, .msg_namelen = sizeof(snl),
            .msg_iov = &iov, .msg_iovlen = 1
        };
+       bool use_rfd;
+
+       use_rfd = (protocol == NETLINK_ROUTE && hdr->nlmsg_type != RTM_GETADDR);
 
        /* Request a reply */
        hdr->nlmsg_flags |= NLM_F_ACK;
@@ -988,18 +1001,23 @@ if_sendnetlink(struct dhcpcd_ctx *ctx, int protocol, struct nlmsghdr *hdr,
                return (int)ps_root_sendnetlink(ctx, protocol, &msg);
 #endif
 
-       if ((s = if_linksocket(&snl, protocol)) == -1)
-               return -1;
+       if (use_rfd) {
+               struct priv *priv = (struct priv *)ctx->priv;
 
+               s = priv->route_fd;
+       } else {
+               if ((s = if_linksocket(&snl, protocol)) == -1)
+                       return -1;
 #ifdef NETLINK_GET_STRICT_CHK
-       if (hdr->nlmsg_type == RTM_GETADDR) {
-               int on = 1;
+               if (hdr->nlmsg_type == RTM_GETADDR) {
+                       int on = 1;
 
-               if (setsockopt(s, SOL_NETLINK, NETLINK_GET_STRICT_CHK,
-                   &on, sizeof(on)) == -1)
-                       logerr("%s: NETLINK_GET_STRICT_CHK", __func__);
-       }
+                       if (setsockopt(s, SOL_NETLINK, NETLINK_GET_STRICT_CHK,
+                           &on, sizeof(on)) == -1)
+                               logerr("%s: NETLINK_GET_STRICT_CHK", __func__);
+               }
 #endif
+       }
 
        if (sendmsg(s, &msg, 0) != -1) {
                unsigned char buf[16 * 1024];
@@ -1011,7 +1029,9 @@ if_sendnetlink(struct dhcpcd_ctx *ctx, int protocol, struct nlmsghdr *hdr,
                r = if_getnetlink(ctx, &riov, s, 0, cb, cbarg);
        } else
                r = -1;
-       close(s);
+
+       if (!use_rfd)
+               close(s);
        return r;
 }
 
@@ -1386,7 +1406,6 @@ if_addressexists(struct interface *ifp, struct in_addr *addr)
 }
 #endif
 
-
 struct nlmr
 {
        struct nlmsghdr hdr;