xref: /netbsd-src/sys/kern/uipc_domain.c (revision ae1bfcddc410612bc8c58b807e1830becb69a24c)
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  *	from: @(#)uipc_domain.c	8.2 (Berkeley) 10/18/93
34  *	$Id: uipc_domain.c,v 1.11 1994/05/13 06:01:30 mycroft 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/time.h>
43 #include <sys/kernel.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <vm/vm.h>
47 #include <sys/sysctl.h>
48 
49 void	pffasttimo __P((void *));
50 void	pfslowtimo __P((void *));
51 
52 #define	ADDDOMAIN(x)	{ \
53 	extern struct domain __CONCAT(x,domain); \
54 	__CONCAT(x,domain.dom_next) = domains; \
55 	domains = &__CONCAT(x,domain); \
56 }
57 
58 void
59 domaininit()
60 {
61 	register struct domain *dp;
62 	register struct protosw *pr;
63 
64 #undef unix
65 #ifndef lint
66 	ADDDOMAIN(unix);
67 	ADDDOMAIN(route);
68 #ifdef INET
69 	ADDDOMAIN(inet);
70 #endif
71 #ifdef NS
72 	ADDDOMAIN(ns);
73 #endif
74 #ifdef ISO
75 	ADDDOMAIN(iso);
76 #endif
77 #ifdef CCITT
78 	ADDDOMAIN(ccitt);
79 #endif
80 #ifdef notdef /* XXXX */
81 #include "imp.h"
82 #if NIMP > 0
83 	ADDDOMAIN(imp);
84 #endif
85 #endif
86 #endif
87 
88 	for (dp = domains; dp; dp = dp->dom_next) {
89 		if (dp->dom_init)
90 			(*dp->dom_init)();
91 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
92 			if (pr->pr_init)
93 				(*pr->pr_init)();
94 	}
95 
96 if (max_linkhdr < 16)		/* XXX */
97 max_linkhdr = 16;
98 	max_hdr = max_linkhdr + max_protohdr;
99 	max_datalen = MHLEN - max_hdr;
100 	timeout(pffasttimo, NULL, 1);
101 	timeout(pfslowtimo, NULL, 1);
102 }
103 
104 struct protosw *
105 pffindtype(family, type)
106 	int family, type;
107 {
108 	register struct domain *dp;
109 	register struct protosw *pr;
110 
111 	for (dp = domains; dp; dp = dp->dom_next)
112 		if (dp->dom_family == family)
113 			goto found;
114 	return (0);
115 found:
116 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
117 		if (pr->pr_type && pr->pr_type == type)
118 			return (pr);
119 	return (0);
120 }
121 
122 struct protosw *
123 pffindproto(family, protocol, type)
124 	int family, protocol, type;
125 {
126 	register struct domain *dp;
127 	register struct protosw *pr;
128 	struct protosw *maybe = 0;
129 
130 	if (family == 0)
131 		return (0);
132 	for (dp = domains; dp; dp = dp->dom_next)
133 		if (dp->dom_family == family)
134 			goto found;
135 	return (0);
136 found:
137 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
138 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
139 			return (pr);
140 
141 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
142 		    pr->pr_protocol == 0 && maybe == (struct protosw *)0)
143 			maybe = pr;
144 	}
145 	return (maybe);
146 }
147 
148 int
149 net_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
150 	int *name;
151 	u_int namelen;
152 	void *oldp;
153 	size_t *oldlenp;
154 	void *newp;
155 	size_t newlen;
156 	struct proc *p;
157 {
158 	register struct domain *dp;
159 	register struct protosw *pr;
160 	int family, protocol;
161 
162 	/*
163 	 * All sysctl names at this level are nonterminal;
164 	 * next two components are protocol family and protocol number,
165 	 * then at least one addition component.
166 	 */
167 	if (namelen < 3)
168 		return (EISDIR);		/* overloaded */
169 	family = name[0];
170 	protocol = name[1];
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 (ENOPROTOOPT);
178 found:
179 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
180 		if (pr->pr_protocol == protocol && pr->pr_sysctl)
181 			return ((*pr->pr_sysctl)(name + 2, namelen - 2,
182 			    oldp, oldlenp, newp, newlen));
183 	return (ENOPROTOOPT);
184 }
185 
186 void
187 pfctlinput(cmd, sa)
188 	int cmd;
189 	struct sockaddr *sa;
190 {
191 	register struct domain *dp;
192 	register struct protosw *pr;
193 
194 	for (dp = domains; dp; dp = dp->dom_next)
195 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
196 			if (pr->pr_ctlinput)
197 				(*pr->pr_ctlinput)(cmd, sa, (caddr_t)0);
198 }
199 
200 void
201 pfslowtimo(arg)
202 	void *arg;
203 {
204 	register struct domain *dp;
205 	register struct protosw *pr;
206 
207 	for (dp = domains; dp; dp = dp->dom_next)
208 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
209 			if (pr->pr_slowtimo)
210 				(*pr->pr_slowtimo)();
211 	timeout(pfslowtimo, NULL, hz/2);
212 }
213 
214 void
215 pffasttimo(arg)
216 	void *arg;
217 {
218 	register struct domain *dp;
219 	register struct protosw *pr;
220 
221 	for (dp = domains; dp; dp = dp->dom_next)
222 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
223 			if (pr->pr_fasttimo)
224 				(*pr->pr_fasttimo)();
225 	timeout(pffasttimo, NULL, hz/5);
226 }
227