uipc_domain.c revision 50477
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 * $FreeBSD: head/sys/kern/uipc_domain.c 50477 1999-08-28 01:08:13Z peter $
35 */
36
37#include <sys/param.h>
38#include <sys/socket.h>
39#include <sys/protosw.h>
40#include <sys/domain.h>
41#include <sys/mbuf.h>
42#include <sys/kernel.h>
43#include <sys/socketvar.h>
44#include <sys/systm.h>
45#include <vm/vm_zone.h>
46
47/*
48 * System initialization
49 *
50 * Note: domain initialization wants to take place on a per domain basis
51 * as a result of traversing a linker set.  Most likely, each domain
52 * want to call a registration function rather than being handled here
53 * in domaininit().  Probably this will look like:
54 *
55 * SYSINIT(unique, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, domain_add, xxx)
56 *
57 * Where 'xxx' is replaced by the address of a parameter struct to be
58 * passed to the doamin_add() function.
59 */
60
61static void domaininit __P((void *));
62SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL)
63
64static void	pffasttimo __P((void *));
65static void	pfslowtimo __P((void *));
66
67struct domain *domains;
68
69/*
70 * Add a new protocol domain to the list of supported domains
71 * Note: you cant unload it again because  a socket may be using it.
72 * XXX can't fail at this time.
73 */
74static void
75net_init_domain(struct domain *dp)
76{
77	register struct protosw *pr;
78	int	s;
79
80	s = splnet();
81	if (dp->dom_init)
82		(*dp->dom_init)();
83	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++){
84		if (pr->pr_usrreqs == 0)
85			panic("domaininit: %ssw[%d] has no usrreqs!",
86			      dp->dom_name,
87			      (int)(pr - dp->dom_protosw));
88		if (pr->pr_init)
89			(*pr->pr_init)();
90	}
91	/*
92	 * update global informatio about maximums
93	 */
94	max_hdr = max_linkhdr + max_protohdr;
95	max_datalen = MHLEN - max_hdr;
96	splx(s);
97}
98
99/*
100 * Add a new protocol domain to the list of supported domains
101 * Note: you cant unload it again because  a socket may be using it.
102 * XXX can't fail at this time.
103 */
104void
105net_add_domain(void *data)
106{
107	int	s;
108	struct domain *dp;
109
110	dp = (struct domain *)data;
111	s = splnet();
112	dp->dom_next = domains;
113	domains = dp;
114	splx(s);
115	net_init_domain(dp);
116}
117
118/* ARGSUSED*/
119static void
120domaininit(void *dummy)
121{
122	/*
123	 * Before we do any setup, make sure to initialize the
124	 * zone allocator we get struct sockets from.  The obvious
125	 * maximum number of sockets is `maxfiles', but it is possible
126	 * to have a socket without an open file (e.g., a connection waiting
127	 * to be accept(2)ed).  Rather than think up and define a
128	 * better value, we just use nmbclusters, since that's what people
129	 * are told to increase first when the network runs out of memory.
130	 * Perhaps we should have two pools, one of unlimited size
131	 * for use during socreate(), and one ZONE_INTERRUPT pool for
132	 * use in sonewconn().
133	 */
134	socket_zone = zinit("socket", sizeof(struct socket), maxsockets,
135			    ZONE_INTERRUPT, 0);
136
137	if (max_linkhdr < 16)		/* XXX */
138		max_linkhdr = 16;
139
140	timeout(pffasttimo, (void *)0, 1);
141	timeout(pfslowtimo, (void *)0, 1);
142}
143
144
145struct protosw *
146pffindtype(family, type)
147	int family;
148	int type;
149{
150	register struct domain *dp;
151	register struct protosw *pr;
152
153	for (dp = domains; dp; dp = dp->dom_next)
154		if (dp->dom_family == family)
155			goto found;
156	return (0);
157found:
158	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
159		if (pr->pr_type && pr->pr_type == type)
160			return (pr);
161	return (0);
162}
163
164struct protosw *
165pffindproto(family, protocol, type)
166	int family;
167	int protocol;
168	int type;
169{
170	register struct domain *dp;
171	register struct protosw *pr;
172	struct protosw *maybe = 0;
173
174	if (family == 0)
175		return (0);
176	for (dp = domains; dp; dp = dp->dom_next)
177		if (dp->dom_family == family)
178			goto found;
179	return (0);
180found:
181	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
182		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
183			return (pr);
184
185		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
186		    pr->pr_protocol == 0 && maybe == (struct protosw *)0)
187			maybe = pr;
188	}
189	return (maybe);
190}
191
192void
193pfctlinput(cmd, sa)
194	int cmd;
195	struct sockaddr *sa;
196{
197	register struct domain *dp;
198	register struct protosw *pr;
199
200	for (dp = domains; dp; dp = dp->dom_next)
201		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
202			if (pr->pr_ctlinput)
203				(*pr->pr_ctlinput)(cmd, sa, (void *)0);
204}
205
206static void
207pfslowtimo(arg)
208	void *arg;
209{
210	register struct domain *dp;
211	register struct protosw *pr;
212
213	for (dp = domains; dp; dp = dp->dom_next)
214		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
215			if (pr->pr_slowtimo)
216				(*pr->pr_slowtimo)();
217	timeout(pfslowtimo, (void *)0, hz/2);
218}
219
220static void
221pffasttimo(arg)
222	void *arg;
223{
224	register struct domain *dp;
225	register struct protosw *pr;
226
227	for (dp = domains; dp; dp = dp->dom_next)
228		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
229			if (pr->pr_fasttimo)
230				(*pr->pr_fasttimo)();
231	timeout(pffasttimo, (void *)0, hz/5);
232}
233