uipc_domain.c revision 1.28 1 /* $NetBSD: uipc_domain.c,v 1.28 1999/08/05 04:04:28 sommerfeld 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 protosw *
136 pffindtype(family, type)
137 int family, type;
138 {
139 register struct domain *dp;
140 register struct protosw *pr;
141
142 for (dp = domains; dp; dp = dp->dom_next)
143 if (dp->dom_family == family)
144 goto found;
145 return (0);
146 found:
147 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
148 if (pr->pr_type && pr->pr_type == type)
149 return (pr);
150 return (0);
151 }
152
153 struct protosw *
154 pffindproto(family, protocol, type)
155 int family, protocol, type;
156 {
157 register struct domain *dp;
158 register struct protosw *pr;
159 struct protosw *maybe = 0;
160
161 if (family == 0)
162 return (0);
163 for (dp = domains; dp; dp = dp->dom_next)
164 if (dp->dom_family == family)
165 goto found;
166 return (0);
167 found:
168 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
169 if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
170 return (pr);
171
172 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
173 pr->pr_protocol == 0 && maybe == (struct protosw *)0)
174 maybe = pr;
175 }
176 return (maybe);
177 }
178
179 int
180 net_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
181 int *name;
182 u_int namelen;
183 void *oldp;
184 size_t *oldlenp;
185 void *newp;
186 size_t newlen;
187 struct proc *p;
188 {
189 register struct domain *dp;
190 register struct protosw *pr;
191 int family, protocol;
192
193 /*
194 * All sysctl names at this level are nonterminal.
195 * PF_KEY: next component is protocol family, and then at least one
196 * additional component.
197 * usually: next two components are protocol family and protocol
198 * number, then at least one addition component.
199 */
200 if (namelen < 2)
201 return (EISDIR); /* overloaded */
202 family = name[0];
203
204 if (family == 0)
205 return (0);
206 for (dp = domains; dp; dp = dp->dom_next)
207 if (dp->dom_family == family)
208 goto found;
209 return (ENOPROTOOPT);
210 found:
211 switch (family) {
212 #ifdef IPSEC
213 case PF_KEY:
214 pr = dp->dom_protosw;
215 if (pr->pr_sysctl)
216 return ((*pr->pr_sysctl)(name + 1, namelen - 1,
217 oldp, oldlenp, newp, newlen));
218 return (ENOPROTOOPT);
219 #endif
220 default:
221 break;
222 }
223 if (namelen < 3)
224 return (EISDIR); /* overloaded */
225 protocol = name[1];
226 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
227 if (pr->pr_protocol == protocol && pr->pr_sysctl)
228 return ((*pr->pr_sysctl)(name + 2, namelen - 2,
229 oldp, oldlenp, newp, newlen));
230 return (ENOPROTOOPT);
231 }
232
233 void
234 pfctlinput(cmd, sa)
235 int cmd;
236 struct sockaddr *sa;
237 {
238 register struct domain *dp;
239 register struct protosw *pr;
240
241 for (dp = domains; dp; dp = dp->dom_next)
242 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
243 if (pr->pr_ctlinput)
244 (*pr->pr_ctlinput)(cmd, sa, NULL);
245 }
246
247 void
248 pfslowtimo(arg)
249 void *arg;
250 {
251 register struct domain *dp;
252 register struct protosw *pr;
253
254 pfslowtimo_now++;
255
256 for (dp = domains; dp; dp = dp->dom_next)
257 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
258 if (pr->pr_slowtimo)
259 (*pr->pr_slowtimo)();
260 timeout(pfslowtimo, NULL, hz/2);
261 }
262
263 void
264 pffasttimo(arg)
265 void *arg;
266 {
267 register struct domain *dp;
268 register struct protosw *pr;
269
270 pffasttimo_now++;
271
272 for (dp = domains; dp; dp = dp->dom_next)
273 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
274 if (pr->pr_fasttimo)
275 (*pr->pr_fasttimo)();
276 timeout(pffasttimo, NULL, hz/5);
277 }
278