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