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*27191Skarels * @(#)uipc_socket.c 6.26 (Berkeley) 04/19/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 MGET(m, M_WAIT, MT_DATA); 34826958Skarels if (uio->uio_resid >= CLBYTES / 2 && space >= CLBYTES) { 34926228Skarels MCLGET(m); 35026228Skarels if (m->m_len != CLBYTES) 35116412Skarels goto nopages; 35226958Skarels len = MIN(CLBYTES, uio->uio_resid); 35321767Skarels space -= CLBYTES; 35416412Skarels } else { 35516412Skarels nopages: 35626958Skarels len = MIN(MIN(MLEN, uio->uio_resid), space); 35721767Skarels space -= len; 35816412Skarels } 35916412Skarels error = uiomove(mtod(m, caddr_t), len, UIO_WRITE, uio); 36016412Skarels m->m_len = len; 36116412Skarels *mp = m; 36216412Skarels if (error) 36316412Skarels goto release; 36416412Skarels mp = &m->m_next; 36521108Skarels if (uio->uio_resid <= 0) 36621108Skarels break; 36716412Skarels } 36821108Skarels if (dontroute) 36921108Skarels so->so_options |= SO_DONTROUTE; 37021108Skarels s = splnet(); /* XXX */ 37121108Skarels error = (*so->so_proto->pr_usrreq)(so, 37221108Skarels (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND, 37321108Skarels top, (caddr_t)nam, rights); 37421108Skarels splx(s); 37521108Skarels if (dontroute) 37621108Skarels so->so_options &= ~SO_DONTROUTE; 37721767Skarels rights = 0; 37825629Skarels rlen = 0; 3796419Sroot top = 0; 38016412Skarels first = 0; 38114781Ssam if (error) 38216412Skarels break; 38316412Skarels } while (uio->uio_resid); 3844890Swnj 3854786Swnj release: 3864890Swnj sbunlock(&so->so_snd); 3876419Sroot if (top) 3886419Sroot m_freem(top); 38921108Skarels if (error == EPIPE) 39021108Skarels psignal(u.u_procp, SIGPIPE); 3914786Swnj return (error); 3924786Swnj } 3934786Swnj 39425629Skarels /* 39525629Skarels * Implement receive operations on a socket. 39625629Skarels * We depend on the way that records are added to the sockbuf 39725629Skarels * by sbappend*. In particular, each record (mbufs linked through m_next) 39825629Skarels * must begin with an address if the protocol so specifies, 39925629Skarels * followed by an optional mbuf containing access rights if supported 40025629Skarels * by the protocol, and then zero or more mbufs of data. 40125629Skarels * In order to avoid blocking network interrupts for the entire time here, 40225629Skarels * we splx() while doing the actual copy to user space. 40325629Skarels * Although the sockbuf is locked, new data may still be appended, 40425629Skarels * and thus we must maintain consistency of the sockbuf during that time. 40525629Skarels */ 40612757Ssam soreceive(so, aname, uio, flags, rightsp) 4074786Swnj register struct socket *so; 4088300Sroot struct mbuf **aname; 40912757Ssam register struct uio *uio; 4108319Sroot int flags; 41112757Ssam struct mbuf **rightsp; 4124786Swnj { 41326958Skarels register struct mbuf *m; 41416993Skarels register int len, error = 0, s, tomark; 41512757Ssam struct protosw *pr = so->so_proto; 41616993Skarels struct mbuf *nextrecord; 41712757Ssam int moff; 4184786Swnj 41912757Ssam if (rightsp) 42012757Ssam *rightsp = 0; 42112757Ssam if (aname) 42212757Ssam *aname = 0; 42312757Ssam if (flags & MSG_OOB) { 4249635Ssam m = m_get(M_WAIT, MT_DATA); 42512757Ssam error = (*pr->pr_usrreq)(so, PRU_RCVOOB, 42624768Skarels m, (struct mbuf *)(flags & MSG_PEEK), (struct mbuf *)0); 4278594Sroot if (error) 42810137Ssam goto bad; 4298319Sroot do { 43010137Ssam len = uio->uio_resid; 4318319Sroot if (len > m->m_len) 4328319Sroot len = m->m_len; 4338594Sroot error = 4348793Sroot uiomove(mtod(m, caddr_t), (int)len, UIO_READ, uio); 4358319Sroot m = m_free(m); 4368594Sroot } while (uio->uio_resid && error == 0 && m); 43710137Ssam bad: 4388319Sroot if (m) 4398771Sroot m_freem(m); 4408594Sroot return (error); 4418319Sroot } 4428319Sroot 4434890Swnj restart: 4444890Swnj sblock(&so->so_rcv); 4458835Sroot s = splnet(); 4464890Swnj 4474890Swnj #define rcverr(errno) { error = errno; splx(s); goto release; } 4484786Swnj if (so->so_rcv.sb_cc == 0) { 4495168Swnj if (so->so_error) { 4505168Swnj error = so->so_error; 4515168Swnj so->so_error = 0; 4525168Swnj splx(s); 4535168Swnj goto release; 4545168Swnj } 4554890Swnj if (so->so_state & SS_CANTRCVMORE) { 4564890Swnj splx(s); 4574890Swnj goto release; 4584890Swnj } 4595015Sroot if ((so->so_state & SS_ISCONNECTED) == 0 && 4605015Sroot (so->so_proto->pr_flags & PR_CONNREQUIRED)) 4615015Sroot rcverr(ENOTCONN); 46225629Skarels if (uio->uio_resid == 0) 46325629Skarels goto release; 4646214Swnj if (so->so_state & SS_NBIO) 4655168Swnj rcverr(EWOULDBLOCK); 4664890Swnj sbunlock(&so->so_rcv); 4674971Swnj sbwait(&so->so_rcv); 4685012Swnj splx(s); 4694890Swnj goto restart; 4704786Swnj } 4718041Sroot u.u_ru.ru_msgrcv++; 4724829Swnj m = so->so_rcv.sb_mb; 47325629Skarels if (m == 0) 47425629Skarels panic("receive 1"); 47525629Skarels nextrecord = m->m_act; 47612757Ssam if (pr->pr_flags & PR_ADDR) { 47725629Skarels if (m->m_type != MT_SONAME) 47816993Skarels panic("receive 1a"); 47916993Skarels if (flags & MSG_PEEK) { 48016993Skarels if (aname) 4818319Sroot *aname = m_copy(m, 0, m->m_len); 48225629Skarels m = m->m_next; 48316993Skarels } else { 48425629Skarels sbfree(&so->so_rcv, m); 48516993Skarels if (aname) { 48616993Skarels *aname = m; 48725629Skarels m = m->m_next; 48825629Skarels (*aname)->m_next = 0; 48926958Skarels so->so_rcv.sb_mb = m; 49025629Skarels } else { 49126958Skarels MFREE(m, so->so_rcv.sb_mb); 49226958Skarels m = so->so_rcv.sb_mb; 49325629Skarels } 49426958Skarels if (m) 49526958Skarels m->m_act = nextrecord; 49616993Skarels } 49716993Skarels } 49816993Skarels if (m && m->m_type == MT_RIGHTS) { 49916993Skarels if ((pr->pr_flags & PR_RIGHTS) == 0) 50026958Skarels panic("receive 2"); 50116993Skarels if (flags & MSG_PEEK) { 50216993Skarels if (rightsp) 50312757Ssam *rightsp = m_copy(m, 0, m->m_len); 50425629Skarels m = m->m_next; 50516993Skarels } else { 50625629Skarels sbfree(&so->so_rcv, m); 50716993Skarels if (rightsp) { 50816993Skarels *rightsp = m; 50926958Skarels so->so_rcv.sb_mb = m->m_next; 51025629Skarels m->m_next = 0; 51126958Skarels m = so->so_rcv.sb_mb; 51225629Skarels } else { 51326958Skarels MFREE(m, so->so_rcv.sb_mb); 51426958Skarels m = so->so_rcv.sb_mb; 51525629Skarels } 51626958Skarels if (m) 51726958Skarels m->m_act = nextrecord; 51812757Ssam } 5194890Swnj } 5208319Sroot moff = 0; 5218319Sroot tomark = so->so_oobmark; 52216993Skarels while (m && uio->uio_resid > 0 && error == 0) { 52325629Skarels if (m->m_type != MT_DATA && m->m_type != MT_HEADER) 52425629Skarels panic("receive 3"); 5257827Sroot len = uio->uio_resid; 5267747Sroot so->so_state &= ~SS_RCVATMARK; 5278319Sroot if (tomark && len > tomark) 5288319Sroot len = tomark; 52921767Skarels if (len > m->m_len - moff) 5308319Sroot len = m->m_len - moff; 5314786Swnj splx(s); 5328594Sroot error = 5338793Sroot uiomove(mtod(m, caddr_t) + moff, (int)len, UIO_READ, uio); 5344786Swnj s = splnet(); 53521767Skarels if (len == m->m_len - moff) { 53625629Skarels if (flags & MSG_PEEK) { 53725629Skarels m = m->m_next; 53825629Skarels moff = 0; 53925629Skarels } else { 54026958Skarels nextrecord = m->m_act; 54125629Skarels sbfree(&so->so_rcv, m); 54226958Skarels MFREE(m, so->so_rcv.sb_mb); 54326958Skarels m = so->so_rcv.sb_mb; 54426958Skarels if (m) 54526958Skarels m->m_act = nextrecord; 54625629Skarels } 5474786Swnj } else { 54812757Ssam if (flags & MSG_PEEK) 5498319Sroot moff += len; 5508319Sroot else { 5518319Sroot m->m_off += len; 5528319Sroot m->m_len -= len; 5538319Sroot so->so_rcv.sb_cc -= len; 5548319Sroot } 5554786Swnj } 55612757Ssam if ((flags & MSG_PEEK) == 0 && so->so_oobmark) { 5577747Sroot so->so_oobmark -= len; 5587747Sroot if (so->so_oobmark == 0) { 5597747Sroot so->so_state |= SS_RCVATMARK; 5607747Sroot break; 5617747Sroot } 5627747Sroot } 5638319Sroot if (tomark) { 5648319Sroot tomark -= len; 5658319Sroot if (tomark == 0) 5668319Sroot break; 5678319Sroot } 56816993Skarels } 56916993Skarels if ((flags & MSG_PEEK) == 0) { 57026500Skarels if (m == 0) 57116993Skarels so->so_rcv.sb_mb = nextrecord; 57226958Skarels else if (pr->pr_flags & PR_ATOMIC) 57326958Skarels (void) sbdroprecord(&so->so_rcv); 57416993Skarels if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) 57516993Skarels (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, 57616993Skarels (struct mbuf *)0, (struct mbuf *)0); 57725629Skarels if (error == 0 && rightsp && *rightsp && 57825629Skarels pr->pr_domain->dom_externalize) 57925629Skarels error = (*pr->pr_domain->dom_externalize)(*rightsp); 58016993Skarels } 5814890Swnj release: 5824916Swnj sbunlock(&so->so_rcv); 5834890Swnj splx(s); 5844916Swnj return (error); 5854786Swnj } 5864786Swnj 58710267Ssam soshutdown(so, how) 58812757Ssam register struct socket *so; 58912757Ssam register int how; 59010267Ssam { 59112757Ssam register struct protosw *pr = so->so_proto; 59210267Ssam 59310267Ssam how++; 59412757Ssam if (how & FREAD) 59512757Ssam sorflush(so); 59610267Ssam if (how & FWRITE) 59712757Ssam return ((*pr->pr_usrreq)(so, PRU_SHUTDOWN, 59812757Ssam (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)); 59910267Ssam return (0); 60010267Ssam } 60110267Ssam 60212757Ssam sorflush(so) 60312757Ssam register struct socket *so; 60412757Ssam { 60512757Ssam register struct sockbuf *sb = &so->so_rcv; 60612757Ssam register struct protosw *pr = so->so_proto; 60712757Ssam register int s; 60812757Ssam struct sockbuf asb; 60912757Ssam 61012757Ssam sblock(sb); 61112757Ssam s = splimp(); 61212757Ssam socantrcvmore(so); 61312757Ssam sbunlock(sb); 61412757Ssam asb = *sb; 61512757Ssam bzero((caddr_t)sb, sizeof (*sb)); 61612757Ssam splx(s); 61716993Skarels if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) 61816993Skarels (*pr->pr_domain->dom_dispose)(asb.sb_mb); 61912757Ssam sbrelease(&asb); 62012757Ssam } 62112757Ssam 62218553Skarels sosetopt(so, level, optname, m0) 62312757Ssam register struct socket *so; 62410267Ssam int level, optname; 62518553Skarels struct mbuf *m0; 62610267Ssam { 62717158Ssam int error = 0; 62818553Skarels register struct mbuf *m = m0; 62910267Ssam 63017158Ssam if (level != SOL_SOCKET) { 63118369Skarels if (so->so_proto && so->so_proto->pr_ctloutput) 63218369Skarels return ((*so->so_proto->pr_ctloutput) 63318553Skarels (PRCO_SETOPT, so, level, optname, &m0)); 63418369Skarels error = ENOPROTOOPT; 63518369Skarels } else { 63618369Skarels switch (optname) { 63710267Ssam 63818369Skarels case SO_LINGER: 63918369Skarels if (m == NULL || m->m_len != sizeof (struct linger)) { 64018369Skarels error = EINVAL; 64118369Skarels goto bad; 64218369Skarels } 64318369Skarels so->so_linger = mtod(m, struct linger *)->l_linger; 64418369Skarels /* fall thru... */ 64517158Ssam 64618369Skarels case SO_DEBUG: 64718369Skarels case SO_KEEPALIVE: 64818369Skarels case SO_DONTROUTE: 64918369Skarels case SO_USELOOPBACK: 65018369Skarels case SO_BROADCAST: 65118369Skarels case SO_REUSEADDR: 652*27191Skarels case SO_OOBINLINE: 65318369Skarels if (m == NULL || m->m_len < sizeof (int)) { 65418369Skarels error = EINVAL; 65518369Skarels goto bad; 65618369Skarels } 65718369Skarels if (*mtod(m, int *)) 65818369Skarels so->so_options |= optname; 65918369Skarels else 66018369Skarels so->so_options &= ~optname; 66118369Skarels break; 66218369Skarels 66318369Skarels case SO_SNDBUF: 66418369Skarels case SO_RCVBUF: 66518369Skarels case SO_SNDLOWAT: 66618369Skarels case SO_RCVLOWAT: 66718369Skarels case SO_SNDTIMEO: 66818369Skarels case SO_RCVTIMEO: 66918369Skarels if (m == NULL || m->m_len < sizeof (int)) { 67018369Skarels error = EINVAL; 67118369Skarels goto bad; 67218369Skarels } 67318369Skarels switch (optname) { 67418369Skarels 67518369Skarels case SO_SNDBUF: 67618369Skarels case SO_RCVBUF: 67718369Skarels if (sbreserve(optname == SO_SNDBUF ? &so->so_snd : 67818369Skarels &so->so_rcv, *mtod(m, int *)) == 0) { 67918369Skarels error = ENOBUFS; 68018369Skarels goto bad; 68118369Skarels } 68218369Skarels break; 68318369Skarels 68418369Skarels case SO_SNDLOWAT: 68518369Skarels so->so_snd.sb_lowat = *mtod(m, int *); 68618369Skarels break; 68718369Skarels case SO_RCVLOWAT: 68818369Skarels so->so_rcv.sb_lowat = *mtod(m, int *); 68918369Skarels break; 69018369Skarels case SO_SNDTIMEO: 69118369Skarels so->so_snd.sb_timeo = *mtod(m, int *); 69218369Skarels break; 69318369Skarels case SO_RCVTIMEO: 69418369Skarels so->so_rcv.sb_timeo = *mtod(m, int *); 69518369Skarels break; 69618369Skarels } 69718369Skarels break; 69818369Skarels 69918369Skarels default: 70018369Skarels error = ENOPROTOOPT; 70118369Skarels break; 70217158Ssam } 70310267Ssam } 70417158Ssam bad: 70517158Ssam if (m) 70617158Ssam (void) m_free(m); 70717158Ssam return (error); 70810267Ssam } 70910267Ssam 71017158Ssam sogetopt(so, level, optname, mp) 71112757Ssam register struct socket *so; 71210267Ssam int level, optname; 71317158Ssam struct mbuf **mp; 71417158Ssam { 71512757Ssam register struct mbuf *m; 71610267Ssam 71718369Skarels if (level != SOL_SOCKET) { 71818369Skarels if (so->so_proto && so->so_proto->pr_ctloutput) { 71918369Skarels return ((*so->so_proto->pr_ctloutput) 72018369Skarels (PRCO_GETOPT, so, level, optname, mp)); 72118369Skarels } else 72218369Skarels return (ENOPROTOOPT); 72318369Skarels } else { 72417158Ssam m = m_get(M_WAIT, MT_SOOPTS); 72525502Skarels m->m_len = sizeof (int); 72625502Skarels 72718369Skarels switch (optname) { 72817158Ssam 72918369Skarels case SO_LINGER: 73018369Skarels m->m_len = sizeof (struct linger); 73118369Skarels mtod(m, struct linger *)->l_onoff = 73218369Skarels so->so_options & SO_LINGER; 73318369Skarels mtod(m, struct linger *)->l_linger = so->so_linger; 73418369Skarels break; 73510267Ssam 73618369Skarels case SO_USELOOPBACK: 73718369Skarels case SO_DONTROUTE: 73818369Skarels case SO_DEBUG: 73918369Skarels case SO_KEEPALIVE: 74018369Skarels case SO_REUSEADDR: 74118369Skarels case SO_BROADCAST: 742*27191Skarels case SO_OOBINLINE: 74318369Skarels *mtod(m, int *) = so->so_options & optname; 74418369Skarels break; 74518369Skarels 74625502Skarels case SO_TYPE: 74725502Skarels *mtod(m, int *) = so->so_type; 74825502Skarels break; 74925502Skarels 75024768Skarels case SO_ERROR: 75124768Skarels *mtod(m, int *) = so->so_error; 75224768Skarels so->so_error = 0; 75324768Skarels break; 75424768Skarels 75518369Skarels case SO_SNDBUF: 75618369Skarels *mtod(m, int *) = so->so_snd.sb_hiwat; 75718369Skarels break; 75818369Skarels 75918369Skarels case SO_RCVBUF: 76018369Skarels *mtod(m, int *) = so->so_rcv.sb_hiwat; 76118369Skarels break; 76218369Skarels 76318369Skarels case SO_SNDLOWAT: 76418369Skarels *mtod(m, int *) = so->so_snd.sb_lowat; 76518369Skarels break; 76618369Skarels 76718369Skarels case SO_RCVLOWAT: 76818369Skarels *mtod(m, int *) = so->so_rcv.sb_lowat; 76918369Skarels break; 77018369Skarels 77118369Skarels case SO_SNDTIMEO: 77218369Skarels *mtod(m, int *) = so->so_snd.sb_timeo; 77318369Skarels break; 77418369Skarels 77518369Skarels case SO_RCVTIMEO: 77618369Skarels *mtod(m, int *) = so->so_rcv.sb_timeo; 77718369Skarels break; 77818369Skarels 77918369Skarels default: 78026362Skarels (void)m_free(m); 78118369Skarels return (ENOPROTOOPT); 78218369Skarels } 78318369Skarels *mp = m; 78418369Skarels return (0); 78510267Ssam } 78610267Ssam } 78710267Ssam 7885423Swnj sohasoutofband(so) 78912757Ssam register struct socket *so; 7905423Swnj { 79123233Skarels struct proc *p; 7925423Swnj 79323233Skarels if (so->so_pgrp < 0) 79423233Skarels gsignal(-so->so_pgrp, SIGURG); 79523233Skarels else if (so->so_pgrp > 0 && (p = pfind(so->so_pgrp)) != 0) 79623233Skarels psignal(p, SIGURG); 79724768Skarels if (so->so_rcv.sb_sel) { 79824768Skarels selwakeup(so->so_rcv.sb_sel, so->so_rcv.sb_flags & SB_COLL); 79924768Skarels so->so_rcv.sb_sel = 0; 80024768Skarels so->so_rcv.sb_flags &= ~SB_COLL; 80124768Skarels } 8025423Swnj } 803