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