1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2023 Alexander V. Chernikov <melifaro@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/param.h> 29 #include <sys/kernel.h> 30 #include <sys/malloc.h> 31 #include <sys/lock.h> 32 #include <sys/rmlock.h> 33 #include <sys/domain.h> 34 #include <sys/mbuf.h> 35 #include <sys/protosw.h> 36 #include <sys/proc.h> 37 #include <sys/ck.h> 38 #include <sys/socket.h> 39 #include <sys/socketvar.h> 40 #include <sys/sysent.h> 41 #include <sys/syslog.h> 42 #include <sys/priv.h> /* priv_check */ 43 44 #include <net/route.h> 45 #include <net/route/route_ctl.h> 46 47 #include <netlink/netlink.h> 48 #include <netlink/netlink_ctl.h> 49 #include <netlink/netlink_var.h> 50 #include <netlink/route/route_var.h> 51 52 /* Standard bits: built-in the kernel */ 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 MALLOC_DEFINE(M_NETLINK, "netlink", "Memory used for netlink packets"); 57 58 /* Netlink-related callbacks needed to glue rtsock, netlink and linuxolator */ 59 static void 60 ignore_route_event(uint32_t fibnum, const struct rib_cmd_info *rc) 61 { 62 } 63 64 static void 65 ignore_ifmsg_event(struct ifnet *ifp, int if_flags_mask) 66 { 67 } 68 69 static struct rtbridge ignore_cb = { 70 .route_f = ignore_route_event, 71 .ifmsg_f = ignore_ifmsg_event, 72 }; 73 74 void *linux_netlink_p = NULL; /* Callback pointer for Linux translator functions */ 75 struct rtbridge *rtsock_callback_p = &ignore_cb; 76 struct rtbridge *netlink_callback_p = &ignore_cb; 77 78 79 /* 80 * nlp accessors. 81 * TODO: move to a separate file once the number grows. 82 */ 83 bool 84 nlp_has_priv(struct nlpcb *nlp, int priv) 85 { 86 return (priv_check_cred(nlp->nl_cred, priv) == 0); 87 } 88 89 struct ucred * 90 nlp_get_cred(struct nlpcb *nlp) 91 { 92 return (nlp->nl_cred); 93 } 94 95 uint32_t 96 nlp_get_pid(const struct nlpcb *nlp) 97 { 98 return (nlp->nl_process_id); 99 } 100 101 bool 102 nlp_unconstrained_vnet(const struct nlpcb *nlp) 103 { 104 return (nlp->nl_unconstrained_vnet); 105 } 106 107 #ifndef NETLINK 108 /* Stub implementations for the loadable functions */ 109 110 static bool 111 get_stub_writer(struct nl_writer *nw) 112 { 113 bzero(nw, sizeof(*nw)); 114 nw->enomem = true; 115 116 return (false); 117 } 118 119 static bool 120 nlmsg_get_unicast_writer_stub(struct nl_writer *nw, int size, struct nlpcb *nlp) 121 { 122 return (get_stub_writer(nw)); 123 } 124 125 static bool 126 nlmsg_get_group_writer_stub(struct nl_writer *nw, int size, int protocol, int group_id) 127 { 128 return (get_stub_writer(nw)); 129 } 130 131 static bool 132 nlmsg_get_chain_writer_stub(struct nl_writer *nw, int size, struct mbuf **pm) 133 { 134 return (get_stub_writer(nw)); 135 } 136 137 static bool 138 nlmsg_flush_stub(struct nl_writer *nw __unused) 139 { 140 return (false); 141 } 142 143 static void 144 nlmsg_ignore_limit_stub(struct nl_writer *nw __unused) 145 { 146 } 147 148 static bool 149 nlmsg_refill_buffer_stub(struct nl_writer *nw __unused, int required_len __unused) 150 { 151 return (false); 152 } 153 154 static bool 155 nlmsg_add_stub(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type, 156 uint16_t flags, uint32_t len) 157 { 158 return (false); 159 } 160 161 static bool 162 nlmsg_end_stub(struct nl_writer *nw __unused) 163 { 164 return (false); 165 } 166 167 static void 168 nlmsg_abort_stub(struct nl_writer *nw __unused) 169 { 170 } 171 172 static bool 173 nlmsg_end_dump_stub(struct nl_writer *nw, int error, struct nlmsghdr *hdr) 174 { 175 return (false); 176 } 177 178 static int 179 nl_modify_ifp_generic_stub(struct ifnet *ifp __unused, 180 struct nl_parsed_link *lattrs __unused, const struct nlattr_bmask *bm __unused, 181 struct nl_pstate *npt __unused) 182 { 183 return (ENOTSUP); 184 } 185 186 static void 187 nl_store_ifp_cookie_stub(struct nl_pstate *npt __unused, struct ifnet *ifp __unused) 188 { 189 } 190 191 static struct nlpcb * 192 nl_get_thread_nlp_stub(struct thread *td __unused) 193 { 194 return (NULL); 195 } 196 197 const static struct nl_function_wrapper nl_stub = { 198 .nlmsg_add = nlmsg_add_stub, 199 .nlmsg_refill_buffer = nlmsg_refill_buffer_stub, 200 .nlmsg_flush = nlmsg_flush_stub, 201 .nlmsg_end = nlmsg_end_stub, 202 .nlmsg_abort = nlmsg_abort_stub, 203 .nlmsg_ignore_limit = nlmsg_ignore_limit_stub, 204 .nlmsg_get_unicast_writer = nlmsg_get_unicast_writer_stub, 205 .nlmsg_get_group_writer = nlmsg_get_group_writer_stub, 206 .nlmsg_get_chain_writer = nlmsg_get_chain_writer_stub, 207 .nlmsg_end_dump = nlmsg_end_dump_stub, 208 .nl_modify_ifp_generic = nl_modify_ifp_generic_stub, 209 .nl_store_ifp_cookie = nl_store_ifp_cookie_stub, 210 .nl_get_thread_nlp = nl_get_thread_nlp_stub, 211 }; 212 213 /* 214 * If the kernel is compiled with netlink as a module, 215 * provide a way to introduce non-stub functioms 216 */ 217 static const struct nl_function_wrapper *_nl = &nl_stub; 218 219 void 220 nl_set_functions(const struct nl_function_wrapper *nl) 221 { 222 _nl = (nl != NULL) ? nl : &nl_stub; 223 } 224 225 /* Function wrappers */ 226 bool 227 nlmsg_get_unicast_writer(struct nl_writer *nw, int size, struct nlpcb *nlp) 228 { 229 return (_nl->nlmsg_get_unicast_writer(nw, size, nlp)); 230 } 231 232 bool 233 nlmsg_get_group_writer(struct nl_writer *nw, int size, int protocol, int group_id) 234 { 235 return (_nl->nlmsg_get_group_writer(nw, size, protocol, group_id)); 236 } 237 238 bool 239 nlmsg_get_chain_writer(struct nl_writer *nw, int size, struct mbuf **pm) 240 { 241 return (_nl->nlmsg_get_chain_writer(nw, size, pm)); 242 } 243 244 bool 245 nlmsg_flush(struct nl_writer *nw) 246 { 247 return (_nl->nlmsg_flush(nw)); 248 } 249 250 void nlmsg_ignore_limit(struct nl_writer *nw) 251 { 252 _nl->nlmsg_ignore_limit(nw); 253 } 254 255 bool 256 nlmsg_refill_buffer(struct nl_writer *nw, int required_len) 257 { 258 return (_nl->nlmsg_refill_buffer(nw, required_len)); 259 } 260 261 bool 262 nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type, 263 uint16_t flags, uint32_t len) 264 { 265 return (_nl->nlmsg_add(nw, portid, seq, type, flags, len)); 266 } 267 268 bool 269 nlmsg_end(struct nl_writer *nw) 270 { 271 return (_nl->nlmsg_end(nw)); 272 } 273 274 void 275 nlmsg_abort(struct nl_writer *nw) 276 { 277 _nl->nlmsg_abort(nw); 278 } 279 280 bool 281 nlmsg_end_dump(struct nl_writer *nw, int error, struct nlmsghdr *hdr) 282 { 283 return (_nl->nlmsg_end_dump(nw, error, hdr)); 284 } 285 286 int 287 nl_modify_ifp_generic(struct ifnet *ifp, struct nl_parsed_link *lattrs, 288 const struct nlattr_bmask *bm , struct nl_pstate *npt) 289 { 290 return (_nl->nl_modify_ifp_generic(ifp, lattrs, bm, npt)); 291 } 292 293 void 294 nl_store_ifp_cookie(struct nl_pstate *npt, struct ifnet *ifp) 295 { 296 return (_nl->nl_store_ifp_cookie(npt, ifp)); 297 } 298 299 struct nlpcb * 300 nl_get_thread_nlp(struct thread *td) 301 { 302 return (_nl->nl_get_thread_nlp(td)); 303 } 304 305 #endif /* !NETLINK */ 306 307