123421Smckusick /* 223421Smckusick * Copyright (c) 1982 Regents of the University of California. 323421Smckusick * All rights reserved. The Berkeley software License Agreement 423421Smckusick * specifies the terms and conditions for redistribution. 523421Smckusick * 6*26500Skarels * @(#)uipc_socket.c 6.24 (Berkeley) 03/07/86 723421Smckusick */ 84786Swnj 917102Sbloom #include "param.h" 1017102Sbloom #include "systm.h" 1117102Sbloom #include "dir.h" 1217102Sbloom #include "user.h" 1317102Sbloom #include "proc.h" 1417102Sbloom #include "file.h" 1517102Sbloom #include "inode.h" 1617102Sbloom #include "buf.h" 1717102Sbloom #include "mbuf.h" 1817102Sbloom #include "un.h" 1917102Sbloom #include "domain.h" 2017102Sbloom #include "protosw.h" 2117102Sbloom #include "socket.h" 2217102Sbloom #include "socketvar.h" 2317102Sbloom #include "stat.h" 2417102Sbloom #include "ioctl.h" 2517102Sbloom #include "uio.h" 266355Ssam #include "../net/route.h" 2712757Ssam #include "../netinet/in.h" 2811571Ssam #include "../net/if.h" 294786Swnj 304786Swnj /* 318300Sroot * Socket operation routines. 328300Sroot * These routines are called by the routines in 338300Sroot * sys_socket.c or from a system process, and 348300Sroot * implement the semantics of socket operations by 358300Sroot * switching out to the protocol specific routines. 3612757Ssam * 3712757Ssam * TODO: 3812757Ssam * test socketpair 3921767Skarels * clean up async 4012757Ssam * out-of-band is a kludge 414786Swnj */ 428594Sroot /*ARGSUSED*/ 4310267Ssam socreate(dom, aso, type, proto) 444786Swnj struct socket **aso; 4512757Ssam register int type; 4612757Ssam int proto; 474786Swnj { 484786Swnj register struct protosw *prp; 494786Swnj register struct socket *so; 5012757Ssam register struct mbuf *m; 5112757Ssam register int error; 524786Swnj 534890Swnj if (proto) 5421767Skarels prp = pffindproto(dom, proto, type); 554890Swnj else 569168Ssam prp = pffindtype(dom, type); 574890Swnj if (prp == 0) 584890Swnj return (EPROTONOSUPPORT); 598300Sroot if (prp->pr_type != type) 608300Sroot return (EPROTOTYPE); 619635Ssam m = m_getclr(M_WAIT, MT_SOCKET); 624786Swnj so = mtod(m, struct socket *); 6312757Ssam so->so_options = 0; 646214Swnj so->so_state = 0; 659168Ssam so->so_type = type; 666214Swnj if (u.u_uid == 0) 676214Swnj so->so_state = SS_PRIV; 684786Swnj so->so_proto = prp; 6912757Ssam error = 7012757Ssam (*prp->pr_usrreq)(so, PRU_ATTACH, 7121767Skarels (struct mbuf *)0, (struct mbuf *)proto, (struct mbuf *)0); 724979Swnj if (error) { 737507Sroot so->so_state |= SS_NOFDREF; 747180Swnj sofree(so); 754890Swnj return (error); 764786Swnj } 774786Swnj *aso = so; 784786Swnj return (0); 794786Swnj } 804786Swnj 8110267Ssam sobind(so, nam) 828300Sroot struct socket *so; 838300Sroot struct mbuf *nam; 848300Sroot { 858300Sroot int s = splnet(); 868300Sroot int error; 878300Sroot 888300Sroot error = 8912757Ssam (*so->so_proto->pr_usrreq)(so, PRU_BIND, 9012757Ssam (struct mbuf *)0, nam, (struct mbuf *)0); 918300Sroot splx(s); 928300Sroot return (error); 938300Sroot } 948300Sroot 958300Sroot solisten(so, backlog) 9612757Ssam register struct socket *so; 978300Sroot int backlog; 988300Sroot { 9912757Ssam int s = splnet(), error; 1008300Sroot 10112757Ssam error = 10212757Ssam (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, 10312757Ssam (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0); 1048300Sroot if (error) { 1058300Sroot splx(s); 1068300Sroot return (error); 1078300Sroot } 1088300Sroot if (so->so_q == 0) { 1098300Sroot so->so_q = so; 1108300Sroot so->so_q0 = so; 1118300Sroot so->so_options |= SO_ACCEPTCONN; 1128300Sroot } 1138300Sroot if (backlog < 0) 1148300Sroot backlog = 0; 11510137Ssam so->so_qlimit = MIN(backlog, SOMAXCONN); 11612493Ssam splx(s); 1178300Sroot return (0); 1188300Sroot } 1198300Sroot 1204916Swnj sofree(so) 12112757Ssam register struct socket *so; 1224916Swnj { 1234916Swnj 1247507Sroot if (so->so_head) { 1257507Sroot if (!soqremque(so, 0) && !soqremque(so, 1)) 1267507Sroot panic("sofree dq"); 1277507Sroot so->so_head = 0; 1287507Sroot } 1297507Sroot if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) 1304950Swnj return; 1314950Swnj sbrelease(&so->so_snd); 13212757Ssam sorflush(so); 1334971Swnj (void) m_free(dtom(so)); 1344916Swnj } 1354916Swnj 1364786Swnj /* 1374890Swnj * Close a socket on last file table reference removal. 1384890Swnj * Initiate disconnect if connected. 1394890Swnj * Free socket when disconnect complete. 1404829Swnj */ 14112757Ssam soclose(so) 1424829Swnj register struct socket *so; 1434829Swnj { 1444890Swnj int s = splnet(); /* conservative */ 1458713Sroot int error; 1464829Swnj 1477507Sroot if (so->so_options & SO_ACCEPTCONN) { 1487507Sroot while (so->so_q0 != so) 14910399Ssam (void) soabort(so->so_q0); 1507507Sroot while (so->so_q != so) 15110399Ssam (void) soabort(so->so_q); 1527507Sroot } 1534890Swnj if (so->so_pcb == 0) 1544890Swnj goto discard; 1554890Swnj if (so->so_state & SS_ISCONNECTED) { 1564890Swnj if ((so->so_state & SS_ISDISCONNECTING) == 0) { 15726245Skarels error = sodisconnect(so); 15812757Ssam if (error) 15912757Ssam goto drop; 1604890Swnj } 16110267Ssam if (so->so_options & SO_LINGER) { 1625281Sroot if ((so->so_state & SS_ISDISCONNECTING) && 16312757Ssam (so->so_state & SS_NBIO)) 16412757Ssam goto drop; 1655281Sroot while (so->so_state & SS_ISCONNECTED) 1665281Sroot sleep((caddr_t)&so->so_timeo, PZERO+1); 1674890Swnj } 1684890Swnj } 1695580Sroot drop: 1706880Ssam if (so->so_pcb) { 17112757Ssam int error2 = 17212757Ssam (*so->so_proto->pr_usrreq)(so, PRU_DETACH, 17312757Ssam (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0); 17412757Ssam if (error == 0) 17512757Ssam error = error2; 1766880Ssam } 1774890Swnj discard: 17810399Ssam if (so->so_state & SS_NOFDREF) 17910399Ssam panic("soclose: NOFDREF"); 1807507Sroot so->so_state |= SS_NOFDREF; 1814950Swnj sofree(so); 1824890Swnj splx(s); 18312757Ssam return (error); 1844829Swnj } 1854829Swnj 18610399Ssam /* 18710399Ssam * Must be called at splnet... 18810399Ssam */ 18910399Ssam soabort(so) 19010399Ssam struct socket *so; 19110399Ssam { 19210399Ssam 19312757Ssam return ( 19412757Ssam (*so->so_proto->pr_usrreq)(so, PRU_ABORT, 19512757Ssam (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)); 19610399Ssam } 19710399Ssam 19810267Ssam soaccept(so, nam) 19912757Ssam register struct socket *so; 2008300Sroot struct mbuf *nam; 2014927Swnj { 2024927Swnj int s = splnet(); 2034927Swnj int error; 2044927Swnj 20510399Ssam if ((so->so_state & SS_NOFDREF) == 0) 20610399Ssam panic("soaccept: !NOFDREF"); 20710267Ssam so->so_state &= ~SS_NOFDREF; 2088300Sroot error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, 20912757Ssam (struct mbuf *)0, nam, (struct mbuf *)0); 2104927Swnj splx(s); 2114927Swnj return (error); 2124927Swnj } 2134927Swnj 21410267Ssam soconnect(so, nam) 21512757Ssam register struct socket *so; 2168300Sroot struct mbuf *nam; 2174786Swnj { 2184890Swnj int s = splnet(); 2194890Swnj int error; 2204786Swnj 22124768Skarels /* 22224768Skarels * If protocol is connection-based, can only connect once. 22324768Skarels * Otherwise, if connected, try to disconnect first. 22424768Skarels * This allows user to disconnect by connecting to, e.g., 22524768Skarels * a null address. 22624768Skarels */ 22724768Skarels if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && 22824768Skarels ((so->so_proto->pr_flags & PR_CONNREQUIRED) || 22924768Skarels (error = sodisconnect(so)))) 2304890Swnj error = EISCONN; 23124768Skarels else 23224768Skarels error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, 23324768Skarels (struct mbuf *)0, nam, (struct mbuf *)0); 2344890Swnj splx(s); 2354890Swnj return (error); 2364786Swnj } 2374786Swnj 23812757Ssam soconnect2(so1, so2) 23912757Ssam register struct socket *so1; 24012757Ssam struct socket *so2; 24112757Ssam { 24212757Ssam int s = splnet(); 24312757Ssam int error; 24412757Ssam 24513113Ssam error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, 24613113Ssam (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0); 24712757Ssam splx(s); 24812757Ssam return (error); 24912757Ssam } 25012757Ssam 25126245Skarels sodisconnect(so) 25212757Ssam register struct socket *so; 2534786Swnj { 2544890Swnj int s = splnet(); 2554890Swnj int error; 2564786Swnj 2574890Swnj if ((so->so_state & SS_ISCONNECTED) == 0) { 2584890Swnj error = ENOTCONN; 2594890Swnj goto bad; 2604890Swnj } 2614890Swnj if (so->so_state & SS_ISDISCONNECTING) { 2624890Swnj error = EALREADY; 2634890Swnj goto bad; 2644890Swnj } 2658300Sroot error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, 26626245Skarels (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0); 2674890Swnj bad: 2684890Swnj splx(s); 2694890Swnj return (error); 2704786Swnj } 2714786Swnj 2724786Swnj /* 2734890Swnj * Send on a socket. 2744890Swnj * If send must go all at once and message is larger than 2754890Swnj * send buffering, then hard error. 2764890Swnj * Lock against other senders. 2774890Swnj * If must go all at once and not enough room now, then 2784890Swnj * inform user that this would block and do nothing. 27916412Skarels * Otherwise, if nonblocking, send as much as possible. 2804786Swnj */ 28112757Ssam sosend(so, nam, uio, flags, rights) 2824786Swnj register struct socket *so; 2838300Sroot struct mbuf *nam; 28412757Ssam register struct uio *uio; 2858319Sroot int flags; 28612757Ssam struct mbuf *rights; 2874786Swnj { 2884890Swnj struct mbuf *top = 0; 28916412Skarels register struct mbuf *m, **mp; 29012757Ssam register int space; 29125629Skarels int len, rlen = 0, error = 0, s, dontroute, first = 1; 2924786Swnj 2937827Sroot if (sosendallatonce(so) && uio->uio_resid > so->so_snd.sb_hiwat) 2944890Swnj return (EMSGSIZE); 29512757Ssam dontroute = 29612757Ssam (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && 29712757Ssam (so->so_proto->pr_flags & PR_ATOMIC); 29816412Skarels u.u_ru.ru_msgsnd++; 29925629Skarels if (rights) 30025629Skarels rlen = rights->m_len; 30116412Skarels #define snderr(errno) { error = errno; splx(s); goto release; } 30216412Skarels 3036419Sroot restart: 3044890Swnj sblock(&so->so_snd); 30516412Skarels do { 30616412Skarels s = splnet(); 30721108Skarels if (so->so_state & SS_CANTSENDMORE) 30816412Skarels snderr(EPIPE); 30916412Skarels if (so->so_error) { 31016412Skarels error = so->so_error; 31116412Skarels so->so_error = 0; /* ??? */ 31216412Skarels splx(s); 31316412Skarels goto release; 31416412Skarels } 31516412Skarels if ((so->so_state & SS_ISCONNECTED) == 0) { 31616412Skarels if (so->so_proto->pr_flags & PR_CONNREQUIRED) 31716412Skarels snderr(ENOTCONN); 31816412Skarels if (nam == 0) 31916412Skarels snderr(EDESTADDRREQ); 32016412Skarels } 32116412Skarels if (flags & MSG_OOB) 32216412Skarels space = 1024; 32316412Skarels else { 32416412Skarels space = sbspace(&so->so_snd); 32525629Skarels if (space <= rlen || 32625629Skarels (sosendallatonce(so) && 32725629Skarels space < uio->uio_resid + rlen) || 32816992Skarels (uio->uio_resid >= CLBYTES && space < CLBYTES && 32916992Skarels so->so_snd.sb_cc >= CLBYTES && 33016992Skarels (so->so_state & SS_NBIO) == 0)) { 33116412Skarels if (so->so_state & SS_NBIO) { 33216412Skarels if (first) 33316412Skarels error = EWOULDBLOCK; 33416412Skarels splx(s); 33516412Skarels goto release; 33616412Skarels } 33716412Skarels sbunlock(&so->so_snd); 33816412Skarels sbwait(&so->so_snd); 33916412Skarels splx(s); 34016412Skarels goto restart; 34116412Skarels } 34216412Skarels } 34316412Skarels splx(s); 34416412Skarels mp = ⊤ 34525629Skarels space -= rlen; 34621108Skarels while (space > 0) { 34716412Skarels register struct iovec *iov = uio->uio_iov; 3484890Swnj 34916412Skarels MGET(m, M_WAIT, MT_DATA); 35021767Skarels if (iov->iov_len >= NBPG && space >= CLBYTES) { 35126228Skarels MCLGET(m); 35226228Skarels if (m->m_len != CLBYTES) 35316412Skarels goto nopages; 35424518Skarels len = MIN(CLBYTES, iov->iov_len); 35521767Skarels space -= CLBYTES; 35616412Skarels } else { 35716412Skarels nopages: 35825629Skarels len = MIN(MIN(MLEN, iov->iov_len), space); 35921767Skarels space -= len; 36016412Skarels } 36116412Skarels error = uiomove(mtod(m, caddr_t), len, UIO_WRITE, uio); 36216412Skarels m->m_len = len; 36316412Skarels *mp = m; 36416412Skarels if (error) 36516412Skarels goto release; 36616412Skarels mp = &m->m_next; 36721108Skarels if (uio->uio_resid <= 0) 36821108Skarels break; 36921108Skarels while (uio->uio_iov->iov_len == 0) { 37021108Skarels uio->uio_iov++; 37121108Skarels uio->uio_iovcnt--; 37221108Skarels if (uio->uio_iovcnt <= 0) 37321108Skarels panic("sosend"); 37421108Skarels } 37516412Skarels } 37621108Skarels if (dontroute) 37721108Skarels so->so_options |= SO_DONTROUTE; 37821108Skarels s = splnet(); /* XXX */ 37921108Skarels error = (*so->so_proto->pr_usrreq)(so, 38021108Skarels (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND, 38121108Skarels top, (caddr_t)nam, rights); 38221108Skarels splx(s); 38321108Skarels if (dontroute) 38421108Skarels so->so_options &= ~SO_DONTROUTE; 38521767Skarels rights = 0; 38625629Skarels rlen = 0; 3876419Sroot top = 0; 38816412Skarels first = 0; 38914781Ssam if (error) 39016412Skarels break; 39116412Skarels } while (uio->uio_resid); 3924890Swnj 3934786Swnj release: 3944890Swnj sbunlock(&so->so_snd); 3956419Sroot if (top) 3966419Sroot m_freem(top); 39721108Skarels if (error == EPIPE) 39821108Skarels psignal(u.u_procp, SIGPIPE); 3994786Swnj return (error); 4004786Swnj } 4014786Swnj 40225629Skarels /* 40325629Skarels * Implement receive operations on a socket. 40425629Skarels * We depend on the way that records are added to the sockbuf 40525629Skarels * by sbappend*. In particular, each record (mbufs linked through m_next) 40625629Skarels * must begin with an address if the protocol so specifies, 40725629Skarels * followed by an optional mbuf containing access rights if supported 40825629Skarels * by the protocol, and then zero or more mbufs of data. 40925629Skarels * In order to avoid blocking network interrupts for the entire time here, 41025629Skarels * we splx() while doing the actual copy to user space. 41125629Skarels * Although the sockbuf is locked, new data may still be appended, 41225629Skarels * and thus we must maintain consistency of the sockbuf during that time. 41325629Skarels */ 41412757Ssam soreceive(so, aname, uio, flags, rightsp) 4154786Swnj register struct socket *so; 4168300Sroot struct mbuf **aname; 41712757Ssam register struct uio *uio; 4188319Sroot int flags; 41912757Ssam struct mbuf **rightsp; 4204786Swnj { 4214786Swnj register struct mbuf *m, *n; 42216993Skarels register int len, error = 0, s, tomark; 42312757Ssam struct protosw *pr = so->so_proto; 42416993Skarels struct mbuf *nextrecord; 42512757Ssam int moff; 4264786Swnj 42712757Ssam if (rightsp) 42812757Ssam *rightsp = 0; 42912757Ssam if (aname) 43012757Ssam *aname = 0; 43112757Ssam if (flags & MSG_OOB) { 4329635Ssam m = m_get(M_WAIT, MT_DATA); 43312757Ssam error = (*pr->pr_usrreq)(so, PRU_RCVOOB, 43424768Skarels m, (struct mbuf *)(flags & MSG_PEEK), (struct mbuf *)0); 4358594Sroot if (error) 43610137Ssam goto bad; 4378319Sroot do { 43810137Ssam len = uio->uio_resid; 4398319Sroot if (len > m->m_len) 4408319Sroot len = m->m_len; 4418594Sroot error = 4428793Sroot uiomove(mtod(m, caddr_t), (int)len, UIO_READ, uio); 4438319Sroot m = m_free(m); 4448594Sroot } while (uio->uio_resid && error == 0 && m); 44510137Ssam bad: 4468319Sroot if (m) 4478771Sroot m_freem(m); 4488594Sroot return (error); 4498319Sroot } 4508319Sroot 4514890Swnj restart: 4524890Swnj sblock(&so->so_rcv); 4538835Sroot s = splnet(); 4544890Swnj 4554890Swnj #define rcverr(errno) { error = errno; splx(s); goto release; } 4564786Swnj if (so->so_rcv.sb_cc == 0) { 4575168Swnj if (so->so_error) { 4585168Swnj error = so->so_error; 4595168Swnj so->so_error = 0; 4605168Swnj splx(s); 4615168Swnj goto release; 4625168Swnj } 4634890Swnj if (so->so_state & SS_CANTRCVMORE) { 4644890Swnj splx(s); 4654890Swnj goto release; 4664890Swnj } 4675015Sroot if ((so->so_state & SS_ISCONNECTED) == 0 && 4685015Sroot (so->so_proto->pr_flags & PR_CONNREQUIRED)) 4695015Sroot rcverr(ENOTCONN); 47025629Skarels if (uio->uio_resid == 0) 47125629Skarels goto release; 4726214Swnj if (so->so_state & SS_NBIO) 4735168Swnj rcverr(EWOULDBLOCK); 4744890Swnj sbunlock(&so->so_rcv); 4754971Swnj sbwait(&so->so_rcv); 4765012Swnj splx(s); 4774890Swnj goto restart; 4784786Swnj } 4798041Sroot u.u_ru.ru_msgrcv++; 4804829Swnj m = so->so_rcv.sb_mb; 48125629Skarels if (m == 0) 48225629Skarels panic("receive 1"); 48325629Skarels nextrecord = m->m_act; 48412757Ssam if (pr->pr_flags & PR_ADDR) { 48525629Skarels if (m->m_type != MT_SONAME) 48616993Skarels panic("receive 1a"); 48716993Skarels if (flags & MSG_PEEK) { 48816993Skarels if (aname) 4898319Sroot *aname = m_copy(m, 0, m->m_len); 49025629Skarels m = m->m_next; 49116993Skarels } else { 49225629Skarels sbfree(&so->so_rcv, m); 49316993Skarels if (aname) { 49416993Skarels *aname = m; 49525629Skarels m = m->m_next; 49625629Skarels (*aname)->m_next = 0; 49725629Skarels } else { 498*26500Skarels nextrecord = m->m_act; 49925629Skarels MFREE(m, n); 50025629Skarels m = n; 50125629Skarels } 50216993Skarels } 50316993Skarels } 50416993Skarels if (m && m->m_type == MT_RIGHTS) { 50516993Skarels if ((pr->pr_flags & PR_RIGHTS) == 0) 50612757Ssam panic("receive 2a"); 50716993Skarels if (flags & MSG_PEEK) { 50816993Skarels if (rightsp) 50912757Ssam *rightsp = m_copy(m, 0, m->m_len); 51025629Skarels m = m->m_next; 51116993Skarels } else { 51225629Skarels sbfree(&so->so_rcv, m); 51316993Skarels if (rightsp) { 51416993Skarels *rightsp = m; 51525629Skarels n = m->m_next; 51625629Skarels m->m_next = 0; 51725629Skarels m = n; 51825629Skarels } else { 51925629Skarels MFREE(m, n); 52025629Skarels m = n; 52125629Skarels } 52212757Ssam } 5234890Swnj } 5248319Sroot moff = 0; 5258319Sroot tomark = so->so_oobmark; 52616993Skarels while (m && uio->uio_resid > 0 && error == 0) { 52725629Skarels if (m->m_type != MT_DATA && m->m_type != MT_HEADER) 52825629Skarels panic("receive 3"); 5297827Sroot len = uio->uio_resid; 5307747Sroot so->so_state &= ~SS_RCVATMARK; 5318319Sroot if (tomark && len > tomark) 5328319Sroot len = tomark; 53321767Skarels if (len > m->m_len - moff) 5348319Sroot len = m->m_len - moff; 53525787Skarels if ((flags & MSG_PEEK) == 0) { 53625787Skarels so->so_rcv.sb_mb = m; 53725787Skarels m->m_act = nextrecord; 53825787Skarels } 5394786Swnj splx(s); 5408594Sroot error = 5418793Sroot uiomove(mtod(m, caddr_t) + moff, (int)len, UIO_READ, uio); 5424786Swnj s = splnet(); 54321767Skarels if (len == m->m_len - moff) { 54425629Skarels if (flags & MSG_PEEK) { 54525629Skarels m = m->m_next; 54625629Skarels moff = 0; 54725629Skarels } else { 54825629Skarels sbfree(&so->so_rcv, m); 54916993Skarels nextrecord = m->m_act; 5508319Sroot MFREE(m, n); 55125629Skarels so->so_rcv.sb_mb = m = n; 55225629Skarels } 5534786Swnj } else { 55412757Ssam if (flags & MSG_PEEK) 5558319Sroot moff += len; 5568319Sroot else { 5578319Sroot m->m_off += len; 5588319Sroot m->m_len -= len; 5598319Sroot so->so_rcv.sb_cc -= len; 5608319Sroot } 5614786Swnj } 56212757Ssam if ((flags & MSG_PEEK) == 0 && so->so_oobmark) { 5637747Sroot so->so_oobmark -= len; 5647747Sroot if (so->so_oobmark == 0) { 5657747Sroot so->so_state |= SS_RCVATMARK; 5667747Sroot break; 5677747Sroot } 5687747Sroot } 5698319Sroot if (tomark) { 5708319Sroot tomark -= len; 5718319Sroot if (tomark == 0) 5728319Sroot break; 5738319Sroot } 57416993Skarels } 57516993Skarels if ((flags & MSG_PEEK) == 0) { 576*26500Skarels if (m == 0) 57716993Skarels so->so_rcv.sb_mb = nextrecord; 578*26500Skarels else { 579*26500Skarels m->m_act = nextrecord; 580*26500Skarels if (pr->pr_flags & PR_ATOMIC) 581*26500Skarels (void) sbdroprecord(&so->so_rcv); 582*26500Skarels } 58316993Skarels if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) 58416993Skarels (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, 58516993Skarels (struct mbuf *)0, (struct mbuf *)0); 58625629Skarels if (error == 0 && rightsp && *rightsp && 58725629Skarels pr->pr_domain->dom_externalize) 58825629Skarels error = (*pr->pr_domain->dom_externalize)(*rightsp); 58916993Skarels } 5904890Swnj release: 5914916Swnj sbunlock(&so->so_rcv); 5924890Swnj splx(s); 5934916Swnj return (error); 5944786Swnj } 5954786Swnj 59610267Ssam soshutdown(so, how) 59712757Ssam register struct socket *so; 59812757Ssam register int how; 59910267Ssam { 60012757Ssam register struct protosw *pr = so->so_proto; 60110267Ssam 60210267Ssam how++; 60312757Ssam if (how & FREAD) 60412757Ssam sorflush(so); 60510267Ssam if (how & FWRITE) 60612757Ssam return ((*pr->pr_usrreq)(so, PRU_SHUTDOWN, 60712757Ssam (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)); 60810267Ssam return (0); 60910267Ssam } 61010267Ssam 61112757Ssam sorflush(so) 61212757Ssam register struct socket *so; 61312757Ssam { 61412757Ssam register struct sockbuf *sb = &so->so_rcv; 61512757Ssam register struct protosw *pr = so->so_proto; 61612757Ssam register int s; 61712757Ssam struct sockbuf asb; 61812757Ssam 61912757Ssam sblock(sb); 62012757Ssam s = splimp(); 62112757Ssam socantrcvmore(so); 62212757Ssam sbunlock(sb); 62312757Ssam asb = *sb; 62412757Ssam bzero((caddr_t)sb, sizeof (*sb)); 62512757Ssam splx(s); 62616993Skarels if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) 62716993Skarels (*pr->pr_domain->dom_dispose)(asb.sb_mb); 62812757Ssam sbrelease(&asb); 62912757Ssam } 63012757Ssam 63118553Skarels sosetopt(so, level, optname, m0) 63212757Ssam register struct socket *so; 63310267Ssam int level, optname; 63418553Skarels struct mbuf *m0; 63510267Ssam { 63617158Ssam int error = 0; 63718553Skarels register struct mbuf *m = m0; 63810267Ssam 63917158Ssam if (level != SOL_SOCKET) { 64018369Skarels if (so->so_proto && so->so_proto->pr_ctloutput) 64118369Skarels return ((*so->so_proto->pr_ctloutput) 64218553Skarels (PRCO_SETOPT, so, level, optname, &m0)); 64318369Skarels error = ENOPROTOOPT; 64418369Skarels } else { 64518369Skarels switch (optname) { 64610267Ssam 64718369Skarels case SO_LINGER: 64818369Skarels if (m == NULL || m->m_len != sizeof (struct linger)) { 64918369Skarels error = EINVAL; 65018369Skarels goto bad; 65118369Skarels } 65218369Skarels so->so_linger = mtod(m, struct linger *)->l_linger; 65318369Skarels /* fall thru... */ 65417158Ssam 65518369Skarels case SO_DEBUG: 65618369Skarels case SO_KEEPALIVE: 65718369Skarels case SO_DONTROUTE: 65818369Skarels case SO_USELOOPBACK: 65918369Skarels case SO_BROADCAST: 66018369Skarels case SO_REUSEADDR: 66118369Skarels if (m == NULL || m->m_len < sizeof (int)) { 66218369Skarels error = EINVAL; 66318369Skarels goto bad; 66418369Skarels } 66518369Skarels if (*mtod(m, int *)) 66618369Skarels so->so_options |= optname; 66718369Skarels else 66818369Skarels so->so_options &= ~optname; 66918369Skarels break; 67018369Skarels 67118369Skarels case SO_SNDBUF: 67218369Skarels case SO_RCVBUF: 67318369Skarels case SO_SNDLOWAT: 67418369Skarels case SO_RCVLOWAT: 67518369Skarels case SO_SNDTIMEO: 67618369Skarels case SO_RCVTIMEO: 67718369Skarels if (m == NULL || m->m_len < sizeof (int)) { 67818369Skarels error = EINVAL; 67918369Skarels goto bad; 68018369Skarels } 68118369Skarels switch (optname) { 68218369Skarels 68318369Skarels case SO_SNDBUF: 68418369Skarels case SO_RCVBUF: 68518369Skarels if (sbreserve(optname == SO_SNDBUF ? &so->so_snd : 68618369Skarels &so->so_rcv, *mtod(m, int *)) == 0) { 68718369Skarels error = ENOBUFS; 68818369Skarels goto bad; 68918369Skarels } 69018369Skarels break; 69118369Skarels 69218369Skarels case SO_SNDLOWAT: 69318369Skarels so->so_snd.sb_lowat = *mtod(m, int *); 69418369Skarels break; 69518369Skarels case SO_RCVLOWAT: 69618369Skarels so->so_rcv.sb_lowat = *mtod(m, int *); 69718369Skarels break; 69818369Skarels case SO_SNDTIMEO: 69918369Skarels so->so_snd.sb_timeo = *mtod(m, int *); 70018369Skarels break; 70118369Skarels case SO_RCVTIMEO: 70218369Skarels so->so_rcv.sb_timeo = *mtod(m, int *); 70318369Skarels break; 70418369Skarels } 70518369Skarels break; 70618369Skarels 70718369Skarels default: 70818369Skarels error = ENOPROTOOPT; 70918369Skarels break; 71017158Ssam } 71110267Ssam } 71217158Ssam bad: 71317158Ssam if (m) 71417158Ssam (void) m_free(m); 71517158Ssam return (error); 71610267Ssam } 71710267Ssam 71817158Ssam sogetopt(so, level, optname, mp) 71912757Ssam register struct socket *so; 72010267Ssam int level, optname; 72117158Ssam struct mbuf **mp; 72217158Ssam { 72312757Ssam register struct mbuf *m; 72410267Ssam 72518369Skarels if (level != SOL_SOCKET) { 72618369Skarels if (so->so_proto && so->so_proto->pr_ctloutput) { 72718369Skarels return ((*so->so_proto->pr_ctloutput) 72818369Skarels (PRCO_GETOPT, so, level, optname, mp)); 72918369Skarels } else 73018369Skarels return (ENOPROTOOPT); 73118369Skarels } else { 73217158Ssam m = m_get(M_WAIT, MT_SOOPTS); 73325502Skarels m->m_len = sizeof (int); 73425502Skarels 73518369Skarels switch (optname) { 73617158Ssam 73718369Skarels case SO_LINGER: 73818369Skarels m->m_len = sizeof (struct linger); 73918369Skarels mtod(m, struct linger *)->l_onoff = 74018369Skarels so->so_options & SO_LINGER; 74118369Skarels mtod(m, struct linger *)->l_linger = so->so_linger; 74218369Skarels break; 74310267Ssam 74418369Skarels case SO_USELOOPBACK: 74518369Skarels case SO_DONTROUTE: 74618369Skarels case SO_DEBUG: 74718369Skarels case SO_KEEPALIVE: 74818369Skarels case SO_REUSEADDR: 74918369Skarels case SO_BROADCAST: 75018369Skarels *mtod(m, int *) = so->so_options & optname; 75118369Skarels break; 75218369Skarels 75325502Skarels case SO_TYPE: 75425502Skarels *mtod(m, int *) = so->so_type; 75525502Skarels break; 75625502Skarels 75724768Skarels case SO_ERROR: 75824768Skarels *mtod(m, int *) = so->so_error; 75924768Skarels so->so_error = 0; 76024768Skarels break; 76124768Skarels 76218369Skarels case SO_SNDBUF: 76318369Skarels *mtod(m, int *) = so->so_snd.sb_hiwat; 76418369Skarels break; 76518369Skarels 76618369Skarels case SO_RCVBUF: 76718369Skarels *mtod(m, int *) = so->so_rcv.sb_hiwat; 76818369Skarels break; 76918369Skarels 77018369Skarels case SO_SNDLOWAT: 77118369Skarels *mtod(m, int *) = so->so_snd.sb_lowat; 77218369Skarels break; 77318369Skarels 77418369Skarels case SO_RCVLOWAT: 77518369Skarels *mtod(m, int *) = so->so_rcv.sb_lowat; 77618369Skarels break; 77718369Skarels 77818369Skarels case SO_SNDTIMEO: 77918369Skarels *mtod(m, int *) = so->so_snd.sb_timeo; 78018369Skarels break; 78118369Skarels 78218369Skarels case SO_RCVTIMEO: 78318369Skarels *mtod(m, int *) = so->so_rcv.sb_timeo; 78418369Skarels break; 78518369Skarels 78618369Skarels default: 78726362Skarels (void)m_free(m); 78818369Skarels return (ENOPROTOOPT); 78918369Skarels } 79018369Skarels *mp = m; 79118369Skarels return (0); 79210267Ssam } 79310267Ssam } 79410267Ssam 7955423Swnj sohasoutofband(so) 79612757Ssam register struct socket *so; 7975423Swnj { 79823233Skarels struct proc *p; 7995423Swnj 80023233Skarels if (so->so_pgrp < 0) 80123233Skarels gsignal(-so->so_pgrp, SIGURG); 80223233Skarels else if (so->so_pgrp > 0 && (p = pfind(so->so_pgrp)) != 0) 80323233Skarels psignal(p, SIGURG); 80424768Skarels if (so->so_rcv.sb_sel) { 80524768Skarels selwakeup(so->so_rcv.sb_sel, so->so_rcv.sb_flags & SB_COLL); 80624768Skarels so->so_rcv.sb_sel = 0; 80724768Skarels so->so_rcv.sb_flags &= ~SB_COLL; 80824768Skarels } 8095423Swnj } 810