]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/uipc_domain.c
xnu-1504.9.17.tar.gz
[apple/xnu.git] / bsd / kern / uipc_domain.c
CommitLineData
1c79356b 1/*
d1ecb069 2 * Copyright (c) 1998-2009 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
1c79356b
A
28/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29/*
30 * Copyright (c) 1982, 1986, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)uipc_domain.c 8.3 (Berkeley) 2/14/95
62 */
63
64#include <sys/param.h>
65#include <sys/socket.h>
66#include <sys/protosw.h>
67#include <sys/domain.h>
68#include <sys/mbuf.h>
69#include <sys/time.h>
70#include <sys/kernel.h>
71#include <sys/systm.h>
91447636 72#include <sys/proc_internal.h>
1c79356b
A
73#include <sys/sysctl.h>
74#include <sys/syslog.h>
75#include <sys/queue.h>
76
2d21ac55
A
77#include <pexpert/pexpert.h>
78
2d21ac55
A
79void init_domain(struct domain *dp) __attribute__((section("__TEXT, initcode")));
80void concat_domain(struct domain *dp) __attribute__((section("__TEXT, initcode")));
81
82
91447636
A
83void pffasttimo(void *);
84void pfslowtimo(void *);
1c79356b 85
2d21ac55
A
86struct protosw *pffindprotonotype(int, int);
87struct protosw *pffindprotonotype_locked(int , int , int);
88struct domain *pffinddomain(int);
2d21ac55 89
1c79356b
A
90/*
91 * Add/delete 'domain': Link structure into system list,
92 * invoke the domain init, and then the proto inits.
93 * To delete, just remove from the list (dom_refs must be zero)
94 */
95
91447636
A
96lck_grp_t *domain_proto_mtx_grp;
97lck_attr_t *domain_proto_mtx_attr;
98static lck_grp_attr_t *domain_proto_mtx_grp_attr;
99lck_mtx_t *domain_proto_mtx;
100extern int do_reclaim;
1c79356b 101
b0d623f7
A
102extern sysctlfn net_sysctl;
103
2d21ac55
A
104static void
105init_proto(struct protosw *pr)
106{
107 TAILQ_INIT(&pr->pr_filter_head);
108 if (pr->pr_init)
109 (*pr->pr_init)();
110
111 /* Make sure pr_init isn't called again!! */
112 pr->pr_init = 0;
113}
114
115void
116init_domain(struct domain *dp)
1c79356b
A
117{
118 struct protosw *pr;
119
91447636
A
120 if ((dp->dom_mtx = lck_mtx_alloc_init(domain_proto_mtx_grp, domain_proto_mtx_attr)) == NULL) {
121 printf("init_domain: can't init domain mtx for domain=%s\n", dp->dom_name);
122 return; /* we have a problem... */
123 }
124
1c79356b
A
125 if (dp->dom_init)
126 (*dp->dom_init)();
127
128 /* and then init the currently installed protos in this domain */
129
130 for (pr = dp->dom_protosw; pr; pr = pr->pr_next) {
131 if (pr->pr_usrreqs == 0)
132 panic("domaininit: %ssw[%d] has no usrreqs!",
133 dp->dom_name,
134 (int)(pr - dp->dom_protosw));
135
2d21ac55
A
136 init_proto(pr);
137
1c79356b
A
138 }
139
140 /* Recompute for new protocol */
141 if (max_linkhdr < 16) /* XXX - Sheesh; everything's ether? */
142 max_linkhdr = 16;
143 if (dp->dom_protohdrlen > max_protohdr)
144 max_protohdr = dp->dom_protohdrlen;
145 max_hdr = max_linkhdr + max_protohdr;
146 max_datalen = MHLEN - max_hdr;
147}
148
2d21ac55
A
149void
150concat_domain(struct domain *dp)
1c79356b 151{
91447636 152 lck_mtx_assert(domain_proto_mtx, LCK_MTX_ASSERT_OWNED);
1c79356b
A
153 dp->dom_next = domains;
154 domains = dp;
155}
156
157void
2d21ac55
A
158net_add_domain(struct domain *dp)
159{
1c79356b
A
160 kprintf("Adding domain %s (family %d)\n", dp->dom_name,
161 dp->dom_family);
162 /* First, link in the domain */
1c79356b 163
91447636 164 lck_mtx_lock(domain_proto_mtx);
1c79356b
A
165 concat_domain(dp);
166
167 init_domain(dp);
91447636 168 lck_mtx_unlock(domain_proto_mtx);
1c79356b 169
1c79356b
A
170}
171
172int
2d21ac55 173net_del_domain(struct domain *dp)
1c79356b 174{ register struct domain *dp1, *dp2;
91447636
A
175 register int retval = 0;
176
177 lck_mtx_lock(domain_proto_mtx);
1c79356b 178
91447636
A
179 if (dp->dom_refs) {
180 lck_mtx_unlock(domain_proto_mtx);
1c79356b 181 return(EBUSY);
91447636 182 }
1c79356b
A
183
184 for (dp2 = NULL, dp1 = domains; dp1; dp2 = dp1, dp1 = dp1->dom_next)
185 { if (dp == dp1)
186 break;
187 }
188 if (dp1)
189 { if (dp2)
190 dp2->dom_next = dp1->dom_next;
191 else
192 domains = dp1->dom_next;
193 } else
194 retval = EPFNOSUPPORT;
91447636 195 lck_mtx_unlock(domain_proto_mtx);
1c79356b
A
196
197 return(retval);
198}
199
200/*
201 * net_add_proto - link a protosw into a domain's protosw chain
91447636
A
202 *
203 * note: protocols must use their own domain lock before calling net_add_proto
1c79356b
A
204 */
205int
2d21ac55 206net_add_proto(struct protosw *pp, struct domain *dp)
1c79356b 207{ register struct protosw *pp1, *pp2;
1c79356b 208
1c79356b
A
209 for (pp2 = NULL, pp1 = dp->dom_protosw; pp1; pp1 = pp1->pr_next)
210 { if (pp1->pr_type == pp->pr_type &&
211 pp1->pr_protocol == pp->pr_protocol) {
1c79356b
A
212 return(EEXIST);
213 }
214 pp2 = pp1;
215 }
216 if (pp2 == NULL)
217 dp->dom_protosw = pp;
218 else
219 pp2->pr_next = pp;
1c79356b 220
2d21ac55
A
221 init_proto(pp);
222
1c79356b
A
223 return(0);
224}
225
226/*
227 * net_del_proto - remove a protosw from a domain's protosw chain.
228 * Search the protosw chain for the element with matching data.
229 * Then unlink and return.
91447636
A
230 *
231 * note: protocols must use their own domain lock before calling net_del_proto
1c79356b
A
232 */
233int
2d21ac55
A
234net_del_proto(int type, int protocol, struct domain *dp)
235{
236 register struct protosw *pp1, *pp2;
1c79356b 237
1c79356b
A
238 for (pp2 = NULL, pp1 = dp->dom_protosw; pp1; pp1 = pp1->pr_next)
239 { if (pp1->pr_type == type &&
240 pp1->pr_protocol == protocol)
241 break;
242 pp2 = pp1;
243 }
244 if (pp1 == NULL) {
1c79356b
A
245 return(ENXIO);
246 }
247 if (pp2)
248 pp2->pr_next = pp1->pr_next;
249 else
250 dp->dom_protosw = pp1->pr_next;
1c79356b
A
251 return(0);
252}
253
254
1c79356b 255#if NS
2d21ac55 256extern struct domain nsdomain;
1c79356b
A
257#endif
258#if ISO
2d21ac55 259extern struct domain isodomain;
1c79356b
A
260#endif
261#if CCITT
2d21ac55 262extern struct domain ccittdomain;
1c79356b
A
263#endif
264
265#if NETAT
2d21ac55 266extern struct domain atalkdomain;
1c79356b
A
267#endif
268#if INET6
2d21ac55 269extern struct domain inet6domain;
1c79356b
A
270#endif
271#if IPSEC
2d21ac55 272extern struct domain keydomain;
1c79356b
A
273#endif
274
2d21ac55
A
275extern struct domain routedomain, ndrvdomain, inetdomain;
276extern struct domain systemdomain;
277
278void
279domaininit(void)
280{
281 register struct domain *dp;
282
91447636
A
283 /*
284 * allocate lock group attribute and group for domain mutexes
285 */
286 domain_proto_mtx_grp_attr = lck_grp_attr_alloc_init();
91447636
A
287
288 domain_proto_mtx_grp = lck_grp_alloc_init("domain", domain_proto_mtx_grp_attr);
289
290 /*
291 * allocate the lock attribute for per domain mutexes
292 */
293 domain_proto_mtx_attr = lck_attr_alloc_init();
91447636
A
294
295 if ((domain_proto_mtx = lck_mtx_alloc_init(domain_proto_mtx_grp, domain_proto_mtx_attr)) == NULL) {
296 printf("domaininit: can't init domain mtx for domain list\n");
297 return; /* we have a problem... */
298 }
1c79356b
A
299 /*
300 * Add all the static domains to the domains list
301 */
302
91447636
A
303 lck_mtx_lock(domain_proto_mtx);
304
1c79356b
A
305 concat_domain(&localdomain);
306 concat_domain(&routedomain);
307 concat_domain(&inetdomain);
308#if NETAT
309 concat_domain(&atalkdomain);
310#endif
311#if INET6
312 concat_domain(&inet6domain);
313#endif
314#if IPSEC
315 concat_domain(&keydomain);
316#endif
317
318#if NS
319 concat_domain(&nsdomain);
320#endif
321#if ISO
322 concat_domain(&isodomain);
323#endif
324#if CCITT
325 concat_domain(&ccittdomain);
326#endif
327 concat_domain(&ndrvdomain);
328
329 concat_domain(&systemdomain);
330
331 /*
332 * Now ask them all to init (XXX including the routing domain,
333 * see above)
334 */
335 for (dp = domains; dp; dp = dp->dom_next)
336 init_domain(dp);
337
91447636 338 lck_mtx_unlock(domain_proto_mtx);
1c79356b
A
339 timeout(pffasttimo, NULL, 1);
340 timeout(pfslowtimo, NULL, 1);
1c79356b
A
341}
342
2d21ac55
A
343static __inline__ struct domain *
344pffinddomain_locked(int pf)
345{
346 struct domain *dp;
347
348 dp = domains;
349 while (dp != NULL)
350 { if (dp->dom_family == pf) {
351 break;
352 }
353 dp = dp->dom_next;
354 }
355 return (dp);
356}
357
1c79356b 358struct protosw *
2d21ac55 359pffindtype(int family, int type)
1c79356b
A
360{
361 register struct domain *dp;
362 register struct protosw *pr;
363
91447636
A
364 lck_mtx_assert(domain_proto_mtx, LCK_MTX_ASSERT_NOTOWNED);
365 lck_mtx_lock(domain_proto_mtx);
2d21ac55
A
366 dp = pffinddomain_locked(family);
367 if (dp == NULL) {
91447636 368 lck_mtx_unlock(domain_proto_mtx);
2d21ac55
A
369 return (NULL);
370 }
1c79356b 371 for (pr = dp->dom_protosw; pr; pr = pr->pr_next)
91447636
A
372 if (pr->pr_type && pr->pr_type == type) {
373 lck_mtx_unlock(domain_proto_mtx);
1c79356b 374 return (pr);
91447636
A
375 }
376 lck_mtx_unlock(domain_proto_mtx);
1c79356b
A
377 return (0);
378}
379
380struct domain *
381pffinddomain(int pf)
2d21ac55
A
382{
383 struct domain *dp;
1c79356b 384
91447636
A
385 lck_mtx_assert(domain_proto_mtx, LCK_MTX_ASSERT_NOTOWNED);
386 lck_mtx_lock(domain_proto_mtx);
2d21ac55 387 dp = pffinddomain_locked(pf);
91447636 388 lck_mtx_unlock(domain_proto_mtx);
1c79356b 389 return(dp);
91447636 390 }
1c79356b
A
391
392struct protosw *
2d21ac55 393pffindproto(int family, int protocol, int type)
91447636
A
394{
395 register struct protosw *pr;
396 lck_mtx_assert(domain_proto_mtx, LCK_MTX_ASSERT_NOTOWNED);
397 lck_mtx_lock(domain_proto_mtx);
398 pr = pffindproto_locked(family, protocol, type);
399 lck_mtx_unlock(domain_proto_mtx);
400 return (pr);
401}
402
403struct protosw *
2d21ac55 404pffindproto_locked(int family, int protocol, int type)
1c79356b
A
405{
406 register struct domain *dp;
407 register struct protosw *pr;
408 struct protosw *maybe = 0;
409
410 if (family == 0)
411 return (0);
2d21ac55
A
412 dp = pffinddomain_locked(family);
413 if (dp == NULL) {
414 return (NULL);
415 }
1c79356b
A
416 for (pr = dp->dom_protosw; pr; pr = pr->pr_next) {
417 if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
418 return (pr);
419
420 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
421 pr->pr_protocol == 0 && maybe == (struct protosw *)0)
422 maybe = pr;
423 }
424 return (maybe);
425}
426
2d21ac55
A
427struct protosw *
428pffindprotonotype_locked(int family, int protocol, __unused int type)
429{
430 register struct domain *dp;
431 register struct protosw *pr;
432
433 if (family == 0)
434 return (0);
435 dp = pffinddomain_locked(family);
436 if (dp == NULL) {
437 return (NULL);
438 }
439 for (pr = dp->dom_protosw; pr; pr = pr->pr_next) {
440 if (pr->pr_protocol == protocol) {
441 return (pr);
442 }
443 }
444 return (NULL);
445}
446
447struct protosw *
448pffindprotonotype(int family, int protocol)
449{
450 register struct protosw *pr;
451 if (protocol == 0) {
452 return (NULL);
453 }
454 lck_mtx_assert(domain_proto_mtx, LCK_MTX_ASSERT_NOTOWNED);
455 lck_mtx_lock(domain_proto_mtx);
456 pr = pffindprotonotype_locked(family, protocol, 0);
457 lck_mtx_unlock(domain_proto_mtx);
458 return (pr);
459}
460
1c79356b 461int
91447636 462net_sysctl(int *name, u_int namelen, user_addr_t oldp, size_t *oldlenp,
2d21ac55 463 user_addr_t newp, size_t newlen, __unused struct proc *p)
1c79356b
A
464{
465 register struct domain *dp;
466 register struct protosw *pr;
91447636 467 int family, protocol, error;
1c79356b
A
468
469 /*
470 * All sysctl names at this level are nonterminal;
471 * next two components are protocol family and protocol number,
472 * then at least one addition component.
473 */
474 if (namelen < 3)
475 return (EISDIR); /* overloaded */
476 family = name[0];
477 protocol = name[1];
478
479 if (family == 0)
480 return (0);
91447636 481 lck_mtx_lock(domain_proto_mtx);
1c79356b
A
482 for (dp = domains; dp; dp = dp->dom_next)
483 if (dp->dom_family == family)
484 goto found;
91447636 485 lck_mtx_unlock(domain_proto_mtx);
1c79356b
A
486 return (ENOPROTOOPT);
487found:
488 for (pr = dp->dom_protosw; pr; pr = pr->pr_next)
91447636
A
489 if (pr->pr_protocol == protocol && pr->pr_sysctl) {
490 error = (*pr->pr_sysctl)(name + 2, namelen - 2,
b0d623f7 491 (void *)(uintptr_t)oldp, oldlenp, (void *)(uintptr_t)newp, newlen);
91447636
A
492 lck_mtx_unlock(domain_proto_mtx);
493 return (error);
494 }
495 lck_mtx_unlock(domain_proto_mtx);
1c79356b
A
496 return (ENOPROTOOPT);
497}
498
499void
2d21ac55 500pfctlinput(int cmd, struct sockaddr *sa)
1c79356b 501{
9bccf70c
A
502 pfctlinput2(cmd, sa, (void*)0);
503}
504
505void
2d21ac55 506pfctlinput2(int cmd, struct sockaddr *sa, void *ctlparam)
9bccf70c
A
507{
508 struct domain *dp;
509 struct protosw *pr;
1c79356b 510
9bccf70c
A
511 if (!sa)
512 return;
91447636
A
513
514 lck_mtx_lock(domain_proto_mtx);
1c79356b
A
515 for (dp = domains; dp; dp = dp->dom_next)
516 for (pr = dp->dom_protosw; pr; pr = pr->pr_next)
517 if (pr->pr_ctlinput)
9bccf70c 518 (*pr->pr_ctlinput)(cmd, sa, ctlparam);
91447636 519 lck_mtx_unlock(domain_proto_mtx);
1c79356b
A
520}
521
522void
2d21ac55 523pfslowtimo(__unused void *arg)
1c79356b
A
524{
525 register struct domain *dp;
526 register struct protosw *pr;
1c79356b 527
91447636
A
528 lck_mtx_lock(domain_proto_mtx);
529 for (dp = domains; dp; dp = dp->dom_next)
530 for (pr = dp->dom_protosw; pr; pr = pr->pr_next) {
1c79356b
A
531 if (pr->pr_slowtimo)
532 (*pr->pr_slowtimo)();
d1ecb069
A
533 if ((do_reclaim || (pr->pr_flags & PR_AGGDRAIN)) &&
534 pr->pr_drain)
91447636
A
535 (*pr->pr_drain)();
536 }
537 do_reclaim = 0;
538 lck_mtx_unlock(domain_proto_mtx);
2d21ac55 539 timeout(pfslowtimo, NULL, hz/PR_SLOWHZ);
1c79356b
A
540}
541
542void
2d21ac55 543pffasttimo(__unused void *arg)
1c79356b
A
544{
545 register struct domain *dp;
546 register struct protosw *pr;
1c79356b 547
91447636 548 lck_mtx_lock(domain_proto_mtx);
1c79356b
A
549 for (dp = domains; dp; dp = dp->dom_next)
550 for (pr = dp->dom_protosw; pr; pr = pr->pr_next)
551 if (pr->pr_fasttimo)
552 (*pr->pr_fasttimo)();
91447636 553 lck_mtx_unlock(domain_proto_mtx);
2d21ac55 554 timeout(pffasttimo, NULL, hz/PR_FASTHZ);
1c79356b 555}