git: 140ddeba0834 - main - route: try to autoload netlink(4) module if not present in the kernel.

From: Alexander V. Chernikov <melifaro_at_FreeBSD.org>
Date: Thu, 30 Mar 2023 09:57:22 UTC
The branch main has been updated by melifaro:

URL: https://cgit.FreeBSD.org/src/commit/?id=140ddeba0834509b9b8159c4d0c59cc7962dcc0e

commit 140ddeba0834509b9b8159c4d0c59cc7962dcc0e
Author:     Alexander V. Chernikov <melifaro@FreeBSD.org>
AuthorDate: 2023-03-29 15:31:26 +0000
Commit:     Alexander V. Chernikov <melifaro@FreeBSD.org>
CommitDate: 2023-03-30 09:53:06 +0000

    route: try to autoload netlink(4) module if not present in the kernel.
    
    Differential Revision: https://reviews.freebsd.org/D39324
---
 sbin/route/route_netlink.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)

diff --git a/sbin/route/route_netlink.c b/sbin/route/route_netlink.c
index 0b9bb4ff5750..66b1920a58db 100644
--- a/sbin/route/route_netlink.c
+++ b/sbin/route/route_netlink.c
@@ -6,6 +6,8 @@
 
 #include <sys/bitcount.h>
 #include <sys/param.h>
+#include <sys/linker.h>
+#include <sys/module.h>
 #include <sys/socket.h>
 #include <sys/sysctl.h>
 #include <sys/time.h>
@@ -90,6 +92,23 @@ get_netmask(struct snl_state *ss, int family, int plen)
 	return (NULL);
 }
 
+static void
+nl_init_socket(struct snl_state *ss)
+{
+	if (snl_init(ss, NETLINK_ROUTE))
+		return;
+
+	if (modfind("netlink") == -1 && errno == ENOENT) {
+		/* Try to load */
+		if (kldload("netlink") == -1)
+			err(1, "netlink is not loaded and load attempt failed");
+		if (snl_init(ss, NETLINK_ROUTE))
+			return;
+	}
+
+	err(1, "unable to open netlink socket");
+}
+
 struct nl_helper {
 	struct snl_state ss_cmd;
 };
@@ -97,8 +116,7 @@ struct nl_helper {
 static void
 nl_helper_init(struct nl_helper *h)
 {
-	if (!snl_init(&h->ss_cmd, NETLINK_ROUTE))
-		err(1, "unable to open netlink socket");
+	nl_init_socket(&h->ss_cmd);
 }
 
 static void
@@ -680,8 +698,7 @@ monitor_nl(int fib)
 	struct snl_state ss_event = {};
 	struct nl_helper h;
 
-	if (!snl_init(&ss_event, NETLINK_ROUTE))
-		err(1, "unable to open netlink socket");
+	nl_init_socket(&ss_event);
 	nl_helper_init(&h);
 
 	int groups[] = {
@@ -789,8 +806,7 @@ flushroutes_fib_nl(int fib, int af)
 	struct snl_writer nw;
 	struct nl_helper h = {};
 
-	if (!snl_init(&ss, NETLINK_ROUTE))
-		err(1, "unable to open netlink socket");
+	nl_init_socket(&ss);
 	snl_init_writer(&ss, &nw);
 
 	struct nlmsghdr *hdr = snl_create_msg_request(&nw, NL_RTM_GETROUTE);