uipc_socket.c revision 116182
1120945Snectar/* 2233294Sstas * Copyright (c) 1982, 1986, 1988, 1990, 1993 3233294Sstas * The Regents of the University of California. All rights reserved. 4233294Sstas * 5120945Snectar * Redistribution and use in source and binary forms, with or without 6233294Sstas * modification, are permitted provided that the following conditions 7233294Sstas * are met: 8233294Sstas * 1. Redistributions of source code must retain the above copyright 9120945Snectar * notice, this list of conditions and the following disclaimer. 10233294Sstas * 2. Redistributions in binary form must reproduce the above copyright 11233294Sstas * notice, this list of conditions and the following disclaimer in the 12120945Snectar * documentation and/or other materials provided with the distribution. 13233294Sstas * 3. All advertising materials mentioning features or use of this software 14233294Sstas * must display the following acknowledgement: 15233294Sstas * This product includes software developed by the University of 16120945Snectar * California, Berkeley and its contributors. 17233294Sstas * 4. Neither the name of the University nor the names of its contributors 18233294Sstas * may be used to endorse or promote products derived from this software 19233294Sstas * without specific prior written permission. 20120945Snectar * 21233294Sstas * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24233294Sstas * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31233294Sstas * SUCH DAMAGE. 32120945Snectar * 33120945Snectar * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94 34233294Sstas */ 35120945Snectar 36120945Snectar#include <sys/cdefs.h> 37120945Snectar__FBSDID("$FreeBSD: head/sys/kern/uipc_socket.c 116182 2003-06-11 00:56:59Z obrien $"); 38120945Snectar 39178825Sdfr#include "opt_inet.h" 40178825Sdfr#include "opt_mac.h" 41233294Sstas#include "opt_zero.h" 42120945Snectar 43120945Snectar#include <sys/param.h> 44120945Snectar#include <sys/systm.h> 45120945Snectar#include <sys/fcntl.h> 46120945Snectar#include <sys/limits.h> 47120945Snectar#include <sys/lock.h> 48178825Sdfr#include <sys/mac.h> 49178825Sdfr#include <sys/malloc.h> 50178825Sdfr#include <sys/mbuf.h> 51178825Sdfr#include <sys/mutex.h> 52178825Sdfr#include <sys/domain.h> 53178825Sdfr#include <sys/file.h> /* for struct knote */ 54178825Sdfr#include <sys/kernel.h> 55178825Sdfr#include <sys/event.h> 56178825Sdfr#include <sys/poll.h> 57178825Sdfr#include <sys/proc.h> 58178825Sdfr#include <sys/protosw.h> 59178825Sdfr#include <sys/socket.h> 60178825Sdfr#include <sys/socketvar.h> 61178825Sdfr#include <sys/resourcevar.h> 62178825Sdfr#include <sys/signalvar.h> 63178825Sdfr#include <sys/sysctl.h> 64178825Sdfr#include <sys/uio.h> 65178825Sdfr#include <sys/jail.h> 66178825Sdfr 67178825Sdfr#include <vm/uma.h> 68120945Snectar 69120945Snectar 70120945Snectar#ifdef INET 71120945Snectarstatic int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt); 72120945Snectar#endif 73120945Snectar 74120945Snectarstatic void filt_sordetach(struct knote *kn); 75120945Snectarstatic int filt_soread(struct knote *kn, long hint); 76120945Snectarstatic void filt_sowdetach(struct knote *kn); 77120945Snectarstatic int filt_sowrite(struct knote *kn, long hint); 78178825Sdfrstatic int filt_solisten(struct knote *kn, long hint); 79120945Snectar 80120945Snectarstatic struct filterops solisten_filtops = 81120945Snectar { 1, NULL, filt_sordetach, filt_solisten }; 82120945Snectarstatic struct filterops soread_filtops = 83120945Snectar { 1, NULL, filt_sordetach, filt_soread }; 84120945Snectarstatic struct filterops sowrite_filtops = 85178825Sdfr { 1, NULL, filt_sowdetach, filt_sowrite }; 86120945Snectar 87120945Snectaruma_zone_t socket_zone; 88120945Snectarso_gen_t so_gencnt; /* generation count for sockets */ 89120945Snectar 90120945SnectarMALLOC_DEFINE(M_SONAME, "soname", "socket name"); 91120945SnectarMALLOC_DEFINE(M_PCB, "pcb", "protocol control block"); 92120945Snectar 93120945SnectarSYSCTL_DECL(_kern_ipc); 94120945Snectar 95120945Snectarstatic int somaxconn = SOMAXCONN; 96120945SnectarSYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW, 97120945Snectar &somaxconn, 0, "Maximum pending socket connection queue size"); 98120945Snectarstatic int numopensockets; 99120945SnectarSYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD, 100120945Snectar &numopensockets, 0, "Number of open sockets"); 101120945Snectar#ifdef ZERO_COPY_SOCKETS 102120945Snectar/* These aren't static because they're used in other files. */ 103120945Snectarint so_zero_copy_send = 1; 104120945Snectarint so_zero_copy_receive = 1; 105178825SdfrSYSCTL_NODE(_kern_ipc, OID_AUTO, zero_copy, CTLFLAG_RD, 0, 106120945Snectar "Zero copy controls"); 107178825SdfrSYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, receive, CTLFLAG_RW, 108233294Sstas &so_zero_copy_receive, 0, "Enable zero copy receive"); 109233294SstasSYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, send, CTLFLAG_RW, 110233294Sstas &so_zero_copy_send, 0, "Enable zero copy send"); 111178825Sdfr#endif /* ZERO_COPY_SOCKETS */ 112178825Sdfr 113233294Sstas 114120945Snectar/* 115178825Sdfr * Socket operation routines. 116178825Sdfr * These routines are called by the routines in 117178825Sdfr * sys_socket.c or from a system process, and 118178825Sdfr * implement the semantics of socket operations by 119178825Sdfr * switching out to the protocol specific routines. 120178825Sdfr */ 121178825Sdfr 122178825Sdfr/* 123178825Sdfr * Get a socket structure from our zone, and initialize it. 124178825Sdfr * Note that it would probably be better to allocate socket 125178825Sdfr * and PCB at the same time, but I'm not convinced that all 126178825Sdfr * the protocols can be easily modified to do this. 127233294Sstas * 128233294Sstas * soalloc() returns a socket with a ref count of 0. 129178825Sdfr */ 130233294Sstasstruct socket * 131233294Sstassoalloc(waitok) 132233294Sstas int waitok; 133233294Sstas{ 134233294Sstas struct socket *so; 135233294Sstas#ifdef MAC 136233294Sstas int error; 137233294Sstas#endif 138233294Sstas int flag; 139233294Sstas 140120945Snectar if (waitok == 1) 141120945Snectar flag = M_WAITOK; 142120945Snectar else 143120945Snectar flag = M_NOWAIT; 144 flag |= M_ZERO; 145 so = uma_zalloc(socket_zone, flag); 146 if (so) { 147#ifdef MAC 148 error = mac_init_socket(so, flag); 149 if (error != 0) { 150 uma_zfree(socket_zone, so); 151 so = NULL; 152 return so; 153 } 154#endif 155 /* XXX race condition for reentrant kernel */ 156 so->so_gencnt = ++so_gencnt; 157 /* sx_init(&so->so_sxlock, "socket sxlock"); */ 158 TAILQ_INIT(&so->so_aiojobq); 159 ++numopensockets; 160 } 161 return so; 162} 163 164/* 165 * socreate returns a socket with a ref count of 1. The socket should be 166 * closed with soclose(). 167 */ 168int 169socreate(dom, aso, type, proto, cred, td) 170 int dom; 171 struct socket **aso; 172 int type; 173 int proto; 174 struct ucred *cred; 175 struct thread *td; 176{ 177 struct protosw *prp; 178 struct socket *so; 179 int error; 180 181 if (proto) 182 prp = pffindproto(dom, proto, type); 183 else 184 prp = pffindtype(dom, type); 185 186 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0) 187 return (EPROTONOSUPPORT); 188 189 if (jailed(cred) && jail_socket_unixiproute_only && 190 prp->pr_domain->dom_family != PF_LOCAL && 191 prp->pr_domain->dom_family != PF_INET && 192 prp->pr_domain->dom_family != PF_ROUTE) { 193 return (EPROTONOSUPPORT); 194 } 195 196 if (prp->pr_type != type) 197 return (EPROTOTYPE); 198 so = soalloc(M_NOWAIT); 199 if (so == NULL) 200 return (ENOBUFS); 201 202 TAILQ_INIT(&so->so_incomp); 203 TAILQ_INIT(&so->so_comp); 204 so->so_type = type; 205 so->so_cred = crhold(cred); 206 so->so_proto = prp; 207#ifdef MAC 208 mac_create_socket(cred, so); 209#endif 210 soref(so); 211 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td); 212 if (error) { 213 so->so_state |= SS_NOFDREF; 214 sorele(so); 215 return (error); 216 } 217 *aso = so; 218 return (0); 219} 220 221int 222sobind(so, nam, td) 223 struct socket *so; 224 struct sockaddr *nam; 225 struct thread *td; 226{ 227 int s = splnet(); 228 int error; 229 230 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td); 231 splx(s); 232 return (error); 233} 234 235void 236sodealloc(struct socket *so) 237{ 238 239 KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count)); 240 so->so_gencnt = ++so_gencnt; 241 if (so->so_rcv.sb_hiwat) 242 (void)chgsbsize(so->so_cred->cr_uidinfo, 243 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY); 244 if (so->so_snd.sb_hiwat) 245 (void)chgsbsize(so->so_cred->cr_uidinfo, 246 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY); 247#ifdef INET 248 /* remove acccept filter if one is present. */ 249 if (so->so_accf != NULL) 250 do_setopt_accept_filter(so, NULL); 251#endif 252#ifdef MAC 253 mac_destroy_socket(so); 254#endif 255 crfree(so->so_cred); 256 /* sx_destroy(&so->so_sxlock); */ 257 uma_zfree(socket_zone, so); 258 --numopensockets; 259} 260 261int 262solisten(so, backlog, td) 263 struct socket *so; 264 int backlog; 265 struct thread *td; 266{ 267 int s, error; 268 269 s = splnet(); 270 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING)) { 271 splx(s); 272 return (EINVAL); 273 } 274 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, td); 275 if (error) { 276 splx(s); 277 return (error); 278 } 279 if (TAILQ_EMPTY(&so->so_comp)) 280 so->so_options |= SO_ACCEPTCONN; 281 if (backlog < 0 || backlog > somaxconn) 282 backlog = somaxconn; 283 so->so_qlimit = backlog; 284 splx(s); 285 return (0); 286} 287 288void 289sofree(so) 290 struct socket *so; 291{ 292 struct socket *head = so->so_head; 293 294 KASSERT(so->so_count == 0, ("socket %p so_count not 0", so)); 295 296 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) 297 return; 298 if (head != NULL) { 299 if (so->so_state & SS_INCOMP) { 300 TAILQ_REMOVE(&head->so_incomp, so, so_list); 301 head->so_incqlen--; 302 } else if (so->so_state & SS_COMP) { 303 /* 304 * We must not decommission a socket that's 305 * on the accept(2) queue. If we do, then 306 * accept(2) may hang after select(2) indicated 307 * that the listening socket was ready. 308 */ 309 return; 310 } else { 311 panic("sofree: not queued"); 312 } 313 so->so_state &= ~SS_INCOMP; 314 so->so_head = NULL; 315 } 316 sbrelease(&so->so_snd, so); 317 sorflush(so); 318 sodealloc(so); 319} 320 321/* 322 * Close a socket on last file table reference removal. 323 * Initiate disconnect if connected. 324 * Free socket when disconnect complete. 325 * 326 * This function will sorele() the socket. Note that soclose() may be 327 * called prior to the ref count reaching zero. The actual socket 328 * structure will not be freed until the ref count reaches zero. 329 */ 330int 331soclose(so) 332 struct socket *so; 333{ 334 int s = splnet(); /* conservative */ 335 int error = 0; 336 337 funsetown(&so->so_sigio); 338 if (so->so_options & SO_ACCEPTCONN) { 339 struct socket *sp, *sonext; 340 341 sp = TAILQ_FIRST(&so->so_incomp); 342 for (; sp != NULL; sp = sonext) { 343 sonext = TAILQ_NEXT(sp, so_list); 344 (void) soabort(sp); 345 } 346 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) { 347 sonext = TAILQ_NEXT(sp, so_list); 348 /* Dequeue from so_comp since sofree() won't do it */ 349 TAILQ_REMOVE(&so->so_comp, sp, so_list); 350 so->so_qlen--; 351 sp->so_state &= ~SS_COMP; 352 sp->so_head = NULL; 353 (void) soabort(sp); 354 } 355 } 356 if (so->so_pcb == 0) 357 goto discard; 358 if (so->so_state & SS_ISCONNECTED) { 359 if ((so->so_state & SS_ISDISCONNECTING) == 0) { 360 error = sodisconnect(so); 361 if (error) 362 goto drop; 363 } 364 if (so->so_options & SO_LINGER) { 365 if ((so->so_state & SS_ISDISCONNECTING) && 366 (so->so_state & SS_NBIO)) 367 goto drop; 368 while (so->so_state & SS_ISCONNECTED) { 369 error = tsleep(&so->so_timeo, 370 PSOCK | PCATCH, "soclos", so->so_linger * hz); 371 if (error) 372 break; 373 } 374 } 375 } 376drop: 377 if (so->so_pcb) { 378 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so); 379 if (error == 0) 380 error = error2; 381 } 382discard: 383 if (so->so_state & SS_NOFDREF) 384 panic("soclose: NOFDREF"); 385 so->so_state |= SS_NOFDREF; 386 sorele(so); 387 splx(s); 388 return (error); 389} 390 391/* 392 * Must be called at splnet... 393 */ 394int 395soabort(so) 396 struct socket *so; 397{ 398 int error; 399 400 error = (*so->so_proto->pr_usrreqs->pru_abort)(so); 401 if (error) { 402 sotryfree(so); /* note: does not decrement the ref count */ 403 return error; 404 } 405 return (0); 406} 407 408int 409soaccept(so, nam) 410 struct socket *so; 411 struct sockaddr **nam; 412{ 413 int s = splnet(); 414 int error; 415 416 if ((so->so_state & SS_NOFDREF) == 0) 417 panic("soaccept: !NOFDREF"); 418 so->so_state &= ~SS_NOFDREF; 419 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam); 420 splx(s); 421 return (error); 422} 423 424int 425soconnect(so, nam, td) 426 struct socket *so; 427 struct sockaddr *nam; 428 struct thread *td; 429{ 430 int s; 431 int error; 432 433 if (so->so_options & SO_ACCEPTCONN) 434 return (EOPNOTSUPP); 435 s = splnet(); 436 /* 437 * If protocol is connection-based, can only connect once. 438 * Otherwise, if connected, try to disconnect first. 439 * This allows user to disconnect by connecting to, e.g., 440 * a null address. 441 */ 442 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && 443 ((so->so_proto->pr_flags & PR_CONNREQUIRED) || 444 (error = sodisconnect(so)))) 445 error = EISCONN; 446 else 447 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td); 448 splx(s); 449 return (error); 450} 451 452int 453soconnect2(so1, so2) 454 struct socket *so1; 455 struct socket *so2; 456{ 457 int s = splnet(); 458 int error; 459 460 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2); 461 splx(s); 462 return (error); 463} 464 465int 466sodisconnect(so) 467 struct socket *so; 468{ 469 int s = splnet(); 470 int error; 471 472 if ((so->so_state & SS_ISCONNECTED) == 0) { 473 error = ENOTCONN; 474 goto bad; 475 } 476 if (so->so_state & SS_ISDISCONNECTING) { 477 error = EALREADY; 478 goto bad; 479 } 480 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so); 481bad: 482 splx(s); 483 return (error); 484} 485 486#define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK) 487/* 488 * Send on a socket. 489 * If send must go all at once and message is larger than 490 * send buffering, then hard error. 491 * Lock against other senders. 492 * If must go all at once and not enough room now, then 493 * inform user that this would block and do nothing. 494 * Otherwise, if nonblocking, send as much as possible. 495 * The data to be sent is described by "uio" if nonzero, 496 * otherwise by the mbuf chain "top" (which must be null 497 * if uio is not). Data provided in mbuf chain must be small 498 * enough to send all at once. 499 * 500 * Returns nonzero on error, timeout or signal; callers 501 * must check for short counts if EINTR/ERESTART are returned. 502 * Data and control buffers are freed on return. 503 */ 504 505#ifdef ZERO_COPY_SOCKETS 506struct so_zerocopy_stats{ 507 int size_ok; 508 int align_ok; 509 int found_ifp; 510}; 511struct so_zerocopy_stats so_zerocp_stats = {0,0,0}; 512#include <netinet/in.h> 513#include <net/route.h> 514#include <netinet/in_pcb.h> 515#include <vm/vm.h> 516#include <vm/vm_page.h> 517#include <vm/vm_object.h> 518#endif /*ZERO_COPY_SOCKETS*/ 519 520int 521sosend(so, addr, uio, top, control, flags, td) 522 struct socket *so; 523 struct sockaddr *addr; 524 struct uio *uio; 525 struct mbuf *top; 526 struct mbuf *control; 527 int flags; 528 struct thread *td; 529{ 530 struct mbuf **mp; 531 struct mbuf *m; 532 long space, len, resid; 533 int clen = 0, error, s, dontroute, mlen; 534 int atomic = sosendallatonce(so) || top; 535#ifdef ZERO_COPY_SOCKETS 536 int cow_send; 537#endif /* ZERO_COPY_SOCKETS */ 538 539 if (uio) 540 resid = uio->uio_resid; 541 else 542 resid = top->m_pkthdr.len; 543 /* 544 * In theory resid should be unsigned. 545 * However, space must be signed, as it might be less than 0 546 * if we over-committed, and we must use a signed comparison 547 * of space and resid. On the other hand, a negative resid 548 * causes us to loop sending 0-length segments to the protocol. 549 * 550 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM 551 * type sockets since that's an error. 552 */ 553 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) { 554 error = EINVAL; 555 goto out; 556 } 557 558 dontroute = 559 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && 560 (so->so_proto->pr_flags & PR_ATOMIC); 561 if (td) 562 td->td_proc->p_stats->p_ru.ru_msgsnd++; 563 if (control) 564 clen = control->m_len; 565#define snderr(errno) { error = (errno); splx(s); goto release; } 566 567restart: 568 error = sblock(&so->so_snd, SBLOCKWAIT(flags)); 569 if (error) 570 goto out; 571 do { 572 s = splnet(); 573 if (so->so_state & SS_CANTSENDMORE) 574 snderr(EPIPE); 575 if (so->so_error) { 576 error = so->so_error; 577 so->so_error = 0; 578 splx(s); 579 goto release; 580 } 581 if ((so->so_state & SS_ISCONNECTED) == 0) { 582 /* 583 * `sendto' and `sendmsg' is allowed on a connection- 584 * based socket if it supports implied connect. 585 * Return ENOTCONN if not connected and no address is 586 * supplied. 587 */ 588 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) && 589 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) { 590 if ((so->so_state & SS_ISCONFIRMING) == 0 && 591 !(resid == 0 && clen != 0)) 592 snderr(ENOTCONN); 593 } else if (addr == 0) 594 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ? 595 ENOTCONN : EDESTADDRREQ); 596 } 597 space = sbspace(&so->so_snd); 598 if (flags & MSG_OOB) 599 space += 1024; 600 if ((atomic && resid > so->so_snd.sb_hiwat) || 601 clen > so->so_snd.sb_hiwat) 602 snderr(EMSGSIZE); 603 if (space < resid + clen && 604 (atomic || space < so->so_snd.sb_lowat || space < clen)) { 605 if (so->so_state & SS_NBIO) 606 snderr(EWOULDBLOCK); 607 sbunlock(&so->so_snd); 608 error = sbwait(&so->so_snd); 609 splx(s); 610 if (error) 611 goto out; 612 goto restart; 613 } 614 splx(s); 615 mp = ⊤ 616 space -= clen; 617 do { 618 if (uio == NULL) { 619 /* 620 * Data is prepackaged in "top". 621 */ 622 resid = 0; 623 if (flags & MSG_EOR) 624 top->m_flags |= M_EOR; 625 } else do { 626#ifdef ZERO_COPY_SOCKETS 627 cow_send = 0; 628#endif /* ZERO_COPY_SOCKETS */ 629 if (top == 0) { 630 MGETHDR(m, M_TRYWAIT, MT_DATA); 631 if (m == NULL) { 632 error = ENOBUFS; 633 goto release; 634 } 635 mlen = MHLEN; 636 m->m_pkthdr.len = 0; 637 m->m_pkthdr.rcvif = (struct ifnet *)0; 638 } else { 639 MGET(m, M_TRYWAIT, MT_DATA); 640 if (m == NULL) { 641 error = ENOBUFS; 642 goto release; 643 } 644 mlen = MLEN; 645 } 646 if (resid >= MINCLSIZE) { 647#ifdef ZERO_COPY_SOCKETS 648 if (so_zero_copy_send && 649 resid>=PAGE_SIZE && 650 space>=PAGE_SIZE && 651 uio->uio_iov->iov_len>=PAGE_SIZE) { 652 so_zerocp_stats.size_ok++; 653 if (!((vm_offset_t) 654 uio->uio_iov->iov_base & PAGE_MASK)){ 655 so_zerocp_stats.align_ok++; 656 cow_send = socow_setup(m, uio); 657 } 658 } 659 if (!cow_send){ 660#endif /* ZERO_COPY_SOCKETS */ 661 MCLGET(m, M_TRYWAIT); 662 if ((m->m_flags & M_EXT) == 0) 663 goto nopages; 664 mlen = MCLBYTES; 665 len = min(min(mlen, resid), space); 666 } else { 667#ifdef ZERO_COPY_SOCKETS 668 len = PAGE_SIZE; 669 } 670 671 } else { 672#endif /* ZERO_COPY_SOCKETS */ 673nopages: 674 len = min(min(mlen, resid), space); 675 /* 676 * For datagram protocols, leave room 677 * for protocol headers in first mbuf. 678 */ 679 if (atomic && top == 0 && len < mlen) 680 MH_ALIGN(m, len); 681 } 682 space -= len; 683#ifdef ZERO_COPY_SOCKETS 684 if (cow_send) 685 error = 0; 686 else 687#endif /* ZERO_COPY_SOCKETS */ 688 error = uiomove(mtod(m, void *), (int)len, uio); 689 resid = uio->uio_resid; 690 m->m_len = len; 691 *mp = m; 692 top->m_pkthdr.len += len; 693 if (error) 694 goto release; 695 mp = &m->m_next; 696 if (resid <= 0) { 697 if (flags & MSG_EOR) 698 top->m_flags |= M_EOR; 699 break; 700 } 701 } while (space > 0 && atomic); 702 if (dontroute) 703 so->so_options |= SO_DONTROUTE; 704 s = splnet(); /* XXX */ 705 /* 706 * XXX all the SS_CANTSENDMORE checks previously 707 * done could be out of date. We could have recieved 708 * a reset packet in an interrupt or maybe we slept 709 * while doing page faults in uiomove() etc. We could 710 * probably recheck again inside the splnet() protection 711 * here, but there are probably other places that this 712 * also happens. We must rethink this. 713 */ 714 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 715 (flags & MSG_OOB) ? PRUS_OOB : 716 /* 717 * If the user set MSG_EOF, the protocol 718 * understands this flag and nothing left to 719 * send then use PRU_SEND_EOF instead of PRU_SEND. 720 */ 721 ((flags & MSG_EOF) && 722 (so->so_proto->pr_flags & PR_IMPLOPCL) && 723 (resid <= 0)) ? 724 PRUS_EOF : 725 /* If there is more to send set PRUS_MORETOCOME */ 726 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0, 727 top, addr, control, td); 728 splx(s); 729 if (dontroute) 730 so->so_options &= ~SO_DONTROUTE; 731 clen = 0; 732 control = 0; 733 top = 0; 734 mp = ⊤ 735 if (error) 736 goto release; 737 } while (resid && space > 0); 738 } while (resid); 739 740release: 741 sbunlock(&so->so_snd); 742out: 743 if (top) 744 m_freem(top); 745 if (control) 746 m_freem(control); 747 return (error); 748} 749 750/* 751 * Implement receive operations on a socket. 752 * We depend on the way that records are added to the sockbuf 753 * by sbappend*. In particular, each record (mbufs linked through m_next) 754 * must begin with an address if the protocol so specifies, 755 * followed by an optional mbuf or mbufs containing ancillary data, 756 * and then zero or more mbufs of data. 757 * In order to avoid blocking network interrupts for the entire time here, 758 * we splx() while doing the actual copy to user space. 759 * Although the sockbuf is locked, new data may still be appended, 760 * and thus we must maintain consistency of the sockbuf during that time. 761 * 762 * The caller may receive the data as a single mbuf chain by supplying 763 * an mbuf **mp0 for use in returning the chain. The uio is then used 764 * only for the count in uio_resid. 765 */ 766int 767soreceive(so, psa, uio, mp0, controlp, flagsp) 768 struct socket *so; 769 struct sockaddr **psa; 770 struct uio *uio; 771 struct mbuf **mp0; 772 struct mbuf **controlp; 773 int *flagsp; 774{ 775 struct mbuf *m, **mp; 776 int flags, len, error, s, offset; 777 struct protosw *pr = so->so_proto; 778 struct mbuf *nextrecord; 779 int moff, type = 0; 780 int orig_resid = uio->uio_resid; 781 782 mp = mp0; 783 if (psa) 784 *psa = 0; 785 if (controlp) 786 *controlp = 0; 787 if (flagsp) 788 flags = *flagsp &~ MSG_EOR; 789 else 790 flags = 0; 791 if (flags & MSG_OOB) { 792 m = m_get(M_TRYWAIT, MT_DATA); 793 if (m == NULL) 794 return (ENOBUFS); 795 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK); 796 if (error) 797 goto bad; 798 do { 799#ifdef ZERO_COPY_SOCKETS 800 if (so_zero_copy_receive) { 801 vm_page_t pg; 802 int disposable; 803 804 if ((m->m_flags & M_EXT) 805 && (m->m_ext.ext_type == EXT_DISPOSABLE)) 806 disposable = 1; 807 else 808 disposable = 0; 809 810 pg = PHYS_TO_VM_PAGE(vtophys(mtod(m, caddr_t))); 811 if (uio->uio_offset == -1) 812 uio->uio_offset =IDX_TO_OFF(pg->pindex); 813 814 error = uiomoveco(mtod(m, void *), 815 min(uio->uio_resid, m->m_len), 816 uio, pg->object, 817 disposable); 818 } else 819#endif /* ZERO_COPY_SOCKETS */ 820 error = uiomove(mtod(m, void *), 821 (int) min(uio->uio_resid, m->m_len), uio); 822 m = m_free(m); 823 } while (uio->uio_resid && error == 0 && m); 824bad: 825 if (m) 826 m_freem(m); 827 return (error); 828 } 829 if (mp) 830 *mp = (struct mbuf *)0; 831 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) 832 (*pr->pr_usrreqs->pru_rcvd)(so, 0); 833 834restart: 835 error = sblock(&so->so_rcv, SBLOCKWAIT(flags)); 836 if (error) 837 return (error); 838 s = splnet(); 839 840 m = so->so_rcv.sb_mb; 841 /* 842 * If we have less data than requested, block awaiting more 843 * (subject to any timeout) if: 844 * 1. the current count is less than the low water mark, or 845 * 2. MSG_WAITALL is set, and it is possible to do the entire 846 * receive operation at once if we block (resid <= hiwat). 847 * 3. MSG_DONTWAIT is not set 848 * If MSG_WAITALL is set but resid is larger than the receive buffer, 849 * we have to do the receive in sections, and thus risk returning 850 * a short count if a timeout or signal occurs after we start. 851 */ 852 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 && 853 so->so_rcv.sb_cc < uio->uio_resid) && 854 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || 855 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) && 856 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) { 857 KASSERT(m != 0 || !so->so_rcv.sb_cc, 858 ("receive: m == %p so->so_rcv.sb_cc == %u", 859 m, so->so_rcv.sb_cc)); 860 if (so->so_error) { 861 if (m) 862 goto dontblock; 863 error = so->so_error; 864 if ((flags & MSG_PEEK) == 0) 865 so->so_error = 0; 866 goto release; 867 } 868 if (so->so_state & SS_CANTRCVMORE) { 869 if (m) 870 goto dontblock; 871 else 872 goto release; 873 } 874 for (; m; m = m->m_next) 875 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { 876 m = so->so_rcv.sb_mb; 877 goto dontblock; 878 } 879 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 880 (so->so_proto->pr_flags & PR_CONNREQUIRED)) { 881 error = ENOTCONN; 882 goto release; 883 } 884 if (uio->uio_resid == 0) 885 goto release; 886 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) { 887 error = EWOULDBLOCK; 888 goto release; 889 } 890 sbunlock(&so->so_rcv); 891 error = sbwait(&so->so_rcv); 892 splx(s); 893 if (error) 894 return (error); 895 goto restart; 896 } 897dontblock: 898 if (uio->uio_td) 899 uio->uio_td->td_proc->p_stats->p_ru.ru_msgrcv++; 900 nextrecord = m->m_nextpkt; 901 if (pr->pr_flags & PR_ADDR) { 902 KASSERT(m->m_type == MT_SONAME, 903 ("m->m_type == %d", m->m_type)); 904 orig_resid = 0; 905 if (psa) 906 *psa = dup_sockaddr(mtod(m, struct sockaddr *), 907 mp0 == 0); 908 if (flags & MSG_PEEK) { 909 m = m->m_next; 910 } else { 911 sbfree(&so->so_rcv, m); 912 so->so_rcv.sb_mb = m_free(m); 913 m = so->so_rcv.sb_mb; 914 } 915 } 916 while (m && m->m_type == MT_CONTROL && error == 0) { 917 if (flags & MSG_PEEK) { 918 if (controlp) 919 *controlp = m_copy(m, 0, m->m_len); 920 m = m->m_next; 921 } else { 922 sbfree(&so->so_rcv, m); 923 so->so_rcv.sb_mb = m->m_next; 924 m->m_next = NULL; 925 if (pr->pr_domain->dom_externalize) 926 error = 927 (*pr->pr_domain->dom_externalize)(m, controlp); 928 else if (controlp) 929 *controlp = m; 930 else 931 m_freem(m); 932 m = so->so_rcv.sb_mb; 933 } 934 if (controlp) { 935 orig_resid = 0; 936 while (*controlp != NULL) 937 controlp = &(*controlp)->m_next; 938 } 939 } 940 if (m) { 941 if ((flags & MSG_PEEK) == 0) 942 m->m_nextpkt = nextrecord; 943 type = m->m_type; 944 if (type == MT_OOBDATA) 945 flags |= MSG_OOB; 946 } 947 moff = 0; 948 offset = 0; 949 while (m && uio->uio_resid > 0 && error == 0) { 950 if (m->m_type == MT_OOBDATA) { 951 if (type != MT_OOBDATA) 952 break; 953 } else if (type == MT_OOBDATA) 954 break; 955 else 956 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER, 957 ("m->m_type == %d", m->m_type)); 958 so->so_state &= ~SS_RCVATMARK; 959 len = uio->uio_resid; 960 if (so->so_oobmark && len > so->so_oobmark - offset) 961 len = so->so_oobmark - offset; 962 if (len > m->m_len - moff) 963 len = m->m_len - moff; 964 /* 965 * If mp is set, just pass back the mbufs. 966 * Otherwise copy them out via the uio, then free. 967 * Sockbuf must be consistent here (points to current mbuf, 968 * it points to next record) when we drop priority; 969 * we must note any additions to the sockbuf when we 970 * block interrupts again. 971 */ 972 if (mp == 0) { 973 splx(s); 974#ifdef ZERO_COPY_SOCKETS 975 if (so_zero_copy_receive) { 976 vm_page_t pg; 977 int disposable; 978 979 if ((m->m_flags & M_EXT) 980 && (m->m_ext.ext_type == EXT_DISPOSABLE)) 981 disposable = 1; 982 else 983 disposable = 0; 984 985 pg = PHYS_TO_VM_PAGE(vtophys(mtod(m, caddr_t) + 986 moff)); 987 988 if (uio->uio_offset == -1) 989 uio->uio_offset =IDX_TO_OFF(pg->pindex); 990 991 error = uiomoveco(mtod(m, char *) + moff, 992 (int)len, uio,pg->object, 993 disposable); 994 } else 995#endif /* ZERO_COPY_SOCKETS */ 996 error = uiomove(mtod(m, char *) + moff, (int)len, uio); 997 s = splnet(); 998 if (error) 999 goto release; 1000 } else 1001 uio->uio_resid -= len; 1002 if (len == m->m_len - moff) { 1003 if (m->m_flags & M_EOR) 1004 flags |= MSG_EOR; 1005 if (flags & MSG_PEEK) { 1006 m = m->m_next; 1007 moff = 0; 1008 } else { 1009 nextrecord = m->m_nextpkt; 1010 sbfree(&so->so_rcv, m); 1011 if (mp) { 1012 *mp = m; 1013 mp = &m->m_next; 1014 so->so_rcv.sb_mb = m = m->m_next; 1015 *mp = (struct mbuf *)0; 1016 } else { 1017 so->so_rcv.sb_mb = m_free(m); 1018 m = so->so_rcv.sb_mb; 1019 } 1020 if (m) 1021 m->m_nextpkt = nextrecord; 1022 } 1023 } else { 1024 if (flags & MSG_PEEK) 1025 moff += len; 1026 else { 1027 if (mp) 1028 *mp = m_copym(m, 0, len, M_TRYWAIT); 1029 m->m_data += len; 1030 m->m_len -= len; 1031 so->so_rcv.sb_cc -= len; 1032 } 1033 } 1034 if (so->so_oobmark) { 1035 if ((flags & MSG_PEEK) == 0) { 1036 so->so_oobmark -= len; 1037 if (so->so_oobmark == 0) { 1038 so->so_state |= SS_RCVATMARK; 1039 break; 1040 } 1041 } else { 1042 offset += len; 1043 if (offset == so->so_oobmark) 1044 break; 1045 } 1046 } 1047 if (flags & MSG_EOR) 1048 break; 1049 /* 1050 * If the MSG_WAITALL flag is set (for non-atomic socket), 1051 * we must not quit until "uio->uio_resid == 0" or an error 1052 * termination. If a signal/timeout occurs, return 1053 * with a short count but without error. 1054 * Keep sockbuf locked against other readers. 1055 */ 1056 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 && 1057 !sosendallatonce(so) && !nextrecord) { 1058 if (so->so_error || so->so_state & SS_CANTRCVMORE) 1059 break; 1060 /* 1061 * Notify the protocol that some data has been 1062 * drained before blocking. 1063 */ 1064 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) 1065 (*pr->pr_usrreqs->pru_rcvd)(so, flags); 1066 error = sbwait(&so->so_rcv); 1067 if (error) { 1068 sbunlock(&so->so_rcv); 1069 splx(s); 1070 return (0); 1071 } 1072 m = so->so_rcv.sb_mb; 1073 if (m) 1074 nextrecord = m->m_nextpkt; 1075 } 1076 } 1077 1078 if (m && pr->pr_flags & PR_ATOMIC) { 1079 flags |= MSG_TRUNC; 1080 if ((flags & MSG_PEEK) == 0) 1081 (void) sbdroprecord(&so->so_rcv); 1082 } 1083 if ((flags & MSG_PEEK) == 0) { 1084 if (m == 0) 1085 so->so_rcv.sb_mb = nextrecord; 1086 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) 1087 (*pr->pr_usrreqs->pru_rcvd)(so, flags); 1088 } 1089 if (orig_resid == uio->uio_resid && orig_resid && 1090 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { 1091 sbunlock(&so->so_rcv); 1092 splx(s); 1093 goto restart; 1094 } 1095 1096 if (flagsp) 1097 *flagsp |= flags; 1098release: 1099 sbunlock(&so->so_rcv); 1100 splx(s); 1101 return (error); 1102} 1103 1104int 1105soshutdown(so, how) 1106 struct socket *so; 1107 int how; 1108{ 1109 struct protosw *pr = so->so_proto; 1110 1111 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) 1112 return (EINVAL); 1113 1114 if (how != SHUT_WR) 1115 sorflush(so); 1116 if (how != SHUT_RD) 1117 return ((*pr->pr_usrreqs->pru_shutdown)(so)); 1118 return (0); 1119} 1120 1121void 1122sorflush(so) 1123 struct socket *so; 1124{ 1125 struct sockbuf *sb = &so->so_rcv; 1126 struct protosw *pr = so->so_proto; 1127 int s; 1128 struct sockbuf asb; 1129 1130 sb->sb_flags |= SB_NOINTR; 1131 (void) sblock(sb, M_WAITOK); 1132 s = splimp(); 1133 socantrcvmore(so); 1134 sbunlock(sb); 1135 asb = *sb; 1136 bzero(sb, sizeof (*sb)); 1137 splx(s); 1138 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) 1139 (*pr->pr_domain->dom_dispose)(asb.sb_mb); 1140 sbrelease(&asb, so); 1141} 1142 1143#ifdef INET 1144static int 1145do_setopt_accept_filter(so, sopt) 1146 struct socket *so; 1147 struct sockopt *sopt; 1148{ 1149 struct accept_filter_arg *afap = NULL; 1150 struct accept_filter *afp; 1151 struct so_accf *af = so->so_accf; 1152 int error = 0; 1153 1154 /* do not set/remove accept filters on non listen sockets */ 1155 if ((so->so_options & SO_ACCEPTCONN) == 0) { 1156 error = EINVAL; 1157 goto out; 1158 } 1159 1160 /* removing the filter */ 1161 if (sopt == NULL) { 1162 if (af != NULL) { 1163 if (af->so_accept_filter != NULL && 1164 af->so_accept_filter->accf_destroy != NULL) { 1165 af->so_accept_filter->accf_destroy(so); 1166 } 1167 if (af->so_accept_filter_str != NULL) { 1168 FREE(af->so_accept_filter_str, M_ACCF); 1169 } 1170 FREE(af, M_ACCF); 1171 so->so_accf = NULL; 1172 } 1173 so->so_options &= ~SO_ACCEPTFILTER; 1174 return (0); 1175 } 1176 /* adding a filter */ 1177 /* must remove previous filter first */ 1178 if (af != NULL) { 1179 error = EINVAL; 1180 goto out; 1181 } 1182 /* don't put large objects on the kernel stack */ 1183 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK); 1184 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap); 1185 afap->af_name[sizeof(afap->af_name)-1] = '\0'; 1186 afap->af_arg[sizeof(afap->af_arg)-1] = '\0'; 1187 if (error) 1188 goto out; 1189 afp = accept_filt_get(afap->af_name); 1190 if (afp == NULL) { 1191 error = ENOENT; 1192 goto out; 1193 } 1194 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK | M_ZERO); 1195 if (afp->accf_create != NULL) { 1196 if (afap->af_name[0] != '\0') { 1197 int len = strlen(afap->af_name) + 1; 1198 1199 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK); 1200 strcpy(af->so_accept_filter_str, afap->af_name); 1201 } 1202 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg); 1203 if (af->so_accept_filter_arg == NULL) { 1204 FREE(af->so_accept_filter_str, M_ACCF); 1205 FREE(af, M_ACCF); 1206 so->so_accf = NULL; 1207 error = EINVAL; 1208 goto out; 1209 } 1210 } 1211 af->so_accept_filter = afp; 1212 so->so_accf = af; 1213 so->so_options |= SO_ACCEPTFILTER; 1214out: 1215 if (afap != NULL) 1216 FREE(afap, M_TEMP); 1217 return (error); 1218} 1219#endif /* INET */ 1220 1221/* 1222 * Perhaps this routine, and sooptcopyout(), below, ought to come in 1223 * an additional variant to handle the case where the option value needs 1224 * to be some kind of integer, but not a specific size. 1225 * In addition to their use here, these functions are also called by the 1226 * protocol-level pr_ctloutput() routines. 1227 */ 1228int 1229sooptcopyin(sopt, buf, len, minlen) 1230 struct sockopt *sopt; 1231 void *buf; 1232 size_t len; 1233 size_t minlen; 1234{ 1235 size_t valsize; 1236 1237 /* 1238 * If the user gives us more than we wanted, we ignore it, 1239 * but if we don't get the minimum length the caller 1240 * wants, we return EINVAL. On success, sopt->sopt_valsize 1241 * is set to however much we actually retrieved. 1242 */ 1243 if ((valsize = sopt->sopt_valsize) < minlen) 1244 return EINVAL; 1245 if (valsize > len) 1246 sopt->sopt_valsize = valsize = len; 1247 1248 if (sopt->sopt_td != 0) 1249 return (copyin(sopt->sopt_val, buf, valsize)); 1250 1251 bcopy(sopt->sopt_val, buf, valsize); 1252 return 0; 1253} 1254 1255int 1256sosetopt(so, sopt) 1257 struct socket *so; 1258 struct sockopt *sopt; 1259{ 1260 int error, optval; 1261 struct linger l; 1262 struct timeval tv; 1263 u_long val; 1264#ifdef MAC 1265 struct mac extmac; 1266#endif 1267 1268 error = 0; 1269 if (sopt->sopt_level != SOL_SOCKET) { 1270 if (so->so_proto && so->so_proto->pr_ctloutput) 1271 return ((*so->so_proto->pr_ctloutput) 1272 (so, sopt)); 1273 error = ENOPROTOOPT; 1274 } else { 1275 switch (sopt->sopt_name) { 1276#ifdef INET 1277 case SO_ACCEPTFILTER: 1278 error = do_setopt_accept_filter(so, sopt); 1279 if (error) 1280 goto bad; 1281 break; 1282#endif 1283 case SO_LINGER: 1284 error = sooptcopyin(sopt, &l, sizeof l, sizeof l); 1285 if (error) 1286 goto bad; 1287 1288 so->so_linger = l.l_linger; 1289 if (l.l_onoff) 1290 so->so_options |= SO_LINGER; 1291 else 1292 so->so_options &= ~SO_LINGER; 1293 break; 1294 1295 case SO_DEBUG: 1296 case SO_KEEPALIVE: 1297 case SO_DONTROUTE: 1298 case SO_USELOOPBACK: 1299 case SO_BROADCAST: 1300 case SO_REUSEADDR: 1301 case SO_REUSEPORT: 1302 case SO_OOBINLINE: 1303 case SO_TIMESTAMP: 1304 case SO_NOSIGPIPE: 1305 error = sooptcopyin(sopt, &optval, sizeof optval, 1306 sizeof optval); 1307 if (error) 1308 goto bad; 1309 if (optval) 1310 so->so_options |= sopt->sopt_name; 1311 else 1312 so->so_options &= ~sopt->sopt_name; 1313 break; 1314 1315 case SO_SNDBUF: 1316 case SO_RCVBUF: 1317 case SO_SNDLOWAT: 1318 case SO_RCVLOWAT: 1319 error = sooptcopyin(sopt, &optval, sizeof optval, 1320 sizeof optval); 1321 if (error) 1322 goto bad; 1323 1324 /* 1325 * Values < 1 make no sense for any of these 1326 * options, so disallow them. 1327 */ 1328 if (optval < 1) { 1329 error = EINVAL; 1330 goto bad; 1331 } 1332 1333 switch (sopt->sopt_name) { 1334 case SO_SNDBUF: 1335 case SO_RCVBUF: 1336 if (sbreserve(sopt->sopt_name == SO_SNDBUF ? 1337 &so->so_snd : &so->so_rcv, (u_long)optval, 1338 so, curthread) == 0) { 1339 error = ENOBUFS; 1340 goto bad; 1341 } 1342 break; 1343 1344 /* 1345 * Make sure the low-water is never greater than 1346 * the high-water. 1347 */ 1348 case SO_SNDLOWAT: 1349 so->so_snd.sb_lowat = 1350 (optval > so->so_snd.sb_hiwat) ? 1351 so->so_snd.sb_hiwat : optval; 1352 break; 1353 case SO_RCVLOWAT: 1354 so->so_rcv.sb_lowat = 1355 (optval > so->so_rcv.sb_hiwat) ? 1356 so->so_rcv.sb_hiwat : optval; 1357 break; 1358 } 1359 break; 1360 1361 case SO_SNDTIMEO: 1362 case SO_RCVTIMEO: 1363 error = sooptcopyin(sopt, &tv, sizeof tv, 1364 sizeof tv); 1365 if (error) 1366 goto bad; 1367 1368 /* assert(hz > 0); */ 1369 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz || 1370 tv.tv_usec < 0 || tv.tv_usec >= 1000000) { 1371 error = EDOM; 1372 goto bad; 1373 } 1374 /* assert(tick > 0); */ 1375 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */ 1376 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick; 1377 if (val > SHRT_MAX) { 1378 error = EDOM; 1379 goto bad; 1380 } 1381 if (val == 0 && tv.tv_usec != 0) 1382 val = 1; 1383 1384 switch (sopt->sopt_name) { 1385 case SO_SNDTIMEO: 1386 so->so_snd.sb_timeo = val; 1387 break; 1388 case SO_RCVTIMEO: 1389 so->so_rcv.sb_timeo = val; 1390 break; 1391 } 1392 break; 1393 case SO_LABEL: 1394#ifdef MAC 1395 error = sooptcopyin(sopt, &extmac, sizeof extmac, 1396 sizeof extmac); 1397 if (error) 1398 goto bad; 1399 1400 error = mac_setsockopt_label_set( 1401 sopt->sopt_td->td_ucred, so, &extmac); 1402 1403#else 1404 error = EOPNOTSUPP; 1405#endif 1406 break; 1407 default: 1408 error = ENOPROTOOPT; 1409 break; 1410 } 1411 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) { 1412 (void) ((*so->so_proto->pr_ctloutput) 1413 (so, sopt)); 1414 } 1415 } 1416bad: 1417 return (error); 1418} 1419 1420/* Helper routine for getsockopt */ 1421int 1422sooptcopyout(sopt, buf, len) 1423 struct sockopt *sopt; 1424 void *buf; 1425 size_t len; 1426{ 1427 int error; 1428 size_t valsize; 1429 1430 error = 0; 1431 1432 /* 1433 * Documented get behavior is that we always return a value, 1434 * possibly truncated to fit in the user's buffer. 1435 * Traditional behavior is that we always tell the user 1436 * precisely how much we copied, rather than something useful 1437 * like the total amount we had available for her. 1438 * Note that this interface is not idempotent; the entire answer must 1439 * generated ahead of time. 1440 */ 1441 valsize = min(len, sopt->sopt_valsize); 1442 sopt->sopt_valsize = valsize; 1443 if (sopt->sopt_val != 0) { 1444 if (sopt->sopt_td != 0) 1445 error = copyout(buf, sopt->sopt_val, valsize); 1446 else 1447 bcopy(buf, sopt->sopt_val, valsize); 1448 } 1449 return error; 1450} 1451 1452int 1453sogetopt(so, sopt) 1454 struct socket *so; 1455 struct sockopt *sopt; 1456{ 1457 int error, optval; 1458 struct linger l; 1459 struct timeval tv; 1460#ifdef INET 1461 struct accept_filter_arg *afap; 1462#endif 1463#ifdef MAC 1464 struct mac extmac; 1465#endif 1466 1467 error = 0; 1468 if (sopt->sopt_level != SOL_SOCKET) { 1469 if (so->so_proto && so->so_proto->pr_ctloutput) { 1470 return ((*so->so_proto->pr_ctloutput) 1471 (so, sopt)); 1472 } else 1473 return (ENOPROTOOPT); 1474 } else { 1475 switch (sopt->sopt_name) { 1476#ifdef INET 1477 case SO_ACCEPTFILTER: 1478 if ((so->so_options & SO_ACCEPTCONN) == 0) 1479 return (EINVAL); 1480 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), 1481 M_TEMP, M_WAITOK | M_ZERO); 1482 if ((so->so_options & SO_ACCEPTFILTER) != 0) { 1483 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name); 1484 if (so->so_accf->so_accept_filter_str != NULL) 1485 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str); 1486 } 1487 error = sooptcopyout(sopt, afap, sizeof(*afap)); 1488 FREE(afap, M_TEMP); 1489 break; 1490#endif 1491 1492 case SO_LINGER: 1493 l.l_onoff = so->so_options & SO_LINGER; 1494 l.l_linger = so->so_linger; 1495 error = sooptcopyout(sopt, &l, sizeof l); 1496 break; 1497 1498 case SO_USELOOPBACK: 1499 case SO_DONTROUTE: 1500 case SO_DEBUG: 1501 case SO_KEEPALIVE: 1502 case SO_REUSEADDR: 1503 case SO_REUSEPORT: 1504 case SO_BROADCAST: 1505 case SO_OOBINLINE: 1506 case SO_TIMESTAMP: 1507 case SO_NOSIGPIPE: 1508 optval = so->so_options & sopt->sopt_name; 1509integer: 1510 error = sooptcopyout(sopt, &optval, sizeof optval); 1511 break; 1512 1513 case SO_TYPE: 1514 optval = so->so_type; 1515 goto integer; 1516 1517 case SO_ERROR: 1518 optval = so->so_error; 1519 so->so_error = 0; 1520 goto integer; 1521 1522 case SO_SNDBUF: 1523 optval = so->so_snd.sb_hiwat; 1524 goto integer; 1525 1526 case SO_RCVBUF: 1527 optval = so->so_rcv.sb_hiwat; 1528 goto integer; 1529 1530 case SO_SNDLOWAT: 1531 optval = so->so_snd.sb_lowat; 1532 goto integer; 1533 1534 case SO_RCVLOWAT: 1535 optval = so->so_rcv.sb_lowat; 1536 goto integer; 1537 1538 case SO_SNDTIMEO: 1539 case SO_RCVTIMEO: 1540 optval = (sopt->sopt_name == SO_SNDTIMEO ? 1541 so->so_snd.sb_timeo : so->so_rcv.sb_timeo); 1542 1543 tv.tv_sec = optval / hz; 1544 tv.tv_usec = (optval % hz) * tick; 1545 error = sooptcopyout(sopt, &tv, sizeof tv); 1546 break; 1547 case SO_LABEL: 1548#ifdef MAC 1549 error = mac_getsockopt_label_get( 1550 sopt->sopt_td->td_ucred, so, &extmac); 1551 if (error) 1552 return (error); 1553 error = sooptcopyout(sopt, &extmac, sizeof extmac); 1554#else 1555 error = EOPNOTSUPP; 1556#endif 1557 break; 1558 case SO_PEERLABEL: 1559#ifdef MAC 1560 error = mac_getsockopt_peerlabel_get( 1561 sopt->sopt_td->td_ucred, so, &extmac); 1562 if (error) 1563 return (error); 1564 error = sooptcopyout(sopt, &extmac, sizeof extmac); 1565#else 1566 error = EOPNOTSUPP; 1567#endif 1568 break; 1569 default: 1570 error = ENOPROTOOPT; 1571 break; 1572 } 1573 return (error); 1574 } 1575} 1576 1577/* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */ 1578int 1579soopt_getm(struct sockopt *sopt, struct mbuf **mp) 1580{ 1581 struct mbuf *m, *m_prev; 1582 int sopt_size = sopt->sopt_valsize; 1583 1584 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA); 1585 if (m == 0) 1586 return ENOBUFS; 1587 if (sopt_size > MLEN) { 1588 MCLGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT); 1589 if ((m->m_flags & M_EXT) == 0) { 1590 m_free(m); 1591 return ENOBUFS; 1592 } 1593 m->m_len = min(MCLBYTES, sopt_size); 1594 } else { 1595 m->m_len = min(MLEN, sopt_size); 1596 } 1597 sopt_size -= m->m_len; 1598 *mp = m; 1599 m_prev = m; 1600 1601 while (sopt_size) { 1602 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA); 1603 if (m == 0) { 1604 m_freem(*mp); 1605 return ENOBUFS; 1606 } 1607 if (sopt_size > MLEN) { 1608 MCLGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT); 1609 if ((m->m_flags & M_EXT) == 0) { 1610 m_freem(*mp); 1611 return ENOBUFS; 1612 } 1613 m->m_len = min(MCLBYTES, sopt_size); 1614 } else { 1615 m->m_len = min(MLEN, sopt_size); 1616 } 1617 sopt_size -= m->m_len; 1618 m_prev->m_next = m; 1619 m_prev = m; 1620 } 1621 return 0; 1622} 1623 1624/* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */ 1625int 1626soopt_mcopyin(struct sockopt *sopt, struct mbuf *m) 1627{ 1628 struct mbuf *m0 = m; 1629 1630 if (sopt->sopt_val == NULL) 1631 return 0; 1632 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 1633 if (sopt->sopt_td != NULL) { 1634 int error; 1635 1636 error = copyin(sopt->sopt_val, mtod(m, char *), 1637 m->m_len); 1638 if (error != 0) { 1639 m_freem(m0); 1640 return(error); 1641 } 1642 } else 1643 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len); 1644 sopt->sopt_valsize -= m->m_len; 1645 (caddr_t)sopt->sopt_val += m->m_len; 1646 m = m->m_next; 1647 } 1648 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */ 1649 panic("ip6_sooptmcopyin"); 1650 return 0; 1651} 1652 1653/* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */ 1654int 1655soopt_mcopyout(struct sockopt *sopt, struct mbuf *m) 1656{ 1657 struct mbuf *m0 = m; 1658 size_t valsize = 0; 1659 1660 if (sopt->sopt_val == NULL) 1661 return 0; 1662 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 1663 if (sopt->sopt_td != NULL) { 1664 int error; 1665 1666 error = copyout(mtod(m, char *), sopt->sopt_val, 1667 m->m_len); 1668 if (error != 0) { 1669 m_freem(m0); 1670 return(error); 1671 } 1672 } else 1673 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len); 1674 sopt->sopt_valsize -= m->m_len; 1675 (caddr_t)sopt->sopt_val += m->m_len; 1676 valsize += m->m_len; 1677 m = m->m_next; 1678 } 1679 if (m != NULL) { 1680 /* enough soopt buffer should be given from user-land */ 1681 m_freem(m0); 1682 return(EINVAL); 1683 } 1684 sopt->sopt_valsize = valsize; 1685 return 0; 1686} 1687 1688void 1689sohasoutofband(so) 1690 struct socket *so; 1691{ 1692 if (so->so_sigio != NULL) 1693 pgsigio(&so->so_sigio, SIGURG, 0); 1694 selwakeup(&so->so_rcv.sb_sel); 1695} 1696 1697int 1698sopoll(struct socket *so, int events, struct ucred *active_cred, 1699 struct thread *td) 1700{ 1701 int revents = 0; 1702 int s = splnet(); 1703 1704 if (events & (POLLIN | POLLRDNORM)) 1705 if (soreadable(so)) 1706 revents |= events & (POLLIN | POLLRDNORM); 1707 1708 if (events & POLLINIGNEOF) 1709 if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat || 1710 !TAILQ_EMPTY(&so->so_comp) || so->so_error) 1711 revents |= POLLINIGNEOF; 1712 1713 if (events & (POLLOUT | POLLWRNORM)) 1714 if (sowriteable(so)) 1715 revents |= events & (POLLOUT | POLLWRNORM); 1716 1717 if (events & (POLLPRI | POLLRDBAND)) 1718 if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) 1719 revents |= events & (POLLPRI | POLLRDBAND); 1720 1721 if (revents == 0) { 1722 if (events & 1723 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | 1724 POLLRDBAND)) { 1725 selrecord(td, &so->so_rcv.sb_sel); 1726 so->so_rcv.sb_flags |= SB_SEL; 1727 } 1728 1729 if (events & (POLLOUT | POLLWRNORM)) { 1730 selrecord(td, &so->so_snd.sb_sel); 1731 so->so_snd.sb_flags |= SB_SEL; 1732 } 1733 } 1734 1735 splx(s); 1736 return (revents); 1737} 1738 1739int 1740soo_kqfilter(struct file *fp, struct knote *kn) 1741{ 1742 struct socket *so = kn->kn_fp->f_data; 1743 struct sockbuf *sb; 1744 int s; 1745 1746 switch (kn->kn_filter) { 1747 case EVFILT_READ: 1748 if (so->so_options & SO_ACCEPTCONN) 1749 kn->kn_fop = &solisten_filtops; 1750 else 1751 kn->kn_fop = &soread_filtops; 1752 sb = &so->so_rcv; 1753 break; 1754 case EVFILT_WRITE: 1755 kn->kn_fop = &sowrite_filtops; 1756 sb = &so->so_snd; 1757 break; 1758 default: 1759 return (1); 1760 } 1761 1762 s = splnet(); 1763 SLIST_INSERT_HEAD(&sb->sb_sel.si_note, kn, kn_selnext); 1764 sb->sb_flags |= SB_KNOTE; 1765 splx(s); 1766 return (0); 1767} 1768 1769static void 1770filt_sordetach(struct knote *kn) 1771{ 1772 struct socket *so = kn->kn_fp->f_data; 1773 int s = splnet(); 1774 1775 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext); 1776 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note)) 1777 so->so_rcv.sb_flags &= ~SB_KNOTE; 1778 splx(s); 1779} 1780 1781/*ARGSUSED*/ 1782static int 1783filt_soread(struct knote *kn, long hint) 1784{ 1785 struct socket *so = kn->kn_fp->f_data; 1786 1787 kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl; 1788 if (so->so_state & SS_CANTRCVMORE) { 1789 kn->kn_flags |= EV_EOF; 1790 kn->kn_fflags = so->so_error; 1791 return (1); 1792 } 1793 if (so->so_error) /* temporary udp error */ 1794 return (1); 1795 if (kn->kn_sfflags & NOTE_LOWAT) 1796 return (kn->kn_data >= kn->kn_sdata); 1797 return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat); 1798} 1799 1800static void 1801filt_sowdetach(struct knote *kn) 1802{ 1803 struct socket *so = kn->kn_fp->f_data; 1804 int s = splnet(); 1805 1806 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext); 1807 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note)) 1808 so->so_snd.sb_flags &= ~SB_KNOTE; 1809 splx(s); 1810} 1811 1812/*ARGSUSED*/ 1813static int 1814filt_sowrite(struct knote *kn, long hint) 1815{ 1816 struct socket *so = kn->kn_fp->f_data; 1817 1818 kn->kn_data = sbspace(&so->so_snd); 1819 if (so->so_state & SS_CANTSENDMORE) { 1820 kn->kn_flags |= EV_EOF; 1821 kn->kn_fflags = so->so_error; 1822 return (1); 1823 } 1824 if (so->so_error) /* temporary udp error */ 1825 return (1); 1826 if (((so->so_state & SS_ISCONNECTED) == 0) && 1827 (so->so_proto->pr_flags & PR_CONNREQUIRED)) 1828 return (0); 1829 if (kn->kn_sfflags & NOTE_LOWAT) 1830 return (kn->kn_data >= kn->kn_sdata); 1831 return (kn->kn_data >= so->so_snd.sb_lowat); 1832} 1833 1834/*ARGSUSED*/ 1835static int 1836filt_solisten(struct knote *kn, long hint) 1837{ 1838 struct socket *so = kn->kn_fp->f_data; 1839 1840 kn->kn_data = so->so_qlen; 1841 return (! TAILQ_EMPTY(&so->so_comp)); 1842} 1843 1844int 1845socheckuid(struct socket *so, uid_t uid) 1846{ 1847 1848 if (so == NULL) 1849 return (EPERM); 1850 if (so->so_cred->cr_uid == uid) 1851 return (0); 1852 return (EPERM); 1853} 1854