xref: /freebsd/sys/netlink/netlink_module.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
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 <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
35 
36 #include <sys/lock.h>
37 #include <sys/rmlock.h>
38 #include <sys/ck.h>
39 #include <sys/syslog.h>
40 
41 #include <netlink/netlink.h>
42 #include <netlink/netlink_ctl.h>
43 #include <netlink/netlink_var.h>
44 
45 #include <machine/atomic.h>
46 
47 MALLOC_DEFINE(M_NETLINK, "netlink", "Memory used for netlink packets");
48 FEATURE(netlink, "Netlink support");
49 
50 #define	DEBUG_MOD_NAME	nl_mod
51 #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
52 #include <netlink/netlink_debug.h>
53 _DECLARE_DEBUG(LOG_DEBUG);
54 
55 SYSCTL_NODE(_net, OID_AUTO, netlink, CTLFLAG_RD, 0, "");
56 SYSCTL_NODE(_net_netlink, OID_AUTO, debug, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
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 static bool
179 can_unload(void)
180 {
181 	struct nl_control *ctl;
182 	bool result = true;
183 
184 	NL_GLOBAL_LOCK();
185 
186 	CK_LIST_FOREACH(ctl, &vnets_head, ctl_next) {
187 		NL_LOG(LOG_DEBUG2, "Iterating VNET head %p", ctl);
188 		if (!CK_LIST_EMPTY(&ctl->ctl_pcb_head)) {
189 			NL_LOG(LOG_NOTICE, "non-empty socket list in ctl %p", ctl);
190 			result = false;
191 			break;
192 		}
193 	}
194 
195 	NL_GLOBAL_UNLOCK();
196 
197 	return (result);
198 }
199 
200 static int
201 netlink_modevent(module_t mod __unused, int what, void *priv __unused)
202 {
203 	int ret = 0;
204 
205 	switch (what) {
206 	case MOD_LOAD:
207 		NL_LOG(LOG_DEBUG2, "Loading");
208 		break;
209 
210 	case MOD_UNLOAD:
211 		NL_LOG(LOG_DEBUG2, "Unload called");
212 		if (can_unload()) {
213 			NL_LOG(LOG_WARNING, "unloading");
214 			netlink_unloading = 1;
215 		} else
216 			ret = EBUSY;
217 		break;
218 
219 	default:
220 		ret = EOPNOTSUPP;
221 		break;
222 	}
223 
224 	return (ret);
225 }
226 static moduledata_t netlink_mod = { "netlink", netlink_modevent, NULL };
227 
228 DECLARE_MODULE(netlink, netlink_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
229 MODULE_VERSION(netlink, 1);
230