xref: /freebsd/sys/kern/uipc_domain.c (revision 94387f252a6227ce5964d8d66fc8892b0a604b2a)
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
pr_accept_notsupp(struct socket * so,struct sockaddr * sa)56 pr_accept_notsupp(struct socket *so, struct sockaddr *sa)
57 {
58 	return (EOPNOTSUPP);
59 }
60 
61 static int
pr_aio_queue_notsupp(struct socket * so,struct kaiocb * job)62 pr_aio_queue_notsupp(struct socket *so, struct kaiocb *job)
63 {
64 	return (EOPNOTSUPP);
65 }
66 
67 static int
pr_bind_notsupp(struct socket * so,struct sockaddr * nam,struct thread * td)68 pr_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
69 {
70 	return (EOPNOTSUPP);
71 }
72 
73 static int
pr_bindat_notsupp(int fd,struct socket * so,struct sockaddr * nam,struct thread * td)74 pr_bindat_notsupp(int fd, struct socket *so, struct sockaddr *nam,
75     struct thread *td)
76 {
77 	return (EOPNOTSUPP);
78 }
79 
80 static int
pr_connect_notsupp(struct socket * so,struct sockaddr * nam,struct thread * td)81 pr_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
82 {
83 	return (EOPNOTSUPP);
84 }
85 
86 static int
pr_connectat_notsupp(int fd,struct socket * so,struct sockaddr * nam,struct thread * td)87 pr_connectat_notsupp(int fd, struct socket *so, struct sockaddr *nam,
88     struct thread *td)
89 {
90 	return (EOPNOTSUPP);
91 }
92 
93 static int
pr_connect2_notsupp(struct socket * so1,struct socket * so2)94 pr_connect2_notsupp(struct socket *so1, struct socket *so2)
95 {
96 	return (EOPNOTSUPP);
97 }
98 
99 static int
pr_control_notsupp(struct socket * so,u_long cmd,void * data,struct ifnet * ifp,struct thread * td)100 pr_control_notsupp(struct socket *so, u_long cmd, void *data,
101     struct ifnet *ifp, struct thread *td)
102 {
103 	return (EOPNOTSUPP);
104 }
105 
106 static int
pr_disconnect_notsupp(struct socket * so)107 pr_disconnect_notsupp(struct socket *so)
108 {
109 	return (EOPNOTSUPP);
110 }
111 
112 int
pr_listen_notsupp(struct socket * so,int backlog,struct thread * td)113 pr_listen_notsupp(struct socket *so, int backlog, struct thread *td)
114 {
115 	return (EOPNOTSUPP);
116 }
117 
118 static int
pr_peeraddr_notsupp(struct socket * so,struct sockaddr * nam)119 pr_peeraddr_notsupp(struct socket *so, struct sockaddr *nam)
120 {
121 	return (EOPNOTSUPP);
122 }
123 
124 static int
pr_rcvd_notsupp(struct socket * so,int flags)125 pr_rcvd_notsupp(struct socket *so, int flags)
126 {
127 	return (EOPNOTSUPP);
128 }
129 
130 static int
pr_rcvoob_notsupp(struct socket * so,struct mbuf * m,int flags)131 pr_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
132 {
133 	return (EOPNOTSUPP);
134 }
135 
136 static int
pr_send_notsupp(struct socket * so,int flags,struct mbuf * m,struct sockaddr * addr,struct mbuf * control,struct thread * td)137 pr_send_notsupp(struct socket *so, int flags, struct mbuf *m,
138     struct sockaddr *addr, struct mbuf *control, struct thread *td)
139 {
140 	if (control != NULL)
141 		m_freem(control);
142 	if ((flags & PRUS_NOTREADY) == 0)
143 		m_freem(m);
144 	return (EOPNOTSUPP);
145 }
146 
147 static int
pr_ready_notsupp(struct socket * so,struct mbuf * m,int count)148 pr_ready_notsupp(struct socket *so, struct mbuf *m, int count)
149 {
150 	return (EOPNOTSUPP);
151 }
152 
153 static int
pr_shutdown_notsupp(struct socket * so,enum shutdown_how how)154 pr_shutdown_notsupp(struct socket *so, enum shutdown_how how)
155 {
156 	return (EOPNOTSUPP);
157 }
158 
159 static int
pr_sockaddr_notsupp(struct socket * so,struct sockaddr * nam)160 pr_sockaddr_notsupp(struct socket *so, struct sockaddr *nam)
161 {
162 	return (EOPNOTSUPP);
163 }
164 
165 static int
pr_sosend_notsupp(struct socket * so,struct sockaddr * addr,struct uio * uio,struct mbuf * top,struct mbuf * control,int flags,struct thread * td)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)
168 {
169 	return (EOPNOTSUPP);
170 }
171 
172 static int
pr_soreceive_notsupp(struct socket * so,struct sockaddr ** paddr,struct uio * uio,struct mbuf ** mp0,struct mbuf ** controlp,int * flagsp)173 pr_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
174     struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
175 {
176 	return (EOPNOTSUPP);
177 }
178 
179 static void
pr_init(struct domain * dom,struct protosw * pr)180 pr_init(struct domain *dom, struct protosw *pr)
181 {
182 
183 	KASSERT(pr->pr_attach != NULL,
184 	    ("%s: protocol doesn't have pr_attach", __func__));
185 
186 	pr->pr_domain = dom;
187 
188 #define	DEFAULT(foo, bar)	if (pr->foo == NULL) pr->foo = bar
189 	DEFAULT(pr_sosend, sosend_generic);
190 	DEFAULT(pr_soreceive, soreceive_generic);
191 	DEFAULT(pr_sopoll, sopoll_generic);
192 	DEFAULT(pr_setsbopt, sbsetopt);
193 
194 #define NOTSUPP(foo)	if (pr->foo == NULL)  pr->foo = foo ## _notsupp
195 	NOTSUPP(pr_accept);
196 	NOTSUPP(pr_aio_queue);
197 	NOTSUPP(pr_bind);
198 	NOTSUPP(pr_bindat);
199 	NOTSUPP(pr_connect);
200 	NOTSUPP(pr_connect2);
201 	NOTSUPP(pr_connectat);
202 	NOTSUPP(pr_control);
203 	NOTSUPP(pr_disconnect);
204 	NOTSUPP(pr_listen);
205 	NOTSUPP(pr_peeraddr);
206 	NOTSUPP(pr_rcvd);
207 	NOTSUPP(pr_rcvoob);
208 	NOTSUPP(pr_send);
209 	NOTSUPP(pr_shutdown);
210 	NOTSUPP(pr_sockaddr);
211 	NOTSUPP(pr_sosend);
212 	NOTSUPP(pr_soreceive);
213 	NOTSUPP(pr_ready);
214 }
215 
216 /*
217  * Add a new protocol domain to the list of supported domains
218  * Note: you can't unload it again because a socket may be using it.
219  * XXX can't fail at this time.
220  */
221 void
domain_add(struct domain * dp)222 domain_add(struct domain *dp)
223 {
224 	struct protosw *pr;
225 
226 	MPASS(IS_DEFAULT_VNET(curvnet));
227 
228 	if (dp->dom_probe != NULL && (*dp->dom_probe)() != 0)
229 		return;
230 
231 	for (int i = 0; i < dp->dom_nprotosw; i++)
232 		if ((pr = dp->dom_protosw[i]) != NULL)
233 			pr_init(dp, pr);
234 
235 	mtx_lock(&dom_mtx);
236 #ifdef INVARIANTS
237 	struct domain *tmp;
238 	SLIST_FOREACH(tmp, &domains, dom_next)
239 		MPASS(tmp->dom_family != dp->dom_family);
240 #endif
241 	SLIST_INSERT_HEAD(&domains, dp, dom_next);
242 	mtx_unlock(&dom_mtx);
243 }
244 
245 void
domain_remove(struct domain * dp)246 domain_remove(struct domain *dp)
247 {
248 
249 	if ((dp->dom_flags & DOMF_UNLOADABLE) == 0)
250 		return;
251 
252 	mtx_lock(&dom_mtx);
253 	SLIST_REMOVE(&domains, dp, domain, dom_next);
254 	mtx_unlock(&dom_mtx);
255 }
256 
257 static void
domainfinalize(void * dummy)258 domainfinalize(void *dummy)
259 {
260 
261 	mtx_lock(&dom_mtx);
262 	KASSERT(domain_init_status == 1, ("domainfinalize called too late!"));
263 	domain_init_status = 2;
264 	mtx_unlock(&dom_mtx);
265 }
266 SYSINIT(domainfin, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, domainfinalize,
267     NULL);
268 
269 struct domain *
pffinddomain(int family)270 pffinddomain(int family)
271 {
272 	struct domain *dp;
273 
274 	SLIST_FOREACH(dp, &domains, dom_next)
275 		if (dp->dom_family == family)
276 			return (dp);
277 	return (NULL);
278 }
279 
280 struct protosw *
pffindproto(int family,int type,int proto)281 pffindproto(int family, int type, int proto)
282 {
283 	struct domain *dp;
284 	struct protosw *pr;
285 
286 	dp = pffinddomain(family);
287 	if (dp == NULL)
288 		return (NULL);
289 
290 	for (int i = 0; i < dp->dom_nprotosw; i++)
291 		if ((pr = dp->dom_protosw[i]) != NULL && pr->pr_type == type &&
292 		    (pr->pr_protocol == 0 || proto == 0 ||
293 		     pr->pr_protocol == proto))
294 			return (pr);
295 
296 	return (NULL);
297 }
298 
299 /*
300  * The caller must make sure that the new protocol is fully set up and ready to
301  * accept requests before it is registered.
302  */
303 int
protosw_register(struct domain * dp,struct protosw * npr)304 protosw_register(struct domain *dp, struct protosw *npr)
305 {
306 	struct protosw **prp;
307 
308 	MPASS(dp);
309 	MPASS(npr && npr->pr_type > 0 && npr->pr_protocol > 0);
310 
311 	prp = NULL;
312 	/*
313 	 * Protect us against races when two protocol registrations for
314 	 * the same protocol happen at the same time.
315 	 */
316 	mtx_lock(&dom_mtx);
317 	for (int i = 0; i < dp->dom_nprotosw; i++) {
318 		if (dp->dom_protosw[i] == NULL) {
319 			/* Remember the first free spacer. */
320 			if (prp == NULL)
321 				prp = &dp->dom_protosw[i];
322 		} else {
323 			/*
324 			 * The new protocol must not yet exist.
325 			 * XXXAO: Check only protocol?
326 			 * XXXGL: Maybe assert that it doesn't exist?
327 			 */
328 			if ((dp->dom_protosw[i]->pr_type == npr->pr_type) &&
329 			    (dp->dom_protosw[i]->pr_protocol ==
330 			    npr->pr_protocol)) {
331 				mtx_unlock(&dom_mtx);
332 				return (EEXIST);
333 			}
334 
335 		}
336 	}
337 
338 	/* If no free spacer is found we can't add the new protocol. */
339 	if (prp == NULL) {
340 		mtx_unlock(&dom_mtx);
341 		return (ENOMEM);
342 	}
343 
344 	pr_init(dp, npr);
345 	*prp = npr;
346 	mtx_unlock(&dom_mtx);
347 
348 	return (0);
349 }
350 
351 /*
352  * The caller must make sure the protocol and its functions correctly shut down
353  * all sockets and release all locks and memory references.
354  */
355 int
protosw_unregister(struct protosw * pr)356 protosw_unregister(struct protosw *pr)
357 {
358 	struct domain *dp;
359 	struct protosw **prp;
360 
361 	dp = pr->pr_domain;
362 	prp = NULL;
363 
364 	mtx_lock(&dom_mtx);
365 	/* The protocol must exist and only once. */
366 	for (int i = 0; i < dp->dom_nprotosw; i++) {
367 		if (dp->dom_protosw[i] == pr) {
368 			KASSERT(prp == NULL,
369 			    ("%s: domain %p protocol %p registered twice\n",
370 			    __func__, dp, pr));
371 			prp = &dp->dom_protosw[i];
372 		}
373 	}
374 
375 	/* Protocol does not exist.  XXXGL: assert that it does? */
376 	if (prp == NULL) {
377 		mtx_unlock(&dom_mtx);
378 		return (EPROTONOSUPPORT);
379 	}
380 
381 	/* De-orbit the protocol and make the slot available again. */
382 	*prp = NULL;
383 	mtx_unlock(&dom_mtx);
384 
385 	return (0);
386 }
387