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