2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
24 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
26 * Copyright (c) 1982, 1986, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * @(#)uipc_domain.c 8.3 (Berkeley) 2/14/95
60 #include <sys/param.h>
61 #include <sys/socket.h>
62 #include <sys/protosw.h>
63 #include <sys/domain.h>
66 #include <sys/kernel.h>
67 #include <sys/systm.h>
68 #include <sys/proc_internal.h>
69 #include <sys/sysctl.h>
70 #include <sys/syslog.h>
71 #include <sys/queue.h>
73 void pffasttimo(void *);
74 void pfslowtimo(void *);
77 * Add/delete 'domain': Link structure into system list,
78 * invoke the domain init, and then the proto inits.
79 * To delete, just remove from the list (dom_refs must be zero)
82 lck_grp_t
*domain_proto_mtx_grp
;
83 lck_attr_t
*domain_proto_mtx_attr
;
84 static lck_grp_attr_t
*domain_proto_mtx_grp_attr
;
85 lck_mtx_t
*domain_proto_mtx
;
86 extern int do_reclaim
;
88 void init_domain(register struct domain
*dp
)
92 if ((dp
->dom_mtx
= lck_mtx_alloc_init(domain_proto_mtx_grp
, domain_proto_mtx_attr
)) == NULL
) {
93 printf("init_domain: can't init domain mtx for domain=%s\n", dp
->dom_name
);
94 return; /* we have a problem... */
100 /* and then init the currently installed protos in this domain */
102 for (pr
= dp
->dom_protosw
; pr
; pr
= pr
->pr_next
) {
103 if (pr
->pr_usrreqs
== 0)
104 panic("domaininit: %ssw[%d] has no usrreqs!",
106 (int)(pr
- dp
->dom_protosw
));
112 /* Recompute for new protocol */
113 if (max_linkhdr
< 16) /* XXX - Sheesh; everything's ether? */
115 if (dp
->dom_protohdrlen
> max_protohdr
)
116 max_protohdr
= dp
->dom_protohdrlen
;
117 max_hdr
= max_linkhdr
+ max_protohdr
;
118 max_datalen
= MHLEN
- max_hdr
;
121 void concat_domain(struct domain
*dp
)
123 lck_mtx_assert(domain_proto_mtx
, LCK_MTX_ASSERT_OWNED
);
124 dp
->dom_next
= domains
;
129 net_add_domain(register struct domain
*dp
)
130 { register struct protosw
*pr
;
132 kprintf("Adding domain %s (family %d)\n", dp
->dom_name
,
134 /* First, link in the domain */
136 lck_mtx_lock(domain_proto_mtx
);
140 lck_mtx_unlock(domain_proto_mtx
);
145 net_del_domain(register struct domain
*dp
)
146 { register struct domain
*dp1
, *dp2
;
147 register int retval
= 0;
149 lck_mtx_lock(domain_proto_mtx
);
152 lck_mtx_unlock(domain_proto_mtx
);
156 for (dp2
= NULL
, dp1
= domains
; dp1
; dp2
= dp1
, dp1
= dp1
->dom_next
)
162 dp2
->dom_next
= dp1
->dom_next
;
164 domains
= dp1
->dom_next
;
166 retval
= EPFNOSUPPORT
;
167 lck_mtx_unlock(domain_proto_mtx
);
173 * net_add_proto - link a protosw into a domain's protosw chain
175 * note: protocols must use their own domain lock before calling net_add_proto
178 net_add_proto(register struct protosw
*pp
,
179 register struct domain
*dp
)
180 { register struct protosw
*pp1
, *pp2
;
182 for (pp2
= NULL
, pp1
= dp
->dom_protosw
; pp1
; pp1
= pp1
->pr_next
)
183 { if (pp1
->pr_type
== pp
->pr_type
&&
184 pp1
->pr_protocol
== pp
->pr_protocol
) {
190 dp
->dom_protosw
= pp
;
194 TAILQ_INIT(&pp
->pr_filter_head
);
198 /* Make sure pr_init isn't called again!! */
204 * net_del_proto - remove a protosw from a domain's protosw chain.
205 * Search the protosw chain for the element with matching data.
206 * Then unlink and return.
208 * note: protocols must use their own domain lock before calling net_del_proto
211 net_del_proto(register int type
,
212 register int protocol
,
213 register struct domain
*dp
)
214 { register struct protosw
*pp1
, *pp2
;
216 for (pp2
= NULL
, pp1
= dp
->dom_protosw
; pp1
; pp1
= pp1
->pr_next
)
217 { if (pp1
->pr_type
== type
&&
218 pp1
->pr_protocol
== protocol
)
226 pp2
->pr_next
= pp1
->pr_next
;
228 dp
->dom_protosw
= pp1
->pr_next
;
235 { register struct domain
*dp
;
236 register struct protosw
*pr
;
237 extern struct domain localdomain
, routedomain
, ndrvdomain
, inetdomain
;
238 extern struct domain systemdomain
;
240 extern struct domain nsdomain
;
243 extern struct domain isodomain
;
246 extern struct domain ccittdomain
;
250 extern struct domain atalkdomain
;
253 extern struct domain inet6domain
;
256 extern struct domain keydomain
;
260 * allocate lock group attribute and group for domain mutexes
262 domain_proto_mtx_grp_attr
= lck_grp_attr_alloc_init();
263 lck_grp_attr_setdefault(domain_proto_mtx_grp_attr
);
265 domain_proto_mtx_grp
= lck_grp_alloc_init("domain", domain_proto_mtx_grp_attr
);
268 * allocate the lock attribute for per domain mutexes
270 domain_proto_mtx_attr
= lck_attr_alloc_init();
271 lck_attr_setdefault(domain_proto_mtx_attr
);
273 if ((domain_proto_mtx
= lck_mtx_alloc_init(domain_proto_mtx_grp
, domain_proto_mtx_attr
)) == NULL
) {
274 printf("domaininit: can't init domain mtx for domain list\n");
275 return; /* we have a problem... */
278 * Add all the static domains to the domains list
281 lck_mtx_lock(domain_proto_mtx
);
283 concat_domain(&localdomain
);
284 concat_domain(&routedomain
);
285 concat_domain(&inetdomain
);
287 concat_domain(&atalkdomain
);
290 concat_domain(&inet6domain
);
293 concat_domain(&keydomain
);
297 concat_domain(&nsdomain
);
300 concat_domain(&isodomain
);
303 concat_domain(&ccittdomain
);
305 concat_domain(&ndrvdomain
);
307 concat_domain(&systemdomain
);
310 * Now ask them all to init (XXX including the routing domain,
313 for (dp
= domains
; dp
; dp
= dp
->dom_next
)
316 lck_mtx_unlock(domain_proto_mtx
);
317 timeout(pffasttimo
, NULL
, 1);
318 timeout(pfslowtimo
, NULL
, 1);
322 pffindtype(family
, type
)
325 register struct domain
*dp
;
326 register struct protosw
*pr
;
328 lck_mtx_assert(domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
329 lck_mtx_lock(domain_proto_mtx
);
330 for (dp
= domains
; dp
; dp
= dp
->dom_next
)
331 if (dp
->dom_family
== family
)
333 lck_mtx_unlock(domain_proto_mtx
);
336 for (pr
= dp
->dom_protosw
; pr
; pr
= pr
->pr_next
)
337 if (pr
->pr_type
&& pr
->pr_type
== type
) {
338 lck_mtx_unlock(domain_proto_mtx
);
341 lck_mtx_unlock(domain_proto_mtx
);
349 lck_mtx_assert(domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
350 lck_mtx_lock(domain_proto_mtx
);
353 { if (dp
->dom_family
== pf
) {
354 lck_mtx_unlock(domain_proto_mtx
);
359 lck_mtx_unlock(domain_proto_mtx
);
364 pffindproto(family
, protocol
, type
)
365 int family
, protocol
, type
;
367 register struct protosw
*pr
;
368 lck_mtx_assert(domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
369 lck_mtx_lock(domain_proto_mtx
);
370 pr
= pffindproto_locked(family
, protocol
, type
);
371 lck_mtx_unlock(domain_proto_mtx
);
376 pffindproto_locked(family
, protocol
, type
)
377 int family
, protocol
, type
;
379 register struct domain
*dp
;
380 register struct protosw
*pr
;
381 struct protosw
*maybe
= 0;
385 for (dp
= domains
; dp
; dp
= dp
->dom_next
)
386 if (dp
->dom_family
== family
)
390 for (pr
= dp
->dom_protosw
; pr
; pr
= pr
->pr_next
) {
391 if ((pr
->pr_protocol
== protocol
) && (pr
->pr_type
== type
))
394 if (type
== SOCK_RAW
&& pr
->pr_type
== SOCK_RAW
&&
395 pr
->pr_protocol
== 0 && maybe
== (struct protosw
*)0)
402 net_sysctl(int *name
, u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
403 user_addr_t newp
, size_t newlen
, struct proc
*p
)
405 register struct domain
*dp
;
406 register struct protosw
*pr
;
407 int family
, protocol
, error
;
410 * All sysctl names at this level are nonterminal;
411 * next two components are protocol family and protocol number,
412 * then at least one addition component.
415 return (EISDIR
); /* overloaded */
421 lck_mtx_lock(domain_proto_mtx
);
422 for (dp
= domains
; dp
; dp
= dp
->dom_next
)
423 if (dp
->dom_family
== family
)
425 lck_mtx_unlock(domain_proto_mtx
);
426 return (ENOPROTOOPT
);
428 for (pr
= dp
->dom_protosw
; pr
; pr
= pr
->pr_next
)
429 if (pr
->pr_protocol
== protocol
&& pr
->pr_sysctl
) {
430 error
= (*pr
->pr_sysctl
)(name
+ 2, namelen
- 2,
431 oldp
, oldlenp
, newp
, newlen
);
432 lck_mtx_unlock(domain_proto_mtx
);
435 lck_mtx_unlock(domain_proto_mtx
);
436 return (ENOPROTOOPT
);
444 pfctlinput2(cmd
, sa
, (void*)0);
448 pfctlinput2(cmd
, sa
, ctlparam
)
459 lck_mtx_lock(domain_proto_mtx
);
460 for (dp
= domains
; dp
; dp
= dp
->dom_next
)
461 for (pr
= dp
->dom_protosw
; pr
; pr
= pr
->pr_next
)
463 (*pr
->pr_ctlinput
)(cmd
, sa
, ctlparam
);
464 lck_mtx_unlock(domain_proto_mtx
);
471 register struct domain
*dp
;
472 register struct protosw
*pr
;
474 lck_mtx_lock(domain_proto_mtx
);
475 for (dp
= domains
; dp
; dp
= dp
->dom_next
)
476 for (pr
= dp
->dom_protosw
; pr
; pr
= pr
->pr_next
) {
478 (*pr
->pr_slowtimo
)();
479 if (do_reclaim
&& pr
->pr_drain
)
483 lck_mtx_unlock(domain_proto_mtx
);
484 timeout(pfslowtimo
, NULL
, hz
/2);
492 register struct domain
*dp
;
493 register struct protosw
*pr
;
495 lck_mtx_lock(domain_proto_mtx
);
496 for (dp
= domains
; dp
; dp
= dp
->dom_next
)
497 for (pr
= dp
->dom_protosw
; pr
; pr
= pr
->pr_next
)
499 (*pr
->pr_fasttimo
)();
500 lck_mtx_unlock(domain_proto_mtx
);
501 timeout(pffasttimo
, NULL
, hz
/5);