xref: /freebsd/sys/netlink/netlink_module.c (revision 0aa2700123e22c2b0a977375e087dc2759b8e980)
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/malloc.h>
33 #include <sys/module.h>
34 
35 #include <sys/lock.h>
36 #include <sys/rmlock.h>
37 #include <sys/ck.h>
38 #include <sys/syslog.h>
39 
40 #include <netlink/netlink.h>
41 #include <netlink/netlink_ctl.h>
42 #include <netlink/netlink_var.h>
43 
44 #include <machine/atomic.h>
45 
46 MALLOC_DEFINE(M_NETLINK, "netlink", "Memory used for netlink packets");
47 
48 #define	DEBUG_MOD_NAME	nl_mod
49 #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
50 #include <netlink/netlink_debug.h>
51 _DECLARE_DEBUG(LOG_DEBUG);
52 
53 SYSCTL_NODE(_net, OID_AUTO, netlink, CTLFLAG_RD, 0, "");
54 SYSCTL_NODE(_net_netlink, OID_AUTO, debug, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
55 
56 #define NL_MAX_HANDLERS	20
57 struct nl_proto_handler _nl_handlers[NL_MAX_HANDLERS];
58 struct nl_proto_handler *nl_handlers = _nl_handlers;
59 
60 CK_LIST_HEAD(nl_control_head, nl_control);
61 static struct nl_control_head vnets_head = CK_LIST_HEAD_INITIALIZER();
62 
63 VNET_DEFINE(struct nl_control *, nl_ctl) = NULL;
64 
65 struct mtx nl_global_mtx;
66 MTX_SYSINIT(nl_global_mtx, &nl_global_mtx, "global netlink lock", MTX_DEF);
67 
68 #define NL_GLOBAL_LOCK()	mtx_lock(&nl_global_mtx)
69 #define NL_GLOBAL_UNLOCK()	mtx_unlock(&nl_global_mtx)
70 
71 int netlink_unloading = 0;
72 
73 static void
74 free_nl_ctl(struct nl_control *ctl)
75 {
76 	rm_destroy(&ctl->ctl_lock);
77 	free(ctl, M_NETLINK);
78 }
79 
80 struct nl_control *
81 vnet_nl_ctl_init(void)
82 {
83 	struct nl_control *ctl;
84 
85 	ctl = malloc(sizeof(struct nl_control), M_NETLINK, M_WAITOK | M_ZERO);
86 	rm_init(&ctl->ctl_lock, "netlink lock");
87 	CK_LIST_INIT(&ctl->ctl_port_head);
88 	CK_LIST_INIT(&ctl->ctl_pcb_head);
89 
90 	NL_GLOBAL_LOCK();
91 
92 	struct nl_control *tmp = atomic_load_ptr(&V_nl_ctl);
93 
94 	if (tmp == NULL) {
95 		atomic_store_ptr(&V_nl_ctl, ctl);
96 		CK_LIST_INSERT_HEAD(&vnets_head, ctl, ctl_next);
97 		NL_LOG(LOG_DEBUG2, "VNET %p init done, inserted %p into global list",
98 		    curvnet, ctl);
99 	} else {
100 		NL_LOG(LOG_DEBUG, "per-VNET init clash, dropping this instance");
101 		free_nl_ctl(ctl);
102 		ctl = tmp;
103 	}
104 
105 	NL_GLOBAL_UNLOCK();
106 
107 	return (ctl);
108 }
109 
110 static void
111 vnet_nl_ctl_destroy(const void *unused __unused)
112 {
113 	struct nl_control *ctl;
114 
115 	/* Assume at the time all of the processes / sockets are dead */
116 
117 	NL_GLOBAL_LOCK();
118 	ctl = atomic_load_ptr(&V_nl_ctl);
119 	atomic_store_ptr(&V_nl_ctl, NULL);
120 	if (ctl != NULL) {
121 		NL_LOG(LOG_DEBUG2, "Removing %p from global list", ctl);
122 		CK_LIST_REMOVE(ctl, ctl_next);
123 	}
124 	NL_GLOBAL_UNLOCK();
125 
126 	if (ctl != NULL)
127 		free_nl_ctl(ctl);
128 }
129 VNET_SYSUNINIT(vnet_nl_ctl_destroy, SI_SUB_PROTO_IF, SI_ORDER_ANY,
130     vnet_nl_ctl_destroy, NULL);
131 
132 int
133 nl_verify_proto(int proto)
134 {
135 	if (proto < 0 || proto >= NL_MAX_HANDLERS) {
136 		return (EINVAL);
137 	}
138 	int handler_defined = nl_handlers[proto].cb != NULL;
139 	return (handler_defined ? 0 : EPROTONOSUPPORT);
140 }
141 
142 const char *
143 nl_get_proto_name(int proto)
144 {
145 	return (nl_handlers[proto].proto_name);
146 }
147 
148 bool
149 netlink_register_proto(int proto, const char *proto_name, nl_handler_f handler)
150 {
151 	if ((proto < 0) || (proto >= NL_MAX_HANDLERS))
152 		return (false);
153 	NL_GLOBAL_LOCK();
154 	KASSERT((nl_handlers[proto].cb == NULL), ("netlink handler %d is already set", proto));
155 	nl_handlers[proto].cb = handler;
156 	nl_handlers[proto].proto_name = proto_name;
157 	NL_GLOBAL_UNLOCK();
158 	NL_LOG(LOG_DEBUG, "Registered netlink %s(%d) handler", proto_name, proto);
159 	return (true);
160 }
161 
162 bool
163 netlink_unregister_proto(int proto)
164 {
165 	if ((proto < 0) || (proto >= NL_MAX_HANDLERS))
166 		return (false);
167 	NL_GLOBAL_LOCK();
168 	KASSERT((nl_handlers[proto].cb != NULL), ("netlink handler %d is not set", proto));
169 	nl_handlers[proto].cb = NULL;
170 	nl_handlers[proto].proto_name = NULL;
171 	NL_GLOBAL_UNLOCK();
172 	NL_LOG(LOG_DEBUG, "Unregistered netlink proto %d handler", proto);
173 	return (true);
174 }
175 
176 static bool
177 can_unload(void)
178 {
179 	struct nl_control *ctl;
180 	bool result = true;
181 
182 	NL_GLOBAL_LOCK();
183 
184 	CK_LIST_FOREACH(ctl, &vnets_head, ctl_next) {
185 		NL_LOG(LOG_DEBUG2, "Iterating VNET head %p", ctl);
186 		if (!CK_LIST_EMPTY(&ctl->ctl_pcb_head)) {
187 			NL_LOG(LOG_NOTICE, "non-empty socket list in ctl %p", ctl);
188 			result = false;
189 			break;
190 		}
191 	}
192 
193 	NL_GLOBAL_UNLOCK();
194 
195 	return (result);
196 }
197 
198 static int
199 netlink_modevent(module_t mod __unused, int what, void *priv __unused)
200 {
201 	int ret = 0;
202 
203 	switch (what) {
204 	case MOD_LOAD:
205 		NL_LOG(LOG_DEBUG, "Loading");
206 		NL_LOG(LOG_NOTICE, "netlink support is in BETA stage");
207 		break;
208 
209 	case MOD_UNLOAD:
210 		NL_LOG(LOG_DEBUG, "Unload called");
211 		if (can_unload()) {
212 			NL_LOG(LOG_WARNING, "unloading");
213 			netlink_unloading = 1;
214 		} else
215 			ret = EBUSY;
216 		break;
217 
218 	default:
219 		ret = EOPNOTSUPP;
220 		break;
221 	}
222 
223 	return (ret);
224 }
225 static moduledata_t netlink_mod = { "netlink", netlink_modevent, NULL };
226 
227 DECLARE_MODULE(netlink, netlink_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
228 MODULE_VERSION(netlink, 1);
229