1 /* 2 * Copyright (c) 1982, 1986, 1988, 1990, 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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94 34 * $FreeBSD$ 35 */ 36 37 #include "opt_inet.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/fcntl.h> 42 #include <sys/malloc.h> 43 #include <sys/mbuf.h> 44 #include <sys/domain.h> 45 #include <sys/file.h> /* for struct knote */ 46 #include <sys/kernel.h> 47 #include <sys/malloc.h> 48 #include <sys/event.h> 49 #include <sys/poll.h> 50 #include <sys/proc.h> 51 #include <sys/protosw.h> 52 #include <sys/socket.h> 53 #include <sys/socketvar.h> 54 #include <sys/resourcevar.h> 55 #include <sys/signalvar.h> 56 #include <sys/sysctl.h> 57 #include <sys/uio.h> 58 #include <sys/jail.h> 59 #include <vm/vm_zone.h> 60 61 #include <machine/limits.h> 62 63 #ifdef INET 64 static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt); 65 #endif 66 67 static int filt_sorattach(struct knote *kn); 68 static void filt_sordetach(struct knote *kn); 69 static int filt_soread(struct knote *kn, long hint); 70 static int filt_sowattach(struct knote *kn); 71 static void filt_sowdetach(struct knote *kn); 72 static int filt_sowrite(struct knote *kn, long hint); 73 static int filt_solisten(struct knote *kn, long hint); 74 75 static struct filterops solisten_filtops = 76 { 1, filt_sorattach, filt_sordetach, filt_solisten }; 77 78 struct filterops so_rwfiltops[] = { 79 { 1, filt_sorattach, filt_sordetach, filt_soread }, 80 { 1, filt_sowattach, filt_sowdetach, filt_sowrite }, 81 }; 82 83 struct vm_zone *socket_zone; 84 so_gen_t so_gencnt; /* generation count for sockets */ 85 86 MALLOC_DEFINE(M_SONAME, "soname", "socket name"); 87 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block"); 88 89 SYSCTL_DECL(_kern_ipc); 90 91 static int somaxconn = SOMAXCONN; 92 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW, 93 &somaxconn, 0, "Maximum pending socket connection queue size"); 94 95 /* 96 * Socket operation routines. 97 * These routines are called by the routines in 98 * sys_socket.c or from a system process, and 99 * implement the semantics of socket operations by 100 * switching out to the protocol specific routines. 101 */ 102 103 /* 104 * Get a socket structure from our zone, and initialize it. 105 * We don't implement `waitok' yet (see comments in uipc_domain.c). 106 * Note that it would probably be better to allocate socket 107 * and PCB at the same time, but I'm not convinced that all 108 * the protocols can be easily modified to do this. 109 */ 110 struct socket * 111 soalloc(waitok) 112 int waitok; 113 { 114 struct socket *so; 115 116 so = zalloci(socket_zone); 117 if (so) { 118 /* XXX race condition for reentrant kernel */ 119 bzero(so, sizeof *so); 120 so->so_gencnt = ++so_gencnt; 121 so->so_zone = socket_zone; 122 TAILQ_INIT(&so->so_aiojobq); 123 } 124 return so; 125 } 126 127 int 128 socreate(dom, aso, type, proto, p) 129 int dom; 130 struct socket **aso; 131 register int type; 132 int proto; 133 struct proc *p; 134 { 135 register struct protosw *prp; 136 register struct socket *so; 137 register int error; 138 139 if (proto) 140 prp = pffindproto(dom, proto, type); 141 else 142 prp = pffindtype(dom, type); 143 144 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0) 145 return (EPROTONOSUPPORT); 146 147 if (p->p_prison && jail_socket_unixiproute_only && 148 prp->pr_domain->dom_family != PF_LOCAL && 149 prp->pr_domain->dom_family != PF_INET && 150 prp->pr_domain->dom_family != PF_ROUTE) { 151 return (EPROTONOSUPPORT); 152 } 153 154 if (prp->pr_type != type) 155 return (EPROTOTYPE); 156 so = soalloc(p != 0); 157 if (so == 0) 158 return (ENOBUFS); 159 160 TAILQ_INIT(&so->so_incomp); 161 TAILQ_INIT(&so->so_comp); 162 so->so_type = type; 163 so->so_cred = p->p_ucred; 164 crhold(so->so_cred); 165 so->so_proto = prp; 166 error = (*prp->pr_usrreqs->pru_attach)(so, proto, p); 167 if (error) { 168 so->so_state |= SS_NOFDREF; 169 sofree(so); 170 return (error); 171 } 172 *aso = so; 173 return (0); 174 } 175 176 int 177 sobind(so, nam, p) 178 struct socket *so; 179 struct sockaddr *nam; 180 struct proc *p; 181 { 182 int s = splnet(); 183 int error; 184 185 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, p); 186 splx(s); 187 return (error); 188 } 189 190 void 191 sodealloc(so) 192 struct socket *so; 193 { 194 195 so->so_gencnt = ++so_gencnt; 196 if (so->so_rcv.sb_hiwat) 197 (void)chgsbsize(so->so_cred->cr_uidinfo, 198 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY); 199 if (so->so_snd.sb_hiwat) 200 (void)chgsbsize(so->so_cred->cr_uidinfo, 201 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY); 202 #ifdef INET 203 if (so->so_accf != NULL) { 204 if (so->so_accf->so_accept_filter != NULL && 205 so->so_accf->so_accept_filter->accf_destroy != NULL) { 206 so->so_accf->so_accept_filter->accf_destroy(so); 207 } 208 if (so->so_accf->so_accept_filter_str != NULL) 209 FREE(so->so_accf->so_accept_filter_str, M_ACCF); 210 FREE(so->so_accf, M_ACCF); 211 } 212 #endif 213 crfree(so->so_cred); 214 zfreei(so->so_zone, so); 215 } 216 217 int 218 solisten(so, backlog, p) 219 register struct socket *so; 220 int backlog; 221 struct proc *p; 222 { 223 int s, error; 224 225 s = splnet(); 226 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, p); 227 if (error) { 228 splx(s); 229 return (error); 230 } 231 if (TAILQ_EMPTY(&so->so_comp)) 232 so->so_options |= SO_ACCEPTCONN; 233 if (backlog < 0 || backlog > somaxconn) 234 backlog = somaxconn; 235 so->so_qlimit = backlog; 236 splx(s); 237 return (0); 238 } 239 240 void 241 sofree(so) 242 register struct socket *so; 243 { 244 struct socket *head = so->so_head; 245 246 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) 247 return; 248 if (head != NULL) { 249 if (so->so_state & SS_INCOMP) { 250 TAILQ_REMOVE(&head->so_incomp, so, so_list); 251 head->so_incqlen--; 252 } else if (so->so_state & SS_COMP) { 253 /* 254 * We must not decommission a socket that's 255 * on the accept(2) queue. If we do, then 256 * accept(2) may hang after select(2) indicated 257 * that the listening socket was ready. 258 */ 259 return; 260 } else { 261 panic("sofree: not queued"); 262 } 263 head->so_qlen--; 264 so->so_state &= ~SS_INCOMP; 265 so->so_head = NULL; 266 } 267 sbrelease(&so->so_snd, so); 268 sorflush(so); 269 sodealloc(so); 270 } 271 272 /* 273 * Close a socket on last file table reference removal. 274 * Initiate disconnect if connected. 275 * Free socket when disconnect complete. 276 */ 277 int 278 soclose(so) 279 register struct socket *so; 280 { 281 int s = splnet(); /* conservative */ 282 int error = 0; 283 284 funsetown(so->so_sigio); 285 if (so->so_options & SO_ACCEPTCONN) { 286 struct socket *sp, *sonext; 287 288 sp = TAILQ_FIRST(&so->so_incomp); 289 for (; sp != NULL; sp = sonext) { 290 sonext = TAILQ_NEXT(sp, so_list); 291 (void) soabort(sp); 292 } 293 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) { 294 sonext = TAILQ_NEXT(sp, so_list); 295 /* Dequeue from so_comp since sofree() won't do it */ 296 TAILQ_REMOVE(&so->so_comp, sp, so_list); 297 so->so_qlen--; 298 sp->so_state &= ~SS_COMP; 299 sp->so_head = NULL; 300 (void) soabort(sp); 301 } 302 } 303 if (so->so_pcb == 0) 304 goto discard; 305 if (so->so_state & SS_ISCONNECTED) { 306 if ((so->so_state & SS_ISDISCONNECTING) == 0) { 307 error = sodisconnect(so); 308 if (error) 309 goto drop; 310 } 311 if (so->so_options & SO_LINGER) { 312 if ((so->so_state & SS_ISDISCONNECTING) && 313 (so->so_state & SS_NBIO)) 314 goto drop; 315 while (so->so_state & SS_ISCONNECTED) { 316 error = tsleep((caddr_t)&so->so_timeo, 317 PSOCK | PCATCH, "soclos", so->so_linger * hz); 318 if (error) 319 break; 320 } 321 } 322 } 323 drop: 324 if (so->so_pcb) { 325 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so); 326 if (error == 0) 327 error = error2; 328 } 329 discard: 330 if (so->so_state & SS_NOFDREF) 331 panic("soclose: NOFDREF"); 332 so->so_state |= SS_NOFDREF; 333 sofree(so); 334 splx(s); 335 return (error); 336 } 337 338 /* 339 * Must be called at splnet... 340 */ 341 int 342 soabort(so) 343 struct socket *so; 344 { 345 int error; 346 347 error = (*so->so_proto->pr_usrreqs->pru_abort)(so); 348 if (error) { 349 sofree(so); 350 return error; 351 } 352 return (0); 353 } 354 355 int 356 soaccept(so, nam) 357 register struct socket *so; 358 struct sockaddr **nam; 359 { 360 int s = splnet(); 361 int error; 362 363 if ((so->so_state & SS_NOFDREF) == 0) 364 panic("soaccept: !NOFDREF"); 365 so->so_state &= ~SS_NOFDREF; 366 if ((so->so_state & SS_ISDISCONNECTED) == 0) 367 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam); 368 else { 369 if (nam) 370 *nam = 0; 371 error = 0; 372 } 373 splx(s); 374 return (error); 375 } 376 377 int 378 soconnect(so, nam, p) 379 register struct socket *so; 380 struct sockaddr *nam; 381 struct proc *p; 382 { 383 int s; 384 int error; 385 386 if (so->so_options & SO_ACCEPTCONN) 387 return (EOPNOTSUPP); 388 s = splnet(); 389 /* 390 * If protocol is connection-based, can only connect once. 391 * Otherwise, if connected, try to disconnect first. 392 * This allows user to disconnect by connecting to, e.g., 393 * a null address. 394 */ 395 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && 396 ((so->so_proto->pr_flags & PR_CONNREQUIRED) || 397 (error = sodisconnect(so)))) 398 error = EISCONN; 399 else 400 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, p); 401 splx(s); 402 return (error); 403 } 404 405 int 406 soconnect2(so1, so2) 407 register struct socket *so1; 408 struct socket *so2; 409 { 410 int s = splnet(); 411 int error; 412 413 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2); 414 splx(s); 415 return (error); 416 } 417 418 int 419 sodisconnect(so) 420 register struct socket *so; 421 { 422 int s = splnet(); 423 int error; 424 425 if ((so->so_state & SS_ISCONNECTED) == 0) { 426 error = ENOTCONN; 427 goto bad; 428 } 429 if (so->so_state & SS_ISDISCONNECTING) { 430 error = EALREADY; 431 goto bad; 432 } 433 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so); 434 bad: 435 splx(s); 436 return (error); 437 } 438 439 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK) 440 /* 441 * Send on a socket. 442 * If send must go all at once and message is larger than 443 * send buffering, then hard error. 444 * Lock against other senders. 445 * If must go all at once and not enough room now, then 446 * inform user that this would block and do nothing. 447 * Otherwise, if nonblocking, send as much as possible. 448 * The data to be sent is described by "uio" if nonzero, 449 * otherwise by the mbuf chain "top" (which must be null 450 * if uio is not). Data provided in mbuf chain must be small 451 * enough to send all at once. 452 * 453 * Returns nonzero on error, timeout or signal; callers 454 * must check for short counts if EINTR/ERESTART are returned. 455 * Data and control buffers are freed on return. 456 */ 457 int 458 sosend(so, addr, uio, top, control, flags, p) 459 register struct socket *so; 460 struct sockaddr *addr; 461 struct uio *uio; 462 struct mbuf *top; 463 struct mbuf *control; 464 int flags; 465 struct proc *p; 466 { 467 struct mbuf **mp; 468 register struct mbuf *m; 469 register long space, len, resid; 470 int clen = 0, error, s, dontroute, mlen; 471 int atomic = sosendallatonce(so) || top; 472 473 if (uio) 474 resid = uio->uio_resid; 475 else 476 resid = top->m_pkthdr.len; 477 /* 478 * In theory resid should be unsigned. 479 * However, space must be signed, as it might be less than 0 480 * if we over-committed, and we must use a signed comparison 481 * of space and resid. On the other hand, a negative resid 482 * causes us to loop sending 0-length segments to the protocol. 483 * 484 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM 485 * type sockets since that's an error. 486 */ 487 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) { 488 error = EINVAL; 489 goto out; 490 } 491 492 dontroute = 493 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && 494 (so->so_proto->pr_flags & PR_ATOMIC); 495 if (p) 496 p->p_stats->p_ru.ru_msgsnd++; 497 if (control) 498 clen = control->m_len; 499 #define snderr(errno) { error = errno; splx(s); goto release; } 500 501 restart: 502 error = sblock(&so->so_snd, SBLOCKWAIT(flags)); 503 if (error) 504 goto out; 505 do { 506 s = splnet(); 507 if (so->so_state & SS_CANTSENDMORE) 508 snderr(EPIPE); 509 if (so->so_error) { 510 error = so->so_error; 511 so->so_error = 0; 512 splx(s); 513 goto release; 514 } 515 if ((so->so_state & SS_ISCONNECTED) == 0) { 516 /* 517 * `sendto' and `sendmsg' is allowed on a connection- 518 * based socket if it supports implied connect. 519 * Return ENOTCONN if not connected and no address is 520 * supplied. 521 */ 522 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) && 523 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) { 524 if ((so->so_state & SS_ISCONFIRMING) == 0 && 525 !(resid == 0 && clen != 0)) 526 snderr(ENOTCONN); 527 } else if (addr == 0) 528 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ? 529 ENOTCONN : EDESTADDRREQ); 530 } 531 space = sbspace(&so->so_snd); 532 if (flags & MSG_OOB) 533 space += 1024; 534 if ((atomic && resid > so->so_snd.sb_hiwat) || 535 clen > so->so_snd.sb_hiwat) 536 snderr(EMSGSIZE); 537 if (space < resid + clen && uio && 538 (atomic || space < so->so_snd.sb_lowat || space < clen)) { 539 if (so->so_state & SS_NBIO) 540 snderr(EWOULDBLOCK); 541 sbunlock(&so->so_snd); 542 error = sbwait(&so->so_snd); 543 splx(s); 544 if (error) 545 goto out; 546 goto restart; 547 } 548 splx(s); 549 mp = ⊤ 550 space -= clen; 551 do { 552 if (uio == NULL) { 553 /* 554 * Data is prepackaged in "top". 555 */ 556 resid = 0; 557 if (flags & MSG_EOR) 558 top->m_flags |= M_EOR; 559 } else do { 560 if (top == 0) { 561 MGETHDR(m, M_TRYWAIT, MT_DATA); 562 if (m == NULL) { 563 error = ENOBUFS; 564 goto release; 565 } 566 mlen = MHLEN; 567 m->m_pkthdr.len = 0; 568 m->m_pkthdr.rcvif = (struct ifnet *)0; 569 } else { 570 MGET(m, M_TRYWAIT, MT_DATA); 571 if (m == NULL) { 572 error = ENOBUFS; 573 goto release; 574 } 575 mlen = MLEN; 576 } 577 if (resid >= MINCLSIZE) { 578 MCLGET(m, M_TRYWAIT); 579 if ((m->m_flags & M_EXT) == 0) 580 goto nopages; 581 mlen = MCLBYTES; 582 len = min(min(mlen, resid), space); 583 } else { 584 nopages: 585 len = min(min(mlen, resid), space); 586 /* 587 * For datagram protocols, leave room 588 * for protocol headers in first mbuf. 589 */ 590 if (atomic && top == 0 && len < mlen) 591 MH_ALIGN(m, len); 592 } 593 space -= len; 594 error = uiomove(mtod(m, caddr_t), (int)len, uio); 595 resid = uio->uio_resid; 596 m->m_len = len; 597 *mp = m; 598 top->m_pkthdr.len += len; 599 if (error) 600 goto release; 601 mp = &m->m_next; 602 if (resid <= 0) { 603 if (flags & MSG_EOR) 604 top->m_flags |= M_EOR; 605 break; 606 } 607 } while (space > 0 && atomic); 608 if (dontroute) 609 so->so_options |= SO_DONTROUTE; 610 s = splnet(); /* XXX */ 611 /* 612 * XXX all the SS_CANTSENDMORE checks previously 613 * done could be out of date. We could have recieved 614 * a reset packet in an interrupt or maybe we slept 615 * while doing page faults in uiomove() etc. We could 616 * probably recheck again inside the splnet() protection 617 * here, but there are probably other places that this 618 * also happens. We must rethink this. 619 */ 620 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 621 (flags & MSG_OOB) ? PRUS_OOB : 622 /* 623 * If the user set MSG_EOF, the protocol 624 * understands this flag and nothing left to 625 * send then use PRU_SEND_EOF instead of PRU_SEND. 626 */ 627 ((flags & MSG_EOF) && 628 (so->so_proto->pr_flags & PR_IMPLOPCL) && 629 (resid <= 0)) ? 630 PRUS_EOF : 631 /* If there is more to send set PRUS_MORETOCOME */ 632 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0, 633 top, addr, control, p); 634 splx(s); 635 if (dontroute) 636 so->so_options &= ~SO_DONTROUTE; 637 clen = 0; 638 control = 0; 639 top = 0; 640 mp = ⊤ 641 if (error) 642 goto release; 643 } while (resid && space > 0); 644 } while (resid); 645 646 release: 647 sbunlock(&so->so_snd); 648 out: 649 if (top) 650 m_freem(top); 651 if (control) 652 m_freem(control); 653 return (error); 654 } 655 656 /* 657 * Implement receive operations on a socket. 658 * We depend on the way that records are added to the sockbuf 659 * by sbappend*. In particular, each record (mbufs linked through m_next) 660 * must begin with an address if the protocol so specifies, 661 * followed by an optional mbuf or mbufs containing ancillary data, 662 * and then zero or more mbufs of data. 663 * In order to avoid blocking network interrupts for the entire time here, 664 * we splx() while doing the actual copy to user space. 665 * Although the sockbuf is locked, new data may still be appended, 666 * and thus we must maintain consistency of the sockbuf during that time. 667 * 668 * The caller may receive the data as a single mbuf chain by supplying 669 * an mbuf **mp0 for use in returning the chain. The uio is then used 670 * only for the count in uio_resid. 671 */ 672 int 673 soreceive(so, psa, uio, mp0, controlp, flagsp) 674 register struct socket *so; 675 struct sockaddr **psa; 676 struct uio *uio; 677 struct mbuf **mp0; 678 struct mbuf **controlp; 679 int *flagsp; 680 { 681 register struct mbuf *m, **mp; 682 register int flags, len, error, s, offset; 683 struct protosw *pr = so->so_proto; 684 struct mbuf *nextrecord; 685 int moff, type = 0; 686 int orig_resid = uio->uio_resid; 687 688 mp = mp0; 689 if (psa) 690 *psa = 0; 691 if (controlp) 692 *controlp = 0; 693 if (flagsp) 694 flags = *flagsp &~ MSG_EOR; 695 else 696 flags = 0; 697 if (flags & MSG_OOB) { 698 m = m_get(M_TRYWAIT, MT_DATA); 699 if (m == NULL) 700 return (ENOBUFS); 701 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK); 702 if (error) 703 goto bad; 704 do { 705 error = uiomove(mtod(m, caddr_t), 706 (int) min(uio->uio_resid, m->m_len), uio); 707 m = m_free(m); 708 } while (uio->uio_resid && error == 0 && m); 709 bad: 710 if (m) 711 m_freem(m); 712 return (error); 713 } 714 if (mp) 715 *mp = (struct mbuf *)0; 716 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) 717 (*pr->pr_usrreqs->pru_rcvd)(so, 0); 718 719 restart: 720 error = sblock(&so->so_rcv, SBLOCKWAIT(flags)); 721 if (error) 722 return (error); 723 s = splnet(); 724 725 m = so->so_rcv.sb_mb; 726 /* 727 * If we have less data than requested, block awaiting more 728 * (subject to any timeout) if: 729 * 1. the current count is less than the low water mark, or 730 * 2. MSG_WAITALL is set, and it is possible to do the entire 731 * receive operation at once if we block (resid <= hiwat). 732 * 3. MSG_DONTWAIT is not set 733 * If MSG_WAITALL is set but resid is larger than the receive buffer, 734 * we have to do the receive in sections, and thus risk returning 735 * a short count if a timeout or signal occurs after we start. 736 */ 737 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 && 738 so->so_rcv.sb_cc < uio->uio_resid) && 739 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || 740 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) && 741 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) { 742 KASSERT(m != 0 || !so->so_rcv.sb_cc, ("receive 1")); 743 if (so->so_error) { 744 if (m) 745 goto dontblock; 746 error = so->so_error; 747 if ((flags & MSG_PEEK) == 0) 748 so->so_error = 0; 749 goto release; 750 } 751 if (so->so_state & SS_CANTRCVMORE) { 752 if (m) 753 goto dontblock; 754 else 755 goto release; 756 } 757 for (; m; m = m->m_next) 758 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { 759 m = so->so_rcv.sb_mb; 760 goto dontblock; 761 } 762 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 763 (so->so_proto->pr_flags & PR_CONNREQUIRED)) { 764 error = ENOTCONN; 765 goto release; 766 } 767 if (uio->uio_resid == 0) 768 goto release; 769 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) { 770 error = EWOULDBLOCK; 771 goto release; 772 } 773 sbunlock(&so->so_rcv); 774 error = sbwait(&so->so_rcv); 775 splx(s); 776 if (error) 777 return (error); 778 goto restart; 779 } 780 dontblock: 781 if (uio->uio_procp) 782 uio->uio_procp->p_stats->p_ru.ru_msgrcv++; 783 nextrecord = m->m_nextpkt; 784 if (pr->pr_flags & PR_ADDR) { 785 KASSERT(m->m_type == MT_SONAME, ("receive 1a")); 786 orig_resid = 0; 787 if (psa) 788 *psa = dup_sockaddr(mtod(m, struct sockaddr *), 789 mp0 == 0); 790 if (flags & MSG_PEEK) { 791 m = m->m_next; 792 } else { 793 sbfree(&so->so_rcv, m); 794 MFREE(m, so->so_rcv.sb_mb); 795 m = so->so_rcv.sb_mb; 796 } 797 } 798 while (m && m->m_type == MT_CONTROL && error == 0) { 799 if (flags & MSG_PEEK) { 800 if (controlp) 801 *controlp = m_copy(m, 0, m->m_len); 802 m = m->m_next; 803 } else { 804 sbfree(&so->so_rcv, m); 805 if (controlp) { 806 if (pr->pr_domain->dom_externalize && 807 mtod(m, struct cmsghdr *)->cmsg_type == 808 SCM_RIGHTS) 809 error = (*pr->pr_domain->dom_externalize)(m); 810 *controlp = m; 811 so->so_rcv.sb_mb = m->m_next; 812 m->m_next = 0; 813 m = so->so_rcv.sb_mb; 814 } else { 815 MFREE(m, so->so_rcv.sb_mb); 816 m = so->so_rcv.sb_mb; 817 } 818 } 819 if (controlp) { 820 orig_resid = 0; 821 controlp = &(*controlp)->m_next; 822 } 823 } 824 if (m) { 825 if ((flags & MSG_PEEK) == 0) 826 m->m_nextpkt = nextrecord; 827 type = m->m_type; 828 if (type == MT_OOBDATA) 829 flags |= MSG_OOB; 830 } 831 moff = 0; 832 offset = 0; 833 while (m && uio->uio_resid > 0 && error == 0) { 834 if (m->m_type == MT_OOBDATA) { 835 if (type != MT_OOBDATA) 836 break; 837 } else if (type == MT_OOBDATA) 838 break; 839 else 840 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER, 841 ("receive 3")); 842 so->so_state &= ~SS_RCVATMARK; 843 len = uio->uio_resid; 844 if (so->so_oobmark && len > so->so_oobmark - offset) 845 len = so->so_oobmark - offset; 846 if (len > m->m_len - moff) 847 len = m->m_len - moff; 848 /* 849 * If mp is set, just pass back the mbufs. 850 * Otherwise copy them out via the uio, then free. 851 * Sockbuf must be consistent here (points to current mbuf, 852 * it points to next record) when we drop priority; 853 * we must note any additions to the sockbuf when we 854 * block interrupts again. 855 */ 856 if (mp == 0) { 857 splx(s); 858 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio); 859 s = splnet(); 860 if (error) 861 goto release; 862 } else 863 uio->uio_resid -= len; 864 if (len == m->m_len - moff) { 865 if (m->m_flags & M_EOR) 866 flags |= MSG_EOR; 867 if (flags & MSG_PEEK) { 868 m = m->m_next; 869 moff = 0; 870 } else { 871 nextrecord = m->m_nextpkt; 872 sbfree(&so->so_rcv, m); 873 if (mp) { 874 *mp = m; 875 mp = &m->m_next; 876 so->so_rcv.sb_mb = m = m->m_next; 877 *mp = (struct mbuf *)0; 878 } else { 879 MFREE(m, so->so_rcv.sb_mb); 880 m = so->so_rcv.sb_mb; 881 } 882 if (m) 883 m->m_nextpkt = nextrecord; 884 } 885 } else { 886 if (flags & MSG_PEEK) 887 moff += len; 888 else { 889 if (mp) 890 *mp = m_copym(m, 0, len, M_TRYWAIT); 891 m->m_data += len; 892 m->m_len -= len; 893 so->so_rcv.sb_cc -= len; 894 } 895 } 896 if (so->so_oobmark) { 897 if ((flags & MSG_PEEK) == 0) { 898 so->so_oobmark -= len; 899 if (so->so_oobmark == 0) { 900 so->so_state |= SS_RCVATMARK; 901 break; 902 } 903 } else { 904 offset += len; 905 if (offset == so->so_oobmark) 906 break; 907 } 908 } 909 if (flags & MSG_EOR) 910 break; 911 /* 912 * If the MSG_WAITALL flag is set (for non-atomic socket), 913 * we must not quit until "uio->uio_resid == 0" or an error 914 * termination. If a signal/timeout occurs, return 915 * with a short count but without error. 916 * Keep sockbuf locked against other readers. 917 */ 918 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 && 919 !sosendallatonce(so) && !nextrecord) { 920 if (so->so_error || so->so_state & SS_CANTRCVMORE) 921 break; 922 error = sbwait(&so->so_rcv); 923 if (error) { 924 sbunlock(&so->so_rcv); 925 splx(s); 926 return (0); 927 } 928 m = so->so_rcv.sb_mb; 929 if (m) 930 nextrecord = m->m_nextpkt; 931 } 932 } 933 934 if (m && pr->pr_flags & PR_ATOMIC) { 935 flags |= MSG_TRUNC; 936 if ((flags & MSG_PEEK) == 0) 937 (void) sbdroprecord(&so->so_rcv); 938 } 939 if ((flags & MSG_PEEK) == 0) { 940 if (m == 0) 941 so->so_rcv.sb_mb = nextrecord; 942 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) 943 (*pr->pr_usrreqs->pru_rcvd)(so, flags); 944 } 945 if (orig_resid == uio->uio_resid && orig_resid && 946 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { 947 sbunlock(&so->so_rcv); 948 splx(s); 949 goto restart; 950 } 951 952 if (flagsp) 953 *flagsp |= flags; 954 release: 955 sbunlock(&so->so_rcv); 956 splx(s); 957 return (error); 958 } 959 960 int 961 soshutdown(so, how) 962 register struct socket *so; 963 register int how; 964 { 965 register struct protosw *pr = so->so_proto; 966 967 how++; 968 if (how & FREAD) 969 sorflush(so); 970 if (how & FWRITE) 971 return ((*pr->pr_usrreqs->pru_shutdown)(so)); 972 return (0); 973 } 974 975 void 976 sorflush(so) 977 register struct socket *so; 978 { 979 register struct sockbuf *sb = &so->so_rcv; 980 register struct protosw *pr = so->so_proto; 981 register int s; 982 struct sockbuf asb; 983 984 sb->sb_flags |= SB_NOINTR; 985 (void) sblock(sb, M_WAITOK); 986 s = splimp(); 987 socantrcvmore(so); 988 sbunlock(sb); 989 asb = *sb; 990 bzero((caddr_t)sb, sizeof (*sb)); 991 splx(s); 992 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) 993 (*pr->pr_domain->dom_dispose)(asb.sb_mb); 994 sbrelease(&asb, so); 995 } 996 997 #ifdef INET 998 static int 999 do_setopt_accept_filter(so, sopt) 1000 struct socket *so; 1001 struct sockopt *sopt; 1002 { 1003 struct accept_filter_arg *afap = NULL; 1004 struct accept_filter *afp; 1005 struct so_accf *af = so->so_accf; 1006 int error = 0; 1007 1008 /* do not set/remove accept filters on non listen sockets */ 1009 if ((so->so_options & SO_ACCEPTCONN) == 0) { 1010 error = EINVAL; 1011 goto out; 1012 } 1013 1014 /* removing the filter */ 1015 if (sopt == NULL) { 1016 if (af != NULL) { 1017 if (af->so_accept_filter != NULL && 1018 af->so_accept_filter->accf_destroy != NULL) { 1019 af->so_accept_filter->accf_destroy(so); 1020 } 1021 if (af->so_accept_filter_str != NULL) { 1022 FREE(af->so_accept_filter_str, M_ACCF); 1023 } 1024 FREE(af, M_ACCF); 1025 so->so_accf = NULL; 1026 } 1027 so->so_options &= ~SO_ACCEPTFILTER; 1028 return (0); 1029 } 1030 /* adding a filter */ 1031 /* must remove previous filter first */ 1032 if (af != NULL) { 1033 error = EINVAL; 1034 goto out; 1035 } 1036 /* don't put large objects on the kernel stack */ 1037 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK); 1038 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap); 1039 afap->af_name[sizeof(afap->af_name)-1] = '\0'; 1040 afap->af_arg[sizeof(afap->af_arg)-1] = '\0'; 1041 if (error) 1042 goto out; 1043 afp = accept_filt_get(afap->af_name); 1044 if (afp == NULL) { 1045 error = ENOENT; 1046 goto out; 1047 } 1048 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK | M_ZERO); 1049 if (afp->accf_create != NULL) { 1050 if (afap->af_name[0] != '\0') { 1051 int len = strlen(afap->af_name) + 1; 1052 1053 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK); 1054 strcpy(af->so_accept_filter_str, afap->af_name); 1055 } 1056 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg); 1057 if (af->so_accept_filter_arg == NULL) { 1058 FREE(af->so_accept_filter_str, M_ACCF); 1059 FREE(af, M_ACCF); 1060 so->so_accf = NULL; 1061 error = EINVAL; 1062 goto out; 1063 } 1064 } 1065 af->so_accept_filter = afp; 1066 so->so_accf = af; 1067 so->so_options |= SO_ACCEPTFILTER; 1068 out: 1069 if (afap != NULL) 1070 FREE(afap, M_TEMP); 1071 return (error); 1072 } 1073 #endif /* INET */ 1074 1075 /* 1076 * Perhaps this routine, and sooptcopyout(), below, ought to come in 1077 * an additional variant to handle the case where the option value needs 1078 * to be some kind of integer, but not a specific size. 1079 * In addition to their use here, these functions are also called by the 1080 * protocol-level pr_ctloutput() routines. 1081 */ 1082 int 1083 sooptcopyin(sopt, buf, len, minlen) 1084 struct sockopt *sopt; 1085 void *buf; 1086 size_t len; 1087 size_t minlen; 1088 { 1089 size_t valsize; 1090 1091 /* 1092 * If the user gives us more than we wanted, we ignore it, 1093 * but if we don't get the minimum length the caller 1094 * wants, we return EINVAL. On success, sopt->sopt_valsize 1095 * is set to however much we actually retrieved. 1096 */ 1097 if ((valsize = sopt->sopt_valsize) < minlen) 1098 return EINVAL; 1099 if (valsize > len) 1100 sopt->sopt_valsize = valsize = len; 1101 1102 if (sopt->sopt_p != 0) 1103 return (copyin(sopt->sopt_val, buf, valsize)); 1104 1105 bcopy(sopt->sopt_val, buf, valsize); 1106 return 0; 1107 } 1108 1109 int 1110 sosetopt(so, sopt) 1111 struct socket *so; 1112 struct sockopt *sopt; 1113 { 1114 int error, optval; 1115 struct linger l; 1116 struct timeval tv; 1117 u_long val; 1118 1119 error = 0; 1120 if (sopt->sopt_level != SOL_SOCKET) { 1121 if (so->so_proto && so->so_proto->pr_ctloutput) 1122 return ((*so->so_proto->pr_ctloutput) 1123 (so, sopt)); 1124 error = ENOPROTOOPT; 1125 } else { 1126 switch (sopt->sopt_name) { 1127 #ifdef INET 1128 case SO_ACCEPTFILTER: 1129 error = do_setopt_accept_filter(so, sopt); 1130 if (error) 1131 goto bad; 1132 break; 1133 #endif 1134 case SO_LINGER: 1135 error = sooptcopyin(sopt, &l, sizeof l, sizeof l); 1136 if (error) 1137 goto bad; 1138 1139 so->so_linger = l.l_linger; 1140 if (l.l_onoff) 1141 so->so_options |= SO_LINGER; 1142 else 1143 so->so_options &= ~SO_LINGER; 1144 break; 1145 1146 case SO_DEBUG: 1147 case SO_KEEPALIVE: 1148 case SO_DONTROUTE: 1149 case SO_USELOOPBACK: 1150 case SO_BROADCAST: 1151 case SO_REUSEADDR: 1152 case SO_REUSEPORT: 1153 case SO_OOBINLINE: 1154 case SO_TIMESTAMP: 1155 error = sooptcopyin(sopt, &optval, sizeof optval, 1156 sizeof optval); 1157 if (error) 1158 goto bad; 1159 if (optval) 1160 so->so_options |= sopt->sopt_name; 1161 else 1162 so->so_options &= ~sopt->sopt_name; 1163 break; 1164 1165 case SO_SNDBUF: 1166 case SO_RCVBUF: 1167 case SO_SNDLOWAT: 1168 case SO_RCVLOWAT: 1169 error = sooptcopyin(sopt, &optval, sizeof optval, 1170 sizeof optval); 1171 if (error) 1172 goto bad; 1173 1174 /* 1175 * Values < 1 make no sense for any of these 1176 * options, so disallow them. 1177 */ 1178 if (optval < 1) { 1179 error = EINVAL; 1180 goto bad; 1181 } 1182 1183 switch (sopt->sopt_name) { 1184 case SO_SNDBUF: 1185 case SO_RCVBUF: 1186 if (sbreserve(sopt->sopt_name == SO_SNDBUF ? 1187 &so->so_snd : &so->so_rcv, (u_long)optval, 1188 so, curproc) == 0) { 1189 error = ENOBUFS; 1190 goto bad; 1191 } 1192 break; 1193 1194 /* 1195 * Make sure the low-water is never greater than 1196 * the high-water. 1197 */ 1198 case SO_SNDLOWAT: 1199 so->so_snd.sb_lowat = 1200 (optval > so->so_snd.sb_hiwat) ? 1201 so->so_snd.sb_hiwat : optval; 1202 break; 1203 case SO_RCVLOWAT: 1204 so->so_rcv.sb_lowat = 1205 (optval > so->so_rcv.sb_hiwat) ? 1206 so->so_rcv.sb_hiwat : optval; 1207 break; 1208 } 1209 break; 1210 1211 case SO_SNDTIMEO: 1212 case SO_RCVTIMEO: 1213 error = sooptcopyin(sopt, &tv, sizeof tv, 1214 sizeof tv); 1215 if (error) 1216 goto bad; 1217 1218 /* assert(hz > 0); */ 1219 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz || 1220 tv.tv_usec < 0 || tv.tv_usec >= 1000000) { 1221 error = EDOM; 1222 goto bad; 1223 } 1224 /* assert(tick > 0); */ 1225 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */ 1226 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick; 1227 if (val > SHRT_MAX) { 1228 error = EDOM; 1229 goto bad; 1230 } 1231 1232 switch (sopt->sopt_name) { 1233 case SO_SNDTIMEO: 1234 so->so_snd.sb_timeo = val; 1235 break; 1236 case SO_RCVTIMEO: 1237 so->so_rcv.sb_timeo = val; 1238 break; 1239 } 1240 break; 1241 default: 1242 error = ENOPROTOOPT; 1243 break; 1244 } 1245 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) { 1246 (void) ((*so->so_proto->pr_ctloutput) 1247 (so, sopt)); 1248 } 1249 } 1250 bad: 1251 return (error); 1252 } 1253 1254 /* Helper routine for getsockopt */ 1255 int 1256 sooptcopyout(sopt, buf, len) 1257 struct sockopt *sopt; 1258 void *buf; 1259 size_t len; 1260 { 1261 int error; 1262 size_t valsize; 1263 1264 error = 0; 1265 1266 /* 1267 * Documented get behavior is that we always return a value, 1268 * possibly truncated to fit in the user's buffer. 1269 * Traditional behavior is that we always tell the user 1270 * precisely how much we copied, rather than something useful 1271 * like the total amount we had available for her. 1272 * Note that this interface is not idempotent; the entire answer must 1273 * generated ahead of time. 1274 */ 1275 valsize = min(len, sopt->sopt_valsize); 1276 sopt->sopt_valsize = valsize; 1277 if (sopt->sopt_val != 0) { 1278 if (sopt->sopt_p != 0) 1279 error = copyout(buf, sopt->sopt_val, valsize); 1280 else 1281 bcopy(buf, sopt->sopt_val, valsize); 1282 } 1283 return error; 1284 } 1285 1286 int 1287 sogetopt(so, sopt) 1288 struct socket *so; 1289 struct sockopt *sopt; 1290 { 1291 int error, optval; 1292 struct linger l; 1293 struct timeval tv; 1294 #ifdef INET 1295 struct accept_filter_arg *afap; 1296 #endif 1297 1298 error = 0; 1299 if (sopt->sopt_level != SOL_SOCKET) { 1300 if (so->so_proto && so->so_proto->pr_ctloutput) { 1301 return ((*so->so_proto->pr_ctloutput) 1302 (so, sopt)); 1303 } else 1304 return (ENOPROTOOPT); 1305 } else { 1306 switch (sopt->sopt_name) { 1307 #ifdef INET 1308 case SO_ACCEPTFILTER: 1309 if ((so->so_options & SO_ACCEPTCONN) == 0) 1310 return (EINVAL); 1311 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), 1312 M_TEMP, M_WAITOK | M_ZERO); 1313 if ((so->so_options & SO_ACCEPTFILTER) != 0) { 1314 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name); 1315 if (so->so_accf->so_accept_filter_str != NULL) 1316 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str); 1317 } 1318 error = sooptcopyout(sopt, afap, sizeof(*afap)); 1319 FREE(afap, M_TEMP); 1320 break; 1321 #endif 1322 1323 case SO_LINGER: 1324 l.l_onoff = so->so_options & SO_LINGER; 1325 l.l_linger = so->so_linger; 1326 error = sooptcopyout(sopt, &l, sizeof l); 1327 break; 1328 1329 case SO_USELOOPBACK: 1330 case SO_DONTROUTE: 1331 case SO_DEBUG: 1332 case SO_KEEPALIVE: 1333 case SO_REUSEADDR: 1334 case SO_REUSEPORT: 1335 case SO_BROADCAST: 1336 case SO_OOBINLINE: 1337 case SO_TIMESTAMP: 1338 optval = so->so_options & sopt->sopt_name; 1339 integer: 1340 error = sooptcopyout(sopt, &optval, sizeof optval); 1341 break; 1342 1343 case SO_TYPE: 1344 optval = so->so_type; 1345 goto integer; 1346 1347 case SO_ERROR: 1348 optval = so->so_error; 1349 so->so_error = 0; 1350 goto integer; 1351 1352 case SO_SNDBUF: 1353 optval = so->so_snd.sb_hiwat; 1354 goto integer; 1355 1356 case SO_RCVBUF: 1357 optval = so->so_rcv.sb_hiwat; 1358 goto integer; 1359 1360 case SO_SNDLOWAT: 1361 optval = so->so_snd.sb_lowat; 1362 goto integer; 1363 1364 case SO_RCVLOWAT: 1365 optval = so->so_rcv.sb_lowat; 1366 goto integer; 1367 1368 case SO_SNDTIMEO: 1369 case SO_RCVTIMEO: 1370 optval = (sopt->sopt_name == SO_SNDTIMEO ? 1371 so->so_snd.sb_timeo : so->so_rcv.sb_timeo); 1372 1373 tv.tv_sec = optval / hz; 1374 tv.tv_usec = (optval % hz) * tick; 1375 error = sooptcopyout(sopt, &tv, sizeof tv); 1376 break; 1377 1378 default: 1379 error = ENOPROTOOPT; 1380 break; 1381 } 1382 return (error); 1383 } 1384 } 1385 1386 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */ 1387 int 1388 soopt_getm(struct sockopt *sopt, struct mbuf **mp) 1389 { 1390 struct mbuf *m, *m_prev; 1391 int sopt_size = sopt->sopt_valsize; 1392 1393 MGET(m, sopt->sopt_p ? M_TRYWAIT : M_DONTWAIT, MT_DATA); 1394 if (m == 0) 1395 return ENOBUFS; 1396 if (sopt_size > MLEN) { 1397 MCLGET(m, sopt->sopt_p ? M_TRYWAIT : M_DONTWAIT); 1398 if ((m->m_flags & M_EXT) == 0) { 1399 m_free(m); 1400 return ENOBUFS; 1401 } 1402 m->m_len = min(MCLBYTES, sopt_size); 1403 } else { 1404 m->m_len = min(MLEN, sopt_size); 1405 } 1406 sopt_size -= m->m_len; 1407 *mp = m; 1408 m_prev = m; 1409 1410 while (sopt_size) { 1411 MGET(m, sopt->sopt_p ? M_TRYWAIT : M_DONTWAIT, MT_DATA); 1412 if (m == 0) { 1413 m_freem(*mp); 1414 return ENOBUFS; 1415 } 1416 if (sopt_size > MLEN) { 1417 MCLGET(m, sopt->sopt_p ? M_TRYWAIT : M_DONTWAIT); 1418 if ((m->m_flags & M_EXT) == 0) { 1419 m_freem(*mp); 1420 return ENOBUFS; 1421 } 1422 m->m_len = min(MCLBYTES, sopt_size); 1423 } else { 1424 m->m_len = min(MLEN, sopt_size); 1425 } 1426 sopt_size -= m->m_len; 1427 m_prev->m_next = m; 1428 m_prev = m; 1429 } 1430 return 0; 1431 } 1432 1433 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */ 1434 int 1435 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m) 1436 { 1437 struct mbuf *m0 = m; 1438 1439 if (sopt->sopt_val == NULL) 1440 return 0; 1441 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 1442 if (sopt->sopt_p != NULL) { 1443 int error; 1444 1445 error = copyin(sopt->sopt_val, mtod(m, char *), 1446 m->m_len); 1447 if (error != 0) { 1448 m_freem(m0); 1449 return(error); 1450 } 1451 } else 1452 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len); 1453 sopt->sopt_valsize -= m->m_len; 1454 (caddr_t)sopt->sopt_val += m->m_len; 1455 m = m->m_next; 1456 } 1457 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */ 1458 panic("ip6_sooptmcopyin"); 1459 return 0; 1460 } 1461 1462 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */ 1463 int 1464 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m) 1465 { 1466 struct mbuf *m0 = m; 1467 size_t valsize = 0; 1468 1469 if (sopt->sopt_val == NULL) 1470 return 0; 1471 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 1472 if (sopt->sopt_p != NULL) { 1473 int error; 1474 1475 error = copyout(mtod(m, char *), sopt->sopt_val, 1476 m->m_len); 1477 if (error != 0) { 1478 m_freem(m0); 1479 return(error); 1480 } 1481 } else 1482 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len); 1483 sopt->sopt_valsize -= m->m_len; 1484 (caddr_t)sopt->sopt_val += m->m_len; 1485 valsize += m->m_len; 1486 m = m->m_next; 1487 } 1488 if (m != NULL) { 1489 /* enough soopt buffer should be given from user-land */ 1490 m_freem(m0); 1491 return(EINVAL); 1492 } 1493 sopt->sopt_valsize = valsize; 1494 return 0; 1495 } 1496 1497 void 1498 sohasoutofband(so) 1499 register struct socket *so; 1500 { 1501 if (so->so_sigio != NULL) 1502 pgsigio(so->so_sigio, SIGURG, 0); 1503 selwakeup(&so->so_rcv.sb_sel); 1504 } 1505 1506 int 1507 sopoll(struct socket *so, int events, struct ucred *cred, struct proc *p) 1508 { 1509 int revents = 0; 1510 int s = splnet(); 1511 1512 if (events & (POLLIN | POLLRDNORM)) 1513 if (soreadable(so)) 1514 revents |= events & (POLLIN | POLLRDNORM); 1515 1516 if (events & (POLLOUT | POLLWRNORM)) 1517 if (sowriteable(so)) 1518 revents |= events & (POLLOUT | POLLWRNORM); 1519 1520 if (events & (POLLPRI | POLLRDBAND)) 1521 if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) 1522 revents |= events & (POLLPRI | POLLRDBAND); 1523 1524 if (revents == 0) { 1525 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) { 1526 selrecord(p, &so->so_rcv.sb_sel); 1527 so->so_rcv.sb_flags |= SB_SEL; 1528 } 1529 1530 if (events & (POLLOUT | POLLWRNORM)) { 1531 selrecord(p, &so->so_snd.sb_sel); 1532 so->so_snd.sb_flags |= SB_SEL; 1533 } 1534 } 1535 1536 splx(s); 1537 return (revents); 1538 } 1539 1540 static int 1541 filt_sorattach(struct knote *kn) 1542 { 1543 struct socket *so = (struct socket *)kn->kn_fp->f_data; 1544 int s = splnet(); 1545 1546 if (so->so_options & SO_ACCEPTCONN) 1547 kn->kn_fop = &solisten_filtops; 1548 SLIST_INSERT_HEAD(&so->so_rcv.sb_sel.si_note, kn, kn_selnext); 1549 so->so_rcv.sb_flags |= SB_KNOTE; 1550 splx(s); 1551 return (0); 1552 } 1553 1554 static void 1555 filt_sordetach(struct knote *kn) 1556 { 1557 struct socket *so = (struct socket *)kn->kn_fp->f_data; 1558 int s = splnet(); 1559 1560 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext); 1561 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note)) 1562 so->so_rcv.sb_flags &= ~SB_KNOTE; 1563 splx(s); 1564 } 1565 1566 /*ARGSUSED*/ 1567 static int 1568 filt_soread(struct knote *kn, long hint) 1569 { 1570 struct socket *so = (struct socket *)kn->kn_fp->f_data; 1571 1572 kn->kn_data = so->so_rcv.sb_cc; 1573 if (so->so_state & SS_CANTRCVMORE) { 1574 kn->kn_flags |= EV_EOF; 1575 return (1); 1576 } 1577 if (so->so_error) /* temporary udp error */ 1578 return (1); 1579 return (kn->kn_data >= so->so_rcv.sb_lowat); 1580 } 1581 1582 static int 1583 filt_sowattach(struct knote *kn) 1584 { 1585 struct socket *so = (struct socket *)kn->kn_fp->f_data; 1586 int s = splnet(); 1587 1588 SLIST_INSERT_HEAD(&so->so_snd.sb_sel.si_note, kn, kn_selnext); 1589 so->so_snd.sb_flags |= SB_KNOTE; 1590 splx(s); 1591 return (0); 1592 } 1593 1594 static void 1595 filt_sowdetach(struct knote *kn) 1596 { 1597 struct socket *so = (struct socket *)kn->kn_fp->f_data; 1598 int s = splnet(); 1599 1600 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext); 1601 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note)) 1602 so->so_snd.sb_flags &= ~SB_KNOTE; 1603 splx(s); 1604 } 1605 1606 /*ARGSUSED*/ 1607 static int 1608 filt_sowrite(struct knote *kn, long hint) 1609 { 1610 struct socket *so = (struct socket *)kn->kn_fp->f_data; 1611 1612 kn->kn_data = sbspace(&so->so_snd); 1613 if (so->so_state & SS_CANTSENDMORE) { 1614 kn->kn_flags |= EV_EOF; 1615 return (1); 1616 } 1617 if (so->so_error) /* temporary udp error */ 1618 return (1); 1619 if (((so->so_state & SS_ISCONNECTED) == 0) && 1620 (so->so_proto->pr_flags & PR_CONNREQUIRED)) 1621 return (0); 1622 return (kn->kn_data >= so->so_snd.sb_lowat); 1623 } 1624 1625 /*ARGSUSED*/ 1626 static int 1627 filt_solisten(struct knote *kn, long hint) 1628 { 1629 struct socket *so = (struct socket *)kn->kn_fp->f_data; 1630 1631 kn->kn_data = so->so_qlen - so->so_incqlen; 1632 return (! TAILQ_EMPTY(&so->so_comp)); 1633 } 1634