xref: /freebsd/sys/kern/uipc_domain.c (revision ef5d438ed4bc17ad7ece3e40fe4d1f9baf3aadf7)
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.11 1995/12/02 17:10:38 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/systm.h>
44 
45 /*
46  * System initialization
47  *
48  * Note: domain initialization wants to take place on a per domain basis
49  * as a result of traversing a linker set.  Most likely, each domain
50  * want to call a registration function rather than being handled here
51  * in domaininit().  Probably this will look like:
52  *
53  * SYSINIT(unique, SI_SUB_PROTO_DOMAI, SI_ORDER_ANY, domain_add, xxx)
54  *
55  * Where 'xxx' is replaced by the address of a parameter struct to be
56  * passed to the doamin_add() function.
57  */
58 
59 static int	x_save_spl;			/* used by kludge*/
60 static void kludge_splimp __P((void *));
61 static void kludge_splx __P((void *));
62 static void domaininit __P((void *));
63 SYSINIT(splimp, SI_SUB_PROTO_BEGIN, SI_ORDER_FIRST, kludge_splimp, &x_save_spl)
64 SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL)
65 SYSINIT(splx, SI_SUB_PROTO_END, SI_ORDER_FIRST, kludge_splx, &x_save_spl)
66 
67 static void	pffasttimo __P((void *));
68 static void	pfslowtimo __P((void *));
69 
70 struct domain *domains;
71 
72 #define	ADDDOMAIN(x)	{ \
73 	__CONCAT(x,domain.dom_next) = domains; \
74 	domains = &__CONCAT(x,domain); \
75 }
76 
77 extern struct linker_set domain_set;
78 
79 /* ARGSUSED*/
80 static void
81 domaininit(dummy)
82 	void *dummy;
83 {
84 	register struct domain *dp, **dpp;
85 	register struct protosw *pr;
86 
87 	/*
88 	 * NB - local domain is always present.
89 	 */
90 	ADDDOMAIN(local);
91 
92 	for (dpp = (struct domain **)domain_set.ls_items; *dpp; dpp++) {
93 		(**dpp).dom_next = domains;
94 		domains = *dpp;
95 	}
96 
97 /* - not in our sources
98 #ifdef ISDN
99 	ADDDOMAIN(isdn);
100 #endif
101 */
102 
103 	for (dp = domains; dp; dp = dp->dom_next) {
104 		if (dp->dom_init)
105 			(*dp->dom_init)();
106 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
107 			if (pr->pr_init)
108 				(*pr->pr_init)();
109 	}
110 
111 	if (max_linkhdr < 16)		/* XXX */
112 		max_linkhdr = 16;
113 	max_hdr = max_linkhdr + max_protohdr;
114 	max_datalen = MHLEN - max_hdr;
115 	timeout(pffasttimo, (void *)0, 1);
116 	timeout(pfslowtimo, (void *)0, 1);
117 }
118 
119 
120 /*
121  * The following two operations are kludge code.  Most likely, they should
122  * be done as a "domainpreinit()" for the first function and then rolled
123  * in as the last act of "domaininit()" for the second.
124  *
125  * In point of fact, it is questionable why other initialization prior
126  * to this does not also take place at splimp by default.
127  */
128 static void
129 kludge_splimp(udata)
130 	void *udata;
131 {
132 	int	*savesplp = udata;
133 
134 	*savesplp = splimp();
135 }
136 
137 static void
138 kludge_splx(udata)
139 	void *udata;
140 {
141 	int	*savesplp = udata;
142 
143 	splx( *savesplp);
144 }
145 
146 
147 
148 struct protosw *
149 pffindtype(int family, 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(int family, int protocol, int type)
167 {
168 	register struct domain *dp;
169 	register struct protosw *pr;
170 	struct protosw *maybe = 0;
171 
172 	if (family == 0)
173 		return (0);
174 	for (dp = domains; dp; dp = dp->dom_next)
175 		if (dp->dom_family == family)
176 			goto found;
177 	return (0);
178 found:
179 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
180 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
181 			return (pr);
182 
183 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
184 		    pr->pr_protocol == 0 && maybe == (struct protosw *)0)
185 			maybe = pr;
186 	}
187 	return (maybe);
188 }
189 
190 void
191 pfctlinput(cmd, sa)
192 	int cmd;
193 	struct sockaddr *sa;
194 {
195 	register struct domain *dp;
196 	register struct protosw *pr;
197 
198 	for (dp = domains; dp; dp = dp->dom_next)
199 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
200 			if (pr->pr_ctlinput)
201 				(*pr->pr_ctlinput)(cmd, sa, (void *)0);
202 }
203 
204 static void
205 pfslowtimo(arg)
206 	void *arg;
207 {
208 	register struct domain *dp;
209 	register struct protosw *pr;
210 
211 	for (dp = domains; dp; dp = dp->dom_next)
212 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
213 			if (pr->pr_slowtimo)
214 				(*pr->pr_slowtimo)();
215 	timeout(pfslowtimo, (void *)0, hz/2);
216 }
217 
218 static void
219 pffasttimo(arg)
220 	void *arg;
221 {
222 	register struct domain *dp;
223 	register struct protosw *pr;
224 
225 	for (dp = domains; dp; dp = dp->dom_next)
226 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
227 			if (pr->pr_fasttimo)
228 				(*pr->pr_fasttimo)();
229 	timeout(pffasttimo, (void *)0, hz/5);
230 }
231