xref: /freebsd/sys/netlink/netlink_route.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/types.h>
29 #include <sys/ck.h>
30 #include <sys/epoch.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/socket.h>
34 
35 #include <net/route.h>
36 #include <net/route/route_ctl.h>
37 #include <netlink/netlink.h>
38 #include <netlink/netlink_ctl.h>
39 #include <netlink/netlink_route.h>
40 #include <netlink/route/route_var.h>
41 
42 #define	DEBUG_MOD_NAME	nl_route_core
43 #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
44 #include <netlink/netlink_debug.h>
45 _DECLARE_DEBUG(LOG_INFO);
46 
47 #define	HANDLER_MAX_NUM	(NL_RTM_MAX + 10)
48 static const struct rtnl_cmd_handler *rtnl_handler[HANDLER_MAX_NUM] = {};
49 
50 bool
rtnl_register_messages(const struct rtnl_cmd_handler * handlers,int count)51 rtnl_register_messages(const struct rtnl_cmd_handler *handlers, int count)
52 {
53 	for (int i = 0; i < count; i++) {
54 		if (handlers[i].cmd >= HANDLER_MAX_NUM)
55 			return (false);
56 		MPASS(rtnl_handler[handlers[i].cmd] == NULL);
57 	}
58 	for (int i = 0; i < count; i++)
59 		rtnl_handler[handlers[i].cmd] = &handlers[i];
60 	return (true);
61 }
62 
63 /*
64  * Handler called by netlink subsystem when matching netlink message is received
65  */
66 static int
rtnl_handle_message(struct nlmsghdr * hdr,struct nl_pstate * npt)67 rtnl_handle_message(struct nlmsghdr *hdr, struct nl_pstate *npt)
68 {
69 	const struct rtnl_cmd_handler *cmd;
70 	struct epoch_tracker et;
71 	struct nlpcb *nlp = npt->nlp;
72 	int error = 0;
73 
74 	if (__predict_false(hdr->nlmsg_type >= HANDLER_MAX_NUM)) {
75 		NLMSG_REPORT_ERR_MSG(npt, "unknown message type: %d", hdr->nlmsg_type);
76 		return (ENOTSUP);
77 	}
78 
79 	cmd = rtnl_handler[hdr->nlmsg_type];
80 	if (__predict_false(cmd == NULL)) {
81 		NLMSG_REPORT_ERR_MSG(npt, "unknown message type: %d", hdr->nlmsg_type);
82 		return (ENOTSUP);
83 	}
84 
85 	NLP_LOG(LOG_DEBUG2, nlp, "received msg %s(%d) len %d", cmd->name,
86 	    hdr->nlmsg_type, hdr->nlmsg_len);
87 
88 	if (cmd->priv != 0 && !nlp_has_priv(nlp, cmd->priv)) {
89 		NLP_LOG(LOG_DEBUG2, nlp, "priv %d check failed for msg %s", cmd->priv, cmd->name);
90 		return (EPERM);
91 	} else if (cmd->priv != 0)
92 		NLP_LOG(LOG_DEBUG3, nlp, "priv %d check passed for msg %s", cmd->priv, cmd->name);
93 
94 	if (!nlp_unconstrained_vnet(nlp) && (cmd->flags & RTNL_F_ALLOW_NONVNET_JAIL) == 0) {
95 		NLP_LOG(LOG_DEBUG2, nlp, "jail check failed for msg %s", cmd->name);
96 		return (EPERM);
97 	}
98 
99 	bool need_epoch = !(cmd->flags & RTNL_F_NOEPOCH);
100 
101 	if (need_epoch)
102 		NET_EPOCH_ENTER(et);
103 	error = cmd->cb(hdr, nlp, npt);
104 	if (need_epoch)
105 		NET_EPOCH_EXIT(et);
106 
107 	NLP_LOG(LOG_DEBUG3, nlp, "message %s -> error %d", cmd->name, error);
108 
109 	return (error);
110 }
111 
112 static struct rtbridge nlbridge = {
113 	.route_f = rtnl_handle_route_event,
114 	.ifmsg_f = rtnl_handle_ifnet_event,
115 };
116 static struct rtbridge *nlbridge_orig_p;
117 
118 static void
rtnl_load(void * u __unused)119 rtnl_load(void *u __unused)
120 {
121 	NL_LOG(LOG_DEBUG2, "rtnl loading");
122 	nlbridge_orig_p = netlink_callback_p;
123 	netlink_callback_p = &nlbridge;
124 	rtnl_neighs_init();
125 	rtnl_ifaces_init();
126 	rtnl_nexthops_init();
127 	rtnl_routes_init();
128 	netlink_register_proto(NETLINK_ROUTE, "NETLINK_ROUTE", rtnl_handle_message);
129 }
130 SYSINIT(rtnl_load, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rtnl_load, NULL);
131 
132 static void
rtnl_unload(void * u __unused)133 rtnl_unload(void *u __unused)
134 {
135 	netlink_callback_p = nlbridge_orig_p;
136 	netlink_unregister_proto(NETLINK_ROUTE);
137 	rtnl_ifaces_destroy();
138 	rtnl_neighs_destroy();
139 
140 	/* Wait till all consumers read nlbridge data */
141 	NET_EPOCH_WAIT();
142 }
143 SYSUNINIT(rtnl_unload, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rtnl_unload, NULL);
144