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