uipc_domain.c revision 1.13 1 /* $NetBSD: uipc_domain.c,v 1.13 1996/02/04 02:17:43 christos 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.2 (Berkeley) 10/18/93
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 #include <kern/kern_extern.h>
51
52 void pffasttimo __P((void *));
53 void pfslowtimo __P((void *));
54
55 #define ADDDOMAIN(x) { \
56 extern struct domain __CONCAT(x,domain); \
57 __CONCAT(x,domain.dom_next) = domains; \
58 domains = &__CONCAT(x,domain); \
59 }
60
61 void
62 domaininit()
63 {
64 register struct domain *dp;
65 register struct protosw *pr;
66
67 #undef unix
68 #ifndef lint
69 ADDDOMAIN(unix);
70 ADDDOMAIN(route);
71 #ifdef INET
72 ADDDOMAIN(inet);
73 #endif
74 #ifdef NS
75 ADDDOMAIN(ns);
76 #endif
77 #ifdef ISO
78 ADDDOMAIN(iso);
79 #endif
80 #ifdef CCITT
81 ADDDOMAIN(ccitt);
82 #endif
83 #ifdef notdef /* XXXX */
84 #include "imp.h"
85 #if NIMP > 0
86 ADDDOMAIN(imp);
87 #endif
88 #endif
89 #endif
90
91 for (dp = domains; dp; dp = dp->dom_next) {
92 if (dp->dom_init)
93 (*dp->dom_init)();
94 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
95 if (pr->pr_init)
96 (*pr->pr_init)();
97 }
98
99 if (max_linkhdr < 16) /* XXX */
100 max_linkhdr = 16;
101 max_hdr = max_linkhdr + max_protohdr;
102 max_datalen = MHLEN - max_hdr;
103 timeout(pffasttimo, NULL, 1);
104 timeout(pfslowtimo, NULL, 1);
105 }
106
107 struct protosw *
108 pffindtype(family, type)
109 int family, type;
110 {
111 register struct domain *dp;
112 register struct protosw *pr;
113
114 for (dp = domains; dp; dp = dp->dom_next)
115 if (dp->dom_family == family)
116 goto found;
117 return (0);
118 found:
119 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
120 if (pr->pr_type && pr->pr_type == type)
121 return (pr);
122 return (0);
123 }
124
125 struct protosw *
126 pffindproto(family, protocol, type)
127 int family, protocol, type;
128 {
129 register struct domain *dp;
130 register struct protosw *pr;
131 struct protosw *maybe = 0;
132
133 if (family == 0)
134 return (0);
135 for (dp = domains; dp; dp = dp->dom_next)
136 if (dp->dom_family == family)
137 goto found;
138 return (0);
139 found:
140 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
141 if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
142 return (pr);
143
144 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
145 pr->pr_protocol == 0 && maybe == (struct protosw *)0)
146 maybe = pr;
147 }
148 return (maybe);
149 }
150
151 int
152 net_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
153 int *name;
154 u_int namelen;
155 void *oldp;
156 size_t *oldlenp;
157 void *newp;
158 size_t newlen;
159 struct proc *p;
160 {
161 register struct domain *dp;
162 register struct protosw *pr;
163 int family, protocol;
164
165 /*
166 * All sysctl names at this level are nonterminal;
167 * next two components are protocol family and protocol number,
168 * then at least one addition component.
169 */
170 if (namelen < 3)
171 return (EISDIR); /* overloaded */
172 family = name[0];
173 protocol = name[1];
174
175 if (family == 0)
176 return (0);
177 for (dp = domains; dp; dp = dp->dom_next)
178 if (dp->dom_family == family)
179 goto found;
180 return (ENOPROTOOPT);
181 found:
182 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
183 if (pr->pr_protocol == protocol && pr->pr_sysctl)
184 return ((*pr->pr_sysctl)(name + 2, namelen - 2,
185 oldp, oldlenp, newp, newlen));
186 return (ENOPROTOOPT);
187 }
188
189 void
190 pfctlinput(cmd, sa)
191 int cmd;
192 struct sockaddr *sa;
193 {
194 register struct domain *dp;
195 register struct protosw *pr;
196
197 for (dp = domains; dp; dp = dp->dom_next)
198 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
199 if (pr->pr_ctlinput)
200 (*pr->pr_ctlinput)(cmd, sa, NULL);
201 }
202
203 void
204 pfslowtimo(arg)
205 void *arg;
206 {
207 register struct domain *dp;
208 register struct protosw *pr;
209
210 for (dp = domains; dp; dp = dp->dom_next)
211 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
212 if (pr->pr_slowtimo)
213 (*pr->pr_slowtimo)();
214 timeout(pfslowtimo, NULL, hz/2);
215 }
216
217 void
218 pffasttimo(arg)
219 void *arg;
220 {
221 register struct domain *dp;
222 register struct protosw *pr;
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_fasttimo)
227 (*pr->pr_fasttimo)();
228 timeout(pffasttimo, NULL, hz/5);
229 }
230