changeset 4970:64b25cbb621f draft

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.
author Roy Marples <roy@marples.name>
date Thu, 16 Jan 2020 17:33:24 +0000
parents 110a9735e7a1
children d050161827d1
files src/if-linux.c
diffstat 1 files changed, 29 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/if-linux.c	Thu Jan 16 14:48:25 2020 +0000
+++ b/src/if-linux.c	Thu Jan 16 17:33:24 2020 +0000
@@ -653,6 +653,11 @@
 	}
 
 	/* 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 @@
 	}
 
 	/* 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 @@
 	    .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 @@
 		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
 	}
-#endif
 
 	if (sendmsg(s, &msg, 0) != -1) {
 		unsigned char buf[16 * 1024];
@@ -1011,7 +1029,9 @@
 		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 @@
 }
 #endif
 
-
 struct nlmr
 {
 	struct nlmsghdr hdr;