1 /* $NetBSD: uipc_domain.c,v 1.82 2009/05/27 23:44:36 pooka Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)uipc_domain.c 8.3 (Berkeley) 2/14/95 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: uipc_domain.c,v 1.82 2009/05/27 23:44:36 pooka Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/socket.h> 39 #include <sys/socketvar.h> 40 #include <sys/protosw.h> 41 #include <sys/domain.h> 42 #include <sys/mbuf.h> 43 #include <sys/time.h> 44 #include <sys/kernel.h> 45 #include <sys/systm.h> 46 #include <sys/callout.h> 47 #include <sys/queue.h> 48 #include <sys/proc.h> 49 #include <sys/sysctl.h> 50 #include <sys/un.h> 51 #include <sys/unpcb.h> 52 #include <sys/file.h> 53 #include <sys/filedesc.h> 54 #include <sys/kauth.h> 55 56 MALLOC_DECLARE(M_SOCKADDR); 57 58 MALLOC_DEFINE(M_SOCKADDR, "sockaddr", "socket endpoints"); 59 60 void pffasttimo(void *); 61 void pfslowtimo(void *); 62 63 struct domainhead domains = STAILQ_HEAD_INITIALIZER(domains); 64 static struct domain *domain_array[AF_MAX]; 65 66 callout_t pffasttimo_ch, pfslowtimo_ch; 67 68 /* 69 * Current time values for fast and slow timeouts. We can use u_int 70 * relatively safely. The fast timer will roll over in 27 years and 71 * the slow timer in 68 years. 72 */ 73 u_int pfslowtimo_now; 74 u_int pffasttimo_now; 75 76 static struct sysctllog *domain_sysctllog; 77 static void sysctl_net_setup(void); 78 79 void 80 domaininit(bool addroute) 81 { 82 __link_set_decl(domains, struct domain); 83 struct domain * const * dpp; 84 struct domain *rt_domain = NULL; 85 86 sysctl_net_setup(); 87 88 /* 89 * Add all of the domains. Make sure the PF_ROUTE 90 * domain is added last. 91 */ 92 __link_set_foreach(dpp, domains) { 93 if ((*dpp)->dom_family == PF_ROUTE) 94 rt_domain = *dpp; 95 else 96 domain_attach(*dpp); 97 } 98 if (rt_domain && addroute) 99 domain_attach(rt_domain); 100 101 callout_init(&pffasttimo_ch, CALLOUT_MPSAFE); 102 callout_init(&pfslowtimo_ch, CALLOUT_MPSAFE); 103 104 callout_reset(&pffasttimo_ch, 1, pffasttimo, NULL); 105 callout_reset(&pfslowtimo_ch, 1, pfslowtimo, NULL); 106 } 107 108 void 109 domain_attach(struct domain *dp) 110 { 111 const struct protosw *pr; 112 113 STAILQ_INSERT_TAIL(&domains, dp, dom_link); 114 if (dp->dom_family < __arraycount(domain_array)) 115 domain_array[dp->dom_family] = dp; 116 117 if (dp->dom_init) 118 (*dp->dom_init)(); 119 120 #ifdef MBUFTRACE 121 if (dp->dom_mowner.mo_name[0] == '\0') { 122 strncpy(dp->dom_mowner.mo_name, dp->dom_name, 123 sizeof(dp->dom_mowner.mo_name)); 124 MOWNER_ATTACH(&dp->dom_mowner); 125 } 126 #endif 127 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { 128 if (pr->pr_init) 129 (*pr->pr_init)(); 130 } 131 132 if (max_linkhdr < 16) /* XXX */ 133 max_linkhdr = 16; 134 max_hdr = max_linkhdr + max_protohdr; 135 max_datalen = MHLEN - max_hdr; 136 } 137 138 struct domain * 139 pffinddomain(int family) 140 { 141 struct domain *dp; 142 143 if (family < __arraycount(domain_array) && domain_array[family] != NULL) 144 return domain_array[family]; 145 146 DOMAIN_FOREACH(dp) 147 if (dp->dom_family == family) 148 return dp; 149 return NULL; 150 } 151 152 const struct protosw * 153 pffindtype(int family, int type) 154 { 155 struct domain *dp; 156 const struct protosw *pr; 157 158 dp = pffinddomain(family); 159 if (dp == NULL) 160 return NULL; 161 162 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 163 if (pr->pr_type && pr->pr_type == type) 164 return pr; 165 166 return NULL; 167 } 168 169 const struct protosw * 170 pffindproto(int family, int protocol, int type) 171 { 172 struct domain *dp; 173 const struct protosw *pr; 174 const struct protosw *maybe = NULL; 175 176 if (family == 0) 177 return NULL; 178 179 dp = pffinddomain(family); 180 if (dp == NULL) 181 return NULL; 182 183 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { 184 if ((pr->pr_protocol == protocol) && (pr->pr_type == type)) 185 return pr; 186 187 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW && 188 pr->pr_protocol == 0 && maybe == NULL) 189 maybe = pr; 190 } 191 return maybe; 192 } 193 194 void * 195 sockaddr_addr(struct sockaddr *sa, socklen_t *slenp) 196 { 197 const struct domain *dom; 198 199 if ((dom = pffinddomain(sa->sa_family)) == NULL || 200 dom->dom_sockaddr_addr == NULL) 201 return NULL; 202 203 return (*dom->dom_sockaddr_addr)(sa, slenp); 204 } 205 206 const void * 207 sockaddr_const_addr(const struct sockaddr *sa, socklen_t *slenp) 208 { 209 const struct domain *dom; 210 211 if ((dom = pffinddomain(sa->sa_family)) == NULL || 212 dom->dom_sockaddr_const_addr == NULL) 213 return NULL; 214 215 return (*dom->dom_sockaddr_const_addr)(sa, slenp); 216 } 217 218 const struct sockaddr * 219 sockaddr_any_by_family(int family) 220 { 221 const struct domain *dom; 222 223 if ((dom = pffinddomain(family)) == NULL) 224 return NULL; 225 226 return dom->dom_sa_any; 227 } 228 229 const struct sockaddr * 230 sockaddr_any(const struct sockaddr *sa) 231 { 232 return sockaddr_any_by_family(sa->sa_family); 233 } 234 235 const void * 236 sockaddr_anyaddr(const struct sockaddr *sa, socklen_t *slenp) 237 { 238 const struct sockaddr *any; 239 240 if ((any = sockaddr_any(sa)) == NULL) 241 return NULL; 242 243 return sockaddr_const_addr(any, slenp); 244 } 245 246 struct sockaddr * 247 sockaddr_alloc(sa_family_t af, socklen_t socklen, int flags) 248 { 249 struct sockaddr *sa; 250 socklen_t reallen = MAX(socklen, offsetof(struct sockaddr, sa_data[0])); 251 252 if ((sa = malloc(reallen, M_SOCKADDR, flags)) == NULL) 253 return NULL; 254 255 sa->sa_family = af; 256 sa->sa_len = reallen; 257 return sa; 258 } 259 260 struct sockaddr * 261 sockaddr_copy(struct sockaddr *dst, socklen_t socklen, 262 const struct sockaddr *src) 263 { 264 if (__predict_false(socklen < src->sa_len)) { 265 panic("%s: source too long, %d < %d bytes", __func__, socklen, 266 src->sa_len); 267 } 268 return memcpy(dst, src, src->sa_len); 269 } 270 271 int 272 sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2) 273 { 274 int len, rc; 275 struct domain *dom; 276 277 if (sa1->sa_family != sa2->sa_family) 278 return sa1->sa_family - sa2->sa_family; 279 280 dom = pffinddomain(sa1->sa_family); 281 282 if (dom != NULL && dom->dom_sockaddr_cmp != NULL) 283 return (*dom->dom_sockaddr_cmp)(sa1, sa2); 284 285 len = MIN(sa1->sa_len, sa2->sa_len); 286 287 if (dom == NULL || dom->dom_sa_cmplen == 0) { 288 if ((rc = memcmp(sa1, sa2, len)) != 0) 289 return rc; 290 return sa1->sa_len - sa2->sa_len; 291 } 292 293 if ((rc = memcmp((const char *)sa1 + dom->dom_sa_cmpofs, 294 (const char *)sa2 + dom->dom_sa_cmpofs, 295 MIN(dom->dom_sa_cmplen, 296 len - MIN(len, dom->dom_sa_cmpofs)))) != 0) 297 return rc; 298 299 return MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa1->sa_len) - 300 MIN(dom->dom_sa_cmplen + dom->dom_sa_cmpofs, sa2->sa_len); 301 } 302 303 struct sockaddr * 304 sockaddr_dup(const struct sockaddr *src, int flags) 305 { 306 struct sockaddr *dst; 307 308 if ((dst = sockaddr_alloc(src->sa_family, src->sa_len, flags)) == NULL) 309 return NULL; 310 311 return sockaddr_copy(dst, dst->sa_len, src); 312 } 313 314 void 315 sockaddr_free(struct sockaddr *sa) 316 { 317 free(sa, M_SOCKADDR); 318 } 319 320 /* 321 * sysctl helper to stuff PF_LOCAL pcbs into sysctl structures 322 */ 323 static void 324 sysctl_dounpcb(struct kinfo_pcb *pcb, const struct socket *so) 325 { 326 struct unpcb *unp = sotounpcb(so); 327 struct sockaddr_un *un = unp->unp_addr; 328 329 memset(pcb, 0, sizeof(*pcb)); 330 331 pcb->ki_family = so->so_proto->pr_domain->dom_family; 332 pcb->ki_type = so->so_proto->pr_type; 333 pcb->ki_protocol = so->so_proto->pr_protocol; 334 pcb->ki_pflags = unp->unp_flags; 335 336 pcb->ki_pcbaddr = PTRTOUINT64(unp); 337 /* pcb->ki_ppcbaddr = unp has no ppcb... */ 338 pcb->ki_sockaddr = PTRTOUINT64(so); 339 340 pcb->ki_sostate = so->so_state; 341 /* pcb->ki_prstate = unp has no state... */ 342 343 pcb->ki_rcvq = so->so_rcv.sb_cc; 344 pcb->ki_sndq = so->so_snd.sb_cc; 345 346 un = (struct sockaddr_un *)&pcb->ki_src; 347 /* 348 * local domain sockets may bind without having a local 349 * endpoint. bleah! 350 */ 351 if (unp->unp_addr != NULL) { 352 un->sun_len = unp->unp_addr->sun_len; 353 un->sun_family = unp->unp_addr->sun_family; 354 strlcpy(un->sun_path, unp->unp_addr->sun_path, 355 sizeof(pcb->ki_s)); 356 } 357 else { 358 un->sun_len = offsetof(struct sockaddr_un, sun_path); 359 un->sun_family = pcb->ki_family; 360 } 361 if (unp->unp_conn != NULL) { 362 un = (struct sockaddr_un *)&pcb->ki_dst; 363 if (unp->unp_conn->unp_addr != NULL) { 364 un->sun_len = unp->unp_conn->unp_addr->sun_len; 365 un->sun_family = unp->unp_conn->unp_addr->sun_family; 366 un->sun_family = unp->unp_conn->unp_addr->sun_family; 367 strlcpy(un->sun_path, unp->unp_conn->unp_addr->sun_path, 368 sizeof(pcb->ki_d)); 369 } 370 else { 371 un->sun_len = offsetof(struct sockaddr_un, sun_path); 372 un->sun_family = pcb->ki_family; 373 } 374 } 375 376 pcb->ki_inode = unp->unp_ino; 377 pcb->ki_vnode = PTRTOUINT64(unp->unp_vnode); 378 pcb->ki_conn = PTRTOUINT64(unp->unp_conn); 379 pcb->ki_refs = PTRTOUINT64(unp->unp_refs); 380 pcb->ki_nextref = PTRTOUINT64(unp->unp_nextref); 381 } 382 383 static int 384 sysctl_unpcblist(SYSCTLFN_ARGS) 385 { 386 struct file *fp, *dfp, *np; 387 struct socket *so; 388 struct kinfo_pcb pcb; 389 char *dp; 390 u_int op, arg; 391 size_t len, needed, elem_size, out_size; 392 int error, elem_count, pf, type, pf2; 393 394 if (namelen == 1 && name[0] == CTL_QUERY) 395 return sysctl_query(SYSCTLFN_CALL(rnode)); 396 397 if (namelen != 4) 398 return EINVAL; 399 400 if (oldp != NULL) { 401 len = *oldlenp; 402 elem_size = name[2]; 403 elem_count = name[3]; 404 if (elem_size != sizeof(pcb)) 405 return EINVAL; 406 } else { 407 len = 0; 408 elem_size = sizeof(pcb); 409 elem_count = INT_MAX; 410 } 411 error = 0; 412 dp = oldp; 413 op = name[0]; 414 arg = name[1]; 415 out_size = elem_size; 416 needed = 0; 417 418 if (name - oname != 4) 419 return EINVAL; 420 421 pf = oname[1]; 422 type = oname[2]; 423 pf2 = (oldp == NULL) ? 0 : pf; 424 425 /* 426 * allocate dummy file descriptor to make position in list. 427 */ 428 sysctl_unlock(); 429 if ((dfp = fgetdummy()) == NULL) { 430 sysctl_relock(); 431 return ENOMEM; 432 } 433 434 /* 435 * there's no "list" of local domain sockets, so we have 436 * to walk the file list looking for them. :-/ 437 */ 438 mutex_enter(&filelist_lock); 439 LIST_FOREACH(fp, &filehead, f_list) { 440 np = LIST_NEXT(fp, f_list); 441 if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET || 442 fp->f_data == NULL) 443 continue; 444 if (kauth_authorize_generic(l->l_cred, 445 KAUTH_GENERIC_CANSEE, fp->f_cred) != 0) 446 continue; 447 so = (struct socket *)fp->f_data; 448 if (so->so_type != type) 449 continue; 450 if (so->so_proto->pr_domain->dom_family != pf) 451 continue; 452 if (len >= elem_size && elem_count > 0) { 453 mutex_enter(&fp->f_lock); 454 fp->f_count++; 455 mutex_exit(&fp->f_lock); 456 LIST_INSERT_AFTER(fp, dfp, f_list); 457 mutex_exit(&filelist_lock); 458 sysctl_dounpcb(&pcb, so); 459 error = copyout(&pcb, dp, out_size); 460 closef(fp); 461 mutex_enter(&filelist_lock); 462 np = LIST_NEXT(dfp, f_list); 463 LIST_REMOVE(dfp, f_list); 464 if (error) 465 break; 466 dp += elem_size; 467 len -= elem_size; 468 } 469 needed += elem_size; 470 if (elem_count > 0 && elem_count != INT_MAX) 471 elem_count--; 472 } 473 mutex_exit(&filelist_lock); 474 fputdummy(dfp); 475 *oldlenp = needed; 476 if (oldp == NULL) 477 *oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb); 478 sysctl_relock(); 479 480 return error; 481 } 482 483 static void 484 sysctl_net_setup(void) 485 { 486 487 KASSERT(domain_sysctllog == NULL); 488 sysctl_createv(&domain_sysctllog, 0, NULL, NULL, 489 CTLFLAG_PERMANENT, 490 CTLTYPE_NODE, "net", NULL, 491 NULL, 0, NULL, 0, 492 CTL_NET, CTL_EOL); 493 sysctl_createv(&domain_sysctllog, 0, NULL, NULL, 494 CTLFLAG_PERMANENT, 495 CTLTYPE_NODE, "local", 496 SYSCTL_DESCR("PF_LOCAL related settings"), 497 NULL, 0, NULL, 0, 498 CTL_NET, PF_LOCAL, CTL_EOL); 499 sysctl_createv(&domain_sysctllog, 0, NULL, NULL, 500 CTLFLAG_PERMANENT, 501 CTLTYPE_NODE, "stream", 502 SYSCTL_DESCR("SOCK_STREAM settings"), 503 NULL, 0, NULL, 0, 504 CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_EOL); 505 sysctl_createv(&domain_sysctllog, 0, NULL, NULL, 506 CTLFLAG_PERMANENT, 507 CTLTYPE_NODE, "dgram", 508 SYSCTL_DESCR("SOCK_DGRAM settings"), 509 NULL, 0, NULL, 0, 510 CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_EOL); 511 512 sysctl_createv(&domain_sysctllog, 0, NULL, NULL, 513 CTLFLAG_PERMANENT, 514 CTLTYPE_STRUCT, "pcblist", 515 SYSCTL_DESCR("SOCK_STREAM protocol control block list"), 516 sysctl_unpcblist, 0, NULL, 0, 517 CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL); 518 sysctl_createv(&domain_sysctllog, 0, NULL, NULL, 519 CTLFLAG_PERMANENT, 520 CTLTYPE_STRUCT, "pcblist", 521 SYSCTL_DESCR("SOCK_DGRAM protocol control block list"), 522 sysctl_unpcblist, 0, NULL, 0, 523 CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL); 524 } 525 526 void 527 pfctlinput(int cmd, const struct sockaddr *sa) 528 { 529 struct domain *dp; 530 const struct protosw *pr; 531 532 DOMAIN_FOREACH(dp) { 533 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { 534 if (pr->pr_ctlinput != NULL) 535 (*pr->pr_ctlinput)(cmd, sa, NULL); 536 } 537 } 538 } 539 540 void 541 pfctlinput2(int cmd, const struct sockaddr *sa, void *ctlparam) 542 { 543 struct domain *dp; 544 const struct protosw *pr; 545 546 if (sa == NULL) 547 return; 548 549 DOMAIN_FOREACH(dp) { 550 /* 551 * the check must be made by xx_ctlinput() anyways, to 552 * make sure we use data item pointed to by ctlparam in 553 * correct way. the following check is made just for safety. 554 */ 555 if (dp->dom_family != sa->sa_family) 556 continue; 557 558 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { 559 if (pr->pr_ctlinput != NULL) 560 (*pr->pr_ctlinput)(cmd, sa, ctlparam); 561 } 562 } 563 } 564 565 void 566 pfslowtimo(void *arg) 567 { 568 struct domain *dp; 569 const struct protosw *pr; 570 571 pfslowtimo_now++; 572 573 DOMAIN_FOREACH(dp) { 574 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 575 if (pr->pr_slowtimo) 576 (*pr->pr_slowtimo)(); 577 } 578 callout_schedule(&pfslowtimo_ch, hz / 2); 579 } 580 581 void 582 pffasttimo(void *arg) 583 { 584 struct domain *dp; 585 const struct protosw *pr; 586 587 pffasttimo_now++; 588 589 DOMAIN_FOREACH(dp) { 590 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 591 if (pr->pr_fasttimo) 592 (*pr->pr_fasttimo)(); 593 } 594 callout_schedule(&pffasttimo_ch, hz / 5); 595 } 596