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