xref: /freebsd/sys/kern/uipc_domain.c (revision eacee0ff7ec955b32e09515246bd97b6edcd2b0f)
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$
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 takes place on a per domain basis
51  * as a result of traversing a SYSINIT linker set.  Most likely,
52  * each domain would want to call DOMAIN_SET(9) itself, which
53  * would cause the domain to be added just after domaininit()
54  * is called during startup.
55  *
56  * See DOMAIN_SET(9) for details on its use.
57  */
58 
59 static void domaininit __P((void *));
60 SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL)
61 
62 static struct callout pffast_callout;
63 static struct callout pfslow_callout;
64 
65 static void	pffasttimo __P((void *));
66 static void	pfslowtimo __P((void *));
67 
68 struct domain *domains;
69 
70 /*
71  * Add a new protocol domain to the list of supported domains
72  * Note: you cant unload it again because  a socket may be using it.
73  * XXX can't fail at this time.
74  */
75 static void
76 net_init_domain(struct domain *dp)
77 {
78 	register struct protosw *pr;
79 	int	s;
80 
81 	s = splnet();
82 	if (dp->dom_init)
83 		(*dp->dom_init)();
84 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++){
85 		if (pr->pr_usrreqs == 0)
86 			panic("domaininit: %ssw[%d] has no usrreqs!",
87 			      dp->dom_name,
88 			      (int)(pr - dp->dom_protosw));
89 		if (pr->pr_init)
90 			(*pr->pr_init)();
91 	}
92 	/*
93 	 * update global informatio about maximums
94 	 */
95 	max_hdr = max_linkhdr + max_protohdr;
96 	max_datalen = MHLEN - max_hdr;
97 	splx(s);
98 }
99 
100 /*
101  * Add a new protocol domain to the list of supported domains
102  * Note: you cant unload it again because  a socket may be using it.
103  * XXX can't fail at this time.
104  */
105 void
106 net_add_domain(void *data)
107 {
108 	int	s;
109 	struct domain *dp;
110 
111 	dp = (struct domain *)data;
112 	s = splnet();
113 	dp->dom_next = domains;
114 	domains = dp;
115 	splx(s);
116 	net_init_domain(dp);
117 }
118 
119 /* ARGSUSED*/
120 static void
121 domaininit(void *dummy)
122 {
123 	/*
124 	 * Before we do any setup, make sure to initialize the
125 	 * zone allocator we get struct sockets from.  The obvious
126 	 * maximum number of sockets is `maxfiles', but it is possible
127 	 * to have a socket without an open file (e.g., a connection waiting
128 	 * to be accept(2)ed).  Rather than think up and define a
129 	 * better value, we just use nmbclusters, since that's what people
130 	 * are told to increase first when the network runs out of memory.
131 	 * Perhaps we should have two pools, one of unlimited size
132 	 * for use during socreate(), and one ZONE_INTERRUPT pool for
133 	 * use in sonewconn().
134 	 */
135 	socket_zone = zinit("socket", sizeof(struct socket), maxsockets,
136 			    ZONE_INTERRUPT, 0);
137 
138 	if (max_linkhdr < 16)		/* XXX */
139 		max_linkhdr = 16;
140 
141 	callout_init(&pffast_callout, 0);
142 	callout_init(&pfslow_callout, 0);
143 
144 	callout_reset(&pffast_callout, 1, pffasttimo, NULL);
145 	callout_reset(&pfslow_callout, 1, pfslowtimo, NULL);
146 }
147 
148 
149 struct protosw *
150 pffindtype(family, type)
151 	int family;
152 	int type;
153 {
154 	register struct domain *dp;
155 	register struct protosw *pr;
156 
157 	for (dp = domains; dp; dp = dp->dom_next)
158 		if (dp->dom_family == family)
159 			goto found;
160 	return (0);
161 found:
162 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
163 		if (pr->pr_type && pr->pr_type == type)
164 			return (pr);
165 	return (0);
166 }
167 
168 struct protosw *
169 pffindproto(family, protocol, type)
170 	int family;
171 	int protocol;
172 	int type;
173 {
174 	register struct domain *dp;
175 	register struct protosw *pr;
176 	struct protosw *maybe = 0;
177 
178 	if (family == 0)
179 		return (0);
180 	for (dp = domains; dp; dp = dp->dom_next)
181 		if (dp->dom_family == family)
182 			goto found;
183 	return (0);
184 found:
185 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
186 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
187 			return (pr);
188 
189 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
190 		    pr->pr_protocol == 0 && maybe == (struct protosw *)0)
191 			maybe = pr;
192 	}
193 	return (maybe);
194 }
195 
196 void
197 pfctlinput(cmd, sa)
198 	int cmd;
199 	struct sockaddr *sa;
200 {
201 	register struct domain *dp;
202 	register struct protosw *pr;
203 
204 	for (dp = domains; dp; dp = dp->dom_next)
205 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
206 			if (pr->pr_ctlinput)
207 				(*pr->pr_ctlinput)(cmd, sa, (void *)0);
208 }
209 
210 void
211 pfctlinput2(cmd, sa, ctlparam)
212 	int cmd;
213 	struct sockaddr *sa;
214 	void *ctlparam;
215 {
216 	struct domain *dp;
217 	struct protosw *pr;
218 
219 	if (!sa)
220 		return;
221 	for (dp = domains; dp; dp = dp->dom_next) {
222 		/*
223 		 * the check must be made by xx_ctlinput() anyways, to
224 		 * make sure we use data item pointed to by ctlparam in
225 		 * correct way.  the following check is made just for safety.
226 		 */
227 		if (dp->dom_family != sa->sa_family)
228 			continue;
229 
230 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
231 			if (pr->pr_ctlinput)
232 				(*pr->pr_ctlinput)(cmd, sa, ctlparam);
233 	}
234 }
235 
236 static void
237 pfslowtimo(arg)
238 	void *arg;
239 {
240 	register struct domain *dp;
241 	register struct protosw *pr;
242 
243 	for (dp = domains; dp; dp = dp->dom_next)
244 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
245 			if (pr->pr_slowtimo)
246 				(*pr->pr_slowtimo)();
247 	callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL);
248 }
249 
250 static void
251 pffasttimo(arg)
252 	void *arg;
253 {
254 	register struct domain *dp;
255 	register struct protosw *pr;
256 
257 	for (dp = domains; dp; dp = dp->dom_next)
258 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
259 			if (pr->pr_fasttimo)
260 				(*pr->pr_fasttimo)();
261 	callout_reset(&pffast_callout, hz/5, pffasttimo, NULL);
262 }
263