2 * Copyright (c) 1982, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)uipc_domain.c 8.2 (Berkeley) 10/18/93
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
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>
41 #include <sys/kernel.h>
43 #include <sys/mutex.h>
44 #include <sys/socketvar.h>
45 #include <sys/systm.h>
49 * System initialization
51 * Note: domain initialization takes place on a per domain basis
52 * as a result of traversing a SYSINIT linker set. Most likely,
53 * each domain would want to call DOMAIN_SET(9) itself, which
54 * would cause the domain to be added just after domaininit()
55 * is called during startup.
57 * See DOMAIN_SET(9) for details on its use.
60 static void domaininit(void *);
61 SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL);
63 static void domainfinalize(void *);
64 SYSINIT(domainfin, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, domainfinalize,
67 static struct callout pffast_callout;
68 static struct callout pfslow_callout;
70 static void pffasttimo(void *);
71 static void pfslowtimo(void *);
73 struct domain *domains; /* registered protocol domains */
74 int domain_init_status = 0;
75 static struct mtx dom_mtx; /* domain list lock */
76 MTX_SYSINIT(domain, &dom_mtx, "domain list", MTX_DEF);
79 * Dummy protocol specific user requests function pointer array.
80 * All functions return EOPNOTSUPP.
82 struct pr_usrreqs nousrreqs = {
83 .pru_accept = pru_accept_notsupp,
84 .pru_attach = pru_attach_notsupp,
85 .pru_bind = pru_bind_notsupp,
86 .pru_connect = pru_connect_notsupp,
87 .pru_connect2 = pru_connect2_notsupp,
88 .pru_control = pru_control_notsupp,
89 .pru_disconnect = pru_disconnect_notsupp,
90 .pru_listen = pru_listen_notsupp,
91 .pru_peeraddr = pru_peeraddr_notsupp,
92 .pru_rcvd = pru_rcvd_notsupp,
93 .pru_rcvoob = pru_rcvoob_notsupp,
94 .pru_send = pru_send_notsupp,
95 .pru_sense = pru_sense_null,
96 .pru_shutdown = pru_shutdown_notsupp,
97 .pru_sockaddr = pru_sockaddr_notsupp,
98 .pru_sosend = pru_sosend_notsupp,
99 .pru_soreceive = pru_soreceive_notsupp,
100 .pru_sopoll = pru_sopoll_notsupp,
104 protosw_init(struct protosw *pr)
106 struct pr_usrreqs *pu;
109 KASSERT(pu != NULL, ("protosw_init: %ssw[%d] has no usrreqs!",
110 pr->pr_domain->dom_name,
111 (int)(pr - pr->pr_domain->dom_protosw)));
113 #define DEFAULT(foo, bar) if ((foo) == NULL) (foo) = (bar)
114 DEFAULT(pu->pru_accept, pru_accept_notsupp);
115 DEFAULT(pu->pru_bind, pru_bind_notsupp);
116 DEFAULT(pu->pru_connect, pru_connect_notsupp);
117 DEFAULT(pu->pru_connect2, pru_connect2_notsupp);
118 DEFAULT(pu->pru_control, pru_control_notsupp);
119 DEFAULT(pu->pru_disconnect, pru_disconnect_notsupp);
120 DEFAULT(pu->pru_listen, pru_listen_notsupp);
121 DEFAULT(pu->pru_peeraddr, pru_peeraddr_notsupp);
122 DEFAULT(pu->pru_rcvd, pru_rcvd_notsupp);
123 DEFAULT(pu->pru_rcvoob, pru_rcvoob_notsupp);
124 DEFAULT(pu->pru_sense, pru_sense_null);
125 DEFAULT(pu->pru_shutdown, pru_shutdown_notsupp);
126 DEFAULT(pu->pru_sockaddr, pru_sockaddr_notsupp);
127 DEFAULT(pu->pru_sosend, sosend_generic);
128 DEFAULT(pu->pru_soreceive, soreceive_generic);
129 DEFAULT(pu->pru_sopoll, sopoll_generic);
136 * Add a new protocol domain to the list of supported domains
137 * Note: you cant unload it again because a socket may be using it.
138 * XXX can't fail at this time.
141 net_init_domain(struct domain *dp)
147 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
150 * update global information about maximums
152 max_hdr = max_linkhdr + max_protohdr;
153 max_datalen = MHLEN - max_hdr;
155 panic("%s: max_datalen < 1", __func__);
159 * Add a new protocol domain to the list of supported domains
160 * Note: you cant unload it again because a socket may be using it.
161 * XXX can't fail at this time.
164 net_add_domain(void *data)
168 dp = (struct domain *)data;
170 dp->dom_next = domains;
173 KASSERT(domain_init_status >= 1,
174 ("attempt to net_add_domain(%s) before domaininit()",
177 if (domain_init_status < 1)
178 printf("WARNING: attempt to net_add_domain(%s) before "
179 "domaininit()\n", dp->dom_name);
182 KASSERT(domain_init_status < 2,
183 ("attempt to net_add_domain(%s) after domainfinalize()",
186 if (domain_init_status >= 2)
187 printf("WARNING: attempt to net_add_domain(%s) after "
188 "domainfinalize()\n", dp->dom_name);
190 mtx_unlock(&dom_mtx);
195 socket_zone_change(void *tag)
198 uma_zone_set_max(socket_zone, maxsockets);
203 domaininit(void *dummy)
207 * Before we do any setup, make sure to initialize the
208 * zone allocator we get struct sockets from.
210 socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL,
211 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
212 uma_zone_set_max(socket_zone, maxsockets);
213 EVENTHANDLER_REGISTER(maxsockets_change, socket_zone_change, NULL,
214 EVENTHANDLER_PRI_FIRST);
216 if (max_linkhdr < 16) /* XXX */
219 callout_init(&pffast_callout, CALLOUT_MPSAFE);
220 callout_init(&pfslow_callout, CALLOUT_MPSAFE);
223 KASSERT(domain_init_status == 0, ("domaininit called too late!"));
224 domain_init_status = 1;
225 mtx_unlock(&dom_mtx);
230 domainfinalize(void *dummy)
234 KASSERT(domain_init_status == 1, ("domainfinalize called too late!"));
235 domain_init_status = 2;
236 mtx_unlock(&dom_mtx);
238 callout_reset(&pffast_callout, 1, pffasttimo, NULL);
239 callout_reset(&pfslow_callout, 1, pfslowtimo, NULL);
243 pffindtype(int family, int type)
248 for (dp = domains; dp; dp = dp->dom_next)
249 if (dp->dom_family == family)
253 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
254 if (pr->pr_type && pr->pr_type == type)
260 pffindproto(int family, int protocol, int type)
264 struct protosw *maybe = 0;
268 for (dp = domains; dp; dp = dp->dom_next)
269 if (dp->dom_family == family)
273 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
274 if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
277 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
278 pr->pr_protocol == 0 && maybe == (struct protosw *)0)
285 * The caller must make sure that the new protocol is fully set up and ready to
286 * accept requests before it is registered.
289 pf_proto_register(int family, struct protosw *npr)
292 struct protosw *pr, *fpr;
296 return (EPFNOSUPPORT);
297 if (npr->pr_type == 0)
299 if (npr->pr_protocol == 0)
300 return (EPROTONOSUPPORT);
301 if (npr->pr_usrreqs == NULL)
304 /* Try to find the specified domain based on the family. */
305 for (dp = domains; dp; dp = dp->dom_next)
306 if (dp->dom_family == family)
308 return (EPFNOSUPPORT);
311 /* Initialize backpointer to struct domain. */
316 * Protect us against races when two protocol registrations for
317 * the same protocol happen at the same time.
321 /* The new protocol must not yet exist. */
322 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
323 if ((pr->pr_type == npr->pr_type) &&
324 (pr->pr_protocol == npr->pr_protocol)) {
325 mtx_unlock(&dom_mtx);
326 return (EEXIST); /* XXX: Check only protocol? */
328 /* While here, remember the first free spacer. */
329 if ((fpr == NULL) && (pr->pr_protocol == PROTO_SPACER))
333 /* If no free spacer is found we can't add the new protocol. */
335 mtx_unlock(&dom_mtx);
339 /* Copy the new struct protosw over the spacer. */
340 bcopy(npr, fpr, sizeof(*fpr));
342 /* Job is done, no more protection required. */
343 mtx_unlock(&dom_mtx);
345 /* Initialize and activate the protocol. */
352 * The caller must make sure the protocol and its functions correctly shut down
353 * all sockets and release all locks and memory references.
356 pf_proto_unregister(int family, int protocol, int type)
359 struct protosw *pr, *dpr;
363 return (EPFNOSUPPORT);
365 return (EPROTONOSUPPORT);
369 /* Try to find the specified domain based on the family type. */
370 for (dp = domains; dp; dp = dp->dom_next)
371 if (dp->dom_family == family)
373 return (EPFNOSUPPORT);
378 /* Lock out everyone else while we are manipulating the protosw. */
381 /* The protocol must exist and only once. */
382 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
383 if ((pr->pr_type == type) && (pr->pr_protocol == protocol)) {
385 mtx_unlock(&dom_mtx);
386 return (EMLINK); /* Should not happen! */
392 /* Protocol does not exist. */
394 mtx_unlock(&dom_mtx);
395 return (EPROTONOSUPPORT);
398 /* De-orbit the protocol and make the slot available again. */
401 dpr->pr_protocol = PROTO_SPACER;
403 dpr->pr_input = NULL;
404 dpr->pr_output = NULL;
405 dpr->pr_ctlinput = NULL;
406 dpr->pr_ctloutput = NULL;
407 dpr->pr_ousrreq = NULL;
409 dpr->pr_fasttimo = NULL;
410 dpr->pr_slowtimo = NULL;
411 dpr->pr_drain = NULL;
412 dpr->pr_usrreqs = &nousrreqs;
414 /* Job is done, not more protection required. */
415 mtx_unlock(&dom_mtx);
421 pfctlinput(int cmd, struct sockaddr *sa)
426 for (dp = domains; dp; dp = dp->dom_next)
427 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
429 (*pr->pr_ctlinput)(cmd, sa, (void *)0);
433 pfctlinput2(int cmd, struct sockaddr *sa, void *ctlparam)
440 for (dp = domains; dp; dp = dp->dom_next) {
442 * the check must be made by xx_ctlinput() anyways, to
443 * make sure we use data item pointed to by ctlparam in
444 * correct way. the following check is made just for safety.
446 if (dp->dom_family != sa->sa_family)
449 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
451 (*pr->pr_ctlinput)(cmd, sa, ctlparam);
456 pfslowtimo(void *arg)
461 for (dp = domains; dp; dp = dp->dom_next)
462 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
464 (*pr->pr_slowtimo)();
465 callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL);
469 pffasttimo(void *arg)
474 for (dp = domains; dp; dp = dp->dom_next)
475 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
477 (*pr->pr_fasttimo)();
478 callout_reset(&pffast_callout, hz/5, pffasttimo, NULL);