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