1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1993 5 * The Regents of the University of California. All rights reserved. 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 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/socket.h> 34 #include <sys/protosw.h> 35 #include <sys/domain.h> 36 #include <sys/eventhandler.h> 37 #include <sys/epoch.h> 38 #include <sys/mbuf.h> 39 #include <sys/kernel.h> 40 #include <sys/lock.h> 41 #include <sys/mutex.h> 42 #include <sys/rmlock.h> 43 #include <sys/socketvar.h> 44 #include <sys/systm.h> 45 46 #include <machine/atomic.h> 47 48 #include <net/vnet.h> 49 50 struct domainhead domains = SLIST_HEAD_INITIALIZER(&domains); 51 int domain_init_status = 1; 52 static struct mtx dom_mtx; /* domain list lock */ 53 MTX_SYSINIT(domain, &dom_mtx, "domain list", MTX_DEF); 54 55 static int 56 pr_accept_notsupp(struct socket *so, struct sockaddr *sa) 57 { 58 return (EOPNOTSUPP); 59 } 60 61 static int 62 pr_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td) 63 { 64 return (EOPNOTSUPP); 65 } 66 67 static int 68 pr_bindat_notsupp(int fd, struct socket *so, struct sockaddr *nam, 69 struct thread *td) 70 { 71 return (EOPNOTSUPP); 72 } 73 74 static int 75 pr_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td) 76 { 77 return (EOPNOTSUPP); 78 } 79 80 static int 81 pr_connectat_notsupp(int fd, struct socket *so, struct sockaddr *nam, 82 struct thread *td) 83 { 84 return (EOPNOTSUPP); 85 } 86 87 static int 88 pr_connect2_notsupp(struct socket *so1, struct socket *so2) 89 { 90 return (EOPNOTSUPP); 91 } 92 93 static int 94 pr_control_notsupp(struct socket *so, u_long cmd, void *data, 95 struct ifnet *ifp, struct thread *td) 96 { 97 return (EOPNOTSUPP); 98 } 99 100 static int 101 pr_disconnect_notsupp(struct socket *so) 102 { 103 return (EOPNOTSUPP); 104 } 105 106 int 107 pr_listen_notsupp(struct socket *so, int backlog, struct thread *td) 108 { 109 return (EOPNOTSUPP); 110 } 111 112 static int 113 pr_peeraddr_notsupp(struct socket *so, struct sockaddr *nam) 114 { 115 return (EOPNOTSUPP); 116 } 117 118 static int 119 pr_rcvd_notsupp(struct socket *so, int flags) 120 { 121 return (EOPNOTSUPP); 122 } 123 124 static int 125 pr_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags) 126 { 127 return (EOPNOTSUPP); 128 } 129 130 static int 131 pr_send_notsupp(struct socket *so, int flags, struct mbuf *m, 132 struct sockaddr *addr, struct mbuf *control, struct thread *td) 133 { 134 if (control != NULL) 135 m_freem(control); 136 if ((flags & PRUS_NOTREADY) == 0) 137 m_freem(m); 138 return (EOPNOTSUPP); 139 } 140 141 static int 142 pr_ready_notsupp(struct socket *so, struct mbuf *m, int count) 143 { 144 return (EOPNOTSUPP); 145 } 146 147 static int 148 pr_shutdown_notsupp(struct socket *so, enum shutdown_how how) 149 { 150 return (EOPNOTSUPP); 151 } 152 153 static int 154 pr_sockaddr_notsupp(struct socket *so, struct sockaddr *nam) 155 { 156 return (EOPNOTSUPP); 157 } 158 159 static int 160 pr_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio, 161 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) 162 { 163 return (EOPNOTSUPP); 164 } 165 166 static int 167 pr_soreceive_notsupp(struct socket *so, struct sockaddr **paddr, 168 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp) 169 { 170 return (EOPNOTSUPP); 171 } 172 173 static void 174 pr_init(struct domain *dom, struct protosw *pr) 175 { 176 177 KASSERT(pr->pr_attach != NULL, 178 ("%s: protocol doesn't have pr_attach", __func__)); 179 180 pr->pr_domain = dom; 181 182 #define DEFAULT(foo, bar) if (pr->foo == NULL) pr->foo = bar 183 DEFAULT(pr_sosend, sosend_generic); 184 DEFAULT(pr_soreceive, soreceive_generic); 185 DEFAULT(pr_sopoll, sopoll_generic); 186 DEFAULT(pr_setsbopt, sbsetopt); 187 DEFAULT(pr_aio_queue, soaio_queue_generic); 188 189 #define NOTSUPP(foo) if (pr->foo == NULL) pr->foo = foo ## _notsupp 190 NOTSUPP(pr_accept); 191 NOTSUPP(pr_bind); 192 NOTSUPP(pr_bindat); 193 NOTSUPP(pr_connect); 194 NOTSUPP(pr_connect2); 195 NOTSUPP(pr_connectat); 196 NOTSUPP(pr_control); 197 NOTSUPP(pr_disconnect); 198 NOTSUPP(pr_listen); 199 NOTSUPP(pr_peeraddr); 200 NOTSUPP(pr_rcvd); 201 NOTSUPP(pr_rcvoob); 202 NOTSUPP(pr_send); 203 NOTSUPP(pr_shutdown); 204 NOTSUPP(pr_sockaddr); 205 NOTSUPP(pr_sosend); 206 NOTSUPP(pr_soreceive); 207 NOTSUPP(pr_ready); 208 } 209 210 /* 211 * Add a new protocol domain to the list of supported domains 212 * Note: you can't unload it again because a socket may be using it. 213 * XXX can't fail at this time. 214 */ 215 void 216 domain_add(struct domain *dp) 217 { 218 struct protosw *pr; 219 220 MPASS(IS_DEFAULT_VNET(curvnet)); 221 222 if (dp->dom_probe != NULL && (*dp->dom_probe)() != 0) 223 return; 224 225 for (int i = 0; i < dp->dom_nprotosw; i++) 226 if ((pr = dp->dom_protosw[i]) != NULL) 227 pr_init(dp, pr); 228 229 mtx_lock(&dom_mtx); 230 #ifdef INVARIANTS 231 struct domain *tmp; 232 SLIST_FOREACH(tmp, &domains, dom_next) 233 MPASS(tmp->dom_family != dp->dom_family); 234 #endif 235 SLIST_INSERT_HEAD(&domains, dp, dom_next); 236 mtx_unlock(&dom_mtx); 237 } 238 239 void 240 domain_remove(struct domain *dp) 241 { 242 243 if ((dp->dom_flags & DOMF_UNLOADABLE) == 0) 244 return; 245 246 mtx_lock(&dom_mtx); 247 SLIST_REMOVE(&domains, dp, domain, dom_next); 248 mtx_unlock(&dom_mtx); 249 } 250 251 static void 252 domainfinalize(void *dummy) 253 { 254 255 mtx_lock(&dom_mtx); 256 KASSERT(domain_init_status == 1, ("domainfinalize called too late!")); 257 domain_init_status = 2; 258 mtx_unlock(&dom_mtx); 259 } 260 SYSINIT(domainfin, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, domainfinalize, 261 NULL); 262 263 struct domain * 264 pffinddomain(int family) 265 { 266 struct domain *dp; 267 268 SLIST_FOREACH(dp, &domains, dom_next) 269 if (dp->dom_family == family) 270 return (dp); 271 return (NULL); 272 } 273 274 struct protosw * 275 pffindproto(int family, int type, int proto) 276 { 277 struct domain *dp; 278 struct protosw *pr; 279 280 dp = pffinddomain(family); 281 if (dp == NULL) 282 return (NULL); 283 284 for (int i = 0; i < dp->dom_nprotosw; i++) 285 if ((pr = dp->dom_protosw[i]) != NULL && pr->pr_type == type && 286 (pr->pr_protocol == 0 || proto == 0 || 287 pr->pr_protocol == proto)) 288 return (pr); 289 290 return (NULL); 291 } 292 293 /* 294 * The caller must make sure that the new protocol is fully set up and ready to 295 * accept requests before it is registered. 296 */ 297 int 298 protosw_register(struct domain *dp, struct protosw *npr) 299 { 300 struct protosw **prp; 301 302 MPASS(dp); 303 MPASS(npr && npr->pr_type > 0 && npr->pr_protocol > 0); 304 305 prp = NULL; 306 /* 307 * Protect us against races when two protocol registrations for 308 * the same protocol happen at the same time. 309 */ 310 mtx_lock(&dom_mtx); 311 for (int i = 0; i < dp->dom_nprotosw; i++) { 312 if (dp->dom_protosw[i] == NULL) { 313 /* Remember the first free spacer. */ 314 if (prp == NULL) 315 prp = &dp->dom_protosw[i]; 316 } else { 317 /* 318 * The new protocol must not yet exist. 319 * XXXAO: Check only protocol? 320 * XXXGL: Maybe assert that it doesn't exist? 321 */ 322 if ((dp->dom_protosw[i]->pr_type == npr->pr_type) && 323 (dp->dom_protosw[i]->pr_protocol == 324 npr->pr_protocol)) { 325 mtx_unlock(&dom_mtx); 326 return (EEXIST); 327 } 328 329 } 330 } 331 332 /* If no free spacer is found we can't add the new protocol. */ 333 if (prp == NULL) { 334 mtx_unlock(&dom_mtx); 335 return (ENOMEM); 336 } 337 338 pr_init(dp, npr); 339 *prp = npr; 340 mtx_unlock(&dom_mtx); 341 342 return (0); 343 } 344 345 /* 346 * The caller must make sure the protocol and its functions correctly shut down 347 * all sockets and release all locks and memory references. 348 */ 349 int 350 protosw_unregister(struct protosw *pr) 351 { 352 struct domain *dp; 353 struct protosw **prp; 354 355 dp = pr->pr_domain; 356 prp = NULL; 357 358 mtx_lock(&dom_mtx); 359 /* The protocol must exist and only once. */ 360 for (int i = 0; i < dp->dom_nprotosw; i++) { 361 if (dp->dom_protosw[i] == pr) { 362 KASSERT(prp == NULL, 363 ("%s: domain %p protocol %p registered twice\n", 364 __func__, dp, pr)); 365 prp = &dp->dom_protosw[i]; 366 } 367 } 368 369 /* Protocol does not exist. XXXGL: assert that it does? */ 370 if (prp == NULL) { 371 mtx_unlock(&dom_mtx); 372 return (EPROTONOSUPPORT); 373 } 374 375 /* De-orbit the protocol and make the slot available again. */ 376 *prp = NULL; 377 mtx_unlock(&dom_mtx); 378 379 return (0); 380 } 381