1 /*-
2 * Copyright (c) 1982, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)uipc_domain.c 8.2 (Berkeley) 10/18/93
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD: releng/6.2/sys/kern/uipc_domain.c 164286 2006-11-14 20:42:41Z cvs2svn $");
34
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/protosw.h>
38 #include <sys/domain.h>
39 #include <sys/eventhandler.h>
40 #include <sys/mbuf.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/socketvar.h>
45 #include <sys/systm.h>
46 #include <vm/uma.h>
47
48 /*
49 * System initialization
50 *
51 * Note: domain initialization takes place on a per domain basis
52 * as a result of traversing a SYSINIT linker set. Most likely,
53 * each domain would want to call DOMAIN_SET(9) itself, which
54 * would cause the domain to be added just after domaininit()
55 * is called during startup.
56 *
57 * See DOMAIN_SET(9) for details on its use.
58 */
59
60 static void domaininit(void *);
61 SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL)
62
63 static void domainfinalize(void *);
64 SYSINIT(domainfin, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, domainfinalize,
65 NULL)
66
67 static struct callout pffast_callout;
68 static struct callout pfslow_callout;
69
70 static void pffasttimo(void *);
71 static void pfslowtimo(void *);
72
73 struct domain *domains; /* registered protocol domains */
74 int domain_init_status = 0;
75 struct mtx dom_mtx; /* domain list lock */
76 MTX_SYSINIT(domain, &dom_mtx, "domain list", MTX_DEF);
77
78 /*
79 * Dummy protocol specific user requests function pointer array.
80 * All functions return EOPNOTSUPP.
81 */
82 struct pr_usrreqs nousrreqs = {
83 .pru_abort = pru_abort_notsupp,
84 .pru_accept = pru_accept_notsupp,
85 .pru_attach = pru_attach_notsupp,
86 .pru_bind = pru_bind_notsupp,
87 .pru_connect = pru_connect_notsupp,
88 .pru_connect2 = pru_connect2_notsupp,
89 .pru_control = pru_control_notsupp,
90 .pru_detach = pru_detach_notsupp,
91 .pru_disconnect = pru_disconnect_notsupp,
92 .pru_listen = pru_listen_notsupp,
93 .pru_peeraddr = pru_peeraddr_notsupp,
94 .pru_rcvd = pru_rcvd_notsupp,
95 .pru_rcvoob = pru_rcvoob_notsupp,
96 .pru_send = pru_send_notsupp,
97 .pru_sense = pru_sense_null,
98 .pru_shutdown = pru_shutdown_notsupp,
99 .pru_sockaddr = pru_sockaddr_notsupp,
100 .pru_sosend = pru_sosend_notsupp,
101 .pru_soreceive = pru_soreceive_notsupp,
102 .pru_sopoll = pru_sopoll_notsupp,
103 .pru_sosetlabel = pru_sosetlabel_null
104 };
105
106 static void
107 protosw_init(struct protosw *pr)
108 {
109 struct pr_usrreqs *pu;
110
111 pu = pr->pr_usrreqs;
112 KASSERT(pu != NULL, ("protosw_init: %ssw[%d] has no usrreqs!",
113 pr->pr_domain->dom_name,
114 (int)(pr - pr->pr_domain->dom_protosw)));
115
116 #define DEFAULT(foo, bar) if ((foo) == NULL) (foo) = (bar)
117 DEFAULT(pu->pru_accept, pru_accept_notsupp);
118 DEFAULT(pu->pru_connect, pru_connect_notsupp);
119 DEFAULT(pu->pru_connect2, pru_connect2_notsupp);
120 DEFAULT(pu->pru_control, pru_control_notsupp);
121 DEFAULT(pu->pru_listen, pru_listen_notsupp);
122 DEFAULT(pu->pru_rcvd, pru_rcvd_notsupp);
123 DEFAULT(pu->pru_rcvoob, pru_rcvoob_notsupp);
124 DEFAULT(pu->pru_sense, pru_sense_null);
125 DEFAULT(pu->pru_sosend, sosend);
126 DEFAULT(pu->pru_soreceive, soreceive);
127 DEFAULT(pu->pru_sopoll, sopoll);
128 DEFAULT(pu->pru_sosetlabel, pru_sosetlabel_null);
129 #undef DEFAULT
130 if (pr->pr_init)
131 (*pr->pr_init)();
132 }
133
134 /*
135 * Add a new protocol domain to the list of supported domains
136 * Note: you cant unload it again because a socket may be using it.
137 * XXX can't fail at this time.
138 */
139 static void
140 net_init_domain(struct domain *dp)
141 {
142 struct protosw *pr;
143
144 if (dp->dom_init)
145 (*dp->dom_init)();
146 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
147 protosw_init(pr);
148 /*
149 * update global information about maximums
150 */
151 max_hdr = max_linkhdr + max_protohdr;
152 max_datalen = MHLEN - max_hdr;
153 if (max_datalen < 1)
154 panic("%s: max_datalen < 1", __func__);
155 }
156
157 /*
158 * Add a new protocol domain to the list of supported domains
159 * Note: you cant unload it again because a socket may be using it.
160 * XXX can't fail at this time.
161 */
162 void
163 net_add_domain(void *data)
164 {
165 struct domain *dp;
166
167 dp = (struct domain *)data;
168 mtx_lock(&dom_mtx);
169 dp->dom_next = domains;
170 domains = dp;
171
172 KASSERT(domain_init_status >= 1,
173 ("attempt to net_add_domain(%s) before domaininit()",
174 dp->dom_name));
175 #ifndef INVARIANTS
176 if (domain_init_status < 1)
177 printf("WARNING: attempt to net_add_domain(%s) before "
178 "domaininit()\n", dp->dom_name);
179 #endif
180 #ifdef notyet
181 KASSERT(domain_init_status < 2,
182 ("attempt to net_add_domain(%s) after domainfinalize()",
183 dp->dom_name));
184 #else
185 #ifdef DIAGNOSTIC
186 if (domain_init_status >= 2)
187 printf("WARNING: attempt to net_add_domain(%s) after "
188 "domainfinalize()\n", dp->dom_name);
189 #endif
190 #endif
191 mtx_unlock(&dom_mtx);
192 net_init_domain(dp);
193 }
194
195 static void
196 socket_zone_change(void *tag)
197 {
198
199 uma_zone_set_max(socket_zone, maxsockets);
200 }
201
202 /* ARGSUSED*/
203 static void
204 domaininit(void *dummy)
205 {
206 /*
207 * Before we do any setup, make sure to initialize the
208 * zone allocator we get struct sockets from.
209 */
210
211 socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL,
212 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
213 uma_zone_set_max(socket_zone, maxsockets);
214 EVENTHANDLER_REGISTER(maxsockets_change, socket_zone_change, NULL,
215 EVENTHANDLER_PRI_FIRST);
216
217 if (max_linkhdr < 16) /* XXX */
218 max_linkhdr = 16;
219
220 if (debug_mpsafenet) {
221 callout_init(&pffast_callout, CALLOUT_MPSAFE);
222 callout_init(&pfslow_callout, CALLOUT_MPSAFE);
223 } else {
224 callout_init(&pffast_callout, 0);
225 callout_init(&pfslow_callout, 0);
226 }
227
228 mtx_lock(&dom_mtx);
229 KASSERT(domain_init_status == 0, ("domaininit called too late!"));
230 domain_init_status = 1;
231 mtx_unlock(&dom_mtx);
232 }
233
234 /* ARGSUSED*/
235 static void
236 domainfinalize(void *dummy)
237 {
238 mtx_lock(&dom_mtx);
239 KASSERT(domain_init_status == 1, ("domainfinalize called too late!"));
240 domain_init_status = 2;
241 mtx_unlock(&dom_mtx);
242
243 callout_reset(&pffast_callout, 1, pffasttimo, NULL);
244 callout_reset(&pfslow_callout, 1, pfslowtimo, NULL);
245 }
246
247 struct protosw *
248 pffindtype(family, type)
249 int family;
250 int type;
251 {
252 register struct domain *dp;
253 register struct protosw *pr;
254
255 for (dp = domains; dp; dp = dp->dom_next)
256 if (dp->dom_family == family)
257 goto found;
258 return (0);
259 found:
260 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
261 if (pr->pr_type && pr->pr_type == type)
262 return (pr);
263 return (0);
264 }
265
266 struct protosw *
267 pffindproto(family, protocol, type)
268 int family;
269 int protocol;
270 int type;
271 {
272 register struct domain *dp;
273 register struct protosw *pr;
274 struct protosw *maybe = 0;
275
276 if (family == 0)
277 return (0);
278 for (dp = domains; dp; dp = dp->dom_next)
279 if (dp->dom_family == family)
280 goto found;
281 return (0);
282 found:
283 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
284 if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
285 return (pr);
286
287 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
288 pr->pr_protocol == 0 && maybe == (struct protosw *)0)
289 maybe = pr;
290 }
291 return (maybe);
292 }
293
294 /*
295 * The caller must make sure that the new protocol is fully set up and ready to
296 * accept requests before it is registered.
297 */
298 int
299 pf_proto_register(family, npr)
300 int family;
301 struct protosw *npr;
302 {
303 struct domain *dp;
304 struct protosw *pr, *fpr;
305
306 /* Sanity checks. */
307 if (family == 0)
308 return (EPFNOSUPPORT);
309 if (npr->pr_type == 0)
310 return (EPROTOTYPE);
311 if (npr->pr_protocol == 0)
312 return (EPROTONOSUPPORT);
313 if (npr->pr_usrreqs == NULL)
314 return (ENXIO);
315
316 /* Try to find the specified domain based on the family. */
317 for (dp = domains; dp; dp = dp->dom_next)
318 if (dp->dom_family == family)
319 goto found;
320 return (EPFNOSUPPORT);
321
322 found:
323 /* Initialize backpointer to struct domain. */
324 npr->pr_domain = dp;
325 fpr = NULL;
326
327 /*
328 * Protect us against races when two protocol registrations for
329 * the same protocol happen at the same time.
330 */
331 mtx_lock(&Giant);
332
333 /* The new protocol must not yet exist. */
334 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
335 if ((pr->pr_type == npr->pr_type) &&
336 (pr->pr_protocol == npr->pr_protocol)) {
337 mtx_unlock(&Giant);
338 return (EEXIST); /* XXX: Check only protocol? */
339 }
340 /* While here, remember the first free spacer. */
341 if ((fpr == NULL) && (pr->pr_protocol == PROTO_SPACER))
342 fpr = pr;
343 }
344
345 /* If no free spacer is found we can't add the new protocol. */
346 if (fpr == NULL) {
347 mtx_unlock(&Giant);
348 return (ENOMEM);
349 }
350
351 /* Copy the new struct protosw over the spacer. */
352 bcopy(npr, fpr, sizeof(*fpr));
353
354 /* Job is done, no more protection required. */
355 mtx_unlock(&Giant);
356
357 /* Initialize and activate the protocol. */
358 protosw_init(fpr);
359
360 return (0);
361 }
362
363 /*
364 * The caller must make sure the protocol and its functions correctly shut down
365 * all sockets and release all locks and memory references.
366 */
367 int
368 pf_proto_unregister(family, protocol, type)
369 int family;
370 int protocol;
371 int type;
372 {
373 struct domain *dp;
374 struct protosw *pr, *dpr;
375
376 /* Sanity checks. */
377 if (family == 0)
378 return (EPFNOSUPPORT);
379 if (protocol == 0)
380 return (EPROTONOSUPPORT);
381 if (type == 0)
382 return (EPROTOTYPE);
383
384 /* Try to find the specified domain based on the family type. */
385 for (dp = domains; dp; dp = dp->dom_next)
386 if (dp->dom_family == family)
387 goto found;
388 return (EPFNOSUPPORT);
389
390 found:
391 dpr = NULL;
392
393 /* Lock out everyone else while we are manipulating the protosw. */
394 mtx_lock(&Giant);
395
396 /* The protocol must exist and only once. */
397 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
398 if ((pr->pr_type == type) && (pr->pr_protocol == protocol)) {
399 if (dpr != NULL) {
400 mtx_unlock(&Giant);
401 return (EMLINK); /* Should not happen! */
402 } else
403 dpr = pr;
404 }
405 }
406
407 /* Protocol does not exist. */
408 if (dpr == NULL) {
409 mtx_unlock(&Giant);
410 return (EPROTONOSUPPORT);
411 }
412
413 /* De-orbit the protocol and make the slot available again. */
414 dpr->pr_type = 0;
415 dpr->pr_domain = dp;
416 dpr->pr_protocol = PROTO_SPACER;
417 dpr->pr_flags = 0;
418 dpr->pr_input = NULL;
419 dpr->pr_output = NULL;
420 dpr->pr_ctlinput = NULL;
421 dpr->pr_ctloutput = NULL;
422 dpr->pr_ousrreq = NULL;
423 dpr->pr_init = NULL;
424 dpr->pr_fasttimo = NULL;
425 dpr->pr_slowtimo = NULL;
426 dpr->pr_drain = NULL;
427 dpr->pr_usrreqs = &nousrreqs;
428
429 /* Job is done, not more protection required. */
430 mtx_unlock(&Giant);
431
432 return (0);
433 }
434
435 void
436 pfctlinput(cmd, sa)
437 int cmd;
438 struct sockaddr *sa;
439 {
440 register struct domain *dp;
441 register struct protosw *pr;
442
443 for (dp = domains; dp; dp = dp->dom_next)
444 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
445 if (pr->pr_ctlinput)
446 (*pr->pr_ctlinput)(cmd, sa, (void *)0);
447 }
448
449 void
450 pfctlinput2(cmd, sa, ctlparam)
451 int cmd;
452 struct sockaddr *sa;
453 void *ctlparam;
454 {
455 struct domain *dp;
456 struct protosw *pr;
457
458 if (!sa)
459 return;
460 for (dp = domains; dp; dp = dp->dom_next) {
461 /*
462 * the check must be made by xx_ctlinput() anyways, to
463 * make sure we use data item pointed to by ctlparam in
464 * correct way. the following check is made just for safety.
465 */
466 if (dp->dom_family != sa->sa_family)
467 continue;
468
469 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
470 if (pr->pr_ctlinput)
471 (*pr->pr_ctlinput)(cmd, sa, ctlparam);
472 }
473 }
474
475 static void
476 pfslowtimo(arg)
477 void *arg;
478 {
479 register struct domain *dp;
480 register struct protosw *pr;
481
482 NET_ASSERT_GIANT();
483
484 for (dp = domains; dp; dp = dp->dom_next)
485 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
486 if (pr->pr_slowtimo)
487 (*pr->pr_slowtimo)();
488 callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL);
489 }
490
491 static void
492 pffasttimo(arg)
493 void *arg;
494 {
495 register struct domain *dp;
496 register struct protosw *pr;
497
498 NET_ASSERT_GIANT();
499
500 for (dp = domains; dp; dp = dp->dom_next)
501 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
502 if (pr->pr_fasttimo)
503 (*pr->pr_fasttimo)();
504 callout_reset(&pffast_callout, hz/5, pffasttimo, NULL);
505 }
Cache object: 55d198a3d588417b5b850b98e7b84e8d
|