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