xref: /freebsd/sys/netlink/netlink_module.c (revision 9e79038c502433f077b4d3b5bb1c0838329f1ebc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2021 Ng Peng Nam Sean
5  * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include "opt_netlink.h"
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 
38 #include <sys/lock.h>
39 #include <sys/rmlock.h>
40 #include <sys/ck.h>
41 #include <sys/syslog.h>
42 
43 #include <netlink/netlink.h>
44 #include <netlink/netlink_ctl.h>
45 #include <netlink/netlink_var.h>
46 #include <netlink/route/route_var.h>
47 
48 #include <machine/atomic.h>
49 
50 FEATURE(netlink, "Netlink support");
51 
52 #define	DEBUG_MOD_NAME	nl_mod
53 #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
54 #include <netlink/netlink_debug.h>
55 _DECLARE_DEBUG(LOG_DEBUG);
56 
57 
58 #define NL_MAX_HANDLERS	20
59 struct nl_proto_handler _nl_handlers[NL_MAX_HANDLERS];
60 struct nl_proto_handler *nl_handlers = _nl_handlers;
61 
62 CK_LIST_HEAD(nl_control_head, nl_control);
63 static struct nl_control_head vnets_head = CK_LIST_HEAD_INITIALIZER();
64 
65 VNET_DEFINE(struct nl_control *, nl_ctl) = NULL;
66 
67 struct mtx nl_global_mtx;
68 MTX_SYSINIT(nl_global_mtx, &nl_global_mtx, "global netlink lock", MTX_DEF);
69 
70 #define NL_GLOBAL_LOCK()	mtx_lock(&nl_global_mtx)
71 #define NL_GLOBAL_UNLOCK()	mtx_unlock(&nl_global_mtx)
72 
73 int netlink_unloading = 0;
74 
75 static void
76 free_nl_ctl(struct nl_control *ctl)
77 {
78 	rm_destroy(&ctl->ctl_lock);
79 	free(ctl, M_NETLINK);
80 }
81 
82 struct nl_control *
83 vnet_nl_ctl_init(void)
84 {
85 	struct nl_control *ctl;
86 
87 	ctl = malloc(sizeof(struct nl_control), M_NETLINK, M_WAITOK | M_ZERO);
88 	rm_init(&ctl->ctl_lock, "netlink lock");
89 	CK_LIST_INIT(&ctl->ctl_port_head);
90 	CK_LIST_INIT(&ctl->ctl_pcb_head);
91 
92 	NL_GLOBAL_LOCK();
93 
94 	struct nl_control *tmp = atomic_load_ptr(&V_nl_ctl);
95 
96 	if (tmp == NULL) {
97 		atomic_store_ptr(&V_nl_ctl, ctl);
98 		CK_LIST_INSERT_HEAD(&vnets_head, ctl, ctl_next);
99 		NL_LOG(LOG_DEBUG2, "VNET %p init done, inserted %p into global list",
100 		    curvnet, ctl);
101 	} else {
102 		NL_LOG(LOG_DEBUG, "per-VNET init clash, dropping this instance");
103 		free_nl_ctl(ctl);
104 		ctl = tmp;
105 	}
106 
107 	NL_GLOBAL_UNLOCK();
108 
109 	return (ctl);
110 }
111 
112 static void
113 vnet_nl_ctl_destroy(const void *unused __unused)
114 {
115 	struct nl_control *ctl;
116 
117 	/* Assume at the time all of the processes / sockets are dead */
118 
119 	NL_GLOBAL_LOCK();
120 	ctl = atomic_load_ptr(&V_nl_ctl);
121 	atomic_store_ptr(&V_nl_ctl, NULL);
122 	if (ctl != NULL) {
123 		NL_LOG(LOG_DEBUG2, "Removing %p from global list", ctl);
124 		CK_LIST_REMOVE(ctl, ctl_next);
125 	}
126 	NL_GLOBAL_UNLOCK();
127 
128 	if (ctl != NULL)
129 		free_nl_ctl(ctl);
130 }
131 VNET_SYSUNINIT(vnet_nl_ctl_destroy, SI_SUB_PROTO_IF, SI_ORDER_ANY,
132     vnet_nl_ctl_destroy, NULL);
133 
134 int
135 nl_verify_proto(int proto)
136 {
137 	if (proto < 0 || proto >= NL_MAX_HANDLERS) {
138 		return (EINVAL);
139 	}
140 	int handler_defined = nl_handlers[proto].cb != NULL;
141 	return (handler_defined ? 0 : EPROTONOSUPPORT);
142 }
143 
144 const char *
145 nl_get_proto_name(int proto)
146 {
147 	return (nl_handlers[proto].proto_name);
148 }
149 
150 bool
151 netlink_register_proto(int proto, const char *proto_name, nl_handler_f handler)
152 {
153 	if ((proto < 0) || (proto >= NL_MAX_HANDLERS))
154 		return (false);
155 	NL_GLOBAL_LOCK();
156 	KASSERT((nl_handlers[proto].cb == NULL), ("netlink handler %d is already set", proto));
157 	nl_handlers[proto].cb = handler;
158 	nl_handlers[proto].proto_name = proto_name;
159 	NL_GLOBAL_UNLOCK();
160 	NL_LOG(LOG_DEBUG2, "Registered netlink %s(%d) handler", proto_name, proto);
161 	return (true);
162 }
163 
164 bool
165 netlink_unregister_proto(int proto)
166 {
167 	if ((proto < 0) || (proto >= NL_MAX_HANDLERS))
168 		return (false);
169 	NL_GLOBAL_LOCK();
170 	KASSERT((nl_handlers[proto].cb != NULL), ("netlink handler %d is not set", proto));
171 	nl_handlers[proto].cb = NULL;
172 	nl_handlers[proto].proto_name = NULL;
173 	NL_GLOBAL_UNLOCK();
174 	NL_LOG(LOG_DEBUG2, "Unregistered netlink proto %d handler", proto);
175 	return (true);
176 }
177 
178 #if !defined(NETLINK) && defined(NETLINK_MODULE)
179 /* Non-stub function provider */
180 const static struct nl_function_wrapper nl_module = {
181 	.nlmsg_add = _nlmsg_add,
182 	.nlmsg_refill_buffer = _nlmsg_refill_buffer,
183 	.nlmsg_flush = _nlmsg_flush,
184 	.nlmsg_end = _nlmsg_end,
185 	.nlmsg_abort = _nlmsg_abort,
186 	.nlmsg_get_unicast_writer = _nlmsg_get_unicast_writer,
187 	.nlmsg_get_group_writer = _nlmsg_get_group_writer,
188 	.nlmsg_get_chain_writer = _nlmsg_get_chain_writer,
189 	.nlmsg_end_dump = _nlmsg_end_dump,
190 	.nl_modify_ifp_generic = _nl_modify_ifp_generic,
191 	.nl_store_ifp_cookie = _nl_store_ifp_cookie,
192 };
193 #endif
194 
195 static bool
196 can_unload(void)
197 {
198 	struct nl_control *ctl;
199 	bool result = true;
200 
201 	NL_GLOBAL_LOCK();
202 
203 	CK_LIST_FOREACH(ctl, &vnets_head, ctl_next) {
204 		NL_LOG(LOG_DEBUG2, "Iterating VNET head %p", ctl);
205 		if (!CK_LIST_EMPTY(&ctl->ctl_pcb_head)) {
206 			NL_LOG(LOG_NOTICE, "non-empty socket list in ctl %p", ctl);
207 			result = false;
208 			break;
209 		}
210 	}
211 
212 	NL_GLOBAL_UNLOCK();
213 
214 	return (result);
215 }
216 
217 static int
218 netlink_modevent(module_t mod __unused, int what, void *priv __unused)
219 {
220 	int ret = 0;
221 
222 	switch (what) {
223 	case MOD_LOAD:
224 		NL_LOG(LOG_DEBUG2, "Loading");
225 #if !defined(NETLINK) && defined(NETLINK_MODULE)
226 		nl_set_functions(&nl_module);
227 #endif
228 		break;
229 
230 	case MOD_UNLOAD:
231 		NL_LOG(LOG_DEBUG2, "Unload called");
232 		if (can_unload()) {
233 			NL_LOG(LOG_WARNING, "unloading");
234 			netlink_unloading = 1;
235 #if !defined(NETLINK) && defined(NETLINK_MODULE)
236 			nl_set_functions(NULL);
237 #endif
238 		} else
239 			ret = EBUSY;
240 		break;
241 
242 	default:
243 		ret = EOPNOTSUPP;
244 		break;
245 	}
246 
247 	return (ret);
248 }
249 static moduledata_t netlink_mod = { "netlink", netlink_modevent, NULL };
250 
251 DECLARE_MODULE(netlink, netlink_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
252 MODULE_VERSION(netlink, 1);
253