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