xref: /freebsd/sys/kern/uipc_domain.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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$");
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 
63 static void domaininit(void *);
64 SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL)
65 
66 static struct callout pffast_callout;
67 static struct callout pfslow_callout;
68 
69 static void	pffasttimo(void *);
70 static void	pfslowtimo(void *);
71 
72 struct domain *domains;		/* registered protocol domains */
73 struct mtx dom_mtx;		/* domain list lock */
74 MTX_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  */
81 static void
82 net_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  */
108 void
109 net_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*/
122 static void
123 domaininit(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 	callout_init(&pffast_callout, 0);
138 	callout_init(&pfslow_callout, 0);
139 
140 	callout_reset(&pffast_callout, 1, pffasttimo, NULL);
141 	callout_reset(&pfslow_callout, 1, pfslowtimo, NULL);
142 }
143 
144 
145 struct protosw *
146 pffindtype(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);
157 found:
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 
164 struct protosw *
165 pffindproto(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);
180 found:
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 
192 void
193 pfctlinput(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 
206 void
207 pfctlinput2(cmd, sa, ctlparam)
208 	int cmd;
209 	struct sockaddr *sa;
210 	void *ctlparam;
211 {
212 	struct domain *dp;
213 	struct protosw *pr;
214 
215 	if (!sa)
216 		return;
217 	for (dp = domains; dp; dp = dp->dom_next) {
218 		/*
219 		 * the check must be made by xx_ctlinput() anyways, to
220 		 * make sure we use data item pointed to by ctlparam in
221 		 * correct way.  the following check is made just for safety.
222 		 */
223 		if (dp->dom_family != sa->sa_family)
224 			continue;
225 
226 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
227 			if (pr->pr_ctlinput)
228 				(*pr->pr_ctlinput)(cmd, sa, ctlparam);
229 	}
230 }
231 
232 static void
233 pfslowtimo(arg)
234 	void *arg;
235 {
236 	register struct domain *dp;
237 	register struct protosw *pr;
238 
239 	for (dp = domains; dp; dp = dp->dom_next)
240 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
241 			if (pr->pr_slowtimo)
242 				(*pr->pr_slowtimo)();
243 	callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL);
244 }
245 
246 static void
247 pffasttimo(arg)
248 	void *arg;
249 {
250 	register struct domain *dp;
251 	register struct protosw *pr;
252 
253 	for (dp = domains; dp; dp = dp->dom_next)
254 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
255 			if (pr->pr_fasttimo)
256 				(*pr->pr_fasttimo)();
257 	callout_reset(&pffast_callout, hz/5, pffasttimo, NULL);
258 }
259