uipc_domain.c revision 127574
1/*
2 * Copyright (c) 1982, 1986, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)uipc_domain.c	8.2 (Berkeley) 10/18/93
34 */
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/sys/kern/uipc_domain.c 127574 2004-03-29 17:00:05Z rwatson $");
38
39#include <sys/param.h>
40#include <sys/socket.h>
41#include <sys/protosw.h>
42#include <sys/domain.h>
43#include <sys/mbuf.h>
44#include <sys/kernel.h>
45#include <sys/lock.h>
46#include <sys/mutex.h>
47#include <sys/socketvar.h>
48#include <sys/systm.h>
49#include <vm/uma.h>
50
51/*
52 * System initialization
53 *
54 * Note: domain initialization takes place on a per domain basis
55 * as a result of traversing a SYSINIT linker set.  Most likely,
56 * each domain would want to call DOMAIN_SET(9) itself, which
57 * would cause the domain to be added just after domaininit()
58 * is called during startup.
59 *
60 * See DOMAIN_SET(9) for details on its use.
61 */
62
63static void domaininit(void *);
64SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL)
65
66static struct callout pffast_callout;
67static struct callout pfslow_callout;
68
69static void	pffasttimo(void *);
70static void	pfslowtimo(void *);
71
72struct domain *domains;		/* registered protocol domains */
73struct mtx dom_mtx;		/* domain list lock */
74MTX_SYSINIT(domain, &dom_mtx, "domain list", MTX_DEF);
75
76/*
77 * Add a new protocol domain to the list of supported domains
78 * Note: you cant unload it again because  a socket may be using it.
79 * XXX can't fail at this time.
80 */
81static void
82net_init_domain(struct domain *dp)
83{
84	struct protosw *pr;
85
86	if (dp->dom_init)
87		(*dp->dom_init)();
88	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++){
89		if (pr->pr_usrreqs == 0)
90			panic("domaininit: %ssw[%d] has no usrreqs!",
91			      dp->dom_name,
92			      (int)(pr - dp->dom_protosw));
93		if (pr->pr_init)
94			(*pr->pr_init)();
95	}
96	/*
97	 * update global information about maximums
98	 */
99	max_hdr = max_linkhdr + max_protohdr;
100	max_datalen = MHLEN - max_hdr;
101}
102
103/*
104 * Add a new protocol domain to the list of supported domains
105 * Note: you cant unload it again because  a socket may be using it.
106 * XXX can't fail at this time.
107 */
108void
109net_add_domain(void *data)
110{
111	struct domain *dp;
112
113	dp = (struct domain *)data;
114	mtx_lock(&dom_mtx);
115	dp->dom_next = domains;
116	domains = dp;
117	mtx_unlock(&dom_mtx);
118	net_init_domain(dp);
119}
120
121/* ARGSUSED*/
122static void
123domaininit(void *dummy)
124{
125	/*
126	 * Before we do any setup, make sure to initialize the
127	 * zone allocator we get struct sockets from.
128	 */
129
130	socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL,
131	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
132	uma_zone_set_max(socket_zone, maxsockets);
133
134	if (max_linkhdr < 16)		/* XXX */
135		max_linkhdr = 16;
136
137	if (debug_mpsafenet) {
138		callout_init(&pffast_callout, CALLOUT_MPSAFE);
139		callout_init(&pfslow_callout, CALLOUT_MPSAFE);
140	} else {
141		callout_init(&pffast_callout, 0);
142		callout_init(&pfslow_callout, 0);
143	}
144
145	callout_reset(&pffast_callout, 1, pffasttimo, NULL);
146	callout_reset(&pfslow_callout, 1, pfslowtimo, NULL);
147}
148
149
150struct protosw *
151pffindtype(family, type)
152	int family;
153	int type;
154{
155	register struct domain *dp;
156	register struct protosw *pr;
157
158	for (dp = domains; dp; dp = dp->dom_next)
159		if (dp->dom_family == family)
160			goto found;
161	return (0);
162found:
163	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
164		if (pr->pr_type && pr->pr_type == type)
165			return (pr);
166	return (0);
167}
168
169struct protosw *
170pffindproto(family, protocol, type)
171	int family;
172	int protocol;
173	int type;
174{
175	register struct domain *dp;
176	register struct protosw *pr;
177	struct protosw *maybe = 0;
178
179	if (family == 0)
180		return (0);
181	for (dp = domains; dp; dp = dp->dom_next)
182		if (dp->dom_family == family)
183			goto found;
184	return (0);
185found:
186	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
187		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
188			return (pr);
189
190		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
191		    pr->pr_protocol == 0 && maybe == (struct protosw *)0)
192			maybe = pr;
193	}
194	return (maybe);
195}
196
197void
198pfctlinput(cmd, sa)
199	int cmd;
200	struct sockaddr *sa;
201{
202	register struct domain *dp;
203	register struct protosw *pr;
204
205	for (dp = domains; dp; dp = dp->dom_next)
206		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
207			if (pr->pr_ctlinput)
208				(*pr->pr_ctlinput)(cmd, sa, (void *)0);
209}
210
211void
212pfctlinput2(cmd, sa, ctlparam)
213	int cmd;
214	struct sockaddr *sa;
215	void *ctlparam;
216{
217	struct domain *dp;
218	struct protosw *pr;
219
220	if (!sa)
221		return;
222	for (dp = domains; dp; dp = dp->dom_next) {
223		/*
224		 * the check must be made by xx_ctlinput() anyways, to
225		 * make sure we use data item pointed to by ctlparam in
226		 * correct way.  the following check is made just for safety.
227		 */
228		if (dp->dom_family != sa->sa_family)
229			continue;
230
231		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
232			if (pr->pr_ctlinput)
233				(*pr->pr_ctlinput)(cmd, sa, ctlparam);
234	}
235}
236
237static void
238pfslowtimo(arg)
239	void *arg;
240{
241	register struct domain *dp;
242	register struct protosw *pr;
243
244	NET_ASSERT_GIANT();
245
246	for (dp = domains; dp; dp = dp->dom_next)
247		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
248			if (pr->pr_slowtimo)
249				(*pr->pr_slowtimo)();
250	callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL);
251}
252
253static void
254pffasttimo(arg)
255	void *arg;
256{
257	register struct domain *dp;
258	register struct protosw *pr;
259
260	NET_ASSERT_GIANT();
261
262	for (dp = domains; dp; dp = dp->dom_next)
263		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
264			if (pr->pr_fasttimo)
265				(*pr->pr_fasttimo)();
266	callout_reset(&pffast_callout, hz/5, pffasttimo, NULL);
267}
268