|
|
1.45 ! tv 1: /* $NetBSD: uipc_socket.c,v 1.44 1999/03/23 10:45:37 lukem Exp $ */
1.16 cgd 2:
1.1 cgd 3: /*
1.15 mycroft 4: * Copyright (c) 1982, 1986, 1988, 1990, 1993
5: * The Regents of the University of California. All rights reserved.
1.1 cgd 6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: *
1.32 fvdl 35: * @(#)uipc_socket.c 8.6 (Berkeley) 5/2/95
1.1 cgd 36: */
1.35 thorpej 37:
38: #include "opt_compat_sunos.h"
1.1 cgd 39:
1.9 mycroft 40: #include <sys/param.h>
41: #include <sys/systm.h>
42: #include <sys/proc.h>
43: #include <sys/file.h>
44: #include <sys/malloc.h>
45: #include <sys/mbuf.h>
46: #include <sys/domain.h>
47: #include <sys/kernel.h>
48: #include <sys/protosw.h>
49: #include <sys/socket.h>
50: #include <sys/socketvar.h>
1.21 christos 51: #include <sys/signalvar.h>
1.9 mycroft 52: #include <sys/resourcevar.h>
1.37 thorpej 53: #include <sys/pool.h>
54:
55: struct pool socket_pool;
56:
57: void
58: soinit()
59: {
60:
61: pool_init(&socket_pool, sizeof(struct socket), 0, 0, 0,
62: "sockpl", 0, NULL, NULL, M_SOCKET);
63: }
1.1 cgd 64:
65: /*
66: * Socket operation routines.
67: * These routines are called by the routines in
68: * sys_socket.c or from a system process, and
69: * implement the semantics of socket operations by
70: * switching out to the protocol specific routines.
71: */
72: /*ARGSUSED*/
1.3 andrew 73: int
1.1 cgd 74: socreate(dom, aso, type, proto)
1.11 mycroft 75: int dom;
1.1 cgd 76: struct socket **aso;
77: register int type;
78: int proto;
79: {
80: struct proc *p = curproc; /* XXX */
81: register struct protosw *prp;
82: register struct socket *so;
83: register int error;
1.39 matt 84: int s;
1.1 cgd 85:
86: if (proto)
87: prp = pffindproto(dom, proto, type);
88: else
89: prp = pffindtype(dom, type);
1.15 mycroft 90: if (prp == 0 || prp->pr_usrreq == 0)
1.1 cgd 91: return (EPROTONOSUPPORT);
92: if (prp->pr_type != type)
93: return (EPROTOTYPE);
1.39 matt 94: s = splsoftnet();
1.37 thorpej 95: so = pool_get(&socket_pool, PR_WAITOK);
1.38 perry 96: memset((caddr_t)so, 0, sizeof(*so));
1.31 thorpej 97: TAILQ_INIT(&so->so_q0);
98: TAILQ_INIT(&so->so_q);
1.1 cgd 99: so->so_type = type;
100: so->so_proto = prp;
1.33 matt 101: so->so_send = sosend;
102: so->so_receive = soreceive;
1.44 lukem 103: if (p != 0)
104: so->so_uid = p->p_ucred->cr_uid;
1.22 mycroft 105: error = (*prp->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0,
106: (struct mbuf *)(long)proto, (struct mbuf *)0, p);
1.1 cgd 107: if (error) {
108: so->so_state |= SS_NOFDREF;
109: sofree(so);
1.39 matt 110: splx(s);
1.1 cgd 111: return (error);
112: }
1.10 deraadt 113: #ifdef COMPAT_SUNOS
1.18 christos 114: {
115: extern struct emul emul_sunos;
116: if (p->p_emul == &emul_sunos && type == SOCK_DGRAM)
117: so->so_options |= SO_BROADCAST;
118: }
1.10 deraadt 119: #endif
1.39 matt 120: splx(s);
1.1 cgd 121: *aso = so;
122: return (0);
123: }
124:
1.3 andrew 125: int
1.1 cgd 126: sobind(so, nam)
127: struct socket *so;
128: struct mbuf *nam;
129: {
1.22 mycroft 130: struct proc *p = curproc; /* XXX */
1.20 mycroft 131: int s = splsoftnet();
1.1 cgd 132: int error;
133:
1.22 mycroft 134: error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, (struct mbuf *)0,
135: nam, (struct mbuf *)0, p);
1.1 cgd 136: splx(s);
137: return (error);
138: }
139:
1.3 andrew 140: int
1.1 cgd 141: solisten(so, backlog)
142: register struct socket *so;
143: int backlog;
144: {
1.20 mycroft 145: int s = splsoftnet(), error;
1.1 cgd 146:
1.22 mycroft 147: error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, (struct mbuf *)0,
148: (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
1.1 cgd 149: if (error) {
150: splx(s);
151: return (error);
152: }
1.31 thorpej 153: if (so->so_q.tqh_first == NULL)
1.1 cgd 154: so->so_options |= SO_ACCEPTCONN;
155: if (backlog < 0)
156: backlog = 0;
157: so->so_qlimit = min(backlog, SOMAXCONN);
158: splx(s);
159: return (0);
160: }
161:
1.21 christos 162: void
1.1 cgd 163: sofree(so)
164: register struct socket *so;
165: {
166:
1.43 mycroft 167: if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
1.1 cgd 168: return;
1.43 mycroft 169: if (so->so_head) {
170: /*
171: * We must not decommission a socket that's on the accept(2)
172: * queue. If we do, then accept(2) may hang after select(2)
173: * indicated that the listening socket was ready.
174: */
175: if (!soqremque(so, 0))
176: return;
177: }
1.1 cgd 178: sbrelease(&so->so_snd);
179: sorflush(so);
1.37 thorpej 180: pool_put(&socket_pool, so);
1.1 cgd 181: }
182:
183: /*
184: * Close a socket on last file table reference removal.
185: * Initiate disconnect if connected.
186: * Free socket when disconnect complete.
187: */
1.3 andrew 188: int
1.1 cgd 189: soclose(so)
190: register struct socket *so;
191: {
1.41 mycroft 192: struct socket *so2;
1.20 mycroft 193: int s = splsoftnet(); /* conservative */
1.1 cgd 194: int error = 0;
195:
196: if (so->so_options & SO_ACCEPTCONN) {
1.41 mycroft 197: while ((so2 = so->so_q0.tqh_first) != 0) {
1.42 mycroft 198: (void) soqremque(so2, 0);
1.41 mycroft 199: (void) soabort(so2);
200: }
201: while ((so2 = so->so_q.tqh_first) != 0) {
1.42 mycroft 202: (void) soqremque(so2, 1);
1.41 mycroft 203: (void) soabort(so2);
204: }
1.1 cgd 205: }
206: if (so->so_pcb == 0)
207: goto discard;
208: if (so->so_state & SS_ISCONNECTED) {
209: if ((so->so_state & SS_ISDISCONNECTING) == 0) {
210: error = sodisconnect(so);
211: if (error)
212: goto drop;
213: }
214: if (so->so_options & SO_LINGER) {
215: if ((so->so_state & SS_ISDISCONNECTING) &&
216: (so->so_state & SS_NBIO))
217: goto drop;
1.21 christos 218: while (so->so_state & SS_ISCONNECTED) {
219: error = tsleep((caddr_t)&so->so_timeo,
220: PSOCK | PCATCH, netcls,
1.30 thorpej 221: so->so_linger * hz);
1.21 christos 222: if (error)
1.1 cgd 223: break;
1.21 christos 224: }
1.1 cgd 225: }
226: }
227: drop:
228: if (so->so_pcb) {
1.22 mycroft 229: int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH,
230: (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
231: (struct proc *)0);
1.1 cgd 232: if (error == 0)
233: error = error2;
234: }
235: discard:
236: if (so->so_state & SS_NOFDREF)
237: panic("soclose: NOFDREF");
238: so->so_state |= SS_NOFDREF;
239: sofree(so);
240: splx(s);
241: return (error);
242: }
243:
244: /*
1.20 mycroft 245: * Must be called at splsoftnet...
1.1 cgd 246: */
1.3 andrew 247: int
1.1 cgd 248: soabort(so)
249: struct socket *so;
250: {
251:
1.22 mycroft 252: return (*so->so_proto->pr_usrreq)(so, PRU_ABORT, (struct mbuf *)0,
253: (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
1.1 cgd 254: }
255:
1.3 andrew 256: int
1.1 cgd 257: soaccept(so, nam)
258: register struct socket *so;
259: struct mbuf *nam;
260: {
1.20 mycroft 261: int s = splsoftnet();
1.1 cgd 262: int error;
263:
264: if ((so->so_state & SS_NOFDREF) == 0)
265: panic("soaccept: !NOFDREF");
266: so->so_state &= ~SS_NOFDREF;
1.41 mycroft 267: if ((so->so_state & SS_ISDISCONNECTED) == 0)
268: error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT,
269: (struct mbuf *)0, nam, (struct mbuf *)0, (struct proc *)0);
270: else
271: error = 0;
1.1 cgd 272: splx(s);
273: return (error);
274: }
275:
1.3 andrew 276: int
1.1 cgd 277: soconnect(so, nam)
278: register struct socket *so;
279: struct mbuf *nam;
280: {
1.23 mycroft 281: struct proc *p = curproc; /* XXX */
1.1 cgd 282: int s;
283: int error;
284:
285: if (so->so_options & SO_ACCEPTCONN)
286: return (EOPNOTSUPP);
1.20 mycroft 287: s = splsoftnet();
1.1 cgd 288: /*
289: * If protocol is connection-based, can only connect once.
290: * Otherwise, if connected, try to disconnect first.
291: * This allows user to disconnect by connecting to, e.g.,
292: * a null address.
293: */
294: if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
295: ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
296: (error = sodisconnect(so))))
297: error = EISCONN;
298: else
299: error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
1.23 mycroft 300: (struct mbuf *)0, nam, (struct mbuf *)0, p);
1.1 cgd 301: splx(s);
302: return (error);
303: }
304:
1.3 andrew 305: int
1.1 cgd 306: soconnect2(so1, so2)
307: register struct socket *so1;
308: struct socket *so2;
309: {
1.20 mycroft 310: int s = splsoftnet();
1.1 cgd 311: int error;
312:
1.22 mycroft 313: error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,
314: (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0,
315: (struct proc *)0);
1.1 cgd 316: splx(s);
317: return (error);
318: }
319:
1.3 andrew 320: int
1.1 cgd 321: sodisconnect(so)
322: register struct socket *so;
323: {
1.20 mycroft 324: int s = splsoftnet();
1.1 cgd 325: int error;
326:
327: if ((so->so_state & SS_ISCONNECTED) == 0) {
328: error = ENOTCONN;
329: goto bad;
330: }
331: if (so->so_state & SS_ISDISCONNECTING) {
332: error = EALREADY;
333: goto bad;
334: }
1.22 mycroft 335: error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
336: (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
337: (struct proc *)0);
1.1 cgd 338: bad:
339: splx(s);
340: return (error);
341: }
342:
1.15 mycroft 343: #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
1.1 cgd 344: /*
345: * Send on a socket.
346: * If send must go all at once and message is larger than
347: * send buffering, then hard error.
348: * Lock against other senders.
349: * If must go all at once and not enough room now, then
350: * inform user that this would block and do nothing.
351: * Otherwise, if nonblocking, send as much as possible.
352: * The data to be sent is described by "uio" if nonzero,
353: * otherwise by the mbuf chain "top" (which must be null
354: * if uio is not). Data provided in mbuf chain must be small
355: * enough to send all at once.
356: *
357: * Returns nonzero on error, timeout or signal; callers
358: * must check for short counts if EINTR/ERESTART are returned.
359: * Data and control buffers are freed on return.
360: */
1.3 andrew 361: int
1.1 cgd 362: sosend(so, addr, uio, top, control, flags)
363: register struct socket *so;
364: struct mbuf *addr;
365: struct uio *uio;
366: struct mbuf *top;
367: struct mbuf *control;
368: int flags;
369: {
1.15 mycroft 370: struct proc *p = curproc; /* XXX */
1.1 cgd 371: struct mbuf **mp;
372: register struct mbuf *m;
373: register long space, len, resid;
374: int clen = 0, error, s, dontroute, mlen;
375: int atomic = sosendallatonce(so) || top;
376:
377: if (uio)
378: resid = uio->uio_resid;
379: else
380: resid = top->m_pkthdr.len;
1.7 cgd 381: /*
382: * In theory resid should be unsigned.
383: * However, space must be signed, as it might be less than 0
384: * if we over-committed, and we must use a signed comparison
385: * of space and resid. On the other hand, a negative resid
386: * causes us to loop sending 0-length segments to the protocol.
387: */
1.29 mycroft 388: if (resid < 0) {
389: error = EINVAL;
390: goto out;
391: }
1.1 cgd 392: dontroute =
393: (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
394: (so->so_proto->pr_flags & PR_ATOMIC);
1.12 mycroft 395: p->p_stats->p_ru.ru_msgsnd++;
1.1 cgd 396: if (control)
397: clen = control->m_len;
398: #define snderr(errno) { error = errno; splx(s); goto release; }
399:
400: restart:
1.21 christos 401: if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0)
1.1 cgd 402: goto out;
403: do {
1.20 mycroft 404: s = splsoftnet();
1.1 cgd 405: if (so->so_state & SS_CANTSENDMORE)
406: snderr(EPIPE);
407: if (so->so_error)
408: snderr(so->so_error);
409: if ((so->so_state & SS_ISCONNECTED) == 0) {
410: if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
411: if ((so->so_state & SS_ISCONFIRMING) == 0 &&
412: !(resid == 0 && clen != 0))
413: snderr(ENOTCONN);
414: } else if (addr == 0)
415: snderr(EDESTADDRREQ);
416: }
417: space = sbspace(&so->so_snd);
418: if (flags & MSG_OOB)
419: space += 1024;
1.21 christos 420: if ((atomic && resid > so->so_snd.sb_hiwat) ||
1.11 mycroft 421: clen > so->so_snd.sb_hiwat)
422: snderr(EMSGSIZE);
423: if (space < resid + clen && uio &&
1.1 cgd 424: (atomic || space < so->so_snd.sb_lowat || space < clen)) {
425: if (so->so_state & SS_NBIO)
426: snderr(EWOULDBLOCK);
427: sbunlock(&so->so_snd);
428: error = sbwait(&so->so_snd);
429: splx(s);
430: if (error)
431: goto out;
432: goto restart;
433: }
434: splx(s);
435: mp = ⊤
436: space -= clen;
437: do {
1.45 ! tv 438: if (uio == NULL) {
! 439: /*
! 440: * Data is prepackaged in "top".
! 441: */
! 442: resid = 0;
! 443: if (flags & MSG_EOR)
! 444: top->m_flags |= M_EOR;
! 445: } else do {
! 446: if (top == 0) {
! 447: MGETHDR(m, M_WAIT, MT_DATA);
! 448: mlen = MHLEN;
! 449: m->m_pkthdr.len = 0;
! 450: m->m_pkthdr.rcvif = (struct ifnet *)0;
! 451: } else {
! 452: MGET(m, M_WAIT, MT_DATA);
! 453: mlen = MLEN;
! 454: }
! 455: if (resid >= MINCLSIZE && space >= MCLBYTES) {
! 456: MCLGET(m, M_WAIT);
! 457: if ((m->m_flags & M_EXT) == 0)
! 458: goto nopages;
! 459: mlen = MCLBYTES;
1.15 mycroft 460: #ifdef MAPPED_MBUFS
1.45 ! tv 461: len = min(MCLBYTES, resid);
1.15 mycroft 462: #else
1.45 ! tv 463: if (atomic && top == 0) {
! 464: len = min(MCLBYTES - max_hdr, resid);
! 465: m->m_data += max_hdr;
! 466: } else
! 467: len = min(MCLBYTES, resid);
1.15 mycroft 468: #endif
1.45 ! tv 469: space -= len;
! 470: } else {
1.1 cgd 471: nopages:
1.45 ! tv 472: len = min(min(mlen, resid), space);
! 473: space -= len;
! 474: /*
! 475: * For datagram protocols, leave room
! 476: * for protocol headers in first mbuf.
! 477: */
! 478: if (atomic && top == 0 && len < mlen)
! 479: MH_ALIGN(m, len);
! 480: }
! 481: error = uiomove(mtod(m, caddr_t), (int)len, uio);
! 482: resid = uio->uio_resid;
! 483: m->m_len = len;
! 484: *mp = m;
! 485: top->m_pkthdr.len += len;
! 486: if (error)
! 487: goto release;
! 488: mp = &m->m_next;
! 489: if (resid <= 0) {
! 490: if (flags & MSG_EOR)
! 491: top->m_flags |= M_EOR;
! 492: break;
! 493: }
! 494: } while (space > 0 && atomic);
! 495:
! 496: if (dontroute)
! 497: so->so_options |= SO_DONTROUTE;
! 498: if (resid > 0)
! 499: so->so_state |= SS_MORETOCOME;
! 500:
! 501: s = splsoftnet(); /* XXX */
! 502: if (so->so_state & SS_CANTSENDMORE)
! 503: error = EPIPE;
! 504: else
! 505: error = (*so->so_proto->pr_usrreq)(so,
! 506: (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
! 507: top, addr, control, p);
! 508: splx(s);
! 509:
! 510: if (dontroute)
! 511: so->so_options &= ~SO_DONTROUTE;
! 512: if (resid > 0)
! 513: so->so_state &= ~SS_MORETOCOME;
! 514: clen = 0;
! 515: control = 0;
! 516: top = 0;
! 517: mp = ⊤
1.1 cgd 518: if (error)
519: goto release;
520: } while (resid && space > 0);
521: } while (resid);
522:
523: release:
524: sbunlock(&so->so_snd);
525: out:
526: if (top)
527: m_freem(top);
528: if (control)
529: m_freem(control);
530: return (error);
531: }
532:
533: /*
534: * Implement receive operations on a socket.
535: * We depend on the way that records are added to the sockbuf
536: * by sbappend*. In particular, each record (mbufs linked through m_next)
537: * must begin with an address if the protocol so specifies,
538: * followed by an optional mbuf or mbufs containing ancillary data,
539: * and then zero or more mbufs of data.
540: * In order to avoid blocking network interrupts for the entire time here,
541: * we splx() while doing the actual copy to user space.
542: * Although the sockbuf is locked, new data may still be appended,
543: * and thus we must maintain consistency of the sockbuf during that time.
544: *
545: * The caller may receive the data as a single mbuf chain by supplying
546: * an mbuf **mp0 for use in returning the chain. The uio is then used
547: * only for the count in uio_resid.
548: */
1.3 andrew 549: int
1.1 cgd 550: soreceive(so, paddr, uio, mp0, controlp, flagsp)
551: register struct socket *so;
552: struct mbuf **paddr;
553: struct uio *uio;
554: struct mbuf **mp0;
555: struct mbuf **controlp;
556: int *flagsp;
557: {
558: register struct mbuf *m, **mp;
559: register int flags, len, error, s, offset;
560: struct protosw *pr = so->so_proto;
561: struct mbuf *nextrecord;
1.21 christos 562: int moff, type = 0;
1.3 andrew 563: int orig_resid = uio->uio_resid;
1.1 cgd 564:
565: mp = mp0;
566: if (paddr)
567: *paddr = 0;
568: if (controlp)
569: *controlp = 0;
570: if (flagsp)
571: flags = *flagsp &~ MSG_EOR;
572: else
573: flags = 0;
574: if (flags & MSG_OOB) {
575: m = m_get(M_WAIT, MT_DATA);
1.17 cgd 576: error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,
1.22 mycroft 577: (struct mbuf *)(long)(flags & MSG_PEEK), (struct mbuf *)0,
578: (struct proc *)0);
1.1 cgd 579: if (error)
580: goto bad;
581: do {
582: error = uiomove(mtod(m, caddr_t),
583: (int) min(uio->uio_resid, m->m_len), uio);
584: m = m_free(m);
585: } while (uio->uio_resid && error == 0 && m);
586: bad:
587: if (m)
588: m_freem(m);
589: return (error);
590: }
591: if (mp)
592: *mp = (struct mbuf *)0;
593: if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
1.22 mycroft 594: (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
595: (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
1.1 cgd 596:
597: restart:
1.21 christos 598: if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0)
1.1 cgd 599: return (error);
1.20 mycroft 600: s = splsoftnet();
1.1 cgd 601:
602: m = so->so_rcv.sb_mb;
603: /*
604: * If we have less data than requested, block awaiting more
605: * (subject to any timeout) if:
1.15 mycroft 606: * 1. the current count is less than the low water mark,
1.1 cgd 607: * 2. MSG_WAITALL is set, and it is possible to do the entire
1.15 mycroft 608: * receive operation at once if we block (resid <= hiwat), or
609: * 3. MSG_DONTWAIT is not set.
1.1 cgd 610: * If MSG_WAITALL is set but resid is larger than the receive buffer,
611: * we have to do the receive in sections, and thus risk returning
612: * a short count if a timeout or signal occurs after we start.
613: */
1.21 christos 614: if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
1.15 mycroft 615: so->so_rcv.sb_cc < uio->uio_resid) &&
1.1 cgd 616: (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
617: ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
1.21 christos 618: m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
1.1 cgd 619: #ifdef DIAGNOSTIC
620: if (m == 0 && so->so_rcv.sb_cc)
621: panic("receive 1");
622: #endif
623: if (so->so_error) {
624: if (m)
1.15 mycroft 625: goto dontblock;
1.1 cgd 626: error = so->so_error;
627: if ((flags & MSG_PEEK) == 0)
628: so->so_error = 0;
629: goto release;
630: }
631: if (so->so_state & SS_CANTRCVMORE) {
632: if (m)
1.15 mycroft 633: goto dontblock;
1.1 cgd 634: else
635: goto release;
636: }
637: for (; m; m = m->m_next)
638: if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
639: m = so->so_rcv.sb_mb;
640: goto dontblock;
641: }
642: if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
643: (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
644: error = ENOTCONN;
645: goto release;
646: }
647: if (uio->uio_resid == 0)
648: goto release;
1.15 mycroft 649: if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
1.1 cgd 650: error = EWOULDBLOCK;
651: goto release;
652: }
653: sbunlock(&so->so_rcv);
654: error = sbwait(&so->so_rcv);
655: splx(s);
656: if (error)
657: return (error);
658: goto restart;
659: }
660: dontblock:
1.15 mycroft 661: #ifdef notyet /* XXXX */
662: if (uio->uio_procp)
663: uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
664: #endif
1.1 cgd 665: nextrecord = m->m_nextpkt;
666: if (pr->pr_flags & PR_ADDR) {
667: #ifdef DIAGNOSTIC
668: if (m->m_type != MT_SONAME)
669: panic("receive 1a");
670: #endif
1.3 andrew 671: orig_resid = 0;
1.1 cgd 672: if (flags & MSG_PEEK) {
673: if (paddr)
674: *paddr = m_copy(m, 0, m->m_len);
675: m = m->m_next;
676: } else {
677: sbfree(&so->so_rcv, m);
678: if (paddr) {
679: *paddr = m;
680: so->so_rcv.sb_mb = m->m_next;
681: m->m_next = 0;
682: m = so->so_rcv.sb_mb;
683: } else {
684: MFREE(m, so->so_rcv.sb_mb);
685: m = so->so_rcv.sb_mb;
686: }
687: }
688: }
689: while (m && m->m_type == MT_CONTROL && error == 0) {
690: if (flags & MSG_PEEK) {
691: if (controlp)
692: *controlp = m_copy(m, 0, m->m_len);
693: m = m->m_next;
694: } else {
695: sbfree(&so->so_rcv, m);
696: if (controlp) {
697: if (pr->pr_domain->dom_externalize &&
698: mtod(m, struct cmsghdr *)->cmsg_type ==
699: SCM_RIGHTS)
1.45 ! tv 700: error = (*pr->pr_domain->dom_externalize)(m);
1.1 cgd 701: *controlp = m;
702: so->so_rcv.sb_mb = m->m_next;
703: m->m_next = 0;
704: m = so->so_rcv.sb_mb;
705: } else {
706: MFREE(m, so->so_rcv.sb_mb);
707: m = so->so_rcv.sb_mb;
708: }
709: }
1.3 andrew 710: if (controlp) {
711: orig_resid = 0;
1.1 cgd 712: controlp = &(*controlp)->m_next;
1.3 andrew 713: }
1.1 cgd 714: }
715: if (m) {
716: if ((flags & MSG_PEEK) == 0)
717: m->m_nextpkt = nextrecord;
718: type = m->m_type;
719: if (type == MT_OOBDATA)
720: flags |= MSG_OOB;
721: }
722: moff = 0;
723: offset = 0;
724: while (m && uio->uio_resid > 0 && error == 0) {
725: if (m->m_type == MT_OOBDATA) {
726: if (type != MT_OOBDATA)
727: break;
728: } else if (type == MT_OOBDATA)
729: break;
730: #ifdef DIAGNOSTIC
731: else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
732: panic("receive 3");
733: #endif
734: so->so_state &= ~SS_RCVATMARK;
735: len = uio->uio_resid;
736: if (so->so_oobmark && len > so->so_oobmark - offset)
737: len = so->so_oobmark - offset;
738: if (len > m->m_len - moff)
739: len = m->m_len - moff;
740: /*
741: * If mp is set, just pass back the mbufs.
742: * Otherwise copy them out via the uio, then free.
743: * Sockbuf must be consistent here (points to current mbuf,
744: * it points to next record) when we drop priority;
745: * we must note any additions to the sockbuf when we
746: * block interrupts again.
747: */
748: if (mp == 0) {
749: splx(s);
750: error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
1.20 mycroft 751: s = splsoftnet();
1.1 cgd 752: } else
753: uio->uio_resid -= len;
754: if (len == m->m_len - moff) {
755: if (m->m_flags & M_EOR)
756: flags |= MSG_EOR;
757: if (flags & MSG_PEEK) {
758: m = m->m_next;
759: moff = 0;
760: } else {
761: nextrecord = m->m_nextpkt;
762: sbfree(&so->so_rcv, m);
763: if (mp) {
764: *mp = m;
765: mp = &m->m_next;
766: so->so_rcv.sb_mb = m = m->m_next;
767: *mp = (struct mbuf *)0;
768: } else {
769: MFREE(m, so->so_rcv.sb_mb);
770: m = so->so_rcv.sb_mb;
771: }
772: if (m)
773: m->m_nextpkt = nextrecord;
774: }
775: } else {
776: if (flags & MSG_PEEK)
777: moff += len;
778: else {
779: if (mp)
780: *mp = m_copym(m, 0, len, M_WAIT);
781: m->m_data += len;
782: m->m_len -= len;
783: so->so_rcv.sb_cc -= len;
784: }
785: }
786: if (so->so_oobmark) {
787: if ((flags & MSG_PEEK) == 0) {
788: so->so_oobmark -= len;
789: if (so->so_oobmark == 0) {
790: so->so_state |= SS_RCVATMARK;
791: break;
792: }
1.7 cgd 793: } else {
1.1 cgd 794: offset += len;
1.7 cgd 795: if (offset == so->so_oobmark)
796: break;
797: }
1.1 cgd 798: }
799: if (flags & MSG_EOR)
800: break;
801: /*
802: * If the MSG_WAITALL flag is set (for non-atomic socket),
803: * we must not quit until "uio->uio_resid == 0" or an error
804: * termination. If a signal/timeout occurs, return
805: * with a short count but without error.
806: * Keep sockbuf locked against other readers.
807: */
808: while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
1.3 andrew 809: !sosendallatonce(so) && !nextrecord) {
1.1 cgd 810: if (so->so_error || so->so_state & SS_CANTRCVMORE)
811: break;
812: error = sbwait(&so->so_rcv);
813: if (error) {
814: sbunlock(&so->so_rcv);
815: splx(s);
816: return (0);
817: }
1.21 christos 818: if ((m = so->so_rcv.sb_mb) != NULL)
1.1 cgd 819: nextrecord = m->m_nextpkt;
820: }
821: }
1.3 andrew 822:
823: if (m && pr->pr_flags & PR_ATOMIC) {
824: flags |= MSG_TRUNC;
825: if ((flags & MSG_PEEK) == 0)
826: (void) sbdroprecord(&so->so_rcv);
827: }
1.1 cgd 828: if ((flags & MSG_PEEK) == 0) {
829: if (m == 0)
830: so->so_rcv.sb_mb = nextrecord;
831: if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1.22 mycroft 832: (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
833: (struct mbuf *)(long)flags, (struct mbuf *)0,
834: (struct proc *)0);
1.1 cgd 835: }
1.3 andrew 836: if (orig_resid == uio->uio_resid && orig_resid &&
837: (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
838: sbunlock(&so->so_rcv);
839: splx(s);
840: goto restart;
841: }
842:
1.1 cgd 843: if (flagsp)
844: *flagsp |= flags;
845: release:
846: sbunlock(&so->so_rcv);
847: splx(s);
848: return (error);
849: }
850:
1.14 mycroft 851: int
1.1 cgd 852: soshutdown(so, how)
1.34 kleink 853: struct socket *so;
854: int how;
1.1 cgd 855: {
1.34 kleink 856: struct protosw *pr = so->so_proto;
857:
858: if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
859: return (EINVAL);
1.1 cgd 860:
1.34 kleink 861: if (how == SHUT_RD || how == SHUT_RDWR)
1.1 cgd 862: sorflush(so);
1.34 kleink 863: if (how == SHUT_WR || how == SHUT_RDWR)
1.22 mycroft 864: return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, (struct mbuf *)0,
865: (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
1.1 cgd 866: return (0);
867: }
868:
1.14 mycroft 869: void
1.1 cgd 870: sorflush(so)
871: register struct socket *so;
872: {
873: register struct sockbuf *sb = &so->so_rcv;
874: register struct protosw *pr = so->so_proto;
875: register int s;
876: struct sockbuf asb;
877:
878: sb->sb_flags |= SB_NOINTR;
1.15 mycroft 879: (void) sblock(sb, M_WAITOK);
1.1 cgd 880: s = splimp();
881: socantrcvmore(so);
882: sbunlock(sb);
883: asb = *sb;
1.38 perry 884: memset((caddr_t)sb, 0, sizeof(*sb));
1.1 cgd 885: splx(s);
886: if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
887: (*pr->pr_domain->dom_dispose)(asb.sb_mb);
888: sbrelease(&asb);
889: }
890:
1.14 mycroft 891: int
1.1 cgd 892: sosetopt(so, level, optname, m0)
893: register struct socket *so;
894: int level, optname;
895: struct mbuf *m0;
896: {
897: int error = 0;
898: register struct mbuf *m = m0;
899:
900: if (level != SOL_SOCKET) {
901: if (so->so_proto && so->so_proto->pr_ctloutput)
902: return ((*so->so_proto->pr_ctloutput)
903: (PRCO_SETOPT, so, level, optname, &m0));
904: error = ENOPROTOOPT;
905: } else {
906: switch (optname) {
907:
908: case SO_LINGER:
1.36 perry 909: if (m == NULL || m->m_len != sizeof(struct linger)) {
1.1 cgd 910: error = EINVAL;
911: goto bad;
912: }
913: so->so_linger = mtod(m, struct linger *)->l_linger;
914: /* fall thru... */
915:
916: case SO_DEBUG:
917: case SO_KEEPALIVE:
918: case SO_DONTROUTE:
919: case SO_USELOOPBACK:
920: case SO_BROADCAST:
921: case SO_REUSEADDR:
1.15 mycroft 922: case SO_REUSEPORT:
1.1 cgd 923: case SO_OOBINLINE:
1.26 thorpej 924: case SO_TIMESTAMP:
1.36 perry 925: if (m == NULL || m->m_len < sizeof(int)) {
1.1 cgd 926: error = EINVAL;
927: goto bad;
928: }
929: if (*mtod(m, int *))
930: so->so_options |= optname;
931: else
932: so->so_options &= ~optname;
933: break;
934:
935: case SO_SNDBUF:
936: case SO_RCVBUF:
937: case SO_SNDLOWAT:
938: case SO_RCVLOWAT:
1.28 thorpej 939: {
940: int optval;
941:
1.36 perry 942: if (m == NULL || m->m_len < sizeof(int)) {
1.1 cgd 943: error = EINVAL;
944: goto bad;
945: }
1.28 thorpej 946:
947: /*
948: * Values < 1 make no sense for any of these
949: * options, so disallow them.
950: */
951: optval = *mtod(m, int *);
952: if (optval < 1) {
953: error = EINVAL;
954: goto bad;
955: }
956:
1.1 cgd 957: switch (optname) {
958:
959: case SO_SNDBUF:
960: case SO_RCVBUF:
961: if (sbreserve(optname == SO_SNDBUF ?
962: &so->so_snd : &so->so_rcv,
1.28 thorpej 963: (u_long) optval) == 0) {
1.1 cgd 964: error = ENOBUFS;
965: goto bad;
966: }
967: break;
968:
1.28 thorpej 969: /*
970: * Make sure the low-water is never greater than
971: * the high-water.
972: */
1.1 cgd 973: case SO_SNDLOWAT:
1.28 thorpej 974: so->so_snd.sb_lowat =
975: (optval > so->so_snd.sb_hiwat) ?
976: so->so_snd.sb_hiwat : optval;
1.1 cgd 977: break;
978: case SO_RCVLOWAT:
1.28 thorpej 979: so->so_rcv.sb_lowat =
980: (optval > so->so_rcv.sb_hiwat) ?
981: so->so_rcv.sb_hiwat : optval;
1.1 cgd 982: break;
983: }
984: break;
1.28 thorpej 985: }
1.1 cgd 986:
987: case SO_SNDTIMEO:
988: case SO_RCVTIMEO:
989: {
990: struct timeval *tv;
991: short val;
992:
1.36 perry 993: if (m == NULL || m->m_len < sizeof(*tv)) {
1.1 cgd 994: error = EINVAL;
995: goto bad;
996: }
997: tv = mtod(m, struct timeval *);
1.19 cgd 998: if (tv->tv_sec * hz + tv->tv_usec / tick > SHRT_MAX) {
1.1 cgd 999: error = EDOM;
1000: goto bad;
1001: }
1002: val = tv->tv_sec * hz + tv->tv_usec / tick;
1003:
1004: switch (optname) {
1005:
1006: case SO_SNDTIMEO:
1007: so->so_snd.sb_timeo = val;
1008: break;
1009: case SO_RCVTIMEO:
1010: so->so_rcv.sb_timeo = val;
1011: break;
1012: }
1013: break;
1014: }
1015:
1016: default:
1017: error = ENOPROTOOPT;
1018: break;
1019: }
1.15 mycroft 1020: if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1021: (void) ((*so->so_proto->pr_ctloutput)
1022: (PRCO_SETOPT, so, level, optname, &m0));
1023: m = NULL; /* freed by protocol */
1024: }
1.1 cgd 1025: }
1026: bad:
1027: if (m)
1028: (void) m_free(m);
1029: return (error);
1030: }
1031:
1.14 mycroft 1032: int
1.1 cgd 1033: sogetopt(so, level, optname, mp)
1034: register struct socket *so;
1035: int level, optname;
1036: struct mbuf **mp;
1037: {
1038: register struct mbuf *m;
1039:
1040: if (level != SOL_SOCKET) {
1041: if (so->so_proto && so->so_proto->pr_ctloutput) {
1042: return ((*so->so_proto->pr_ctloutput)
1043: (PRCO_GETOPT, so, level, optname, mp));
1044: } else
1045: return (ENOPROTOOPT);
1046: } else {
1047: m = m_get(M_WAIT, MT_SOOPTS);
1.36 perry 1048: m->m_len = sizeof(int);
1.1 cgd 1049:
1050: switch (optname) {
1051:
1052: case SO_LINGER:
1.36 perry 1053: m->m_len = sizeof(struct linger);
1.1 cgd 1054: mtod(m, struct linger *)->l_onoff =
1055: so->so_options & SO_LINGER;
1056: mtod(m, struct linger *)->l_linger = so->so_linger;
1057: break;
1058:
1059: case SO_USELOOPBACK:
1060: case SO_DONTROUTE:
1061: case SO_DEBUG:
1062: case SO_KEEPALIVE:
1063: case SO_REUSEADDR:
1.15 mycroft 1064: case SO_REUSEPORT:
1.1 cgd 1065: case SO_BROADCAST:
1066: case SO_OOBINLINE:
1.26 thorpej 1067: case SO_TIMESTAMP:
1.1 cgd 1068: *mtod(m, int *) = so->so_options & optname;
1069: break;
1070:
1071: case SO_TYPE:
1072: *mtod(m, int *) = so->so_type;
1073: break;
1074:
1075: case SO_ERROR:
1076: *mtod(m, int *) = so->so_error;
1077: so->so_error = 0;
1078: break;
1079:
1080: case SO_SNDBUF:
1081: *mtod(m, int *) = so->so_snd.sb_hiwat;
1082: break;
1083:
1084: case SO_RCVBUF:
1085: *mtod(m, int *) = so->so_rcv.sb_hiwat;
1086: break;
1087:
1088: case SO_SNDLOWAT:
1089: *mtod(m, int *) = so->so_snd.sb_lowat;
1090: break;
1091:
1092: case SO_RCVLOWAT:
1093: *mtod(m, int *) = so->so_rcv.sb_lowat;
1094: break;
1095:
1096: case SO_SNDTIMEO:
1097: case SO_RCVTIMEO:
1098: {
1099: int val = (optname == SO_SNDTIMEO ?
1100: so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1101:
1102: m->m_len = sizeof(struct timeval);
1103: mtod(m, struct timeval *)->tv_sec = val / hz;
1104: mtod(m, struct timeval *)->tv_usec =
1.27 kleink 1105: (val % hz) * tick;
1.1 cgd 1106: break;
1107: }
1108:
1109: default:
1110: (void)m_free(m);
1111: return (ENOPROTOOPT);
1112: }
1113: *mp = m;
1114: return (0);
1115: }
1116: }
1117:
1.14 mycroft 1118: void
1.1 cgd 1119: sohasoutofband(so)
1120: register struct socket *so;
1121: {
1122: struct proc *p;
1123:
1124: if (so->so_pgid < 0)
1125: gsignal(-so->so_pgid, SIGURG);
1126: else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
1127: psignal(p, SIGURG);
1.2 cgd 1128: selwakeup(&so->so_rcv.sb_sel);
1.1 cgd 1129: }