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