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