2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2021 Ng Peng Nam Sean
5 * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
36 #include <sys/rmlock.h>
38 #include <sys/syslog.h>
40 #include <netlink/netlink.h>
41 #include <netlink/netlink_ctl.h>
42 #include <netlink/netlink_var.h>
44 #include <machine/atomic.h>
46 MALLOC_DEFINE(M_NETLINK, "netlink", "Memory used for netlink packets");
47 FEATURE(netlink, "Netlink support");
49 #define DEBUG_MOD_NAME nl_mod
50 #define DEBUG_MAX_LEVEL LOG_DEBUG3
51 #include <netlink/netlink_debug.h>
52 _DECLARE_DEBUG(LOG_DEBUG);
54 SYSCTL_NODE(_net, OID_AUTO, netlink, CTLFLAG_RD, 0, "");
55 SYSCTL_NODE(_net_netlink, OID_AUTO, debug, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
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;
61 CK_LIST_HEAD(nl_control_head, nl_control);
62 static struct nl_control_head vnets_head = CK_LIST_HEAD_INITIALIZER();
64 VNET_DEFINE(struct nl_control *, nl_ctl) = NULL;
66 struct mtx nl_global_mtx;
67 MTX_SYSINIT(nl_global_mtx, &nl_global_mtx, "global netlink lock", MTX_DEF);
69 #define NL_GLOBAL_LOCK() mtx_lock(&nl_global_mtx)
70 #define NL_GLOBAL_UNLOCK() mtx_unlock(&nl_global_mtx)
72 int netlink_unloading = 0;
75 free_nl_ctl(struct nl_control *ctl)
77 rm_destroy(&ctl->ctl_lock);
82 vnet_nl_ctl_init(void)
84 struct nl_control *ctl;
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);
93 struct nl_control *tmp = atomic_load_ptr(&V_nl_ctl);
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",
101 NL_LOG(LOG_DEBUG, "per-VNET init clash, dropping this instance");
112 vnet_nl_ctl_destroy(const void *unused __unused)
114 struct nl_control *ctl;
116 /* Assume at the time all of the processes / sockets are dead */
119 ctl = atomic_load_ptr(&V_nl_ctl);
120 atomic_store_ptr(&V_nl_ctl, NULL);
122 NL_LOG(LOG_DEBUG2, "Removing %p from global list", ctl);
123 CK_LIST_REMOVE(ctl, ctl_next);
130 VNET_SYSUNINIT(vnet_nl_ctl_destroy, SI_SUB_PROTO_IF, SI_ORDER_ANY,
131 vnet_nl_ctl_destroy, NULL);
134 nl_verify_proto(int proto)
136 if (proto < 0 || proto >= NL_MAX_HANDLERS) {
139 int handler_defined = nl_handlers[proto].cb != NULL;
140 return (handler_defined ? 0 : EPROTONOSUPPORT);
144 nl_get_proto_name(int proto)
146 return (nl_handlers[proto].proto_name);
150 netlink_register_proto(int proto, const char *proto_name, nl_handler_f handler)
152 if ((proto < 0) || (proto >= NL_MAX_HANDLERS))
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;
159 NL_LOG(LOG_DEBUG2, "Registered netlink %s(%d) handler", proto_name, proto);
164 netlink_unregister_proto(int proto)
166 if ((proto < 0) || (proto >= NL_MAX_HANDLERS))
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;
173 NL_LOG(LOG_DEBUG2, "Unregistered netlink proto %d handler", proto);
180 struct nl_control *ctl;
185 CK_LIST_FOREACH(ctl, &vnets_head, ctl_next) {
186 NL_LOG(LOG_DEBUG2, "Iterating VNET head %p", ctl);
187 if (!CK_LIST_EMPTY(&ctl->ctl_pcb_head)) {
188 NL_LOG(LOG_NOTICE, "non-empty socket list in ctl %p", ctl);
200 netlink_modevent(module_t mod __unused, int what, void *priv __unused)
206 NL_LOG(LOG_DEBUG2, "Loading");
210 NL_LOG(LOG_DEBUG2, "Unload called");
212 NL_LOG(LOG_WARNING, "unloading");
213 netlink_unloading = 1;
225 static moduledata_t netlink_mod = { "netlink", netlink_modevent, NULL };
227 DECLARE_MODULE(netlink, netlink_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
228 MODULE_VERSION(netlink, 1);