uipc_domain.c revision 1.29 1 /* $NetBSD: uipc_domain.c,v 1.29 2000/02/06 02:54:16 thorpej 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/proc.h>
56 #include <vm/vm.h>
57 #include <sys/sysctl.h>
58
59 void pffasttimo __P((void *));
60 void pfslowtimo __P((void *));
61
62 /*
63 * Current time values for fast and slow timeouts. We can use u_int
64 * relatively safely. The fast timer will roll over in 27 years and
65 * the slow timer in 68 years.
66 */
67 u_int pfslowtimo_now;
68 u_int pffasttimo_now;
69
70 #define ADDDOMAIN(x) { \
71 extern struct domain __CONCAT(x,domain); \
72 __CONCAT(x,domain.dom_next) = domains; \
73 domains = &__CONCAT(x,domain); \
74 }
75
76 void
77 domaininit()
78 {
79 register struct domain *dp;
80 register struct protosw *pr;
81
82 #undef unix
83 /*
84 * KAME NOTE: ADDDOMAIN(route) is moved to the last part so that
85 * it will be initialized as the *first* element. confusing!
86 */
87 #ifndef lint
88 ADDDOMAIN(unix);
89 #ifdef INET
90 ADDDOMAIN(inet);
91 #endif
92 #ifdef INET6
93 ADDDOMAIN(inet6);
94 #endif
95 #ifdef NS
96 ADDDOMAIN(ns);
97 #endif
98 #ifdef ISO
99 ADDDOMAIN(iso);
100 #endif
101 #ifdef CCITT
102 ADDDOMAIN(ccitt);
103 #endif
104 #ifdef NATM
105 ADDDOMAIN(natm);
106 #endif
107 #ifdef NETATALK
108 ADDDOMAIN(atalk);
109 #endif
110 #ifdef IPSEC
111 ADDDOMAIN(key);
112 #endif
113 #if NARP > 0
114 ADDDOMAIN(arp);
115 #endif
116 ADDDOMAIN(route);
117 #endif /* ! lint */
118
119 for (dp = domains; dp; dp = dp->dom_next) {
120 if (dp->dom_init)
121 (*dp->dom_init)();
122 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
123 if (pr->pr_init)
124 (*pr->pr_init)();
125 }
126
127 if (max_linkhdr < 16) /* XXX */
128 max_linkhdr = 16;
129 max_hdr = max_linkhdr + max_protohdr;
130 max_datalen = MHLEN - max_hdr;
131 timeout(pffasttimo, NULL, 1);
132 timeout(pfslowtimo, NULL, 1);
133 }
134
135 struct domain *
136 pffinddomain(family)
137 int family;
138 {
139 struct domain *dp;
140
141 for (dp = domains; dp != NULL; dp = dp->dom_next)
142 if (dp->dom_family == family)
143 return (dp);
144 return (NULL);
145 }
146
147 struct protosw *
148 pffindtype(family, type)
149 int family, type;
150 {
151 struct domain *dp;
152 struct protosw *pr;
153
154 dp = pffinddomain(family);
155 if (dp == NULL)
156 return (NULL);
157
158 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
159 if (pr->pr_type && pr->pr_type == type)
160 return (pr);
161
162 return (NULL);
163 }
164
165 struct protosw *
166 pffindproto(family, protocol, type)
167 int family, protocol, type;
168 {
169 struct domain *dp;
170 struct protosw *pr;
171 struct protosw *maybe = NULL;
172
173 if (family == 0)
174 return (NULL);
175
176 dp = pffinddomain(family);
177 if (dp == NULL)
178 return (NULL);
179
180 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
181 if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
182 return (pr);
183
184 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
185 pr->pr_protocol == 0 && maybe == NULL)
186 maybe = pr;
187 }
188 return (maybe);
189 }
190
191 int
192 net_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
193 int *name;
194 u_int namelen;
195 void *oldp;
196 size_t *oldlenp;
197 void *newp;
198 size_t newlen;
199 struct proc *p;
200 {
201 struct domain *dp;
202 struct protosw *pr;
203 int family, protocol;
204
205 /*
206 * All sysctl names at this level are nonterminal.
207 * PF_KEY: next component is protocol family, and then at least one
208 * additional component.
209 * usually: next two components are protocol family and protocol
210 * number, then at least one addition component.
211 */
212 if (namelen < 2)
213 return (EISDIR); /* overloaded */
214 family = name[0];
215
216 if (family == 0)
217 return (0);
218
219 dp = pffinddomain(family);
220 if (dp == NULL)
221 return (ENOPROTOOPT);
222
223 switch (family) {
224 #ifdef IPSEC
225 case PF_KEY:
226 pr = dp->dom_protosw;
227 if (pr->pr_sysctl)
228 return ((*pr->pr_sysctl)(name + 1, namelen - 1,
229 oldp, oldlenp, newp, newlen));
230 return (ENOPROTOOPT);
231 #endif
232 default:
233 break;
234 }
235 if (namelen < 3)
236 return (EISDIR); /* overloaded */
237 protocol = name[1];
238 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
239 if (pr->pr_protocol == protocol && pr->pr_sysctl)
240 return ((*pr->pr_sysctl)(name + 2, namelen - 2,
241 oldp, oldlenp, newp, newlen));
242 return (ENOPROTOOPT);
243 }
244
245 void
246 pfctlinput(cmd, sa)
247 int cmd;
248 struct sockaddr *sa;
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_ctlinput)
256 (*pr->pr_ctlinput)(cmd, sa, NULL);
257 }
258
259 void
260 pfslowtimo(arg)
261 void *arg;
262 {
263 register struct domain *dp;
264 register struct protosw *pr;
265
266 pfslowtimo_now++;
267
268 for (dp = domains; dp; dp = dp->dom_next)
269 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
270 if (pr->pr_slowtimo)
271 (*pr->pr_slowtimo)();
272 timeout(pfslowtimo, NULL, hz/2);
273 }
274
275 void
276 pffasttimo(arg)
277 void *arg;
278 {
279 register struct domain *dp;
280 register struct protosw *pr;
281
282 pffasttimo_now++;
283
284 for (dp = domains; dp; dp = dp->dom_next)
285 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
286 if (pr->pr_fasttimo)
287 (*pr->pr_fasttimo)();
288 timeout(pffasttimo, NULL, hz/5);
289 }
290