xref: /netbsd-src/sys/kern/uipc_domain.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: uipc_domain.c,v 1.32 2000/06/27 17:41:43 mrg Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)uipc_domain.c	8.3 (Berkeley) 2/14/95
36  */
37 
38 #include "opt_inet.h"
39 #include "opt_ipsec.h"
40 #include "opt_atalk.h"
41 #include "opt_ccitt.h"
42 #include "opt_iso.h"
43 #include "opt_ns.h"
44 #include "opt_natm.h"
45 #include "arp.h"
46 
47 #include <sys/param.h>
48 #include <sys/socket.h>
49 #include <sys/protosw.h>
50 #include <sys/domain.h>
51 #include <sys/mbuf.h>
52 #include <sys/time.h>
53 #include <sys/kernel.h>
54 #include <sys/systm.h>
55 #include <sys/callout.h>
56 #include <sys/proc.h>
57 #include <uvm/uvm_extern.h>
58 #include <sys/sysctl.h>
59 
60 void	pffasttimo __P((void *));
61 void	pfslowtimo __P((void *));
62 
63 struct callout pffasttimo_ch, pfslowtimo_ch;
64 
65 /*
66  * Current time values for fast and slow timeouts.  We can use u_int
67  * relatively safely.  The fast timer will roll over in 27 years and
68  * the slow timer in 68 years.
69  */
70 u_int	pfslowtimo_now;
71 u_int	pffasttimo_now;
72 
73 #define	ADDDOMAIN(x)	{ \
74 	extern struct domain __CONCAT(x,domain); \
75 	__CONCAT(x,domain.dom_next) = domains; \
76 	domains = &__CONCAT(x,domain); \
77 }
78 
79 void
80 domaininit()
81 {
82 	struct domain *dp;
83 	struct protosw *pr;
84 
85 #undef unix
86 	/*
87 	 * KAME NOTE: ADDDOMAIN(route) is moved to the last part so that
88 	 * it will be initialized as the *first* element.  confusing!
89 	 */
90 #ifndef lint
91 	ADDDOMAIN(unix);
92 #ifdef INET
93 	ADDDOMAIN(inet);
94 #endif
95 #ifdef INET6
96 	ADDDOMAIN(inet6);
97 #endif
98 #ifdef NS
99 	ADDDOMAIN(ns);
100 #endif
101 #ifdef ISO
102 	ADDDOMAIN(iso);
103 #endif
104 #ifdef CCITT
105 	ADDDOMAIN(ccitt);
106 #endif
107 #ifdef NATM
108 	ADDDOMAIN(natm);
109 #endif
110 #ifdef NETATALK
111 	ADDDOMAIN(atalk);
112 #endif
113 #ifdef IPSEC
114 	ADDDOMAIN(key);
115 #endif
116 #if NARP > 0
117 	ADDDOMAIN(arp);
118 #endif
119 	ADDDOMAIN(route);
120 #endif /* ! lint */
121 
122 	for (dp = domains; dp; dp = dp->dom_next) {
123 		if (dp->dom_init)
124 			(*dp->dom_init)();
125 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
126 			if (pr->pr_init)
127 				(*pr->pr_init)();
128 	}
129 
130 	if (max_linkhdr < 16)		/* XXX */
131 		max_linkhdr = 16;
132 	max_hdr = max_linkhdr + max_protohdr;
133 	max_datalen = MHLEN - max_hdr;
134 
135 	callout_init(&pffasttimo_ch);
136 	callout_init(&pfslowtimo_ch);
137 
138 	callout_reset(&pffasttimo_ch, 1, pffasttimo, NULL);
139 	callout_reset(&pfslowtimo_ch, 1, pfslowtimo, NULL);
140 }
141 
142 struct domain *
143 pffinddomain(family)
144 	int family;
145 {
146 	struct domain *dp;
147 
148 	for (dp = domains; dp != NULL; dp = dp->dom_next)
149 		if (dp->dom_family == family)
150 			return (dp);
151 	return (NULL);
152 }
153 
154 struct protosw *
155 pffindtype(family, type)
156 	int family, type;
157 {
158 	struct domain *dp;
159 	struct protosw *pr;
160 
161 	dp = pffinddomain(family);
162 	if (dp == NULL)
163 		return (NULL);
164 
165 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
166 		if (pr->pr_type && pr->pr_type == type)
167 			return (pr);
168 
169 	return (NULL);
170 }
171 
172 struct protosw *
173 pffindproto(family, protocol, type)
174 	int family, protocol, type;
175 {
176 	struct domain *dp;
177 	struct protosw *pr;
178 	struct protosw *maybe = NULL;
179 
180 	if (family == 0)
181 		return (NULL);
182 
183 	dp = pffinddomain(family);
184 	if (dp == NULL)
185 		return (NULL);
186 
187 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
188 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
189 			return (pr);
190 
191 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
192 		    pr->pr_protocol == 0 && maybe == NULL)
193 			maybe = pr;
194 	}
195 	return (maybe);
196 }
197 
198 int
199 net_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
200 	int *name;
201 	u_int namelen;
202 	void *oldp;
203 	size_t *oldlenp;
204 	void *newp;
205 	size_t newlen;
206 	struct proc *p;
207 {
208 	struct domain *dp;
209 	struct protosw *pr;
210 	int family, protocol;
211 
212 	/*
213 	 * All sysctl names at this level are nonterminal.
214 	 * PF_KEY: next component is protocol family, and then at least one
215 	 *	additional component.
216 	 * usually: next two components are protocol family and protocol
217 	 *	number, then at least one addition component.
218 	 */
219 	if (namelen < 2)
220 		return (EISDIR);		/* overloaded */
221 	family = name[0];
222 
223 	if (family == 0)
224 		return (0);
225 
226 	dp = pffinddomain(family);
227 	if (dp == NULL)
228 		return (ENOPROTOOPT);
229 
230 	switch (family) {
231 #ifdef IPSEC
232 	case PF_KEY:
233 		pr = dp->dom_protosw;
234 		if (pr->pr_sysctl)
235 			return ((*pr->pr_sysctl)(name + 1, namelen - 1,
236 				oldp, oldlenp, newp, newlen));
237 		return (ENOPROTOOPT);
238 #endif
239 	default:
240 		break;
241 	}
242 	if (namelen < 3)
243 		return (EISDIR);		/* overloaded */
244 	protocol = name[1];
245 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
246 		if (pr->pr_protocol == protocol && pr->pr_sysctl)
247 			return ((*pr->pr_sysctl)(name + 2, namelen - 2,
248 			    oldp, oldlenp, newp, newlen));
249 	return (ENOPROTOOPT);
250 }
251 
252 void
253 pfctlinput(cmd, sa)
254 	int cmd;
255 	struct sockaddr *sa;
256 {
257 	struct domain *dp;
258 	struct protosw *pr;
259 
260 	for (dp = domains; dp; dp = dp->dom_next)
261 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
262 			if (pr->pr_ctlinput)
263 				(*pr->pr_ctlinput)(cmd, sa, NULL);
264 }
265 
266 void
267 pfslowtimo(arg)
268 	void *arg;
269 {
270 	struct domain *dp;
271 	struct protosw *pr;
272 
273 	pfslowtimo_now++;
274 
275 	for (dp = domains; dp; dp = dp->dom_next)
276 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
277 			if (pr->pr_slowtimo)
278 				(*pr->pr_slowtimo)();
279 	callout_reset(&pfslowtimo_ch, hz / 2, pfslowtimo, NULL);
280 }
281 
282 void
283 pffasttimo(arg)
284 	void *arg;
285 {
286 	struct domain *dp;
287 	struct protosw *pr;
288 
289 	pffasttimo_now++;
290 
291 	for (dp = domains; dp; dp = dp->dom_next)
292 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
293 			if (pr->pr_fasttimo)
294 				(*pr->pr_fasttimo)();
295 	callout_reset(&pffasttimo_ch, hz / 5, pffasttimo, NULL);
296 }
297