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