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