|
|
1.46 ! sommerfe 1: /* $NetBSD: uipc_socket.c,v 1.45 1999/05/15 16:42:48 tv 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);
1.46 ! sommerfe 495:
! 496: #ifdef TEST_FOR_PANIC_UIPC_3_RACE_CONDITION
! 497: {
! 498: extern struct domain unixdomain;
! 499:
! 500: if (so->so_proto->pr_domain == &unixdomain)
! 501: sleep(&lbolt, PVFS);
! 502: }
! 503: #endif
! 504:
! 505: s = splsoftnet();
! 506:
! 507: if (so->so_state & SS_CANTSENDMORE)
! 508: snderr(EPIPE);
1.45 tv 509:
510: if (dontroute)
511: so->so_options |= SO_DONTROUTE;
512: if (resid > 0)
513: so->so_state |= SS_MORETOCOME;
1.46 ! sommerfe 514: error = (*so->so_proto->pr_usrreq)(so,
! 515: (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
! 516: top, addr, control, p);
1.45 tv 517: if (dontroute)
518: so->so_options &= ~SO_DONTROUTE;
519: if (resid > 0)
520: so->so_state &= ~SS_MORETOCOME;
1.46 ! sommerfe 521: splx(s);
! 522:
1.45 tv 523: clen = 0;
524: control = 0;
525: top = 0;
526: mp = ⊤
1.1 cgd 527: if (error)
528: goto release;
529: } while (resid && space > 0);
530: } while (resid);
531:
532: release:
533: sbunlock(&so->so_snd);
534: out:
535: if (top)
536: m_freem(top);
537: if (control)
538: m_freem(control);
539: return (error);
540: }
541:
542: /*
543: * Implement receive operations on a socket.
544: * We depend on the way that records are added to the sockbuf
545: * by sbappend*. In particular, each record (mbufs linked through m_next)
546: * must begin with an address if the protocol so specifies,
547: * followed by an optional mbuf or mbufs containing ancillary data,
548: * and then zero or more mbufs of data.
549: * In order to avoid blocking network interrupts for the entire time here,
550: * we splx() while doing the actual copy to user space.
551: * Although the sockbuf is locked, new data may still be appended,
552: * and thus we must maintain consistency of the sockbuf during that time.
553: *
554: * The caller may receive the data as a single mbuf chain by supplying
555: * an mbuf **mp0 for use in returning the chain. The uio is then used
556: * only for the count in uio_resid.
557: */
1.3 andrew 558: int
1.1 cgd 559: soreceive(so, paddr, uio, mp0, controlp, flagsp)
560: register struct socket *so;
561: struct mbuf **paddr;
562: struct uio *uio;
563: struct mbuf **mp0;
564: struct mbuf **controlp;
565: int *flagsp;
566: {
567: register struct mbuf *m, **mp;
568: register int flags, len, error, s, offset;
569: struct protosw *pr = so->so_proto;
570: struct mbuf *nextrecord;
1.21 christos 571: int moff, type = 0;
1.3 andrew 572: int orig_resid = uio->uio_resid;
1.1 cgd 573:
574: mp = mp0;
575: if (paddr)
576: *paddr = 0;
577: if (controlp)
578: *controlp = 0;
579: if (flagsp)
580: flags = *flagsp &~ MSG_EOR;
581: else
582: flags = 0;
583: if (flags & MSG_OOB) {
584: m = m_get(M_WAIT, MT_DATA);
1.17 cgd 585: error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,
1.22 mycroft 586: (struct mbuf *)(long)(flags & MSG_PEEK), (struct mbuf *)0,
587: (struct proc *)0);
1.1 cgd 588: if (error)
589: goto bad;
590: do {
591: error = uiomove(mtod(m, caddr_t),
592: (int) min(uio->uio_resid, m->m_len), uio);
593: m = m_free(m);
594: } while (uio->uio_resid && error == 0 && m);
595: bad:
596: if (m)
597: m_freem(m);
598: return (error);
599: }
600: if (mp)
601: *mp = (struct mbuf *)0;
602: if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
1.22 mycroft 603: (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
604: (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
1.1 cgd 605:
606: restart:
1.21 christos 607: if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0)
1.1 cgd 608: return (error);
1.20 mycroft 609: s = splsoftnet();
1.1 cgd 610:
611: m = so->so_rcv.sb_mb;
612: /*
613: * If we have less data than requested, block awaiting more
614: * (subject to any timeout) if:
1.15 mycroft 615: * 1. the current count is less than the low water mark,
1.1 cgd 616: * 2. MSG_WAITALL is set, and it is possible to do the entire
1.15 mycroft 617: * receive operation at once if we block (resid <= hiwat), or
618: * 3. MSG_DONTWAIT is not set.
1.1 cgd 619: * If MSG_WAITALL is set but resid is larger than the receive buffer,
620: * we have to do the receive in sections, and thus risk returning
621: * a short count if a timeout or signal occurs after we start.
622: */
1.21 christos 623: if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
1.15 mycroft 624: so->so_rcv.sb_cc < uio->uio_resid) &&
1.1 cgd 625: (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
626: ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
1.21 christos 627: m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
1.1 cgd 628: #ifdef DIAGNOSTIC
629: if (m == 0 && so->so_rcv.sb_cc)
630: panic("receive 1");
631: #endif
632: if (so->so_error) {
633: if (m)
1.15 mycroft 634: goto dontblock;
1.1 cgd 635: error = so->so_error;
636: if ((flags & MSG_PEEK) == 0)
637: so->so_error = 0;
638: goto release;
639: }
640: if (so->so_state & SS_CANTRCVMORE) {
641: if (m)
1.15 mycroft 642: goto dontblock;
1.1 cgd 643: else
644: goto release;
645: }
646: for (; m; m = m->m_next)
647: if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
648: m = so->so_rcv.sb_mb;
649: goto dontblock;
650: }
651: if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
652: (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
653: error = ENOTCONN;
654: goto release;
655: }
656: if (uio->uio_resid == 0)
657: goto release;
1.15 mycroft 658: if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
1.1 cgd 659: error = EWOULDBLOCK;
660: goto release;
661: }
662: sbunlock(&so->so_rcv);
663: error = sbwait(&so->so_rcv);
664: splx(s);
665: if (error)
666: return (error);
667: goto restart;
668: }
669: dontblock:
1.15 mycroft 670: #ifdef notyet /* XXXX */
671: if (uio->uio_procp)
672: uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
673: #endif
1.1 cgd 674: nextrecord = m->m_nextpkt;
675: if (pr->pr_flags & PR_ADDR) {
676: #ifdef DIAGNOSTIC
677: if (m->m_type != MT_SONAME)
678: panic("receive 1a");
679: #endif
1.3 andrew 680: orig_resid = 0;
1.1 cgd 681: if (flags & MSG_PEEK) {
682: if (paddr)
683: *paddr = m_copy(m, 0, m->m_len);
684: m = m->m_next;
685: } else {
686: sbfree(&so->so_rcv, m);
687: if (paddr) {
688: *paddr = m;
689: so->so_rcv.sb_mb = m->m_next;
690: m->m_next = 0;
691: m = so->so_rcv.sb_mb;
692: } else {
693: MFREE(m, so->so_rcv.sb_mb);
694: m = so->so_rcv.sb_mb;
695: }
696: }
697: }
698: while (m && m->m_type == MT_CONTROL && error == 0) {
699: if (flags & MSG_PEEK) {
700: if (controlp)
701: *controlp = m_copy(m, 0, m->m_len);
702: m = m->m_next;
703: } else {
704: sbfree(&so->so_rcv, m);
705: if (controlp) {
706: if (pr->pr_domain->dom_externalize &&
707: mtod(m, struct cmsghdr *)->cmsg_type ==
708: SCM_RIGHTS)
1.45 tv 709: error = (*pr->pr_domain->dom_externalize)(m);
1.1 cgd 710: *controlp = m;
711: so->so_rcv.sb_mb = m->m_next;
712: m->m_next = 0;
713: m = so->so_rcv.sb_mb;
714: } else {
715: MFREE(m, so->so_rcv.sb_mb);
716: m = so->so_rcv.sb_mb;
717: }
718: }
1.3 andrew 719: if (controlp) {
720: orig_resid = 0;
1.1 cgd 721: controlp = &(*controlp)->m_next;
1.3 andrew 722: }
1.1 cgd 723: }
724: if (m) {
725: if ((flags & MSG_PEEK) == 0)
726: m->m_nextpkt = nextrecord;
727: type = m->m_type;
728: if (type == MT_OOBDATA)
729: flags |= MSG_OOB;
730: }
731: moff = 0;
732: offset = 0;
733: while (m && uio->uio_resid > 0 && error == 0) {
734: if (m->m_type == MT_OOBDATA) {
735: if (type != MT_OOBDATA)
736: break;
737: } else if (type == MT_OOBDATA)
738: break;
739: #ifdef DIAGNOSTIC
740: else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
741: panic("receive 3");
742: #endif
743: so->so_state &= ~SS_RCVATMARK;
744: len = uio->uio_resid;
745: if (so->so_oobmark && len > so->so_oobmark - offset)
746: len = so->so_oobmark - offset;
747: if (len > m->m_len - moff)
748: len = m->m_len - moff;
749: /*
750: * If mp is set, just pass back the mbufs.
751: * Otherwise copy them out via the uio, then free.
752: * Sockbuf must be consistent here (points to current mbuf,
753: * it points to next record) when we drop priority;
754: * we must note any additions to the sockbuf when we
755: * block interrupts again.
756: */
757: if (mp == 0) {
758: splx(s);
759: error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
1.20 mycroft 760: s = splsoftnet();
1.1 cgd 761: } else
762: uio->uio_resid -= len;
763: if (len == m->m_len - moff) {
764: if (m->m_flags & M_EOR)
765: flags |= MSG_EOR;
766: if (flags & MSG_PEEK) {
767: m = m->m_next;
768: moff = 0;
769: } else {
770: nextrecord = m->m_nextpkt;
771: sbfree(&so->so_rcv, m);
772: if (mp) {
773: *mp = m;
774: mp = &m->m_next;
775: so->so_rcv.sb_mb = m = m->m_next;
776: *mp = (struct mbuf *)0;
777: } else {
778: MFREE(m, so->so_rcv.sb_mb);
779: m = so->so_rcv.sb_mb;
780: }
781: if (m)
782: m->m_nextpkt = nextrecord;
783: }
784: } else {
785: if (flags & MSG_PEEK)
786: moff += len;
787: else {
788: if (mp)
789: *mp = m_copym(m, 0, len, M_WAIT);
790: m->m_data += len;
791: m->m_len -= len;
792: so->so_rcv.sb_cc -= len;
793: }
794: }
795: if (so->so_oobmark) {
796: if ((flags & MSG_PEEK) == 0) {
797: so->so_oobmark -= len;
798: if (so->so_oobmark == 0) {
799: so->so_state |= SS_RCVATMARK;
800: break;
801: }
1.7 cgd 802: } else {
1.1 cgd 803: offset += len;
1.7 cgd 804: if (offset == so->so_oobmark)
805: break;
806: }
1.1 cgd 807: }
808: if (flags & MSG_EOR)
809: break;
810: /*
811: * If the MSG_WAITALL flag is set (for non-atomic socket),
812: * we must not quit until "uio->uio_resid == 0" or an error
813: * termination. If a signal/timeout occurs, return
814: * with a short count but without error.
815: * Keep sockbuf locked against other readers.
816: */
817: while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
1.3 andrew 818: !sosendallatonce(so) && !nextrecord) {
1.1 cgd 819: if (so->so_error || so->so_state & SS_CANTRCVMORE)
820: break;
821: error = sbwait(&so->so_rcv);
822: if (error) {
823: sbunlock(&so->so_rcv);
824: splx(s);
825: return (0);
826: }
1.21 christos 827: if ((m = so->so_rcv.sb_mb) != NULL)
1.1 cgd 828: nextrecord = m->m_nextpkt;
829: }
830: }
1.3 andrew 831:
832: if (m && pr->pr_flags & PR_ATOMIC) {
833: flags |= MSG_TRUNC;
834: if ((flags & MSG_PEEK) == 0)
835: (void) sbdroprecord(&so->so_rcv);
836: }
1.1 cgd 837: if ((flags & MSG_PEEK) == 0) {
838: if (m == 0)
839: so->so_rcv.sb_mb = nextrecord;
840: if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1.22 mycroft 841: (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
842: (struct mbuf *)(long)flags, (struct mbuf *)0,
843: (struct proc *)0);
1.1 cgd 844: }
1.3 andrew 845: if (orig_resid == uio->uio_resid && orig_resid &&
846: (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
847: sbunlock(&so->so_rcv);
848: splx(s);
849: goto restart;
850: }
851:
1.1 cgd 852: if (flagsp)
853: *flagsp |= flags;
854: release:
855: sbunlock(&so->so_rcv);
856: splx(s);
857: return (error);
858: }
859:
1.14 mycroft 860: int
1.1 cgd 861: soshutdown(so, how)
1.34 kleink 862: struct socket *so;
863: int how;
1.1 cgd 864: {
1.34 kleink 865: struct protosw *pr = so->so_proto;
866:
867: if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
868: return (EINVAL);
1.1 cgd 869:
1.34 kleink 870: if (how == SHUT_RD || how == SHUT_RDWR)
1.1 cgd 871: sorflush(so);
1.34 kleink 872: if (how == SHUT_WR || how == SHUT_RDWR)
1.22 mycroft 873: return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, (struct mbuf *)0,
874: (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
1.1 cgd 875: return (0);
876: }
877:
1.14 mycroft 878: void
1.1 cgd 879: sorflush(so)
880: register struct socket *so;
881: {
882: register struct sockbuf *sb = &so->so_rcv;
883: register struct protosw *pr = so->so_proto;
884: register int s;
885: struct sockbuf asb;
886:
887: sb->sb_flags |= SB_NOINTR;
1.15 mycroft 888: (void) sblock(sb, M_WAITOK);
1.1 cgd 889: s = splimp();
890: socantrcvmore(so);
891: sbunlock(sb);
892: asb = *sb;
1.38 perry 893: memset((caddr_t)sb, 0, sizeof(*sb));
1.1 cgd 894: splx(s);
895: if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
896: (*pr->pr_domain->dom_dispose)(asb.sb_mb);
897: sbrelease(&asb);
898: }
899:
1.14 mycroft 900: int
1.1 cgd 901: sosetopt(so, level, optname, m0)
902: register struct socket *so;
903: int level, optname;
904: struct mbuf *m0;
905: {
906: int error = 0;
907: register struct mbuf *m = m0;
908:
909: if (level != SOL_SOCKET) {
910: if (so->so_proto && so->so_proto->pr_ctloutput)
911: return ((*so->so_proto->pr_ctloutput)
912: (PRCO_SETOPT, so, level, optname, &m0));
913: error = ENOPROTOOPT;
914: } else {
915: switch (optname) {
916:
917: case SO_LINGER:
1.36 perry 918: if (m == NULL || m->m_len != sizeof(struct linger)) {
1.1 cgd 919: error = EINVAL;
920: goto bad;
921: }
922: so->so_linger = mtod(m, struct linger *)->l_linger;
923: /* fall thru... */
924:
925: case SO_DEBUG:
926: case SO_KEEPALIVE:
927: case SO_DONTROUTE:
928: case SO_USELOOPBACK:
929: case SO_BROADCAST:
930: case SO_REUSEADDR:
1.15 mycroft 931: case SO_REUSEPORT:
1.1 cgd 932: case SO_OOBINLINE:
1.26 thorpej 933: case SO_TIMESTAMP:
1.36 perry 934: if (m == NULL || m->m_len < sizeof(int)) {
1.1 cgd 935: error = EINVAL;
936: goto bad;
937: }
938: if (*mtod(m, int *))
939: so->so_options |= optname;
940: else
941: so->so_options &= ~optname;
942: break;
943:
944: case SO_SNDBUF:
945: case SO_RCVBUF:
946: case SO_SNDLOWAT:
947: case SO_RCVLOWAT:
1.28 thorpej 948: {
949: int optval;
950:
1.36 perry 951: if (m == NULL || m->m_len < sizeof(int)) {
1.1 cgd 952: error = EINVAL;
953: goto bad;
954: }
1.28 thorpej 955:
956: /*
957: * Values < 1 make no sense for any of these
958: * options, so disallow them.
959: */
960: optval = *mtod(m, int *);
961: if (optval < 1) {
962: error = EINVAL;
963: goto bad;
964: }
965:
1.1 cgd 966: switch (optname) {
967:
968: case SO_SNDBUF:
969: case SO_RCVBUF:
970: if (sbreserve(optname == SO_SNDBUF ?
971: &so->so_snd : &so->so_rcv,
1.28 thorpej 972: (u_long) optval) == 0) {
1.1 cgd 973: error = ENOBUFS;
974: goto bad;
975: }
976: break;
977:
1.28 thorpej 978: /*
979: * Make sure the low-water is never greater than
980: * the high-water.
981: */
1.1 cgd 982: case SO_SNDLOWAT:
1.28 thorpej 983: so->so_snd.sb_lowat =
984: (optval > so->so_snd.sb_hiwat) ?
985: so->so_snd.sb_hiwat : optval;
1.1 cgd 986: break;
987: case SO_RCVLOWAT:
1.28 thorpej 988: so->so_rcv.sb_lowat =
989: (optval > so->so_rcv.sb_hiwat) ?
990: so->so_rcv.sb_hiwat : optval;
1.1 cgd 991: break;
992: }
993: break;
1.28 thorpej 994: }
1.1 cgd 995:
996: case SO_SNDTIMEO:
997: case SO_RCVTIMEO:
998: {
999: struct timeval *tv;
1000: short val;
1001:
1.36 perry 1002: if (m == NULL || m->m_len < sizeof(*tv)) {
1.1 cgd 1003: error = EINVAL;
1004: goto bad;
1005: }
1006: tv = mtod(m, struct timeval *);
1.19 cgd 1007: if (tv->tv_sec * hz + tv->tv_usec / tick > SHRT_MAX) {
1.1 cgd 1008: error = EDOM;
1009: goto bad;
1010: }
1011: val = tv->tv_sec * hz + tv->tv_usec / tick;
1012:
1013: switch (optname) {
1014:
1015: case SO_SNDTIMEO:
1016: so->so_snd.sb_timeo = val;
1017: break;
1018: case SO_RCVTIMEO:
1019: so->so_rcv.sb_timeo = val;
1020: break;
1021: }
1022: break;
1023: }
1024:
1025: default:
1026: error = ENOPROTOOPT;
1027: break;
1028: }
1.15 mycroft 1029: if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1030: (void) ((*so->so_proto->pr_ctloutput)
1031: (PRCO_SETOPT, so, level, optname, &m0));
1032: m = NULL; /* freed by protocol */
1033: }
1.1 cgd 1034: }
1035: bad:
1036: if (m)
1037: (void) m_free(m);
1038: return (error);
1039: }
1040:
1.14 mycroft 1041: int
1.1 cgd 1042: sogetopt(so, level, optname, mp)
1043: register struct socket *so;
1044: int level, optname;
1045: struct mbuf **mp;
1046: {
1047: register struct mbuf *m;
1048:
1049: if (level != SOL_SOCKET) {
1050: if (so->so_proto && so->so_proto->pr_ctloutput) {
1051: return ((*so->so_proto->pr_ctloutput)
1052: (PRCO_GETOPT, so, level, optname, mp));
1053: } else
1054: return (ENOPROTOOPT);
1055: } else {
1056: m = m_get(M_WAIT, MT_SOOPTS);
1.36 perry 1057: m->m_len = sizeof(int);
1.1 cgd 1058:
1059: switch (optname) {
1060:
1061: case SO_LINGER:
1.36 perry 1062: m->m_len = sizeof(struct linger);
1.1 cgd 1063: mtod(m, struct linger *)->l_onoff =
1064: so->so_options & SO_LINGER;
1065: mtod(m, struct linger *)->l_linger = so->so_linger;
1066: break;
1067:
1068: case SO_USELOOPBACK:
1069: case SO_DONTROUTE:
1070: case SO_DEBUG:
1071: case SO_KEEPALIVE:
1072: case SO_REUSEADDR:
1.15 mycroft 1073: case SO_REUSEPORT:
1.1 cgd 1074: case SO_BROADCAST:
1075: case SO_OOBINLINE:
1.26 thorpej 1076: case SO_TIMESTAMP:
1.1 cgd 1077: *mtod(m, int *) = so->so_options & optname;
1078: break;
1079:
1080: case SO_TYPE:
1081: *mtod(m, int *) = so->so_type;
1082: break;
1083:
1084: case SO_ERROR:
1085: *mtod(m, int *) = so->so_error;
1086: so->so_error = 0;
1087: break;
1088:
1089: case SO_SNDBUF:
1090: *mtod(m, int *) = so->so_snd.sb_hiwat;
1091: break;
1092:
1093: case SO_RCVBUF:
1094: *mtod(m, int *) = so->so_rcv.sb_hiwat;
1095: break;
1096:
1097: case SO_SNDLOWAT:
1098: *mtod(m, int *) = so->so_snd.sb_lowat;
1099: break;
1100:
1101: case SO_RCVLOWAT:
1102: *mtod(m, int *) = so->so_rcv.sb_lowat;
1103: break;
1104:
1105: case SO_SNDTIMEO:
1106: case SO_RCVTIMEO:
1107: {
1108: int val = (optname == SO_SNDTIMEO ?
1109: so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1110:
1111: m->m_len = sizeof(struct timeval);
1112: mtod(m, struct timeval *)->tv_sec = val / hz;
1113: mtod(m, struct timeval *)->tv_usec =
1.27 kleink 1114: (val % hz) * tick;
1.1 cgd 1115: break;
1116: }
1117:
1118: default:
1119: (void)m_free(m);
1120: return (ENOPROTOOPT);
1121: }
1122: *mp = m;
1123: return (0);
1124: }
1125: }
1126:
1.14 mycroft 1127: void
1.1 cgd 1128: sohasoutofband(so)
1129: register struct socket *so;
1130: {
1131: struct proc *p;
1132:
1133: if (so->so_pgid < 0)
1134: gsignal(-so->so_pgid, SIGURG);
1135: else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
1136: psignal(p, SIGURG);
1.2 cgd 1137: selwakeup(&so->so_rcv.sb_sel);
1.1 cgd 1138: }