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