]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netlink/netlink_module.c
vmware: Fix a typo in a source code comment
[FreeBSD/FreeBSD.git] / sys / netlink / netlink_module.c
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/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/kernel.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 FEATURE(netlink, "Netlink support");
48
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);
53
54 SYSCTL_NODE(_net, OID_AUTO, netlink, CTLFLAG_RD, 0, "");
55 SYSCTL_NODE(_net_netlink, OID_AUTO, debug, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
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 static bool
178 can_unload(void)
179 {
180         struct nl_control *ctl;
181         bool result = true;
182
183         NL_GLOBAL_LOCK();
184
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);
189                         result = false;
190                         break;
191                 }
192         }
193
194         NL_GLOBAL_UNLOCK();
195
196         return (result);
197 }
198
199 static int
200 netlink_modevent(module_t mod __unused, int what, void *priv __unused)
201 {
202         int ret = 0;
203
204         switch (what) {
205         case MOD_LOAD:
206                 NL_LOG(LOG_DEBUG2, "Loading");
207                 break;
208
209         case MOD_UNLOAD:
210                 NL_LOG(LOG_DEBUG2, "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);