xref: /freebsd/sys/kern/uipc_domain.c (revision 2546665afcaf0d53dc2c7058fee96354b3680f5a)
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  * 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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  *	@(#)uipc_domain.c	8.2 (Berkeley) 10/18/93
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/protosw.h>
38 #include <sys/domain.h>
39 #include <sys/mbuf.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/socketvar.h>
44 #include <sys/systm.h>
45 #include <vm/uma.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(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(void *);
66 static void	pfslowtimo(void *);
67 
68 struct domain *domains;		/* registered protocol domains */
69 struct mtx dom_mtx;		/* domain list lock */
70 MTX_SYSINIT(domain, &dom_mtx, "domain list", MTX_DEF);
71 
72 /*
73  * Add a new protocol domain to the list of supported domains
74  * Note: you cant unload it again because  a socket may be using it.
75  * XXX can't fail at this time.
76  */
77 static void
78 net_init_domain(struct domain *dp)
79 {
80 	struct protosw *pr;
81 
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 information about maximums
94 	 */
95 	max_hdr = max_linkhdr + max_protohdr;
96 	max_datalen = MHLEN - max_hdr;
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  */
104 void
105 net_add_domain(void *data)
106 {
107 	struct domain *dp;
108 
109 	dp = (struct domain *)data;
110 	mtx_lock(&dom_mtx);
111 	dp->dom_next = domains;
112 	domains = dp;
113 	mtx_unlock(&dom_mtx);
114 	net_init_domain(dp);
115 }
116 
117 /* ARGSUSED*/
118 static void
119 domaininit(void *dummy)
120 {
121 	/*
122 	 * Before we do any setup, make sure to initialize the
123 	 * zone allocator we get struct sockets from.
124 	 */
125 
126 	socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL,
127 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
128 	uma_zone_set_max(socket_zone, maxsockets);
129 
130 	if (max_linkhdr < 16)		/* XXX */
131 		max_linkhdr = 16;
132 
133 	if (debug_mpsafenet) {
134 		callout_init(&pffast_callout, CALLOUT_MPSAFE);
135 		callout_init(&pfslow_callout, CALLOUT_MPSAFE);
136 	} else {
137 		callout_init(&pffast_callout, 0);
138 		callout_init(&pfslow_callout, 0);
139 	}
140 
141 	callout_reset(&pffast_callout, 1, pffasttimo, NULL);
142 	callout_reset(&pfslow_callout, 1, pfslowtimo, NULL);
143 }
144 
145 
146 struct protosw *
147 pffindtype(family, type)
148 	int family;
149 	int type;
150 {
151 	register struct domain *dp;
152 	register struct protosw *pr;
153 
154 	for (dp = domains; dp; dp = dp->dom_next)
155 		if (dp->dom_family == family)
156 			goto found;
157 	return (0);
158 found:
159 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
160 		if (pr->pr_type && pr->pr_type == type)
161 			return (pr);
162 	return (0);
163 }
164 
165 struct protosw *
166 pffindproto(family, protocol, type)
167 	int family;
168 	int protocol;
169 	int type;
170 {
171 	register struct domain *dp;
172 	register struct protosw *pr;
173 	struct protosw *maybe = 0;
174 
175 	if (family == 0)
176 		return (0);
177 	for (dp = domains; dp; dp = dp->dom_next)
178 		if (dp->dom_family == family)
179 			goto found;
180 	return (0);
181 found:
182 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
183 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
184 			return (pr);
185 
186 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
187 		    pr->pr_protocol == 0 && maybe == (struct protosw *)0)
188 			maybe = pr;
189 	}
190 	return (maybe);
191 }
192 
193 void
194 pfctlinput(cmd, sa)
195 	int cmd;
196 	struct sockaddr *sa;
197 {
198 	register struct domain *dp;
199 	register struct protosw *pr;
200 
201 	for (dp = domains; dp; dp = dp->dom_next)
202 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
203 			if (pr->pr_ctlinput)
204 				(*pr->pr_ctlinput)(cmd, sa, (void *)0);
205 }
206 
207 void
208 pfctlinput2(cmd, sa, ctlparam)
209 	int cmd;
210 	struct sockaddr *sa;
211 	void *ctlparam;
212 {
213 	struct domain *dp;
214 	struct protosw *pr;
215 
216 	if (!sa)
217 		return;
218 	for (dp = domains; dp; dp = dp->dom_next) {
219 		/*
220 		 * the check must be made by xx_ctlinput() anyways, to
221 		 * make sure we use data item pointed to by ctlparam in
222 		 * correct way.  the following check is made just for safety.
223 		 */
224 		if (dp->dom_family != sa->sa_family)
225 			continue;
226 
227 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
228 			if (pr->pr_ctlinput)
229 				(*pr->pr_ctlinput)(cmd, sa, ctlparam);
230 	}
231 }
232 
233 static void
234 pfslowtimo(arg)
235 	void *arg;
236 {
237 	register struct domain *dp;
238 	register struct protosw *pr;
239 
240 	NET_ASSERT_GIANT();
241 
242 	for (dp = domains; dp; dp = dp->dom_next)
243 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
244 			if (pr->pr_slowtimo)
245 				(*pr->pr_slowtimo)();
246 	callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL);
247 }
248 
249 static void
250 pffasttimo(arg)
251 	void *arg;
252 {
253 	register struct domain *dp;
254 	register struct protosw *pr;
255 
256 	NET_ASSERT_GIANT();
257 
258 	for (dp = domains; dp; dp = dp->dom_next)
259 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
260 			if (pr->pr_fasttimo)
261 				(*pr->pr_fasttimo)();
262 	callout_reset(&pffast_callout, hz/5, pffasttimo, NULL);
263 }
264