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 <sys/param.h>
30#include <sys/kernel.h>
31#include <sys/malloc.h>
32#include <sys/module.h>
33
34#include <sys/lock.h>
35#include <sys/rmlock.h>
36#include <sys/ck.h>
37#include <sys/syslog.h>
38
39#include <netlink/netlink.h>
40#include <netlink/netlink_ctl.h>
41#include <netlink/netlink_var.h>
42#include <netlink/route/route_var.h>
43
44#include <machine/atomic.h>
45
46FEATURE(netlink, "Netlink support");
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_INFO);
52
53
54#define NL_MAX_HANDLERS	20
55struct nl_proto_handler _nl_handlers[NL_MAX_HANDLERS];
56struct nl_proto_handler *nl_handlers = _nl_handlers;
57
58CK_LIST_HEAD(nl_control_head, nl_control);
59static struct nl_control_head vnets_head = CK_LIST_HEAD_INITIALIZER();
60
61VNET_DEFINE(struct nl_control *, nl_ctl) = NULL;
62
63struct mtx nl_global_mtx;
64MTX_SYSINIT(nl_global_mtx, &nl_global_mtx, "global netlink lock", MTX_DEF);
65
66#define NL_GLOBAL_LOCK()	mtx_lock(&nl_global_mtx)
67#define NL_GLOBAL_UNLOCK()	mtx_unlock(&nl_global_mtx)
68
69int netlink_unloading = 0;
70
71static void
72free_nl_ctl(struct nl_control *ctl)
73{
74	rm_destroy(&ctl->ctl_lock);
75	free(ctl, M_NETLINK);
76}
77
78struct nl_control *
79vnet_nl_ctl_init(void)
80{
81	struct nl_control *ctl;
82
83	ctl = malloc(sizeof(struct nl_control), M_NETLINK, M_WAITOK | M_ZERO);
84	rm_init(&ctl->ctl_lock, "netlink lock");
85	CK_LIST_INIT(&ctl->ctl_port_head);
86	CK_LIST_INIT(&ctl->ctl_pcb_head);
87
88	NL_GLOBAL_LOCK();
89
90	struct nl_control *tmp = atomic_load_ptr(&V_nl_ctl);
91
92	if (tmp == NULL) {
93		atomic_store_ptr(&V_nl_ctl, ctl);
94		CK_LIST_INSERT_HEAD(&vnets_head, ctl, ctl_next);
95		NL_LOG(LOG_DEBUG2, "VNET %p init done, inserted %p into global list",
96		    curvnet, ctl);
97	} else {
98		NL_LOG(LOG_DEBUG, "per-VNET init clash, dropping this instance");
99		free_nl_ctl(ctl);
100		ctl = tmp;
101	}
102
103	NL_GLOBAL_UNLOCK();
104
105	return (ctl);
106}
107
108static void
109vnet_nl_ctl_destroy(const void *unused __unused)
110{
111	struct nl_control *ctl;
112
113	/* Assume at the time all of the processes / sockets are dead */
114
115	NL_GLOBAL_LOCK();
116	ctl = atomic_load_ptr(&V_nl_ctl);
117	atomic_store_ptr(&V_nl_ctl, NULL);
118	if (ctl != NULL) {
119		NL_LOG(LOG_DEBUG2, "Removing %p from global list", ctl);
120		CK_LIST_REMOVE(ctl, ctl_next);
121	}
122	NL_GLOBAL_UNLOCK();
123
124	if (ctl != NULL)
125		free_nl_ctl(ctl);
126}
127VNET_SYSUNINIT(vnet_nl_ctl_destroy, SI_SUB_PROTO_IF, SI_ORDER_ANY,
128    vnet_nl_ctl_destroy, NULL);
129
130int
131nl_verify_proto(int proto)
132{
133	if (proto < 0 || proto >= NL_MAX_HANDLERS) {
134		return (EINVAL);
135	}
136	int handler_defined = nl_handlers[proto].cb != NULL;
137	return (handler_defined ? 0 : EPROTONOSUPPORT);
138}
139
140const char *
141nl_get_proto_name(int proto)
142{
143	return (nl_handlers[proto].proto_name);
144}
145
146bool
147netlink_register_proto(int proto, const char *proto_name, nl_handler_f handler)
148{
149	if ((proto < 0) || (proto >= NL_MAX_HANDLERS))
150		return (false);
151	NL_GLOBAL_LOCK();
152	KASSERT((nl_handlers[proto].cb == NULL), ("netlink handler %d is already set", proto));
153	nl_handlers[proto].cb = handler;
154	nl_handlers[proto].proto_name = proto_name;
155	NL_GLOBAL_UNLOCK();
156	NL_LOG(LOG_DEBUG2, "Registered netlink %s(%d) handler", proto_name, proto);
157	return (true);
158}
159
160bool
161netlink_unregister_proto(int proto)
162{
163	if ((proto < 0) || (proto >= NL_MAX_HANDLERS))
164		return (false);
165	NL_GLOBAL_LOCK();
166	KASSERT((nl_handlers[proto].cb != NULL), ("netlink handler %d is not set", proto));
167	nl_handlers[proto].cb = NULL;
168	nl_handlers[proto].proto_name = NULL;
169	NL_GLOBAL_UNLOCK();
170	NL_LOG(LOG_DEBUG2, "Unregistered netlink proto %d handler", proto);
171	return (true);
172}
173
174#if !defined(NETLINK) && defined(NETLINK_MODULE)
175/* Non-stub function provider */
176const static struct nl_function_wrapper nl_module = {
177	.nlmsg_add = _nlmsg_add,
178	.nlmsg_refill_buffer = _nlmsg_refill_buffer,
179	.nlmsg_flush = _nlmsg_flush,
180	.nlmsg_end = _nlmsg_end,
181	.nlmsg_abort = _nlmsg_abort,
182	.nlmsg_get_unicast_writer = _nlmsg_get_unicast_writer,
183	.nlmsg_get_group_writer = _nlmsg_get_group_writer,
184	.nlmsg_end_dump = _nlmsg_end_dump,
185	.nl_modify_ifp_generic = _nl_modify_ifp_generic,
186	.nl_store_ifp_cookie = _nl_store_ifp_cookie,
187	.nl_get_thread_nlp = _nl_get_thread_nlp,
188};
189#endif
190
191static bool
192can_unload(void)
193{
194	struct nl_control *ctl;
195	bool result = true;
196
197	NL_GLOBAL_LOCK();
198
199	CK_LIST_FOREACH(ctl, &vnets_head, ctl_next) {
200		NL_LOG(LOG_DEBUG2, "Iterating VNET head %p", ctl);
201		if (!CK_LIST_EMPTY(&ctl->ctl_pcb_head)) {
202			NL_LOG(LOG_NOTICE, "non-empty socket list in ctl %p", ctl);
203			result = false;
204			break;
205		}
206	}
207
208	NL_GLOBAL_UNLOCK();
209
210	return (result);
211}
212
213static int
214netlink_modevent(module_t mod __unused, int what, void *priv __unused)
215{
216	int ret = 0;
217
218	switch (what) {
219	case MOD_LOAD:
220		NL_LOG(LOG_DEBUG2, "Loading");
221		nl_osd_register();
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			nl_osd_unregister();
236		} else
237			ret = EBUSY;
238		break;
239
240	default:
241		ret = EOPNOTSUPP;
242		break;
243	}
244
245	return (ret);
246}
247static moduledata_t netlink_mod = { "netlink", netlink_modevent, NULL };
248
249DECLARE_MODULE(netlink, netlink_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
250MODULE_VERSION(netlink, 1);
251