]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/uipc_domain.c
xnu-1699.22.73.tar.gz
[apple/xnu.git] / bsd / kern / uipc_domain.c
CommitLineData
1c79356b 1/*
6d2010ae 2 * Copyright (c) 1998-2011 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 79void init_domain(struct domain *dp) __attribute__((section("__TEXT, initcode")));
6d2010ae 80void prepend_domain(struct domain *dp) __attribute__((section("__TEXT, initcode")));
2d21ac55 81
91447636 82void pfslowtimo(void *);
1c79356b 83
2d21ac55
A
84struct protosw *pffindprotonotype(int, int);
85struct protosw *pffindprotonotype_locked(int , int , int);
86struct domain *pffinddomain(int);
6d2010ae 87static void net_update_uptime(void);
2d21ac55 88
1c79356b
A
89/*
90 * Add/delete 'domain': Link structure into system list,
91 * invoke the domain init, and then the proto inits.
92 * To delete, just remove from the list (dom_refs must be zero)
93 */
94
91447636
A
95lck_grp_t *domain_proto_mtx_grp;
96lck_attr_t *domain_proto_mtx_attr;
97static lck_grp_attr_t *domain_proto_mtx_grp_attr;
98lck_mtx_t *domain_proto_mtx;
99extern int do_reclaim;
1c79356b 100
b0d623f7
A
101extern sysctlfn net_sysctl;
102
6d2010ae
A
103static u_int64_t uptime;
104
105#ifdef INET6
106extern void ip6_fin(void);
107#endif
108
2d21ac55
A
109static void
110init_proto(struct protosw *pr)
111{
112 TAILQ_INIT(&pr->pr_filter_head);
113 if (pr->pr_init)
114 (*pr->pr_init)();
115
116 /* Make sure pr_init isn't called again!! */
117 pr->pr_init = 0;
118}
119
120void
121init_domain(struct domain *dp)
1c79356b
A
122{
123 struct protosw *pr;
124
91447636
A
125 if ((dp->dom_mtx = lck_mtx_alloc_init(domain_proto_mtx_grp, domain_proto_mtx_attr)) == NULL) {
126 printf("init_domain: can't init domain mtx for domain=%s\n", dp->dom_name);
127 return; /* we have a problem... */
128 }
129
1c79356b
A
130 if (dp->dom_init)
131 (*dp->dom_init)();
132
133 /* and then init the currently installed protos in this domain */
134
135 for (pr = dp->dom_protosw; pr; pr = pr->pr_next) {
136 if (pr->pr_usrreqs == 0)
137 panic("domaininit: %ssw[%d] has no usrreqs!",
138 dp->dom_name,
139 (int)(pr - dp->dom_protosw));
140
6d2010ae
A
141#if __APPLE__
142 /*
143 * Warn that pr_fasttimo (now pr_unused) is deprecated since rdar://7617868
144 */
145 if (pr->pr_unused != NULL) {
146 printf("init_domain: warning %s, proto %d: pr_fasttimo is deprecated and won't be called\n",
147 dp->dom_name, pr->pr_protocol);
148 }
149#endif
150
2d21ac55
A
151 init_proto(pr);
152
1c79356b
A
153 }
154
155 /* Recompute for new protocol */
156 if (max_linkhdr < 16) /* XXX - Sheesh; everything's ether? */
157 max_linkhdr = 16;
158 if (dp->dom_protohdrlen > max_protohdr)
159 max_protohdr = dp->dom_protohdrlen;
160 max_hdr = max_linkhdr + max_protohdr;
161 max_datalen = MHLEN - max_hdr;
162}
163
2d21ac55 164void
6d2010ae
A
165prepend_domain(struct domain *dp)
166{
91447636 167 lck_mtx_assert(domain_proto_mtx, LCK_MTX_ASSERT_OWNED);
1c79356b
A
168 dp->dom_next = domains;
169 domains = dp;
170}
171
172void
2d21ac55
A
173net_add_domain(struct domain *dp)
174{
1c79356b
A
175 kprintf("Adding domain %s (family %d)\n", dp->dom_name,
176 dp->dom_family);
177 /* First, link in the domain */
1c79356b 178
91447636 179 lck_mtx_lock(domain_proto_mtx);
6d2010ae 180 prepend_domain(dp);
1c79356b
A
181
182 init_domain(dp);
91447636 183 lck_mtx_unlock(domain_proto_mtx);
1c79356b 184
1c79356b
A
185}
186
187int
2d21ac55 188net_del_domain(struct domain *dp)
1c79356b 189{ register struct domain *dp1, *dp2;
91447636
A
190 register int retval = 0;
191
192 lck_mtx_lock(domain_proto_mtx);
1c79356b 193
91447636
A
194 if (dp->dom_refs) {
195 lck_mtx_unlock(domain_proto_mtx);
1c79356b 196 return(EBUSY);
91447636 197 }
1c79356b
A
198
199 for (dp2 = NULL, dp1 = domains; dp1; dp2 = dp1, dp1 = dp1->dom_next)
200 { if (dp == dp1)
201 break;
202 }
203 if (dp1)
204 { if (dp2)
205 dp2->dom_next = dp1->dom_next;
206 else
207 domains = dp1->dom_next;
208 } else
209 retval = EPFNOSUPPORT;
91447636 210 lck_mtx_unlock(domain_proto_mtx);
1c79356b
A
211
212 return(retval);
213}
214
215/*
216 * net_add_proto - link a protosw into a domain's protosw chain
91447636
A
217 *
218 * note: protocols must use their own domain lock before calling net_add_proto
1c79356b
A
219 */
220int
2d21ac55 221net_add_proto(struct protosw *pp, struct domain *dp)
1c79356b 222{ register struct protosw *pp1, *pp2;
1c79356b 223
1c79356b
A
224 for (pp2 = NULL, pp1 = dp->dom_protosw; pp1; pp1 = pp1->pr_next)
225 { if (pp1->pr_type == pp->pr_type &&
226 pp1->pr_protocol == pp->pr_protocol) {
1c79356b
A
227 return(EEXIST);
228 }
229 pp2 = pp1;
230 }
231 if (pp2 == NULL)
232 dp->dom_protosw = pp;
233 else
234 pp2->pr_next = pp;
1c79356b 235
2d21ac55
A
236 init_proto(pp);
237
1c79356b
A
238 return(0);
239}
240
241/*
242 * net_del_proto - remove a protosw from a domain's protosw chain.
243 * Search the protosw chain for the element with matching data.
244 * Then unlink and return.
91447636
A
245 *
246 * note: protocols must use their own domain lock before calling net_del_proto
1c79356b
A
247 */
248int
2d21ac55
A
249net_del_proto(int type, int protocol, struct domain *dp)
250{
251 register struct protosw *pp1, *pp2;
1c79356b 252
1c79356b
A
253 for (pp2 = NULL, pp1 = dp->dom_protosw; pp1; pp1 = pp1->pr_next)
254 { if (pp1->pr_type == type &&
255 pp1->pr_protocol == protocol)
256 break;
257 pp2 = pp1;
258 }
259 if (pp1 == NULL) {
1c79356b
A
260 return(ENXIO);
261 }
262 if (pp2)
263 pp2->pr_next = pp1->pr_next;
264 else
265 dp->dom_protosw = pp1->pr_next;
1c79356b
A
266 return(0);
267}
268
269
1c79356b 270#if NS
2d21ac55 271extern struct domain nsdomain;
1c79356b
A
272#endif
273#if ISO
2d21ac55 274extern struct domain isodomain;
1c79356b
A
275#endif
276#if CCITT
2d21ac55 277extern struct domain ccittdomain;
1c79356b
A
278#endif
279
280#if NETAT
2d21ac55 281extern struct domain atalkdomain;
1c79356b
A
282#endif
283#if INET6
2d21ac55 284extern struct domain inet6domain;
1c79356b
A
285#endif
286#if IPSEC
2d21ac55 287extern struct domain keydomain;
1c79356b
A
288#endif
289
2d21ac55
A
290extern struct domain routedomain, ndrvdomain, inetdomain;
291extern struct domain systemdomain;
292
293void
294domaininit(void)
295{
296 register struct domain *dp;
297
91447636
A
298 /*
299 * allocate lock group attribute and group for domain mutexes
300 */
301 domain_proto_mtx_grp_attr = lck_grp_attr_alloc_init();
91447636
A
302
303 domain_proto_mtx_grp = lck_grp_alloc_init("domain", domain_proto_mtx_grp_attr);
304
305 /*
306 * allocate the lock attribute for per domain mutexes
307 */
308 domain_proto_mtx_attr = lck_attr_alloc_init();
91447636
A
309
310 if ((domain_proto_mtx = lck_mtx_alloc_init(domain_proto_mtx_grp, domain_proto_mtx_attr)) == NULL) {
311 printf("domaininit: can't init domain mtx for domain list\n");
312 return; /* we have a problem... */
313 }
1c79356b
A
314 /*
315 * Add all the static domains to the domains list
316 */
317
91447636
A
318 lck_mtx_lock(domain_proto_mtx);
319
6d2010ae
A
320 prepend_domain(&localdomain);
321 prepend_domain(&inetdomain);
1c79356b 322#if NETAT
6d2010ae 323 prepend_domain(&atalkdomain);
1c79356b
A
324#endif
325#if INET6
6d2010ae 326 prepend_domain(&inet6domain);
1c79356b 327#endif
6d2010ae
A
328 prepend_domain(&routedomain);
329
1c79356b 330#if IPSEC
6d2010ae 331 prepend_domain(&keydomain);
1c79356b
A
332#endif
333
334#if NS
6d2010ae 335 prepend_domain(&nsdomain);
1c79356b
A
336#endif
337#if ISO
6d2010ae 338 prepend_domain(&isodomain);
1c79356b
A
339#endif
340#if CCITT
6d2010ae 341 prepend_domain(&ccittdomain);
1c79356b 342#endif
6d2010ae 343 prepend_domain(&ndrvdomain);
1c79356b 344
6d2010ae 345 prepend_domain(&systemdomain);
1c79356b
A
346
347 /*
348 * Now ask them all to init (XXX including the routing domain,
349 * see above)
350 */
351 for (dp = domains; dp; dp = dp->dom_next)
352 init_domain(dp);
353
91447636 354 lck_mtx_unlock(domain_proto_mtx);
1c79356b 355 timeout(pfslowtimo, NULL, 1);
1c79356b
A
356}
357
6d2010ae
A
358void
359domainfin(void)
360{
361#ifdef INET6
362 ip6_fin();
363#endif
364}
365
2d21ac55
A
366static __inline__ struct domain *
367pffinddomain_locked(int pf)
368{
369 struct domain *dp;
370
371 dp = domains;
372 while (dp != NULL)
373 { if (dp->dom_family == pf) {
374 break;
375 }
376 dp = dp->dom_next;
377 }
378 return (dp);
379}
380
1c79356b 381struct protosw *
2d21ac55 382pffindtype(int family, int type)
1c79356b
A
383{
384 register struct domain *dp;
385 register struct protosw *pr;
386
91447636
A
387 lck_mtx_assert(domain_proto_mtx, LCK_MTX_ASSERT_NOTOWNED);
388 lck_mtx_lock(domain_proto_mtx);
2d21ac55
A
389 dp = pffinddomain_locked(family);
390 if (dp == NULL) {
91447636 391 lck_mtx_unlock(domain_proto_mtx);
2d21ac55
A
392 return (NULL);
393 }
1c79356b 394 for (pr = dp->dom_protosw; pr; pr = pr->pr_next)
91447636
A
395 if (pr->pr_type && pr->pr_type == type) {
396 lck_mtx_unlock(domain_proto_mtx);
1c79356b 397 return (pr);
91447636
A
398 }
399 lck_mtx_unlock(domain_proto_mtx);
1c79356b
A
400 return (0);
401}
402
403struct domain *
404pffinddomain(int pf)
2d21ac55
A
405{
406 struct domain *dp;
1c79356b 407
91447636
A
408 lck_mtx_assert(domain_proto_mtx, LCK_MTX_ASSERT_NOTOWNED);
409 lck_mtx_lock(domain_proto_mtx);
2d21ac55 410 dp = pffinddomain_locked(pf);
91447636 411 lck_mtx_unlock(domain_proto_mtx);
1c79356b 412 return(dp);
91447636 413 }
1c79356b
A
414
415struct protosw *
2d21ac55 416pffindproto(int family, int protocol, int type)
91447636
A
417{
418 register struct protosw *pr;
419 lck_mtx_assert(domain_proto_mtx, LCK_MTX_ASSERT_NOTOWNED);
420 lck_mtx_lock(domain_proto_mtx);
421 pr = pffindproto_locked(family, protocol, type);
422 lck_mtx_unlock(domain_proto_mtx);
423 return (pr);
424}
425
426struct protosw *
2d21ac55 427pffindproto_locked(int family, int protocol, int type)
1c79356b
A
428{
429 register struct domain *dp;
430 register struct protosw *pr;
431 struct protosw *maybe = 0;
432
433 if (family == 0)
434 return (0);
2d21ac55
A
435 dp = pffinddomain_locked(family);
436 if (dp == NULL) {
437 return (NULL);
438 }
1c79356b
A
439 for (pr = dp->dom_protosw; pr; pr = pr->pr_next) {
440 if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
441 return (pr);
442
443 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
444 pr->pr_protocol == 0 && maybe == (struct protosw *)0)
445 maybe = pr;
446 }
447 return (maybe);
448}
449
2d21ac55
A
450struct protosw *
451pffindprotonotype_locked(int family, int protocol, __unused int type)
452{
453 register struct domain *dp;
454 register struct protosw *pr;
455
456 if (family == 0)
457 return (0);
458 dp = pffinddomain_locked(family);
459 if (dp == NULL) {
460 return (NULL);
461 }
462 for (pr = dp->dom_protosw; pr; pr = pr->pr_next) {
463 if (pr->pr_protocol == protocol) {
464 return (pr);
465 }
466 }
467 return (NULL);
468}
469
470struct protosw *
471pffindprotonotype(int family, int protocol)
472{
473 register struct protosw *pr;
474 if (protocol == 0) {
475 return (NULL);
476 }
477 lck_mtx_assert(domain_proto_mtx, LCK_MTX_ASSERT_NOTOWNED);
478 lck_mtx_lock(domain_proto_mtx);
479 pr = pffindprotonotype_locked(family, protocol, 0);
480 lck_mtx_unlock(domain_proto_mtx);
481 return (pr);
482}
483
1c79356b 484int
91447636 485net_sysctl(int *name, u_int namelen, user_addr_t oldp, size_t *oldlenp,
2d21ac55 486 user_addr_t newp, size_t newlen, __unused struct proc *p)
1c79356b
A
487{
488 register struct domain *dp;
489 register struct protosw *pr;
91447636 490 int family, protocol, error;
1c79356b
A
491
492 /*
493 * All sysctl names at this level are nonterminal;
494 * next two components are protocol family and protocol number,
495 * then at least one addition component.
496 */
497 if (namelen < 3)
498 return (EISDIR); /* overloaded */
499 family = name[0];
500 protocol = name[1];
501
502 if (family == 0)
503 return (0);
91447636 504 lck_mtx_lock(domain_proto_mtx);
1c79356b
A
505 for (dp = domains; dp; dp = dp->dom_next)
506 if (dp->dom_family == family)
507 goto found;
91447636 508 lck_mtx_unlock(domain_proto_mtx);
1c79356b
A
509 return (ENOPROTOOPT);
510found:
511 for (pr = dp->dom_protosw; pr; pr = pr->pr_next)
91447636
A
512 if (pr->pr_protocol == protocol && pr->pr_sysctl) {
513 error = (*pr->pr_sysctl)(name + 2, namelen - 2,
b0d623f7 514 (void *)(uintptr_t)oldp, oldlenp, (void *)(uintptr_t)newp, newlen);
91447636
A
515 lck_mtx_unlock(domain_proto_mtx);
516 return (error);
517 }
518 lck_mtx_unlock(domain_proto_mtx);
1c79356b
A
519 return (ENOPROTOOPT);
520}
521
522void
2d21ac55 523pfctlinput(int cmd, struct sockaddr *sa)
1c79356b 524{
9bccf70c
A
525 pfctlinput2(cmd, sa, (void*)0);
526}
527
528void
2d21ac55 529pfctlinput2(int cmd, struct sockaddr *sa, void *ctlparam)
9bccf70c
A
530{
531 struct domain *dp;
532 struct protosw *pr;
1c79356b 533
9bccf70c
A
534 if (!sa)
535 return;
91447636
A
536
537 lck_mtx_lock(domain_proto_mtx);
1c79356b
A
538 for (dp = domains; dp; dp = dp->dom_next)
539 for (pr = dp->dom_protosw; pr; pr = pr->pr_next)
540 if (pr->pr_ctlinput)
9bccf70c 541 (*pr->pr_ctlinput)(cmd, sa, ctlparam);
91447636 542 lck_mtx_unlock(domain_proto_mtx);
1c79356b
A
543}
544
545void
2d21ac55 546pfslowtimo(__unused void *arg)
1c79356b
A
547{
548 register struct domain *dp;
549 register struct protosw *pr;
1c79356b 550
6d2010ae
A
551 /*
552 * Update coarse-grained networking timestamp (in sec.); the idea
553 * is to piggy-back on the periodic slow timeout callout to update
554 * the counter returnable via net_uptime().
555 */
556 net_update_uptime();
557
91447636
A
558 lck_mtx_lock(domain_proto_mtx);
559 for (dp = domains; dp; dp = dp->dom_next)
560 for (pr = dp->dom_protosw; pr; pr = pr->pr_next) {
1c79356b
A
561 if (pr->pr_slowtimo)
562 (*pr->pr_slowtimo)();
d1ecb069
A
563 if ((do_reclaim || (pr->pr_flags & PR_AGGDRAIN)) &&
564 pr->pr_drain)
91447636
A
565 (*pr->pr_drain)();
566 }
567 do_reclaim = 0;
568 lck_mtx_unlock(domain_proto_mtx);
2d21ac55 569 timeout(pfslowtimo, NULL, hz/PR_SLOWHZ);
1c79356b
A
570}
571
6d2010ae
A
572static void
573net_update_uptime(void)
1c79356b 574{
6d2010ae 575 struct timeval tv;
1c79356b 576
6d2010ae
A
577 microuptime(&tv);
578 uptime = tv.tv_sec;
579}
580
581/*
582 * An alternative way to obtain the coarse-grained uptime (in seconds)
583 * for networking code which do not require high-precision timestamp,
584 * as this is significantly cheaper than microuptime().
585 */
586u_int64_t
587net_uptime(void)
588{
589 /* If we get here before pfslowtimo() fires for the first time */
590 if (uptime == 0)
591 net_update_uptime();
592
593 return (uptime);
1c79356b 594}