source: rtems/cpukit/libnetworking/kern/uipc_socket.c @ 79e69da6

4.104.114.84.95
Last change on this file since 79e69da6 was 79e69da6, checked in by Ralf Corsepius <ralf.corsepius@…>, on 04/22/04 at 03:27:13

2004-04-22 Ralf Corsepius <ralf_corsepius@…>

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