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