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