]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_domain.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / bsd / kern / uipc_domain.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
24 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 /*
26 * Copyright (c) 1982, 1986, 1993
27 * The Regents of the University of California. All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
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.
44 *
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
55 * SUCH DAMAGE.
56 *
57 * @(#)uipc_domain.c 8.3 (Berkeley) 2/14/95
58 */
59
60 #include <sys/param.h>
61 #include <sys/socket.h>
62 #include <sys/protosw.h>
63 #include <sys/domain.h>
64 #include <sys/mbuf.h>
65 #include <sys/time.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>
72
73 void pffasttimo(void *);
74 void pfslowtimo(void *);
75
76 /*
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)
80 */
81
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;
87
88 void init_domain(register struct domain *dp)
89 {
90 struct protosw *pr;
91
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... */
95 }
96
97 if (dp->dom_init)
98 (*dp->dom_init)();
99
100 /* and then init the currently installed protos in this domain */
101
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!",
105 dp->dom_name,
106 (int)(pr - dp->dom_protosw));
107
108 if (pr->pr_init)
109 (*pr->pr_init)();
110 }
111
112 /* Recompute for new protocol */
113 if (max_linkhdr < 16) /* XXX - Sheesh; everything's ether? */
114 max_linkhdr = 16;
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;
119 }
120
121 void concat_domain(struct domain *dp)
122 {
123 lck_mtx_assert(domain_proto_mtx, LCK_MTX_ASSERT_OWNED);
124 dp->dom_next = domains;
125 domains = dp;
126 }
127
128 void
129 net_add_domain(register struct domain *dp)
130 { register struct protosw *pr;
131
132 kprintf("Adding domain %s (family %d)\n", dp->dom_name,
133 dp->dom_family);
134 /* First, link in the domain */
135
136 lck_mtx_lock(domain_proto_mtx);
137 concat_domain(dp);
138
139 init_domain(dp);
140 lck_mtx_unlock(domain_proto_mtx);
141
142 }
143
144 int
145 net_del_domain(register struct domain *dp)
146 { register struct domain *dp1, *dp2;
147 register int retval = 0;
148
149 lck_mtx_lock(domain_proto_mtx);
150
151 if (dp->dom_refs) {
152 lck_mtx_unlock(domain_proto_mtx);
153 return(EBUSY);
154 }
155
156 for (dp2 = NULL, dp1 = domains; dp1; dp2 = dp1, dp1 = dp1->dom_next)
157 { if (dp == dp1)
158 break;
159 }
160 if (dp1)
161 { if (dp2)
162 dp2->dom_next = dp1->dom_next;
163 else
164 domains = dp1->dom_next;
165 } else
166 retval = EPFNOSUPPORT;
167 lck_mtx_unlock(domain_proto_mtx);
168
169 return(retval);
170 }
171
172 /*
173 * net_add_proto - link a protosw into a domain's protosw chain
174 *
175 * note: protocols must use their own domain lock before calling net_add_proto
176 */
177 int
178 net_add_proto(register struct protosw *pp,
179 register struct domain *dp)
180 { register struct protosw *pp1, *pp2;
181
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) {
185 return(EEXIST);
186 }
187 pp2 = pp1;
188 }
189 if (pp2 == NULL)
190 dp->dom_protosw = pp;
191 else
192 pp2->pr_next = pp;
193 pp->pr_next = NULL;
194 TAILQ_INIT(&pp->pr_filter_head);
195 if (pp->pr_init)
196 (*pp->pr_init)();
197
198 /* Make sure pr_init isn't called again!! */
199 pp->pr_init = 0;
200 return(0);
201 }
202
203 /*
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.
207 *
208 * note: protocols must use their own domain lock before calling net_del_proto
209 */
210 int
211 net_del_proto(register int type,
212 register int protocol,
213 register struct domain *dp)
214 { register struct protosw *pp1, *pp2;
215
216 for (pp2 = NULL, pp1 = dp->dom_protosw; pp1; pp1 = pp1->pr_next)
217 { if (pp1->pr_type == type &&
218 pp1->pr_protocol == protocol)
219 break;
220 pp2 = pp1;
221 }
222 if (pp1 == NULL) {
223 return(ENXIO);
224 }
225 if (pp2)
226 pp2->pr_next = pp1->pr_next;
227 else
228 dp->dom_protosw = pp1->pr_next;
229 return(0);
230 }
231
232
233 void
234 domaininit()
235 { register struct domain *dp;
236 register struct protosw *pr;
237 extern struct domain localdomain, routedomain, ndrvdomain, inetdomain;
238 extern struct domain systemdomain;
239 #if NS
240 extern struct domain nsdomain;
241 #endif
242 #if ISO
243 extern struct domain isodomain;
244 #endif
245 #if CCITT
246 extern struct domain ccittdomain;
247 #endif
248
249 #if NETAT
250 extern struct domain atalkdomain;
251 #endif
252 #if INET6
253 extern struct domain inet6domain;
254 #endif
255 #if IPSEC
256 extern struct domain keydomain;
257 #endif
258
259 /*
260 * allocate lock group attribute and group for domain mutexes
261 */
262 domain_proto_mtx_grp_attr = lck_grp_attr_alloc_init();
263 lck_grp_attr_setdefault(domain_proto_mtx_grp_attr);
264
265 domain_proto_mtx_grp = lck_grp_alloc_init("domain", domain_proto_mtx_grp_attr);
266
267 /*
268 * allocate the lock attribute for per domain mutexes
269 */
270 domain_proto_mtx_attr = lck_attr_alloc_init();
271 lck_attr_setdefault(domain_proto_mtx_attr);
272
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... */
276 }
277 /*
278 * Add all the static domains to the domains list
279 */
280
281 lck_mtx_lock(domain_proto_mtx);
282
283 concat_domain(&localdomain);
284 concat_domain(&routedomain);
285 concat_domain(&inetdomain);
286 #if NETAT
287 concat_domain(&atalkdomain);
288 #endif
289 #if INET6
290 concat_domain(&inet6domain);
291 #endif
292 #if IPSEC
293 concat_domain(&keydomain);
294 #endif
295
296 #if NS
297 concat_domain(&nsdomain);
298 #endif
299 #if ISO
300 concat_domain(&isodomain);
301 #endif
302 #if CCITT
303 concat_domain(&ccittdomain);
304 #endif
305 concat_domain(&ndrvdomain);
306
307 concat_domain(&systemdomain);
308
309 /*
310 * Now ask them all to init (XXX including the routing domain,
311 * see above)
312 */
313 for (dp = domains; dp; dp = dp->dom_next)
314 init_domain(dp);
315
316 lck_mtx_unlock(domain_proto_mtx);
317 timeout(pffasttimo, NULL, 1);
318 timeout(pfslowtimo, NULL, 1);
319 }
320
321 struct protosw *
322 pffindtype(family, type)
323 int family, type;
324 {
325 register struct domain *dp;
326 register struct protosw *pr;
327
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)
332 goto found;
333 lck_mtx_unlock(domain_proto_mtx);
334 return (0);
335 found:
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);
339 return (pr);
340 }
341 lck_mtx_unlock(domain_proto_mtx);
342 return (0);
343 }
344
345 struct domain *
346 pffinddomain(int pf)
347 { struct domain *dp;
348
349 lck_mtx_assert(domain_proto_mtx, LCK_MTX_ASSERT_NOTOWNED);
350 lck_mtx_lock(domain_proto_mtx);
351 dp = domains;
352 while (dp)
353 { if (dp->dom_family == pf) {
354 lck_mtx_unlock(domain_proto_mtx);
355 return(dp);
356 }
357 dp = dp->dom_next;
358 }
359 lck_mtx_unlock(domain_proto_mtx);
360 return(NULL);
361 }
362
363 struct protosw *
364 pffindproto(family, protocol, type)
365 int family, protocol, type;
366 {
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);
372 return (pr);
373 }
374
375 struct protosw *
376 pffindproto_locked(family, protocol, type)
377 int family, protocol, type;
378 {
379 register struct domain *dp;
380 register struct protosw *pr;
381 struct protosw *maybe = 0;
382
383 if (family == 0)
384 return (0);
385 for (dp = domains; dp; dp = dp->dom_next)
386 if (dp->dom_family == family)
387 goto found;
388 return (0);
389 found:
390 for (pr = dp->dom_protosw; pr; pr = pr->pr_next) {
391 if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
392 return (pr);
393
394 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
395 pr->pr_protocol == 0 && maybe == (struct protosw *)0)
396 maybe = pr;
397 }
398 return (maybe);
399 }
400
401 int
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)
404 {
405 register struct domain *dp;
406 register struct protosw *pr;
407 int family, protocol, error;
408
409 /*
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.
413 */
414 if (namelen < 3)
415 return (EISDIR); /* overloaded */
416 family = name[0];
417 protocol = name[1];
418
419 if (family == 0)
420 return (0);
421 lck_mtx_lock(domain_proto_mtx);
422 for (dp = domains; dp; dp = dp->dom_next)
423 if (dp->dom_family == family)
424 goto found;
425 lck_mtx_unlock(domain_proto_mtx);
426 return (ENOPROTOOPT);
427 found:
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);
433 return (error);
434 }
435 lck_mtx_unlock(domain_proto_mtx);
436 return (ENOPROTOOPT);
437 }
438
439 void
440 pfctlinput(cmd, sa)
441 int cmd;
442 struct sockaddr *sa;
443 {
444 pfctlinput2(cmd, sa, (void*)0);
445 }
446
447 void
448 pfctlinput2(cmd, sa, ctlparam)
449 int cmd;
450 struct sockaddr *sa;
451 void *ctlparam;
452 {
453 struct domain *dp;
454 struct protosw *pr;
455
456 if (!sa)
457 return;
458
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)
462 if (pr->pr_ctlinput)
463 (*pr->pr_ctlinput)(cmd, sa, ctlparam);
464 lck_mtx_unlock(domain_proto_mtx);
465 }
466
467 void
468 pfslowtimo(arg)
469 void *arg;
470 {
471 register struct domain *dp;
472 register struct protosw *pr;
473
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) {
477 if (pr->pr_slowtimo)
478 (*pr->pr_slowtimo)();
479 if (do_reclaim && pr->pr_drain)
480 (*pr->pr_drain)();
481 }
482 do_reclaim = 0;
483 lck_mtx_unlock(domain_proto_mtx);
484 timeout(pfslowtimo, NULL, hz/2);
485
486 }
487
488 void
489 pffasttimo(arg)
490 void *arg;
491 {
492 register struct domain *dp;
493 register struct protosw *pr;
494
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)
498 if (pr->pr_fasttimo)
499 (*pr->pr_fasttimo)();
500 lck_mtx_unlock(domain_proto_mtx);
501 timeout(pffasttimo, NULL, hz/5);
502 }