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