2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
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
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>
39 #include <sys/kernel.h>
41 #include <sys/mutex.h>
42 #include <sys/rmlock.h>
43 #include <sys/socketvar.h>
44 #include <sys/systm.h>
46 #include <machine/atomic.h>
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);
56 pr_accept_notsupp(struct socket *so, struct sockaddr *sa)
62 pr_aio_queue_notsupp(struct socket *so, struct kaiocb *job)
68 pr_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
74 pr_bindat_notsupp(int fd, struct socket *so, struct sockaddr *nam,
81 pr_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
87 pr_connectat_notsupp(int fd, struct socket *so, struct sockaddr *nam,
94 pr_connect2_notsupp(struct socket *so1, struct socket *so2)
100 pr_control_notsupp(struct socket *so, u_long cmd, void *data,
101 struct ifnet *ifp, struct thread *td)
107 pr_disconnect_notsupp(struct socket *so)
113 pr_listen_notsupp(struct socket *so, int backlog, struct thread *td)
119 pr_peeraddr_notsupp(struct socket *so, struct sockaddr *nam)
125 pr_rcvd_notsupp(struct socket *so, int flags)
131 pr_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
137 pr_send_notsupp(struct socket *so, int flags, struct mbuf *m,
138 struct sockaddr *addr, struct mbuf *control, struct thread *td)
142 if ((flags & PRUS_NOTREADY) == 0)
148 pr_ready_notsupp(struct socket *so, struct mbuf *m, int count)
154 pr_shutdown_notsupp(struct socket *so)
160 pr_sockaddr_notsupp(struct socket *so, struct sockaddr *nam)
166 pr_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
167 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
173 pr_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
174 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
180 pr_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
187 pr_init(struct domain *dom, struct protosw *pr)
190 KASSERT(pr->pr_attach != NULL,
191 ("%s: protocol doesn't have pr_attach", __func__));
195 #define DEFAULT(foo, bar) if (pr->foo == NULL) pr->foo = bar
196 DEFAULT(pr_sosend, sosend_generic);
197 DEFAULT(pr_soreceive, soreceive_generic);
198 DEFAULT(pr_sopoll, sopoll_generic);
199 DEFAULT(pr_setsbopt, sbsetopt);
201 #define NOTSUPP(foo) if (pr->foo == NULL) pr->foo = foo ## _notsupp
203 NOTSUPP(pr_aio_queue);
207 NOTSUPP(pr_connect2);
208 NOTSUPP(pr_connectat);
210 NOTSUPP(pr_disconnect);
212 NOTSUPP(pr_peeraddr);
216 NOTSUPP(pr_shutdown);
217 NOTSUPP(pr_sockaddr);
219 NOTSUPP(pr_soreceive);
225 * Add a new protocol domain to the list of supported domains
226 * Note: you cant unload it again because a socket may be using it.
227 * XXX can't fail at this time.
230 domain_add(struct domain *dp)
234 MPASS(IS_DEFAULT_VNET(curvnet));
236 if (dp->dom_probe != NULL && (*dp->dom_probe)() != 0)
239 for (int i = 0; i < dp->dom_nprotosw; i++)
240 if ((pr = dp->dom_protosw[i]) != NULL)
246 SLIST_FOREACH(tmp, &domains, dom_next)
247 MPASS(tmp->dom_family != dp->dom_family);
249 SLIST_INSERT_HEAD(&domains, dp, dom_next);
250 mtx_unlock(&dom_mtx);
254 domain_remove(struct domain *dp)
257 if ((dp->dom_flags & DOMF_UNLOADABLE) == 0)
261 SLIST_REMOVE(&domains, dp, domain, dom_next);
262 mtx_unlock(&dom_mtx);
266 domainfinalize(void *dummy)
270 KASSERT(domain_init_status == 1, ("domainfinalize called too late!"));
271 domain_init_status = 2;
272 mtx_unlock(&dom_mtx);
274 SYSINIT(domainfin, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, domainfinalize,
278 pffinddomain(int family)
282 SLIST_FOREACH(dp, &domains, dom_next)
283 if (dp->dom_family == family)
289 pffindproto(int family, int type, int proto)
294 dp = pffinddomain(family);
298 for (int i = 0; i < dp->dom_nprotosw; i++)
299 if ((pr = dp->dom_protosw[i]) != NULL && pr->pr_type == type &&
300 (pr->pr_protocol == 0 || proto == 0 ||
301 pr->pr_protocol == proto))
308 * The caller must make sure that the new protocol is fully set up and ready to
309 * accept requests before it is registered.
312 protosw_register(struct domain *dp, struct protosw *npr)
314 struct protosw **prp;
317 MPASS(npr && npr->pr_type > 0 && npr->pr_protocol > 0);
321 * Protect us against races when two protocol registrations for
322 * the same protocol happen at the same time.
325 for (int i = 0; i < dp->dom_nprotosw; i++) {
326 if (dp->dom_protosw[i] == NULL) {
327 /* Remember the first free spacer. */
329 prp = &dp->dom_protosw[i];
332 * The new protocol must not yet exist.
333 * XXXAO: Check only protocol?
334 * XXXGL: Maybe assert that it doesn't exist?
336 if ((dp->dom_protosw[i]->pr_type == npr->pr_type) &&
337 (dp->dom_protosw[i]->pr_protocol ==
339 mtx_unlock(&dom_mtx);
346 /* If no free spacer is found we can't add the new protocol. */
348 mtx_unlock(&dom_mtx);
354 mtx_unlock(&dom_mtx);
360 * The caller must make sure the protocol and its functions correctly shut down
361 * all sockets and release all locks and memory references.
364 protosw_unregister(struct protosw *pr)
367 struct protosw **prp;
373 /* The protocol must exist and only once. */
374 for (int i = 0; i < dp->dom_nprotosw; i++) {
375 if (dp->dom_protosw[i] == pr) {
377 ("%s: domain %p protocol %p registered twice\n",
379 prp = &dp->dom_protosw[i];
383 /* Protocol does not exist. XXXGL: assert that it does? */
385 mtx_unlock(&dom_mtx);
386 return (EPROTONOSUPPORT);
389 /* De-orbit the protocol and make the slot available again. */
391 mtx_unlock(&dom_mtx);