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.4/sys/kern/uipc_domain.c 186405 2008-12-23 01:23:09Z cperciva $");
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_bind, pru_bind_notsupp);
119 DEFAULT(pu->pru_connect, pru_connect_notsupp);
120 DEFAULT(pu->pru_connect2, pru_connect2_notsupp);
121 DEFAULT(pu->pru_control, pru_control_notsupp);
122 DEFAULT(pu->pru_disconnect, pru_disconnect_notsupp);
123 DEFAULT(pu->pru_listen, pru_listen_notsupp);
124 DEFAULT(pu->pru_peeraddr, pru_peeraddr_notsupp);
125 DEFAULT(pu->pru_rcvd, pru_rcvd_notsupp);
126 DEFAULT(pu->pru_rcvoob, pru_rcvoob_notsupp);
127 DEFAULT(pu->pru_sense, pru_sense_null);
128 DEFAULT(pu->pru_shutdown, pru_shutdown_notsupp);
129 DEFAULT(pu->pru_sockaddr, pru_sockaddr_notsupp);
130 DEFAULT(pu->pru_sosend, sosend);
131 DEFAULT(pu->pru_soreceive, soreceive);
132 DEFAULT(pu->pru_sopoll, sopoll);
133 DEFAULT(pu->pru_sosetlabel, pru_sosetlabel_null);
134 #undef DEFAULT
135 if (pr->pr_init)
136 (*pr->pr_init)();
137 }
138
139 /*
140 * Add a new protocol domain to the list of supported domains
141 * Note: you cant unload it again because a socket may be using it.
142 * XXX can't fail at this time.
143 */
144 static void
145 net_init_domain(struct domain *dp)
146 {
147 struct protosw *pr;
148
149 if (dp->dom_init)
150 (*dp->dom_init)();
151 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
152 protosw_init(pr);
153 /*
154 * update global information about maximums
155 */
156 max_hdr = max_linkhdr + max_protohdr;
157 max_datalen = MHLEN - max_hdr;
158 if (max_datalen < 1)
159 panic("%s: max_datalen < 1", __func__);
160 }
161
162 /*
163 * Add a new protocol domain to the list of supported domains
164 * Note: you cant unload it again because a socket may be using it.
165 * XXX can't fail at this time.
166 */
167 void
168 net_add_domain(void *data)
169 {
170 struct domain *dp;
171
172 dp = (struct domain *)data;
173 mtx_lock(&dom_mtx);
174 dp->dom_next = domains;
175 domains = dp;
176
177 KASSERT(domain_init_status >= 1,
178 ("attempt to net_add_domain(%s) before domaininit()",
179 dp->dom_name));
180 #ifndef INVARIANTS
181 if (domain_init_status < 1)
182 printf("WARNING: attempt to net_add_domain(%s) before "
183 "domaininit()\n", dp->dom_name);
184 #endif
185 #ifdef notyet
186 KASSERT(domain_init_status < 2,
187 ("attempt to net_add_domain(%s) after domainfinalize()",
188 dp->dom_name));
189 #else
190 #ifdef DIAGNOSTIC
191 if (domain_init_status >= 2)
192 printf("WARNING: attempt to net_add_domain(%s) after "
193 "domainfinalize()\n", dp->dom_name);
194 #endif
195 #endif
196 mtx_unlock(&dom_mtx);
197 net_init_domain(dp);
198 }
199
200 static void
201 socket_zone_change(void *tag)
202 {
203
204 uma_zone_set_max(socket_zone, maxsockets);
205 }
206
207 /* ARGSUSED*/
208 static void
209 domaininit(void *dummy)
210 {
211 /*
212 * Before we do any setup, make sure to initialize the
213 * zone allocator we get struct sockets from.
214 */
215
216 socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL,
217 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
218 uma_zone_set_max(socket_zone, maxsockets);
219 EVENTHANDLER_REGISTER(maxsockets_change, socket_zone_change, NULL,
220 EVENTHANDLER_PRI_FIRST);
221
222 if (max_linkhdr < 16) /* XXX */
223 max_linkhdr = 16;
224
225 if (debug_mpsafenet) {
226 callout_init(&pffast_callout, CALLOUT_MPSAFE);
227 callout_init(&pfslow_callout, CALLOUT_MPSAFE);
228 } else {
229 callout_init(&pffast_callout, 0);
230 callout_init(&pfslow_callout, 0);
231 }
232
233 mtx_lock(&dom_mtx);
234 KASSERT(domain_init_status == 0, ("domaininit called too late!"));
235 domain_init_status = 1;
236 mtx_unlock(&dom_mtx);
237 }
238
239 /* ARGSUSED*/
240 static void
241 domainfinalize(void *dummy)
242 {
243 mtx_lock(&dom_mtx);
244 KASSERT(domain_init_status == 1, ("domainfinalize called too late!"));
245 domain_init_status = 2;
246 mtx_unlock(&dom_mtx);
247
248 callout_reset(&pffast_callout, 1, pffasttimo, NULL);
249 callout_reset(&pfslow_callout, 1, pfslowtimo, NULL);
250 }
251
252 struct protosw *
253 pffindtype(family, type)
254 int family;
255 int type;
256 {
257 register struct domain *dp;
258 register struct protosw *pr;
259
260 for (dp = domains; dp; dp = dp->dom_next)
261 if (dp->dom_family == family)
262 goto found;
263 return (0);
264 found:
265 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
266 if (pr->pr_type && pr->pr_type == type)
267 return (pr);
268 return (0);
269 }
270
271 struct protosw *
272 pffindproto(family, protocol, type)
273 int family;
274 int protocol;
275 int type;
276 {
277 register struct domain *dp;
278 register struct protosw *pr;
279 struct protosw *maybe = 0;
280
281 if (family == 0)
282 return (0);
283 for (dp = domains; dp; dp = dp->dom_next)
284 if (dp->dom_family == family)
285 goto found;
286 return (0);
287 found:
288 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
289 if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
290 return (pr);
291
292 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
293 pr->pr_protocol == 0 && maybe == (struct protosw *)0)
294 maybe = pr;
295 }
296 return (maybe);
297 }
298
299 /*
300 * The caller must make sure that the new protocol is fully set up and ready to
301 * accept requests before it is registered.
302 */
303 int
304 pf_proto_register(family, npr)
305 int family;
306 struct protosw *npr;
307 {
308 struct domain *dp;
309 struct protosw *pr, *fpr;
310
311 /* Sanity checks. */
312 if (family == 0)
313 return (EPFNOSUPPORT);
314 if (npr->pr_type == 0)
315 return (EPROTOTYPE);
316 if (npr->pr_protocol == 0)
317 return (EPROTONOSUPPORT);
318 if (npr->pr_usrreqs == NULL)
319 return (ENXIO);
320
321 /* Try to find the specified domain based on the family. */
322 for (dp = domains; dp; dp = dp->dom_next)
323 if (dp->dom_family == family)
324 goto found;
325 return (EPFNOSUPPORT);
326
327 found:
328 /* Initialize backpointer to struct domain. */
329 npr->pr_domain = dp;
330 fpr = NULL;
331
332 /*
333 * Protect us against races when two protocol registrations for
334 * the same protocol happen at the same time.
335 */
336 mtx_lock(&Giant);
337
338 /* The new protocol must not yet exist. */
339 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
340 if ((pr->pr_type == npr->pr_type) &&
341 (pr->pr_protocol == npr->pr_protocol)) {
342 mtx_unlock(&Giant);
343 return (EEXIST); /* XXX: Check only protocol? */
344 }
345 /* While here, remember the first free spacer. */
346 if ((fpr == NULL) && (pr->pr_protocol == PROTO_SPACER))
347 fpr = pr;
348 }
349
350 /* If no free spacer is found we can't add the new protocol. */
351 if (fpr == NULL) {
352 mtx_unlock(&Giant);
353 return (ENOMEM);
354 }
355
356 /* Copy the new struct protosw over the spacer. */
357 bcopy(npr, fpr, sizeof(*fpr));
358
359 /* Job is done, no more protection required. */
360 mtx_unlock(&Giant);
361
362 /* Initialize and activate the protocol. */
363 protosw_init(fpr);
364
365 return (0);
366 }
367
368 /*
369 * The caller must make sure the protocol and its functions correctly shut down
370 * all sockets and release all locks and memory references.
371 */
372 int
373 pf_proto_unregister(family, protocol, type)
374 int family;
375 int protocol;
376 int type;
377 {
378 struct domain *dp;
379 struct protosw *pr, *dpr;
380
381 /* Sanity checks. */
382 if (family == 0)
383 return (EPFNOSUPPORT);
384 if (protocol == 0)
385 return (EPROTONOSUPPORT);
386 if (type == 0)
387 return (EPROTOTYPE);
388
389 /* Try to find the specified domain based on the family type. */
390 for (dp = domains; dp; dp = dp->dom_next)
391 if (dp->dom_family == family)
392 goto found;
393 return (EPFNOSUPPORT);
394
395 found:
396 dpr = NULL;
397
398 /* Lock out everyone else while we are manipulating the protosw. */
399 mtx_lock(&Giant);
400
401 /* The protocol must exist and only once. */
402 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
403 if ((pr->pr_type == type) && (pr->pr_protocol == protocol)) {
404 if (dpr != NULL) {
405 mtx_unlock(&Giant);
406 return (EMLINK); /* Should not happen! */
407 } else
408 dpr = pr;
409 }
410 }
411
412 /* Protocol does not exist. */
413 if (dpr == NULL) {
414 mtx_unlock(&Giant);
415 return (EPROTONOSUPPORT);
416 }
417
418 /* De-orbit the protocol and make the slot available again. */
419 dpr->pr_type = 0;
420 dpr->pr_domain = dp;
421 dpr->pr_protocol = PROTO_SPACER;
422 dpr->pr_flags = 0;
423 dpr->pr_input = NULL;
424 dpr->pr_output = NULL;
425 dpr->pr_ctlinput = NULL;
426 dpr->pr_ctloutput = NULL;
427 dpr->pr_ousrreq = NULL;
428 dpr->pr_init = NULL;
429 dpr->pr_fasttimo = NULL;
430 dpr->pr_slowtimo = NULL;
431 dpr->pr_drain = NULL;
432 dpr->pr_usrreqs = &nousrreqs;
433
434 /* Job is done, not more protection required. */
435 mtx_unlock(&Giant);
436
437 return (0);
438 }
439
440 void
441 pfctlinput(cmd, sa)
442 int cmd;
443 struct sockaddr *sa;
444 {
445 register struct domain *dp;
446 register struct protosw *pr;
447
448 for (dp = domains; dp; dp = dp->dom_next)
449 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
450 if (pr->pr_ctlinput)
451 (*pr->pr_ctlinput)(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 for (dp = domains; dp; dp = dp->dom_next) {
466 /*
467 * the check must be made by xx_ctlinput() anyways, to
468 * make sure we use data item pointed to by ctlparam in
469 * correct way. the following check is made just for safety.
470 */
471 if (dp->dom_family != sa->sa_family)
472 continue;
473
474 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
475 if (pr->pr_ctlinput)
476 (*pr->pr_ctlinput)(cmd, sa, ctlparam);
477 }
478 }
479
480 static void
481 pfslowtimo(arg)
482 void *arg;
483 {
484 register struct domain *dp;
485 register struct protosw *pr;
486
487 NET_ASSERT_GIANT();
488
489 for (dp = domains; dp; dp = dp->dom_next)
490 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
491 if (pr->pr_slowtimo)
492 (*pr->pr_slowtimo)();
493 callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL);
494 }
495
496 static void
497 pffasttimo(arg)
498 void *arg;
499 {
500 register struct domain *dp;
501 register struct protosw *pr;
502
503 NET_ASSERT_GIANT();
504
505 for (dp = domains; dp; dp = dp->dom_next)
506 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
507 if (pr->pr_fasttimo)
508 (*pr->pr_fasttimo)();
509 callout_reset(&pffast_callout, hz/5, pffasttimo, NULL);
510 }
Cache object: bb33e85108b7d04d4ead51aeafe13db5
|