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