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