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