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