From: Roy Marples Date: Thu, 16 Jan 2020 17:33:24 +0000 (+0000) Subject: Linux: Use the persistent route socket for writing netlink msgs X-Git-Tag: v9.0.0~109 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1bf3fbb15c4fe8cbf87c6a346e7108d6fbb170e3;p=thirdparty%2Fdhcpcd.git Linux: Use the persistent route socket for writing netlink msgs This reverts 2c8dc9ee7. This *should* be fine because we can detect ourself and privsep sending messages so we can ignore them. --- diff --git a/src/if-linux.c b/src/if-linux.c index d42383b5..4765a7f7 100644 --- a/src/if-linux.c +++ b/src/if-linux.c @@ -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;