xref: /freebsd/sys/netlink/netlink_module.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 
37 #include <sys/lock.h>
38 #include <sys/rmlock.h>
39 #include <sys/ck.h>
40 #include <sys/syslog.h>
41 
42 #include <netlink/netlink.h>
43 #include <netlink/netlink_ctl.h>
44 #include <netlink/netlink_var.h>
45 #include <netlink/route/route_var.h>
46 
47 #include <machine/atomic.h>
48 
49 FEATURE(netlink, "Netlink support");
50 
51 #define	DEBUG_MOD_NAME	nl_mod
52 #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
53 #include <netlink/netlink_debug.h>
54 _DECLARE_DEBUG(LOG_INFO);
55 
56 
57 #define NL_MAX_HANDLERS	20
58 struct nl_proto_handler _nl_handlers[NL_MAX_HANDLERS];
59 struct nl_proto_handler *nl_handlers = _nl_handlers;
60 
61 CK_LIST_HEAD(nl_control_head, nl_control);
62 static struct nl_control_head vnets_head = CK_LIST_HEAD_INITIALIZER();
63 
64 VNET_DEFINE(struct nl_control *, nl_ctl) = NULL;
65 
66 struct mtx nl_global_mtx;
67 MTX_SYSINIT(nl_global_mtx, &nl_global_mtx, "global netlink lock", MTX_DEF);
68 
69 #define NL_GLOBAL_LOCK()	mtx_lock(&nl_global_mtx)
70 #define NL_GLOBAL_UNLOCK()	mtx_unlock(&nl_global_mtx)
71 
72 int netlink_unloading = 0;
73 
74 static void
75 free_nl_ctl(struct nl_control *ctl)
76 {
77 	rm_destroy(&ctl->ctl_lock);
78 	free(ctl, M_NETLINK);
79 }
80 
81 struct nl_control *
82 vnet_nl_ctl_init(void)
83 {
84 	struct nl_control *ctl;
85 
86 	ctl = malloc(sizeof(struct nl_control), M_NETLINK, M_WAITOK | M_ZERO);
87 	rm_init(&ctl->ctl_lock, "netlink lock");
88 	CK_LIST_INIT(&ctl->ctl_port_head);
89 	CK_LIST_INIT(&ctl->ctl_pcb_head);
90 
91 	NL_GLOBAL_LOCK();
92 
93 	struct nl_control *tmp = atomic_load_ptr(&V_nl_ctl);
94 
95 	if (tmp == NULL) {
96 		atomic_store_ptr(&V_nl_ctl, ctl);
97 		CK_LIST_INSERT_HEAD(&vnets_head, ctl, ctl_next);
98 		NL_LOG(LOG_DEBUG2, "VNET %p init done, inserted %p into global list",
99 		    curvnet, ctl);
100 	} else {
101 		NL_LOG(LOG_DEBUG, "per-VNET init clash, dropping this instance");
102 		free_nl_ctl(ctl);
103 		ctl = tmp;
104 	}
105 
106 	NL_GLOBAL_UNLOCK();
107 
108 	return (ctl);
109 }
110 
111 static void
112 vnet_nl_ctl_destroy(const void *unused __unused)
113 {
114 	struct nl_control *ctl;
115 
116 	/* Assume at the time all of the processes / sockets are dead */
117 
118 	NL_GLOBAL_LOCK();
119 	ctl = atomic_load_ptr(&V_nl_ctl);
120 	atomic_store_ptr(&V_nl_ctl, NULL);
121 	if (ctl != NULL) {
122 		NL_LOG(LOG_DEBUG2, "Removing %p from global list", ctl);
123 		CK_LIST_REMOVE(ctl, ctl_next);
124 	}
125 	NL_GLOBAL_UNLOCK();
126 
127 	if (ctl != NULL)
128 		free_nl_ctl(ctl);
129 }
130 VNET_SYSUNINIT(vnet_nl_ctl_destroy, SI_SUB_PROTO_IF, SI_ORDER_ANY,
131     vnet_nl_ctl_destroy, NULL);
132 
133 int
134 nl_verify_proto(int proto)
135 {
136 	if (proto < 0 || proto >= NL_MAX_HANDLERS) {
137 		return (EINVAL);
138 	}
139 	int handler_defined = nl_handlers[proto].cb != NULL;
140 	return (handler_defined ? 0 : EPROTONOSUPPORT);
141 }
142 
143 const char *
144 nl_get_proto_name(int proto)
145 {
146 	return (nl_handlers[proto].proto_name);
147 }
148 
149 bool
150 netlink_register_proto(int proto, const char *proto_name, nl_handler_f handler)
151 {
152 	if ((proto < 0) || (proto >= NL_MAX_HANDLERS))
153 		return (false);
154 	NL_GLOBAL_LOCK();
155 	KASSERT((nl_handlers[proto].cb == NULL), ("netlink handler %d is already set", proto));
156 	nl_handlers[proto].cb = handler;
157 	nl_handlers[proto].proto_name = proto_name;
158 	NL_GLOBAL_UNLOCK();
159 	NL_LOG(LOG_DEBUG2, "Registered netlink %s(%d) handler", proto_name, proto);
160 	return (true);
161 }
162 
163 bool
164 netlink_unregister_proto(int proto)
165 {
166 	if ((proto < 0) || (proto >= NL_MAX_HANDLERS))
167 		return (false);
168 	NL_GLOBAL_LOCK();
169 	KASSERT((nl_handlers[proto].cb != NULL), ("netlink handler %d is not set", proto));
170 	nl_handlers[proto].cb = NULL;
171 	nl_handlers[proto].proto_name = NULL;
172 	NL_GLOBAL_UNLOCK();
173 	NL_LOG(LOG_DEBUG2, "Unregistered netlink proto %d handler", proto);
174 	return (true);
175 }
176 
177 #if !defined(NETLINK) && defined(NETLINK_MODULE)
178 /* Non-stub function provider */
179 const static struct nl_function_wrapper nl_module = {
180 	.nlmsg_add = _nlmsg_add,
181 	.nlmsg_refill_buffer = _nlmsg_refill_buffer,
182 	.nlmsg_flush = _nlmsg_flush,
183 	.nlmsg_end = _nlmsg_end,
184 	.nlmsg_abort = _nlmsg_abort,
185 	.nlmsg_get_unicast_writer = _nlmsg_get_unicast_writer,
186 	.nlmsg_get_group_writer = _nlmsg_get_group_writer,
187 	.nlmsg_get_chain_writer = _nlmsg_get_chain_writer,
188 	.nlmsg_end_dump = _nlmsg_end_dump,
189 	.nl_modify_ifp_generic = _nl_modify_ifp_generic,
190 	.nl_store_ifp_cookie = _nl_store_ifp_cookie,
191 	.nl_get_thread_nlp = _nl_get_thread_nlp,
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 		nl_init_msg_zone();
226 		nl_osd_register();
227 #if !defined(NETLINK) && defined(NETLINK_MODULE)
228 		nl_set_functions(&nl_module);
229 #endif
230 		break;
231 
232 	case MOD_UNLOAD:
233 		NL_LOG(LOG_DEBUG2, "Unload called");
234 		if (can_unload()) {
235 			NL_LOG(LOG_WARNING, "unloading");
236 			netlink_unloading = 1;
237 #if !defined(NETLINK) && defined(NETLINK_MODULE)
238 			nl_set_functions(NULL);
239 #endif
240 			nl_osd_unregister();
241 			nl_destroy_msg_zone();
242 		} else
243 			ret = EBUSY;
244 		break;
245 
246 	default:
247 		ret = EOPNOTSUPP;
248 		break;
249 	}
250 
251 	return (ret);
252 }
253 static moduledata_t netlink_mod = { "netlink", netlink_modevent, NULL };
254 
255 DECLARE_MODULE(netlink, netlink_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
256 MODULE_VERSION(netlink, 1);
257