1*14781Ssam /* uipc_socket.c 4.77 83/08/20 */ 24786Swnj 34786Swnj #include "../h/param.h" 44829Swnj #include "../h/systm.h" 54786Swnj #include "../h/dir.h" 64786Swnj #include "../h/user.h" 74829Swnj #include "../h/proc.h" 84829Swnj #include "../h/file.h" 94786Swnj #include "../h/inode.h" 104829Swnj #include "../h/buf.h" 114786Swnj #include "../h/mbuf.h" 1212757Ssam #include "../h/un.h" 134829Swnj #include "../h/protosw.h" 144829Swnj #include "../h/socket.h" 154829Swnj #include "../h/socketvar.h" 164916Swnj #include "../h/stat.h" 175281Sroot #include "../h/ioctl.h" 188391Swnj #include "../h/uio.h" 196355Ssam #include "../net/route.h" 2012757Ssam #include "../netinet/in.h" 2111571Ssam #include "../net/if.h" 224786Swnj 234786Swnj /* 248300Sroot * Socket operation routines. 258300Sroot * These routines are called by the routines in 268300Sroot * sys_socket.c or from a system process, and 278300Sroot * implement the semantics of socket operations by 288300Sroot * switching out to the protocol specific routines. 2912757Ssam * 3012757Ssam * TODO: 3112757Ssam * sostat 3212757Ssam * test socketpair 3312757Ssam * PR_RIGHTS 3412757Ssam * clean up select, async 3512757Ssam * out-of-band is a kludge 364786Swnj */ 378594Sroot /*ARGSUSED*/ 3810267Ssam socreate(dom, aso, type, proto) 394786Swnj struct socket **aso; 4012757Ssam register int type; 4112757Ssam int proto; 424786Swnj { 434786Swnj register struct protosw *prp; 444786Swnj register struct socket *so; 4512757Ssam register struct mbuf *m; 4612757Ssam register int error; 474786Swnj 484890Swnj if (proto) 499168Ssam prp = pffindproto(dom, proto); 504890Swnj else 519168Ssam prp = pffindtype(dom, type); 524890Swnj if (prp == 0) 534890Swnj return (EPROTONOSUPPORT); 548300Sroot if (prp->pr_type != type) 558300Sroot return (EPROTOTYPE); 569635Ssam m = m_getclr(M_WAIT, MT_SOCKET); 574786Swnj if (m == 0) 584786Swnj return (ENOBUFS); 594786Swnj so = mtod(m, struct socket *); 6012757Ssam so->so_options = 0; 616214Swnj so->so_state = 0; 629168Ssam so->so_type = type; 636214Swnj if (u.u_uid == 0) 646214Swnj so->so_state = SS_PRIV; 654786Swnj so->so_proto = prp; 6612757Ssam error = 6712757Ssam (*prp->pr_usrreq)(so, PRU_ATTACH, 6812757Ssam (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0); 694979Swnj if (error) { 707507Sroot so->so_state |= SS_NOFDREF; 717180Swnj sofree(so); 724890Swnj return (error); 734786Swnj } 744786Swnj *aso = so; 754786Swnj return (0); 764786Swnj } 774786Swnj 7810267Ssam sobind(so, nam) 798300Sroot struct socket *so; 808300Sroot struct mbuf *nam; 818300Sroot { 828300Sroot int s = splnet(); 838300Sroot int error; 848300Sroot 858300Sroot error = 8612757Ssam (*so->so_proto->pr_usrreq)(so, PRU_BIND, 8712757Ssam (struct mbuf *)0, nam, (struct mbuf *)0); 888300Sroot splx(s); 898300Sroot return (error); 908300Sroot } 918300Sroot 928300Sroot solisten(so, backlog) 9312757Ssam register struct socket *so; 948300Sroot int backlog; 958300Sroot { 9612757Ssam int s = splnet(), error; 978300Sroot 9812757Ssam error = 9912757Ssam (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, 10012757Ssam (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0); 1018300Sroot if (error) { 1028300Sroot splx(s); 1038300Sroot return (error); 1048300Sroot } 1058300Sroot if (so->so_q == 0) { 1068300Sroot so->so_q = so; 1078300Sroot so->so_q0 = so; 1088300Sroot so->so_options |= SO_ACCEPTCONN; 1098300Sroot } 1108300Sroot if (backlog < 0) 1118300Sroot backlog = 0; 11210137Ssam so->so_qlimit = MIN(backlog, SOMAXCONN); 11312493Ssam splx(s); 1148300Sroot return (0); 1158300Sroot } 1168300Sroot 1174916Swnj sofree(so) 11812757Ssam register struct socket *so; 1194916Swnj { 1204916Swnj 1217507Sroot if (so->so_head) { 1227507Sroot if (!soqremque(so, 0) && !soqremque(so, 1)) 1237507Sroot panic("sofree dq"); 1247507Sroot so->so_head = 0; 1257507Sroot } 1267507Sroot if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) 1274950Swnj return; 1284950Swnj sbrelease(&so->so_snd); 12912757Ssam sorflush(so); 1304971Swnj (void) m_free(dtom(so)); 1314916Swnj } 1324916Swnj 1334786Swnj /* 1344890Swnj * Close a socket on last file table reference removal. 1354890Swnj * Initiate disconnect if connected. 1364890Swnj * Free socket when disconnect complete. 1374829Swnj */ 13812757Ssam soclose(so) 1394829Swnj register struct socket *so; 1404829Swnj { 1414890Swnj int s = splnet(); /* conservative */ 1428713Sroot int error; 1434829Swnj 1447507Sroot if (so->so_options & SO_ACCEPTCONN) { 1457507Sroot while (so->so_q0 != so) 14610399Ssam (void) soabort(so->so_q0); 1477507Sroot while (so->so_q != so) 14810399Ssam (void) soabort(so->so_q); 1497507Sroot } 1504890Swnj if (so->so_pcb == 0) 1514890Swnj goto discard; 1524890Swnj if (so->so_state & SS_ISCONNECTED) { 1534890Swnj if ((so->so_state & SS_ISDISCONNECTING) == 0) { 1548725Sroot error = sodisconnect(so, (struct mbuf *)0); 15512757Ssam if (error) 15612757Ssam goto drop; 1574890Swnj } 15810267Ssam if (so->so_options & SO_LINGER) { 1595281Sroot if ((so->so_state & SS_ISDISCONNECTING) && 16012757Ssam (so->so_state & SS_NBIO)) 16112757Ssam goto drop; 1625281Sroot while (so->so_state & SS_ISCONNECTED) 1635281Sroot sleep((caddr_t)&so->so_timeo, PZERO+1); 1644890Swnj } 1654890Swnj } 1665580Sroot drop: 1676880Ssam if (so->so_pcb) { 16812757Ssam int error2 = 16912757Ssam (*so->so_proto->pr_usrreq)(so, PRU_DETACH, 17012757Ssam (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0); 17112757Ssam if (error == 0) 17212757Ssam error = error2; 1736880Ssam } 1744890Swnj discard: 17510399Ssam if (so->so_state & SS_NOFDREF) 17610399Ssam panic("soclose: NOFDREF"); 1777507Sroot so->so_state |= SS_NOFDREF; 1784950Swnj sofree(so); 1794890Swnj splx(s); 18012757Ssam return (error); 1814829Swnj } 1824829Swnj 18310399Ssam /* 18410399Ssam * Must be called at splnet... 18510399Ssam */ 18610399Ssam soabort(so) 18710399Ssam struct socket *so; 18810399Ssam { 18910399Ssam 19012757Ssam return ( 19112757Ssam (*so->so_proto->pr_usrreq)(so, PRU_ABORT, 19212757Ssam (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)); 19310399Ssam } 19410399Ssam 19510267Ssam soaccept(so, nam) 19612757Ssam register struct socket *so; 1978300Sroot struct mbuf *nam; 1984927Swnj { 1994927Swnj int s = splnet(); 2004927Swnj int error; 2014927Swnj 20210399Ssam if ((so->so_state & SS_NOFDREF) == 0) 20310399Ssam panic("soaccept: !NOFDREF"); 20410267Ssam so->so_state &= ~SS_NOFDREF; 2058300Sroot error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, 20612757Ssam (struct mbuf *)0, nam, (struct mbuf *)0); 2074927Swnj splx(s); 2084927Swnj return (error); 2094927Swnj } 2104927Swnj 21110267Ssam soconnect(so, nam) 21212757Ssam register struct socket *so; 2138300Sroot struct mbuf *nam; 2144786Swnj { 2154890Swnj int s = splnet(); 2164890Swnj int error; 2174786Swnj 2184890Swnj if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) { 2194890Swnj error = EISCONN; 2204890Swnj goto bad; 2214890Swnj } 2228300Sroot error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, 22312757Ssam (struct mbuf *)0, nam, (struct mbuf *)0); 2244890Swnj bad: 2254890Swnj splx(s); 2264890Swnj return (error); 2274786Swnj } 2284786Swnj 22912757Ssam soconnect2(so1, so2) 23012757Ssam register struct socket *so1; 23112757Ssam struct socket *so2; 23212757Ssam { 23312757Ssam int s = splnet(); 23412757Ssam int error; 23512757Ssam 23613113Ssam error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, 23713113Ssam (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0); 23812757Ssam splx(s); 23912757Ssam return (error); 24012757Ssam } 24112757Ssam 2428300Sroot sodisconnect(so, nam) 24312757Ssam register struct socket *so; 2448300Sroot struct mbuf *nam; 2454786Swnj { 2464890Swnj int s = splnet(); 2474890Swnj int error; 2484786Swnj 2494890Swnj if ((so->so_state & SS_ISCONNECTED) == 0) { 2504890Swnj error = ENOTCONN; 2514890Swnj goto bad; 2524890Swnj } 2534890Swnj if (so->so_state & SS_ISDISCONNECTING) { 2544890Swnj error = EALREADY; 2554890Swnj goto bad; 2564890Swnj } 2578300Sroot error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, 25812757Ssam (struct mbuf *)0, nam, (struct mbuf *)0); 2594890Swnj bad: 2604890Swnj splx(s); 2614890Swnj return (error); 2624786Swnj } 2634786Swnj 2644786Swnj /* 2654890Swnj * Send on a socket. 2664890Swnj * If send must go all at once and message is larger than 2674890Swnj * send buffering, then hard error. 2684890Swnj * Lock against other senders. 2694890Swnj * If must go all at once and not enough room now, then 2704890Swnj * inform user that this would block and do nothing. 2714786Swnj */ 27212757Ssam sosend(so, nam, uio, flags, rights) 2734786Swnj register struct socket *so; 2748300Sroot struct mbuf *nam; 27512757Ssam register struct uio *uio; 2768319Sroot int flags; 27712757Ssam struct mbuf *rights; 2784786Swnj { 2794890Swnj struct mbuf *top = 0; 2804890Swnj register struct mbuf *m, **mp = ⊤ 28112757Ssam register int space; 28212757Ssam int len, error = 0, s, dontroute; 2834786Swnj 2847827Sroot if (sosendallatonce(so) && uio->uio_resid > so->so_snd.sb_hiwat) 2854890Swnj return (EMSGSIZE); 28612757Ssam dontroute = 28712757Ssam (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && 28812757Ssam (so->so_proto->pr_flags & PR_ATOMIC); 2896419Sroot restart: 2904890Swnj sblock(&so->so_snd); 2914890Swnj #define snderr(errno) { error = errno; splx(s); goto release; } 2924890Swnj 2938041Sroot u.u_ru.ru_msgsnd++; 2946419Sroot again: 2954890Swnj s = splnet(); 2966419Sroot if (so->so_state & SS_CANTSENDMORE) { 2976419Sroot psignal(u.u_procp, SIGPIPE); 2986419Sroot snderr(EPIPE); 2996419Sroot } 3005168Swnj if (so->so_error) { 3015168Swnj error = so->so_error; 3026419Sroot so->so_error = 0; /* ??? */ 3035168Swnj splx(s); 3045168Swnj goto release; 3055168Swnj } 3064890Swnj if ((so->so_state & SS_ISCONNECTED) == 0) { 3074890Swnj if (so->so_proto->pr_flags & PR_CONNREQUIRED) 3084890Swnj snderr(ENOTCONN); 3098300Sroot if (nam == 0) 3104890Swnj snderr(EDESTADDRREQ); 3114890Swnj } 3124890Swnj if (top) { 31310267Ssam if (dontroute) 31410267Ssam so->so_options |= SO_DONTROUTE; 3158319Sroot error = (*so->so_proto->pr_usrreq)(so, 31612757Ssam (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND, 31712757Ssam top, (caddr_t)nam, rights); 31810267Ssam if (dontroute) 31910267Ssam so->so_options &= ~SO_DONTROUTE; 3206419Sroot top = 0; 3214890Swnj if (error) { 3224890Swnj splx(s); 3234786Swnj goto release; 3244786Swnj } 3254890Swnj mp = ⊤ 3264786Swnj } 3277827Sroot if (uio->uio_resid == 0) { 3284979Swnj splx(s); 3294979Swnj goto release; 3304979Swnj } 33112757Ssam if (flags & MSG_OOB) 3328319Sroot space = 1024; 3338319Sroot else { 3348319Sroot space = sbspace(&so->so_snd); 3358319Sroot if (space <= 0 || 3368319Sroot sosendallatonce(so) && space < uio->uio_resid) { 3378319Sroot if (so->so_state & SS_NBIO) 3388319Sroot snderr(EWOULDBLOCK); 3398319Sroot sbunlock(&so->so_snd); 3408319Sroot sbwait(&so->so_snd); 3418319Sroot splx(s); 3428319Sroot goto restart; 3438319Sroot } 3444786Swnj } 3454890Swnj splx(s); 3467827Sroot while (uio->uio_resid > 0 && space > 0) { 3477827Sroot register struct iovec *iov = uio->uio_iov; 3487827Sroot 3497827Sroot if (iov->iov_len == 0) { 3507827Sroot uio->uio_iov++; 3517827Sroot uio->uio_iovcnt--; 3527827Sroot if (uio->uio_iovcnt < 0) 3537827Sroot panic("sosend"); 3547827Sroot continue; 3557827Sroot } 3569635Ssam MGET(m, M_WAIT, MT_DATA); 3574890Swnj if (m == NULL) { 3586419Sroot error = ENOBUFS; /* SIGPIPE? */ 3594890Swnj goto release; 3604786Swnj } 3617827Sroot if (iov->iov_len >= CLBYTES && space >= CLBYTES) { 3624890Swnj register struct mbuf *p; 3635095Swnj MCLGET(p, 1); 3644890Swnj if (p == 0) 3654890Swnj goto nopages; 3664890Swnj m->m_off = (int)p - (int)m; 3675095Swnj len = CLBYTES; 3684890Swnj } else { 3694786Swnj nopages: 3707827Sroot len = MIN(MLEN, iov->iov_len); 3714786Swnj } 372*14781Ssam error = uiomove(mtod(m, caddr_t), len, UIO_WRITE, uio); 3734890Swnj m->m_len = len; 3744890Swnj *mp = m; 375*14781Ssam if (error) 376*14781Ssam goto release; 3774890Swnj mp = &m->m_next; 37812757Ssam if (flags & MSG_OOB) 3798319Sroot space -= len; 3808319Sroot else 3818319Sroot space = sbspace(&so->so_snd); 3824786Swnj } 3834890Swnj goto again; 3844890Swnj 3854786Swnj release: 3864890Swnj sbunlock(&so->so_snd); 3876419Sroot if (top) 3886419Sroot m_freem(top); 3894786Swnj return (error); 3904786Swnj } 3914786Swnj 39212757Ssam soreceive(so, aname, uio, flags, rightsp) 3934786Swnj register struct socket *so; 3948300Sroot struct mbuf **aname; 39512757Ssam register struct uio *uio; 3968319Sroot int flags; 39712757Ssam struct mbuf **rightsp; 3984786Swnj { 3994786Swnj register struct mbuf *m, *n; 40012757Ssam register int len, error = 0, s, eor, tomark; 40112757Ssam struct protosw *pr = so->so_proto; 40212757Ssam int moff; 4034786Swnj 40412757Ssam if (rightsp) 40512757Ssam *rightsp = 0; 40612757Ssam if (aname) 40712757Ssam *aname = 0; 40812757Ssam if (flags & MSG_OOB) { 4099635Ssam m = m_get(M_WAIT, MT_DATA); 41012757Ssam if (m == 0) 41110137Ssam return (ENOBUFS); 41212757Ssam error = (*pr->pr_usrreq)(so, PRU_RCVOOB, 41312757Ssam m, (struct mbuf *)0, (struct mbuf *)0); 4148594Sroot if (error) 41510137Ssam goto bad; 4168319Sroot do { 41710137Ssam len = uio->uio_resid; 4188319Sroot if (len > m->m_len) 4198319Sroot len = m->m_len; 4208594Sroot error = 4218793Sroot uiomove(mtod(m, caddr_t), (int)len, UIO_READ, uio); 4228319Sroot m = m_free(m); 4238594Sroot } while (uio->uio_resid && error == 0 && m); 42410137Ssam bad: 4258319Sroot if (m) 4268771Sroot m_freem(m); 4278594Sroot return (error); 4288319Sroot } 4298319Sroot 4304890Swnj restart: 4314890Swnj sblock(&so->so_rcv); 4328835Sroot s = splnet(); 4334890Swnj 4344890Swnj #define rcverr(errno) { error = errno; splx(s); goto release; } 4354786Swnj if (so->so_rcv.sb_cc == 0) { 4365168Swnj if (so->so_error) { 4375168Swnj error = so->so_error; 4385168Swnj so->so_error = 0; 4395168Swnj splx(s); 4405168Swnj goto release; 4415168Swnj } 4424890Swnj if (so->so_state & SS_CANTRCVMORE) { 4434890Swnj splx(s); 4444890Swnj goto release; 4454890Swnj } 4465015Sroot if ((so->so_state & SS_ISCONNECTED) == 0 && 4475015Sroot (so->so_proto->pr_flags & PR_CONNREQUIRED)) 4485015Sroot rcverr(ENOTCONN); 4496214Swnj if (so->so_state & SS_NBIO) 4505168Swnj rcverr(EWOULDBLOCK); 4514890Swnj sbunlock(&so->so_rcv); 4524971Swnj sbwait(&so->so_rcv); 4535012Swnj splx(s); 4544890Swnj goto restart; 4554786Swnj } 4568041Sroot u.u_ru.ru_msgrcv++; 4574829Swnj m = so->so_rcv.sb_mb; 4584786Swnj if (m == 0) 4594786Swnj panic("receive"); 46012757Ssam if (pr->pr_flags & PR_ADDR) { 46112757Ssam if ((flags & MSG_PEEK) == 0) { 4628319Sroot so->so_rcv.sb_cc -= m->m_len; 4638319Sroot so->so_rcv.sb_mbcnt -= MSIZE; 4648319Sroot } 4658300Sroot if (aname) { 46612757Ssam if (flags & MSG_PEEK) { 4678319Sroot *aname = m_copy(m, 0, m->m_len); 46810137Ssam if (*aname == NULL) 46910137Ssam panic("receive 2"); 47010137Ssam } else 4718319Sroot *aname = m; 4728300Sroot m = m->m_next; 4738300Sroot (*aname)->m_next = 0; 4748300Sroot } else 47512757Ssam if (flags & MSG_PEEK) 4768319Sroot m = m->m_next; 4778319Sroot else 4788319Sroot m = m_free(m); 4794890Swnj if (m == 0) 48012757Ssam panic("receive 2a"); 48112757Ssam if (rightsp) { 48212757Ssam if (m->m_len) 48312757Ssam *rightsp = m_copy(m, 0, m->m_len); 48412757Ssam else { 48512757Ssam *rightsp = m_get(M_DONTWAIT, MT_SONAME); 48612757Ssam if (*rightsp) 48712757Ssam (*rightsp)->m_len = 0; 48812757Ssam } 48912757Ssam if (*rightsp == NULL) 49012757Ssam panic("receive 2b"); 49112757Ssam } 49212757Ssam if (flags & MSG_PEEK) 49312757Ssam m = m->m_next; 49412757Ssam else { 49512757Ssam so->so_rcv.sb_cc -= m->m_len; 49612757Ssam so->so_rcv.sb_mbcnt -= MSIZE; 49712757Ssam m = m_free(m); 49812757Ssam } 49912757Ssam if (m == 0) 50010137Ssam panic("receive 3"); 50112757Ssam if ((flags & MSG_PEEK) == 0) 5028548Sroot so->so_rcv.sb_mb = m; 5034890Swnj } 5044786Swnj eor = 0; 5058319Sroot moff = 0; 5068319Sroot tomark = so->so_oobmark; 5074786Swnj do { 5087827Sroot if (uio->uio_resid <= 0) 5097747Sroot break; 5107827Sroot len = uio->uio_resid; 5117747Sroot so->so_state &= ~SS_RCVATMARK; 5128319Sroot if (tomark && len > tomark) 5138319Sroot len = tomark; 5148548Sroot if (moff+len > m->m_len - moff) 5158319Sroot len = m->m_len - moff; 5164786Swnj splx(s); 5178594Sroot error = 5188793Sroot uiomove(mtod(m, caddr_t) + moff, (int)len, UIO_READ, uio); 5194786Swnj s = splnet(); 5204786Swnj if (len == m->m_len) { 5216091Sroot eor = (int)m->m_act; 52212757Ssam if (flags & MSG_PEEK) 5238319Sroot m = m->m_next; 5248319Sroot else { 5258319Sroot sbfree(&so->so_rcv, m); 5268319Sroot MFREE(m, n); 5278319Sroot m = n; 5288548Sroot so->so_rcv.sb_mb = m; 5298319Sroot } 5308319Sroot moff = 0; 5314786Swnj } else { 53212757Ssam if (flags & MSG_PEEK) 5338319Sroot moff += len; 5348319Sroot else { 5358319Sroot m->m_off += len; 5368319Sroot m->m_len -= len; 5378319Sroot so->so_rcv.sb_cc -= len; 5388319Sroot } 5394786Swnj } 54012757Ssam if ((flags & MSG_PEEK) == 0 && so->so_oobmark) { 5417747Sroot so->so_oobmark -= len; 5427747Sroot if (so->so_oobmark == 0) { 5437747Sroot so->so_state |= SS_RCVATMARK; 5447747Sroot break; 5457747Sroot } 5467747Sroot } 5478319Sroot if (tomark) { 5488319Sroot tomark -= len; 5498319Sroot if (tomark == 0) 5508319Sroot break; 5518319Sroot } 5528594Sroot } while (m && error == 0 && !eor); 55312757Ssam if (flags & MSG_PEEK) 5548319Sroot goto release; 5554786Swnj if ((so->so_proto->pr_flags & PR_ATOMIC) && eor == 0) 5564786Swnj do { 5574786Swnj if (m == 0) 55810137Ssam panic("receive 4"); 5594890Swnj sbfree(&so->so_rcv, m); 5604786Swnj eor = (int)m->m_act; 5614786Swnj so->so_rcv.sb_mb = m->m_next; 5624786Swnj MFREE(m, n); 5634890Swnj m = n; 5644786Swnj } while (eor == 0); 5654890Swnj if ((so->so_proto->pr_flags & PR_WANTRCVD) && so->so_pcb) 5668300Sroot (*so->so_proto->pr_usrreq)(so, PRU_RCVD, 56712757Ssam (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0); 5684890Swnj release: 5694916Swnj sbunlock(&so->so_rcv); 57012757Ssam if (error == 0 && rightsp && 57112757Ssam *rightsp && so->so_proto->pr_family == AF_UNIX) 57212757Ssam error = unp_externalize(*rightsp); 5734890Swnj splx(s); 5744916Swnj return (error); 5754786Swnj } 5764786Swnj 57710267Ssam soshutdown(so, how) 57812757Ssam register struct socket *so; 57912757Ssam register int how; 58010267Ssam { 58112757Ssam register struct protosw *pr = so->so_proto; 58210267Ssam 58310267Ssam how++; 58412757Ssam if (how & FREAD) 58512757Ssam sorflush(so); 58610267Ssam if (how & FWRITE) 58712757Ssam return ((*pr->pr_usrreq)(so, PRU_SHUTDOWN, 58812757Ssam (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)); 58910267Ssam return (0); 59010267Ssam } 59110267Ssam 59212757Ssam sorflush(so) 59312757Ssam register struct socket *so; 59412757Ssam { 59512757Ssam register struct sockbuf *sb = &so->so_rcv; 59612757Ssam register struct protosw *pr = so->so_proto; 59712757Ssam register int s; 59812757Ssam struct sockbuf asb; 59912757Ssam 60012757Ssam sblock(sb); 60112757Ssam s = splimp(); 60212757Ssam socantrcvmore(so); 60312757Ssam sbunlock(sb); 60412757Ssam asb = *sb; 60512757Ssam bzero((caddr_t)sb, sizeof (*sb)); 60612757Ssam splx(s); 60712757Ssam if (pr->pr_family == AF_UNIX && (pr->pr_flags & PR_RIGHTS)) 60812757Ssam unp_scan(asb.sb_mb, unp_discard); 60912757Ssam sbrelease(&asb); 61012757Ssam } 61112757Ssam 61210267Ssam sosetopt(so, level, optname, m) 61312757Ssam register struct socket *so; 61410267Ssam int level, optname; 61512757Ssam register struct mbuf *m; 61610267Ssam { 61710267Ssam 61810267Ssam if (level != SOL_SOCKET) 61910267Ssam return (EINVAL); /* XXX */ 62010267Ssam switch (optname) { 62110267Ssam 62210267Ssam case SO_DEBUG: 62310269Ssam case SO_KEEPALIVE: 62410599Ssam case SO_DONTROUTE: 62510599Ssam case SO_USELOOPBACK: 62610599Ssam case SO_REUSEADDR: 62710599Ssam so->so_options |= optname; 62810269Ssam break; 62910269Ssam 63010267Ssam case SO_LINGER: 63110269Ssam if (m == NULL || m->m_len != sizeof (int)) 63210269Ssam return (EINVAL); 63310267Ssam so->so_options |= SO_LINGER; 63410269Ssam so->so_linger = *mtod(m, int *); 63510267Ssam break; 63610267Ssam 63710267Ssam case SO_DONTLINGER: 63810267Ssam so->so_options &= ~SO_LINGER; 63910267Ssam so->so_linger = 0; 64010267Ssam break; 64110267Ssam 64210267Ssam default: 64310267Ssam return (EINVAL); 64410267Ssam } 64510267Ssam return (0); 64610267Ssam } 64710267Ssam 64810267Ssam sogetopt(so, level, optname, m) 64912757Ssam register struct socket *so; 65010267Ssam int level, optname; 65112757Ssam register struct mbuf *m; 65210267Ssam { 65310267Ssam 65410267Ssam if (level != SOL_SOCKET) 65510267Ssam return (EINVAL); /* XXX */ 65610267Ssam switch (optname) { 65710267Ssam 65810267Ssam case SO_USELOOPBACK: 65910267Ssam case SO_DONTROUTE: 66010267Ssam case SO_DEBUG: 66110267Ssam case SO_KEEPALIVE: 66210267Ssam case SO_LINGER: 66310599Ssam case SO_REUSEADDR: 66410267Ssam if ((so->so_options & optname) == 0) 66510267Ssam return (ENOPROTOOPT); 66610269Ssam if (optname == SO_LINGER && m != NULL) { 66710267Ssam *mtod(m, int *) = so->so_linger; 66810267Ssam m->m_len = sizeof (so->so_linger); 66910267Ssam } 67010267Ssam break; 67110267Ssam 67210267Ssam default: 67310267Ssam return (EINVAL); 67410267Ssam } 67510267Ssam return (0); 67610267Ssam } 67710267Ssam 6785423Swnj sohasoutofband(so) 67912757Ssam register struct socket *so; 6805423Swnj { 6815423Swnj 6825423Swnj if (so->so_pgrp == 0) 6835423Swnj return; 6845423Swnj if (so->so_pgrp > 0) 6855423Swnj gsignal(so->so_pgrp, SIGURG); 6865429Swnj else { 6875429Swnj struct proc *p = pfind(-so->so_pgrp); 6885429Swnj 6895429Swnj if (p) 6905429Swnj psignal(p, SIGURG); 6915429Swnj } 6925423Swnj } 693