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