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