xref: /freebsd/sys/kern/uipc_domain.c (revision 380a989b3223d455375b4fae70fd0b9bdd43bafb)
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  *	$Id: uipc_domain.c,v 1.18 1997/09/16 11:43:36 bde Exp $
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_DOMAI, 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 
61 static int	x_save_spl;			/* used by kludge*/
62 static void kludge_splimp __P((void *));
63 static void kludge_splx __P((void *));
64 static void domaininit __P((void *));
65 SYSINIT(splimp, SI_SUB_PROTO_BEGIN, SI_ORDER_FIRST, kludge_splimp, &x_save_spl)
66 SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL)
67 SYSINIT(splx, SI_SUB_PROTO_END, SI_ORDER_FIRST, kludge_splx, &x_save_spl)
68 
69 static void	pffasttimo __P((void *));
70 static void	pfslowtimo __P((void *));
71 
72 struct domain *domains;
73 
74 #define	ADDDOMAIN(x)	{ \
75 	__CONCAT(x,domain.dom_next) = domains; \
76 	domains = &__CONCAT(x,domain); \
77 }
78 
79 extern struct linker_set domain_set;
80 
81 /* ARGSUSED*/
82 static void
83 domaininit(dummy)
84 	void *dummy;
85 {
86 	register struct domain *dp, **dpp;
87 	register struct protosw *pr;
88 
89 	/*
90 	 * Before we do any setup, make sure to initialize the
91 	 * zone allocator we get struct sockets from.  The obvious
92 	 * maximum number of sockets is `maxfiles', but it is possible
93 	 * to have a socket without an open file (e.g., a connection waiting
94 	 * to be accept(2)ed).  Rather than think up and define a
95 	 * better value, we just use nmbclusters, since that's what people
96 	 * are told to increase first when the network runs out of memory.
97 	 * Perhaps we should have two pools, one of unlimited size
98 	 * for use during socreate(), and one ZONE_INTERRUPT pool for
99 	 * use in sonewconn().
100 	 */
101 	socket_zone = zinit("socket", sizeof(struct socket), maxsockets,
102 			    ZONE_INTERRUPT, 0);
103 
104 	/*
105 	 * NB - local domain is always present.
106 	 */
107 	ADDDOMAIN(local);
108 
109 	for (dpp = (struct domain **)domain_set.ls_items; *dpp; dpp++) {
110 		(**dpp).dom_next = domains;
111 		domains = *dpp;
112 	}
113 
114 	for (dp = domains; dp; dp = dp->dom_next) {
115 		if (dp->dom_init)
116 			(*dp->dom_init)();
117 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++){
118 			if (pr->pr_usrreqs == 0)
119 				panic("domaininit: %ssw[%d] has no usrreqs!",
120 				      dp->dom_name,
121 				      (int)(pr - dp->dom_protosw));
122 			if (pr->pr_init)
123 				(*pr->pr_init)();
124 		}
125 	}
126 
127 	if (max_linkhdr < 16)		/* XXX */
128 		max_linkhdr = 16;
129 	max_hdr = max_linkhdr + max_protohdr;
130 	max_datalen = MHLEN - max_hdr;
131 	timeout(pffasttimo, (void *)0, 1);
132 	timeout(pfslowtimo, (void *)0, 1);
133 }
134 
135 
136 /*
137  * The following two operations are kludge code.  Most likely, they should
138  * be done as a "domainpreinit()" for the first function and then rolled
139  * in as the last act of "domaininit()" for the second.
140  *
141  * In point of fact, it is questionable why other initialization prior
142  * to this does not also take place at splimp by default.
143  */
144 static void
145 kludge_splimp(udata)
146 	void *udata;
147 {
148 	int	*savesplp = udata;
149 
150 	*savesplp = splimp();
151 }
152 
153 static void
154 kludge_splx(udata)
155 	void *udata;
156 {
157 	int	*savesplp = udata;
158 
159 	splx(*savesplp);
160 }
161 
162 
163 
164 struct protosw *
165 pffindtype(family, type)
166 	int family;
167 	int type;
168 {
169 	register struct domain *dp;
170 	register struct protosw *pr;
171 
172 	for (dp = domains; dp; dp = dp->dom_next)
173 		if (dp->dom_family == family)
174 			goto found;
175 	return (0);
176 found:
177 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
178 		if (pr->pr_type && pr->pr_type == type)
179 			return (pr);
180 	return (0);
181 }
182 
183 struct protosw *
184 pffindproto(family, protocol, type)
185 	int family;
186 	int protocol;
187 	int type;
188 {
189 	register struct domain *dp;
190 	register struct protosw *pr;
191 	struct protosw *maybe = 0;
192 
193 	if (family == 0)
194 		return (0);
195 	for (dp = domains; dp; dp = dp->dom_next)
196 		if (dp->dom_family == family)
197 			goto found;
198 	return (0);
199 found:
200 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
201 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
202 			return (pr);
203 
204 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
205 		    pr->pr_protocol == 0 && maybe == (struct protosw *)0)
206 			maybe = pr;
207 	}
208 	return (maybe);
209 }
210 
211 void
212 pfctlinput(cmd, sa)
213 	int cmd;
214 	struct sockaddr *sa;
215 {
216 	register struct domain *dp;
217 	register struct protosw *pr;
218 
219 	for (dp = domains; dp; dp = dp->dom_next)
220 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
221 			if (pr->pr_ctlinput)
222 				(*pr->pr_ctlinput)(cmd, sa, (void *)0);
223 }
224 
225 static void
226 pfslowtimo(arg)
227 	void *arg;
228 {
229 	register struct domain *dp;
230 	register struct protosw *pr;
231 
232 	for (dp = domains; dp; dp = dp->dom_next)
233 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
234 			if (pr->pr_slowtimo)
235 				(*pr->pr_slowtimo)();
236 	timeout(pfslowtimo, (void *)0, hz/2);
237 }
238 
239 static void
240 pffasttimo(arg)
241 	void *arg;
242 {
243 	register struct domain *dp;
244 	register struct protosw *pr;
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_fasttimo)
249 				(*pr->pr_fasttimo)();
250 	timeout(pffasttimo, (void *)0, hz/5);
251 }
252