Home | History | Annotate | Line # | Download | only in kern
uipc_socket2.c revision 1.70.12.1
      1  1.70.12.1      tron /*	$NetBSD: uipc_socket2.c,v 1.70.12.1 2006/05/24 15:50:41 tron Exp $	*/
      2        1.9       cgd 
      3        1.1       cgd /*
      4        1.7   mycroft  * Copyright (c) 1982, 1986, 1988, 1990, 1993
      5        1.7   mycroft  *	The Regents of the University of California.  All rights reserved.
      6        1.1       cgd  *
      7        1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8        1.1       cgd  * modification, are permitted provided that the following conditions
      9        1.1       cgd  * are met:
     10        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15       1.54       agc  * 3. Neither the name of the University nor the names of its contributors
     16        1.1       cgd  *    may be used to endorse or promote products derived from this software
     17        1.1       cgd  *    without specific prior written permission.
     18        1.1       cgd  *
     19        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29        1.1       cgd  * SUCH DAMAGE.
     30        1.1       cgd  *
     31       1.23      fvdl  *	@(#)uipc_socket2.c	8.2 (Berkeley) 2/14/95
     32        1.1       cgd  */
     33       1.42     lukem 
     34       1.42     lukem #include <sys/cdefs.h>
     35  1.70.12.1      tron __KERNEL_RCSID(0, "$NetBSD: uipc_socket2.c,v 1.70.12.1 2006/05/24 15:50:41 tron Exp $");
     36       1.51    martin 
     37       1.51    martin #include "opt_mbuftrace.h"
     38       1.58   thorpej #include "opt_sb_max.h"
     39        1.1       cgd 
     40        1.5   mycroft #include <sys/param.h>
     41        1.5   mycroft #include <sys/systm.h>
     42        1.5   mycroft #include <sys/proc.h>
     43        1.5   mycroft #include <sys/file.h>
     44        1.5   mycroft #include <sys/buf.h>
     45        1.5   mycroft #include <sys/malloc.h>
     46        1.5   mycroft #include <sys/mbuf.h>
     47        1.5   mycroft #include <sys/protosw.h>
     48       1.55  christos #include <sys/poll.h>
     49        1.5   mycroft #include <sys/socket.h>
     50        1.5   mycroft #include <sys/socketvar.h>
     51       1.11  christos #include <sys/signalvar.h>
     52  1.70.12.1      tron #include <sys/kauth.h>
     53        1.1       cgd 
     54        1.1       cgd /*
     55        1.1       cgd  * Primitive routines for operating on sockets and socket buffers
     56        1.1       cgd  */
     57        1.1       cgd 
     58        1.1       cgd /* strings for sleep message: */
     59       1.21   mycroft const char	netcon[] = "netcon";
     60       1.21   mycroft const char	netcls[] = "netcls";
     61       1.41     enami const char	netio[] = "netio";
     62       1.41     enami const char	netlck[] = "netlck";
     63        1.1       cgd 
     64       1.58   thorpej u_long	sb_max = SB_MAX;	/* maximum socket buffer size */
     65       1.58   thorpej static u_long sb_max_adj;	/* adjusted sb_max */
     66       1.58   thorpej 
     67        1.1       cgd /*
     68        1.1       cgd  * Procedures to manipulate state flags of socket
     69        1.1       cgd  * and do appropriate wakeups.  Normal sequence from the
     70        1.1       cgd  * active (originating) side is that soisconnecting() is
     71        1.1       cgd  * called during processing of connect() call,
     72        1.1       cgd  * resulting in an eventual call to soisconnected() if/when the
     73        1.1       cgd  * connection is established.  When the connection is torn down
     74        1.1       cgd  * soisdisconnecting() is called during processing of disconnect() call,
     75        1.1       cgd  * and soisdisconnected() is called when the connection to the peer
     76        1.1       cgd  * is totally severed.  The semantics of these routines are such that
     77        1.1       cgd  * connectionless protocols can call soisconnected() and soisdisconnected()
     78        1.1       cgd  * only, bypassing the in-progress calls when setting up a ``connection''
     79        1.1       cgd  * takes no time.
     80        1.1       cgd  *
     81        1.1       cgd  * From the passive side, a socket is created with
     82        1.1       cgd  * two queues of sockets: so_q0 for connections in progress
     83        1.1       cgd  * and so_q for connections already made and awaiting user acceptance.
     84        1.1       cgd  * As a protocol is preparing incoming connections, it creates a socket
     85        1.1       cgd  * structure queued on so_q0 by calling sonewconn().  When the connection
     86        1.1       cgd  * is established, soisconnected() is called, and transfers the
     87        1.1       cgd  * socket structure to so_q, making it available to accept().
     88       1.66     perry  *
     89        1.1       cgd  * If a socket is closed with sockets on either
     90        1.1       cgd  * so_q0 or so_q, these sockets are dropped.
     91        1.1       cgd  *
     92        1.1       cgd  * If higher level protocols are implemented in
     93        1.1       cgd  * the kernel, the wakeups done here will sometimes
     94        1.1       cgd  * cause software-interrupt process scheduling.
     95        1.1       cgd  */
     96        1.1       cgd 
     97        1.7   mycroft void
     98       1.37     lukem soisconnecting(struct socket *so)
     99        1.1       cgd {
    100        1.1       cgd 
    101        1.1       cgd 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
    102        1.1       cgd 	so->so_state |= SS_ISCONNECTING;
    103        1.1       cgd }
    104        1.1       cgd 
    105        1.7   mycroft void
    106       1.37     lukem soisconnected(struct socket *so)
    107        1.1       cgd {
    108       1.37     lukem 	struct socket	*head;
    109        1.1       cgd 
    110       1.37     lukem 	head = so->so_head;
    111        1.1       cgd 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
    112        1.1       cgd 	so->so_state |= SS_ISCONNECTED;
    113        1.1       cgd 	if (head && soqremque(so, 0)) {
    114        1.1       cgd 		soqinsque(head, so, 1);
    115        1.1       cgd 		sorwakeup(head);
    116        1.1       cgd 		wakeup((caddr_t)&head->so_timeo);
    117        1.1       cgd 	} else {
    118        1.1       cgd 		wakeup((caddr_t)&so->so_timeo);
    119        1.1       cgd 		sorwakeup(so);
    120        1.1       cgd 		sowwakeup(so);
    121        1.1       cgd 	}
    122        1.1       cgd }
    123        1.1       cgd 
    124        1.7   mycroft void
    125       1.37     lukem soisdisconnecting(struct socket *so)
    126        1.1       cgd {
    127        1.1       cgd 
    128        1.1       cgd 	so->so_state &= ~SS_ISCONNECTING;
    129        1.1       cgd 	so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
    130        1.1       cgd 	wakeup((caddr_t)&so->so_timeo);
    131        1.1       cgd 	sowwakeup(so);
    132        1.1       cgd 	sorwakeup(so);
    133        1.1       cgd }
    134        1.1       cgd 
    135        1.7   mycroft void
    136       1.37     lukem soisdisconnected(struct socket *so)
    137        1.1       cgd {
    138        1.1       cgd 
    139        1.1       cgd 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
    140       1.27   mycroft 	so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
    141        1.1       cgd 	wakeup((caddr_t)&so->so_timeo);
    142        1.1       cgd 	sowwakeup(so);
    143        1.1       cgd 	sorwakeup(so);
    144        1.1       cgd }
    145        1.1       cgd 
    146        1.1       cgd /*
    147        1.1       cgd  * When an attempt at a new connection is noted on a socket
    148        1.1       cgd  * which accepts connections, sonewconn is called.  If the
    149        1.1       cgd  * connection is possible (subject to space constraints, etc.)
    150        1.1       cgd  * then we allocate a new structure, propoerly linked into the
    151        1.1       cgd  * data structure of the original socket, and return this.
    152        1.1       cgd  * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
    153        1.1       cgd  *
    154        1.1       cgd  * Currently, sonewconn() is defined as sonewconn1() in socketvar.h
    155        1.1       cgd  * to catch calls that are missing the (new) second parameter.
    156        1.1       cgd  */
    157        1.1       cgd struct socket *
    158       1.37     lukem sonewconn1(struct socket *head, int connstatus)
    159        1.1       cgd {
    160       1.37     lukem 	struct socket	*so;
    161       1.37     lukem 	int		soqueue;
    162        1.1       cgd 
    163       1.37     lukem 	soqueue = connstatus ? 1 : 0;
    164        1.1       cgd 	if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2)
    165        1.1       cgd 		return ((struct socket *)0);
    166       1.25   thorpej 	so = pool_get(&socket_pool, PR_NOWAIT);
    167       1.66     perry 	if (so == NULL)
    168       1.25   thorpej 		return (NULL);
    169       1.26     perry 	memset((caddr_t)so, 0, sizeof(*so));
    170        1.1       cgd 	so->so_type = head->so_type;
    171        1.1       cgd 	so->so_options = head->so_options &~ SO_ACCEPTCONN;
    172        1.1       cgd 	so->so_linger = head->so_linger;
    173        1.1       cgd 	so->so_state = head->so_state | SS_NOFDREF;
    174        1.1       cgd 	so->so_proto = head->so_proto;
    175        1.1       cgd 	so->so_timeo = head->so_timeo;
    176        1.1       cgd 	so->so_pgid = head->so_pgid;
    177       1.24      matt 	so->so_send = head->so_send;
    178       1.24      matt 	so->so_receive = head->so_receive;
    179       1.67  christos 	so->so_uidinfo = head->so_uidinfo;
    180       1.49      matt #ifdef MBUFTRACE
    181       1.49      matt 	so->so_mowner = head->so_mowner;
    182       1.49      matt 	so->so_rcv.sb_mowner = head->so_rcv.sb_mowner;
    183       1.49      matt 	so->so_snd.sb_mowner = head->so_snd.sb_mowner;
    184       1.49      matt #endif
    185        1.1       cgd 	(void) soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat);
    186        1.1       cgd 	soqinsque(head, so, soqueue);
    187        1.1       cgd 	if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH,
    188       1.12   mycroft 	    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
    189       1.69  christos 	    (struct lwp *)0)) {
    190        1.1       cgd 		(void) soqremque(so, soqueue);
    191       1.25   thorpej 		pool_put(&socket_pool, so);
    192       1.25   thorpej 		return (NULL);
    193        1.1       cgd 	}
    194        1.1       cgd 	if (connstatus) {
    195        1.1       cgd 		sorwakeup(head);
    196        1.1       cgd 		wakeup((caddr_t)&head->so_timeo);
    197        1.1       cgd 		so->so_state |= connstatus;
    198        1.1       cgd 	}
    199        1.1       cgd 	return (so);
    200        1.1       cgd }
    201        1.1       cgd 
    202        1.7   mycroft void
    203       1.37     lukem soqinsque(struct socket *head, struct socket *so, int q)
    204        1.1       cgd {
    205        1.1       cgd 
    206       1.22   thorpej #ifdef DIAGNOSTIC
    207       1.22   thorpej 	if (so->so_onq != NULL)
    208       1.22   thorpej 		panic("soqinsque");
    209       1.22   thorpej #endif
    210       1.22   thorpej 
    211        1.1       cgd 	so->so_head = head;
    212        1.1       cgd 	if (q == 0) {
    213        1.1       cgd 		head->so_q0len++;
    214       1.22   thorpej 		so->so_onq = &head->so_q0;
    215        1.1       cgd 	} else {
    216        1.1       cgd 		head->so_qlen++;
    217       1.22   thorpej 		so->so_onq = &head->so_q;
    218        1.1       cgd 	}
    219       1.22   thorpej 	TAILQ_INSERT_TAIL(so->so_onq, so, so_qe);
    220        1.1       cgd }
    221        1.1       cgd 
    222        1.7   mycroft int
    223       1.37     lukem soqremque(struct socket *so, int q)
    224        1.1       cgd {
    225       1.37     lukem 	struct socket	*head;
    226        1.1       cgd 
    227       1.37     lukem 	head = so->so_head;
    228       1.22   thorpej 	if (q == 0) {
    229       1.22   thorpej 		if (so->so_onq != &head->so_q0)
    230       1.17   thorpej 			return (0);
    231        1.1       cgd 		head->so_q0len--;
    232        1.1       cgd 	} else {
    233       1.22   thorpej 		if (so->so_onq != &head->so_q)
    234       1.22   thorpej 			return (0);
    235        1.1       cgd 		head->so_qlen--;
    236        1.1       cgd 	}
    237       1.22   thorpej 	TAILQ_REMOVE(so->so_onq, so, so_qe);
    238       1.22   thorpej 	so->so_onq = NULL;
    239       1.22   thorpej 	so->so_head = NULL;
    240        1.1       cgd 	return (1);
    241        1.1       cgd }
    242        1.1       cgd 
    243        1.1       cgd /*
    244        1.1       cgd  * Socantsendmore indicates that no more data will be sent on the
    245        1.1       cgd  * socket; it would normally be applied to a socket when the user
    246        1.1       cgd  * informs the system that no more data is to be sent, by the protocol
    247        1.1       cgd  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
    248        1.1       cgd  * will be received, and will normally be applied to the socket by a
    249        1.1       cgd  * protocol when it detects that the peer will send no more data.
    250        1.1       cgd  * Data queued for reading in the socket may yet be read.
    251        1.1       cgd  */
    252        1.1       cgd 
    253        1.4    andrew void
    254       1.37     lukem socantsendmore(struct socket *so)
    255        1.1       cgd {
    256        1.1       cgd 
    257        1.1       cgd 	so->so_state |= SS_CANTSENDMORE;
    258        1.1       cgd 	sowwakeup(so);
    259        1.1       cgd }
    260        1.1       cgd 
    261        1.4    andrew void
    262       1.37     lukem socantrcvmore(struct socket *so)
    263        1.1       cgd {
    264        1.1       cgd 
    265        1.1       cgd 	so->so_state |= SS_CANTRCVMORE;
    266        1.1       cgd 	sorwakeup(so);
    267        1.1       cgd }
    268        1.1       cgd 
    269        1.1       cgd /*
    270        1.1       cgd  * Wait for data to arrive at/drain from a socket buffer.
    271        1.1       cgd  */
    272        1.7   mycroft int
    273       1.37     lukem sbwait(struct sockbuf *sb)
    274        1.1       cgd {
    275        1.1       cgd 
    276        1.1       cgd 	sb->sb_flags |= SB_WAIT;
    277        1.1       cgd 	return (tsleep((caddr_t)&sb->sb_cc,
    278        1.1       cgd 	    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, netio,
    279        1.1       cgd 	    sb->sb_timeo));
    280        1.1       cgd }
    281        1.1       cgd 
    282       1.66     perry /*
    283        1.1       cgd  * Lock a sockbuf already known to be locked;
    284        1.1       cgd  * return any error returned from sleep (EINTR).
    285        1.1       cgd  */
    286        1.7   mycroft int
    287       1.37     lukem sb_lock(struct sockbuf *sb)
    288        1.1       cgd {
    289       1.37     lukem 	int	error;
    290        1.1       cgd 
    291        1.1       cgd 	while (sb->sb_flags & SB_LOCK) {
    292        1.1       cgd 		sb->sb_flags |= SB_WANT;
    293       1.66     perry 		error = tsleep((caddr_t)&sb->sb_flags,
    294       1.41     enami 		    (sb->sb_flags & SB_NOINTR) ?  PSOCK : PSOCK|PCATCH,
    295       1.41     enami 		    netlck, 0);
    296       1.11  christos 		if (error)
    297        1.1       cgd 			return (error);
    298        1.1       cgd 	}
    299        1.1       cgd 	sb->sb_flags |= SB_LOCK;
    300        1.1       cgd 	return (0);
    301        1.1       cgd }
    302        1.1       cgd 
    303        1.1       cgd /*
    304        1.1       cgd  * Wakeup processes waiting on a socket buffer.
    305        1.1       cgd  * Do asynchronous notification via SIGIO
    306       1.39      manu  * if the socket buffer has the SB_ASYNC flag set.
    307        1.1       cgd  */
    308        1.7   mycroft void
    309       1.55  christos sowakeup(struct socket *so, struct sockbuf *sb, int code)
    310        1.1       cgd {
    311       1.48  jdolecek 	selnotify(&sb->sb_sel, 0);
    312        1.7   mycroft 	sb->sb_flags &= ~SB_SEL;
    313        1.1       cgd 	if (sb->sb_flags & SB_WAIT) {
    314        1.1       cgd 		sb->sb_flags &= ~SB_WAIT;
    315        1.1       cgd 		wakeup((caddr_t)&sb->sb_cc);
    316        1.1       cgd 	}
    317       1.39      manu 	if (sb->sb_flags & SB_ASYNC) {
    318       1.56  jdolecek 		int band;
    319       1.57  christos 		if (code == POLL_IN)
    320       1.57  christos 			band = POLLIN|POLLRDNORM;
    321       1.57  christos 		else
    322       1.57  christos 			band = POLLOUT|POLLWRNORM;
    323       1.57  christos 		fownsignal(so->so_pgid, SIGIO, code, band, so);
    324        1.1       cgd 	}
    325       1.24      matt 	if (sb->sb_flags & SB_UPCALL)
    326       1.24      matt 		(*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
    327        1.1       cgd }
    328        1.1       cgd 
    329        1.1       cgd /*
    330        1.1       cgd  * Socket buffer (struct sockbuf) utility routines.
    331        1.1       cgd  *
    332        1.1       cgd  * Each socket contains two socket buffers: one for sending data and
    333        1.1       cgd  * one for receiving data.  Each buffer contains a queue of mbufs,
    334        1.1       cgd  * information about the number of mbufs and amount of data in the
    335       1.13   mycroft  * queue, and other fields allowing poll() statements and notification
    336        1.1       cgd  * on data availability to be implemented.
    337        1.1       cgd  *
    338        1.1       cgd  * Data stored in a socket buffer is maintained as a list of records.
    339        1.1       cgd  * Each record is a list of mbufs chained together with the m_next
    340        1.1       cgd  * field.  Records are chained together with the m_nextpkt field. The upper
    341        1.1       cgd  * level routine soreceive() expects the following conventions to be
    342        1.1       cgd  * observed when placing information in the receive buffer:
    343        1.1       cgd  *
    344        1.1       cgd  * 1. If the protocol requires each message be preceded by the sender's
    345        1.1       cgd  *    name, then a record containing that name must be present before
    346        1.1       cgd  *    any associated data (mbuf's must be of type MT_SONAME).
    347        1.1       cgd  * 2. If the protocol supports the exchange of ``access rights'' (really
    348        1.1       cgd  *    just additional data associated with the message), and there are
    349        1.1       cgd  *    ``rights'' to be received, then a record containing this data
    350       1.10   mycroft  *    should be present (mbuf's must be of type MT_CONTROL).
    351        1.1       cgd  * 3. If a name or rights record exists, then it must be followed by
    352        1.1       cgd  *    a data record, perhaps of zero length.
    353        1.1       cgd  *
    354        1.1       cgd  * Before using a new socket structure it is first necessary to reserve
    355        1.1       cgd  * buffer space to the socket, by calling sbreserve().  This should commit
    356        1.1       cgd  * some of the available buffer space in the system buffer pool for the
    357        1.1       cgd  * socket (currently, it does nothing but enforce limits).  The space
    358        1.1       cgd  * should be released by calling sbrelease() when the socket is destroyed.
    359        1.1       cgd  */
    360        1.1       cgd 
    361        1.7   mycroft int
    362       1.58   thorpej sb_max_set(u_long new_sbmax)
    363       1.58   thorpej {
    364       1.58   thorpej 	int s;
    365       1.58   thorpej 
    366       1.58   thorpej 	if (new_sbmax < (16 * 1024))
    367       1.58   thorpej 		return (EINVAL);
    368       1.58   thorpej 
    369       1.58   thorpej 	s = splsoftnet();
    370       1.58   thorpej 	sb_max = new_sbmax;
    371       1.58   thorpej 	sb_max_adj = (u_quad_t)new_sbmax * MCLBYTES / (MSIZE + MCLBYTES);
    372       1.58   thorpej 	splx(s);
    373       1.58   thorpej 
    374       1.58   thorpej 	return (0);
    375       1.58   thorpej }
    376       1.58   thorpej 
    377       1.58   thorpej int
    378       1.37     lukem soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
    379        1.1       cgd {
    380        1.1       cgd 
    381       1.59  christos 	if (sbreserve(&so->so_snd, sndcc, so) == 0)
    382        1.1       cgd 		goto bad;
    383       1.59  christos 	if (sbreserve(&so->so_rcv, rcvcc, so) == 0)
    384        1.1       cgd 		goto bad2;
    385        1.1       cgd 	if (so->so_rcv.sb_lowat == 0)
    386        1.1       cgd 		so->so_rcv.sb_lowat = 1;
    387        1.1       cgd 	if (so->so_snd.sb_lowat == 0)
    388        1.1       cgd 		so->so_snd.sb_lowat = MCLBYTES;
    389        1.1       cgd 	if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
    390        1.1       cgd 		so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
    391        1.1       cgd 	return (0);
    392       1.37     lukem  bad2:
    393       1.59  christos 	sbrelease(&so->so_snd, so);
    394       1.37     lukem  bad:
    395        1.1       cgd 	return (ENOBUFS);
    396        1.1       cgd }
    397        1.1       cgd 
    398        1.1       cgd /*
    399        1.1       cgd  * Allot mbufs to a sockbuf.
    400        1.1       cgd  * Attempt to scale mbmax so that mbcnt doesn't become limiting
    401        1.1       cgd  * if buffering efficiency is near the normal case.
    402        1.1       cgd  */
    403        1.7   mycroft int
    404       1.59  christos sbreserve(struct sockbuf *sb, u_long cc, struct socket *so)
    405        1.1       cgd {
    406       1.59  christos 	struct proc *p = curproc; /* XXX */
    407       1.62  christos 	rlim_t maxcc;
    408       1.67  christos 	struct uidinfo *uidinfo;
    409        1.1       cgd 
    410       1.58   thorpej 	KDASSERT(sb_max_adj != 0);
    411       1.58   thorpej 	if (cc == 0 || cc > sb_max_adj)
    412        1.1       cgd 		return (0);
    413       1.60      matt 	if (so) {
    414  1.70.12.1      tron 		if (p && kauth_cred_geteuid(p->p_cred) == so->so_uidinfo->ui_uid)
    415       1.60      matt 			maxcc = p->p_rlimit[RLIMIT_SBSIZE].rlim_cur;
    416       1.60      matt 		else
    417       1.60      matt 			maxcc = RLIM_INFINITY;
    418       1.67  christos 		uidinfo = so->so_uidinfo;
    419       1.62  christos 	} else {
    420       1.67  christos 		uidinfo = uid_find(0);	/* XXX: nothing better */
    421       1.62  christos 		maxcc = RLIM_INFINITY;
    422       1.60      matt 	}
    423       1.67  christos 	if (!chgsbsize(uidinfo, &sb->sb_hiwat, cc, maxcc))
    424       1.62  christos 		return 0;
    425        1.1       cgd 	sb->sb_mbmax = min(cc * 2, sb_max);
    426        1.1       cgd 	if (sb->sb_lowat > sb->sb_hiwat)
    427        1.1       cgd 		sb->sb_lowat = sb->sb_hiwat;
    428        1.1       cgd 	return (1);
    429        1.1       cgd }
    430        1.1       cgd 
    431        1.1       cgd /*
    432        1.1       cgd  * Free mbufs held by a socket, and reserved mbuf space.
    433        1.1       cgd  */
    434        1.7   mycroft void
    435       1.59  christos sbrelease(struct sockbuf *sb, struct socket *so)
    436        1.1       cgd {
    437        1.1       cgd 
    438        1.1       cgd 	sbflush(sb);
    439       1.67  christos 	(void)chgsbsize(so->so_uidinfo, &sb->sb_hiwat, 0,
    440       1.59  christos 	    RLIM_INFINITY);
    441       1.59  christos 	sb->sb_mbmax = 0;
    442        1.1       cgd }
    443        1.1       cgd 
    444        1.1       cgd /*
    445        1.1       cgd  * Routines to add and remove
    446        1.1       cgd  * data from an mbuf queue.
    447        1.1       cgd  *
    448        1.1       cgd  * The routines sbappend() or sbappendrecord() are normally called to
    449        1.1       cgd  * append new mbufs to a socket buffer, after checking that adequate
    450        1.1       cgd  * space is available, comparing the function sbspace() with the amount
    451        1.1       cgd  * of data to be added.  sbappendrecord() differs from sbappend() in
    452        1.1       cgd  * that data supplied is treated as the beginning of a new record.
    453        1.1       cgd  * To place a sender's address, optional access rights, and data in a
    454        1.1       cgd  * socket receive buffer, sbappendaddr() should be used.  To place
    455        1.1       cgd  * access rights and data in a socket receive buffer, sbappendrights()
    456        1.1       cgd  * should be used.  In either case, the new data begins a new record.
    457        1.1       cgd  * Note that unlike sbappend() and sbappendrecord(), these routines check
    458        1.1       cgd  * for the caller that there will be enough space to store the data.
    459        1.1       cgd  * Each fails if there is not enough space, or if it cannot find mbufs
    460        1.1       cgd  * to store additional information in.
    461        1.1       cgd  *
    462        1.1       cgd  * Reliable protocols may use the socket send buffer to hold data
    463        1.1       cgd  * awaiting acknowledgement.  Data is normally copied from a socket
    464        1.1       cgd  * send buffer in a protocol with m_copy for output to a peer,
    465        1.1       cgd  * and then removing the data from the socket buffer with sbdrop()
    466        1.1       cgd  * or sbdroprecord() when the data is acknowledged by the peer.
    467        1.1       cgd  */
    468        1.1       cgd 
    469       1.43   thorpej #ifdef SOCKBUF_DEBUG
    470       1.43   thorpej void
    471       1.43   thorpej sblastrecordchk(struct sockbuf *sb, const char *where)
    472       1.43   thorpej {
    473       1.43   thorpej 	struct mbuf *m = sb->sb_mb;
    474       1.43   thorpej 
    475       1.43   thorpej 	while (m && m->m_nextpkt)
    476       1.43   thorpej 		m = m->m_nextpkt;
    477       1.43   thorpej 
    478       1.43   thorpej 	if (m != sb->sb_lastrecord) {
    479       1.43   thorpej 		printf("sblastrecordchk: sb_mb %p sb_lastrecord %p last %p\n",
    480       1.43   thorpej 		    sb->sb_mb, sb->sb_lastrecord, m);
    481       1.43   thorpej 		printf("packet chain:\n");
    482       1.43   thorpej 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
    483       1.43   thorpej 			printf("\t%p\n", m);
    484       1.47    provos 		panic("sblastrecordchk from %s", where);
    485       1.43   thorpej 	}
    486       1.43   thorpej }
    487       1.43   thorpej 
    488       1.43   thorpej void
    489       1.43   thorpej sblastmbufchk(struct sockbuf *sb, const char *where)
    490       1.43   thorpej {
    491       1.43   thorpej 	struct mbuf *m = sb->sb_mb;
    492       1.43   thorpej 	struct mbuf *n;
    493       1.43   thorpej 
    494       1.43   thorpej 	while (m && m->m_nextpkt)
    495       1.43   thorpej 		m = m->m_nextpkt;
    496       1.43   thorpej 
    497       1.43   thorpej 	while (m && m->m_next)
    498       1.43   thorpej 		m = m->m_next;
    499       1.43   thorpej 
    500       1.43   thorpej 	if (m != sb->sb_mbtail) {
    501       1.43   thorpej 		printf("sblastmbufchk: sb_mb %p sb_mbtail %p last %p\n",
    502       1.43   thorpej 		    sb->sb_mb, sb->sb_mbtail, m);
    503       1.43   thorpej 		printf("packet tree:\n");
    504       1.43   thorpej 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
    505       1.43   thorpej 			printf("\t");
    506       1.43   thorpej 			for (n = m; n != NULL; n = n->m_next)
    507       1.43   thorpej 				printf("%p ", n);
    508       1.43   thorpej 			printf("\n");
    509       1.43   thorpej 		}
    510       1.43   thorpej 		panic("sblastmbufchk from %s", where);
    511       1.43   thorpej 	}
    512       1.43   thorpej }
    513       1.43   thorpej #endif /* SOCKBUF_DEBUG */
    514       1.43   thorpej 
    515       1.63  jonathan /*
    516       1.63  jonathan  * Link a chain of records onto a socket buffer
    517       1.63  jonathan  */
    518       1.63  jonathan #define	SBLINKRECORDCHAIN(sb, m0, mlast)				\
    519       1.43   thorpej do {									\
    520       1.43   thorpej 	if ((sb)->sb_lastrecord != NULL)				\
    521       1.43   thorpej 		(sb)->sb_lastrecord->m_nextpkt = (m0);			\
    522       1.43   thorpej 	else								\
    523       1.43   thorpej 		(sb)->sb_mb = (m0);					\
    524       1.63  jonathan 	(sb)->sb_lastrecord = (mlast);					\
    525       1.43   thorpej } while (/*CONSTCOND*/0)
    526       1.43   thorpej 
    527       1.63  jonathan 
    528       1.63  jonathan #define	SBLINKRECORD(sb, m0)						\
    529       1.63  jonathan     SBLINKRECORDCHAIN(sb, m0, m0)
    530       1.63  jonathan 
    531        1.1       cgd /*
    532        1.1       cgd  * Append mbuf chain m to the last record in the
    533        1.1       cgd  * socket buffer sb.  The additional space associated
    534        1.1       cgd  * the mbuf chain is recorded in sb.  Empty mbufs are
    535        1.1       cgd  * discarded and mbufs are compacted where possible.
    536        1.1       cgd  */
    537        1.7   mycroft void
    538       1.37     lukem sbappend(struct sockbuf *sb, struct mbuf *m)
    539        1.1       cgd {
    540       1.37     lukem 	struct mbuf	*n;
    541        1.1       cgd 
    542        1.1       cgd 	if (m == 0)
    543        1.1       cgd 		return;
    544       1.43   thorpej 
    545       1.49      matt #ifdef MBUFTRACE
    546       1.65  jonathan 	m_claimm(m, sb->sb_mowner);
    547       1.49      matt #endif
    548       1.49      matt 
    549       1.43   thorpej 	SBLASTRECORDCHK(sb, "sbappend 1");
    550       1.43   thorpej 
    551       1.43   thorpej 	if ((n = sb->sb_lastrecord) != NULL) {
    552       1.43   thorpej 		/*
    553       1.43   thorpej 		 * XXX Would like to simply use sb_mbtail here, but
    554       1.43   thorpej 		 * XXX I need to verify that I won't miss an EOR that
    555       1.43   thorpej 		 * XXX way.
    556       1.43   thorpej 		 */
    557        1.1       cgd 		do {
    558        1.1       cgd 			if (n->m_flags & M_EOR) {
    559        1.1       cgd 				sbappendrecord(sb, m); /* XXXXXX!!!! */
    560        1.1       cgd 				return;
    561        1.1       cgd 			}
    562        1.1       cgd 		} while (n->m_next && (n = n->m_next));
    563       1.43   thorpej 	} else {
    564       1.43   thorpej 		/*
    565       1.43   thorpej 		 * If this is the first record in the socket buffer, it's
    566       1.43   thorpej 		 * also the last record.
    567       1.43   thorpej 		 */
    568       1.43   thorpej 		sb->sb_lastrecord = m;
    569        1.1       cgd 	}
    570        1.1       cgd 	sbcompress(sb, m, n);
    571       1.43   thorpej 	SBLASTRECORDCHK(sb, "sbappend 2");
    572       1.43   thorpej }
    573       1.43   thorpej 
    574       1.43   thorpej /*
    575       1.43   thorpej  * This version of sbappend() should only be used when the caller
    576       1.43   thorpej  * absolutely knows that there will never be more than one record
    577       1.43   thorpej  * in the socket buffer, that is, a stream protocol (such as TCP).
    578       1.43   thorpej  */
    579       1.43   thorpej void
    580       1.44   thorpej sbappendstream(struct sockbuf *sb, struct mbuf *m)
    581       1.43   thorpej {
    582       1.43   thorpej 
    583       1.43   thorpej 	KDASSERT(m->m_nextpkt == NULL);
    584       1.43   thorpej 	KASSERT(sb->sb_mb == sb->sb_lastrecord);
    585       1.43   thorpej 
    586       1.43   thorpej 	SBLASTMBUFCHK(sb, __func__);
    587       1.43   thorpej 
    588       1.49      matt #ifdef MBUFTRACE
    589       1.65  jonathan 	m_claimm(m, sb->sb_mowner);
    590       1.49      matt #endif
    591       1.49      matt 
    592       1.43   thorpej 	sbcompress(sb, m, sb->sb_mbtail);
    593       1.43   thorpej 
    594       1.43   thorpej 	sb->sb_lastrecord = sb->sb_mb;
    595       1.43   thorpej 	SBLASTRECORDCHK(sb, __func__);
    596        1.1       cgd }
    597        1.1       cgd 
    598        1.1       cgd #ifdef SOCKBUF_DEBUG
    599        1.7   mycroft void
    600       1.37     lukem sbcheck(struct sockbuf *sb)
    601        1.1       cgd {
    602       1.37     lukem 	struct mbuf	*m;
    603       1.43   thorpej 	u_long		len, mbcnt;
    604        1.1       cgd 
    605       1.37     lukem 	len = 0;
    606       1.37     lukem 	mbcnt = 0;
    607        1.1       cgd 	for (m = sb->sb_mb; m; m = m->m_next) {
    608        1.1       cgd 		len += m->m_len;
    609        1.1       cgd 		mbcnt += MSIZE;
    610        1.1       cgd 		if (m->m_flags & M_EXT)
    611        1.1       cgd 			mbcnt += m->m_ext.ext_size;
    612        1.1       cgd 		if (m->m_nextpkt)
    613        1.1       cgd 			panic("sbcheck nextpkt");
    614        1.1       cgd 	}
    615        1.1       cgd 	if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
    616       1.43   thorpej 		printf("cc %lu != %lu || mbcnt %lu != %lu\n", len, sb->sb_cc,
    617        1.1       cgd 		    mbcnt, sb->sb_mbcnt);
    618        1.1       cgd 		panic("sbcheck");
    619        1.1       cgd 	}
    620        1.1       cgd }
    621        1.1       cgd #endif
    622        1.1       cgd 
    623        1.1       cgd /*
    624        1.1       cgd  * As above, except the mbuf chain
    625        1.1       cgd  * begins a new record.
    626        1.1       cgd  */
    627        1.7   mycroft void
    628       1.37     lukem sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
    629        1.1       cgd {
    630       1.37     lukem 	struct mbuf	*m;
    631        1.1       cgd 
    632        1.1       cgd 	if (m0 == 0)
    633        1.1       cgd 		return;
    634       1.43   thorpej 
    635       1.49      matt #ifdef MBUFTRACE
    636       1.65  jonathan 	m_claimm(m0, sb->sb_mowner);
    637       1.49      matt #endif
    638        1.1       cgd 	/*
    639        1.1       cgd 	 * Put the first mbuf on the queue.
    640        1.1       cgd 	 * Note this permits zero length records.
    641        1.1       cgd 	 */
    642        1.1       cgd 	sballoc(sb, m0);
    643       1.43   thorpej 	SBLASTRECORDCHK(sb, "sbappendrecord 1");
    644       1.43   thorpej 	SBLINKRECORD(sb, m0);
    645        1.1       cgd 	m = m0->m_next;
    646        1.1       cgd 	m0->m_next = 0;
    647        1.1       cgd 	if (m && (m0->m_flags & M_EOR)) {
    648        1.1       cgd 		m0->m_flags &= ~M_EOR;
    649        1.1       cgd 		m->m_flags |= M_EOR;
    650        1.1       cgd 	}
    651        1.1       cgd 	sbcompress(sb, m, m0);
    652       1.43   thorpej 	SBLASTRECORDCHK(sb, "sbappendrecord 2");
    653        1.1       cgd }
    654        1.1       cgd 
    655        1.1       cgd /*
    656        1.1       cgd  * As above except that OOB data
    657        1.1       cgd  * is inserted at the beginning of the sockbuf,
    658        1.1       cgd  * but after any other OOB data.
    659        1.1       cgd  */
    660        1.7   mycroft void
    661       1.37     lukem sbinsertoob(struct sockbuf *sb, struct mbuf *m0)
    662        1.1       cgd {
    663       1.37     lukem 	struct mbuf	*m, **mp;
    664        1.1       cgd 
    665        1.1       cgd 	if (m0 == 0)
    666        1.1       cgd 		return;
    667       1.43   thorpej 
    668       1.43   thorpej 	SBLASTRECORDCHK(sb, "sbinsertoob 1");
    669       1.43   thorpej 
    670       1.11  christos 	for (mp = &sb->sb_mb; (m = *mp) != NULL; mp = &((*mp)->m_nextpkt)) {
    671        1.1       cgd 	    again:
    672        1.1       cgd 		switch (m->m_type) {
    673        1.1       cgd 
    674        1.1       cgd 		case MT_OOBDATA:
    675        1.1       cgd 			continue;		/* WANT next train */
    676        1.1       cgd 
    677        1.1       cgd 		case MT_CONTROL:
    678       1.11  christos 			if ((m = m->m_next) != NULL)
    679        1.1       cgd 				goto again;	/* inspect THIS train further */
    680        1.1       cgd 		}
    681        1.1       cgd 		break;
    682        1.1       cgd 	}
    683        1.1       cgd 	/*
    684        1.1       cgd 	 * Put the first mbuf on the queue.
    685        1.1       cgd 	 * Note this permits zero length records.
    686        1.1       cgd 	 */
    687        1.1       cgd 	sballoc(sb, m0);
    688        1.1       cgd 	m0->m_nextpkt = *mp;
    689       1.43   thorpej 	if (*mp == NULL) {
    690       1.43   thorpej 		/* m0 is actually the new tail */
    691       1.43   thorpej 		sb->sb_lastrecord = m0;
    692       1.43   thorpej 	}
    693        1.1       cgd 	*mp = m0;
    694        1.1       cgd 	m = m0->m_next;
    695        1.1       cgd 	m0->m_next = 0;
    696        1.1       cgd 	if (m && (m0->m_flags & M_EOR)) {
    697        1.1       cgd 		m0->m_flags &= ~M_EOR;
    698        1.1       cgd 		m->m_flags |= M_EOR;
    699        1.1       cgd 	}
    700        1.1       cgd 	sbcompress(sb, m, m0);
    701       1.43   thorpej 	SBLASTRECORDCHK(sb, "sbinsertoob 2");
    702        1.1       cgd }
    703        1.1       cgd 
    704        1.1       cgd /*
    705        1.1       cgd  * Append address and data, and optionally, control (ancillary) data
    706        1.1       cgd  * to the receive queue of a socket.  If present,
    707        1.1       cgd  * m0 must include a packet header with total length.
    708        1.1       cgd  * Returns 0 if no space in sockbuf or insufficient mbufs.
    709        1.1       cgd  */
    710        1.7   mycroft int
    711       1.61      matt sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa, struct mbuf *m0,
    712       1.37     lukem 	struct mbuf *control)
    713        1.1       cgd {
    714       1.43   thorpej 	struct mbuf	*m, *n, *nlast;
    715       1.50      fvdl 	int		space, len;
    716        1.1       cgd 
    717       1.37     lukem 	space = asa->sa_len;
    718       1.37     lukem 
    719       1.49      matt 	if (m0 != NULL) {
    720       1.49      matt 		if ((m0->m_flags & M_PKTHDR) == 0)
    721       1.49      matt 			panic("sbappendaddr");
    722        1.1       cgd 		space += m0->m_pkthdr.len;
    723       1.49      matt #ifdef MBUFTRACE
    724       1.65  jonathan 		m_claimm(m0, sb->sb_mowner);
    725       1.49      matt #endif
    726       1.49      matt 	}
    727        1.1       cgd 	for (n = control; n; n = n->m_next) {
    728        1.1       cgd 		space += n->m_len;
    729       1.49      matt 		MCLAIM(n, sb->sb_mowner);
    730        1.1       cgd 		if (n->m_next == 0)	/* keep pointer to last control buf */
    731        1.1       cgd 			break;
    732        1.1       cgd 	}
    733        1.1       cgd 	if (space > sbspace(sb))
    734        1.1       cgd 		return (0);
    735        1.1       cgd 	MGET(m, M_DONTWAIT, MT_SONAME);
    736        1.1       cgd 	if (m == 0)
    737        1.1       cgd 		return (0);
    738       1.49      matt 	MCLAIM(m, sb->sb_mowner);
    739       1.50      fvdl 	/*
    740       1.50      fvdl 	 * XXX avoid 'comparison always true' warning which isn't easily
    741       1.50      fvdl 	 * avoided.
    742       1.50      fvdl 	 */
    743       1.50      fvdl 	len = asa->sa_len;
    744       1.50      fvdl 	if (len > MLEN) {
    745       1.20   thorpej 		MEXTMALLOC(m, asa->sa_len, M_NOWAIT);
    746       1.20   thorpej 		if ((m->m_flags & M_EXT) == 0) {
    747       1.20   thorpej 			m_free(m);
    748       1.20   thorpej 			return (0);
    749       1.20   thorpej 		}
    750       1.20   thorpej 	}
    751        1.1       cgd 	m->m_len = asa->sa_len;
    752       1.68  christos 	memcpy(mtod(m, caddr_t), asa, asa->sa_len);
    753        1.1       cgd 	if (n)
    754        1.1       cgd 		n->m_next = m0;		/* concatenate data to control */
    755        1.1       cgd 	else
    756        1.1       cgd 		control = m0;
    757        1.1       cgd 	m->m_next = control;
    758       1.43   thorpej 
    759       1.43   thorpej 	SBLASTRECORDCHK(sb, "sbappendaddr 1");
    760       1.43   thorpej 
    761       1.43   thorpej 	for (n = m; n->m_next != NULL; n = n->m_next)
    762        1.1       cgd 		sballoc(sb, n);
    763       1.43   thorpej 	sballoc(sb, n);
    764       1.43   thorpej 	nlast = n;
    765       1.43   thorpej 	SBLINKRECORD(sb, m);
    766       1.43   thorpej 
    767       1.43   thorpej 	sb->sb_mbtail = nlast;
    768       1.43   thorpej 	SBLASTMBUFCHK(sb, "sbappendaddr");
    769       1.43   thorpej 
    770       1.43   thorpej 	SBLASTRECORDCHK(sb, "sbappendaddr 2");
    771       1.43   thorpej 
    772        1.1       cgd 	return (1);
    773        1.1       cgd }
    774        1.1       cgd 
    775       1.63  jonathan /*
    776       1.63  jonathan  * Helper for sbappendchainaddr: prepend a struct sockaddr* to
    777       1.63  jonathan  * an mbuf chain.
    778       1.63  jonathan  */
    779       1.70     perry static inline struct mbuf *
    780       1.64  jonathan m_prepend_sockaddr(struct sockbuf *sb, struct mbuf *m0,
    781       1.64  jonathan 		   const struct sockaddr *asa)
    782       1.63  jonathan {
    783       1.63  jonathan 	struct mbuf *m;
    784       1.64  jonathan 	const int salen = asa->sa_len;
    785       1.63  jonathan 
    786       1.63  jonathan 	/* only the first in each chain need be a pkthdr */
    787       1.63  jonathan 	MGETHDR(m, M_DONTWAIT, MT_SONAME);
    788       1.63  jonathan 	if (m == 0)
    789       1.63  jonathan 		return (0);
    790       1.63  jonathan 	MCLAIM(m, sb->sb_mowner);
    791       1.64  jonathan #ifdef notyet
    792       1.64  jonathan 	if (salen > MHLEN) {
    793       1.64  jonathan 		MEXTMALLOC(m, salen, M_NOWAIT);
    794       1.64  jonathan 		if ((m->m_flags & M_EXT) == 0) {
    795       1.64  jonathan 			m_free(m);
    796       1.64  jonathan 			return (0);
    797       1.64  jonathan 		}
    798       1.64  jonathan 	}
    799       1.64  jonathan #else
    800       1.64  jonathan 	KASSERT(salen <= MHLEN);
    801       1.64  jonathan #endif
    802       1.64  jonathan 	m->m_len = salen;
    803       1.68  christos 	memcpy(mtod(m, caddr_t), asa, salen);
    804       1.63  jonathan 	m->m_next = m0;
    805       1.64  jonathan 	m->m_pkthdr.len = salen + m0->m_pkthdr.len;
    806       1.63  jonathan 
    807       1.63  jonathan 	return m;
    808       1.63  jonathan }
    809       1.63  jonathan 
    810       1.63  jonathan int
    811       1.63  jonathan sbappendaddrchain(struct sockbuf *sb, const struct sockaddr *asa,
    812       1.63  jonathan 		  struct mbuf *m0, int sbprio)
    813       1.63  jonathan {
    814       1.63  jonathan 	int space;
    815       1.63  jonathan 	struct mbuf *m, *n, *n0, *nlast;
    816       1.63  jonathan 	int error;
    817       1.63  jonathan 
    818       1.63  jonathan 	/*
    819       1.63  jonathan 	 * XXX sbprio reserved for encoding priority of this* request:
    820       1.63  jonathan 	 *  SB_PRIO_NONE --> honour normal sb limits
    821       1.63  jonathan 	 *  SB_PRIO_ONESHOT_OVERFLOW --> if socket has any space,
    822       1.63  jonathan 	 *	take whole chain. Intended for large requests
    823       1.63  jonathan 	 *      that should be delivered atomically (all, or none).
    824       1.63  jonathan 	 * SB_PRIO_OVERDRAFT -- allow a small (2*MLEN) overflow
    825       1.63  jonathan 	 *       over normal socket limits, for messages indicating
    826       1.63  jonathan 	 *       buffer overflow in earlier normal/lower-priority messages
    827       1.63  jonathan 	 * SB_PRIO_BESTEFFORT -->  ignore limits entirely.
    828       1.63  jonathan 	 *       Intended for  kernel-generated messages only.
    829       1.63  jonathan 	 *        Up to generator to avoid total mbuf resource exhaustion.
    830       1.63  jonathan 	 */
    831       1.63  jonathan 	(void)sbprio;
    832       1.63  jonathan 
    833       1.63  jonathan 	if (m0 && (m0->m_flags & M_PKTHDR) == 0)
    834       1.63  jonathan 		panic("sbappendaddrchain");
    835       1.63  jonathan 
    836       1.63  jonathan 	space = sbspace(sb);
    837       1.66     perry 
    838       1.63  jonathan #ifdef notyet
    839       1.66     perry 	/*
    840       1.63  jonathan 	 * Enforce SB_PRIO_* limits as described above.
    841       1.63  jonathan 	 */
    842       1.63  jonathan #endif
    843       1.63  jonathan 
    844       1.63  jonathan 	n0 = NULL;
    845       1.63  jonathan 	nlast = NULL;
    846       1.63  jonathan 	for (m = m0; m; m = m->m_nextpkt) {
    847       1.63  jonathan 		struct mbuf *np;
    848       1.63  jonathan 
    849       1.64  jonathan #ifdef MBUFTRACE
    850       1.65  jonathan 		m_claimm(m, sb->sb_mowner);
    851       1.64  jonathan #endif
    852       1.64  jonathan 
    853       1.63  jonathan 		/* Prepend sockaddr to this record (m) of input chain m0 */
    854       1.64  jonathan 	  	n = m_prepend_sockaddr(sb, m, asa);
    855       1.63  jonathan 		if (n == NULL) {
    856       1.63  jonathan 			error = ENOBUFS;
    857       1.63  jonathan 			goto bad;
    858       1.63  jonathan 		}
    859       1.63  jonathan 
    860       1.63  jonathan 		/* Append record (asa+m) to end of new chain n0 */
    861       1.63  jonathan 		if (n0 == NULL) {
    862       1.63  jonathan 			n0 = n;
    863       1.63  jonathan 		} else {
    864       1.63  jonathan 			nlast->m_nextpkt = n;
    865       1.63  jonathan 		}
    866       1.63  jonathan 		/* Keep track of last record on new chain */
    867       1.63  jonathan 		nlast = n;
    868       1.63  jonathan 
    869       1.63  jonathan 		for (np = n; np; np = np->m_next)
    870       1.63  jonathan 			sballoc(sb, np);
    871       1.63  jonathan 	}
    872       1.63  jonathan 
    873       1.64  jonathan 	SBLASTRECORDCHK(sb, "sbappendaddrchain 1");
    874       1.64  jonathan 
    875       1.63  jonathan 	/* Drop the entire chain of (asa+m) records onto the socket */
    876       1.63  jonathan 	SBLINKRECORDCHAIN(sb, n0, nlast);
    877       1.64  jonathan 
    878       1.64  jonathan 	SBLASTRECORDCHK(sb, "sbappendaddrchain 2");
    879       1.64  jonathan 
    880       1.63  jonathan 	for (m = nlast; m->m_next; m = m->m_next)
    881       1.63  jonathan 		;
    882       1.63  jonathan 	sb->sb_mbtail = m;
    883       1.64  jonathan 	SBLASTMBUFCHK(sb, "sbappendaddrchain");
    884       1.64  jonathan 
    885       1.63  jonathan 	return (1);
    886       1.63  jonathan 
    887       1.63  jonathan bad:
    888       1.64  jonathan 	/*
    889       1.64  jonathan 	 * On error, free the prepended addreseses. For consistency
    890       1.64  jonathan 	 * with sbappendaddr(), leave it to our caller to free
    891       1.64  jonathan 	 * the input record chain passed to us as m0.
    892       1.64  jonathan 	 */
    893       1.64  jonathan 	while ((n = n0) != NULL) {
    894       1.64  jonathan 	  	struct mbuf *np;
    895       1.64  jonathan 
    896       1.64  jonathan 		/* Undo the sballoc() of this record */
    897       1.64  jonathan 		for (np = n; np; np = np->m_next)
    898       1.64  jonathan 			sbfree(sb, np);
    899       1.64  jonathan 
    900       1.64  jonathan 		n0 = n->m_nextpkt;	/* iterate at next prepended address */
    901       1.64  jonathan 		MFREE(n, np);		/* free prepended address (not data) */
    902       1.64  jonathan 	}
    903       1.66     perry 	return 0;
    904       1.63  jonathan }
    905       1.63  jonathan 
    906       1.63  jonathan 
    907        1.7   mycroft int
    908       1.37     lukem sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
    909        1.1       cgd {
    910       1.43   thorpej 	struct mbuf	*m, *mlast, *n;
    911       1.37     lukem 	int		space;
    912        1.1       cgd 
    913       1.37     lukem 	space = 0;
    914        1.1       cgd 	if (control == 0)
    915        1.1       cgd 		panic("sbappendcontrol");
    916        1.1       cgd 	for (m = control; ; m = m->m_next) {
    917        1.1       cgd 		space += m->m_len;
    918       1.49      matt 		MCLAIM(m, sb->sb_mowner);
    919        1.1       cgd 		if (m->m_next == 0)
    920        1.1       cgd 			break;
    921        1.1       cgd 	}
    922        1.1       cgd 	n = m;			/* save pointer to last control buffer */
    923       1.49      matt 	for (m = m0; m; m = m->m_next) {
    924       1.49      matt 		MCLAIM(m, sb->sb_mowner);
    925        1.1       cgd 		space += m->m_len;
    926       1.49      matt 	}
    927        1.1       cgd 	if (space > sbspace(sb))
    928        1.1       cgd 		return (0);
    929        1.1       cgd 	n->m_next = m0;			/* concatenate data to control */
    930       1.43   thorpej 
    931       1.43   thorpej 	SBLASTRECORDCHK(sb, "sbappendcontrol 1");
    932       1.43   thorpej 
    933       1.43   thorpej 	for (m = control; m->m_next != NULL; m = m->m_next)
    934        1.1       cgd 		sballoc(sb, m);
    935       1.43   thorpej 	sballoc(sb, m);
    936       1.43   thorpej 	mlast = m;
    937       1.43   thorpej 	SBLINKRECORD(sb, control);
    938       1.43   thorpej 
    939       1.43   thorpej 	sb->sb_mbtail = mlast;
    940       1.43   thorpej 	SBLASTMBUFCHK(sb, "sbappendcontrol");
    941       1.43   thorpej 
    942       1.43   thorpej 	SBLASTRECORDCHK(sb, "sbappendcontrol 2");
    943       1.43   thorpej 
    944        1.1       cgd 	return (1);
    945        1.1       cgd }
    946        1.1       cgd 
    947        1.1       cgd /*
    948        1.1       cgd  * Compress mbuf chain m into the socket
    949        1.1       cgd  * buffer sb following mbuf n.  If n
    950        1.1       cgd  * is null, the buffer is presumed empty.
    951        1.1       cgd  */
    952        1.7   mycroft void
    953       1.37     lukem sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
    954        1.1       cgd {
    955       1.37     lukem 	int		eor;
    956       1.37     lukem 	struct mbuf	*o;
    957        1.1       cgd 
    958       1.37     lukem 	eor = 0;
    959        1.1       cgd 	while (m) {
    960        1.1       cgd 		eor |= m->m_flags & M_EOR;
    961        1.1       cgd 		if (m->m_len == 0 &&
    962        1.1       cgd 		    (eor == 0 ||
    963        1.1       cgd 		     (((o = m->m_next) || (o = n)) &&
    964        1.1       cgd 		      o->m_type == m->m_type))) {
    965       1.46   thorpej 			if (sb->sb_lastrecord == m)
    966       1.46   thorpej 				sb->sb_lastrecord = m->m_next;
    967        1.1       cgd 			m = m_free(m);
    968        1.1       cgd 			continue;
    969        1.1       cgd 		}
    970       1.40   thorpej 		if (n && (n->m_flags & M_EOR) == 0 &&
    971       1.40   thorpej 		    /* M_TRAILINGSPACE() checks buffer writeability */
    972       1.40   thorpej 		    m->m_len <= MCLBYTES / 4 && /* XXX Don't copy too much */
    973       1.40   thorpej 		    m->m_len <= M_TRAILINGSPACE(n) &&
    974       1.40   thorpej 		    n->m_type == m->m_type) {
    975       1.26     perry 			memcpy(mtod(n, caddr_t) + n->m_len, mtod(m, caddr_t),
    976        1.1       cgd 			    (unsigned)m->m_len);
    977        1.1       cgd 			n->m_len += m->m_len;
    978        1.1       cgd 			sb->sb_cc += m->m_len;
    979        1.1       cgd 			m = m_free(m);
    980        1.1       cgd 			continue;
    981        1.1       cgd 		}
    982        1.1       cgd 		if (n)
    983        1.1       cgd 			n->m_next = m;
    984        1.1       cgd 		else
    985        1.1       cgd 			sb->sb_mb = m;
    986       1.43   thorpej 		sb->sb_mbtail = m;
    987        1.1       cgd 		sballoc(sb, m);
    988        1.1       cgd 		n = m;
    989        1.1       cgd 		m->m_flags &= ~M_EOR;
    990        1.1       cgd 		m = m->m_next;
    991        1.1       cgd 		n->m_next = 0;
    992        1.1       cgd 	}
    993        1.1       cgd 	if (eor) {
    994        1.1       cgd 		if (n)
    995        1.1       cgd 			n->m_flags |= eor;
    996        1.1       cgd 		else
    997       1.15  christos 			printf("semi-panic: sbcompress\n");
    998        1.1       cgd 	}
    999       1.43   thorpej 	SBLASTMBUFCHK(sb, __func__);
   1000        1.1       cgd }
   1001        1.1       cgd 
   1002        1.1       cgd /*
   1003        1.1       cgd  * Free all mbufs in a sockbuf.
   1004        1.1       cgd  * Check that all resources are reclaimed.
   1005        1.1       cgd  */
   1006        1.7   mycroft void
   1007       1.37     lukem sbflush(struct sockbuf *sb)
   1008        1.1       cgd {
   1009        1.1       cgd 
   1010       1.43   thorpej 	KASSERT((sb->sb_flags & SB_LOCK) == 0);
   1011       1.43   thorpej 
   1012        1.1       cgd 	while (sb->sb_mbcnt)
   1013        1.1       cgd 		sbdrop(sb, (int)sb->sb_cc);
   1014       1.43   thorpej 
   1015       1.43   thorpej 	KASSERT(sb->sb_cc == 0);
   1016       1.43   thorpej 	KASSERT(sb->sb_mb == NULL);
   1017       1.43   thorpej 	KASSERT(sb->sb_mbtail == NULL);
   1018       1.43   thorpej 	KASSERT(sb->sb_lastrecord == NULL);
   1019        1.1       cgd }
   1020        1.1       cgd 
   1021        1.1       cgd /*
   1022        1.1       cgd  * Drop data from (the front of) a sockbuf.
   1023        1.1       cgd  */
   1024        1.7   mycroft void
   1025       1.37     lukem sbdrop(struct sockbuf *sb, int len)
   1026        1.1       cgd {
   1027       1.37     lukem 	struct mbuf	*m, *mn, *next;
   1028        1.1       cgd 
   1029        1.1       cgd 	next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
   1030        1.1       cgd 	while (len > 0) {
   1031        1.1       cgd 		if (m == 0) {
   1032        1.1       cgd 			if (next == 0)
   1033        1.1       cgd 				panic("sbdrop");
   1034        1.1       cgd 			m = next;
   1035        1.1       cgd 			next = m->m_nextpkt;
   1036        1.1       cgd 			continue;
   1037        1.1       cgd 		}
   1038        1.1       cgd 		if (m->m_len > len) {
   1039        1.1       cgd 			m->m_len -= len;
   1040        1.1       cgd 			m->m_data += len;
   1041        1.1       cgd 			sb->sb_cc -= len;
   1042        1.1       cgd 			break;
   1043        1.1       cgd 		}
   1044        1.1       cgd 		len -= m->m_len;
   1045        1.1       cgd 		sbfree(sb, m);
   1046        1.1       cgd 		MFREE(m, mn);
   1047        1.1       cgd 		m = mn;
   1048        1.1       cgd 	}
   1049        1.1       cgd 	while (m && m->m_len == 0) {
   1050        1.1       cgd 		sbfree(sb, m);
   1051        1.1       cgd 		MFREE(m, mn);
   1052        1.1       cgd 		m = mn;
   1053        1.1       cgd 	}
   1054        1.1       cgd 	if (m) {
   1055        1.1       cgd 		sb->sb_mb = m;
   1056        1.1       cgd 		m->m_nextpkt = next;
   1057        1.1       cgd 	} else
   1058        1.1       cgd 		sb->sb_mb = next;
   1059       1.43   thorpej 	/*
   1060       1.45   thorpej 	 * First part is an inline SB_EMPTY_FIXUP().  Second part
   1061       1.43   thorpej 	 * makes sure sb_lastrecord is up-to-date if we dropped
   1062       1.43   thorpej 	 * part of the last record.
   1063       1.43   thorpej 	 */
   1064       1.43   thorpej 	m = sb->sb_mb;
   1065       1.43   thorpej 	if (m == NULL) {
   1066       1.43   thorpej 		sb->sb_mbtail = NULL;
   1067       1.43   thorpej 		sb->sb_lastrecord = NULL;
   1068       1.43   thorpej 	} else if (m->m_nextpkt == NULL)
   1069       1.43   thorpej 		sb->sb_lastrecord = m;
   1070        1.1       cgd }
   1071        1.1       cgd 
   1072        1.1       cgd /*
   1073        1.1       cgd  * Drop a record off the front of a sockbuf
   1074        1.1       cgd  * and move the next record to the front.
   1075        1.1       cgd  */
   1076        1.7   mycroft void
   1077       1.37     lukem sbdroprecord(struct sockbuf *sb)
   1078        1.1       cgd {
   1079       1.37     lukem 	struct mbuf	*m, *mn;
   1080        1.1       cgd 
   1081        1.1       cgd 	m = sb->sb_mb;
   1082        1.1       cgd 	if (m) {
   1083        1.1       cgd 		sb->sb_mb = m->m_nextpkt;
   1084        1.1       cgd 		do {
   1085        1.1       cgd 			sbfree(sb, m);
   1086        1.1       cgd 			MFREE(m, mn);
   1087       1.11  christos 		} while ((m = mn) != NULL);
   1088        1.1       cgd 	}
   1089       1.45   thorpej 	SB_EMPTY_FIXUP(sb);
   1090       1.19   thorpej }
   1091       1.19   thorpej 
   1092       1.19   thorpej /*
   1093       1.19   thorpej  * Create a "control" mbuf containing the specified data
   1094       1.19   thorpej  * with the specified type for presentation on a socket buffer.
   1095       1.19   thorpej  */
   1096       1.19   thorpej struct mbuf *
   1097       1.37     lukem sbcreatecontrol(caddr_t p, int size, int type, int level)
   1098       1.19   thorpej {
   1099       1.37     lukem 	struct cmsghdr	*cp;
   1100       1.37     lukem 	struct mbuf	*m;
   1101       1.19   thorpej 
   1102       1.35    itojun 	if (CMSG_SPACE(size) > MCLBYTES) {
   1103       1.30    itojun 		printf("sbcreatecontrol: message too large %d\n", size);
   1104       1.30    itojun 		return NULL;
   1105       1.30    itojun 	}
   1106       1.30    itojun 
   1107       1.19   thorpej 	if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
   1108       1.19   thorpej 		return ((struct mbuf *) NULL);
   1109       1.35    itojun 	if (CMSG_SPACE(size) > MLEN) {
   1110       1.30    itojun 		MCLGET(m, M_DONTWAIT);
   1111       1.30    itojun 		if ((m->m_flags & M_EXT) == 0) {
   1112       1.30    itojun 			m_free(m);
   1113       1.30    itojun 			return NULL;
   1114       1.30    itojun 		}
   1115       1.30    itojun 	}
   1116       1.19   thorpej 	cp = mtod(m, struct cmsghdr *);
   1117       1.26     perry 	memcpy(CMSG_DATA(cp), p, size);
   1118       1.35    itojun 	m->m_len = CMSG_SPACE(size);
   1119       1.35    itojun 	cp->cmsg_len = CMSG_LEN(size);
   1120       1.19   thorpej 	cp->cmsg_level = level;
   1121       1.19   thorpej 	cp->cmsg_type = type;
   1122       1.19   thorpej 	return (m);
   1123        1.1       cgd }
   1124