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