1 /** $MirOS: src/sys/kern/uipc_domain.c,v 1.3 2005/07/07 14:39:26 tg Exp $ */
2 /* $OpenBSD: uipc_domain.c,v 1.23 2005/06/08 06:18:54 henning Exp $ */
3 /* $NetBSD: uipc_domain.c,v 1.14 1996/02/09 19:00:44 christos Exp $ */
4
5 /*
6 * Copyright (c) 1982, 1986, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)uipc_domain.c 8.2 (Berkeley) 10/18/93
34 */
35
36 #include <sys/param.h>
37 #include <sys/socket.h>
38 #include <sys/protosw.h>
39 #include <sys/domain.h>
40 #include <sys/mbuf.h>
41 #include <sys/time.h>
42 #include <sys/kernel.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <uvm/uvm_extern.h>
46 #include <sys/sysctl.h>
47 #include <sys/timeout.h>
48
49 #include "bpfilter.h"
50
51 struct domain *domains;
52
53 void pffasttimo(void *);
54 void pfslowtimo(void *);
55 struct domain * pffinddomain(int);
56
57 #if defined (KEY) || defined (IPSEC)
58 int pfkey_init(void);
59 #endif /* KEY || IPSEC */
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
domaininit(void)68 domaininit(void)
69 {
70 struct domain *dp;
71 struct protosw *pr;
72 static struct timeout pffast_timeout;
73 static struct timeout pfslow_timeout;
74
75 #undef unix
76 /*
77 * KAME NOTE: ADDDOMAIN(route) is moved to the last part so that
78 * it will be initialized as the *first* element. confusing!
79 */
80 #ifndef lint
81 ADDDOMAIN(unix);
82 #ifdef INET
83 ADDDOMAIN(inet);
84 #endif
85 #ifdef INET6
86 ADDDOMAIN(inet6);
87 #endif /* INET6 */
88 #if defined (KEY) || defined (IPSEC)
89 pfkey_init();
90 #endif /* KEY || IPSEC */
91 #ifdef IPX
92 ADDDOMAIN(ipx);
93 #endif
94 #ifdef NETATALK
95 ADDDOMAIN(atalk);
96 #endif
97 #ifdef NATM
98 ADDDOMAIN(natm);
99 #endif
100 #ifdef IPSEC
101 #ifdef __KAME__
102 ADDDOMAIN(key);
103 #endif
104 #endif
105 ADDDOMAIN(route);
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_set(&pffast_timeout, pffasttimo, &pffast_timeout);
121 timeout_set(&pfslow_timeout, pfslowtimo, &pfslow_timeout);
122 timeout_add(&pffast_timeout, 1);
123 timeout_add(&pfslow_timeout, 1);
124 }
125
126 struct domain *
pffinddomain(int family)127 pffinddomain(int family)
128 {
129 struct domain *dp;
130
131 for (dp = domains; dp != NULL; dp = dp->dom_next)
132 if (dp->dom_family == family)
133 return (dp);
134 return (NULL);
135 }
136
137 struct protosw *
pffindtype(int family,int type)138 pffindtype(int family, int type)
139 {
140 struct domain *dp;
141 struct protosw *pr;
142
143 dp = pffinddomain(family);
144 if (dp == NULL)
145 return (NULL);
146
147 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
148 if (pr->pr_type && pr->pr_type == type)
149 return (pr);
150 return (NULL);
151 }
152
153 struct protosw *
pffindproto(int family,int protocol,int type)154 pffindproto(int family, int protocol, int type)
155 {
156 struct domain *dp;
157 struct protosw *pr;
158 struct protosw *maybe = NULL;
159
160 if (family == 0)
161 return (NULL);
162
163 dp = pffinddomain(family);
164 if (dp == NULL)
165 return (NULL);
166
167 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
168 if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
169 return (pr);
170
171 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
172 pr->pr_protocol == 0 && maybe == NULL)
173 maybe = pr;
174 }
175 return (maybe);
176 }
177
178 int
net_sysctl(int * name,u_int namelen,void * oldp,size_t * oldlenp,void * newp,size_t newlen,struct proc * p)179 net_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
180 size_t newlen, struct proc *p)
181 {
182 struct domain *dp;
183 struct protosw *pr;
184 int family, protocol;
185
186 /*
187 * All sysctl names at this level are nonterminal.
188 * Usually: next two components are protocol family and protocol
189 * number, then at least one addition component.
190 */
191 if (namelen < 2)
192 return (EISDIR); /* overloaded */
193 family = name[0];
194
195 if (family == 0)
196 return (0);
197 #if NBPFILTER > 0
198 if (family == PF_BPF)
199 return (bpf_sysctl(name + 1, namelen - 1, oldp, oldlenp,
200 newp, newlen));
201 #endif
202 dp = pffinddomain(family);
203 if (dp == NULL)
204 return (ENOPROTOOPT);
205
206 if (namelen < 3)
207 return (EISDIR); /* overloaded */
208 protocol = name[1];
209 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
210 if (pr->pr_protocol == protocol && pr->pr_sysctl)
211 return ((*pr->pr_sysctl)(name + 2, namelen - 2,
212 oldp, oldlenp, newp, newlen));
213 return (ENOPROTOOPT);
214 }
215
216 void
pfctlinput(int cmd,struct sockaddr * sa)217 pfctlinput(int cmd, struct sockaddr *sa)
218 {
219 struct domain *dp;
220 struct protosw *pr;
221
222 for (dp = domains; dp; dp = dp->dom_next)
223 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
224 if (pr->pr_ctlinput)
225 (*pr->pr_ctlinput)(cmd, sa, NULL);
226 }
227
228 void
pfslowtimo(void * arg)229 pfslowtimo(void *arg)
230 {
231 struct timeout *to = (struct timeout *)arg;
232 struct domain *dp;
233 struct protosw *pr;
234
235 for (dp = domains; dp; dp = dp->dom_next)
236 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
237 if (pr->pr_slowtimo)
238 (*pr->pr_slowtimo)();
239 timeout_add(to, hz/2);
240 }
241
242 void
pffasttimo(void * arg)243 pffasttimo(void *arg)
244 {
245 struct timeout *to = (struct timeout *)arg;
246 struct domain *dp;
247 struct protosw *pr;
248
249 for (dp = domains; dp; dp = dp->dom_next)
250 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
251 if (pr->pr_fasttimo)
252 (*pr->pr_fasttimo)();
253 timeout_add(to, hz/5);
254 }
255