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$"); 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 if (domain_init_status >= 2) 186 printf("WARNING: attempt to net_add_domain(%s) after " 187 "domainfinalize()\n", dp->dom_name); 188 #endif 189 mtx_unlock(&dom_mtx); 190 net_init_domain(dp); 191 } 192 193 static void 194 socket_zone_change(void *tag) 195 { 196 197 uma_zone_set_max(socket_zone, maxsockets); 198 } 199 200 /* ARGSUSED*/ 201 static void 202 domaininit(void *dummy) 203 { 204 /* 205 * Before we do any setup, make sure to initialize the 206 * zone allocator we get struct sockets from. 207 */ 208 209 socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL, 210 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 211 uma_zone_set_max(socket_zone, maxsockets); 212 EVENTHANDLER_REGISTER(maxsockets_change, socket_zone_change, NULL, 213 EVENTHANDLER_PRI_FIRST); 214 215 if (max_linkhdr < 16) /* XXX */ 216 max_linkhdr = 16; 217 218 if (debug_mpsafenet) { 219 callout_init(&pffast_callout, CALLOUT_MPSAFE); 220 callout_init(&pfslow_callout, CALLOUT_MPSAFE); 221 } else { 222 callout_init(&pffast_callout, 0); 223 callout_init(&pfslow_callout, 0); 224 } 225 226 mtx_lock(&dom_mtx); 227 KASSERT(domain_init_status == 0, ("domaininit called too late!")); 228 domain_init_status = 1; 229 mtx_unlock(&dom_mtx); 230 } 231 232 /* ARGSUSED*/ 233 static void 234 domainfinalize(void *dummy) 235 { 236 mtx_lock(&dom_mtx); 237 KASSERT(domain_init_status == 1, ("domainfinalize called too late!")); 238 domain_init_status = 2; 239 mtx_unlock(&dom_mtx); 240 241 callout_reset(&pffast_callout, 1, pffasttimo, NULL); 242 callout_reset(&pfslow_callout, 1, pfslowtimo, NULL); 243 } 244 245 struct protosw * 246 pffindtype(family, type) 247 int family; 248 int type; 249 { 250 register struct domain *dp; 251 register struct protosw *pr; 252 253 for (dp = domains; dp; dp = dp->dom_next) 254 if (dp->dom_family == family) 255 goto found; 256 return (0); 257 found: 258 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 259 if (pr->pr_type && pr->pr_type == type) 260 return (pr); 261 return (0); 262 } 263 264 struct protosw * 265 pffindproto(family, protocol, type) 266 int family; 267 int protocol; 268 int type; 269 { 270 register struct domain *dp; 271 register struct protosw *pr; 272 struct protosw *maybe = 0; 273 274 if (family == 0) 275 return (0); 276 for (dp = domains; dp; dp = dp->dom_next) 277 if (dp->dom_family == family) 278 goto found; 279 return (0); 280 found: 281 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { 282 if ((pr->pr_protocol == protocol) && (pr->pr_type == type)) 283 return (pr); 284 285 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW && 286 pr->pr_protocol == 0 && maybe == (struct protosw *)0) 287 maybe = pr; 288 } 289 return (maybe); 290 } 291 292 /* 293 * The caller must make sure that the new protocol is fully set up and ready to 294 * accept requests before it is registered. 295 */ 296 int 297 pf_proto_register(family, npr) 298 int family; 299 struct protosw *npr; 300 { 301 struct domain *dp; 302 struct protosw *pr, *fpr; 303 304 /* Sanity checks. */ 305 if (family == 0) 306 return (EPFNOSUPPORT); 307 if (npr->pr_type == 0) 308 return (EPROTOTYPE); 309 if (npr->pr_protocol == 0) 310 return (EPROTONOSUPPORT); 311 if (npr->pr_usrreqs == NULL) 312 return (ENXIO); 313 314 /* Try to find the specified domain based on the family. */ 315 for (dp = domains; dp; dp = dp->dom_next) 316 if (dp->dom_family == family) 317 goto found; 318 return (EPFNOSUPPORT); 319 320 found: 321 /* Initialize backpointer to struct domain. */ 322 npr->pr_domain = dp; 323 fpr = NULL; 324 325 /* 326 * Protect us against races when two protocol registrations for 327 * the same protocol happen at the same time. 328 */ 329 mtx_lock(&Giant); 330 331 /* The new protocol must not yet exist. */ 332 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { 333 if ((pr->pr_type == npr->pr_type) && 334 (pr->pr_protocol == npr->pr_protocol)) { 335 mtx_unlock(&Giant); 336 return (EEXIST); /* XXX: Check only protocol? */ 337 } 338 /* While here, remember the first free spacer. */ 339 if ((fpr == NULL) && (pr->pr_protocol == PROTO_SPACER)) 340 fpr = pr; 341 } 342 343 /* If no free spacer is found we can't add the new protocol. */ 344 if (fpr == NULL) { 345 mtx_unlock(&Giant); 346 return (ENOMEM); 347 } 348 349 /* Copy the new struct protosw over the spacer. */ 350 bcopy(npr, fpr, sizeof(*fpr)); 351 352 /* Job is done, no more protection required. */ 353 mtx_unlock(&Giant); 354 355 /* Initialize and activate the protocol. */ 356 protosw_init(fpr); 357 358 return (0); 359 } 360 361 /* 362 * The caller must make sure the protocol and its functions correctly shut down 363 * all sockets and release all locks and memory references. 364 */ 365 int 366 pf_proto_unregister(family, protocol, type) 367 int family; 368 int protocol; 369 int type; 370 { 371 struct domain *dp; 372 struct protosw *pr, *dpr; 373 374 /* Sanity checks. */ 375 if (family == 0) 376 return (EPFNOSUPPORT); 377 if (protocol == 0) 378 return (EPROTONOSUPPORT); 379 if (type == 0) 380 return (EPROTOTYPE); 381 382 /* Try to find the specified domain based on the family type. */ 383 for (dp = domains; dp; dp = dp->dom_next) 384 if (dp->dom_family == family) 385 goto found; 386 return (EPFNOSUPPORT); 387 388 found: 389 dpr = NULL; 390 391 /* Lock out everyone else while we are manipulating the protosw. */ 392 mtx_lock(&Giant); 393 394 /* The protocol must exist and only once. */ 395 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { 396 if ((pr->pr_type == type) && (pr->pr_protocol == protocol)) { 397 if (dpr != NULL) { 398 mtx_unlock(&Giant); 399 return (EMLINK); /* Should not happen! */ 400 } else 401 dpr = pr; 402 } 403 } 404 405 /* Protocol does not exist. */ 406 if (dpr == NULL) { 407 mtx_unlock(&Giant); 408 return (EPROTONOSUPPORT); 409 } 410 411 /* De-orbit the protocol and make the slot available again. */ 412 dpr->pr_type = 0; 413 dpr->pr_domain = dp; 414 dpr->pr_protocol = PROTO_SPACER; 415 dpr->pr_flags = 0; 416 dpr->pr_input = NULL; 417 dpr->pr_output = NULL; 418 dpr->pr_ctlinput = NULL; 419 dpr->pr_ctloutput = NULL; 420 dpr->pr_ousrreq = NULL; 421 dpr->pr_init = NULL; 422 dpr->pr_fasttimo = NULL; 423 dpr->pr_slowtimo = NULL; 424 dpr->pr_drain = NULL; 425 dpr->pr_usrreqs = &nousrreqs; 426 427 /* Job is done, not more protection required. */ 428 mtx_unlock(&Giant); 429 430 return (0); 431 } 432 433 void 434 pfctlinput(cmd, sa) 435 int cmd; 436 struct sockaddr *sa; 437 { 438 register struct domain *dp; 439 register struct protosw *pr; 440 441 for (dp = domains; dp; dp = dp->dom_next) 442 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 443 if (pr->pr_ctlinput) 444 (*pr->pr_ctlinput)(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 for (dp = domains; dp; dp = dp->dom_next) { 459 /* 460 * the check must be made by xx_ctlinput() anyways, to 461 * make sure we use data item pointed to by ctlparam in 462 * correct way. the following check is made just for safety. 463 */ 464 if (dp->dom_family != sa->sa_family) 465 continue; 466 467 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 468 if (pr->pr_ctlinput) 469 (*pr->pr_ctlinput)(cmd, sa, ctlparam); 470 } 471 } 472 473 static void 474 pfslowtimo(arg) 475 void *arg; 476 { 477 register struct domain *dp; 478 register struct protosw *pr; 479 480 NET_ASSERT_GIANT(); 481 482 for (dp = domains; dp; dp = dp->dom_next) 483 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 484 if (pr->pr_slowtimo) 485 (*pr->pr_slowtimo)(); 486 callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL); 487 } 488 489 static void 490 pffasttimo(arg) 491 void *arg; 492 { 493 register struct domain *dp; 494 register struct protosw *pr; 495 496 NET_ASSERT_GIANT(); 497 498 for (dp = domains; dp; dp = dp->dom_next) 499 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 500 if (pr->pr_fasttimo) 501 (*pr->pr_fasttimo)(); 502 callout_reset(&pffast_callout, hz/5, pffasttimo, NULL); 503 } 504