Home | History | Annotate | Line # | Download | only in kern
uipc_socket2.c revision 1.91.2.5
      1  1.91.2.5      yamt /*	$NetBSD: uipc_socket2.c,v 1.91.2.5 2010/03/11 15:04:20 yamt Exp $	*/
      2      1.91        ad 
      3      1.91        ad /*-
      4      1.91        ad  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5      1.91        ad  * All rights reserved.
      6      1.91        ad  *
      7      1.91        ad  * Redistribution and use in source and binary forms, with or without
      8      1.91        ad  * modification, are permitted provided that the following conditions
      9      1.91        ad  * are met:
     10      1.91        ad  * 1. Redistributions of source code must retain the above copyright
     11      1.91        ad  *    notice, this list of conditions and the following disclaimer.
     12      1.91        ad  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.91        ad  *    notice, this list of conditions and the following disclaimer in the
     14      1.91        ad  *    documentation and/or other materials provided with the distribution.
     15      1.91        ad  *
     16      1.91        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17      1.91        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18      1.91        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19      1.91        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20      1.91        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21      1.91        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22      1.91        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23      1.91        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24      1.91        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25      1.91        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26      1.91        ad  * POSSIBILITY OF SUCH DAMAGE.
     27      1.91        ad  */
     28       1.9       cgd 
     29       1.1       cgd /*
     30       1.7   mycroft  * Copyright (c) 1982, 1986, 1988, 1990, 1993
     31       1.7   mycroft  *	The Regents of the University of California.  All rights reserved.
     32       1.1       cgd  *
     33       1.1       cgd  * Redistribution and use in source and binary forms, with or without
     34       1.1       cgd  * modification, are permitted provided that the following conditions
     35       1.1       cgd  * are met:
     36       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     37       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     38       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     39       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     40       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     41      1.54       agc  * 3. Neither the name of the University nor the names of its contributors
     42       1.1       cgd  *    may be used to endorse or promote products derived from this software
     43       1.1       cgd  *    without specific prior written permission.
     44       1.1       cgd  *
     45       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     46       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     49       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55       1.1       cgd  * SUCH DAMAGE.
     56       1.1       cgd  *
     57      1.23      fvdl  *	@(#)uipc_socket2.c	8.2 (Berkeley) 2/14/95
     58       1.1       cgd  */
     59      1.42     lukem 
     60      1.42     lukem #include <sys/cdefs.h>
     61  1.91.2.5      yamt __KERNEL_RCSID(0, "$NetBSD: uipc_socket2.c,v 1.91.2.5 2010/03/11 15:04:20 yamt Exp $");
     62      1.51    martin 
     63      1.51    martin #include "opt_mbuftrace.h"
     64      1.58   thorpej #include "opt_sb_max.h"
     65       1.1       cgd 
     66       1.5   mycroft #include <sys/param.h>
     67       1.5   mycroft #include <sys/systm.h>
     68       1.5   mycroft #include <sys/proc.h>
     69       1.5   mycroft #include <sys/file.h>
     70       1.5   mycroft #include <sys/buf.h>
     71       1.5   mycroft #include <sys/malloc.h>
     72       1.5   mycroft #include <sys/mbuf.h>
     73       1.5   mycroft #include <sys/protosw.h>
     74      1.91        ad #include <sys/domain.h>
     75      1.55  christos #include <sys/poll.h>
     76       1.5   mycroft #include <sys/socket.h>
     77       1.5   mycroft #include <sys/socketvar.h>
     78      1.11  christos #include <sys/signalvar.h>
     79      1.71      elad #include <sys/kauth.h>
     80      1.91        ad #include <sys/pool.h>
     81  1.91.2.2      yamt #include <sys/uidinfo.h>
     82       1.1       cgd 
     83       1.1       cgd /*
     84      1.91        ad  * Primitive routines for operating on sockets and socket buffers.
     85      1.91        ad  *
     86      1.91        ad  * Locking rules and assumptions:
     87      1.91        ad  *
     88      1.91        ad  * o socket::so_lock can change on the fly.  The low level routines used
     89      1.91        ad  *   to lock sockets are aware of this.  When so_lock is acquired, the
     90      1.91        ad  *   routine locking must check to see if so_lock still points to the
     91      1.91        ad  *   lock that was acquired.  If so_lock has changed in the meantime, the
     92      1.91        ad  *   now irellevant lock that was acquired must be dropped and the lock
     93      1.91        ad  *   operation retried.  Although not proven here, this is completely safe
     94      1.91        ad  *   on a multiprocessor system, even with relaxed memory ordering, given
     95      1.91        ad  *   the next two rules:
     96      1.91        ad  *
     97      1.91        ad  * o In order to mutate so_lock, the lock pointed to by the current value
     98      1.91        ad  *   of so_lock must be held: i.e., the socket must be held locked by the
     99      1.91        ad  *   changing thread.  The thread must issue membar_exit() to prevent
    100      1.91        ad  *   memory accesses being reordered, and can set so_lock to the desired
    101      1.91        ad  *   value.  If the lock pointed to by the new value of so_lock is not
    102      1.91        ad  *   held by the changing thread, the socket must then be considered
    103      1.91        ad  *   unlocked.
    104      1.91        ad  *
    105      1.91        ad  * o If so_lock is mutated, and the previous lock referred to by so_lock
    106      1.91        ad  *   could still be visible to other threads in the system (e.g. via file
    107      1.91        ad  *   descriptor or protocol-internal reference), then the old lock must
    108      1.91        ad  *   remain valid until the socket and/or protocol control block has been
    109      1.91        ad  *   torn down.
    110      1.91        ad  *
    111      1.91        ad  * o If a socket has a non-NULL so_head value (i.e. is in the process of
    112      1.91        ad  *   connecting), then locking the socket must also lock the socket pointed
    113      1.91        ad  *   to by so_head: their lock pointers must match.
    114      1.91        ad  *
    115      1.91        ad  * o If a socket has connections in progress (so_q, so_q0 not empty) then
    116      1.91        ad  *   locking the socket must also lock the sockets attached to both queues.
    117      1.91        ad  *   Again, their lock pointers must match.
    118      1.91        ad  *
    119      1.91        ad  * o Beyond the initial lock assigment in socreate(), assigning locks to
    120      1.91        ad  *   sockets is the responsibility of the individual protocols / protocol
    121      1.91        ad  *   domains.
    122       1.1       cgd  */
    123       1.1       cgd 
    124  1.91.2.2      yamt static pool_cache_t socket_cache;
    125       1.1       cgd 
    126      1.58   thorpej u_long	sb_max = SB_MAX;	/* maximum socket buffer size */
    127      1.58   thorpej static u_long sb_max_adj;	/* adjusted sb_max */
    128      1.58   thorpej 
    129       1.1       cgd /*
    130       1.1       cgd  * Procedures to manipulate state flags of socket
    131       1.1       cgd  * and do appropriate wakeups.  Normal sequence from the
    132       1.1       cgd  * active (originating) side is that soisconnecting() is
    133       1.1       cgd  * called during processing of connect() call,
    134       1.1       cgd  * resulting in an eventual call to soisconnected() if/when the
    135       1.1       cgd  * connection is established.  When the connection is torn down
    136       1.1       cgd  * soisdisconnecting() is called during processing of disconnect() call,
    137       1.1       cgd  * and soisdisconnected() is called when the connection to the peer
    138       1.1       cgd  * is totally severed.  The semantics of these routines are such that
    139       1.1       cgd  * connectionless protocols can call soisconnected() and soisdisconnected()
    140       1.1       cgd  * only, bypassing the in-progress calls when setting up a ``connection''
    141       1.1       cgd  * takes no time.
    142       1.1       cgd  *
    143       1.1       cgd  * From the passive side, a socket is created with
    144       1.1       cgd  * two queues of sockets: so_q0 for connections in progress
    145       1.1       cgd  * and so_q for connections already made and awaiting user acceptance.
    146       1.1       cgd  * As a protocol is preparing incoming connections, it creates a socket
    147       1.1       cgd  * structure queued on so_q0 by calling sonewconn().  When the connection
    148       1.1       cgd  * is established, soisconnected() is called, and transfers the
    149       1.1       cgd  * socket structure to so_q, making it available to accept().
    150      1.66     perry  *
    151       1.1       cgd  * If a socket is closed with sockets on either
    152       1.1       cgd  * so_q0 or so_q, these sockets are dropped.
    153       1.1       cgd  *
    154       1.1       cgd  * If higher level protocols are implemented in
    155       1.1       cgd  * the kernel, the wakeups done here will sometimes
    156       1.1       cgd  * cause software-interrupt process scheduling.
    157       1.1       cgd  */
    158       1.1       cgd 
    159       1.7   mycroft void
    160      1.37     lukem soisconnecting(struct socket *so)
    161       1.1       cgd {
    162       1.1       cgd 
    163      1.91        ad 	KASSERT(solocked(so));
    164      1.91        ad 
    165       1.1       cgd 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
    166       1.1       cgd 	so->so_state |= SS_ISCONNECTING;
    167       1.1       cgd }
    168       1.1       cgd 
    169       1.7   mycroft void
    170      1.37     lukem soisconnected(struct socket *so)
    171       1.1       cgd {
    172      1.37     lukem 	struct socket	*head;
    173       1.1       cgd 
    174      1.37     lukem 	head = so->so_head;
    175      1.91        ad 
    176      1.91        ad 	KASSERT(solocked(so));
    177      1.91        ad 	KASSERT(head == NULL || solocked2(so, head));
    178      1.91        ad 
    179       1.1       cgd 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
    180       1.1       cgd 	so->so_state |= SS_ISCONNECTED;
    181  1.91.2.2      yamt 	if (head && so->so_onq == &head->so_q0) {
    182  1.91.2.2      yamt 		if ((so->so_options & SO_ACCEPTFILTER) == 0) {
    183  1.91.2.2      yamt 			soqremque(so, 0);
    184  1.91.2.2      yamt 			soqinsque(head, so, 1);
    185  1.91.2.2      yamt 			sorwakeup(head);
    186  1.91.2.2      yamt 			cv_broadcast(&head->so_cv);
    187  1.91.2.2      yamt 		} else {
    188  1.91.2.2      yamt 			so->so_upcall =
    189  1.91.2.2      yamt 			    head->so_accf->so_accept_filter->accf_callback;
    190  1.91.2.2      yamt 			so->so_upcallarg = head->so_accf->so_accept_filter_arg;
    191  1.91.2.2      yamt 			so->so_rcv.sb_flags |= SB_UPCALL;
    192  1.91.2.2      yamt 			so->so_options &= ~SO_ACCEPTFILTER;
    193  1.91.2.4      yamt 			(*so->so_upcall)(so, so->so_upcallarg,
    194  1.91.2.4      yamt 					 POLLIN|POLLRDNORM, M_DONTWAIT);
    195  1.91.2.2      yamt 		}
    196       1.1       cgd 	} else {
    197      1.91        ad 		cv_broadcast(&so->so_cv);
    198       1.1       cgd 		sorwakeup(so);
    199       1.1       cgd 		sowwakeup(so);
    200       1.1       cgd 	}
    201       1.1       cgd }
    202       1.1       cgd 
    203       1.7   mycroft void
    204      1.37     lukem soisdisconnecting(struct socket *so)
    205       1.1       cgd {
    206       1.1       cgd 
    207      1.91        ad 	KASSERT(solocked(so));
    208      1.91        ad 
    209       1.1       cgd 	so->so_state &= ~SS_ISCONNECTING;
    210       1.1       cgd 	so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
    211      1.91        ad 	cv_broadcast(&so->so_cv);
    212       1.1       cgd 	sowwakeup(so);
    213       1.1       cgd 	sorwakeup(so);
    214       1.1       cgd }
    215       1.1       cgd 
    216       1.7   mycroft void
    217      1.37     lukem soisdisconnected(struct socket *so)
    218       1.1       cgd {
    219       1.1       cgd 
    220      1.91        ad 	KASSERT(solocked(so));
    221      1.91        ad 
    222       1.1       cgd 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
    223      1.27   mycroft 	so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
    224      1.91        ad 	cv_broadcast(&so->so_cv);
    225       1.1       cgd 	sowwakeup(so);
    226       1.1       cgd 	sorwakeup(so);
    227       1.1       cgd }
    228       1.1       cgd 
    229  1.91.2.2      yamt void
    230  1.91.2.2      yamt soinit2(void)
    231  1.91.2.2      yamt {
    232  1.91.2.2      yamt 
    233  1.91.2.2      yamt 	socket_cache = pool_cache_init(sizeof(struct socket), 0, 0, 0,
    234  1.91.2.2      yamt 	    "socket", NULL, IPL_SOFTNET, NULL, NULL, NULL);
    235  1.91.2.2      yamt }
    236  1.91.2.2      yamt 
    237       1.1       cgd /*
    238       1.1       cgd  * When an attempt at a new connection is noted on a socket
    239       1.1       cgd  * which accepts connections, sonewconn is called.  If the
    240       1.1       cgd  * connection is possible (subject to space constraints, etc.)
    241       1.1       cgd  * then we allocate a new structure, propoerly linked into the
    242       1.1       cgd  * data structure of the original socket, and return this.
    243      1.77    plunky  * Connstatus may be 0, SS_ISCONFIRMING, or SS_ISCONNECTED.
    244       1.1       cgd  */
    245       1.1       cgd struct socket *
    246      1.76    plunky sonewconn(struct socket *head, int connstatus)
    247       1.1       cgd {
    248      1.37     lukem 	struct socket	*so;
    249      1.91        ad 	int		soqueue, error;
    250      1.91        ad 
    251  1.91.2.2      yamt 	KASSERT(connstatus == 0 || connstatus == SS_ISCONFIRMING ||
    252  1.91.2.2      yamt 	    connstatus == SS_ISCONNECTED);
    253      1.91        ad 	KASSERT(solocked(head));
    254       1.1       cgd 
    255  1.91.2.2      yamt 	if ((head->so_options & SO_ACCEPTFILTER) != 0)
    256  1.91.2.2      yamt 		connstatus = 0;
    257      1.37     lukem 	soqueue = connstatus ? 1 : 0;
    258       1.1       cgd 	if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2)
    259  1.91.2.2      yamt 		return NULL;
    260      1.91        ad 	so = soget(false);
    261      1.66     perry 	if (so == NULL)
    262  1.91.2.2      yamt 		return NULL;
    263      1.91        ad 	mutex_obj_hold(head->so_lock);
    264      1.91        ad 	so->so_lock = head->so_lock;
    265       1.1       cgd 	so->so_type = head->so_type;
    266       1.1       cgd 	so->so_options = head->so_options &~ SO_ACCEPTCONN;
    267       1.1       cgd 	so->so_linger = head->so_linger;
    268       1.1       cgd 	so->so_state = head->so_state | SS_NOFDREF;
    269      1.89        ad 	so->so_nbio = head->so_nbio;
    270       1.1       cgd 	so->so_proto = head->so_proto;
    271       1.1       cgd 	so->so_timeo = head->so_timeo;
    272       1.1       cgd 	so->so_pgid = head->so_pgid;
    273      1.24      matt 	so->so_send = head->so_send;
    274      1.24      matt 	so->so_receive = head->so_receive;
    275      1.67  christos 	so->so_uidinfo = head->so_uidinfo;
    276  1.91.2.2      yamt 	so->so_cpid = head->so_cpid;
    277      1.49      matt #ifdef MBUFTRACE
    278      1.49      matt 	so->so_mowner = head->so_mowner;
    279      1.49      matt 	so->so_rcv.sb_mowner = head->so_rcv.sb_mowner;
    280      1.49      matt 	so->so_snd.sb_mowner = head->so_snd.sb_mowner;
    281      1.49      matt #endif
    282  1.91.2.3      yamt 	if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) != 0)
    283  1.91.2.3      yamt 		goto out;
    284      1.83       tls 	so->so_snd.sb_lowat = head->so_snd.sb_lowat;
    285      1.83       tls 	so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
    286      1.84       tls 	so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
    287      1.84       tls 	so->so_snd.sb_timeo = head->so_snd.sb_timeo;
    288      1.85     rmind 	so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
    289      1.85     rmind 	so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
    290       1.1       cgd 	soqinsque(head, so, soqueue);
    291      1.91        ad 	error = (*so->so_proto->pr_usrreq)(so, PRU_ATTACH, NULL, NULL,
    292      1.91        ad 	    NULL, NULL);
    293      1.91        ad 	KASSERT(solocked(so));
    294      1.91        ad 	if (error != 0) {
    295       1.1       cgd 		(void) soqremque(so, soqueue);
    296  1.91.2.3      yamt out:
    297  1.91.2.2      yamt 		/*
    298  1.91.2.2      yamt 		 * Remove acccept filter if one is present.
    299  1.91.2.2      yamt 		 * XXX Is this really needed?
    300  1.91.2.2      yamt 		 */
    301  1.91.2.2      yamt 		if (so->so_accf != NULL)
    302  1.91.2.2      yamt 			(void)accept_filt_clear(so);
    303      1.91        ad 		soput(so);
    304  1.91.2.2      yamt 		return NULL;
    305       1.1       cgd 	}
    306       1.1       cgd 	if (connstatus) {
    307       1.1       cgd 		sorwakeup(head);
    308      1.91        ad 		cv_broadcast(&head->so_cv);
    309       1.1       cgd 		so->so_state |= connstatus;
    310       1.1       cgd 	}
    311  1.91.2.2      yamt 	return so;
    312       1.1       cgd }
    313       1.1       cgd 
    314      1.91        ad struct socket *
    315      1.91        ad soget(bool waitok)
    316      1.91        ad {
    317      1.91        ad 	struct socket *so;
    318      1.91        ad 
    319  1.91.2.2      yamt 	so = pool_cache_get(socket_cache, (waitok ? PR_WAITOK : PR_NOWAIT));
    320      1.91        ad 	if (__predict_false(so == NULL))
    321      1.91        ad 		return (NULL);
    322      1.91        ad 	memset(so, 0, sizeof(*so));
    323      1.91        ad 	TAILQ_INIT(&so->so_q0);
    324      1.91        ad 	TAILQ_INIT(&so->so_q);
    325      1.91        ad 	cv_init(&so->so_cv, "socket");
    326      1.91        ad 	cv_init(&so->so_rcv.sb_cv, "netio");
    327      1.91        ad 	cv_init(&so->so_snd.sb_cv, "netio");
    328      1.91        ad 	selinit(&so->so_rcv.sb_sel);
    329      1.91        ad 	selinit(&so->so_snd.sb_sel);
    330      1.91        ad 	so->so_rcv.sb_so = so;
    331      1.91        ad 	so->so_snd.sb_so = so;
    332      1.91        ad 	return so;
    333      1.91        ad }
    334      1.91        ad 
    335      1.91        ad void
    336      1.91        ad soput(struct socket *so)
    337      1.91        ad {
    338      1.91        ad 
    339      1.91        ad 	KASSERT(!cv_has_waiters(&so->so_cv));
    340      1.91        ad 	KASSERT(!cv_has_waiters(&so->so_rcv.sb_cv));
    341      1.91        ad 	KASSERT(!cv_has_waiters(&so->so_snd.sb_cv));
    342      1.91        ad 	seldestroy(&so->so_rcv.sb_sel);
    343      1.91        ad 	seldestroy(&so->so_snd.sb_sel);
    344      1.91        ad 	mutex_obj_free(so->so_lock);
    345      1.91        ad 	cv_destroy(&so->so_cv);
    346      1.91        ad 	cv_destroy(&so->so_rcv.sb_cv);
    347      1.91        ad 	cv_destroy(&so->so_snd.sb_cv);
    348  1.91.2.2      yamt 	pool_cache_put(socket_cache, so);
    349      1.91        ad }
    350      1.91        ad 
    351       1.7   mycroft void
    352      1.37     lukem soqinsque(struct socket *head, struct socket *so, int q)
    353       1.1       cgd {
    354       1.1       cgd 
    355      1.91        ad 	KASSERT(solocked2(head, so));
    356      1.91        ad 
    357      1.22   thorpej #ifdef DIAGNOSTIC
    358      1.22   thorpej 	if (so->so_onq != NULL)
    359      1.22   thorpej 		panic("soqinsque");
    360      1.22   thorpej #endif
    361      1.22   thorpej 
    362       1.1       cgd 	so->so_head = head;
    363       1.1       cgd 	if (q == 0) {
    364       1.1       cgd 		head->so_q0len++;
    365      1.22   thorpej 		so->so_onq = &head->so_q0;
    366       1.1       cgd 	} else {
    367       1.1       cgd 		head->so_qlen++;
    368      1.22   thorpej 		so->so_onq = &head->so_q;
    369       1.1       cgd 	}
    370      1.22   thorpej 	TAILQ_INSERT_TAIL(so->so_onq, so, so_qe);
    371       1.1       cgd }
    372       1.1       cgd 
    373       1.7   mycroft int
    374      1.37     lukem soqremque(struct socket *so, int q)
    375       1.1       cgd {
    376      1.37     lukem 	struct socket	*head;
    377       1.1       cgd 
    378      1.37     lukem 	head = so->so_head;
    379      1.91        ad 
    380      1.91        ad 	KASSERT(solocked(so));
    381      1.22   thorpej 	if (q == 0) {
    382      1.22   thorpej 		if (so->so_onq != &head->so_q0)
    383      1.17   thorpej 			return (0);
    384       1.1       cgd 		head->so_q0len--;
    385       1.1       cgd 	} else {
    386      1.22   thorpej 		if (so->so_onq != &head->so_q)
    387      1.22   thorpej 			return (0);
    388       1.1       cgd 		head->so_qlen--;
    389       1.1       cgd 	}
    390      1.91        ad 	KASSERT(solocked2(so, head));
    391      1.22   thorpej 	TAILQ_REMOVE(so->so_onq, so, so_qe);
    392      1.22   thorpej 	so->so_onq = NULL;
    393      1.22   thorpej 	so->so_head = NULL;
    394       1.1       cgd 	return (1);
    395       1.1       cgd }
    396       1.1       cgd 
    397       1.1       cgd /*
    398       1.1       cgd  * Socantsendmore indicates that no more data will be sent on the
    399       1.1       cgd  * socket; it would normally be applied to a socket when the user
    400       1.1       cgd  * informs the system that no more data is to be sent, by the protocol
    401       1.1       cgd  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
    402       1.1       cgd  * will be received, and will normally be applied to the socket by a
    403       1.1       cgd  * protocol when it detects that the peer will send no more data.
    404       1.1       cgd  * Data queued for reading in the socket may yet be read.
    405       1.1       cgd  */
    406       1.1       cgd 
    407       1.4    andrew void
    408      1.37     lukem socantsendmore(struct socket *so)
    409       1.1       cgd {
    410       1.1       cgd 
    411      1.91        ad 	KASSERT(solocked(so));
    412      1.91        ad 
    413       1.1       cgd 	so->so_state |= SS_CANTSENDMORE;
    414       1.1       cgd 	sowwakeup(so);
    415       1.1       cgd }
    416       1.1       cgd 
    417       1.4    andrew void
    418      1.37     lukem socantrcvmore(struct socket *so)
    419       1.1       cgd {
    420       1.1       cgd 
    421      1.91        ad 	KASSERT(solocked(so));
    422      1.91        ad 
    423       1.1       cgd 	so->so_state |= SS_CANTRCVMORE;
    424       1.1       cgd 	sorwakeup(so);
    425       1.1       cgd }
    426       1.1       cgd 
    427       1.1       cgd /*
    428       1.1       cgd  * Wait for data to arrive at/drain from a socket buffer.
    429       1.1       cgd  */
    430       1.7   mycroft int
    431      1.37     lukem sbwait(struct sockbuf *sb)
    432       1.1       cgd {
    433      1.91        ad 	struct socket *so;
    434      1.91        ad 	kmutex_t *lock;
    435      1.91        ad 	int error;
    436       1.1       cgd 
    437      1.91        ad 	so = sb->sb_so;
    438       1.1       cgd 
    439      1.91        ad 	KASSERT(solocked(so));
    440       1.1       cgd 
    441      1.91        ad 	sb->sb_flags |= SB_NOTIFY;
    442      1.91        ad 	lock = so->so_lock;
    443      1.91        ad 	if ((sb->sb_flags & SB_NOINTR) != 0)
    444      1.91        ad 		error = cv_timedwait(&sb->sb_cv, lock, sb->sb_timeo);
    445      1.91        ad 	else
    446      1.91        ad 		error = cv_timedwait_sig(&sb->sb_cv, lock, sb->sb_timeo);
    447      1.91        ad 	if (__predict_false(lock != so->so_lock))
    448      1.91        ad 		solockretry(so, lock);
    449      1.91        ad 	return error;
    450       1.1       cgd }
    451       1.1       cgd 
    452       1.1       cgd /*
    453       1.1       cgd  * Wakeup processes waiting on a socket buffer.
    454       1.1       cgd  * Do asynchronous notification via SIGIO
    455      1.39      manu  * if the socket buffer has the SB_ASYNC flag set.
    456       1.1       cgd  */
    457       1.7   mycroft void
    458      1.55  christos sowakeup(struct socket *so, struct sockbuf *sb, int code)
    459       1.1       cgd {
    460      1.90     rmind 	int band;
    461      1.90     rmind 
    462      1.91        ad 	KASSERT(solocked(so));
    463      1.91        ad 	KASSERT(sb->sb_so == so);
    464      1.91        ad 
    465      1.90     rmind 	if (code == POLL_IN)
    466      1.90     rmind 		band = POLLIN|POLLRDNORM;
    467      1.90     rmind 	else
    468      1.90     rmind 		band = POLLOUT|POLLWRNORM;
    469      1.91        ad 	sb->sb_flags &= ~SB_NOTIFY;
    470      1.91        ad 	selnotify(&sb->sb_sel, band, NOTE_SUBMIT);
    471      1.91        ad 	cv_broadcast(&sb->sb_cv);
    472      1.90     rmind 	if (sb->sb_flags & SB_ASYNC)
    473      1.57  christos 		fownsignal(so->so_pgid, SIGIO, code, band, so);
    474      1.24      matt 	if (sb->sb_flags & SB_UPCALL)
    475  1.91.2.4      yamt 		(*so->so_upcall)(so, so->so_upcallarg, band, M_DONTWAIT);
    476       1.1       cgd }
    477       1.1       cgd 
    478       1.1       cgd /*
    479  1.91.2.2      yamt  * Reset a socket's lock pointer.  Wake all threads waiting on the
    480  1.91.2.2      yamt  * socket's condition variables so that they can restart their waits
    481  1.91.2.2      yamt  * using the new lock.  The existing lock must be held.
    482  1.91.2.2      yamt  */
    483  1.91.2.2      yamt void
    484  1.91.2.2      yamt solockreset(struct socket *so, kmutex_t *lock)
    485  1.91.2.2      yamt {
    486  1.91.2.2      yamt 
    487  1.91.2.2      yamt 	KASSERT(solocked(so));
    488  1.91.2.2      yamt 
    489  1.91.2.2      yamt 	so->so_lock = lock;
    490  1.91.2.2      yamt 	cv_broadcast(&so->so_snd.sb_cv);
    491  1.91.2.2      yamt 	cv_broadcast(&so->so_rcv.sb_cv);
    492  1.91.2.2      yamt 	cv_broadcast(&so->so_cv);
    493  1.91.2.2      yamt }
    494  1.91.2.2      yamt 
    495  1.91.2.2      yamt /*
    496       1.1       cgd  * Socket buffer (struct sockbuf) utility routines.
    497       1.1       cgd  *
    498       1.1       cgd  * Each socket contains two socket buffers: one for sending data and
    499       1.1       cgd  * one for receiving data.  Each buffer contains a queue of mbufs,
    500       1.1       cgd  * information about the number of mbufs and amount of data in the
    501      1.13   mycroft  * queue, and other fields allowing poll() statements and notification
    502       1.1       cgd  * on data availability to be implemented.
    503       1.1       cgd  *
    504       1.1       cgd  * Data stored in a socket buffer is maintained as a list of records.
    505       1.1       cgd  * Each record is a list of mbufs chained together with the m_next
    506       1.1       cgd  * field.  Records are chained together with the m_nextpkt field. The upper
    507       1.1       cgd  * level routine soreceive() expects the following conventions to be
    508       1.1       cgd  * observed when placing information in the receive buffer:
    509       1.1       cgd  *
    510       1.1       cgd  * 1. If the protocol requires each message be preceded by the sender's
    511       1.1       cgd  *    name, then a record containing that name must be present before
    512       1.1       cgd  *    any associated data (mbuf's must be of type MT_SONAME).
    513       1.1       cgd  * 2. If the protocol supports the exchange of ``access rights'' (really
    514       1.1       cgd  *    just additional data associated with the message), and there are
    515       1.1       cgd  *    ``rights'' to be received, then a record containing this data
    516      1.10   mycroft  *    should be present (mbuf's must be of type MT_CONTROL).
    517       1.1       cgd  * 3. If a name or rights record exists, then it must be followed by
    518       1.1       cgd  *    a data record, perhaps of zero length.
    519       1.1       cgd  *
    520       1.1       cgd  * Before using a new socket structure it is first necessary to reserve
    521       1.1       cgd  * buffer space to the socket, by calling sbreserve().  This should commit
    522       1.1       cgd  * some of the available buffer space in the system buffer pool for the
    523       1.1       cgd  * socket (currently, it does nothing but enforce limits).  The space
    524       1.1       cgd  * should be released by calling sbrelease() when the socket is destroyed.
    525       1.1       cgd  */
    526       1.1       cgd 
    527       1.7   mycroft int
    528      1.58   thorpej sb_max_set(u_long new_sbmax)
    529      1.58   thorpej {
    530      1.58   thorpej 	int s;
    531      1.58   thorpej 
    532      1.58   thorpej 	if (new_sbmax < (16 * 1024))
    533      1.58   thorpej 		return (EINVAL);
    534      1.58   thorpej 
    535      1.58   thorpej 	s = splsoftnet();
    536      1.58   thorpej 	sb_max = new_sbmax;
    537      1.58   thorpej 	sb_max_adj = (u_quad_t)new_sbmax * MCLBYTES / (MSIZE + MCLBYTES);
    538      1.58   thorpej 	splx(s);
    539      1.58   thorpej 
    540      1.58   thorpej 	return (0);
    541      1.58   thorpej }
    542      1.58   thorpej 
    543      1.58   thorpej int
    544      1.37     lukem soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
    545       1.1       cgd {
    546      1.91        ad 
    547      1.91        ad 	KASSERT(so->so_lock == NULL || solocked(so));
    548      1.91        ad 
    549      1.74  christos 	/*
    550      1.74  christos 	 * there's at least one application (a configure script of screen)
    551      1.74  christos 	 * which expects a fifo is writable even if it has "some" bytes
    552      1.74  christos 	 * in its buffer.
    553      1.74  christos 	 * so we want to make sure (hiwat - lowat) >= (some bytes).
    554      1.74  christos 	 *
    555      1.74  christos 	 * PIPE_BUF here is an arbitrary value chosen as (some bytes) above.
    556      1.74  christos 	 * we expect it's large enough for such applications.
    557      1.74  christos 	 */
    558      1.74  christos 	u_long  lowat = MAX(sock_loan_thresh, MCLBYTES);
    559      1.74  christos 	u_long  hiwat = lowat + PIPE_BUF;
    560       1.1       cgd 
    561      1.74  christos 	if (sndcc < hiwat)
    562      1.74  christos 		sndcc = hiwat;
    563      1.59  christos 	if (sbreserve(&so->so_snd, sndcc, so) == 0)
    564       1.1       cgd 		goto bad;
    565      1.59  christos 	if (sbreserve(&so->so_rcv, rcvcc, so) == 0)
    566       1.1       cgd 		goto bad2;
    567       1.1       cgd 	if (so->so_rcv.sb_lowat == 0)
    568       1.1       cgd 		so->so_rcv.sb_lowat = 1;
    569       1.1       cgd 	if (so->so_snd.sb_lowat == 0)
    570      1.74  christos 		so->so_snd.sb_lowat = lowat;
    571       1.1       cgd 	if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
    572       1.1       cgd 		so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
    573       1.1       cgd 	return (0);
    574      1.37     lukem  bad2:
    575      1.59  christos 	sbrelease(&so->so_snd, so);
    576      1.37     lukem  bad:
    577       1.1       cgd 	return (ENOBUFS);
    578       1.1       cgd }
    579       1.1       cgd 
    580       1.1       cgd /*
    581       1.1       cgd  * Allot mbufs to a sockbuf.
    582       1.1       cgd  * Attempt to scale mbmax so that mbcnt doesn't become limiting
    583       1.1       cgd  * if buffering efficiency is near the normal case.
    584       1.1       cgd  */
    585       1.7   mycroft int
    586      1.59  christos sbreserve(struct sockbuf *sb, u_long cc, struct socket *so)
    587       1.1       cgd {
    588      1.75        ad 	struct lwp *l = curlwp; /* XXX */
    589      1.62  christos 	rlim_t maxcc;
    590      1.67  christos 	struct uidinfo *uidinfo;
    591       1.1       cgd 
    592      1.91        ad 	KASSERT(so->so_lock == NULL || solocked(so));
    593      1.91        ad 	KASSERT(sb->sb_so == so);
    594      1.91        ad 	KASSERT(sb_max_adj != 0);
    595      1.91        ad 
    596      1.58   thorpej 	if (cc == 0 || cc > sb_max_adj)
    597       1.1       cgd 		return (0);
    598  1.91.2.2      yamt 
    599  1.91.2.5      yamt 	maxcc = l->l_proc->p_rlimit[RLIMIT_SBSIZE].rlim_cur;
    600  1.91.2.2      yamt 
    601  1.91.2.2      yamt 	uidinfo = so->so_uidinfo;
    602      1.67  christos 	if (!chgsbsize(uidinfo, &sb->sb_hiwat, cc, maxcc))
    603      1.62  christos 		return 0;
    604       1.1       cgd 	sb->sb_mbmax = min(cc * 2, sb_max);
    605       1.1       cgd 	if (sb->sb_lowat > sb->sb_hiwat)
    606       1.1       cgd 		sb->sb_lowat = sb->sb_hiwat;
    607       1.1       cgd 	return (1);
    608       1.1       cgd }
    609       1.1       cgd 
    610       1.1       cgd /*
    611      1.91        ad  * Free mbufs held by a socket, and reserved mbuf space.  We do not assert
    612      1.91        ad  * that the socket is held locked here: see sorflush().
    613       1.1       cgd  */
    614       1.7   mycroft void
    615      1.59  christos sbrelease(struct sockbuf *sb, struct socket *so)
    616       1.1       cgd {
    617       1.1       cgd 
    618      1.91        ad 	KASSERT(sb->sb_so == so);
    619      1.91        ad 
    620       1.1       cgd 	sbflush(sb);
    621      1.87      yamt 	(void)chgsbsize(so->so_uidinfo, &sb->sb_hiwat, 0, RLIM_INFINITY);
    622      1.59  christos 	sb->sb_mbmax = 0;
    623       1.1       cgd }
    624       1.1       cgd 
    625       1.1       cgd /*
    626       1.1       cgd  * Routines to add and remove
    627       1.1       cgd  * data from an mbuf queue.
    628       1.1       cgd  *
    629       1.1       cgd  * The routines sbappend() or sbappendrecord() are normally called to
    630       1.1       cgd  * append new mbufs to a socket buffer, after checking that adequate
    631       1.1       cgd  * space is available, comparing the function sbspace() with the amount
    632       1.1       cgd  * of data to be added.  sbappendrecord() differs from sbappend() in
    633       1.1       cgd  * that data supplied is treated as the beginning of a new record.
    634       1.1       cgd  * To place a sender's address, optional access rights, and data in a
    635       1.1       cgd  * socket receive buffer, sbappendaddr() should be used.  To place
    636       1.1       cgd  * access rights and data in a socket receive buffer, sbappendrights()
    637       1.1       cgd  * should be used.  In either case, the new data begins a new record.
    638       1.1       cgd  * Note that unlike sbappend() and sbappendrecord(), these routines check
    639       1.1       cgd  * for the caller that there will be enough space to store the data.
    640       1.1       cgd  * Each fails if there is not enough space, or if it cannot find mbufs
    641       1.1       cgd  * to store additional information in.
    642       1.1       cgd  *
    643       1.1       cgd  * Reliable protocols may use the socket send buffer to hold data
    644       1.1       cgd  * awaiting acknowledgement.  Data is normally copied from a socket
    645       1.1       cgd  * send buffer in a protocol with m_copy for output to a peer,
    646       1.1       cgd  * and then removing the data from the socket buffer with sbdrop()
    647       1.1       cgd  * or sbdroprecord() when the data is acknowledged by the peer.
    648       1.1       cgd  */
    649       1.1       cgd 
    650      1.43   thorpej #ifdef SOCKBUF_DEBUG
    651      1.43   thorpej void
    652      1.43   thorpej sblastrecordchk(struct sockbuf *sb, const char *where)
    653      1.43   thorpej {
    654      1.43   thorpej 	struct mbuf *m = sb->sb_mb;
    655      1.43   thorpej 
    656      1.91        ad 	KASSERT(solocked(sb->sb_so));
    657      1.91        ad 
    658      1.43   thorpej 	while (m && m->m_nextpkt)
    659      1.43   thorpej 		m = m->m_nextpkt;
    660      1.43   thorpej 
    661      1.43   thorpej 	if (m != sb->sb_lastrecord) {
    662      1.43   thorpej 		printf("sblastrecordchk: sb_mb %p sb_lastrecord %p last %p\n",
    663      1.43   thorpej 		    sb->sb_mb, sb->sb_lastrecord, m);
    664      1.43   thorpej 		printf("packet chain:\n");
    665      1.43   thorpej 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
    666      1.43   thorpej 			printf("\t%p\n", m);
    667      1.47    provos 		panic("sblastrecordchk from %s", where);
    668      1.43   thorpej 	}
    669      1.43   thorpej }
    670      1.43   thorpej 
    671      1.43   thorpej void
    672      1.43   thorpej sblastmbufchk(struct sockbuf *sb, const char *where)
    673      1.43   thorpej {
    674      1.43   thorpej 	struct mbuf *m = sb->sb_mb;
    675      1.43   thorpej 	struct mbuf *n;
    676      1.43   thorpej 
    677      1.91        ad 	KASSERT(solocked(sb->sb_so));
    678      1.91        ad 
    679      1.43   thorpej 	while (m && m->m_nextpkt)
    680      1.43   thorpej 		m = m->m_nextpkt;
    681      1.43   thorpej 
    682      1.43   thorpej 	while (m && m->m_next)
    683      1.43   thorpej 		m = m->m_next;
    684      1.43   thorpej 
    685      1.43   thorpej 	if (m != sb->sb_mbtail) {
    686      1.43   thorpej 		printf("sblastmbufchk: sb_mb %p sb_mbtail %p last %p\n",
    687      1.43   thorpej 		    sb->sb_mb, sb->sb_mbtail, m);
    688      1.43   thorpej 		printf("packet tree:\n");
    689      1.43   thorpej 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
    690      1.43   thorpej 			printf("\t");
    691      1.43   thorpej 			for (n = m; n != NULL; n = n->m_next)
    692      1.43   thorpej 				printf("%p ", n);
    693      1.43   thorpej 			printf("\n");
    694      1.43   thorpej 		}
    695      1.43   thorpej 		panic("sblastmbufchk from %s", where);
    696      1.43   thorpej 	}
    697      1.43   thorpej }
    698      1.43   thorpej #endif /* SOCKBUF_DEBUG */
    699      1.43   thorpej 
    700      1.63  jonathan /*
    701      1.63  jonathan  * Link a chain of records onto a socket buffer
    702      1.63  jonathan  */
    703      1.63  jonathan #define	SBLINKRECORDCHAIN(sb, m0, mlast)				\
    704      1.43   thorpej do {									\
    705      1.43   thorpej 	if ((sb)->sb_lastrecord != NULL)				\
    706      1.43   thorpej 		(sb)->sb_lastrecord->m_nextpkt = (m0);			\
    707      1.43   thorpej 	else								\
    708      1.43   thorpej 		(sb)->sb_mb = (m0);					\
    709      1.63  jonathan 	(sb)->sb_lastrecord = (mlast);					\
    710      1.43   thorpej } while (/*CONSTCOND*/0)
    711      1.43   thorpej 
    712      1.63  jonathan 
    713      1.63  jonathan #define	SBLINKRECORD(sb, m0)						\
    714      1.63  jonathan     SBLINKRECORDCHAIN(sb, m0, m0)
    715      1.63  jonathan 
    716       1.1       cgd /*
    717       1.1       cgd  * Append mbuf chain m to the last record in the
    718       1.1       cgd  * socket buffer sb.  The additional space associated
    719       1.1       cgd  * the mbuf chain is recorded in sb.  Empty mbufs are
    720       1.1       cgd  * discarded and mbufs are compacted where possible.
    721       1.1       cgd  */
    722       1.7   mycroft void
    723      1.37     lukem sbappend(struct sockbuf *sb, struct mbuf *m)
    724       1.1       cgd {
    725      1.37     lukem 	struct mbuf	*n;
    726       1.1       cgd 
    727      1.91        ad 	KASSERT(solocked(sb->sb_so));
    728      1.91        ad 
    729       1.1       cgd 	if (m == 0)
    730       1.1       cgd 		return;
    731      1.43   thorpej 
    732      1.49      matt #ifdef MBUFTRACE
    733      1.65  jonathan 	m_claimm(m, sb->sb_mowner);
    734      1.49      matt #endif
    735      1.49      matt 
    736      1.43   thorpej 	SBLASTRECORDCHK(sb, "sbappend 1");
    737      1.43   thorpej 
    738      1.43   thorpej 	if ((n = sb->sb_lastrecord) != NULL) {
    739      1.43   thorpej 		/*
    740      1.43   thorpej 		 * XXX Would like to simply use sb_mbtail here, but
    741      1.43   thorpej 		 * XXX I need to verify that I won't miss an EOR that
    742      1.43   thorpej 		 * XXX way.
    743      1.43   thorpej 		 */
    744       1.1       cgd 		do {
    745       1.1       cgd 			if (n->m_flags & M_EOR) {
    746       1.1       cgd 				sbappendrecord(sb, m); /* XXXXXX!!!! */
    747       1.1       cgd 				return;
    748       1.1       cgd 			}
    749       1.1       cgd 		} while (n->m_next && (n = n->m_next));
    750      1.43   thorpej 	} else {
    751      1.43   thorpej 		/*
    752      1.43   thorpej 		 * If this is the first record in the socket buffer, it's
    753      1.43   thorpej 		 * also the last record.
    754      1.43   thorpej 		 */
    755      1.43   thorpej 		sb->sb_lastrecord = m;
    756       1.1       cgd 	}
    757       1.1       cgd 	sbcompress(sb, m, n);
    758      1.43   thorpej 	SBLASTRECORDCHK(sb, "sbappend 2");
    759      1.43   thorpej }
    760      1.43   thorpej 
    761      1.43   thorpej /*
    762      1.43   thorpej  * This version of sbappend() should only be used when the caller
    763      1.43   thorpej  * absolutely knows that there will never be more than one record
    764      1.43   thorpej  * in the socket buffer, that is, a stream protocol (such as TCP).
    765      1.43   thorpej  */
    766      1.43   thorpej void
    767      1.44   thorpej sbappendstream(struct sockbuf *sb, struct mbuf *m)
    768      1.43   thorpej {
    769      1.43   thorpej 
    770      1.91        ad 	KASSERT(solocked(sb->sb_so));
    771      1.43   thorpej 	KDASSERT(m->m_nextpkt == NULL);
    772      1.43   thorpej 	KASSERT(sb->sb_mb == sb->sb_lastrecord);
    773      1.43   thorpej 
    774      1.43   thorpej 	SBLASTMBUFCHK(sb, __func__);
    775      1.43   thorpej 
    776      1.49      matt #ifdef MBUFTRACE
    777      1.65  jonathan 	m_claimm(m, sb->sb_mowner);
    778      1.49      matt #endif
    779      1.49      matt 
    780      1.43   thorpej 	sbcompress(sb, m, sb->sb_mbtail);
    781      1.43   thorpej 
    782      1.43   thorpej 	sb->sb_lastrecord = sb->sb_mb;
    783      1.43   thorpej 	SBLASTRECORDCHK(sb, __func__);
    784       1.1       cgd }
    785       1.1       cgd 
    786       1.1       cgd #ifdef SOCKBUF_DEBUG
    787       1.7   mycroft void
    788      1.37     lukem sbcheck(struct sockbuf *sb)
    789       1.1       cgd {
    790      1.91        ad 	struct mbuf	*m, *m2;
    791      1.43   thorpej 	u_long		len, mbcnt;
    792       1.1       cgd 
    793      1.91        ad 	KASSERT(solocked(sb->sb_so));
    794      1.91        ad 
    795      1.37     lukem 	len = 0;
    796      1.37     lukem 	mbcnt = 0;
    797      1.91        ad 	for (m = sb->sb_mb; m; m = m->m_nextpkt) {
    798      1.91        ad 		for (m2 = m; m2 != NULL; m2 = m2->m_next) {
    799      1.91        ad 			len += m2->m_len;
    800      1.91        ad 			mbcnt += MSIZE;
    801      1.91        ad 			if (m2->m_flags & M_EXT)
    802      1.91        ad 				mbcnt += m2->m_ext.ext_size;
    803      1.91        ad 			if (m2->m_nextpkt != NULL)
    804      1.91        ad 				panic("sbcheck nextpkt");
    805      1.91        ad 		}
    806       1.1       cgd 	}
    807       1.1       cgd 	if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
    808      1.43   thorpej 		printf("cc %lu != %lu || mbcnt %lu != %lu\n", len, sb->sb_cc,
    809       1.1       cgd 		    mbcnt, sb->sb_mbcnt);
    810       1.1       cgd 		panic("sbcheck");
    811       1.1       cgd 	}
    812       1.1       cgd }
    813       1.1       cgd #endif
    814       1.1       cgd 
    815       1.1       cgd /*
    816       1.1       cgd  * As above, except the mbuf chain
    817       1.1       cgd  * begins a new record.
    818       1.1       cgd  */
    819       1.7   mycroft void
    820      1.37     lukem sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
    821       1.1       cgd {
    822      1.37     lukem 	struct mbuf	*m;
    823       1.1       cgd 
    824      1.91        ad 	KASSERT(solocked(sb->sb_so));
    825      1.91        ad 
    826       1.1       cgd 	if (m0 == 0)
    827       1.1       cgd 		return;
    828      1.43   thorpej 
    829      1.49      matt #ifdef MBUFTRACE
    830      1.65  jonathan 	m_claimm(m0, sb->sb_mowner);
    831      1.49      matt #endif
    832       1.1       cgd 	/*
    833       1.1       cgd 	 * Put the first mbuf on the queue.
    834       1.1       cgd 	 * Note this permits zero length records.
    835       1.1       cgd 	 */
    836       1.1       cgd 	sballoc(sb, m0);
    837      1.43   thorpej 	SBLASTRECORDCHK(sb, "sbappendrecord 1");
    838      1.43   thorpej 	SBLINKRECORD(sb, m0);
    839       1.1       cgd 	m = m0->m_next;
    840       1.1       cgd 	m0->m_next = 0;
    841       1.1       cgd 	if (m && (m0->m_flags & M_EOR)) {
    842       1.1       cgd 		m0->m_flags &= ~M_EOR;
    843       1.1       cgd 		m->m_flags |= M_EOR;
    844       1.1       cgd 	}
    845       1.1       cgd 	sbcompress(sb, m, m0);
    846      1.43   thorpej 	SBLASTRECORDCHK(sb, "sbappendrecord 2");
    847       1.1       cgd }
    848       1.1       cgd 
    849       1.1       cgd /*
    850       1.1       cgd  * As above except that OOB data
    851       1.1       cgd  * is inserted at the beginning of the sockbuf,
    852       1.1       cgd  * but after any other OOB data.
    853       1.1       cgd  */
    854       1.7   mycroft void
    855      1.37     lukem sbinsertoob(struct sockbuf *sb, struct mbuf *m0)
    856       1.1       cgd {
    857      1.37     lukem 	struct mbuf	*m, **mp;
    858       1.1       cgd 
    859      1.91        ad 	KASSERT(solocked(sb->sb_so));
    860      1.91        ad 
    861       1.1       cgd 	if (m0 == 0)
    862       1.1       cgd 		return;
    863      1.43   thorpej 
    864      1.43   thorpej 	SBLASTRECORDCHK(sb, "sbinsertoob 1");
    865      1.43   thorpej 
    866      1.11  christos 	for (mp = &sb->sb_mb; (m = *mp) != NULL; mp = &((*mp)->m_nextpkt)) {
    867       1.1       cgd 	    again:
    868       1.1       cgd 		switch (m->m_type) {
    869       1.1       cgd 
    870       1.1       cgd 		case MT_OOBDATA:
    871       1.1       cgd 			continue;		/* WANT next train */
    872       1.1       cgd 
    873       1.1       cgd 		case MT_CONTROL:
    874      1.11  christos 			if ((m = m->m_next) != NULL)
    875       1.1       cgd 				goto again;	/* inspect THIS train further */
    876       1.1       cgd 		}
    877       1.1       cgd 		break;
    878       1.1       cgd 	}
    879       1.1       cgd 	/*
    880       1.1       cgd 	 * Put the first mbuf on the queue.
    881       1.1       cgd 	 * Note this permits zero length records.
    882       1.1       cgd 	 */
    883       1.1       cgd 	sballoc(sb, m0);
    884       1.1       cgd 	m0->m_nextpkt = *mp;
    885      1.43   thorpej 	if (*mp == NULL) {
    886      1.43   thorpej 		/* m0 is actually the new tail */
    887      1.43   thorpej 		sb->sb_lastrecord = m0;
    888      1.43   thorpej 	}
    889       1.1       cgd 	*mp = m0;
    890       1.1       cgd 	m = m0->m_next;
    891       1.1       cgd 	m0->m_next = 0;
    892       1.1       cgd 	if (m && (m0->m_flags & M_EOR)) {
    893       1.1       cgd 		m0->m_flags &= ~M_EOR;
    894       1.1       cgd 		m->m_flags |= M_EOR;
    895       1.1       cgd 	}
    896       1.1       cgd 	sbcompress(sb, m, m0);
    897      1.43   thorpej 	SBLASTRECORDCHK(sb, "sbinsertoob 2");
    898       1.1       cgd }
    899       1.1       cgd 
    900       1.1       cgd /*
    901       1.1       cgd  * Append address and data, and optionally, control (ancillary) data
    902       1.1       cgd  * to the receive queue of a socket.  If present,
    903       1.1       cgd  * m0 must include a packet header with total length.
    904       1.1       cgd  * Returns 0 if no space in sockbuf or insufficient mbufs.
    905       1.1       cgd  */
    906       1.7   mycroft int
    907      1.61      matt sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa, struct mbuf *m0,
    908      1.37     lukem 	struct mbuf *control)
    909       1.1       cgd {
    910      1.43   thorpej 	struct mbuf	*m, *n, *nlast;
    911      1.50      fvdl 	int		space, len;
    912       1.1       cgd 
    913      1.91        ad 	KASSERT(solocked(sb->sb_so));
    914      1.91        ad 
    915      1.37     lukem 	space = asa->sa_len;
    916      1.37     lukem 
    917      1.49      matt 	if (m0 != NULL) {
    918      1.49      matt 		if ((m0->m_flags & M_PKTHDR) == 0)
    919      1.49      matt 			panic("sbappendaddr");
    920       1.1       cgd 		space += m0->m_pkthdr.len;
    921      1.49      matt #ifdef MBUFTRACE
    922      1.65  jonathan 		m_claimm(m0, sb->sb_mowner);
    923      1.49      matt #endif
    924      1.49      matt 	}
    925       1.1       cgd 	for (n = control; n; n = n->m_next) {
    926       1.1       cgd 		space += n->m_len;
    927      1.49      matt 		MCLAIM(n, sb->sb_mowner);
    928       1.1       cgd 		if (n->m_next == 0)	/* keep pointer to last control buf */
    929       1.1       cgd 			break;
    930       1.1       cgd 	}
    931       1.1       cgd 	if (space > sbspace(sb))
    932       1.1       cgd 		return (0);
    933       1.1       cgd 	MGET(m, M_DONTWAIT, MT_SONAME);
    934       1.1       cgd 	if (m == 0)
    935       1.1       cgd 		return (0);
    936      1.49      matt 	MCLAIM(m, sb->sb_mowner);
    937      1.50      fvdl 	/*
    938      1.50      fvdl 	 * XXX avoid 'comparison always true' warning which isn't easily
    939      1.50      fvdl 	 * avoided.
    940      1.50      fvdl 	 */
    941      1.50      fvdl 	len = asa->sa_len;
    942      1.50      fvdl 	if (len > MLEN) {
    943      1.20   thorpej 		MEXTMALLOC(m, asa->sa_len, M_NOWAIT);
    944      1.20   thorpej 		if ((m->m_flags & M_EXT) == 0) {
    945      1.20   thorpej 			m_free(m);
    946      1.20   thorpej 			return (0);
    947      1.20   thorpej 		}
    948      1.20   thorpej 	}
    949       1.1       cgd 	m->m_len = asa->sa_len;
    950      1.82  christos 	memcpy(mtod(m, void *), asa, asa->sa_len);
    951       1.1       cgd 	if (n)
    952       1.1       cgd 		n->m_next = m0;		/* concatenate data to control */
    953       1.1       cgd 	else
    954       1.1       cgd 		control = m0;
    955       1.1       cgd 	m->m_next = control;
    956      1.43   thorpej 
    957      1.43   thorpej 	SBLASTRECORDCHK(sb, "sbappendaddr 1");
    958      1.43   thorpej 
    959      1.43   thorpej 	for (n = m; n->m_next != NULL; n = n->m_next)
    960       1.1       cgd 		sballoc(sb, n);
    961      1.43   thorpej 	sballoc(sb, n);
    962      1.43   thorpej 	nlast = n;
    963      1.43   thorpej 	SBLINKRECORD(sb, m);
    964      1.43   thorpej 
    965      1.43   thorpej 	sb->sb_mbtail = nlast;
    966      1.43   thorpej 	SBLASTMBUFCHK(sb, "sbappendaddr");
    967      1.43   thorpej 	SBLASTRECORDCHK(sb, "sbappendaddr 2");
    968      1.43   thorpej 
    969       1.1       cgd 	return (1);
    970       1.1       cgd }
    971       1.1       cgd 
    972      1.63  jonathan /*
    973      1.63  jonathan  * Helper for sbappendchainaddr: prepend a struct sockaddr* to
    974      1.63  jonathan  * an mbuf chain.
    975      1.63  jonathan  */
    976      1.70     perry static inline struct mbuf *
    977      1.81      yamt m_prepend_sockaddr(struct sockbuf *sb, struct mbuf *m0,
    978      1.64  jonathan 		   const struct sockaddr *asa)
    979      1.63  jonathan {
    980      1.63  jonathan 	struct mbuf *m;
    981      1.64  jonathan 	const int salen = asa->sa_len;
    982      1.63  jonathan 
    983      1.91        ad 	KASSERT(solocked(sb->sb_so));
    984      1.91        ad 
    985      1.63  jonathan 	/* only the first in each chain need be a pkthdr */
    986      1.63  jonathan 	MGETHDR(m, M_DONTWAIT, MT_SONAME);
    987      1.63  jonathan 	if (m == 0)
    988      1.63  jonathan 		return (0);
    989      1.63  jonathan 	MCLAIM(m, sb->sb_mowner);
    990      1.64  jonathan #ifdef notyet
    991      1.64  jonathan 	if (salen > MHLEN) {
    992      1.64  jonathan 		MEXTMALLOC(m, salen, M_NOWAIT);
    993      1.64  jonathan 		if ((m->m_flags & M_EXT) == 0) {
    994      1.64  jonathan 			m_free(m);
    995      1.64  jonathan 			return (0);
    996      1.64  jonathan 		}
    997      1.64  jonathan 	}
    998      1.64  jonathan #else
    999      1.64  jonathan 	KASSERT(salen <= MHLEN);
   1000      1.64  jonathan #endif
   1001      1.64  jonathan 	m->m_len = salen;
   1002      1.82  christos 	memcpy(mtod(m, void *), asa, salen);
   1003      1.63  jonathan 	m->m_next = m0;
   1004      1.64  jonathan 	m->m_pkthdr.len = salen + m0->m_pkthdr.len;
   1005      1.63  jonathan 
   1006      1.63  jonathan 	return m;
   1007      1.63  jonathan }
   1008      1.63  jonathan 
   1009      1.63  jonathan int
   1010      1.63  jonathan sbappendaddrchain(struct sockbuf *sb, const struct sockaddr *asa,
   1011      1.63  jonathan 		  struct mbuf *m0, int sbprio)
   1012      1.63  jonathan {
   1013      1.63  jonathan 	int space;
   1014      1.63  jonathan 	struct mbuf *m, *n, *n0, *nlast;
   1015      1.63  jonathan 	int error;
   1016      1.63  jonathan 
   1017      1.91        ad 	KASSERT(solocked(sb->sb_so));
   1018      1.91        ad 
   1019      1.63  jonathan 	/*
   1020      1.63  jonathan 	 * XXX sbprio reserved for encoding priority of this* request:
   1021      1.63  jonathan 	 *  SB_PRIO_NONE --> honour normal sb limits
   1022      1.63  jonathan 	 *  SB_PRIO_ONESHOT_OVERFLOW --> if socket has any space,
   1023      1.63  jonathan 	 *	take whole chain. Intended for large requests
   1024      1.63  jonathan 	 *      that should be delivered atomically (all, or none).
   1025      1.63  jonathan 	 * SB_PRIO_OVERDRAFT -- allow a small (2*MLEN) overflow
   1026      1.63  jonathan 	 *       over normal socket limits, for messages indicating
   1027      1.63  jonathan 	 *       buffer overflow in earlier normal/lower-priority messages
   1028      1.63  jonathan 	 * SB_PRIO_BESTEFFORT -->  ignore limits entirely.
   1029      1.63  jonathan 	 *       Intended for  kernel-generated messages only.
   1030      1.63  jonathan 	 *        Up to generator to avoid total mbuf resource exhaustion.
   1031      1.63  jonathan 	 */
   1032      1.63  jonathan 	(void)sbprio;
   1033      1.63  jonathan 
   1034      1.63  jonathan 	if (m0 && (m0->m_flags & M_PKTHDR) == 0)
   1035      1.63  jonathan 		panic("sbappendaddrchain");
   1036      1.63  jonathan 
   1037      1.63  jonathan 	space = sbspace(sb);
   1038      1.66     perry 
   1039      1.63  jonathan #ifdef notyet
   1040      1.66     perry 	/*
   1041      1.63  jonathan 	 * Enforce SB_PRIO_* limits as described above.
   1042      1.63  jonathan 	 */
   1043      1.63  jonathan #endif
   1044      1.63  jonathan 
   1045      1.63  jonathan 	n0 = NULL;
   1046      1.63  jonathan 	nlast = NULL;
   1047      1.63  jonathan 	for (m = m0; m; m = m->m_nextpkt) {
   1048      1.63  jonathan 		struct mbuf *np;
   1049      1.63  jonathan 
   1050      1.64  jonathan #ifdef MBUFTRACE
   1051      1.65  jonathan 		m_claimm(m, sb->sb_mowner);
   1052      1.64  jonathan #endif
   1053      1.64  jonathan 
   1054      1.63  jonathan 		/* Prepend sockaddr to this record (m) of input chain m0 */
   1055      1.64  jonathan 	  	n = m_prepend_sockaddr(sb, m, asa);
   1056      1.63  jonathan 		if (n == NULL) {
   1057      1.63  jonathan 			error = ENOBUFS;
   1058      1.63  jonathan 			goto bad;
   1059      1.63  jonathan 		}
   1060      1.63  jonathan 
   1061      1.63  jonathan 		/* Append record (asa+m) to end of new chain n0 */
   1062      1.63  jonathan 		if (n0 == NULL) {
   1063      1.63  jonathan 			n0 = n;
   1064      1.63  jonathan 		} else {
   1065      1.63  jonathan 			nlast->m_nextpkt = n;
   1066      1.63  jonathan 		}
   1067      1.63  jonathan 		/* Keep track of last record on new chain */
   1068      1.63  jonathan 		nlast = n;
   1069      1.63  jonathan 
   1070      1.63  jonathan 		for (np = n; np; np = np->m_next)
   1071      1.63  jonathan 			sballoc(sb, np);
   1072      1.63  jonathan 	}
   1073      1.63  jonathan 
   1074      1.64  jonathan 	SBLASTRECORDCHK(sb, "sbappendaddrchain 1");
   1075      1.64  jonathan 
   1076      1.63  jonathan 	/* Drop the entire chain of (asa+m) records onto the socket */
   1077      1.63  jonathan 	SBLINKRECORDCHAIN(sb, n0, nlast);
   1078      1.64  jonathan 
   1079      1.64  jonathan 	SBLASTRECORDCHK(sb, "sbappendaddrchain 2");
   1080      1.64  jonathan 
   1081      1.63  jonathan 	for (m = nlast; m->m_next; m = m->m_next)
   1082      1.63  jonathan 		;
   1083      1.63  jonathan 	sb->sb_mbtail = m;
   1084      1.64  jonathan 	SBLASTMBUFCHK(sb, "sbappendaddrchain");
   1085      1.64  jonathan 
   1086      1.63  jonathan 	return (1);
   1087      1.63  jonathan 
   1088      1.63  jonathan bad:
   1089      1.64  jonathan 	/*
   1090      1.64  jonathan 	 * On error, free the prepended addreseses. For consistency
   1091      1.64  jonathan 	 * with sbappendaddr(), leave it to our caller to free
   1092      1.64  jonathan 	 * the input record chain passed to us as m0.
   1093      1.64  jonathan 	 */
   1094      1.64  jonathan 	while ((n = n0) != NULL) {
   1095      1.64  jonathan 	  	struct mbuf *np;
   1096      1.64  jonathan 
   1097      1.64  jonathan 		/* Undo the sballoc() of this record */
   1098      1.64  jonathan 		for (np = n; np; np = np->m_next)
   1099      1.64  jonathan 			sbfree(sb, np);
   1100      1.64  jonathan 
   1101      1.64  jonathan 		n0 = n->m_nextpkt;	/* iterate at next prepended address */
   1102      1.64  jonathan 		MFREE(n, np);		/* free prepended address (not data) */
   1103      1.64  jonathan 	}
   1104      1.66     perry 	return 0;
   1105      1.63  jonathan }
   1106      1.63  jonathan 
   1107      1.63  jonathan 
   1108       1.7   mycroft int
   1109      1.37     lukem sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
   1110       1.1       cgd {
   1111      1.43   thorpej 	struct mbuf	*m, *mlast, *n;
   1112      1.37     lukem 	int		space;
   1113       1.1       cgd 
   1114      1.91        ad 	KASSERT(solocked(sb->sb_so));
   1115      1.91        ad 
   1116      1.37     lukem 	space = 0;
   1117       1.1       cgd 	if (control == 0)
   1118       1.1       cgd 		panic("sbappendcontrol");
   1119       1.1       cgd 	for (m = control; ; m = m->m_next) {
   1120       1.1       cgd 		space += m->m_len;
   1121      1.49      matt 		MCLAIM(m, sb->sb_mowner);
   1122       1.1       cgd 		if (m->m_next == 0)
   1123       1.1       cgd 			break;
   1124       1.1       cgd 	}
   1125       1.1       cgd 	n = m;			/* save pointer to last control buffer */
   1126      1.49      matt 	for (m = m0; m; m = m->m_next) {
   1127      1.49      matt 		MCLAIM(m, sb->sb_mowner);
   1128       1.1       cgd 		space += m->m_len;
   1129      1.49      matt 	}
   1130       1.1       cgd 	if (space > sbspace(sb))
   1131       1.1       cgd 		return (0);
   1132       1.1       cgd 	n->m_next = m0;			/* concatenate data to control */
   1133      1.43   thorpej 
   1134      1.43   thorpej 	SBLASTRECORDCHK(sb, "sbappendcontrol 1");
   1135      1.43   thorpej 
   1136      1.43   thorpej 	for (m = control; m->m_next != NULL; m = m->m_next)
   1137       1.1       cgd 		sballoc(sb, m);
   1138      1.43   thorpej 	sballoc(sb, m);
   1139      1.43   thorpej 	mlast = m;
   1140      1.43   thorpej 	SBLINKRECORD(sb, control);
   1141      1.43   thorpej 
   1142      1.43   thorpej 	sb->sb_mbtail = mlast;
   1143      1.43   thorpej 	SBLASTMBUFCHK(sb, "sbappendcontrol");
   1144      1.43   thorpej 	SBLASTRECORDCHK(sb, "sbappendcontrol 2");
   1145      1.43   thorpej 
   1146       1.1       cgd 	return (1);
   1147       1.1       cgd }
   1148       1.1       cgd 
   1149       1.1       cgd /*
   1150       1.1       cgd  * Compress mbuf chain m into the socket
   1151       1.1       cgd  * buffer sb following mbuf n.  If n
   1152       1.1       cgd  * is null, the buffer is presumed empty.
   1153       1.1       cgd  */
   1154       1.7   mycroft void
   1155      1.37     lukem sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
   1156       1.1       cgd {
   1157      1.37     lukem 	int		eor;
   1158      1.37     lukem 	struct mbuf	*o;
   1159       1.1       cgd 
   1160      1.91        ad 	KASSERT(solocked(sb->sb_so));
   1161      1.91        ad 
   1162      1.37     lukem 	eor = 0;
   1163       1.1       cgd 	while (m) {
   1164       1.1       cgd 		eor |= m->m_flags & M_EOR;
   1165       1.1       cgd 		if (m->m_len == 0 &&
   1166       1.1       cgd 		    (eor == 0 ||
   1167       1.1       cgd 		     (((o = m->m_next) || (o = n)) &&
   1168       1.1       cgd 		      o->m_type == m->m_type))) {
   1169      1.46   thorpej 			if (sb->sb_lastrecord == m)
   1170      1.46   thorpej 				sb->sb_lastrecord = m->m_next;
   1171       1.1       cgd 			m = m_free(m);
   1172       1.1       cgd 			continue;
   1173       1.1       cgd 		}
   1174      1.40   thorpej 		if (n && (n->m_flags & M_EOR) == 0 &&
   1175      1.40   thorpej 		    /* M_TRAILINGSPACE() checks buffer writeability */
   1176      1.40   thorpej 		    m->m_len <= MCLBYTES / 4 && /* XXX Don't copy too much */
   1177      1.40   thorpej 		    m->m_len <= M_TRAILINGSPACE(n) &&
   1178      1.40   thorpej 		    n->m_type == m->m_type) {
   1179      1.82  christos 			memcpy(mtod(n, char *) + n->m_len, mtod(m, void *),
   1180       1.1       cgd 			    (unsigned)m->m_len);
   1181       1.1       cgd 			n->m_len += m->m_len;
   1182       1.1       cgd 			sb->sb_cc += m->m_len;
   1183       1.1       cgd 			m = m_free(m);
   1184       1.1       cgd 			continue;
   1185       1.1       cgd 		}
   1186       1.1       cgd 		if (n)
   1187       1.1       cgd 			n->m_next = m;
   1188       1.1       cgd 		else
   1189       1.1       cgd 			sb->sb_mb = m;
   1190      1.43   thorpej 		sb->sb_mbtail = m;
   1191       1.1       cgd 		sballoc(sb, m);
   1192       1.1       cgd 		n = m;
   1193       1.1       cgd 		m->m_flags &= ~M_EOR;
   1194       1.1       cgd 		m = m->m_next;
   1195       1.1       cgd 		n->m_next = 0;
   1196       1.1       cgd 	}
   1197       1.1       cgd 	if (eor) {
   1198       1.1       cgd 		if (n)
   1199       1.1       cgd 			n->m_flags |= eor;
   1200       1.1       cgd 		else
   1201      1.15  christos 			printf("semi-panic: sbcompress\n");
   1202       1.1       cgd 	}
   1203      1.43   thorpej 	SBLASTMBUFCHK(sb, __func__);
   1204       1.1       cgd }
   1205       1.1       cgd 
   1206       1.1       cgd /*
   1207       1.1       cgd  * Free all mbufs in a sockbuf.
   1208       1.1       cgd  * Check that all resources are reclaimed.
   1209       1.1       cgd  */
   1210       1.7   mycroft void
   1211      1.37     lukem sbflush(struct sockbuf *sb)
   1212       1.1       cgd {
   1213       1.1       cgd 
   1214      1.91        ad 	KASSERT(solocked(sb->sb_so));
   1215      1.43   thorpej 	KASSERT((sb->sb_flags & SB_LOCK) == 0);
   1216      1.43   thorpej 
   1217       1.1       cgd 	while (sb->sb_mbcnt)
   1218       1.1       cgd 		sbdrop(sb, (int)sb->sb_cc);
   1219      1.43   thorpej 
   1220      1.43   thorpej 	KASSERT(sb->sb_cc == 0);
   1221      1.43   thorpej 	KASSERT(sb->sb_mb == NULL);
   1222      1.43   thorpej 	KASSERT(sb->sb_mbtail == NULL);
   1223      1.43   thorpej 	KASSERT(sb->sb_lastrecord == NULL);
   1224       1.1       cgd }
   1225       1.1       cgd 
   1226       1.1       cgd /*
   1227       1.1       cgd  * Drop data from (the front of) a sockbuf.
   1228       1.1       cgd  */
   1229       1.7   mycroft void
   1230      1.37     lukem sbdrop(struct sockbuf *sb, int len)
   1231       1.1       cgd {
   1232      1.37     lukem 	struct mbuf	*m, *mn, *next;
   1233       1.1       cgd 
   1234      1.91        ad 	KASSERT(solocked(sb->sb_so));
   1235      1.91        ad 
   1236       1.1       cgd 	next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
   1237       1.1       cgd 	while (len > 0) {
   1238       1.1       cgd 		if (m == 0) {
   1239       1.1       cgd 			if (next == 0)
   1240       1.1       cgd 				panic("sbdrop");
   1241       1.1       cgd 			m = next;
   1242       1.1       cgd 			next = m->m_nextpkt;
   1243       1.1       cgd 			continue;
   1244       1.1       cgd 		}
   1245       1.1       cgd 		if (m->m_len > len) {
   1246       1.1       cgd 			m->m_len -= len;
   1247       1.1       cgd 			m->m_data += len;
   1248       1.1       cgd 			sb->sb_cc -= len;
   1249       1.1       cgd 			break;
   1250       1.1       cgd 		}
   1251       1.1       cgd 		len -= m->m_len;
   1252       1.1       cgd 		sbfree(sb, m);
   1253       1.1       cgd 		MFREE(m, mn);
   1254       1.1       cgd 		m = mn;
   1255       1.1       cgd 	}
   1256       1.1       cgd 	while (m && m->m_len == 0) {
   1257       1.1       cgd 		sbfree(sb, m);
   1258       1.1       cgd 		MFREE(m, mn);
   1259       1.1       cgd 		m = mn;
   1260       1.1       cgd 	}
   1261       1.1       cgd 	if (m) {
   1262       1.1       cgd 		sb->sb_mb = m;
   1263       1.1       cgd 		m->m_nextpkt = next;
   1264       1.1       cgd 	} else
   1265       1.1       cgd 		sb->sb_mb = next;
   1266      1.43   thorpej 	/*
   1267      1.45   thorpej 	 * First part is an inline SB_EMPTY_FIXUP().  Second part
   1268      1.43   thorpej 	 * makes sure sb_lastrecord is up-to-date if we dropped
   1269      1.43   thorpej 	 * part of the last record.
   1270      1.43   thorpej 	 */
   1271      1.43   thorpej 	m = sb->sb_mb;
   1272      1.43   thorpej 	if (m == NULL) {
   1273      1.43   thorpej 		sb->sb_mbtail = NULL;
   1274      1.43   thorpej 		sb->sb_lastrecord = NULL;
   1275      1.43   thorpej 	} else if (m->m_nextpkt == NULL)
   1276      1.43   thorpej 		sb->sb_lastrecord = m;
   1277       1.1       cgd }
   1278       1.1       cgd 
   1279       1.1       cgd /*
   1280       1.1       cgd  * Drop a record off the front of a sockbuf
   1281       1.1       cgd  * and move the next record to the front.
   1282       1.1       cgd  */
   1283       1.7   mycroft void
   1284      1.37     lukem sbdroprecord(struct sockbuf *sb)
   1285       1.1       cgd {
   1286      1.37     lukem 	struct mbuf	*m, *mn;
   1287       1.1       cgd 
   1288      1.91        ad 	KASSERT(solocked(sb->sb_so));
   1289      1.91        ad 
   1290       1.1       cgd 	m = sb->sb_mb;
   1291       1.1       cgd 	if (m) {
   1292       1.1       cgd 		sb->sb_mb = m->m_nextpkt;
   1293       1.1       cgd 		do {
   1294       1.1       cgd 			sbfree(sb, m);
   1295       1.1       cgd 			MFREE(m, mn);
   1296      1.11  christos 		} while ((m = mn) != NULL);
   1297       1.1       cgd 	}
   1298      1.45   thorpej 	SB_EMPTY_FIXUP(sb);
   1299      1.19   thorpej }
   1300      1.19   thorpej 
   1301      1.19   thorpej /*
   1302      1.19   thorpej  * Create a "control" mbuf containing the specified data
   1303      1.19   thorpej  * with the specified type for presentation on a socket buffer.
   1304      1.19   thorpej  */
   1305      1.19   thorpej struct mbuf *
   1306      1.82  christos sbcreatecontrol(void *p, int size, int type, int level)
   1307      1.19   thorpej {
   1308      1.37     lukem 	struct cmsghdr	*cp;
   1309      1.37     lukem 	struct mbuf	*m;
   1310      1.19   thorpej 
   1311      1.35    itojun 	if (CMSG_SPACE(size) > MCLBYTES) {
   1312      1.30    itojun 		printf("sbcreatecontrol: message too large %d\n", size);
   1313      1.30    itojun 		return NULL;
   1314      1.30    itojun 	}
   1315      1.30    itojun 
   1316      1.19   thorpej 	if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
   1317      1.19   thorpej 		return ((struct mbuf *) NULL);
   1318      1.35    itojun 	if (CMSG_SPACE(size) > MLEN) {
   1319      1.30    itojun 		MCLGET(m, M_DONTWAIT);
   1320      1.30    itojun 		if ((m->m_flags & M_EXT) == 0) {
   1321      1.30    itojun 			m_free(m);
   1322      1.30    itojun 			return NULL;
   1323      1.30    itojun 		}
   1324      1.30    itojun 	}
   1325      1.19   thorpej 	cp = mtod(m, struct cmsghdr *);
   1326      1.26     perry 	memcpy(CMSG_DATA(cp), p, size);
   1327      1.35    itojun 	m->m_len = CMSG_SPACE(size);
   1328      1.35    itojun 	cp->cmsg_len = CMSG_LEN(size);
   1329      1.19   thorpej 	cp->cmsg_level = level;
   1330      1.19   thorpej 	cp->cmsg_type = type;
   1331      1.19   thorpej 	return (m);
   1332       1.1       cgd }
   1333      1.91        ad 
   1334      1.91        ad void
   1335      1.91        ad solockretry(struct socket *so, kmutex_t *lock)
   1336      1.91        ad {
   1337      1.91        ad 
   1338      1.91        ad 	while (lock != so->so_lock) {
   1339      1.91        ad 		mutex_exit(lock);
   1340      1.91        ad 		lock = so->so_lock;
   1341      1.91        ad 		mutex_enter(lock);
   1342      1.91        ad 	}
   1343      1.91        ad }
   1344      1.91        ad 
   1345      1.91        ad bool
   1346      1.91        ad solocked(struct socket *so)
   1347      1.91        ad {
   1348      1.91        ad 
   1349      1.91        ad 	return mutex_owned(so->so_lock);
   1350      1.91        ad }
   1351      1.91        ad 
   1352      1.91        ad bool
   1353      1.91        ad solocked2(struct socket *so1, struct socket *so2)
   1354      1.91        ad {
   1355      1.91        ad 	kmutex_t *lock;
   1356      1.91        ad 
   1357      1.91        ad 	lock = so1->so_lock;
   1358      1.91        ad 	if (lock != so2->so_lock)
   1359      1.91        ad 		return false;
   1360      1.91        ad 	return mutex_owned(lock);
   1361      1.91        ad }
   1362      1.91        ad 
   1363      1.91        ad /*
   1364      1.91        ad  * Assign a default lock to a new socket.  For PRU_ATTACH, and done by
   1365      1.91        ad  * protocols that do not have special locking requirements.
   1366      1.91        ad  */
   1367      1.91        ad void
   1368      1.91        ad sosetlock(struct socket *so)
   1369      1.91        ad {
   1370      1.91        ad 	kmutex_t *lock;
   1371      1.91        ad 
   1372      1.91        ad 	if (so->so_lock == NULL) {
   1373      1.91        ad 		lock = softnet_lock;
   1374      1.91        ad 		so->so_lock = lock;
   1375      1.91        ad 		mutex_obj_hold(lock);
   1376      1.91        ad 		mutex_enter(lock);
   1377      1.91        ad 	}
   1378      1.91        ad 
   1379      1.91        ad 	/* In all cases, lock must be held on return from PRU_ATTACH. */
   1380      1.91        ad 	KASSERT(solocked(so));
   1381      1.91        ad }
   1382      1.91        ad 
   1383      1.91        ad /*
   1384      1.91        ad  * Set lock on sockbuf sb; sleep if lock is already held.
   1385      1.91        ad  * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
   1386      1.91        ad  * Returns error without lock if sleep is interrupted.
   1387      1.91        ad  */
   1388      1.91        ad int
   1389      1.91        ad sblock(struct sockbuf *sb, int wf)
   1390      1.91        ad {
   1391      1.91        ad 	struct socket *so;
   1392      1.91        ad 	kmutex_t *lock;
   1393      1.91        ad 	int error;
   1394      1.91        ad 
   1395      1.91        ad 	KASSERT(solocked(sb->sb_so));
   1396      1.91        ad 
   1397      1.91        ad 	for (;;) {
   1398      1.91        ad 		if (__predict_true((sb->sb_flags & SB_LOCK) == 0)) {
   1399      1.91        ad 			sb->sb_flags |= SB_LOCK;
   1400      1.91        ad 			return 0;
   1401      1.91        ad 		}
   1402      1.91        ad 		if (wf != M_WAITOK)
   1403      1.91        ad 			return EWOULDBLOCK;
   1404      1.91        ad 		so = sb->sb_so;
   1405      1.91        ad 		lock = so->so_lock;
   1406      1.91        ad 		if ((sb->sb_flags & SB_NOINTR) != 0) {
   1407      1.91        ad 			cv_wait(&so->so_cv, lock);
   1408      1.91        ad 			error = 0;
   1409      1.91        ad 		} else
   1410      1.91        ad 			error = cv_wait_sig(&so->so_cv, lock);
   1411      1.91        ad 		if (__predict_false(lock != so->so_lock))
   1412      1.91        ad 			solockretry(so, lock);
   1413      1.91        ad 		if (error != 0)
   1414      1.91        ad 			return error;
   1415      1.91        ad 	}
   1416      1.91        ad }
   1417      1.91        ad 
   1418      1.91        ad void
   1419      1.91        ad sbunlock(struct sockbuf *sb)
   1420      1.91        ad {
   1421      1.91        ad 	struct socket *so;
   1422      1.91        ad 
   1423      1.91        ad 	so = sb->sb_so;
   1424      1.91        ad 
   1425      1.91        ad 	KASSERT(solocked(so));
   1426      1.91        ad 	KASSERT((sb->sb_flags & SB_LOCK) != 0);
   1427      1.91        ad 
   1428      1.91        ad 	sb->sb_flags &= ~SB_LOCK;
   1429      1.91        ad 	cv_broadcast(&so->so_cv);
   1430      1.91        ad }
   1431      1.91        ad 
   1432      1.91        ad int
   1433  1.91.2.2      yamt sowait(struct socket *so, bool catch, int timo)
   1434      1.91        ad {
   1435      1.91        ad 	kmutex_t *lock;
   1436      1.91        ad 	int error;
   1437      1.91        ad 
   1438      1.91        ad 	KASSERT(solocked(so));
   1439  1.91.2.2      yamt 	KASSERT(catch || timo != 0);
   1440      1.91        ad 
   1441      1.91        ad 	lock = so->so_lock;
   1442  1.91.2.2      yamt 	if (catch)
   1443  1.91.2.2      yamt 		error = cv_timedwait_sig(&so->so_cv, lock, timo);
   1444  1.91.2.2      yamt 	else
   1445  1.91.2.2      yamt 		error = cv_timedwait(&so->so_cv, lock, timo);
   1446      1.91        ad 	if (__predict_false(lock != so->so_lock))
   1447      1.91        ad 		solockretry(so, lock);
   1448      1.91        ad 	return error;
   1449      1.91        ad }
   1450