xref: /freebsd/sys/netlink/netlink_module.c (revision a50d73d5782a351ad83e8d1f84d11720a12e70d3)
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 
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_DEBUG);
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 };
190 #endif
191 
192 static bool
193 can_unload(void)
194 {
195 	struct nl_control *ctl;
196 	bool result = true;
197 
198 	NL_GLOBAL_LOCK();
199 
200 	CK_LIST_FOREACH(ctl, &vnets_head, ctl_next) {
201 		NL_LOG(LOG_DEBUG2, "Iterating VNET head %p", ctl);
202 		if (!CK_LIST_EMPTY(&ctl->ctl_pcb_head)) {
203 			NL_LOG(LOG_NOTICE, "non-empty socket list in ctl %p", ctl);
204 			result = false;
205 			break;
206 		}
207 	}
208 
209 	NL_GLOBAL_UNLOCK();
210 
211 	return (result);
212 }
213 
214 static int
215 netlink_modevent(module_t mod __unused, int what, void *priv __unused)
216 {
217 	int ret = 0;
218 
219 	switch (what) {
220 	case MOD_LOAD:
221 		NL_LOG(LOG_DEBUG2, "Loading");
222 #if !defined(NETLINK) && defined(NETLINK_MODULE)
223 		nl_set_functions(&nl_module);
224 #endif
225 		break;
226 
227 	case MOD_UNLOAD:
228 		NL_LOG(LOG_DEBUG2, "Unload called");
229 		if (can_unload()) {
230 			NL_LOG(LOG_WARNING, "unloading");
231 			netlink_unloading = 1;
232 #if !defined(NETLINK) && defined(NETLINK_MODULE)
233 			nl_set_functions(NULL);
234 #endif
235 		} else
236 			ret = EBUSY;
237 		break;
238 
239 	default:
240 		ret = EOPNOTSUPP;
241 		break;
242 	}
243 
244 	return (ret);
245 }
246 static moduledata_t netlink_mod = { "netlink", netlink_modevent, NULL };
247 
248 DECLARE_MODULE(netlink, netlink_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
249 MODULE_VERSION(netlink, 1);
250